From ed9e08a763d98f512906c67d1174b9c11ee1a830 Mon Sep 17 00:00:00 2001 From: aritroCoder Date: Wed, 31 Jul 2024 20:02:09 +0530 Subject: [PATCH 1/9] didweb support added Signed-off-by: aritroCoder --- .../wallet/default_verification_key_strategy.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/acapy_agent/wallet/default_verification_key_strategy.py b/acapy_agent/wallet/default_verification_key_strategy.py index 9d9f47b040..9431e791c8 100644 --- a/acapy_agent/wallet/default_verification_key_strategy.py +++ b/acapy_agent/wallet/default_verification_key_strategy.py @@ -7,6 +7,8 @@ from acapy_agent.did.did_key import DIDKey from acapy_agent.wallet.key_type import KeyType +from acapy_agent.resolver.did_resolver import DIDResolver + class BaseVerificationKeyStrategy(ABC): """Base class for defining which verification method is in use.""" @@ -60,5 +62,11 @@ async def get_verification_method_id_for_did( elif did.startswith("did:sov:"): # key-1 is what uniresolver uses for key id return did + "#key-1" - + elif did.startswith("did:web:"): + did_resolver = profile.inject(DIDResolver) + did_document = await did_resolver.resolve(profile=profile, did=did) + verification_method_list = did_document.get("verificationMethod", {}) + # taking the first verification method from did document + verification_method_id = verification_method_list[0].get("id") + return verification_method_id return None From f31ed34e4939cce0ad6cdb3945d37ce61048a8f5 Mon Sep 17 00:00:00 2001 From: aritroCoder Date: Tue, 6 Aug 2024 00:26:25 +0530 Subject: [PATCH 2/9] fix: added proof_type for searching DID verification key Signed-off-by: aritroCoder --- .../present_proof/dif/pres_exch_handler.py | 9 ++++++-- acapy_agent/vc/vc_ld/manager.py | 2 +- .../default_verification_key_strategy.py | 22 +++++++++++++++---- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/acapy_agent/protocols/present_proof/dif/pres_exch_handler.py b/acapy_agent/protocols/present_proof/dif/pres_exch_handler.py index b699a9bb71..5f15fb1004 100644 --- a/acapy_agent/protocols/present_proof/dif/pres_exch_handler.py +++ b/acapy_agent/protocols/present_proof/dif/pres_exch_handler.py @@ -119,8 +119,13 @@ 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(BaseVerificationKeyStrategy) - verification_method = await verkey_id_strategy.get_verification_method_id_for_did( - issuer_id, self.profile, proof_purpose="assertionMethod" + verification_method = ( + await verkey_id_strategy.get_verification_method_id_for_did( + issuer_id, + self.profile, + proof_type=self.proof_type, + proof_purpose="assertionMethod", + ) ) if verification_method is None: diff --git a/acapy_agent/vc/vc_ld/manager.py b/acapy_agent/vc/vc_ld/manager.py index 52808707f3..7151bdda34 100644 --- a/acapy_agent/vc/vc_ld/manager.py +++ b/acapy_agent/vc/vc_ld/manager.py @@ -344,7 +344,7 @@ async def _get_suite_for_document( verification_method = ( options.verification_method or await verkey_id_strategy.get_verification_method_id_for_did( - issuer_id, self.profile, proof_purpose="assertionMethod" + issuer_id, self.profile, proof_type, proof_purpose="assertionMethod" ) ) diff --git a/acapy_agent/wallet/default_verification_key_strategy.py b/acapy_agent/wallet/default_verification_key_strategy.py index 9431e791c8..8f8bdff2ba 100644 --- a/acapy_agent/wallet/default_verification_key_strategy.py +++ b/acapy_agent/wallet/default_verification_key_strategy.py @@ -18,6 +18,7 @@ async def get_verification_method_id_for_did( self, did: str, profile: Optional[Profile], + proof_type: Optional[str] = None, allowed_verification_method_types: Optional[List[KeyType]] = None, proof_purpose: Optional[str] = None, ) -> Optional[str]: @@ -39,11 +40,18 @@ class DefaultVerificationKeyStrategy(BaseVerificationKeyStrategy): Supports did:key: and did:sov only. """ + def __init__(self): + """Initialize the key types mapping.""" + self.key_types_mapping = { + "Ed25519Signature2018": "Ed25519VerificationKey2018", + "Ed25519Signature2020": "Ed25519VerificationKey2020", + } async def get_verification_method_id_for_did( self, did: str, profile: Optional[Profile], + proof_type: Optional[str] = None, allowed_verification_method_types: Optional[List[KeyType]] = None, proof_purpose: Optional[str] = None, ) -> Optional[str]: @@ -65,8 +73,14 @@ async def get_verification_method_id_for_did( elif did.startswith("did:web:"): did_resolver = profile.inject(DIDResolver) did_document = await did_resolver.resolve(profile=profile, did=did) - verification_method_list = did_document.get("verificationMethod", {}) - # taking the first verification method from did document - verification_method_id = verification_method_list[0].get("id") - return verification_method_id + if proof_type: + verification_method_type = self.key_types_mapping[proof_type] + verification_method_list = did_document.get("verificationMethod", None) + for method in verification_method_list: + if method.get("type") == verification_method_type: + return method.get("id") + else: + # taking the first verification method from did document + verification_method_id = verification_method_list[0].get("id") + return verification_method_id return None From c787ec4a326e995dd2d046286f73c62c7b73a611 Mon Sep 17 00:00:00 2001 From: Aritra Bhaduri <92646038+aritroCoder@users.noreply.github.com> Date: Tue, 6 Aug 2024 18:18:07 +0530 Subject: [PATCH 3/9] Update aries_cloudagent/wallet/default_verification_key_strategy.py added multikey support Co-authored-by: Patrick St-Louis <43082425+PatStLouis@users.noreply.github.com> Signed-off-by: Aritra Bhaduri <92646038+aritroCoder@users.noreply.github.com> --- acapy_agent/wallet/default_verification_key_strategy.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/acapy_agent/wallet/default_verification_key_strategy.py b/acapy_agent/wallet/default_verification_key_strategy.py index 8f8bdff2ba..08d8bdb18e 100644 --- a/acapy_agent/wallet/default_verification_key_strategy.py +++ b/acapy_agent/wallet/default_verification_key_strategy.py @@ -43,8 +43,8 @@ class DefaultVerificationKeyStrategy(BaseVerificationKeyStrategy): def __init__(self): """Initialize the key types mapping.""" self.key_types_mapping = { - "Ed25519Signature2018": "Ed25519VerificationKey2018", - "Ed25519Signature2020": "Ed25519VerificationKey2020", + "Ed25519Signature2018": ["Ed25519VerificationKey2018"], + "Ed25519Signature2020": ["Ed25519VerificationKey2020", "Multikey"], } async def get_verification_method_id_for_did( From 8b34457677091b3a51e60a7d29d7be2b2a0f5867 Mon Sep 17 00:00:00 2001 From: Aritra Bhaduri <92646038+aritroCoder@users.noreply.github.com> Date: Tue, 6 Aug 2024 18:18:19 +0530 Subject: [PATCH 4/9] Update aries_cloudagent/wallet/default_verification_key_strategy.py added multikey support Co-authored-by: Patrick St-Louis <43082425+PatStLouis@users.noreply.github.com> Signed-off-by: Aritra Bhaduri <92646038+aritroCoder@users.noreply.github.com> --- acapy_agent/wallet/default_verification_key_strategy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/acapy_agent/wallet/default_verification_key_strategy.py b/acapy_agent/wallet/default_verification_key_strategy.py index 08d8bdb18e..70daaf5016 100644 --- a/acapy_agent/wallet/default_verification_key_strategy.py +++ b/acapy_agent/wallet/default_verification_key_strategy.py @@ -74,7 +74,7 @@ async def get_verification_method_id_for_did( did_resolver = profile.inject(DIDResolver) did_document = await did_resolver.resolve(profile=profile, did=did) if proof_type: - verification_method_type = self.key_types_mapping[proof_type] + verification_method_types = self.key_types_mapping[proof_type] verification_method_list = did_document.get("verificationMethod", None) for method in verification_method_list: if method.get("type") == verification_method_type: From 92430f6946d59d4c63965ccf27b871b92a3dc388 Mon Sep 17 00:00:00 2001 From: Aritra Bhaduri <92646038+aritroCoder@users.noreply.github.com> Date: Tue, 6 Aug 2024 18:18:27 +0530 Subject: [PATCH 5/9] Update aries_cloudagent/wallet/default_verification_key_strategy.py added multikey support Co-authored-by: Patrick St-Louis <43082425+PatStLouis@users.noreply.github.com> Signed-off-by: Aritra Bhaduri <92646038+aritroCoder@users.noreply.github.com> --- acapy_agent/wallet/default_verification_key_strategy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/acapy_agent/wallet/default_verification_key_strategy.py b/acapy_agent/wallet/default_verification_key_strategy.py index 70daaf5016..93bcf50122 100644 --- a/acapy_agent/wallet/default_verification_key_strategy.py +++ b/acapy_agent/wallet/default_verification_key_strategy.py @@ -77,7 +77,7 @@ async def get_verification_method_id_for_did( verification_method_types = self.key_types_mapping[proof_type] verification_method_list = did_document.get("verificationMethod", None) for method in verification_method_list: - if method.get("type") == verification_method_type: + if method.get("type") in verification_method_types: return method.get("id") else: # taking the first verification method from did document From 9144f76b41ea2f26ba73b73fa2e1cd45c3ba7c1f Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Wed, 9 Oct 2024 15:35:49 -0400 Subject: [PATCH 6/9] feat: more robust vm selection for all did types Signed-off-by: Daniel Bluhm --- .../present_proof/dif/pres_exch_handler.py | 19 ++--- acapy_agent/vc/vc_ld/manager.py | 10 ++- .../default_verification_key_strategy.py | 72 +++++++++++++------ acapy_agent/wallet/jwt.py | 2 - 4 files changed, 61 insertions(+), 42 deletions(-) diff --git a/acapy_agent/protocols/present_proof/dif/pres_exch_handler.py b/acapy_agent/protocols/present_proof/dif/pres_exch_handler.py index 5f15fb1004..7072b635bb 100644 --- a/acapy_agent/protocols/present_proof/dif/pres_exch_handler.py +++ b/acapy_agent/protocols/present_proof/dif/pres_exch_handler.py @@ -118,21 +118,14 @@ 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(BaseVerificationKeyStrategy) - verification_method = ( - await verkey_id_strategy.get_verification_method_id_for_did( - issuer_id, - self.profile, - proof_type=self.proof_type, - proof_purpose="assertionMethod", - ) + vm_id_strategy = self.profile.context.inject(BaseVerificationKeyStrategy) + verification_method = await vm_id_strategy.get_verification_method_id_for_did( + issuer_id, + self.profile, + proof_type=self.proof_type, + proof_purpose="assertionMethod", ) - 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] diff --git a/acapy_agent/vc/vc_ld/manager.py b/acapy_agent/vc/vc_ld/manager.py index 7151bdda34..d6d4b6809c 100644 --- a/acapy_agent/vc/vc_ld/manager.py +++ b/acapy_agent/vc/vc_ld/manager.py @@ -344,15 +344,13 @@ async def _get_suite_for_document( verification_method = ( options.verification_method or await verkey_id_strategy.get_verification_method_id_for_did( - issuer_id, self.profile, proof_type, proof_purpose="assertionMethod" + issuer_id, + self.profile, + proof_type=proof_type, + proof_purpose="assertionMethod", ) ) - if verification_method is None: - raise VcLdpManagerError( - f"Unable to get retrieve verification method for did {issuer_id}" - ) - suite = await self._get_suite( proof_type=proof_type, verification_method=verification_method, diff --git a/acapy_agent/wallet/default_verification_key_strategy.py b/acapy_agent/wallet/default_verification_key_strategy.py index 93bcf50122..937a4003d9 100644 --- a/acapy_agent/wallet/default_verification_key_strategy.py +++ b/acapy_agent/wallet/default_verification_key_strategy.py @@ -1,14 +1,20 @@ """Utilities for specifying which verification method is in use for a given DID.""" from abc import ABC, abstractmethod -from typing import List, Optional +import logging +from typing import Optional +from acapy_agent.core.error import BaseError from acapy_agent.core.profile import Profile from acapy_agent.did.did_key import DIDKey -from acapy_agent.wallet.key_type import KeyType - from acapy_agent.resolver.did_resolver import DIDResolver +LOGGER = logging.getLogger(__name__) + + +class VerificationKeyStrategyError(BaseError): + """Raised on issues with verfication method derivation.""" + class BaseVerificationKeyStrategy(ABC): """Base class for defining which verification method is in use.""" @@ -17,9 +23,9 @@ class BaseVerificationKeyStrategy(ABC): async def get_verification_method_id_for_did( self, did: str, - profile: Optional[Profile], + profile: Profile, + *, proof_type: Optional[str] = None, - 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. @@ -40,6 +46,7 @@ class DefaultVerificationKeyStrategy(BaseVerificationKeyStrategy): Supports did:key: and did:sov only. """ + def __init__(self): """Initialize the key types mapping.""" self.key_types_mapping = { @@ -50,9 +57,9 @@ def __init__(self): async def get_verification_method_id_for_did( self, did: str, - profile: Optional[Profile], + profile: Profile, + *, proof_type: Optional[str] = None, - 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. @@ -65,22 +72,45 @@ async def get_verification_method_id_for_did( :params proof_purpose: the verkey relationship (assertionMethod, keyAgreement, ..) :returns Optional[str]: the current verkey ID """ + proof_type = proof_type or "Ed25519Signature2018" + proof_purpose = proof_purpose or "assertionMethod" + + if proof_purpose not in ( + "authentication", + "assertionMethod", + "capabilityInvocation", + "capabilityDelegation", + ): + raise ValueError("Invalid proof purpose") + 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" - elif did.startswith("did:web:"): - did_resolver = profile.inject(DIDResolver) - did_document = await did_resolver.resolve(profile=profile, did=did) - if proof_type: - verification_method_types = self.key_types_mapping[proof_type] - verification_method_list = did_document.get("verificationMethod", None) - for method in verification_method_list: - if method.get("type") in verification_method_types: - return method.get("id") - else: - # taking the first verification method from did document - verification_method_id = verification_method_list[0].get("id") - return verification_method_id - return None + + resolver = profile.inject(DIDResolver) + did_document = await resolver.resolve(profile=profile, did=did) + method_types = self.key_types_mapping[proof_type] + + methods = did_document.get(proof_purpose, []) + methods = [vm for vm in methods if vm.get("type") in method_types] + if not methods: + raise VerificationKeyStrategyError( + f"No matching verification method found for did {did} with proof " + f"type {proof_type} and purpose {proof_purpose}" + ) + + if len(methods) > 1: + LOGGER.info( + ( + "More than 1 verification method matched for did %s with proof " + "type %s and purpose %s; returning the first: %s" + ), + did, + proof_type, + proof_purpose, + methods[0].id, + ) + + return methods[0].id diff --git a/acapy_agent/wallet/jwt.py b/acapy_agent/wallet/jwt.py index bf16bfb790..33edd22593 100644 --- a/acapy_agent/wallet/jwt.py +++ b/acapy_agent/wallet/jwt.py @@ -61,8 +61,6 @@ async def jwt_sign( verification_method = await verkey_strat.get_verification_method_id_for_did( did, profile ) - if not verification_method: - raise ValueError("Could not determine verification method from DID") else: # We look up keys by did for now did = DIDUrl.parse(verification_method).did From e9beda3cd2958489ad4be4f8890dea3921df8ac8 Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Wed, 9 Oct 2024 15:52:30 -0400 Subject: [PATCH 7/9] fix: dereference refs in vm relationship Signed-off-by: Daniel Bluhm --- .../default_verification_key_strategy.py | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/acapy_agent/wallet/default_verification_key_strategy.py b/acapy_agent/wallet/default_verification_key_strategy.py index 937a4003d9..313b9d7520 100644 --- a/acapy_agent/wallet/default_verification_key_strategy.py +++ b/acapy_agent/wallet/default_verification_key_strategy.py @@ -4,10 +4,12 @@ import logging from typing import Optional -from acapy_agent.core.error import BaseError -from acapy_agent.core.profile import Profile -from acapy_agent.did.did_key import DIDKey -from acapy_agent.resolver.did_resolver import DIDResolver +from pydid import DIDDocument + +from ..core.error import BaseError +from ..core.profile import Profile +from ..did.did_key import DIDKey +from ..resolver.did_resolver import DIDResolver LOGGER = logging.getLogger(__name__) @@ -90,11 +92,21 @@ async def get_verification_method_id_for_did( return did + "#key-1" resolver = profile.inject(DIDResolver) - did_document = await resolver.resolve(profile=profile, did=did) - method_types = self.key_types_mapping[proof_type] + doc_raw = await resolver.resolve(profile=profile, did=did) + doc = DIDDocument.deserialize(doc_raw) + + methods_or_refs = getattr(doc, proof_purpose, []) + # Dereference any refs in the verification relationship + methods = [ + await resolver.dereference_verification_method(profile, method, document=doc) + if isinstance(method, str) + else method + for method in methods_or_refs + ] - methods = did_document.get(proof_purpose, []) - methods = [vm for vm in methods if vm.get("type") in method_types] + method_types = self.key_types_mapping[proof_type] + # Filter methods by type expected for proof_type + methods = [vm for vm in methods if vm.type in method_types] if not methods: raise VerificationKeyStrategyError( f"No matching verification method found for did {did} with proof " From 8178eb2edd356a5f7291bed17961d1b995db9c32 Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Wed, 9 Oct 2024 16:16:52 -0400 Subject: [PATCH 8/9] fix: on sign vp, use auth proof purpose Signed-off-by: Daniel Bluhm --- .../present_proof/dif/pres_exch_handler.py | 12 ++++++--- .../default_verification_key_strategy.py | 27 ++++++++++++------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/acapy_agent/protocols/present_proof/dif/pres_exch_handler.py b/acapy_agent/protocols/present_proof/dif/pres_exch_handler.py index 7072b635bb..3eff6d28f9 100644 --- a/acapy_agent/protocols/present_proof/dif/pres_exch_handler.py +++ b/acapy_agent/protocols/present_proof/dif/pres_exch_handler.py @@ -40,7 +40,10 @@ from ....vc.vc_di.prove import create_signed_anoncreds_presentation from ....vc.vc_ld.prove import create_presentation, derive_credential, sign_presentation from ....wallet.base import BaseWallet, DIDInfo -from ....wallet.default_verification_key_strategy import BaseVerificationKeyStrategy +from ....wallet.default_verification_key_strategy import ( + BaseVerificationKeyStrategy, + ProofPurposeStr, +) from ....wallet.error import WalletError, WalletNotFoundError from ....wallet.key_type import BLS12381G2, ED25519 from .pres_exch import ( @@ -115,15 +118,17 @@ async def _get_issue_suite( self, *, issuer_id: str, + proof_purpose: Optional[ProofPurposeStr] = None, ): """Get signature suite for signing presentation.""" + proof_purpose = proof_purpose or "assertionMethod" did_info = await self._did_info_for_did(issuer_id) vm_id_strategy = self.profile.context.inject(BaseVerificationKeyStrategy) verification_method = await vm_id_strategy.get_verification_method_id_for_did( issuer_id, self.profile, proof_type=self.proof_type, - proof_purpose="assertionMethod", + proof_purpose=proof_purpose, ) # Get signature class based on proof type @@ -1300,8 +1305,9 @@ async def create_vp( ) else: vp = self.__add_dif_fields_to_vp(vp, submission_property) + assert issuer_id issue_suite = await self._get_issue_suite( - issuer_id=issuer_id, + issuer_id=issuer_id, proof_purpose="authentication" ) signed_vp = await sign_presentation( presentation=vp, diff --git a/acapy_agent/wallet/default_verification_key_strategy.py b/acapy_agent/wallet/default_verification_key_strategy.py index 313b9d7520..04f3bd5886 100644 --- a/acapy_agent/wallet/default_verification_key_strategy.py +++ b/acapy_agent/wallet/default_verification_key_strategy.py @@ -2,7 +2,7 @@ from abc import ABC, abstractmethod import logging -from typing import Optional +from typing import Literal, Optional from pydid import DIDDocument @@ -14,6 +14,20 @@ LOGGER = logging.getLogger(__name__) +ProofPurposeStr = Literal[ + "assertionMethod", + "authentication", + "capabilityDelegation", + "capabilityInvocation", +] +PROOF_PURPOSES = ( + "authentication", + "assertionMethod", + "capabilityInvocation", + "capabilityDelegation", +) + + class VerificationKeyStrategyError(BaseError): """Raised on issues with verfication method derivation.""" @@ -28,7 +42,7 @@ async def get_verification_method_id_for_did( profile: Profile, *, proof_type: Optional[str] = None, - proof_purpose: Optional[str] = None, + proof_purpose: Optional[ProofPurposeStr] = None, ) -> Optional[str]: """Given a DID, returns the verification key ID in use. @@ -62,7 +76,7 @@ async def get_verification_method_id_for_did( profile: Profile, *, proof_type: Optional[str] = None, - proof_purpose: Optional[str] = None, + proof_purpose: Optional[ProofPurposeStr] = None, ) -> Optional[str]: """Given a did:key or did:sov, returns the verification key ID in use. @@ -77,12 +91,7 @@ async def get_verification_method_id_for_did( proof_type = proof_type or "Ed25519Signature2018" proof_purpose = proof_purpose or "assertionMethod" - if proof_purpose not in ( - "authentication", - "assertionMethod", - "capabilityInvocation", - "capabilityDelegation", - ): + if proof_purpose not in PROOF_PURPOSES: raise ValueError("Invalid proof purpose") if did.startswith("did:key:"): From f3e27067eb0071bc59246e5270a50f5da6fdd291 Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Mon, 11 Nov 2024 12:48:55 -0600 Subject: [PATCH 9/9] fix: negative test case for default verkey strat Signed-off-by: Daniel Bluhm --- .../wallet/default_verification_key_strategy.py | 13 +++++++++---- .../tests/test_default_verification_key_strategy.py | 9 ++++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/acapy_agent/wallet/default_verification_key_strategy.py b/acapy_agent/wallet/default_verification_key_strategy.py index 04f3bd5886..982ebc12aa 100644 --- a/acapy_agent/wallet/default_verification_key_strategy.py +++ b/acapy_agent/wallet/default_verification_key_strategy.py @@ -43,7 +43,7 @@ async def get_verification_method_id_for_did( *, proof_type: Optional[str] = None, proof_purpose: Optional[ProofPurposeStr] = None, - ) -> Optional[str]: + ) -> str: """Given a DID, returns the verification key ID in use. Returns None if no strategy is specified for this DID. @@ -54,7 +54,7 @@ async def get_verification_method_id_for_did( :params proof_purpose: the verkey relationship (assertionMethod, keyAgreement, ..) :returns Optional[str]: the current verkey ID """ - pass + ... class DefaultVerificationKeyStrategy(BaseVerificationKeyStrategy): @@ -77,7 +77,7 @@ async def get_verification_method_id_for_did( *, proof_type: Optional[str] = None, proof_purpose: Optional[ProofPurposeStr] = None, - ) -> Optional[str]: + ) -> 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. @@ -113,7 +113,12 @@ async def get_verification_method_id_for_did( for method in methods_or_refs ] - method_types = self.key_types_mapping[proof_type] + method_types = self.key_types_mapping.get(proof_type) + if not method_types: + raise VerificationKeyStrategyError( + f"proof type {proof_type} is not supported" + ) + # Filter methods by type expected for proof_type methods = [vm for vm in methods if vm.type in method_types] if not methods: diff --git a/acapy_agent/wallet/tests/test_default_verification_key_strategy.py b/acapy_agent/wallet/tests/test_default_verification_key_strategy.py index 137e3fcb0b..0a644d7757 100644 --- a/acapy_agent/wallet/tests/test_default_verification_key_strategy.py +++ b/acapy_agent/wallet/tests/test_default_verification_key_strategy.py @@ -1,4 +1,7 @@ from unittest import IsolatedAsyncioTestCase +import pytest + +from acapy_agent.resolver.did_resolver import DIDResolver from ...did.did_key import DIDKey from ...utils.testing import create_test_profile @@ -13,6 +16,8 @@ class TestDefaultVerificationKeyStrategy(IsolatedAsyncioTestCase): async def asyncSetUp(self) -> None: self.profile = await create_test_profile() + resolver = DIDResolver() + self.profile.context.injector.bind_instance(DIDResolver, resolver) async def test_with_did_sov(self): strategy = DefaultVerificationKeyStrategy() @@ -30,9 +35,7 @@ async def test_with_did_key(self): async def test_unsupported_did_method(self): strategy = DefaultVerificationKeyStrategy() - assert ( + with pytest.raises(Exception): await strategy.get_verification_method_id_for_did( "did:test:test", self.profile ) - is None - )