1010
1111from mautrix .types import (
1212 DeviceID ,
13+ DeviceKeys ,
1314 EncryptionAlgorithm ,
1415 EncryptionKeyAlgorithm ,
1516 IdentityKey ,
17+ KeyID ,
1618 SigningKey ,
1719 UserID ,
1820)
1921
20- from . import base
2122from .sessions import Session
23+ from .signature import sign_olm
2224
2325
2426class OlmAccount (olm .Account ):
@@ -74,19 +76,18 @@ def new_outbound_session(self, target_key: IdentityKey, one_time_key: IdentityKe
7476 session .pickle ("roundtrip" ), passphrase = "roundtrip" , creation_time = datetime .now ()
7577 )
7678
77- def get_device_keys (self , user_id : UserID , device_id : DeviceID ) -> Dict [str , Any ]:
78- device_keys = {
79- "user_id" : user_id ,
80- "device_id" : device_id ,
81- "algorithms" : [EncryptionAlgorithm .OLM_V1 .value , EncryptionAlgorithm .MEGOLM_V1 .value ],
82- "keys" : {
83- f"{ algorithm } :{ device_id } " : key for algorithm , key in self .identity_keys .items ()
79+ def get_device_keys (self , user_id : UserID , device_id : DeviceID ) -> DeviceKeys :
80+ device_keys = DeviceKeys (
81+ user_id = user_id ,
82+ device_id = device_id ,
83+ algorithms = [EncryptionAlgorithm .OLM_V1 , EncryptionAlgorithm .MEGOLM_V1 ],
84+ keys = {
85+ KeyID (algorithm = EncryptionKeyAlgorithm (algorithm ), key_id = key ): key
86+ for algorithm , key in self .identity_keys .items ()
8487 },
85- }
86- signature = self .sign (base .canonical_json (device_keys ))
87- device_keys ["signatures" ] = {
88- user_id : {f"{ EncryptionKeyAlgorithm .ED25519 } :{ device_id } " : signature }
89- }
88+ signatures = {},
89+ )
90+ device_keys .signatures [user_id ] = {KeyID .ed25519 (device_id ): sign_olm (device_keys , self )}
9091 return device_keys
9192
9293 def get_one_time_keys (
@@ -97,12 +98,12 @@ def get_one_time_keys(
9798 self .generate_one_time_keys (new_count )
9899 keys = {}
99100 for key_id , key in self .one_time_keys .get ("curve25519" , {}).items ():
100- signature = self .sign (base .canonical_json ({"key" : key }))
101- keys [f"{ EncryptionKeyAlgorithm .SIGNED_CURVE25519 } :{ key_id } " ] = {
101+ keys [str (KeyID .signed_curve25519 (IdentityKey (key_id )))] = {
102102 "key" : key ,
103103 "signatures" : {
104- user_id : {f"{ EncryptionKeyAlgorithm .ED25519 } :{ device_id } " : signature }
104+ user_id : {
105+ str (KeyID .ed25519 (device_id )): sign_olm ({"key" : key }, self ),
106+ }
105107 },
106108 }
107- self .mark_keys_as_published ()
108109 return keys
0 commit comments