33from dataclasses import dataclass
44import json
55import uuid
6- from typing import Generic , Optional , Sequence , Union
6+ from typing import Any , Generic , Optional , Sequence , Union
77
88from pydantic import AnyUrl
9- from pydid import DIDDocument , VerificationMethod
9+ from pydid import DIDDocument , DIDUrl , VerificationMethod
1010from pydid .service import DIDCommV1Service
1111
1212from didcomm_messaging .crypto import P , S , SecretsManager
@@ -58,14 +58,35 @@ class Target:
5858class V1DIDCommMessagingService (Generic [P , S ]):
5959 """Main entrypoint for DIDComm Messaging."""
6060
61- def vm_to_v1_kid (self , crypto : V1CryptoService , doc : DIDDocument , ref : str ) -> str :
61+ def local_vm_ref_to_v1_kid (
62+ self , crypto : V1CryptoService , doc : DIDDocument , ref : str
63+ ) -> str :
6264 """Convert a verification method ref to a DIDComm v1 kid."""
6365 return crypto .public_key_to_v1_kid (
6466 crypto .verification_method_to_public_key (
6567 doc .dereference_as (VerificationMethod , ref )
6668 )
6769 )
6870
71+ async def routing_key_to_kid (
72+ self ,
73+ crypto : V1CryptoService [P , S ],
74+ resolver : DIDResolver ,
75+ doc : DIDDocument ,
76+ routing_key : DIDUrl ,
77+ ) -> str :
78+ """Resolve routing key to a kid."""
79+ if routing_key .did is None or routing_key .did == doc .id :
80+ return self .local_vm_ref_to_v1_kid (crypto , doc , routing_key )
81+
82+ assert routing_key .did != doc .id
83+ routing_key_vm = await resolver .resolve_and_dereference_verification_method (
84+ routing_key .did
85+ )
86+ return crypto .public_key_to_v1_kid (
87+ crypto .verification_method_to_public_key (routing_key_vm )
88+ )
89+
6990 async def did_to_target (
7091 self , crypto : V1CryptoService [P , S ], resolver : DIDResolver , did : str
7192 ) -> Target :
@@ -81,19 +102,11 @@ async def did_to_target(
81102 target = services [0 ]
82103
83104 recipient_keys = [
84- self .vm_to_v1_kid (crypto , doc , recip ) for recip in target .recipient_keys
105+ self .local_vm_ref_to_v1_kid (crypto , doc , recip )
106+ for recip in target .recipient_keys
85107 ]
86108 routing_keys = [
87- crypto .public_key_to_v1_kid (
88- crypto .verification_method_to_public_key (
89- VerificationMethod (
90- id = routing_key ,
91- type = "Ed25519VerificationKey2020" ,
92- controller = routing_key .split ("#" )[0 ],
93- public_key_multibase = routing_key .split ("#" )[1 ],
94- )
95- )
96- )
109+ await self .routing_key_to_kid (crypto , resolver , doc , routing_key )
97110 for routing_key in target .routing_keys
98111 ]
99112 endpoint = target .service_endpoint
@@ -121,11 +134,12 @@ async def from_did_to_kid(
121134 target = services [0 ]
122135
123136 recipient_keys = [
124- self .vm_to_v1_kid (crypto , doc , recip ) for recip in target .recipient_keys
137+ self .local_vm_ref_to_v1_kid (crypto , doc , recip )
138+ for recip in target .recipient_keys
125139 ]
126140 return recipient_keys [0 ]
127141
128- def forward_wrap (self , to : str , msg : str ) -> bytes :
142+ def forward_wrap (self , to : str , msg : dict ) -> bytes :
129143 """Wrap a message in a forward."""
130144 forward = {
131145 "@id" : str (uuid .uuid4 ()),
@@ -141,7 +155,7 @@ async def pack(
141155 resolver : DIDResolver ,
142156 secrets : SecretsManager [S ],
143157 packaging : V1PackagingService [P , S ],
144- message : Union [dict , str , bytes ],
158+ message : Union [dict , str , bytes , Any ],
145159 to : Union [str , Target ],
146160 frm : Optional [str ] = None ,
147161 ** options ,
0 commit comments