33Resolution is performed using the IndyLedger class.
44"""
55
6- from typing import Any , Mapping , Pattern
6+ import logging
7+ from typing import Optional , Pattern
78
89from pydid import DID , DIDDocumentBuilder
910from pydid .verification_method import Ed25519VerificationKey2018 , VerificationMethod
2122
2223from ..base import BaseDIDResolver , DIDNotFound , ResolverError , ResolverType
2324
25+ LOGGER = logging .getLogger (__name__ )
26+
2427
2528class NoIndyLedger (ResolverError ):
2629 """Raised when there is no Indy ledger instance configured."""
@@ -46,60 +49,83 @@ def supported_did_regex(self) -> Pattern:
4649 """Return supported_did_regex of Indy DID Resolver."""
4750 return IndyDID .PATTERN
4851
49- def _add_endpoint_as_endpoint_value_pair (
50- self ,
51- builder : DIDDocumentBuilder ,
52- endpoint : str ,
53- recipient_key : VerificationMethod ,
54- ):
55- builder .service .add_didcomm (
56- ident = self .SERVICE_TYPE_DID_COMMUNICATION ,
57- type_ = self .SERVICE_TYPE_DID_COMMUNICATION ,
58- service_endpoint = endpoint ,
59- priority = 1 ,
60- recipient_keys = [recipient_key ],
61- routing_keys = [],
62- )
63-
64- def _add_endpoint_as_map (
52+ def process_endpoint_types (self , types ):
53+ """Process endpoint types to return only expected types,
54+ subset of expected types, or default types.
55+ """
56+ expected_types = ["endpoint" , "did-communication" , "DIDComm" ]
57+ default_types = ["endpoint" , "did-communication" ]
58+ if len (types ) <= 0 :
59+ return default_types
60+ for type in types :
61+ if type not in expected_types :
62+ return default_types
63+ return types
64+
65+ def add_services (
6566 self ,
6667 builder : DIDDocumentBuilder ,
67- endpoint : Mapping [ str , Any ],
68- recipient_key : VerificationMethod ,
68+ endpoints : Optional [ dict ],
69+ recipient_key : VerificationMethod = None ,
6970 ):
70- types = endpoint .get ("types" , [self .SERVICE_TYPE_DID_COMMUNICATION ])
71- routing_keys = endpoint .get ("routingKeys" , [])
72- endpoint_url = endpoint .get ("endpoint" )
73- if not endpoint_url :
74- raise ValueError ("endpoint url not found in endpoint attrib" )
75-
76- if self .SERVICE_TYPE_DIDCOMM in types :
77- builder .service .add (
78- ident = "#didcomm-1" ,
79- type_ = self .SERVICE_TYPE_DIDCOMM ,
80- service_endpoint = endpoint_url ,
81- recipient_keys = [recipient_key .id ],
82- routing_keys = routing_keys ,
83- accept = ["didcomm/v2" ],
84- )
85- builder .context .append (self .CONTEXT_DIDCOMM_V2 )
86- if self .SERVICE_TYPE_DID_COMMUNICATION in types :
87- builder .service .add (
88- ident = "did-communication" ,
89- type_ = self .SERVICE_TYPE_DID_COMMUNICATION ,
90- service_endpoint = endpoint_url ,
91- priority = 1 ,
92- routing_keys = routing_keys ,
93- recipient_keys = [recipient_key .id ],
94- accept = ["didcomm/aip2;env=rfc19" ],
95- )
96- if self .SERVICE_TYPE_ENDPOINT in types :
97- builder .service .add (
98- ident = "endpoint" ,
99- service_endpoint = endpoint_url ,
100- type_ = self .SERVICE_TYPE_ENDPOINT ,
71+ """Add services."""
72+ if not endpoints :
73+ return
74+
75+ endpoint = endpoints .get ("endpoint" )
76+ routing_keys = endpoints .get ("routingKeys" , [])
77+ types = endpoints .get ("types" , [self .SERVICE_TYPE_DID_COMMUNICATION ])
78+
79+ other_endpoints = {
80+ key : endpoints [key ]
81+ for key in ("profile" , "linked_domains" )
82+ if key in endpoints
83+ }
84+
85+ if endpoint :
86+ processed_types = self .process_endpoint_types (types )
87+
88+ if self .SERVICE_TYPE_ENDPOINT in processed_types :
89+ builder .service .add (
90+ ident = "endpoint" ,
91+ service_endpoint = endpoint ,
92+ type_ = self .SERVICE_TYPE_ENDPOINT ,
93+ )
94+
95+ if self .SERVICE_TYPE_DID_COMMUNICATION in processed_types :
96+ builder .service .add (
97+ ident = "did-communication" ,
98+ type_ = self .SERVICE_TYPE_DID_COMMUNICATION ,
99+ service_endpoint = endpoint ,
100+ priority = 1 ,
101+ routing_keys = routing_keys ,
102+ recipient_keys = [recipient_key .id ],
103+ accept = ["didcomm/aip2;env=rfc19" ],
104+ )
105+
106+ if self .SERVICE_TYPE_DIDCOMM in types :
107+ builder .service .add (
108+ ident = "#didcomm-1" ,
109+ type_ = self .SERVICE_TYPE_DIDCOMM ,
110+ service_endpoint = endpoint ,
111+ recipient_keys = [recipient_key .id ],
112+ routing_keys = routing_keys ,
113+ accept = ["didcomm/v2" ],
114+ )
115+ builder .context .append (self .CONTEXT_DIDCOMM_V2 )
116+ else :
117+ LOGGER .warning (
118+ "No endpoint for DID although endpoint attrib was resolvable"
101119 )
102120
121+ if other_endpoints :
122+ for type_ , endpoint in other_endpoints .items ():
123+ builder .service .add (
124+ ident = type_ ,
125+ type_ = EndpointType .get (type_ ).w3c ,
126+ service_endpoint = endpoint ,
127+ )
128+
103129 async def _resolve (self , profile : Profile , did : str ) -> dict :
104130 """Resolve an indy DID."""
105131 multitenant_mgr = profile .inject_or (BaseMultitenantManager )
@@ -119,7 +145,7 @@ async def _resolve(self, profile: Profile, did: str) -> dict:
119145 try :
120146 async with ledger :
121147 recipient_key = await ledger .get_key_for_did (did )
122- endpoints = await ledger .get_all_endpoints_for_did (did )
148+ endpoints : Optional [ dict ] = await ledger .get_all_endpoints_for_did (did )
123149 except LedgerError as err :
124150 raise DIDNotFound (f"DID { did } could not be resolved" ) from err
125151
@@ -130,22 +156,7 @@ async def _resolve(self, profile: Profile, did: str) -> dict:
130156 )
131157 builder .authentication .reference (vmethod .id )
132158 builder .assertion_method .reference (vmethod .id )
133- if endpoints :
134- for type_ , endpoint in endpoints .items ():
135- if type_ == EndpointType .ENDPOINT .indy :
136- if isinstance (endpoint , dict ):
137- self ._add_endpoint_as_map (builder , endpoint , vmethod )
138- else :
139- self ._add_endpoint_as_endpoint_value_pair (
140- builder , endpoint , vmethod
141- )
142- else :
143- # Accept all service types for now, i.e. profile, linked_domains
144- builder .service .add (
145- ident = type_ ,
146- type_ = type_ ,
147- service_endpoint = endpoint ,
148- )
159+ self .add_services (builder , endpoints , vmethod )
149160
150161 result = builder .build ()
151162 return result .serialize ()
0 commit comments