From 9a305dec4cc54659bc70b233a5b5a08eb2e2fa2e Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Thu, 21 Jul 2022 10:37:58 -0600 Subject: [PATCH 01/27] fix: Switch from using Raw public keys to using DID Keys for Mediation This is work for #1859 Signed-off-by: Colton Wolkins (Indicio work address) --- .../coordinate_mediation/v1_0/messages/inner/keylist_key.py | 4 ++-- .../v1_0/messages/inner/keylist_update_rule.py | 4 ++-- .../v1_0/messages/inner/keylist_updated.py | 4 ++-- .../coordinate_mediation/v1_0/models/mediation_record.py | 4 ++-- .../coordinate_mediation/v1_0/tests/test_mediation_manager.py | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py index aa7701ff5a..45a1df48c3 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py @@ -3,7 +3,7 @@ from marshmallow import EXCLUDE, fields from ......messaging.models.base import BaseModel, BaseModelSchema -from ......messaging.valid import INDY_RAW_PUBLIC_KEY +from ......messaging.valid import DID_KEY class KeylistKey(BaseModel): @@ -44,4 +44,4 @@ class Meta: model_class = KeylistKey unknown = EXCLUDE - recipient_key = fields.Str(required=True, **INDY_RAW_PUBLIC_KEY) + recipient_key = fields.Str(required=True, **DID_KEY) diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py index 6ece696537..c4dd529935 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py @@ -8,7 +8,7 @@ from marshmallow.validate import OneOf from ......messaging.models.base import BaseModel, BaseModelSchema -from ......messaging.valid import INDY_RAW_PUBLIC_KEY +from ......messaging.valid import DID_KEY class KeylistUpdateRule(BaseModel): @@ -45,7 +45,7 @@ class Meta: model_class = KeylistUpdateRule recipient_key = fields.Str( - description="Key to remove or add", required=True, **INDY_RAW_PUBLIC_KEY + description="Key to remove or add", required=True, **DID_KEY ) action = fields.Str( required=True, diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py index ac354e45d1..1fe36c8044 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py @@ -6,7 +6,7 @@ from marshmallow import EXCLUDE, fields from ......messaging.models.base import BaseModel, BaseModelSchema -from ......messaging.valid import INDY_RAW_PUBLIC_KEY +from ......messaging.valid import DID_KEY class KeylistUpdated(BaseModel): @@ -54,6 +54,6 @@ class Meta: model_class = KeylistUpdated unknown = EXCLUDE - recipient_key = fields.Str(required=True, **INDY_RAW_PUBLIC_KEY) + recipient_key = fields.Str(required=True, **DID_KEY) action = fields.Str(required=True) result = fields.Str(required=True) diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/models/mediation_record.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/models/mediation_record.py index 0bfda79c7f..f7801a45c5 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/models/mediation_record.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/models/mediation_record.py @@ -6,7 +6,7 @@ from .....core.profile import ProfileSession from .....messaging.models.base_record import BaseRecord, BaseRecordSchema -from .....messaging.valid import INDY_RAW_PUBLIC_KEY +from .....messaging.valid import DID_KEY from .....storage.base import StorageDuplicateError, StorageNotFoundError @@ -172,5 +172,5 @@ class Meta: connection_id = fields.Str(required=True) mediator_terms = fields.List(fields.Str(), required=False) recipient_terms = fields.List(fields.Str(), required=False) - routing_keys = fields.List(fields.Str(**INDY_RAW_PUBLIC_KEY), required=False) + routing_keys = fields.List(fields.Str(**DID_KEY), required=False) endpoint = fields.Str(required=False) diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_mediation_manager.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_mediation_manager.py index f3a03c0a30..788404feb8 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_mediation_manager.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_mediation_manager.py @@ -28,8 +28,8 @@ TEST_CONN_ID = "conn-id" TEST_THREAD_ID = "thread-id" TEST_ENDPOINT = "https://example.com" -TEST_VERKEY = "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx" -TEST_ROUTE_VERKEY = "9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC" +TEST_VERKEY = "did:key:z3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx" +TEST_ROUTE_VERKEY = "did:key:z9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC" pytestmark = pytest.mark.asyncio From 495584eef87390746f06d20fac6c0824e637abe2 Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Thu, 21 Jul 2022 11:15:37 -0600 Subject: [PATCH 02/27] fix: Send keys back in did:key format during response Signed-off-by: Colton Wolkins (Indicio work address) --- .../coordinate_mediation/v1_0/messages/mediate_grant.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py index b8616090dc..692f3892e8 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py @@ -41,7 +41,7 @@ def __init__( """ super(MediationGrant, self).__init__(**kwargs) self.endpoint = endpoint - self.routing_keys = list(routing_keys) if routing_keys else [] + self.routing_keys = list(f"did:key:z{key}" for key in routing_keys) if routing_keys else [] class MediationGrantSchema(AgentMessageSchema): From 0975af6091a2e38625c91ef89fd5082ecbddec0c Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Tue, 26 Jul 2022 09:21:36 -0600 Subject: [PATCH 03/27] fix: Fix the way routing DIDs are being created/sent Signed-off-by: Colton Wolkins (Indicio work address) --- .../protocols/coordinate_mediation/v1_0/manager.py | 2 +- .../coordinate_mediation/v1_0/messages/mediate_grant.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py index 195dd5fd5a..6a5af46957 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py @@ -183,7 +183,7 @@ async def grant_request( await mediation_record.save(session, reason="Mediation request granted") grant = MediationGrant( endpoint=session.settings.get("default_endpoint"), - routing_keys=[routing_did.verkey], + routing_keys=[routing_did.did], ) return mediation_record, grant diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py index 692f3892e8..c18a2d132d 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py @@ -8,6 +8,8 @@ from marshmallow import fields from .....messaging.agent_message import AgentMessage, AgentMessageSchema +from .....did.did_key import DIDKey +from .....wallet.key_type import KeyType from ..message_types import MEDIATE_GRANT, PROTOCOL_PACKAGE HANDLER_CLASS = ( @@ -41,7 +43,7 @@ def __init__( """ super(MediationGrant, self).__init__(**kwargs) self.endpoint = endpoint - self.routing_keys = list(f"did:key:z{key}" for key in routing_keys) if routing_keys else [] + self.routing_keys = list(f"did:key:z{DIDKey.from_public_key_b58(key, KeyType.ED25519).did}" for key in routing_keys) if routing_keys else [] class MediationGrantSchema(AgentMessageSchema): From d0b19d9ad6715e671940d0008c958ecbf4e7289e Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Tue, 26 Jul 2022 10:04:02 -0600 Subject: [PATCH 04/27] fix: Reformat did during grant Signed-off-by: Colton Wolkins (Indicio work address) --- .../protocols/coordinate_mediation/v1_0/manager.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py index 6a5af46957..c96358b6a1 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py @@ -13,6 +13,7 @@ from ....wallet.did_method import DIDMethod from ....wallet.base import BaseWallet from ....wallet.did_info import DIDInfo +from ....did.did_key import DIDKey from ...routing.v1_0.manager import RoutingManager from ...routing.v1_0.models.route_record import RouteRecord @@ -180,6 +181,8 @@ async def grant_request( mediation_record.state = MediationRecord.STATE_GRANTED + routing_did = DIDKey.from_public_key_b58(routing_did.verkey, routing_did.key_type) + await mediation_record.save(session, reason="Mediation request granted") grant = MediationGrant( endpoint=session.settings.get("default_endpoint"), From 7ff2c0cc63d27c25d76a7c7dfa28973f1e56a309 Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Tue, 26 Jul 2022 10:27:51 -0600 Subject: [PATCH 05/27] Revert "fix: Reformat did during grant" This reverts commit d0b19d9ad6715e671940d0008c958ecbf4e7289e. Signed-off-by: Colton Wolkins (Indicio work address) --- .../protocols/coordinate_mediation/v1_0/manager.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py index c96358b6a1..6a5af46957 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py @@ -13,7 +13,6 @@ from ....wallet.did_method import DIDMethod from ....wallet.base import BaseWallet from ....wallet.did_info import DIDInfo -from ....did.did_key import DIDKey from ...routing.v1_0.manager import RoutingManager from ...routing.v1_0.models.route_record import RouteRecord @@ -181,8 +180,6 @@ async def grant_request( mediation_record.state = MediationRecord.STATE_GRANTED - routing_did = DIDKey.from_public_key_b58(routing_did.verkey, routing_did.key_type) - await mediation_record.save(session, reason="Mediation request granted") grant = MediationGrant( endpoint=session.settings.get("default_endpoint"), From 1ad866da13794980ff060377349281b9c9378ced Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Tue, 26 Jul 2022 23:57:48 -0600 Subject: [PATCH 06/27] fix: Fix how Mediation Grant returns routing keys Signed-off-by: Colton Wolkins (Indicio work address) --- .../protocols/coordinate_mediation/v1_0/manager.py | 2 +- .../coordinate_mediation/v1_0/messages/mediate_grant.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py index 6a5af46957..195dd5fd5a 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py @@ -183,7 +183,7 @@ async def grant_request( await mediation_record.save(session, reason="Mediation request granted") grant = MediationGrant( endpoint=session.settings.get("default_endpoint"), - routing_keys=[routing_did.did], + routing_keys=[routing_did.verkey], ) return mediation_record, grant diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py index c18a2d132d..3b9e80fc74 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py @@ -43,7 +43,10 @@ def __init__( """ super(MediationGrant, self).__init__(**kwargs) self.endpoint = endpoint - self.routing_keys = list(f"did:key:z{DIDKey.from_public_key_b58(key, KeyType.ED25519).did}" for key in routing_keys) if routing_keys else [] + self.routing_keys = list( + (key if key.startswith("did:key:z") + else DIDKey.from_public_key_b58(key, KeyType.ED25519).did) + for key in routing_keys) if routing_keys else [] class MediationGrantSchema(AgentMessageSchema): From 4a0be009528ddbda35d55ccfba504dbd9fc05357 Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Wed, 27 Jul 2022 00:54:21 -0600 Subject: [PATCH 07/27] chore: Fix unit tests Signed-off-by: Colton Wolkins (Indicio work address) --- .../v1_0/handlers/tests/test_mediation_grant_handler.py | 2 +- .../coordinate_mediation/v1_0/messages/tests/test_keylist.py | 2 +- .../v1_0/messages/tests/test_keylist_update.py | 2 +- .../v1_0/messages/tests/test_keylist_update_response.py | 2 +- .../v1_0/messages/tests/test_mediate_grant.py | 2 +- .../v1_0/tests/test_mediation_manager.py | 5 ++++- 6 files changed, 9 insertions(+), 6 deletions(-) diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/handlers/tests/test_mediation_grant_handler.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/handlers/tests/test_mediation_grant_handler.py index c4c5d1a074..fd75c1a0d6 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/handlers/tests/test_mediation_grant_handler.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/handlers/tests/test_mediation_grant_handler.py @@ -18,7 +18,7 @@ from .. import mediation_grant_handler as test_module TEST_CONN_ID = "conn-id" -TEST_VERKEY = "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx" +TEST_VERKEY = "did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL" TEST_ENDPOINT = "https://example.com" diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_keylist.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_keylist.py index a65512d026..a955580966 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_keylist.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_keylist.py @@ -18,7 +18,7 @@ class TestKeylist(MessageTest, TestCase): "pagination": KeylistQueryPaginate(10, 10), "keys": [ KeylistKey( - recipient_key="3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx", + recipient_key="did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL", action="added", result="success", ) diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_keylist_update.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_keylist_update.py index 650105425e..c22a75ba9e 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_keylist_update.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_keylist_update.py @@ -15,6 +15,6 @@ class TestKeylistUpdate(MessageTest, TestCase): SCHEMA = KeylistUpdateSchema VALUES = { "updates": [ - KeylistUpdateRule("3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx", "add") + KeylistUpdateRule("did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL", "add") ] } diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_keylist_update_response.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_keylist_update_response.py index b7f70e25af..e59ea63511 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_keylist_update_response.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_keylist_update_response.py @@ -16,7 +16,7 @@ class TestKeylistUpdateResponse(MessageTest, TestCase): VALUES = { "updated": [ KeylistUpdated( - recipient_key="3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx", + recipient_key="did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL", action="added", result="success", ) diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_mediate_grant.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_mediate_grant.py index 2f73b32afe..5d24ca330b 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_mediate_grant.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_mediate_grant.py @@ -12,4 +12,4 @@ class TestMediateGrant(MessageTest, TestCase): TYPE = MEDIATE_GRANT CLASS = MediationGrant SCHEMA = MediationGrantSchema - VALUES = {"endpoint": "http://localhost:3000", "routing_keys": ["test_routing_key"]} + VALUES = {"endpoint": "http://localhost:3000", "routing_keys": ["did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL"]} diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_mediation_manager.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_mediation_manager.py index 788404feb8..3e9a1175fa 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_mediation_manager.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_mediation_manager.py @@ -9,6 +9,7 @@ from .....core.event_bus import EventBus, MockEventBus from .....core.in_memory import InMemoryProfile from .....core.profile import Profile, ProfileSession +from .....did.did_key import DIDKey from .....storage.error import StorageNotFoundError from ....routing.v1_0.models.route_record import RouteRecord from ..manager import ( @@ -113,8 +114,10 @@ async def test_grant_request(self, session, manager): assert record.connection_id == TEST_CONN_ID record, grant = await manager.grant_request(record.mediation_id) assert grant.endpoint == session.settings.get("default_endpoint") + routing_key = (await manager._retrieve_routing_did(session)) + routing_key = DIDKey.from_public_key_b58(routing_key.verkey, routing_key.key_type).did assert grant.routing_keys == [ - (await manager._retrieve_routing_did(session)).verkey + routing_key ] async def test_deny_request(self, manager): From 72921d1181355975b1242aa34d36366352bb8e65 Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Wed, 27 Jul 2022 08:13:08 -0600 Subject: [PATCH 08/27] feat: Add did:key conversions throughout mediation Also attempt to revert key formats before forwarding messages Signed-off-by: Colton Wolkins (Indicio work address) --- .../v1_0/messages/inner/keylist_key.py | 9 +++++++-- .../v1_0/messages/inner/keylist_update_rule.py | 7 ++++++- .../v1_0/messages/inner/keylist_updated.py | 7 ++++++- .../v1_0/messages/mediate_grant.py | 2 +- aries_cloudagent/transport/pack_format.py | 13 ++++++++++++- 5 files changed, 32 insertions(+), 6 deletions(-) diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py index 45a1df48c3..eb7819819f 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py @@ -3,7 +3,9 @@ from marshmallow import EXCLUDE, fields from ......messaging.models.base import BaseModel, BaseModelSchema -from ......messaging.valid import DID_KEY +from ......messaging.valid import INDY_RAW_PUBLIC_KEY, DID_KEY +from ......did.did_key import DIDKey +from ......wallet.key_type import KeyType class KeylistKey(BaseModel): @@ -32,7 +34,10 @@ def __init__( """ super().__init__(**kwargs) - self.recipient_key = recipient_key + if key.startswith("did:key:"): + self.recipient_key = recipient_key + else: + self.recipient_key = DIDKey.from_public_key_b58(key, KeyType.ED25519).did class KeylistKeySchema(BaseModelSchema): diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py index c4dd529935..cf79bd92db 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py @@ -9,6 +9,8 @@ from ......messaging.models.base import BaseModel, BaseModelSchema from ......messaging.valid import DID_KEY +from ......did.did_key import DIDKey +from ......wallet.key_type import KeyType class KeylistUpdateRule(BaseModel): @@ -32,7 +34,10 @@ def __init__(self, recipient_key: str, action: str, **kwargs): """ super().__init__(**kwargs) - self.recipient_key = recipient_key + if key.startswith("did:key:"): + self.recipient_key = recipient_key + else: + self.recipient_key = DIDKey.from_public_key_b58(key, KeyType.ED25519).did self.action = action diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py index 1fe36c8044..942ef0c030 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py @@ -7,6 +7,8 @@ from ......messaging.models.base import BaseModel, BaseModelSchema from ......messaging.valid import DID_KEY +from ......did.did_key import DIDKey +from ......wallet.key_type import KeyType class KeylistUpdated(BaseModel): @@ -40,7 +42,10 @@ def __init__( """ super().__init__(**kwargs) - self.recipient_key = recipient_key + if key.startswith("did:key:"): + self.recipient_key = recipient_key + else: + self.recipient_key = DIDKey.from_public_key_b58(key, KeyType.ED25519).did self.action = action self.result = result diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py index 3b9e80fc74..f15ec524d6 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py @@ -44,7 +44,7 @@ def __init__( super(MediationGrant, self).__init__(**kwargs) self.endpoint = endpoint self.routing_keys = list( - (key if key.startswith("did:key:z") + (key if key.startswith("did:key:") else DIDKey.from_public_key_b58(key, KeyType.ED25519).did) for key in routing_keys) if routing_keys else [] diff --git a/aries_cloudagent/transport/pack_format.py b/aries_cloudagent/transport/pack_format.py index f982748098..a16fbb118b 100644 --- a/aries_cloudagent/transport/pack_format.py +++ b/aries_cloudagent/transport/pack_format.py @@ -7,6 +7,8 @@ from ..core.profile import ProfileSession from ..protocols.routing.v1_0.messages.forward import Forward +from ..did.did_key import DIDKey +from ..wallet.key_type import KeyType from ..messaging.util import time_now from ..utils.task_queue import TaskQueue @@ -183,12 +185,21 @@ async def pack( raise WireFormatEncodeError("Message pack failed") from e if routing_keys: - recip_keys = recipient_keys + recip_keys = [] + for key in recipient_keys: + if key.startswith("did:key:"): + recip_keys.append(DIDKey.from_did(key).public_key_b58) + else: + recip_keys.append(recipient_key) for router_key in routing_keys: message = json.loads(message.decode("utf-8")) fwd_msg = Forward(to=recip_keys[0], msg=message) # Forwards are anon packed recip_keys = [router_key] + if router_key.startswith("did:key:"): + recip_keys = [DIDKey.from_did(router_key).public_key_b58] + else: + recip_keys = [recipient_key] try: message = await wallet.pack_message(fwd_msg.to_json(), recip_keys) except WalletError as e: From f22af92705c36aa05b247bf2d15e5329045ecbe6 Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Wed, 27 Jul 2022 08:27:13 -0600 Subject: [PATCH 09/27] fix: Typos in variable names Signed-off-by: Colton Wolkins (Indicio work address) --- .../coordinate_mediation/v1_0/messages/inner/keylist_key.py | 4 ++-- .../v1_0/messages/inner/keylist_update_rule.py | 4 ++-- .../v1_0/messages/inner/keylist_updated.py | 4 ++-- aries_cloudagent/transport/pack_format.py | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py index eb7819819f..dbc9583893 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py @@ -34,10 +34,10 @@ def __init__( """ super().__init__(**kwargs) - if key.startswith("did:key:"): + if recipient_key.startswith("did:key:"): self.recipient_key = recipient_key else: - self.recipient_key = DIDKey.from_public_key_b58(key, KeyType.ED25519).did + self.recipient_key = DIDKey.from_public_key_b58(recipient_key, KeyType.ED25519).did class KeylistKeySchema(BaseModelSchema): diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py index cf79bd92db..eb7c843810 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py @@ -34,10 +34,10 @@ def __init__(self, recipient_key: str, action: str, **kwargs): """ super().__init__(**kwargs) - if key.startswith("did:key:"): + if recipient_key.startswith("did:key:"): self.recipient_key = recipient_key else: - self.recipient_key = DIDKey.from_public_key_b58(key, KeyType.ED25519).did + self.recipient_key = DIDKey.from_public_key_b58(recipient_key, KeyType.ED25519).did self.action = action diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py index 942ef0c030..8f17e5d52e 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py @@ -42,10 +42,10 @@ def __init__( """ super().__init__(**kwargs) - if key.startswith("did:key:"): + if recipient_key.startswith("did:key:"): self.recipient_key = recipient_key else: - self.recipient_key = DIDKey.from_public_key_b58(key, KeyType.ED25519).did + self.recipient_key = DIDKey.from_public_key_b58(recipient_key, KeyType.ED25519).did self.action = action self.result = result diff --git a/aries_cloudagent/transport/pack_format.py b/aries_cloudagent/transport/pack_format.py index a16fbb118b..967ec8322f 100644 --- a/aries_cloudagent/transport/pack_format.py +++ b/aries_cloudagent/transport/pack_format.py @@ -190,7 +190,7 @@ async def pack( if key.startswith("did:key:"): recip_keys.append(DIDKey.from_did(key).public_key_b58) else: - recip_keys.append(recipient_key) + recip_keys.append(key) for router_key in routing_keys: message = json.loads(message.decode("utf-8")) fwd_msg = Forward(to=recip_keys[0], msg=message) From 4e5d589626d8f2a8e3557266b5adb6312ea12c89 Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Wed, 27 Jul 2022 12:11:24 -0600 Subject: [PATCH 10/27] fix: Convert from did:key to verkey when retrieving recipient Signed-off-by: Colton Wolkins (Indicio work address) --- aries_cloudagent/protocols/routing/v1_0/manager.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/aries_cloudagent/protocols/routing/v1_0/manager.py b/aries_cloudagent/protocols/routing/v1_0/manager.py index 44c1b7b7c6..ad5a9f23b7 100644 --- a/aries_cloudagent/protocols/routing/v1_0/manager.py +++ b/aries_cloudagent/protocols/routing/v1_0/manager.py @@ -4,6 +4,8 @@ from ....core.error import BaseError from ....core.profile import Profile +from ....did.did_key import DIDKey +from ....wallet.key_type import KeyType from ....storage.error import ( StorageError, StorageDuplicateError, @@ -56,6 +58,8 @@ async def get_recipient(self, recip_verkey: str) -> RouteRecord: try: async with self._profile.session() as session: + if recip_verkey.startswith("did:key:"): + recip_verkey = DIDKey.from_did(recip_verkey).public_key_b58 record = await RouteRecord.retrieve_by_recipient_key( session, recip_verkey ) From a66091a316df7d465cb770ef033c8c08dbf7a446 Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Wed, 27 Jul 2022 12:33:50 -0600 Subject: [PATCH 11/27] chore: test Signed-off-by: Colton Wolkins (Indicio work address) --- aries_cloudagent/protocols/routing/v1_0/manager.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aries_cloudagent/protocols/routing/v1_0/manager.py b/aries_cloudagent/protocols/routing/v1_0/manager.py index ad5a9f23b7..bd3c0b49f4 100644 --- a/aries_cloudagent/protocols/routing/v1_0/manager.py +++ b/aries_cloudagent/protocols/routing/v1_0/manager.py @@ -58,8 +58,8 @@ async def get_recipient(self, recip_verkey: str) -> RouteRecord: try: async with self._profile.session() as session: - if recip_verkey.startswith("did:key:"): - recip_verkey = DIDKey.from_did(recip_verkey).public_key_b58 + if not recip_verkey.startswith("did:key:"): + recip_verkey = DIDKey.from_public_key_b58(key, KeyType.ED25519).did record = await RouteRecord.retrieve_by_recipient_key( session, recip_verkey ) From 6460304d849efd5d6cee2eeb1817c8b00cbf49bb Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Tue, 2 Aug 2022 08:49:31 -0600 Subject: [PATCH 12/27] fix: Add RoutingKey validator Add the RoutingKey Validator that supports both DIDKey and Raw Indy Public Keys for backwards compatibility reasons Signed-off-by: Colton Wolkins (Indicio work address) --- aries_cloudagent/messaging/valid.py | 16 ++++++++++++++++ .../v1_0/messages/inner/keylist_update_rule.py | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/aries_cloudagent/messaging/valid.py b/aries_cloudagent/messaging/valid.py index acaf9a27f8..cde3449f7f 100644 --- a/aries_cloudagent/messaging/valid.py +++ b/aries_cloudagent/messaging/valid.py @@ -349,6 +349,21 @@ def __init__(self): ) +class RoutingKey(Regexp): + """Validate value against indy (Ed25519VerificationKey2018) raw public key or DID key specification.""" + + EXAMPLE = DIDKey.EXAMPLE + PATTERN = re.compile(DIDKey.PATTERN.pattern + "|" + IndyRawPublicKey.PATTERN) + + def __init__(self): + """Initializer.""" + + super().__init__( + RoutingKey.PATTERN, + error="Value {input} is not in W3C did:key or Ed25519VerificationKey2018 key format", + ) + + class IndyCredDefId(Regexp): """Validate value against indy credential definition identifier specification.""" @@ -788,6 +803,7 @@ def __init__( JWT = {"validate": JSONWebToken(), "example": JSONWebToken.EXAMPLE} DID_KEY = {"validate": DIDKey(), "example": DIDKey.EXAMPLE} DID_POSTURE = {"validate": DIDPosture(), "example": DIDPosture.EXAMPLE} +ROUTING_KEY = {"validate": RoutingKey(), "example": RoutingKey.EXAMPLE} INDY_DID = {"validate": IndyDID(), "example": IndyDID.EXAMPLE} GENERIC_DID = {"validate": MaybeIndyDID(), "example": MaybeIndyDID.EXAMPLE} INDY_RAW_PUBLIC_KEY = { diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py index eb7c843810..96406b54ed 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py @@ -8,7 +8,7 @@ from marshmallow.validate import OneOf from ......messaging.models.base import BaseModel, BaseModelSchema -from ......messaging.valid import DID_KEY +from ......messaging.valid import ROUTING_KEY from ......did.did_key import DIDKey from ......wallet.key_type import KeyType @@ -50,7 +50,7 @@ class Meta: model_class = KeylistUpdateRule recipient_key = fields.Str( - description="Key to remove or add", required=True, **DID_KEY + description="Key to remove or add", required=True, **ROUTING_KEY ) action = fields.Str( required=True, From 27cb777576be34d6c5c3ba6213465525fc101a12 Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Tue, 2 Aug 2022 10:22:28 -0600 Subject: [PATCH 13/27] fix: Decode recipient keys before packing message Signed-off-by: Colton Wolkins (Indicio work address) --- aries_cloudagent/transport/pack_format.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/aries_cloudagent/transport/pack_format.py b/aries_cloudagent/transport/pack_format.py index 967ec8322f..c1172a3156 100644 --- a/aries_cloudagent/transport/pack_format.py +++ b/aries_cloudagent/transport/pack_format.py @@ -177,20 +177,21 @@ async def pack( if not wallet: raise WireFormatEncodeError("No wallet instance") + recip_keys = [] + for key in recipient_keys: + if key.startswith("did:key:"): + recip_keys.append(DIDKey.from_did(key).public_key_b58) + else: + recip_keys.append(key) + try: message = await wallet.pack_message( - message_json, recipient_keys, sender_key + message_json, recip_keys, sender_key ) except WalletError as e: raise WireFormatEncodeError("Message pack failed") from e if routing_keys: - recip_keys = [] - for key in recipient_keys: - if key.startswith("did:key:"): - recip_keys.append(DIDKey.from_did(key).public_key_b58) - else: - recip_keys.append(key) for router_key in routing_keys: message = json.loads(message.decode("utf-8")) fwd_msg = Forward(to=recip_keys[0], msg=message) @@ -198,8 +199,6 @@ async def pack( recip_keys = [router_key] if router_key.startswith("did:key:"): recip_keys = [DIDKey.from_did(router_key).public_key_b58] - else: - recip_keys = [recipient_key] try: message = await wallet.pack_message(fwd_msg.to_json(), recip_keys) except WalletError as e: From 707dad21bcd317d872b6deef52b33adfff5f3fea Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Tue, 2 Aug 2022 11:01:57 -0600 Subject: [PATCH 14/27] fix: Decode routing keys when making DIDDoc Signed-off-by: Colton Wolkins (Indicio work address) --- aries_cloudagent/connections/base_manager.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/aries_cloudagent/connections/base_manager.py b/aries_cloudagent/connections/base_manager.py index bd5e281694..78011f2c7c 100644 --- a/aries_cloudagent/connections/base_manager.py +++ b/aries_cloudagent/connections/base_manager.py @@ -18,6 +18,7 @@ from ..core.error import BaseError from ..core.profile import Profile from ..did.did_key import DIDKey +from ..wallet.key_type import KeyType from ..protocols.connections.v1_0.messages.connection_invitation import ( ConnectionInvitation, ) @@ -116,10 +117,12 @@ async def create_did_document( raise BaseConnectionManagerError( "Routing DIDDoc service has no recipient key(s)" ) + key = service.recip_keys[0].value + key = DIDKey.from_did(key).public_key_b58 if key.startswith("did:key:") else key rk = PublicKey( did_info.did, f"routing-{router_idx}", - service.recip_keys[0].value, + key, PublicKeyType.ED25519_SIG_2018, did_controller, True, @@ -135,7 +138,7 @@ async def create_did_document( PublicKey( did_info.did, f"routing-{idx}", - key, + DIDKey.from_did(key).public_key_b58 if key.startswith("did:key:") else key, PublicKeyType.ED25519_SIG_2018, did_controller, # TODO: get correct controller did_info True, # TODO: should this be true? From 5ed52473c4f1249338f08eb4d4aeb31229ed0481 Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Tue, 2 Aug 2022 11:31:32 -0600 Subject: [PATCH 15/27] fix: Fix typo in variable names Signed-off-by: Colton Wolkins (Indicio work address) --- aries_cloudagent/protocols/routing/v1_0/manager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aries_cloudagent/protocols/routing/v1_0/manager.py b/aries_cloudagent/protocols/routing/v1_0/manager.py index bd3c0b49f4..48556fe347 100644 --- a/aries_cloudagent/protocols/routing/v1_0/manager.py +++ b/aries_cloudagent/protocols/routing/v1_0/manager.py @@ -59,7 +59,7 @@ async def get_recipient(self, recip_verkey: str) -> RouteRecord: try: async with self._profile.session() as session: if not recip_verkey.startswith("did:key:"): - recip_verkey = DIDKey.from_public_key_b58(key, KeyType.ED25519).did + recip_verkey = DIDKey.from_public_key_b58(recip_verkey, KeyType.ED25519).did record = await RouteRecord.retrieve_by_recipient_key( session, recip_verkey ) From 3ee7f433ca7a39d3fcc600aea0f8e771fa1bdc0c Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Wed, 3 Aug 2022 13:18:49 -0600 Subject: [PATCH 16/27] fix: Fix unit tests after did:key work Signed-off-by: Colton Wolkins (Indicio work address) --- aries_cloudagent/connections/base_manager.py | 9 ++++--- aries_cloudagent/messaging/valid.py | 10 ++++++-- .../multitenant/tests/test_base.py | 24 ++++++++++--------- .../connections/v1_0/tests/test_manager.py | 5 +++- .../tests/test_keylist_query_handler.py | 3 ++- .../v1_0/messages/inner/keylist_key.py | 6 +++-- .../messages/inner/keylist_update_rule.py | 4 +++- .../v1_0/messages/inner/keylist_updated.py | 4 +++- .../v1_0/tests/test_routes.py | 20 ++++++++++------ .../didexchange/v1_0/tests/test_manager.py | 5 +++- .../protocols/routing/v1_0/manager.py | 7 ++++-- aries_cloudagent/transport/pack_format.py | 1 - 12 files changed, 65 insertions(+), 33 deletions(-) diff --git a/aries_cloudagent/connections/base_manager.py b/aries_cloudagent/connections/base_manager.py index 78011f2c7c..6421a47c9c 100644 --- a/aries_cloudagent/connections/base_manager.py +++ b/aries_cloudagent/connections/base_manager.py @@ -18,7 +18,6 @@ from ..core.error import BaseError from ..core.profile import Profile from ..did.did_key import DIDKey -from ..wallet.key_type import KeyType from ..protocols.connections.v1_0.messages.connection_invitation import ( ConnectionInvitation, ) @@ -118,7 +117,10 @@ async def create_did_document( "Routing DIDDoc service has no recipient key(s)" ) key = service.recip_keys[0].value - key = DIDKey.from_did(key).public_key_b58 if key.startswith("did:key:") else key + key = ( + DIDKey.from_did(key).public_key_b58 + if key.startswith("did:key:") else key + ) rk = PublicKey( did_info.did, f"routing-{router_idx}", @@ -138,7 +140,8 @@ async def create_did_document( PublicKey( did_info.did, f"routing-{idx}", - DIDKey.from_did(key).public_key_b58 if key.startswith("did:key:") else key, + DIDKey.from_did(key).public_key_b58 + if key.startswith("did:key:") else key, PublicKeyType.ED25519_SIG_2018, did_controller, # TODO: get correct controller did_info True, # TODO: should this be true? diff --git a/aries_cloudagent/messaging/valid.py b/aries_cloudagent/messaging/valid.py index cde3449f7f..a62d587fff 100644 --- a/aries_cloudagent/messaging/valid.py +++ b/aries_cloudagent/messaging/valid.py @@ -350,7 +350,12 @@ def __init__(self): class RoutingKey(Regexp): - """Validate value against indy (Ed25519VerificationKey2018) raw public key or DID key specification.""" + """ + Validate between indy or did key. + + Validate value against indy (Ed25519VerificationKey2018) + raw public key or DID key specification. + """ EXAMPLE = DIDKey.EXAMPLE PATTERN = re.compile(DIDKey.PATTERN.pattern + "|" + IndyRawPublicKey.PATTERN) @@ -360,7 +365,8 @@ def __init__(self): super().__init__( RoutingKey.PATTERN, - error="Value {input} is not in W3C did:key or Ed25519VerificationKey2018 key format", + error=("Value {input} is not in W3C did:key" + " or Ed25519VerificationKey2018 key format"), ) diff --git a/aries_cloudagent/multitenant/tests/test_base.py b/aries_cloudagent/multitenant/tests/test_base.py index 5d37297b9d..f5a67249a4 100644 --- a/aries_cloudagent/multitenant/tests/test_base.py +++ b/aries_cloudagent/multitenant/tests/test_base.py @@ -25,6 +25,8 @@ from ..error import WalletKeyMissingError from .. import base as test_module +RECIPIENT_KEY = "H3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV" + class MockMultitenantManager(BaseMultitenantManager): async def get_wallet_profile( @@ -141,7 +143,7 @@ async def test_wallet_exists_false(self): assert wallet_name_exists is False async def test_get_wallet_by_key_routing_record_does_not_exist(self): - recipient_key = "test" + recipient_key = RECIPIENT_KEY with async_mock.patch.object(WalletRecord, "retrieve_by_id") as retrieve_by_id: wallet = await self.manager._get_wallet_by_key(recipient_key) @@ -152,7 +154,7 @@ async def test_get_wallet_by_key_routing_record_does_not_exist(self): await self.manager._get_wallet_by_key(recipient_key) async def test_get_wallet_by_key_wallet_record_does_not_exist(self): - recipient_key = "test-recipient-key" + recipient_key = RECIPIENT_KEY wallet_id = "test-wallet-id" route_record = RouteRecord(wallet_id=wallet_id, recipient_key=recipient_key) @@ -163,7 +165,7 @@ async def test_get_wallet_by_key_wallet_record_does_not_exist(self): await self.manager._get_wallet_by_key(recipient_key) async def test_get_wallet_by_key(self): - recipient_key = "test-recipient-key" + recipient_key = RECIPIENT_KEY wallet_record = WalletRecord(settings={}) async with self.profile.session() as session: @@ -356,10 +358,10 @@ async def test_add_key_no_mediation(self): ) as create_route_record, async_mock.patch.object( MediationManager, "add_key" ) as mediation_add_key: - await self.manager.add_key("wallet_id", "recipient_key") + await self.manager.add_key("wallet_id", RECIPIENT_KEY) create_route_record.assert_called_once_with( - recipient_key="recipient_key", internal_wallet_id="wallet_id" + recipient_key=RECIPIENT_KEY, internal_wallet_id="wallet_id" ) mediation_add_key.assert_not_called() @@ -372,11 +374,11 @@ async def test_add_key_skip_if_exists_does_not_exist(self): retrieve_by_recipient_key.side_effect = StorageNotFoundError() await self.manager.add_key( - "wallet_id", "recipient_key", skip_if_exists=True + "wallet_id", RECIPIENT_KEY, skip_if_exists=True ) create_route_record.assert_called_once_with( - recipient_key="recipient_key", internal_wallet_id="wallet_id" + recipient_key=RECIPIENT_KEY, internal_wallet_id="wallet_id" ) async def test_add_key_skip_if_exists_does_exist(self): @@ -386,7 +388,7 @@ async def test_add_key_skip_if_exists_does_exist(self): RouteRecord, "retrieve_by_recipient_key" ) as retrieve_by_recipient_key: await self.manager.add_key( - "wallet_id", "recipient_key", skip_if_exists=True + "wallet_id", RECIPIENT_KEY, skip_if_exists=True ) create_route_record.assert_not_called() @@ -405,14 +407,14 @@ async def test_add_key_mediation(self): get_default_mediator.return_value = default_mediator mediation_add_key.return_value = keylist_updates - await self.manager.add_key("wallet_id", "recipient_key") + await self.manager.add_key("wallet_id", RECIPIENT_KEY) create_route_record.assert_called_once_with( - recipient_key="recipient_key", internal_wallet_id="wallet_id" + recipient_key=RECIPIENT_KEY, internal_wallet_id="wallet_id" ) get_default_mediator.assert_called_once() - mediation_add_key.assert_called_once_with("recipient_key") + mediation_add_key.assert_called_once_with(RECIPIENT_KEY) self.responder.send.assert_called_once_with( keylist_updates, connection_id=default_mediator.connection_id ) diff --git a/aries_cloudagent/protocols/connections/v1_0/tests/test_manager.py b/aries_cloudagent/protocols/connections/v1_0/tests/test_manager.py index c1b1bb0ba1..a0a8ca5829 100644 --- a/aries_cloudagent/protocols/connections/v1_0/tests/test_manager.py +++ b/aries_cloudagent/protocols/connections/v1_0/tests/test_manager.py @@ -1052,7 +1052,10 @@ async def test_receive_request_mediation_id(self): assert len(message.updates) == 1 (remove,) = message.updates assert remove.action == KeylistUpdateRule.RULE_REMOVE - assert remove.recipient_key == record.invitation_key + did_key = DIDKey.from_public_key_b58( + record.invitation_key, KeyType.ED25519 + ).did + assert remove.recipient_key == did_key async def test_receive_request_bad_mediation(self): mock_request = async_mock.MagicMock() diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/handlers/tests/test_keylist_query_handler.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/handlers/tests/test_keylist_query_handler.py index 6d6d7d9152..fd216be4a7 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/handlers/tests/test_keylist_query_handler.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/handlers/tests/test_keylist_query_handler.py @@ -18,6 +18,7 @@ TEST_CONN_ID = "conn-id" TEST_VERKEY = "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx" +TEST_VERKEY_DIDKEY = "did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL" class TestKeylistQueryHandler(AsyncTestCase): @@ -77,4 +78,4 @@ async def test_handler(self): result, _target = responder.messages[0] assert isinstance(result, Keylist) assert len(result.keys) == 1 - assert result.keys[0].recipient_key == TEST_VERKEY + assert result.keys[0].recipient_key == TEST_VERKEY_DIDKEY diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py index dbc9583893..c320ea62aa 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py @@ -3,7 +3,7 @@ from marshmallow import EXCLUDE, fields from ......messaging.models.base import BaseModel, BaseModelSchema -from ......messaging.valid import INDY_RAW_PUBLIC_KEY, DID_KEY +from ......messaging.valid import DID_KEY from ......did.did_key import DIDKey from ......wallet.key_type import KeyType @@ -37,7 +37,9 @@ def __init__( if recipient_key.startswith("did:key:"): self.recipient_key = recipient_key else: - self.recipient_key = DIDKey.from_public_key_b58(recipient_key, KeyType.ED25519).did + self.recipient_key = ( + DIDKey.from_public_key_b58(recipient_key, KeyType.ED25519).did + ) class KeylistKeySchema(BaseModelSchema): diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py index 96406b54ed..1ad0dc5d71 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py @@ -37,7 +37,9 @@ def __init__(self, recipient_key: str, action: str, **kwargs): if recipient_key.startswith("did:key:"): self.recipient_key = recipient_key else: - self.recipient_key = DIDKey.from_public_key_b58(recipient_key, KeyType.ED25519).did + self.recipient_key = ( + DIDKey.from_public_key_b58(recipient_key, KeyType.ED25519).did + ) self.action = action diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py index 8f17e5d52e..a2e4a11cd2 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py @@ -45,7 +45,9 @@ def __init__( if recipient_key.startswith("did:key:"): self.recipient_key = recipient_key else: - self.recipient_key = DIDKey.from_public_key_b58(recipient_key, KeyType.ED25519).did + self.recipient_key = ( + DIDKey.from_public_key_b58(recipient_key, KeyType.ED25519).did + ) self.action = action self.result = result diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_routes.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_routes.py index a19070a6e6..c63bca7c8b 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_routes.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_routes.py @@ -454,8 +454,14 @@ async def test_get_keylist_storage_error(self): async def test_send_keylist_update(self): body = { "updates": [ - {"recipient_key": "test-key0", "action": "add"}, - {"recipient_key": "test-key1", "action": "remove"}, + {"recipient_key": "EwUKjVLboiLSuoWSEtDvrgrd41EUxG5bLecQrkHB63Up", "action": "add"}, + {"recipient_key": "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx", "action": "remove"}, + ] + } + body_with_didkey = { + "updates": [ + {"recipient_key": "did:key:z6MktPjNKjb39Fpv2JM8vTBmhnQcsaWLN9Kx2fXLh2FC1GGC", "action": "add"}, + {"recipient_key": "did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL", "action": "remove"}, ] } @@ -477,13 +483,13 @@ async def test_send_keylist_update(self): ), ) as mock_response: results, status = await test_module.send_keylist_update(self.request) - assert results["updates"] == body["updates"] + assert results["updates"] == body_with_didkey["updates"] assert status == 201 async def test_send_keylist_update_bad_action(self): self.request.json.return_value = { "updates": [ - {"recipient_key": "test-key0", "action": "wrong"}, + {"recipient_key": "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx", "action": "wrong"}, ] } @@ -493,7 +499,7 @@ async def test_send_keylist_update_bad_action(self): async def test_send_keylist_update_bad_mediation_state(self): self.request.json.return_value = { "updates": [ - {"recipient_key": "test-key0", "action": "add"}, + {"recipient_key": "EwUKjVLboiLSuoWSEtDvrgrd41EUxG5bLecQrkHB63Up", "action": "add"}, ] } @@ -516,7 +522,7 @@ async def test_send_keylist_update_bad_updates(self): async def test_send_keylist_update_x_no_mediation_rec(self): self.request.json.return_value = { "updates": [ - {"recipient_key": "test-key0", "action": "add"}, + {"recipient_key": "EwUKjVLboiLSuoWSEtDvrgrd41EUxG5bLecQrkHB63Up", "action": "add"}, ] } with async_mock.patch.object( @@ -529,7 +535,7 @@ async def test_send_keylist_update_x_no_mediation_rec(self): async def test_send_keylist_update_x_storage_error(self): self.request.json.return_value = { "updates": [ - {"recipient_key": "test-key0", "action": "add"}, + {"recipient_key": "EwUKjVLboiLSuoWSEtDvrgrd41EUxG5bLecQrkHB63Up", "action": "add"}, ] } diff --git a/aries_cloudagent/protocols/didexchange/v1_0/tests/test_manager.py b/aries_cloudagent/protocols/didexchange/v1_0/tests/test_manager.py index 3ffd26a181..2eb10cd20d 100644 --- a/aries_cloudagent/protocols/didexchange/v1_0/tests/test_manager.py +++ b/aries_cloudagent/protocols/didexchange/v1_0/tests/test_manager.py @@ -717,7 +717,10 @@ async def test_receive_request_with_mediator_without_multi_use_multitenant(self) assert len(message.updates) == 1 (remove,) = message.updates assert remove.action == KeylistUpdateRule.RULE_REMOVE - assert remove.recipient_key == record.invitation_key + did_key = DIDKey.from_public_key_b58( + record.invitation_key, KeyType.ED25519 + ).did + assert remove.recipient_key == did_key async def test_receive_request_with_mediator_without_multi_use_multitenant_mismatch( self, diff --git a/aries_cloudagent/protocols/routing/v1_0/manager.py b/aries_cloudagent/protocols/routing/v1_0/manager.py index 48556fe347..44db54cc50 100644 --- a/aries_cloudagent/protocols/routing/v1_0/manager.py +++ b/aries_cloudagent/protocols/routing/v1_0/manager.py @@ -58,8 +58,11 @@ async def get_recipient(self, recip_verkey: str) -> RouteRecord: try: async with self._profile.session() as session: - if not recip_verkey.startswith("did:key:"): - recip_verkey = DIDKey.from_public_key_b58(recip_verkey, KeyType.ED25519).did + if recip_verkey.startswith("did:key:"): + recip_verkey = ( + DIDKey.from_did(recip_verkey, KeyType.ED25519) + ) + recip_verkey = recip_verkey.public_key_b58 record = await RouteRecord.retrieve_by_recipient_key( session, recip_verkey ) diff --git a/aries_cloudagent/transport/pack_format.py b/aries_cloudagent/transport/pack_format.py index c1172a3156..2c078868e1 100644 --- a/aries_cloudagent/transport/pack_format.py +++ b/aries_cloudagent/transport/pack_format.py @@ -8,7 +8,6 @@ from ..protocols.routing.v1_0.messages.forward import Forward from ..did.did_key import DIDKey -from ..wallet.key_type import KeyType from ..messaging.util import time_now from ..utils.task_queue import TaskQueue From e49c3c3d7b15bf8df7d958be17a189ddc5798706 Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Thu, 4 Aug 2022 08:06:33 -0600 Subject: [PATCH 17/27] feat: Search for both route keys when getting recipients Signed-off-by: Colton Wolkins (Indicio work address) --- .../protocols/routing/v1_0/manager.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/aries_cloudagent/protocols/routing/v1_0/manager.py b/aries_cloudagent/protocols/routing/v1_0/manager.py index 44db54cc50..3d596cfa4b 100644 --- a/aries_cloudagent/protocols/routing/v1_0/manager.py +++ b/aries_cloudagent/protocols/routing/v1_0/manager.py @@ -62,9 +62,21 @@ async def get_recipient(self, recip_verkey: str) -> RouteRecord: recip_verkey = ( DIDKey.from_did(recip_verkey, KeyType.ED25519) ) - recip_verkey = recip_verkey.public_key_b58 - record = await RouteRecord.retrieve_by_recipient_key( - session, recip_verkey + # recip_verkey = recip_verkey.public_key_b58 + else: + recip_verkey = ( + DIDKey.from_public_key_b58( + recip_verkey, + KeyType.ED25519, + ) + ) + # record = await RouteRecord.retrieve_by_recipient_key( + tag_filter = {"$or":[ + {"recipient_key": recip_verkey.did}, + {"recipient_key": recip_verkey.public_key_b58} + ]} + record = await RouteRecord.retrieve_by_tag_filter( + session, tag_filter ) except StorageDuplicateError: raise RouteNotFoundError( From e55ae7d7cdc52e2f0eba87b24359b5e2f5948395 Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Thu, 4 Aug 2022 10:41:51 -0600 Subject: [PATCH 18/27] fix: Move backwards compat code to proper location & fix tests Signed-off-by: Colton Wolkins (Indicio work address) --- .../v1_0/tests/test_mediation_manager.py | 6 +++-- .../protocols/routing/v1_0/manager.py | 23 ++----------------- .../routing/v1_0/models/route_record.py | 21 ++++++++++++++++- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_mediation_manager.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_mediation_manager.py index 3e9a1175fa..4f86b8eee4 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_mediation_manager.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_mediation_manager.py @@ -29,8 +29,10 @@ TEST_CONN_ID = "conn-id" TEST_THREAD_ID = "thread-id" TEST_ENDPOINT = "https://example.com" -TEST_VERKEY = "did:key:z3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx" -TEST_ROUTE_VERKEY = "did:key:z9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC" +# TEST_VERKEY = "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx" +TEST_VERKEY = "did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL" +# TEST_ROUTE_VERKEY = "9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC" +TEST_ROUTE_VERKEY = "did:key:z6MknxTj6Zj1VrDWc1ofaZtmCVv2zNXpD58Xup4ijDGoQhya" pytestmark = pytest.mark.asyncio diff --git a/aries_cloudagent/protocols/routing/v1_0/manager.py b/aries_cloudagent/protocols/routing/v1_0/manager.py index 3d596cfa4b..44c1b7b7c6 100644 --- a/aries_cloudagent/protocols/routing/v1_0/manager.py +++ b/aries_cloudagent/protocols/routing/v1_0/manager.py @@ -4,8 +4,6 @@ from ....core.error import BaseError from ....core.profile import Profile -from ....did.did_key import DIDKey -from ....wallet.key_type import KeyType from ....storage.error import ( StorageError, StorageDuplicateError, @@ -58,25 +56,8 @@ async def get_recipient(self, recip_verkey: str) -> RouteRecord: try: async with self._profile.session() as session: - if recip_verkey.startswith("did:key:"): - recip_verkey = ( - DIDKey.from_did(recip_verkey, KeyType.ED25519) - ) - # recip_verkey = recip_verkey.public_key_b58 - else: - recip_verkey = ( - DIDKey.from_public_key_b58( - recip_verkey, - KeyType.ED25519, - ) - ) - # record = await RouteRecord.retrieve_by_recipient_key( - tag_filter = {"$or":[ - {"recipient_key": recip_verkey.did}, - {"recipient_key": recip_verkey.public_key_b58} - ]} - record = await RouteRecord.retrieve_by_tag_filter( - session, tag_filter + record = await RouteRecord.retrieve_by_recipient_key( + session, recip_verkey ) except StorageDuplicateError: raise RouteNotFoundError( diff --git a/aries_cloudagent/protocols/routing/v1_0/models/route_record.py b/aries_cloudagent/protocols/routing/v1_0/models/route_record.py index e750cd15eb..f0016a979c 100644 --- a/aries_cloudagent/protocols/routing/v1_0/models/route_record.py +++ b/aries_cloudagent/protocols/routing/v1_0/models/route_record.py @@ -4,6 +4,8 @@ from .....core.profile import ProfileSession from .....messaging.models.base_record import BaseRecord, BaseRecordSchema +from .....did.did_key import DIDKey +from .....wallet.key_type import KeyType class RouteRecord(BaseRecord): @@ -75,7 +77,24 @@ async def retrieve_by_recipient_key( RouteRecord: retrieved route record """ - tag_filter = {"recipient_key": recipient_key} + if recipient_key.startswith("did:key:"): + recipient_key = ( + DIDKey.from_did(recipient_key) + ) + # recipient_key = recipient_key.public_key_b58 + else: + recipient_key = ( + DIDKey.from_public_key_b58( + recipient_key, + KeyType.ED25519, + ) + ) + # record = await RouteRecord.retrieve_by_recipient_key( + tag_filter = {"recipient_key": {"$in": [ + recipient_key.did, + recipient_key.public_key_b58, + ]}} + # tag_filter = {"recipient_key": recipient_key} return await cls.retrieve_by_tag_filter(session, tag_filter) @classmethod From a3ba4bfcc1c5ff1f9c1d29ed9b5a7fb3b4e56879 Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Thu, 4 Aug 2022 11:27:39 -0600 Subject: [PATCH 19/27] fix: Fix invited not getting created/retrieved Signed-off-by: Colton Wolkins (Indicio work address) --- .../v1_0/messages/connection_invitation.py | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/aries_cloudagent/protocols/connections/v1_0/messages/connection_invitation.py b/aries_cloudagent/protocols/connections/v1_0/messages/connection_invitation.py index 48ca887989..4f0d7d68e6 100644 --- a/aries_cloudagent/protocols/connections/v1_0/messages/connection_invitation.py +++ b/aries_cloudagent/protocols/connections/v1_0/messages/connection_invitation.py @@ -5,8 +5,9 @@ from marshmallow import EXCLUDE, fields, validates_schema, ValidationError +from .....did.did_key import DIDKey from .....messaging.agent_message import AgentMessage, AgentMessageSchema -from .....messaging.valid import INDY_DID, INDY_RAW_PUBLIC_KEY +from .....messaging.valid import INDY_DID, ROUTING_KEY from .....wallet.util import b64_to_bytes, bytes_to_b64 from ..message_types import CONNECTION_INVITATION, PROTOCOL_PACKAGE @@ -53,11 +54,26 @@ def __init__( super().__init__(**kwargs) self.label = label self.did = did - self.recipient_keys = list(recipient_keys) if recipient_keys else None self.endpoint = endpoint - self.routing_keys = list(routing_keys) if routing_keys else None self.image_url = image_url + if recipient_keys: + public_keys = list() + for key in recipient_keys: + public_keys.append( + DIDKey.from_did(key).public_key_b58 + if key.startswith("did:key:") else key + ) + self.recipient_keys = public_keys if recipient_keys else None + if routing_keys: + public_keys = list() + for key in routing_keys: + public_keys.append( + DIDKey.from_did(key).public_key_b58 + if key.startswith("did:key:") else key + ) + self.routing_keys = public_keys if routing_keys else None + def to_url(self, base_url: str = None) -> str: """ Convert an invitation to URL format for sharing. @@ -109,7 +125,7 @@ class Meta: required=False, description="DID for connection invitation", **INDY_DID ) recipient_keys = fields.List( - fields.Str(description="Recipient public key", **INDY_RAW_PUBLIC_KEY), + fields.Str(description="Recipient public key", **ROUTING_KEY), data_key="recipientKeys", required=False, description="List of recipient keys", @@ -121,7 +137,7 @@ class Meta: example="http://192.168.56.101:8020", ) routing_keys = fields.List( - fields.Str(description="Routing key", **INDY_RAW_PUBLIC_KEY), + fields.Str(description="Routing key", **ROUTING_KEY), data_key="routingKeys", required=False, description="List of routing keys", From 578833c8035ff638cbd700dfaf981f4ced00c4da Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Mon, 15 Aug 2022 11:08:37 -0600 Subject: [PATCH 20/27] revert: Changes to non-coordinate mediation related files Signed-off-by: Colton Wolkins (Indicio work address) --- aries_cloudagent/connections/base_manager.py | 10 ++----- .../multitenant/tests/test_base.py | 24 ++++++++--------- .../v1_0/messages/connection_invitation.py | 26 ++++--------------- .../connections/v1_0/tests/test_manager.py | 5 +--- .../didexchange/v1_0/tests/test_manager.py | 5 +--- .../routing/v1_0/models/route_record.py | 21 +-------------- aries_cloudagent/transport/pack_format.py | 13 ++-------- 7 files changed, 23 insertions(+), 81 deletions(-) diff --git a/aries_cloudagent/connections/base_manager.py b/aries_cloudagent/connections/base_manager.py index 6421a47c9c..bd5e281694 100644 --- a/aries_cloudagent/connections/base_manager.py +++ b/aries_cloudagent/connections/base_manager.py @@ -116,15 +116,10 @@ async def create_did_document( raise BaseConnectionManagerError( "Routing DIDDoc service has no recipient key(s)" ) - key = service.recip_keys[0].value - key = ( - DIDKey.from_did(key).public_key_b58 - if key.startswith("did:key:") else key - ) rk = PublicKey( did_info.did, f"routing-{router_idx}", - key, + service.recip_keys[0].value, PublicKeyType.ED25519_SIG_2018, did_controller, True, @@ -140,8 +135,7 @@ async def create_did_document( PublicKey( did_info.did, f"routing-{idx}", - DIDKey.from_did(key).public_key_b58 - if key.startswith("did:key:") else key, + key, PublicKeyType.ED25519_SIG_2018, did_controller, # TODO: get correct controller did_info True, # TODO: should this be true? diff --git a/aries_cloudagent/multitenant/tests/test_base.py b/aries_cloudagent/multitenant/tests/test_base.py index f5a67249a4..5d37297b9d 100644 --- a/aries_cloudagent/multitenant/tests/test_base.py +++ b/aries_cloudagent/multitenant/tests/test_base.py @@ -25,8 +25,6 @@ from ..error import WalletKeyMissingError from .. import base as test_module -RECIPIENT_KEY = "H3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV" - class MockMultitenantManager(BaseMultitenantManager): async def get_wallet_profile( @@ -143,7 +141,7 @@ async def test_wallet_exists_false(self): assert wallet_name_exists is False async def test_get_wallet_by_key_routing_record_does_not_exist(self): - recipient_key = RECIPIENT_KEY + recipient_key = "test" with async_mock.patch.object(WalletRecord, "retrieve_by_id") as retrieve_by_id: wallet = await self.manager._get_wallet_by_key(recipient_key) @@ -154,7 +152,7 @@ async def test_get_wallet_by_key_routing_record_does_not_exist(self): await self.manager._get_wallet_by_key(recipient_key) async def test_get_wallet_by_key_wallet_record_does_not_exist(self): - recipient_key = RECIPIENT_KEY + recipient_key = "test-recipient-key" wallet_id = "test-wallet-id" route_record = RouteRecord(wallet_id=wallet_id, recipient_key=recipient_key) @@ -165,7 +163,7 @@ async def test_get_wallet_by_key_wallet_record_does_not_exist(self): await self.manager._get_wallet_by_key(recipient_key) async def test_get_wallet_by_key(self): - recipient_key = RECIPIENT_KEY + recipient_key = "test-recipient-key" wallet_record = WalletRecord(settings={}) async with self.profile.session() as session: @@ -358,10 +356,10 @@ async def test_add_key_no_mediation(self): ) as create_route_record, async_mock.patch.object( MediationManager, "add_key" ) as mediation_add_key: - await self.manager.add_key("wallet_id", RECIPIENT_KEY) + await self.manager.add_key("wallet_id", "recipient_key") create_route_record.assert_called_once_with( - recipient_key=RECIPIENT_KEY, internal_wallet_id="wallet_id" + recipient_key="recipient_key", internal_wallet_id="wallet_id" ) mediation_add_key.assert_not_called() @@ -374,11 +372,11 @@ async def test_add_key_skip_if_exists_does_not_exist(self): retrieve_by_recipient_key.side_effect = StorageNotFoundError() await self.manager.add_key( - "wallet_id", RECIPIENT_KEY, skip_if_exists=True + "wallet_id", "recipient_key", skip_if_exists=True ) create_route_record.assert_called_once_with( - recipient_key=RECIPIENT_KEY, internal_wallet_id="wallet_id" + recipient_key="recipient_key", internal_wallet_id="wallet_id" ) async def test_add_key_skip_if_exists_does_exist(self): @@ -388,7 +386,7 @@ async def test_add_key_skip_if_exists_does_exist(self): RouteRecord, "retrieve_by_recipient_key" ) as retrieve_by_recipient_key: await self.manager.add_key( - "wallet_id", RECIPIENT_KEY, skip_if_exists=True + "wallet_id", "recipient_key", skip_if_exists=True ) create_route_record.assert_not_called() @@ -407,14 +405,14 @@ async def test_add_key_mediation(self): get_default_mediator.return_value = default_mediator mediation_add_key.return_value = keylist_updates - await self.manager.add_key("wallet_id", RECIPIENT_KEY) + await self.manager.add_key("wallet_id", "recipient_key") create_route_record.assert_called_once_with( - recipient_key=RECIPIENT_KEY, internal_wallet_id="wallet_id" + recipient_key="recipient_key", internal_wallet_id="wallet_id" ) get_default_mediator.assert_called_once() - mediation_add_key.assert_called_once_with(RECIPIENT_KEY) + mediation_add_key.assert_called_once_with("recipient_key") self.responder.send.assert_called_once_with( keylist_updates, connection_id=default_mediator.connection_id ) diff --git a/aries_cloudagent/protocols/connections/v1_0/messages/connection_invitation.py b/aries_cloudagent/protocols/connections/v1_0/messages/connection_invitation.py index 4f0d7d68e6..48ca887989 100644 --- a/aries_cloudagent/protocols/connections/v1_0/messages/connection_invitation.py +++ b/aries_cloudagent/protocols/connections/v1_0/messages/connection_invitation.py @@ -5,9 +5,8 @@ from marshmallow import EXCLUDE, fields, validates_schema, ValidationError -from .....did.did_key import DIDKey from .....messaging.agent_message import AgentMessage, AgentMessageSchema -from .....messaging.valid import INDY_DID, ROUTING_KEY +from .....messaging.valid import INDY_DID, INDY_RAW_PUBLIC_KEY from .....wallet.util import b64_to_bytes, bytes_to_b64 from ..message_types import CONNECTION_INVITATION, PROTOCOL_PACKAGE @@ -54,26 +53,11 @@ def __init__( super().__init__(**kwargs) self.label = label self.did = did + self.recipient_keys = list(recipient_keys) if recipient_keys else None self.endpoint = endpoint + self.routing_keys = list(routing_keys) if routing_keys else None self.image_url = image_url - if recipient_keys: - public_keys = list() - for key in recipient_keys: - public_keys.append( - DIDKey.from_did(key).public_key_b58 - if key.startswith("did:key:") else key - ) - self.recipient_keys = public_keys if recipient_keys else None - if routing_keys: - public_keys = list() - for key in routing_keys: - public_keys.append( - DIDKey.from_did(key).public_key_b58 - if key.startswith("did:key:") else key - ) - self.routing_keys = public_keys if routing_keys else None - def to_url(self, base_url: str = None) -> str: """ Convert an invitation to URL format for sharing. @@ -125,7 +109,7 @@ class Meta: required=False, description="DID for connection invitation", **INDY_DID ) recipient_keys = fields.List( - fields.Str(description="Recipient public key", **ROUTING_KEY), + fields.Str(description="Recipient public key", **INDY_RAW_PUBLIC_KEY), data_key="recipientKeys", required=False, description="List of recipient keys", @@ -137,7 +121,7 @@ class Meta: example="http://192.168.56.101:8020", ) routing_keys = fields.List( - fields.Str(description="Routing key", **ROUTING_KEY), + fields.Str(description="Routing key", **INDY_RAW_PUBLIC_KEY), data_key="routingKeys", required=False, description="List of routing keys", diff --git a/aries_cloudagent/protocols/connections/v1_0/tests/test_manager.py b/aries_cloudagent/protocols/connections/v1_0/tests/test_manager.py index a0a8ca5829..c1b1bb0ba1 100644 --- a/aries_cloudagent/protocols/connections/v1_0/tests/test_manager.py +++ b/aries_cloudagent/protocols/connections/v1_0/tests/test_manager.py @@ -1052,10 +1052,7 @@ async def test_receive_request_mediation_id(self): assert len(message.updates) == 1 (remove,) = message.updates assert remove.action == KeylistUpdateRule.RULE_REMOVE - did_key = DIDKey.from_public_key_b58( - record.invitation_key, KeyType.ED25519 - ).did - assert remove.recipient_key == did_key + assert remove.recipient_key == record.invitation_key async def test_receive_request_bad_mediation(self): mock_request = async_mock.MagicMock() diff --git a/aries_cloudagent/protocols/didexchange/v1_0/tests/test_manager.py b/aries_cloudagent/protocols/didexchange/v1_0/tests/test_manager.py index 2eb10cd20d..3ffd26a181 100644 --- a/aries_cloudagent/protocols/didexchange/v1_0/tests/test_manager.py +++ b/aries_cloudagent/protocols/didexchange/v1_0/tests/test_manager.py @@ -717,10 +717,7 @@ async def test_receive_request_with_mediator_without_multi_use_multitenant(self) assert len(message.updates) == 1 (remove,) = message.updates assert remove.action == KeylistUpdateRule.RULE_REMOVE - did_key = DIDKey.from_public_key_b58( - record.invitation_key, KeyType.ED25519 - ).did - assert remove.recipient_key == did_key + assert remove.recipient_key == record.invitation_key async def test_receive_request_with_mediator_without_multi_use_multitenant_mismatch( self, diff --git a/aries_cloudagent/protocols/routing/v1_0/models/route_record.py b/aries_cloudagent/protocols/routing/v1_0/models/route_record.py index f0016a979c..e750cd15eb 100644 --- a/aries_cloudagent/protocols/routing/v1_0/models/route_record.py +++ b/aries_cloudagent/protocols/routing/v1_0/models/route_record.py @@ -4,8 +4,6 @@ from .....core.profile import ProfileSession from .....messaging.models.base_record import BaseRecord, BaseRecordSchema -from .....did.did_key import DIDKey -from .....wallet.key_type import KeyType class RouteRecord(BaseRecord): @@ -77,24 +75,7 @@ async def retrieve_by_recipient_key( RouteRecord: retrieved route record """ - if recipient_key.startswith("did:key:"): - recipient_key = ( - DIDKey.from_did(recipient_key) - ) - # recipient_key = recipient_key.public_key_b58 - else: - recipient_key = ( - DIDKey.from_public_key_b58( - recipient_key, - KeyType.ED25519, - ) - ) - # record = await RouteRecord.retrieve_by_recipient_key( - tag_filter = {"recipient_key": {"$in": [ - recipient_key.did, - recipient_key.public_key_b58, - ]}} - # tag_filter = {"recipient_key": recipient_key} + tag_filter = {"recipient_key": recipient_key} return await cls.retrieve_by_tag_filter(session, tag_filter) @classmethod diff --git a/aries_cloudagent/transport/pack_format.py b/aries_cloudagent/transport/pack_format.py index 2c078868e1..f982748098 100644 --- a/aries_cloudagent/transport/pack_format.py +++ b/aries_cloudagent/transport/pack_format.py @@ -7,7 +7,6 @@ from ..core.profile import ProfileSession from ..protocols.routing.v1_0.messages.forward import Forward -from ..did.did_key import DIDKey from ..messaging.util import time_now from ..utils.task_queue import TaskQueue @@ -176,28 +175,20 @@ async def pack( if not wallet: raise WireFormatEncodeError("No wallet instance") - recip_keys = [] - for key in recipient_keys: - if key.startswith("did:key:"): - recip_keys.append(DIDKey.from_did(key).public_key_b58) - else: - recip_keys.append(key) - try: message = await wallet.pack_message( - message_json, recip_keys, sender_key + message_json, recipient_keys, sender_key ) except WalletError as e: raise WireFormatEncodeError("Message pack failed") from e if routing_keys: + recip_keys = recipient_keys for router_key in routing_keys: message = json.loads(message.decode("utf-8")) fwd_msg = Forward(to=recip_keys[0], msg=message) # Forwards are anon packed recip_keys = [router_key] - if router_key.startswith("did:key:"): - recip_keys = [DIDKey.from_did(router_key).public_key_b58] try: message = await wallet.pack_message(fwd_msg.to_json(), recip_keys) except WalletError as e: From f69632385c98e7ea9a81bcc15f499a55c84ce141 Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Mon, 15 Aug 2022 13:48:11 -0600 Subject: [PATCH 21/27] fix: Limit changes to the coordinate mediation protocol Signed-off-by: Colton Wolkins (Indicio work address) --- .../handlers/tests/test_mediation_grant_handler.py | 3 ++- .../protocols/coordinate_mediation/v1_0/manager.py | 10 +++++++++- .../v1_0/tests/test_mediation_manager.py | 6 +++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/handlers/tests/test_mediation_grant_handler.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/handlers/tests/test_mediation_grant_handler.py index fd75c1a0d6..e8924dbb83 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/handlers/tests/test_mediation_grant_handler.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/handlers/tests/test_mediation_grant_handler.py @@ -18,6 +18,7 @@ from .. import mediation_grant_handler as test_module TEST_CONN_ID = "conn-id" +TEST_RECORD_VERKEY = "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx" TEST_VERKEY = "did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL" TEST_ENDPOINT = "https://example.com" @@ -58,7 +59,7 @@ async def test_handler(self): assert record assert record.state == MediationRecord.STATE_GRANTED assert record.endpoint == TEST_ENDPOINT - assert record.routing_keys == [TEST_VERKEY] + assert record.routing_keys == [TEST_RECORD_VERKEY] async def test_handler_connection_has_set_to_default_meta(self): handler, responder = MediationGrantHandler(), MockResponder() diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py index 195dd5fd5a..0cd5744b92 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py @@ -6,6 +6,7 @@ from ....core.error import BaseError from ....core.profile import Profile, ProfileSession +from ....did.did_key import DIDKey from ....storage.base import BaseStorage from ....storage.error import StorageNotFoundError from ....storage.record import StorageRecord @@ -447,7 +448,14 @@ async def request_granted(self, record: MediationRecord, grant: MediationGrant): """ record.state = MediationRecord.STATE_GRANTED record.endpoint = grant.endpoint - record.routing_keys = grant.routing_keys + # record.routing_keys = grant.routing_keys + routing_keys = [] + for key in grant.routing_keys: + routing_keys.append( + DIDKey.from_did(key).public_key_b58 + if key.startswith("did:key:") else key + ) + record.routing_keys = routing_keys async with self._profile.session() as session: await record.save(session, reason="Mediation request granted.") diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_mediation_manager.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_mediation_manager.py index 4f86b8eee4..dc0c42766c 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_mediation_manager.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_mediation_manager.py @@ -29,9 +29,9 @@ TEST_CONN_ID = "conn-id" TEST_THREAD_ID = "thread-id" TEST_ENDPOINT = "https://example.com" -# TEST_VERKEY = "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx" +# TEST_RECORD_VERKEY = "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx" TEST_VERKEY = "did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL" -# TEST_ROUTE_VERKEY = "9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC" +TEST_ROUTE_RECORD_VERKEY = "9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC" TEST_ROUTE_VERKEY = "did:key:z6MknxTj6Zj1VrDWc1ofaZtmCVv2zNXpD58Xup4ijDGoQhya" pytestmark = pytest.mark.asyncio @@ -294,7 +294,7 @@ async def test_request_granted(self, manager): await manager.request_granted(record, grant) assert record.state == MediationRecord.STATE_GRANTED assert record.endpoint == TEST_ENDPOINT - assert record.routing_keys == [TEST_ROUTE_VERKEY] + assert record.routing_keys == [TEST_ROUTE_RECORD_VERKEY] async def test_request_denied(self, manager): """test_request_denied.""" From 834bfb19a9631e7a6c26d04506b5d8ec4bd7253c Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Tue, 16 Aug 2022 09:33:00 -0600 Subject: [PATCH 22/27] fix: Fix unit tests Signed-off-by: Colton Wolkins (Indicio work address) --- .../multitenant/tests/test_route_manager.py | 41 +++++++++++-------- .../v1_0/tests/test_route_manager.py | 26 +++++++----- 2 files changed, 38 insertions(+), 29 deletions(-) diff --git a/aries_cloudagent/multitenant/tests/test_route_manager.py b/aries_cloudagent/multitenant/tests/test_route_manager.py index 87bfef7129..75f4547a0a 100644 --- a/aries_cloudagent/multitenant/tests/test_route_manager.py +++ b/aries_cloudagent/multitenant/tests/test_route_manager.py @@ -13,6 +13,11 @@ from ...storage.error import StorageNotFoundError from ..route_manager import MultitenantRouteManager +TEST_RECORD_VERKEY = "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx" +TEST_VERKEY = "did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL" +TEST_ROUTE_RECORD_VERKEY = "9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC" +TEST_ROUTE_VERKEY = "did:key:z6MknxTj6Zj1VrDWc1ofaZtmCVv2zNXpD58Xup4ijDGoQhya" + @pytest.fixture def wallet_id(): @@ -68,18 +73,18 @@ async def test_route_for_key_sub_mediator_no_base_mediator( ) as mock_create_route_record: keylist_update = await route_manager._route_for_key( sub_profile, - "test-recipient-key", + TEST_VERKEY, mediation_record, skip_if_exists=False, replace_key=None, ) mock_create_route_record.assert_called_once_with( - recipient_key="test-recipient-key", internal_wallet_id=wallet_id + recipient_key=TEST_VERKEY, internal_wallet_id=wallet_id ) assert keylist_update assert keylist_update.serialize()["updates"] == [ - {"action": "add", "recipient_key": "test-recipient-key"} + {"action": "add", "recipient_key": TEST_VERKEY} ] assert mock_responder.messages assert ( @@ -112,18 +117,18 @@ async def test_route_for_key_sub_mediator_and_base_mediator( ) as mock_create_route_record: keylist_update = await route_manager._route_for_key( sub_profile, - "test-recipient-key", + TEST_VERKEY, mediation_record, skip_if_exists=False, replace_key=None, ) mock_create_route_record.assert_called_once_with( - recipient_key="test-recipient-key", internal_wallet_id=wallet_id + recipient_key=TEST_VERKEY, internal_wallet_id=wallet_id ) assert keylist_update assert keylist_update.serialize()["updates"] == [ - {"action": "add", "recipient_key": "test-recipient-key"} + {"action": "add", "recipient_key": TEST_VERKEY} ] assert mock_responder.messages assert ( @@ -153,18 +158,18 @@ async def test_route_for_key_base_mediator_no_sub_mediator( ) as mock_create_route_record: keylist_update = await route_manager._route_for_key( sub_profile, - "test-recipient-key", + TEST_VERKEY, None, skip_if_exists=False, replace_key=None, ) mock_create_route_record.assert_called_once_with( - recipient_key="test-recipient-key", internal_wallet_id=wallet_id + recipient_key=TEST_VERKEY, internal_wallet_id=wallet_id ) assert keylist_update assert keylist_update.serialize()["updates"] == [ - {"action": "add", "recipient_key": "test-recipient-key"} + {"action": "add", "recipient_key": TEST_VERKEY} ] assert mock_responder.messages assert ( @@ -187,7 +192,7 @@ async def test_route_for_key_skip_if_exists_and_exists( ): keylist_update = await route_manager._route_for_key( sub_profile, - "test-recipient-key", + TEST_VERKEY, mediation_record, skip_if_exists=True, replace_key=None, @@ -212,14 +217,14 @@ async def test_route_for_key_skip_if_exists_and_absent( ): keylist_update = await route_manager._route_for_key( sub_profile, - "test-recipient-key", + TEST_VERKEY, mediation_record, skip_if_exists=True, replace_key=None, ) assert keylist_update assert keylist_update.serialize()["updates"] == [ - {"action": "add", "recipient_key": "test-recipient-key"} + {"action": "add", "recipient_key": TEST_VERKEY} ] assert mock_responder.messages assert ( @@ -239,15 +244,15 @@ async def test_route_for_key_replace_key( ) keylist_update = await route_manager._route_for_key( sub_profile, - "test-recipient-key", + TEST_VERKEY, mediation_record, skip_if_exists=False, - replace_key="test-replace-key", + replace_key=TEST_ROUTE_VERKEY, ) assert keylist_update assert keylist_update.serialize()["updates"] == [ - {"action": "add", "recipient_key": "test-recipient-key"}, - {"action": "remove", "recipient_key": "test-replace-key"}, + {"action": "add", "recipient_key": TEST_VERKEY}, + {"action": "remove", "recipient_key": TEST_ROUTE_VERKEY}, ] assert mock_responder.messages assert ( @@ -264,10 +269,10 @@ async def test_route_for_key_no_mediator( assert ( await route_manager._route_for_key( sub_profile, - "test-recipient-key", + TEST_VERKEY, None, skip_if_exists=True, - replace_key="test-replace-key", + replace_key=TEST_ROUTE_VERKEY, ) is None ) diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_route_manager.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_route_manager.py index dcb315337b..cd56195317 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_route_manager.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_route_manager.py @@ -18,6 +18,10 @@ RouteManagerError, ) +TEST_RECORD_VERKEY = "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx" +TEST_VERKEY = "did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL" +TEST_ROUTE_RECORD_VERKEY = "9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC" +TEST_ROUTE_VERKEY = "did:key:z6MknxTj6Zj1VrDWc1ofaZtmCVv2zNXpD58Xup4ijDGoQhya" class MockRouteManager(RouteManager): """Concretion of RouteManager for testing.""" @@ -486,14 +490,14 @@ async def test_mediation_route_for_key( ) keylist_update = await mediation_route_manager._route_for_key( profile, - "test-recipient-key", + TEST_VERKEY, mediation_record, skip_if_exists=False, replace_key=None, ) assert keylist_update assert keylist_update.serialize()["updates"] == [ - {"action": "add", "recipient_key": "test-recipient-key"} + {"action": "add", "recipient_key": TEST_VERKEY} ] assert mock_responder.messages assert ( @@ -516,7 +520,7 @@ async def test_mediation_route_for_key_skip_if_exists_and_exists( ): keylist_update = await mediation_route_manager._route_for_key( profile, - "test-recipient-key", + TEST_VERKEY, mediation_record, skip_if_exists=True, replace_key=None, @@ -541,14 +545,14 @@ async def test_mediation_route_for_key_skip_if_exists_and_absent( ): keylist_update = await mediation_route_manager._route_for_key( profile, - "test-recipient-key", + TEST_VERKEY, mediation_record, skip_if_exists=True, replace_key=None, ) assert keylist_update assert keylist_update.serialize()["updates"] == [ - {"action": "add", "recipient_key": "test-recipient-key"} + {"action": "add", "recipient_key": TEST_VERKEY} ] assert mock_responder.messages assert ( @@ -568,15 +572,15 @@ async def test_mediation_route_for_key_replace_key( ) keylist_update = await mediation_route_manager._route_for_key( profile, - "test-recipient-key", + TEST_VERKEY, mediation_record, skip_if_exists=False, - replace_key="test-replace-key", + replace_key=TEST_ROUTE_VERKEY, ) assert keylist_update assert keylist_update.serialize()["updates"] == [ - {"action": "add", "recipient_key": "test-recipient-key"}, - {"action": "remove", "recipient_key": "test-replace-key"}, + {"action": "add", "recipient_key": TEST_VERKEY}, + {"action": "remove", "recipient_key": TEST_ROUTE_VERKEY}, ] assert mock_responder.messages assert ( @@ -593,10 +597,10 @@ async def test_mediation_route_for_key_no_mediator( assert ( await mediation_route_manager._route_for_key( profile, - "test-recipient-key", + TEST_VERKEY, None, skip_if_exists=True, - replace_key="test-replace-key", + replace_key=TEST_ROUTE_VERKEY, ) is None ) From d86f5869dd728c2244c7c597da14bda726429a2a Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Tue, 16 Aug 2022 10:29:24 -0600 Subject: [PATCH 23/27] fix: Normalize recipient keys when updating routes Signed-off-by: Colton Wolkins (Indicio work address) --- .../protocols/coordinate_mediation/v1_0/manager.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py index 72c2f496b8..ec15019ec9 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py @@ -249,9 +249,16 @@ async def update_keylist( RouteUpdate.ACTION_CREATE: KeylistUpdateRule.RULE_ADD, } + def normalize_public_key(key: str): + if key.startswith("did:key:"): + return DIDKey.from_did(key).public_key_b58 + + return key + def rule_to_update(rule: KeylistUpdateRule): + recipient_key = normalize_public_key(rule.recipient_key) return RouteUpdate( - recipient_key=rule.recipient_key, action=action_map[rule.action] + recipient_key=recipient_key, action=action_map[rule.action] ) def updated_to_keylist_updated(updated: RouteUpdated): From 59640da72028262e58bdf92b88575c7b0fe93de3 Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Tue, 16 Aug 2022 11:03:35 -0600 Subject: [PATCH 24/27] ci: Fix coordinate mediation tests Signed-off-by: Colton Wolkins (Indicio work address) --- .../v1_0/tests/test_mediation_manager.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_mediation_manager.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_mediation_manager.py index dc0c42766c..7266c7f32a 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_mediation_manager.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_mediation_manager.py @@ -29,7 +29,7 @@ TEST_CONN_ID = "conn-id" TEST_THREAD_ID = "thread-id" TEST_ENDPOINT = "https://example.com" -# TEST_RECORD_VERKEY = "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx" +TEST_RECORD_VERKEY = "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx" TEST_VERKEY = "did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL" TEST_ROUTE_RECORD_VERKEY = "9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC" TEST_ROUTE_VERKEY = "did:key:z6MknxTj6Zj1VrDWc1ofaZtmCVv2zNXpD58Xup4ijDGoQhya" @@ -133,7 +133,7 @@ async def test_deny_request(self, manager): async def test_update_keylist_delete(self, session, manager, record): """test_update_keylist_delete.""" - await RouteRecord(connection_id=TEST_CONN_ID, recipient_key=TEST_VERKEY).save( + await RouteRecord(connection_id=TEST_CONN_ID, recipient_key=TEST_RECORD_VERKEY).save( session ) response = await manager.update_keylist( @@ -168,7 +168,7 @@ async def test_update_keylist_create(self, manager, record): async def test_update_keylist_create_existing(self, session, manager, record): """test_update_keylist_create_existing.""" - await RouteRecord(connection_id=TEST_CONN_ID, recipient_key=TEST_VERKEY).save( + await RouteRecord(connection_id=TEST_CONN_ID, recipient_key=TEST_RECORD_VERKEY).save( session ) response = await manager.update_keylist( From dc85e32cf6ee0b60d6774e6b7b7b4ebf016ab8e2 Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Tue, 16 Aug 2022 11:17:56 -0600 Subject: [PATCH 25/27] ci: Fix black formatting errors Signed-off-by: Colton Wolkins (Indicio work address) --- aries_cloudagent/messaging/valid.py | 6 ++- .../coordinate_mediation/v1_0/manager.py | 3 +- .../v1_0/messages/inner/keylist_key.py | 6 +-- .../messages/inner/keylist_update_rule.py | 6 +-- .../v1_0/messages/inner/keylist_updated.py | 6 +-- .../v1_0/messages/mediate_grant.py | 16 ++++++-- .../messages/tests/test_keylist_update.py | 4 +- .../v1_0/messages/tests/test_mediate_grant.py | 5 ++- .../v1_0/tests/test_mediation_manager.py | 22 +++++----- .../v1_0/tests/test_route_manager.py | 1 + .../v1_0/tests/test_routes.py | 40 +++++++++++++++---- 11 files changed, 78 insertions(+), 37 deletions(-) diff --git a/aries_cloudagent/messaging/valid.py b/aries_cloudagent/messaging/valid.py index a62d587fff..6253c6b304 100644 --- a/aries_cloudagent/messaging/valid.py +++ b/aries_cloudagent/messaging/valid.py @@ -365,8 +365,10 @@ def __init__(self): super().__init__( RoutingKey.PATTERN, - error=("Value {input} is not in W3C did:key" - " or Ed25519VerificationKey2018 key format"), + error=( + "Value {input} is not in W3C did:key" + " or Ed25519VerificationKey2018 key format" + ), ) diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py index ec15019ec9..c959ea9437 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py @@ -458,7 +458,8 @@ async def request_granted(self, record: MediationRecord, grant: MediationGrant): for key in grant.routing_keys: routing_keys.append( DIDKey.from_did(key).public_key_b58 - if key.startswith("did:key:") else key + if key.startswith("did:key:") + else key ) record.routing_keys = routing_keys async with self._profile.session() as session: diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py index c320ea62aa..7c47a1ea0e 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py @@ -37,9 +37,9 @@ def __init__( if recipient_key.startswith("did:key:"): self.recipient_key = recipient_key else: - self.recipient_key = ( - DIDKey.from_public_key_b58(recipient_key, KeyType.ED25519).did - ) + self.recipient_key = DIDKey.from_public_key_b58( + recipient_key, KeyType.ED25519 + ).did class KeylistKeySchema(BaseModelSchema): diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py index 1ad0dc5d71..a8d85746e8 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py @@ -37,9 +37,9 @@ def __init__(self, recipient_key: str, action: str, **kwargs): if recipient_key.startswith("did:key:"): self.recipient_key = recipient_key else: - self.recipient_key = ( - DIDKey.from_public_key_b58(recipient_key, KeyType.ED25519).did - ) + self.recipient_key = DIDKey.from_public_key_b58( + recipient_key, KeyType.ED25519 + ).did self.action = action diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py index a2e4a11cd2..b2daec9856 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py @@ -45,9 +45,9 @@ def __init__( if recipient_key.startswith("did:key:"): self.recipient_key = recipient_key else: - self.recipient_key = ( - DIDKey.from_public_key_b58(recipient_key, KeyType.ED25519).did - ) + self.recipient_key = DIDKey.from_public_key_b58( + recipient_key, KeyType.ED25519 + ).did self.action = action self.result = result diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py index f15ec524d6..c4da77dc8d 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py @@ -43,10 +43,18 @@ def __init__( """ super(MediationGrant, self).__init__(**kwargs) self.endpoint = endpoint - self.routing_keys = list( - (key if key.startswith("did:key:") - else DIDKey.from_public_key_b58(key, KeyType.ED25519).did) - for key in routing_keys) if routing_keys else [] + self.routing_keys = ( + list( + ( + key + if key.startswith("did:key:") + else DIDKey.from_public_key_b58(key, KeyType.ED25519).did + ) + for key in routing_keys + ) + if routing_keys + else [] + ) class MediationGrantSchema(AgentMessageSchema): diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_keylist_update.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_keylist_update.py index c22a75ba9e..caa0b3c976 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_keylist_update.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_keylist_update.py @@ -15,6 +15,8 @@ class TestKeylistUpdate(MessageTest, TestCase): SCHEMA = KeylistUpdateSchema VALUES = { "updates": [ - KeylistUpdateRule("did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL", "add") + KeylistUpdateRule( + "did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL", "add" + ) ] } diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_mediate_grant.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_mediate_grant.py index 5d24ca330b..9c992ff883 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_mediate_grant.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/tests/test_mediate_grant.py @@ -12,4 +12,7 @@ class TestMediateGrant(MessageTest, TestCase): TYPE = MEDIATE_GRANT CLASS = MediationGrant SCHEMA = MediationGrantSchema - VALUES = {"endpoint": "http://localhost:3000", "routing_keys": ["did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL"]} + VALUES = { + "endpoint": "http://localhost:3000", + "routing_keys": ["did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL"], + } diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_mediation_manager.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_mediation_manager.py index 7266c7f32a..fe541b5488 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_mediation_manager.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_mediation_manager.py @@ -116,11 +116,11 @@ async def test_grant_request(self, session, manager): assert record.connection_id == TEST_CONN_ID record, grant = await manager.grant_request(record.mediation_id) assert grant.endpoint == session.settings.get("default_endpoint") - routing_key = (await manager._retrieve_routing_did(session)) - routing_key = DIDKey.from_public_key_b58(routing_key.verkey, routing_key.key_type).did - assert grant.routing_keys == [ - routing_key - ] + routing_key = await manager._retrieve_routing_did(session) + routing_key = DIDKey.from_public_key_b58( + routing_key.verkey, routing_key.key_type + ).did + assert grant.routing_keys == [routing_key] async def test_deny_request(self, manager): """test_deny_request.""" @@ -133,9 +133,9 @@ async def test_deny_request(self, manager): async def test_update_keylist_delete(self, session, manager, record): """test_update_keylist_delete.""" - await RouteRecord(connection_id=TEST_CONN_ID, recipient_key=TEST_RECORD_VERKEY).save( - session - ) + await RouteRecord( + connection_id=TEST_CONN_ID, recipient_key=TEST_RECORD_VERKEY + ).save(session) response = await manager.update_keylist( record=record, updates=[ @@ -168,9 +168,9 @@ async def test_update_keylist_create(self, manager, record): async def test_update_keylist_create_existing(self, session, manager, record): """test_update_keylist_create_existing.""" - await RouteRecord(connection_id=TEST_CONN_ID, recipient_key=TEST_RECORD_VERKEY).save( - session - ) + await RouteRecord( + connection_id=TEST_CONN_ID, recipient_key=TEST_RECORD_VERKEY + ).save(session) response = await manager.update_keylist( record=record, updates=[ diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_route_manager.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_route_manager.py index cd56195317..2196ec76db 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_route_manager.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_route_manager.py @@ -23,6 +23,7 @@ TEST_ROUTE_RECORD_VERKEY = "9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC" TEST_ROUTE_VERKEY = "did:key:z6MknxTj6Zj1VrDWc1ofaZtmCVv2zNXpD58Xup4ijDGoQhya" + class MockRouteManager(RouteManager): """Concretion of RouteManager for testing.""" diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_routes.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_routes.py index ea86aac6b5..4cbd017a45 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_routes.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/tests/test_routes.py @@ -449,14 +449,26 @@ async def test_get_keylist_storage_error(self): async def test_send_keylist_update(self): body = { "updates": [ - {"recipient_key": "EwUKjVLboiLSuoWSEtDvrgrd41EUxG5bLecQrkHB63Up", "action": "add"}, - {"recipient_key": "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx", "action": "remove"}, + { + "recipient_key": "EwUKjVLboiLSuoWSEtDvrgrd41EUxG5bLecQrkHB63Up", + "action": "add", + }, + { + "recipient_key": "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx", + "action": "remove", + }, ] } body_with_didkey = { "updates": [ - {"recipient_key": "did:key:z6MktPjNKjb39Fpv2JM8vTBmhnQcsaWLN9Kx2fXLh2FC1GGC", "action": "add"}, - {"recipient_key": "did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL", "action": "remove"}, + { + "recipient_key": "did:key:z6MktPjNKjb39Fpv2JM8vTBmhnQcsaWLN9Kx2fXLh2FC1GGC", + "action": "add", + }, + { + "recipient_key": "did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL", + "action": "remove", + }, ] } @@ -484,7 +496,10 @@ async def test_send_keylist_update(self): async def test_send_keylist_update_bad_action(self): self.request.json.return_value = { "updates": [ - {"recipient_key": "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx", "action": "wrong"}, + { + "recipient_key": "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx", + "action": "wrong", + }, ] } @@ -494,7 +509,10 @@ async def test_send_keylist_update_bad_action(self): async def test_send_keylist_update_bad_mediation_state(self): self.request.json.return_value = { "updates": [ - {"recipient_key": "EwUKjVLboiLSuoWSEtDvrgrd41EUxG5bLecQrkHB63Up", "action": "add"}, + { + "recipient_key": "EwUKjVLboiLSuoWSEtDvrgrd41EUxG5bLecQrkHB63Up", + "action": "add", + }, ] } @@ -517,7 +535,10 @@ async def test_send_keylist_update_bad_updates(self): async def test_send_keylist_update_x_no_mediation_rec(self): self.request.json.return_value = { "updates": [ - {"recipient_key": "EwUKjVLboiLSuoWSEtDvrgrd41EUxG5bLecQrkHB63Up", "action": "add"}, + { + "recipient_key": "EwUKjVLboiLSuoWSEtDvrgrd41EUxG5bLecQrkHB63Up", + "action": "add", + }, ] } with async_mock.patch.object( @@ -530,7 +551,10 @@ async def test_send_keylist_update_x_no_mediation_rec(self): async def test_send_keylist_update_x_storage_error(self): self.request.json.return_value = { "updates": [ - {"recipient_key": "EwUKjVLboiLSuoWSEtDvrgrd41EUxG5bLecQrkHB63Up", "action": "add"}, + { + "recipient_key": "EwUKjVLboiLSuoWSEtDvrgrd41EUxG5bLecQrkHB63Up", + "action": "add", + }, ] } From 87c64dc0a7e5fbcd24ce8b5d7e90e22e0b43a06e Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Mon, 22 Aug 2022 08:11:35 -0600 Subject: [PATCH 26/27] chore: Reduce code duplication Signed-off-by: Colton Wolkins (Indicio work address) --- .../coordinate_mediation/v1_0/manager.py | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py index c959ea9437..22fc6887b9 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py @@ -223,6 +223,12 @@ async def deny_request( ) return mediation_record, deny + def normalize_public_key(self, key: str): + if key.startswith("did:key:"): + return DIDKey.from_did(key).public_key_b58 + + return key + async def update_keylist( self, record: MediationRecord, updates: Sequence[KeylistUpdateRule] ) -> KeylistUpdateResponse: @@ -249,14 +255,8 @@ async def update_keylist( RouteUpdate.ACTION_CREATE: KeylistUpdateRule.RULE_ADD, } - def normalize_public_key(key: str): - if key.startswith("did:key:"): - return DIDKey.from_did(key).public_key_b58 - - return key - def rule_to_update(rule: KeylistUpdateRule): - recipient_key = normalize_public_key(rule.recipient_key) + recipient_key = self.normalize_public_key(rule.recipient_key) return RouteUpdate( recipient_key=recipient_key, action=action_map[rule.action] ) @@ -456,11 +456,7 @@ async def request_granted(self, record: MediationRecord, grant: MediationGrant): # record.routing_keys = grant.routing_keys routing_keys = [] for key in grant.routing_keys: - routing_keys.append( - DIDKey.from_did(key).public_key_b58 - if key.startswith("did:key:") - else key - ) + routing_keys.append(self.normalize_public_key(key)) record.routing_keys = routing_keys async with self._profile.session() as session: await record.save(session, reason="Mediation request granted.") From 36f12301b530401579fcb7a9db30af7ca51c6a5c Mon Sep 17 00:00:00 2001 From: "Colton Wolkins (Indicio work address)" Date: Mon, 22 Aug 2022 08:42:45 -0600 Subject: [PATCH 27/27] chore: further reduce code duplication Signed-off-by: Colton Wolkins (Indicio work address) --- .../coordinate_mediation/v1_0/manager.py | 12 +++--------- .../v1_0/messages/inner/keylist_key.py | 10 ++-------- .../messages/inner/keylist_update_rule.py | 10 ++-------- .../v1_0/messages/inner/keylist_updated.py | 10 ++-------- .../v1_0/messages/mediate_grant.py | 12 ++---------- .../v1_0/normalization.py | 19 +++++++++++++++++++ 6 files changed, 30 insertions(+), 43 deletions(-) create mode 100644 aries_cloudagent/protocols/coordinate_mediation/v1_0/normalization.py diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py index 22fc6887b9..f525dd0088 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py @@ -6,7 +6,6 @@ from ....core.error import BaseError from ....core.profile import Profile, ProfileSession -from ....did.did_key import DIDKey from ....storage.base import BaseStorage from ....storage.error import StorageNotFoundError from ....storage.record import StorageRecord @@ -30,6 +29,7 @@ from .messages.mediate_grant import MediationGrant from .messages.mediate_request import MediationRequest from .models.mediation_record import MediationRecord +from .normalization import normalize_from_did_key LOGGER = logging.getLogger(__name__) @@ -223,12 +223,6 @@ async def deny_request( ) return mediation_record, deny - def normalize_public_key(self, key: str): - if key.startswith("did:key:"): - return DIDKey.from_did(key).public_key_b58 - - return key - async def update_keylist( self, record: MediationRecord, updates: Sequence[KeylistUpdateRule] ) -> KeylistUpdateResponse: @@ -256,7 +250,7 @@ async def update_keylist( } def rule_to_update(rule: KeylistUpdateRule): - recipient_key = self.normalize_public_key(rule.recipient_key) + recipient_key = normalize_from_did_key(rule.recipient_key) return RouteUpdate( recipient_key=recipient_key, action=action_map[rule.action] ) @@ -456,7 +450,7 @@ async def request_granted(self, record: MediationRecord, grant: MediationGrant): # record.routing_keys = grant.routing_keys routing_keys = [] for key in grant.routing_keys: - routing_keys.append(self.normalize_public_key(key)) + routing_keys.append(normalize_from_did_key(key)) record.routing_keys = routing_keys async with self._profile.session() as session: await record.save(session, reason="Mediation request granted.") diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py index 7c47a1ea0e..90982731ba 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_key.py @@ -4,8 +4,7 @@ from ......messaging.models.base import BaseModel, BaseModelSchema from ......messaging.valid import DID_KEY -from ......did.did_key import DIDKey -from ......wallet.key_type import KeyType +from ...normalization import normalize_from_public_key class KeylistKey(BaseModel): @@ -34,12 +33,7 @@ def __init__( """ super().__init__(**kwargs) - if recipient_key.startswith("did:key:"): - self.recipient_key = recipient_key - else: - self.recipient_key = DIDKey.from_public_key_b58( - recipient_key, KeyType.ED25519 - ).did + self.recipient_key = normalize_from_public_key(recipient_key) class KeylistKeySchema(BaseModelSchema): diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py index a8d85746e8..de3524d4f9 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_update_rule.py @@ -9,8 +9,7 @@ from ......messaging.models.base import BaseModel, BaseModelSchema from ......messaging.valid import ROUTING_KEY -from ......did.did_key import DIDKey -from ......wallet.key_type import KeyType +from ...normalization import normalize_from_public_key class KeylistUpdateRule(BaseModel): @@ -34,12 +33,7 @@ def __init__(self, recipient_key: str, action: str, **kwargs): """ super().__init__(**kwargs) - if recipient_key.startswith("did:key:"): - self.recipient_key = recipient_key - else: - self.recipient_key = DIDKey.from_public_key_b58( - recipient_key, KeyType.ED25519 - ).did + self.recipient_key = normalize_from_public_key(recipient_key) self.action = action diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py index b2daec9856..388e166451 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/inner/keylist_updated.py @@ -7,8 +7,7 @@ from ......messaging.models.base import BaseModel, BaseModelSchema from ......messaging.valid import DID_KEY -from ......did.did_key import DIDKey -from ......wallet.key_type import KeyType +from ...normalization import normalize_from_public_key class KeylistUpdated(BaseModel): @@ -42,12 +41,7 @@ def __init__( """ super().__init__(**kwargs) - if recipient_key.startswith("did:key:"): - self.recipient_key = recipient_key - else: - self.recipient_key = DIDKey.from_public_key_b58( - recipient_key, KeyType.ED25519 - ).did + self.recipient_key = normalize_from_public_key(recipient_key) self.action = action self.result = result diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py index c4da77dc8d..6f82d8be48 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/messages/mediate_grant.py @@ -8,9 +8,8 @@ from marshmallow import fields from .....messaging.agent_message import AgentMessage, AgentMessageSchema -from .....did.did_key import DIDKey -from .....wallet.key_type import KeyType from ..message_types import MEDIATE_GRANT, PROTOCOL_PACKAGE +from ..normalization import normalize_from_public_key HANDLER_CLASS = ( f"{PROTOCOL_PACKAGE}.handlers.mediation_grant_handler.MediationGrantHandler" @@ -44,14 +43,7 @@ def __init__( super(MediationGrant, self).__init__(**kwargs) self.endpoint = endpoint self.routing_keys = ( - list( - ( - key - if key.startswith("did:key:") - else DIDKey.from_public_key_b58(key, KeyType.ED25519).did - ) - for key in routing_keys - ) + list(normalize_from_public_key(key) for key in routing_keys) if routing_keys else [] ) diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/normalization.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/normalization.py new file mode 100644 index 0000000000..1d8610e60c --- /dev/null +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/normalization.py @@ -0,0 +1,19 @@ +"""Normalization methods used while transitioning to DID:Key method.""" +from ....did.did_key import DIDKey +from ....wallet.key_type import KeyType + + +def normalize_from_did_key(key: str): + """Normalize Recipient/Routing keys from DID:Key to public keys.""" + if key.startswith("did:key:"): + return DIDKey.from_did(key).public_key_b58 + + return key + + +def normalize_from_public_key(key: str): + """Normalize Recipient/Routing keys from public keys to DID:Key.""" + if key.startswith("did:key:"): + return key + + return DIDKey.from_public_key_b58(key, KeyType.ED25519).did