From ad0e60d663a92397e78ac6d83afede093c024567 Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Wed, 10 Jan 2024 08:17:08 -0500 Subject: [PATCH] fix: minor type hint corrections for VcLdpManager Signed-off-by: Daniel Bluhm --- .../suites/bbs_bls_signature_2020.py | 6 +-- .../vc/ld_proofs/suites/linked_data_proof.py | 8 +-- .../ld_proofs/suites/linked_data_signature.py | 6 +-- aries_cloudagent/vc/vc_ld/manager.py | 51 +++++++++++-------- .../vc/vc_ld/tests/test_manager.py | 2 +- 5 files changed, 40 insertions(+), 33 deletions(-) diff --git a/aries_cloudagent/vc/ld_proofs/suites/bbs_bls_signature_2020.py b/aries_cloudagent/vc/ld_proofs/suites/bbs_bls_signature_2020.py index 7b2c82401f..915497c67e 100644 --- a/aries_cloudagent/vc/ld_proofs/suites/bbs_bls_signature_2020.py +++ b/aries_cloudagent/vc/ld_proofs/suites/bbs_bls_signature_2020.py @@ -2,7 +2,7 @@ from datetime import datetime, timezone from pytz import utc -from typing import List, Union +from typing import List, Optional, Union from ....wallet.util import b64_to_bytes, bytes_to_b64 @@ -24,8 +24,8 @@ def __init__( self, *, key_pair: KeyPair, - proof: dict = None, - verification_method: str = None, + proof: Optional[dict] = None, + verification_method: Optional[str] = None, date: Union[datetime, None] = None, ): """Create new BbsBlsSignature2020 instance. diff --git a/aries_cloudagent/vc/ld_proofs/suites/linked_data_proof.py b/aries_cloudagent/vc/ld_proofs/suites/linked_data_proof.py index d8df7cc535..cb54192903 100644 --- a/aries_cloudagent/vc/ld_proofs/suites/linked_data_proof.py +++ b/aries_cloudagent/vc/ld_proofs/suites/linked_data_proof.py @@ -2,7 +2,7 @@ from abc import ABC -from typing import ClassVar, List, Union +from typing import ClassVar, List, Optional, Union from pyld import jsonld from typing_extensions import TypedDict @@ -30,8 +30,8 @@ class LinkedDataProof(ABC): def __init__( self, *, - proof: dict = None, - supported_derive_proof_types: Union[List[str], None] = None, + proof: Optional[dict] = None, + supported_derive_proof_types: Optional[List[str]] = None, ): """Initialize new LinkedDataProof instance.""" self.proof = proof @@ -90,7 +90,7 @@ async def derive_proof( document: dict, reveal_document: dict, document_loader: DocumentLoaderMethod, - nonce: bytes = None, + nonce: Optional[bytes] = None, ) -> DeriveProofResult: """Derive proof for document, returning derived document and proof. diff --git a/aries_cloudagent/vc/ld_proofs/suites/linked_data_signature.py b/aries_cloudagent/vc/ld_proofs/suites/linked_data_signature.py index eabc8e86e2..36721f562e 100644 --- a/aries_cloudagent/vc/ld_proofs/suites/linked_data_signature.py +++ b/aries_cloudagent/vc/ld_proofs/suites/linked_data_signature.py @@ -4,7 +4,7 @@ from datetime import datetime, timezone from hashlib import sha256 from pytz import utc -from typing import Union +from typing import Optional, Union from ..constants import SECURITY_CONTEXT_URL from ..document_loader import DocumentLoaderMethod @@ -21,8 +21,8 @@ class LinkedDataSignature(LinkedDataProof, metaclass=ABCMeta): def __init__( self, *, - proof: dict = None, - verification_method: str = None, + proof: Optional[dict] = None, + verification_method: Optional[str] = None, date: Union[datetime, None] = None, ): """Create new LinkedDataSignature instance. diff --git a/aries_cloudagent/vc/vc_ld/manager.py b/aries_cloudagent/vc/vc_ld/manager.py index 89f11e9ebc..93375e7f83 100644 --- a/aries_cloudagent/vc/vc_ld/manager.py +++ b/aries_cloudagent/vc/vc_ld/manager.py @@ -1,7 +1,7 @@ """Manager for performing Linked Data Proof signatures over JSON-LD formatted W3C VCs.""" -from typing import Dict, Optional, Type +from typing import Dict, List, Optional, Type, Union, cast from ...core.profile import Profile from ...wallet.base import BaseWallet @@ -32,31 +32,37 @@ from .models.options import LDProofVCOptions from .verify import verify_credential, verify_presentation - +SignatureTypes = Union[ + Type[Ed25519Signature2018], + Type[Ed25519Signature2020], + Type[BbsBlsSignature2020], +] +ProofTypes = Union[ + Type[Ed25519Signature2018], + Type[Ed25519Signature2020], + Type[BbsBlsSignature2020], + Type[BbsBlsSignatureProof2020], +] SUPPORTED_ISSUANCE_PROOF_PURPOSES = { CredentialIssuancePurpose.term, AuthenticationProofPurpose.term, } -SUPPORTED_ISSUANCE_SUITES = {Ed25519Signature2018, Ed25519Signature2020} -SIGNATURE_SUITE_KEY_TYPE_MAPPING: Dict[Type[LinkedDataProof], KeyType] = { +SIGNATURE_SUITE_KEY_TYPE_MAPPING: Dict[SignatureTypes, KeyType] = { Ed25519Signature2018: ED25519, Ed25519Signature2020: ED25519, } +PROOF_KEY_TYPE_MAPPING = cast( + Dict[ProofTypes, KeyType], SIGNATURE_SUITE_KEY_TYPE_MAPPING +) # We only want to add bbs suites to supported if the module is installed if BbsBlsSignature2020.BBS_SUPPORTED: - SUPPORTED_ISSUANCE_SUITES.add(BbsBlsSignature2020) - SUPPORTED_ISSUANCE_SUITES.add(BbsBlsSignatureProof2020) - SIGNATURE_SUITE_KEY_TYPE_MAPPING.update( - { - BbsBlsSignature2020: BLS12381G2, - BbsBlsSignatureProof2020: BLS12381G2, - } - ) + SIGNATURE_SUITE_KEY_TYPE_MAPPING[BbsBlsSignature2020] = BLS12381G2 + PROOF_KEY_TYPE_MAPPING[BbsBlsSignatureProof2020] = BLS12381G2 -PROOF_TYPE_SIGNATURE_SUITE_MAPPING = { +PROOF_TYPE_SIGNATURE_SUITE_MAPPING: Dict[str, SignatureTypes] = { suite.signature_type: suite for suite in SIGNATURE_SUITE_KEY_TYPE_MAPPING } @@ -311,16 +317,17 @@ async def _get_suite_for_credential( return suite - async def _get_all_suites(self): + async def _get_all_proof_suites(self) -> List[LinkedDataProof]: """Get all supported suites for verifying presentation.""" - suites = [] - for suite, key_type in SIGNATURE_SUITE_KEY_TYPE_MAPPING.items(): - suites.append( - suite( + return [ + cast( + LinkedDataProof, + SuiteClass( key_pair=WalletKeyPair(profile=self.profile, key_type=key_type), - ) + ), ) - return suites + for SuiteClass, key_type in PROOF_KEY_TYPE_MAPPING.items() + ] async def issue( self, credential: VerifiableCredential, options: LDProofVCOptions @@ -354,7 +361,7 @@ async def verify_presentation( return await verify_presentation( presentation=vp.serialize(), - suites=await self._get_all_suites(), + suites=await self._get_all_proof_suites(), document_loader=self.profile.inject(DocumentLoader), challenge=options.challenge, ) @@ -365,6 +372,6 @@ async def verify_credential( """Verify a VC with a Linked Data Proof.""" return await verify_credential( credential=vc.serialize(), - suites=await self._get_all_suites(), + suites=await self._get_all_proof_suites(), document_loader=self.profile.inject(DocumentLoader), ) diff --git a/aries_cloudagent/vc/vc_ld/tests/test_manager.py b/aries_cloudagent/vc/vc_ld/tests/test_manager.py index 33d03450f6..0f2b06888f 100644 --- a/aries_cloudagent/vc/vc_ld/tests/test_manager.py +++ b/aries_cloudagent/vc/vc_ld/tests/test_manager.py @@ -356,7 +356,7 @@ async def test_issue_bbs( @pytest.mark.asyncio async def test_get_all_suites(manager: VcLdpManager): - suites = await manager._get_all_suites() + suites = await manager._get_all_proof_suites() assert len(suites) == 4 types = ( Ed25519Signature2018,