Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: minor type hint corrections for VcLdpManager #2704

Merged
merged 3 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
date: Union[datetime, None] = None,
date: Optional[datetime] = None,

):
"""Create new LinkedDataSignature instance.
Expand Down
60 changes: 37 additions & 23 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,24 @@ async def _get_suite_for_credential(

return suite

async def _get_all_suites(self):
"""Get all supported suites for verifying presentation."""
suites = []
for suite, key_type in SIGNATURE_SUITE_KEY_TYPE_MAPPING.items():
suites.append(
suite(
async def _get_all_proof_suites(self) -> List[LinkedDataProof]:
"""Get all supported suites for verifying presentation.

Returns a list of suites instantiated with a key type, derived from
PROOF_KEY_TYPE_MAPPING entries.
"""
return [
# Satisfy type checks with a cast to LinkedDataProof
cast(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section could use a short comment to understand what is happening, there's a lot going on in the return statement.

I could suggest:
# We iterate through the PROOF_KEY_TYPE_MAPPING dictionary to return an array of it's items as LinkedDataProof objects

LinkedDataProof,
# Instantiate suite with a key type
SuiteClass(
key_pair=WalletKeyPair(profile=self.profile, key_type=key_type),
)
),
)
return suites
# for each suite class -> key_type pair from PROOF_KEY_TYPE_MAPPING
for SuiteClass, key_type in PROOF_KEY_TYPE_MAPPING.items()
]

async def issue(
self, credential: VerifiableCredential, options: LDProofVCOptions
Expand Down Expand Up @@ -354,7 +368,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 +379,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