diff --git a/aries_cloudagent/resolver/default/legacy_peer.py b/aries_cloudagent/resolver/default/legacy_peer.py index 3027df1b71..3b012adede 100644 --- a/aries_cloudagent/resolver/default/legacy_peer.py +++ b/aries_cloudagent/resolver/default/legacy_peer.py @@ -135,7 +135,18 @@ def recip_base58_to_ref(vms: List[dict], recip: str) -> str: return recip @classmethod - def didcomm_services_recip_keys_are_refs_routing_keys_are_did_key( + def did_key_to_did_key_ref(cls, key: str): + """Convert did:key to did:key ref.""" + # Check if key is already a ref + if key.rfind("#") != -1: + return key + # Get the value after removing did:key: + value = key.replace("did:key:", "") + + return key + "#" + value + + @classmethod + def didcomm_services_recip_keys_are_refs_routing_keys_are_did_key_ref( cls, value: dict, ) -> dict: @@ -152,7 +163,7 @@ def didcomm_services_recip_keys_are_refs_routing_keys_are_did_key( service["routingKeys"] = [ DIDKey.from_public_key_b58(key, ED25519).key_id if "did:key:" not in key - else key + else cls.did_key_to_did_key_ref(key) for key in service["routingKeys"] ] return value @@ -235,7 +246,7 @@ def apply(cls, value: dict) -> dict: cls.fully_qualified_ids_and_controllers, cls.didcomm_services_use_updated_conventions, cls.remove_routing_keys_from_verification_method, - cls.didcomm_services_recip_keys_are_refs_routing_keys_are_did_key, + cls.didcomm_services_recip_keys_are_refs_routing_keys_are_did_key_ref, ): value = correction(value) diff --git a/aries_cloudagent/resolver/default/tests/test_legacy_peer.py b/aries_cloudagent/resolver/default/tests/test_legacy_peer.py index e55b040e6c..3f21da0657 100644 --- a/aries_cloudagent/resolver/default/tests/test_legacy_peer.py +++ b/aries_cloudagent/resolver/default/tests/test_legacy_peer.py @@ -499,3 +499,74 @@ def test_corrections(self, input_doc: dict, expected: dict): doc = pydid.deserialize_document(actual) assert doc.service assert isinstance(doc.service[0], pydid.DIDCommService) + + @pytest.mark.parametrize( + ("input_doc", "expected"), + [ + ( + { + "@context": "https://w3id.org/did/v1", + "id": "StwSYX1WFcJ7MBfYWxmuQ9", + "publicKey": [ + { + "type": "Ed25519VerificationKey2018", + "id": "StwSYX1WFcJ7MBfYWxmuQ9#1", + "controller": "StwSYX1WFcJ7MBfYWxmuQ9", + "publicKeyBase58": "F7cEyTgzUbFwHsTwC2cK2Zy8bdraeoMY8921gyDmefwK", + } + ], + "authentication": [ + { + "type": "Ed25519VerificationKey2018", + "publicKey": "StwSYX1WFcJ7MBfYWxmuQ9#1", + } + ], + "service": [ + { + "type": "IndyAgent", + "id": "StwSYX1WFcJ7MBfYWxmuQ9#IndyAgentService", + "serviceEndpoint": "https://example.com/endpoint", + "recipientKeys": [ + "F7cEyTgzUbFwHsTwC2cK2Zy8bdraeoMY8921gyDmefwK" + ], + "routingKeys": [ + "did:key:z6Mko2LnynhGbkPQdZ3PQBUgCmrzdH9aJe7HTs4LKontx8Ge" + ], + } + ], + }, + { + "@context": "https://w3id.org/did/v1", + "id": "did:sov:StwSYX1WFcJ7MBfYWxmuQ9", + "verificationMethod": [ + { + "type": "Ed25519VerificationKey2018", + "id": "did:sov:StwSYX1WFcJ7MBfYWxmuQ9#1", + "controller": "did:sov:StwSYX1WFcJ7MBfYWxmuQ9", + "publicKeyBase58": "F7cEyTgzUbFwHsTwC2cK2Zy8bdraeoMY8921gyDmefwK", + } + ], + "authentication": ["did:sov:StwSYX1WFcJ7MBfYWxmuQ9#1"], + "service": [ + { + "id": "did:sov:StwSYX1WFcJ7MBfYWxmuQ9#didcomm-0", + "type": "did-communication", + "serviceEndpoint": "https://example.com/endpoint", + "recipientKeys": ["did:sov:StwSYX1WFcJ7MBfYWxmuQ9#1"], + "routingKeys": [ + "did:key:z6Mko2LnynhGbkPQdZ3PQBUgCmrzdH9aJe7HTs4LKontx8Ge#z6Mko2LnynhGbkPQdZ3PQBUgCmrzdH9aJe7HTs4LKontx8Ge" + ], + } + ], + }, + ) + ], + ) + def test_corrections_on_doc_as_received(self, input_doc: dict, expected: dict): + parsed = DIDDoc.deserialize(input_doc) + actual = test_module.LegacyDocCorrections.apply(parsed.serialize()) + assert actual == expected + assert expected == test_module.LegacyDocCorrections.apply(expected) + doc = pydid.deserialize_document(actual) + assert doc.service + assert isinstance(doc.service[0], pydid.DIDCommService)