From 27c3b3caf1483ccaa384fee3a9748db639bd94b0 Mon Sep 17 00:00:00 2001 From: Sacha Kozma Date: Wed, 17 May 2023 17:30:30 +0200 Subject: [PATCH 1/5] refactor: put _get_verification_method in an injectable class Signed-off-by: Sacha Kozma --- aries_cloudagent/config/default_context.py | 4 ++ .../v2_0/formats/ld_proof/handler.py | 27 +++++------- .../formats/ld_proof/tests/test_handler.py | 26 ++++------- .../present_proof/dif/pres_exch_handler.py | 22 ++++------ .../dif/tests/test_pres_exch_handler.py | 13 ------ .../default_verification_key_strategy.py | 43 +++++++++++++++++++ .../test_default_verification_key_strategy.py | 27 ++++++++++++ 7 files changed, 100 insertions(+), 62 deletions(-) create mode 100644 aries_cloudagent/wallet/default_verification_key_strategy.py create mode 100644 aries_cloudagent/wallet/tests/test_default_verification_key_strategy.py diff --git a/aries_cloudagent/config/default_context.py b/aries_cloudagent/config/default_context.py index 360f103fa1..2194c1e077 100644 --- a/aries_cloudagent/config/default_context.py +++ b/aries_cloudagent/config/default_context.py @@ -17,6 +17,7 @@ from ..transport.wire_format import BaseWireFormat from ..utils.dependencies import is_indy_sdk_module_installed from ..utils.stats import Collector +from ..wallet.default_verification_key_strategy import DefaultVerificationKeyStrategy from ..wallet.did_method import DIDMethods from ..wallet.key_type import KeyTypes from .base_context import ContextBuilder @@ -53,6 +54,9 @@ async def build_context(self) -> InjectionContext: context.injector.bind_instance(DIDResolver, DIDResolver([])) context.injector.bind_instance(DIDMethods, DIDMethods()) context.injector.bind_instance(KeyTypes, KeyTypes()) + context.injector.bind_instance( + DefaultVerificationKeyStrategy, DefaultVerificationKeyStrategy() + ) await self.bind_providers(context) await self.load_plugins(context) diff --git a/aries_cloudagent/protocols/issue_credential/v2_0/formats/ld_proof/handler.py b/aries_cloudagent/protocols/issue_credential/v2_0/formats/ld_proof/handler.py index c4fccc02d5..81a0f60ab2 100644 --- a/aries_cloudagent/protocols/issue_credential/v2_0/formats/ld_proof/handler.py +++ b/aries_cloudagent/protocols/issue_credential/v2_0/formats/ld_proof/handler.py @@ -12,7 +12,6 @@ from pyld import jsonld from pyld.jsonld import JsonLdProcessor -from ......did.did_key import DIDKey from ......messaging.decorators.attach_decorator import AttachDecorator from ......storage.vc_holder.base import VCHolder from ......storage.vc_holder.vc_record import VCRecord @@ -35,6 +34,9 @@ ) from ......vc.ld_proofs.constants import SECURITY_CONTEXT_BBS_URL from ......wallet.base import BaseWallet, DIDInfo +from ......wallet.default_verification_key_strategy import ( + DefaultVerificationKeyStrategy, +) from ......wallet.error import WalletNotFoundError from ......wallet.key_type import BLS12381G2, ED25519 @@ -270,9 +272,13 @@ async def _get_suite_for_detail( ) did_info = await self._did_info_for_did(issuer_id) - verification_method = verification_method or self._get_verification_method( - issuer_id - ) + verkey_id_strategy = self.profile.context.inject(DefaultVerificationKeyStrategy) + verification_method = verkey_id_strategy.get_verkey_id_for_did(issuer_id) + + if verification_method is None: + raise V20CredFormatError( + f"Unable to get retrieve verification method for did {issuer_id}" + ) suite = await self._get_suite( proof_type=proof_type, @@ -309,19 +315,6 @@ async def _get_suite( ), ) - def _get_verification_method(self, did: str): - """Get the verification method for a did.""" - - if did.startswith("did:key:"): - return DIDKey.from_did(did).key_id - elif did.startswith("did:sov:"): - # key-1 is what the resolver uses for key id - return did + "#key-1" - else: - raise V20CredFormatError( - f"Unable to get retrieve verification method for did {did}" - ) - def _get_proof_purpose( self, *, proof_purpose: str = None, challenge: str = None, domain: str = None ) -> ProofPurpose: diff --git a/aries_cloudagent/protocols/issue_credential/v2_0/formats/ld_proof/tests/test_handler.py b/aries_cloudagent/protocols/issue_credential/v2_0/formats/ld_proof/tests/test_handler.py index b89dad7da5..5384d0d132 100644 --- a/aries_cloudagent/protocols/issue_credential/v2_0/formats/ld_proof/tests/test_handler.py +++ b/aries_cloudagent/protocols/issue_credential/v2_0/formats/ld_proof/tests/test_handler.py @@ -26,6 +26,9 @@ ) from .......vc.ld_proofs.constants import SECURITY_CONTEXT_BBS_URL from .......vc.tests.document_loader import custom_document_loader +from .......wallet.default_verification_key_strategy import ( + DefaultVerificationKeyStrategy, +) from .......wallet.key_type import BLS12381G2, ED25519 from .......wallet.error import WalletNotFoundError from .......wallet.did_method import SOV @@ -124,6 +127,11 @@ async def setUp(self): # Set custom document loader self.context.injector.bind_instance(DocumentLoader, custom_document_loader) + # Set default verkey ID strategy + self.context.injector.bind_instance( + DefaultVerificationKeyStrategy, DefaultVerificationKeyStrategy() + ) + self.handler = LDProofCredFormatHandler(self.profile) self.cred_proposal = V20CredProposal( @@ -318,24 +326,6 @@ async def test_get_suite(self): assert suite.key_pair.key_type == ED25519 assert suite.key_pair.public_key_base58 == did_info.verkey - async def test_get_verification_method(self): - assert ( - self.handler._get_verification_method(TEST_DID_KEY) - == DIDKey.from_did(TEST_DID_KEY).key_id - ) - - assert ( - self.handler._get_verification_method(TEST_DID_SOV) - == TEST_DID_SOV + "#key-1" - ) - - with self.assertRaises(V20CredFormatError) as context: - self.handler._get_verification_method("did:random:not-supported") - - assert "Unable to get retrieve verification method for did" in str( - context.exception - ) - async def test_get_proof_purpose(self): purpose = self.handler._get_proof_purpose() assert type(purpose) == CredentialIssuancePurpose diff --git a/aries_cloudagent/protocols/present_proof/dif/pres_exch_handler.py b/aries_cloudagent/protocols/present_proof/dif/pres_exch_handler.py index 76cde3729d..239a9f7aec 100644 --- a/aries_cloudagent/protocols/present_proof/dif/pres_exch_handler.py +++ b/aries_cloudagent/protocols/present_proof/dif/pres_exch_handler.py @@ -24,7 +24,6 @@ from ....core.error import BaseError from ....core.profile import Profile -from ....did.did_key import DIDKey from ....storage.vc_holder.vc_record import VCRecord from ....vc.ld_proofs import ( Ed25519Signature2018, @@ -39,6 +38,7 @@ ) from ....vc.vc_ld.prove import sign_presentation, create_presentation, derive_credential from ....wallet.base import BaseWallet, DIDInfo +from ....wallet.default_verification_key_strategy import DefaultVerificationKeyStrategy from ....wallet.error import WalletError, WalletNotFoundError from ....wallet.key_type import BLS12381G2, ED25519 @@ -117,7 +117,13 @@ async def _get_issue_suite( ): """Get signature suite for signing presentation.""" did_info = await self._did_info_for_did(issuer_id) - verification_method = self._get_verification_method(issuer_id) + verkey_id_strategy = self.profile.context.inject(DefaultVerificationKeyStrategy) + verification_method = verkey_id_strategy.get_verkey_id_for_did(issuer_id) + + if verification_method is None: + raise DIFPresExchError( + f"Unable to get retrieve verification method for did {issuer_id}" + ) # Get signature class based on proof type SignatureClass = self.PROOF_TYPE_SIGNATURE_SUITE_MAPPING[self.proof_type] @@ -151,18 +157,6 @@ async def _get_derive_suite( ), ) - def _get_verification_method(self, did: str): - """Get the verification method for a did.""" - if did.startswith("did:key:"): - return DIDKey.from_did(did).key_id - elif did.startswith("did:sov:"): - # key-1 is what uniresolver uses for key id - return did + "#key-1" - else: - raise DIFPresExchError( - f"Unable to get retrieve verification method for did {did}" - ) - async def _did_info_for_did(self, did: str) -> DIDInfo: """Get the did info for specified did. diff --git a/aries_cloudagent/protocols/present_proof/dif/tests/test_pres_exch_handler.py b/aries_cloudagent/protocols/present_proof/dif/tests/test_pres_exch_handler.py index 87597359cc..6771dfc19c 100644 --- a/aries_cloudagent/protocols/present_proof/dif/tests/test_pres_exch_handler.py +++ b/aries_cloudagent/protocols/present_proof/dif/tests/test_pres_exch_handler.py @@ -1867,19 +1867,6 @@ def test_cred_schema_match_b(self, profile, setup_tuple): test_cred, "https://example.org/examples/degree.json" ) - def test_verification_method(self, profile): - dif_pres_exch_handler = DIFPresExchHandler(profile) - assert ( - dif_pres_exch_handler._get_verification_method( - "did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL" - ) - == DIDKey.from_did( - "did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL" - ).key_id - ) - with pytest.raises(DIFPresExchError): - dif_pres_exch_handler._get_verification_method("did:test:test") - @pytest.mark.asyncio @pytest.mark.ursa_bbs_signatures async def test_sign_pres_no_cred_subject_id(self, profile, setup_tuple): diff --git a/aries_cloudagent/wallet/default_verification_key_strategy.py b/aries_cloudagent/wallet/default_verification_key_strategy.py new file mode 100644 index 0000000000..2ca51ab402 --- /dev/null +++ b/aries_cloudagent/wallet/default_verification_key_strategy.py @@ -0,0 +1,43 @@ +"""Utilities for specifying which verification method is in use for a given DID.""" +from abc import ABC, abstractmethod +from typing import Optional + +from aries_cloudagent.did.did_key import DIDKey + + +class DefaultVerificationKeyStrategyBase(ABC): + """Base class for defining which verification method is in use.""" + + @abstractmethod + def get_verkey_id_for_did(self, did) -> Optional[str]: + """Given a DID, returns the verification key ID in use. + + Returns None if no strategy is specified for this DID. + + :params str did: the did + :returns Optional[str]: the current verkey ID + """ + pass + + +class DefaultVerificationKeyStrategy(DefaultVerificationKeyStrategyBase): + """A basic implementation for verkey strategy. + + Supports did:key: and did:sov only. + """ + + def get_verkey_id_for_did(self, did) -> Optional[str]: + """Given a did:key or did:sov, returns the verification key ID in use. + + Returns None if no strategy is specified for this DID. + + :params str did: the did + :returns Optional[str]: the current verkey ID + """ + if did.startswith("did:key:"): + return DIDKey.from_did(did).key_id + elif did.startswith("did:sov:"): + # key-1 is what uniresolver uses for key id + return did + "#key-1" + + return None diff --git a/aries_cloudagent/wallet/tests/test_default_verification_key_strategy.py b/aries_cloudagent/wallet/tests/test_default_verification_key_strategy.py new file mode 100644 index 0000000000..d68ca45653 --- /dev/null +++ b/aries_cloudagent/wallet/tests/test_default_verification_key_strategy.py @@ -0,0 +1,27 @@ +from unittest import TestCase + +from aries_cloudagent.did.did_key import DIDKey + +from aries_cloudagent.wallet.default_verification_key_strategy import ( + DefaultVerificationKeyStrategy, +) + +TEST_DID_SOV = "did:sov:LjgpST2rjsoxYegQDRm7EL" +TEST_DID_KEY = "did:key:z6Mkgg342Ycpuk263R9d8Aq6MUaxPn1DDeHyGo38EefXmgDL" + + +class TestDefaultVerificationKeyStrategy(TestCase): + def test_with_did_sov(self): + strategy = DefaultVerificationKeyStrategy() + assert strategy.get_verkey_id_for_did(TEST_DID_SOV) == TEST_DID_SOV + "#key-1" + + def test_with_did_key(self): + strategy = DefaultVerificationKeyStrategy() + assert ( + strategy.get_verkey_id_for_did(TEST_DID_KEY) + == DIDKey.from_did(TEST_DID_KEY).key_id + ) + + def test_unsupported_did_method(self): + strategy = DefaultVerificationKeyStrategy() + assert strategy.get_verkey_id_for_did("did:test:test") is None From 6001328c40d1d012ff625a802c0eb3c56b1900fe Mon Sep 17 00:00:00 2001 From: Sacha Kozma Date: Mon, 22 May 2023 11:49:15 +0200 Subject: [PATCH 2/5] fix: use given verification_method when given + renaming Signed-off-by: Sacha Kozma --- .../issue_credential/v2_0/formats/ld_proof/handler.py | 5 ++++- .../protocols/present_proof/dif/pres_exch_handler.py | 4 +++- .../wallet/default_verification_key_strategy.py | 4 ++-- .../tests/test_default_verification_key_strategy.py | 9 ++++++--- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/aries_cloudagent/protocols/issue_credential/v2_0/formats/ld_proof/handler.py b/aries_cloudagent/protocols/issue_credential/v2_0/formats/ld_proof/handler.py index 81a0f60ab2..0ea8aabf93 100644 --- a/aries_cloudagent/protocols/issue_credential/v2_0/formats/ld_proof/handler.py +++ b/aries_cloudagent/protocols/issue_credential/v2_0/formats/ld_proof/handler.py @@ -273,7 +273,10 @@ async def _get_suite_for_detail( did_info = await self._did_info_for_did(issuer_id) verkey_id_strategy = self.profile.context.inject(DefaultVerificationKeyStrategy) - verification_method = verkey_id_strategy.get_verkey_id_for_did(issuer_id) + verification_method = ( + verification_method + or verkey_id_strategy.get_verification_method_id_for_did(issuer_id) + ) if verification_method is None: raise V20CredFormatError( diff --git a/aries_cloudagent/protocols/present_proof/dif/pres_exch_handler.py b/aries_cloudagent/protocols/present_proof/dif/pres_exch_handler.py index 239a9f7aec..191b9676bb 100644 --- a/aries_cloudagent/protocols/present_proof/dif/pres_exch_handler.py +++ b/aries_cloudagent/protocols/present_proof/dif/pres_exch_handler.py @@ -118,7 +118,9 @@ async def _get_issue_suite( """Get signature suite for signing presentation.""" did_info = await self._did_info_for_did(issuer_id) verkey_id_strategy = self.profile.context.inject(DefaultVerificationKeyStrategy) - verification_method = verkey_id_strategy.get_verkey_id_for_did(issuer_id) + verification_method = verkey_id_strategy.get_verification_method_id_for_did( + issuer_id + ) if verification_method is None: raise DIFPresExchError( diff --git a/aries_cloudagent/wallet/default_verification_key_strategy.py b/aries_cloudagent/wallet/default_verification_key_strategy.py index 2ca51ab402..cd3e65c59a 100644 --- a/aries_cloudagent/wallet/default_verification_key_strategy.py +++ b/aries_cloudagent/wallet/default_verification_key_strategy.py @@ -9,7 +9,7 @@ class DefaultVerificationKeyStrategyBase(ABC): """Base class for defining which verification method is in use.""" @abstractmethod - def get_verkey_id_for_did(self, did) -> Optional[str]: + def get_verification_method_id_for_did(self, did) -> Optional[str]: """Given a DID, returns the verification key ID in use. Returns None if no strategy is specified for this DID. @@ -26,7 +26,7 @@ class DefaultVerificationKeyStrategy(DefaultVerificationKeyStrategyBase): Supports did:key: and did:sov only. """ - def get_verkey_id_for_did(self, did) -> Optional[str]: + def get_verification_method_id_for_did(self, did) -> Optional[str]: """Given a did:key or did:sov, returns the verification key ID in use. Returns None if no strategy is specified for this DID. diff --git a/aries_cloudagent/wallet/tests/test_default_verification_key_strategy.py b/aries_cloudagent/wallet/tests/test_default_verification_key_strategy.py index d68ca45653..c53207836e 100644 --- a/aries_cloudagent/wallet/tests/test_default_verification_key_strategy.py +++ b/aries_cloudagent/wallet/tests/test_default_verification_key_strategy.py @@ -13,15 +13,18 @@ class TestDefaultVerificationKeyStrategy(TestCase): def test_with_did_sov(self): strategy = DefaultVerificationKeyStrategy() - assert strategy.get_verkey_id_for_did(TEST_DID_SOV) == TEST_DID_SOV + "#key-1" + assert ( + strategy.get_verification_method_id_for_did(TEST_DID_SOV) + == TEST_DID_SOV + "#key-1" + ) def test_with_did_key(self): strategy = DefaultVerificationKeyStrategy() assert ( - strategy.get_verkey_id_for_did(TEST_DID_KEY) + strategy.get_verification_method_id_for_did(TEST_DID_KEY) == DIDKey.from_did(TEST_DID_KEY).key_id ) def test_unsupported_did_method(self): strategy = DefaultVerificationKeyStrategy() - assert strategy.get_verkey_id_for_did("did:test:test") is None + assert strategy.get_verification_method_id_for_did("did:test:test") is None From 6ec48054cc42e92ea2ded8d870e28f6a2878e441 Mon Sep 17 00:00:00 2001 From: Sacha Kozma Date: Mon, 22 May 2023 13:49:29 +0200 Subject: [PATCH 3/5] fix: set up context correctly in pres_exch tests Signed-off-by: Sacha Kozma --- .../present_proof/dif/tests/test_pres_exch_handler.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/aries_cloudagent/protocols/present_proof/dif/tests/test_pres_exch_handler.py b/aries_cloudagent/protocols/present_proof/dif/tests/test_pres_exch_handler.py index 6771dfc19c..51211e824a 100644 --- a/aries_cloudagent/protocols/present_proof/dif/tests/test_pres_exch_handler.py +++ b/aries_cloudagent/protocols/present_proof/dif/tests/test_pres_exch_handler.py @@ -15,6 +15,7 @@ from .....storage.vc_holder.vc_record import VCRecord from .....wallet.base import BaseWallet, DIDInfo from .....wallet.crypto import KeyType +from .....wallet.default_verification_key_strategy import DefaultVerificationKeyStrategy from .....wallet.did_method import SOV, KEY, DIDMethods from .....wallet.error import WalletNotFoundError from .....vc.ld_proofs import ( @@ -73,6 +74,9 @@ def profile(): context = profile.context context.injector.bind_instance(DIDResolver, DIDResolver([])) context.injector.bind_instance(DocumentLoader, custom_document_loader) + context.injector.bind_instance( + DefaultVerificationKeyStrategy, DefaultVerificationKeyStrategy() + ) context.settings["debug.auto_respond_presentation_request"] = True return profile From 73d313f28d8047fb3d55eb7160af1d11ad4d547a Mon Sep 17 00:00:00 2001 From: Sacha Kozma Date: Tue, 13 Jun 2023 09:29:17 +0200 Subject: [PATCH 4/5] chore: rename injection token and classes Signed-off-by: Sacha Kozma --- aries_cloudagent/config/default_context.py | 7 +++++-- .../issue_credential/v2_0/formats/ld_proof/handler.py | 4 ++-- .../v2_0/formats/ld_proof/tests/test_handler.py | 3 ++- .../protocols/present_proof/dif/pres_exch_handler.py | 6 ++++-- .../present_proof/dif/tests/test_pres_exch_handler.py | 7 +++++-- .../wallet/default_verification_key_strategy.py | 4 ++-- 6 files changed, 20 insertions(+), 11 deletions(-) diff --git a/aries_cloudagent/config/default_context.py b/aries_cloudagent/config/default_context.py index 2194c1e077..203aaac65b 100644 --- a/aries_cloudagent/config/default_context.py +++ b/aries_cloudagent/config/default_context.py @@ -17,7 +17,10 @@ from ..transport.wire_format import BaseWireFormat from ..utils.dependencies import is_indy_sdk_module_installed from ..utils.stats import Collector -from ..wallet.default_verification_key_strategy import DefaultVerificationKeyStrategy +from ..wallet.default_verification_key_strategy import ( + DefaultVerificationKeyStrategy, + BaseVerificationKeyStrategy, +) from ..wallet.did_method import DIDMethods from ..wallet.key_type import KeyTypes from .base_context import ContextBuilder @@ -55,7 +58,7 @@ async def build_context(self) -> InjectionContext: context.injector.bind_instance(DIDMethods, DIDMethods()) context.injector.bind_instance(KeyTypes, KeyTypes()) context.injector.bind_instance( - DefaultVerificationKeyStrategy, DefaultVerificationKeyStrategy() + BaseVerificationKeyStrategy, DefaultVerificationKeyStrategy() ) await self.bind_providers(context) diff --git a/aries_cloudagent/protocols/issue_credential/v2_0/formats/ld_proof/handler.py b/aries_cloudagent/protocols/issue_credential/v2_0/formats/ld_proof/handler.py index 95cf55e017..908ee0e804 100644 --- a/aries_cloudagent/protocols/issue_credential/v2_0/formats/ld_proof/handler.py +++ b/aries_cloudagent/protocols/issue_credential/v2_0/formats/ld_proof/handler.py @@ -35,7 +35,7 @@ from ......vc.ld_proofs.constants import SECURITY_CONTEXT_BBS_URL from ......wallet.base import BaseWallet, DIDInfo from ......wallet.default_verification_key_strategy import ( - DefaultVerificationKeyStrategy, + BaseVerificationKeyStrategy, ) from ......wallet.error import WalletNotFoundError from ......wallet.key_type import BLS12381G2, ED25519 @@ -272,7 +272,7 @@ async def _get_suite_for_detail( ) did_info = await self._did_info_for_did(issuer_id) - verkey_id_strategy = self.profile.context.inject(DefaultVerificationKeyStrategy) + verkey_id_strategy = self.profile.context.inject(BaseVerificationKeyStrategy) verification_method = ( verification_method or verkey_id_strategy.get_verification_method_id_for_did(issuer_id) diff --git a/aries_cloudagent/protocols/issue_credential/v2_0/formats/ld_proof/tests/test_handler.py b/aries_cloudagent/protocols/issue_credential/v2_0/formats/ld_proof/tests/test_handler.py index 5384d0d132..4c059e52cd 100644 --- a/aries_cloudagent/protocols/issue_credential/v2_0/formats/ld_proof/tests/test_handler.py +++ b/aries_cloudagent/protocols/issue_credential/v2_0/formats/ld_proof/tests/test_handler.py @@ -28,6 +28,7 @@ from .......vc.tests.document_loader import custom_document_loader from .......wallet.default_verification_key_strategy import ( DefaultVerificationKeyStrategy, + BaseVerificationKeyStrategy, ) from .......wallet.key_type import BLS12381G2, ED25519 from .......wallet.error import WalletNotFoundError @@ -129,7 +130,7 @@ async def setUp(self): # Set default verkey ID strategy self.context.injector.bind_instance( - DefaultVerificationKeyStrategy, DefaultVerificationKeyStrategy() + BaseVerificationKeyStrategy, DefaultVerificationKeyStrategy() ) self.handler = LDProofCredFormatHandler(self.profile) diff --git a/aries_cloudagent/protocols/present_proof/dif/pres_exch_handler.py b/aries_cloudagent/protocols/present_proof/dif/pres_exch_handler.py index 0d2ac468de..033b29c7b8 100644 --- a/aries_cloudagent/protocols/present_proof/dif/pres_exch_handler.py +++ b/aries_cloudagent/protocols/present_proof/dif/pres_exch_handler.py @@ -38,7 +38,9 @@ ) from ....vc.vc_ld.prove import sign_presentation, create_presentation, derive_credential from ....wallet.base import BaseWallet, DIDInfo -from ....wallet.default_verification_key_strategy import DefaultVerificationKeyStrategy +from ....wallet.default_verification_key_strategy import ( + BaseVerificationKeyStrategy, +) from ....wallet.error import WalletError, WalletNotFoundError from ....wallet.key_type import BLS12381G2, ED25519 @@ -117,7 +119,7 @@ async def _get_issue_suite( ): """Get signature suite for signing presentation.""" did_info = await self._did_info_for_did(issuer_id) - verkey_id_strategy = self.profile.context.inject(DefaultVerificationKeyStrategy) + verkey_id_strategy = self.profile.context.inject(BaseVerificationKeyStrategy) verification_method = verkey_id_strategy.get_verification_method_id_for_did( issuer_id ) diff --git a/aries_cloudagent/protocols/present_proof/dif/tests/test_pres_exch_handler.py b/aries_cloudagent/protocols/present_proof/dif/tests/test_pres_exch_handler.py index 51211e824a..e487d218cf 100644 --- a/aries_cloudagent/protocols/present_proof/dif/tests/test_pres_exch_handler.py +++ b/aries_cloudagent/protocols/present_proof/dif/tests/test_pres_exch_handler.py @@ -15,7 +15,10 @@ from .....storage.vc_holder.vc_record import VCRecord from .....wallet.base import BaseWallet, DIDInfo from .....wallet.crypto import KeyType -from .....wallet.default_verification_key_strategy import DefaultVerificationKeyStrategy +from .....wallet.default_verification_key_strategy import ( + DefaultVerificationKeyStrategy, + BaseVerificationKeyStrategy, +) from .....wallet.did_method import SOV, KEY, DIDMethods from .....wallet.error import WalletNotFoundError from .....vc.ld_proofs import ( @@ -75,7 +78,7 @@ def profile(): context.injector.bind_instance(DIDResolver, DIDResolver([])) context.injector.bind_instance(DocumentLoader, custom_document_loader) context.injector.bind_instance( - DefaultVerificationKeyStrategy, DefaultVerificationKeyStrategy() + BaseVerificationKeyStrategy, DefaultVerificationKeyStrategy() ) context.settings["debug.auto_respond_presentation_request"] = True return profile diff --git a/aries_cloudagent/wallet/default_verification_key_strategy.py b/aries_cloudagent/wallet/default_verification_key_strategy.py index cd3e65c59a..40ff763e9c 100644 --- a/aries_cloudagent/wallet/default_verification_key_strategy.py +++ b/aries_cloudagent/wallet/default_verification_key_strategy.py @@ -5,7 +5,7 @@ from aries_cloudagent.did.did_key import DIDKey -class DefaultVerificationKeyStrategyBase(ABC): +class BaseVerificationKeyStrategy(ABC): """Base class for defining which verification method is in use.""" @abstractmethod @@ -20,7 +20,7 @@ def get_verification_method_id_for_did(self, did) -> Optional[str]: pass -class DefaultVerificationKeyStrategy(DefaultVerificationKeyStrategyBase): +class DefaultVerificationKeyStrategy(BaseVerificationKeyStrategy): """A basic implementation for verkey strategy. Supports did:key: and did:sov only. From 19ff65363a3431885b7f7d5ce604d1988f58e770 Mon Sep 17 00:00:00 2001 From: Sacha Kozma Date: Tue, 13 Jun 2023 13:06:03 +0200 Subject: [PATCH 5/5] feat: add proof_purpose and allowed_method_types Signed-off-by: Sacha Kozma --- .../v2_0/formats/ld_proof/handler.py | 4 +++- .../present_proof/dif/pres_exch_handler.py | 2 +- .../default_verification_key_strategy.py | 24 +++++++++++++++---- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/aries_cloudagent/protocols/issue_credential/v2_0/formats/ld_proof/handler.py b/aries_cloudagent/protocols/issue_credential/v2_0/formats/ld_proof/handler.py index 908ee0e804..c9e6eb8ee1 100644 --- a/aries_cloudagent/protocols/issue_credential/v2_0/formats/ld_proof/handler.py +++ b/aries_cloudagent/protocols/issue_credential/v2_0/formats/ld_proof/handler.py @@ -275,7 +275,9 @@ async def _get_suite_for_detail( verkey_id_strategy = self.profile.context.inject(BaseVerificationKeyStrategy) verification_method = ( verification_method - or verkey_id_strategy.get_verification_method_id_for_did(issuer_id) + or verkey_id_strategy.get_verification_method_id_for_did( + issuer_id, proof_purpose="assertionMethod" + ) ) if verification_method is None: diff --git a/aries_cloudagent/protocols/present_proof/dif/pres_exch_handler.py b/aries_cloudagent/protocols/present_proof/dif/pres_exch_handler.py index 033b29c7b8..5fe28602a1 100644 --- a/aries_cloudagent/protocols/present_proof/dif/pres_exch_handler.py +++ b/aries_cloudagent/protocols/present_proof/dif/pres_exch_handler.py @@ -121,7 +121,7 @@ async def _get_issue_suite( did_info = await self._did_info_for_did(issuer_id) verkey_id_strategy = self.profile.context.inject(BaseVerificationKeyStrategy) verification_method = verkey_id_strategy.get_verification_method_id_for_did( - issuer_id + issuer_id, proof_purpose="assertionMethod" ) if verification_method is None: diff --git a/aries_cloudagent/wallet/default_verification_key_strategy.py b/aries_cloudagent/wallet/default_verification_key_strategy.py index 40ff763e9c..4a5ffd89b1 100644 --- a/aries_cloudagent/wallet/default_verification_key_strategy.py +++ b/aries_cloudagent/wallet/default_verification_key_strategy.py @@ -1,6 +1,8 @@ """Utilities for specifying which verification method is in use for a given DID.""" from abc import ABC, abstractmethod -from typing import Optional +from typing import Optional, List + +from aries_cloudagent.wallet.key_type import KeyType from aries_cloudagent.did.did_key import DIDKey @@ -9,12 +11,19 @@ class BaseVerificationKeyStrategy(ABC): """Base class for defining which verification method is in use.""" @abstractmethod - def get_verification_method_id_for_did(self, did) -> Optional[str]: + def get_verification_method_id_for_did( + self, + did: str, + allowed_verification_method_types: Optional[List[KeyType]] = None, + proof_purpose: Optional[str] = None, + ) -> Optional[str]: """Given a DID, returns the verification key ID in use. Returns None if no strategy is specified for this DID. - :params str did: the did + :params did: the did + :params allowed_verification_method_types: list of accepted key types + :params proof_purpose: the verkey relationship (assertionMethod, keyAgreement, ..) :returns Optional[str]: the current verkey ID """ pass @@ -26,12 +35,19 @@ class DefaultVerificationKeyStrategy(BaseVerificationKeyStrategy): Supports did:key: and did:sov only. """ - def get_verification_method_id_for_did(self, did) -> Optional[str]: + def get_verification_method_id_for_did( + self, + did: str, + allowed_verification_method_types: Optional[List[KeyType]] = None, + proof_purpose: Optional[str] = None, + ) -> Optional[str]: """Given a did:key or did:sov, returns the verification key ID in use. Returns None if no strategy is specified for this DID. :params str did: the did + :params allowed_verification_method_types: list of accepted key types + :params proof_purpose: the verkey relationship (assertionMethod, keyAgreement, ..) :returns Optional[str]: the current verkey ID """ if did.startswith("did:key:"):