|
1 | 1 | # -*- coding: utf-8 -*- |
2 | | - |
| 2 | +# |
3 | 3 | # Copyright 2014-2016 OpenMarket Ltd |
| 4 | +# Copyright 2020 The Matrix.org Foundation C.I.C. |
4 | 5 | # |
5 | 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
6 | 7 | # you may not use this file except in compliance with the License. |
|
17 | 18 | import collections.abc |
18 | 19 | import hashlib |
19 | 20 | import logging |
| 21 | +from typing import Dict |
20 | 22 |
|
21 | 23 | from canonicaljson import encode_canonical_json |
22 | 24 | from signedjson.sign import sign_json |
@@ -115,18 +117,28 @@ def compute_event_reference_hash(event, hash_algorithm=hashlib.sha256): |
115 | 117 | return hashed.name, hashed.digest() |
116 | 118 |
|
117 | 119 |
|
118 | | -def compute_event_signature(event_dict, signature_name, signing_key): |
| 120 | +def compute_event_signature( |
| 121 | + room_version: RoomVersion, |
| 122 | + event_dict: JsonDict, |
| 123 | + signature_name: str, |
| 124 | + signing_key: SigningKey, |
| 125 | +) -> Dict[str, Dict[str, str]]: |
119 | 126 | """Compute the signature of the event for the given name and key. |
120 | 127 |
|
121 | 128 | Args: |
122 | | - event_dict (dict): The event as a dict |
123 | | - signature_name (str): The name of the entity signing the event |
| 129 | + room_version: the version of the room that this event is in. |
| 130 | + (the room version determines the redaction algorithm and hence the |
| 131 | + json to be signed) |
| 132 | +
|
| 133 | + event_dict: The event as a dict |
| 134 | +
|
| 135 | + signature_name: The name of the entity signing the event |
124 | 136 | (typically the server's hostname). |
125 | | - signing_key (syutil.crypto.SigningKey): The key to sign with |
| 137 | +
|
| 138 | + signing_key: The key to sign with |
126 | 139 |
|
127 | 140 | Returns: |
128 | | - dict[str, dict[str, str]]: Returns a dictionary in the same format of |
129 | | - an event's signatures field. |
| 141 | + a dictionary in the same format of an event's signatures field. |
130 | 142 | """ |
131 | 143 | redact_json = prune_event_dict(event_dict) |
132 | 144 | redact_json.pop("age_ts", None) |
@@ -161,5 +173,5 @@ def add_hashes_and_signatures( |
161 | 173 | event_dict.setdefault("hashes", {})[name] = encode_base64(digest) |
162 | 174 |
|
163 | 175 | event_dict["signatures"] = compute_event_signature( |
164 | | - event_dict, signature_name=signature_name, signing_key=signing_key |
| 176 | + room_version, event_dict, signature_name=signature_name, signing_key=signing_key |
165 | 177 | ) |
0 commit comments