Skip to content

Commit

Permalink
fix: minor type hint corrections for VcLdpManager
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Bluhm <[email protected]>
  • Loading branch information
dbluhm committed Jan 10, 2024
1 parent 1cfd492 commit ad0e60d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions aries_cloudagent/vc/ld_proofs/suites/linked_data_proof.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions aries_cloudagent/vc/ld_proofs/suites/linked_data_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
51 changes: 29 additions & 22 deletions aries_cloudagent/vc/vc_ld/manager.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
Expand All @@ -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),
)
2 changes: 1 addition & 1 deletion aries_cloudagent/vc/vc_ld/tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit ad0e60d

Please sign in to comment.