summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--synapse/crypto/event_signing.py13
-rw-r--r--synapse/storage/__init__.py11
-rw-r--r--synapse/storage/event_federation.py2
3 files changed, 18 insertions, 8 deletions
diff --git a/synapse/crypto/event_signing.py b/synapse/crypto/event_signing.py
index 61edd2c6f9..07e383e221 100644
--- a/synapse/crypto/event_signing.py
+++ b/synapse/crypto/event_signing.py
@@ -16,11 +16,12 @@
 
 
 from synapse.federation.units import Pdu
-from synapse.api.events.utils import prune_pdu
+from synapse.api.events.utils import prune_pdu, prune_event
 from syutil.jsonutil import encode_canonical_json
 from syutil.base64util import encode_base64, decode_base64
 from syutil.crypto.jsonsign import sign_json, verify_signed_json
 
+import copy
 import hashlib
 import logging
 
@@ -69,6 +70,16 @@ def compute_pdu_event_reference_hash(pdu, hash_algorithm=hashlib.sha256):
     return (hashed.name, hashed.digest())
 
 
+def compute_event_reference_hash(event, hash_algorithm=hashlib.sha256):
+    tmp_event = copy.deepcopy(event)
+    tmp_event = prune_event(tmp_event)
+    event_json = tmp_event.get_dict()
+    event_json.pop("signatures", None)
+    event_json_bytes = encode_canonical_json(event_json)
+    hashed = hash_algorithm(event_json_bytes)
+    return (hashed.name, hashed.digest())
+
+
 def sign_event_pdu(pdu, signature_name, signing_key):
     tmp_pdu = Pdu(**pdu.get_dict())
     tmp_pdu = prune_pdu(tmp_pdu)
diff --git a/synapse/storage/__init__.py b/synapse/storage/__init__.py
index c2560f6045..31a0022d54 100644
--- a/synapse/storage/__init__.py
+++ b/synapse/storage/__init__.py
@@ -46,7 +46,7 @@ from .signatures import SignatureStore
 
 from syutil.base64util import decode_base64
 
-from synapse.crypto.event_signing import compute_pdu_event_reference_hash
+from synapse.crypto.event_signing import compute_event_reference_hash
 
 
 import json
@@ -271,11 +271,10 @@ class DataStore(RoomMemberStore, RoomStore,
                     txn, event.event_id, prev_event_id, alg, hash_bytes
                 )
 
-        # TODO
-        # (ref_alg, ref_hash_bytes) = compute_pdu_event_reference_hash(pdu)
-        # self._store_event_reference_hash_txn(
-        #    txn, event.event_id, ref_alg, ref_hash_bytes
-        # )
+        (ref_alg, ref_hash_bytes) = compute_event_reference_hash(event)
+        self._store_event_reference_hash_txn(
+            txn, event.event_id, ref_alg, ref_hash_bytes
+        )
 
         self._update_min_depth_for_room_txn(txn, event.room_id, event.depth)
 
diff --git a/synapse/storage/event_federation.py b/synapse/storage/event_federation.py
index 8357071db6..dcc116bad2 100644
--- a/synapse/storage/event_federation.py
+++ b/synapse/storage/event_federation.py
@@ -69,7 +69,7 @@ class EventFederationStore(SQLBaseStore):
 
         results = []
         for event_id, depth in txn.fetchall():
-            hashes = self._get_prev_event_hashes_txn(txn, event_id)
+            hashes = self._get_event_reference_hashes_txn(txn, event_id)
             prev_hashes = {
                 k: encode_base64(v) for k, v in hashes.items()
                 if k == "sha256"