From 718e74cd77d6797578c57789dd08d573771f1015 Mon Sep 17 00:00:00 2001 From: Varun Chatterji Date: Sun, 8 Oct 2023 01:09:22 +0800 Subject: [PATCH 1/4] Fixed: https://github.com/hyperledger/aries-cloudagent-python/issues/2531 Signed-off-by: Varun Chatterji --- .../resolver/default/legacy_peer.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/aries_cloudagent/resolver/default/legacy_peer.py b/aries_cloudagent/resolver/default/legacy_peer.py index 3027df1b71..52ec422ad8 100644 --- a/aries_cloudagent/resolver/default/legacy_peer.py +++ b/aries_cloudagent/resolver/default/legacy_peer.py @@ -135,7 +135,17 @@ 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): + # 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: @@ -150,9 +160,9 @@ def didcomm_services_recip_keys_are_refs_routing_keys_are_did_key( ] if "routingKeys" in service: service["routingKeys"] = [ - DIDKey.from_public_key_b58(key, ED25519).key_id + cls.did_key_to_did_key_ref(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 +245,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) From 58fd7c039a6fa9073a8938c4fb3284c35a1f1dc3 Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Sat, 7 Oct 2023 15:38:20 -0400 Subject: [PATCH 2/4] style: minor style correction Signed-off-by: Daniel Bluhm --- aries_cloudagent/resolver/default/legacy_peer.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/aries_cloudagent/resolver/default/legacy_peer.py b/aries_cloudagent/resolver/default/legacy_peer.py index 52ec422ad8..aee9840b2f 100644 --- a/aries_cloudagent/resolver/default/legacy_peer.py +++ b/aries_cloudagent/resolver/default/legacy_peer.py @@ -135,7 +135,8 @@ def recip_base58_to_ref(vms: List[dict], recip: str) -> str: return recip @classmethod - def did_key_to_did_key_ref(cls, key:str): + 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 @@ -160,7 +161,9 @@ def didcomm_services_recip_keys_are_refs_routing_keys_are_did_key_ref( ] if "routingKeys" in service: service["routingKeys"] = [ - cls.did_key_to_did_key_ref(DIDKey.from_public_key_b58(key, ED25519).key_id) + cls.did_key_to_did_key_ref( + DIDKey.from_public_key_b58(key, ED25519).key_id + ) if "did:key:" not in key else cls.did_key_to_did_key_ref(key) for key in service["routingKeys"] From 7e5230526f26e64faad6cbaebdee65992fc3da74 Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Sat, 7 Oct 2023 16:20:19 -0400 Subject: [PATCH 3/4] test: did:key to did:key ref Signed-off-by: Daniel Bluhm --- .../default/tests/test_legacy_peer.py | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) 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) From d90102845c87840b4bbc084844ecfdede5f192af Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Sat, 7 Oct 2023 16:24:41 -0400 Subject: [PATCH 4/4] fix: remove redundant transform step Signed-off-by: Daniel Bluhm --- aries_cloudagent/resolver/default/legacy_peer.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/aries_cloudagent/resolver/default/legacy_peer.py b/aries_cloudagent/resolver/default/legacy_peer.py index aee9840b2f..3b012adede 100644 --- a/aries_cloudagent/resolver/default/legacy_peer.py +++ b/aries_cloudagent/resolver/default/legacy_peer.py @@ -161,9 +161,7 @@ def didcomm_services_recip_keys_are_refs_routing_keys_are_did_key_ref( ] if "routingKeys" in service: service["routingKeys"] = [ - cls.did_key_to_did_key_ref( - DIDKey.from_public_key_b58(key, ED25519).key_id - ) + DIDKey.from_public_key_b58(key, ED25519).key_id if "did:key:" not in key else cls.did_key_to_did_key_ref(key) for key in service["routingKeys"]