async def store_did_document(self, did_doc: DIDDoc): """Store a DID document. Args: did_doc: The `DIDDoc` instance to be persisted """ assert did_doc.did storage: BaseStorage = await self.context.inject(BaseStorage) try: stored_doc, record = await self.fetch_did_document(did_doc.did) except StorageNotFoundError: record = StorageRecord(self.RECORD_TYPE_DID_DOC, did_doc.to_json(), {"did": did_doc.did}) await storage.add_record(record) else: await storage.update_record_value(record, did_doc.to_json()) await self.remove_keys_for_did(did_doc.did) for key in did_doc.pubkey.values(): if key.controller == did_doc.did: await self.add_key_for_did(did_doc.did, key.value)
async def fetch_did_document(self, did: str) -> Tuple[DIDDoc, StorageRecord]: """Retrieve a DID Document for a given DID. Args: did: The DID to search for """ storage: BaseStorage = await self.context.inject(BaseStorage) record = await storage.search_records(self.RECORD_TYPE_DID_DOC, { "did": did }).fetch_single() return DIDDoc.from_json(record.value), record
def _deserialize(self, value, attr, data, **kwargs): """ Deserialize a value into a DIDDoc. Args: value: The value to deserialize Returns: The deserialized value """ return DIDDoc.deserialize(value)
def make_did_doc(self): doc = DIDDoc(did=self.test_did) controller = self.test_did ident = "1" pk_value = self.test_verkey pk = PublicKey( self.test_did, ident, pk_value, PublicKeyType.ED25519_SIG_2018, controller, False, ) doc.set(pk) recip_keys = [pk] routing_keys = [] service = Service( self.test_did, "indy", "IndyAgent", recip_keys, routing_keys, self.test_endpoint, ) doc.set(service) return doc
def did_doc(): doc = DIDDoc(did=TEST_DID) controller = TEST_DID ident = "1" pk_value = TEST_VERKEY pk = PublicKey( TEST_DID, ident, pk_value, PublicKeyType.ED25519_SIG_2018, controller, False, ) doc.set(pk) recip_keys = [pk] router_keys = [] service = Service( TEST_DID, "indy", "IndyAgent", recip_keys, router_keys, TEST_ENDPOINT, ) doc.set(service) yield doc
async def test_make_model_conn_detail_interpolate_authn_service(self): did_doc_dict = self.make_did_doc().serialize() del did_doc_dict["authentication"] del did_doc_dict["service"] did_doc = DIDDoc.deserialize(did_doc_dict) connection_request = ConnectionRequest( connection=ConnectionDetail(did=self.test_did, did_doc=did_doc), label=self.test_label, ) data = connection_request.serialize() model_instance = ConnectionRequest.deserialize(data) assert type(model_instance) is type(connection_request)
async def handle(self, context: RequestContext, responder: BaseResponder): """Handle static connection creation request.""" connection_mgr = ConnectionManager(context) wallet: BaseWallet = await context.inject(BaseWallet) # Make our info for the connection my_info = await wallet.create_local_did() # Create connection record connection = ConnectionRecord( initiator=ConnectionRecord.INITIATOR_SELF, my_did=my_info.did, their_did=context.message.static_did, their_label=context.message.label, their_role=context.message.role if context.message.role else None, state=ConnectionRecord.STATE_ACTIVE, invitation_mode=ConnectionRecord.INVITATION_MODE_STATIC ) # Construct their did doc from the basic components in message diddoc = DIDDoc(context.message.static_did) public_key = PublicKey( did=context.message.static_did, ident="1", value=context.message.static_key, pk_type=PublicKeyType.ED25519_SIG_2018, controller=context.message.static_did ) service = Service( did=context.message.static_did, ident="indy", typ="IndyAgent", recip_keys=[public_key], routing_keys=[], endpoint=context.message.static_endpoint ) diddoc.set(public_key) diddoc.set(service) # Save await connection_mgr.store_did_document(diddoc) await connection.save( context, reason='Created new static connection' ) # Prepare response info = StaticConnectionInfo( did=my_info.did, key=my_info.verkey, endpoint=context.settings.get("default_endpoint") ) info.assign_thread_from(context.message) await responder.send_reply(info) return
def make_did_doc(self, did, verkey): doc = DIDDoc(did=did) controller = did ident = "1" pk_value = verkey pk = PublicKey(did, ident, pk_value, PublicKeyType.ED25519_SIG_2018, controller, False) doc.set(pk) recip_keys = [pk] router_keys = [] service = Service(did, "indy", "IndyAgent", recip_keys, router_keys, TestConfig.test_endpoint) doc.set(service) return doc
async def create_did_document( self, did_info: DIDInfo, inbound_connection_id: str = None, svc_endpoints: Sequence[str] = [], ) -> DIDDoc: """Create our DID document for a given DID. Args: did_info: The DID information (DID and verkey) used in the connection inbound_connection_id: The ID of the inbound routing connection to use svc_endpoints: Custom endpoints for the DID Document Returns: The prepared `DIDDoc` instance """ did_doc = DIDDoc(did=did_info.did) did_controller = did_info.did did_key = did_info.verkey pk = PublicKey( did_info.did, "1", did_key, PublicKeyType.ED25519_SIG_2018, did_controller, True, ) did_doc.set(pk) router_id = inbound_connection_id routing_keys = [] router_idx = 1 while router_id: # look up routing connection information router = await ConnectionRecord.retrieve_by_id( self.context, router_id) if router.state != ConnectionRecord.STATE_ACTIVE: raise ConnectionManagerError( f"Router connection not active: {router_id}") routing_doc, _ = await self.fetch_did_document(router.their_did) if not routing_doc.service: raise ConnectionManagerError( f"No services defined by routing DIDDoc: {router_id}") for service in routing_doc.service.values(): if not service.endpoint: raise ConnectionManagerError( "Routing DIDDoc service has no service endpoint") if not service.recip_keys: raise ConnectionManagerError( "Routing DIDDoc service has no recipient key(s)") rk = PublicKey( did_info.did, f"routing-{router_idx}", service.recip_keys[0].value, PublicKeyType.ED25519_SIG_2018, did_controller, True, ) routing_keys.append(rk) svc_endpoints = [service.endpoint] break router_id = router.inbound_connection_id for endpoint_index, svc_endpoint in enumerate(svc_endpoints): endpoint_ident = "indy" if endpoint_index == 0 else f"indy{endpoint_index}" service = Service( did_info.did, endpoint_ident, "IndyAgent", [pk], routing_keys, svc_endpoint, ) did_doc.set(service) return did_doc