Skip to content

Commit

Permalink
Merge branch 'main' into fix/vc-ldp-manager-types
Browse files Browse the repository at this point in the history
  • Loading branch information
swcurran authored Jan 16, 2024
2 parents e78d62e + b3c4fd4 commit 2b894b2
Show file tree
Hide file tree
Showing 34 changed files with 503 additions and 164 deletions.
2 changes: 1 addition & 1 deletion aries_cloudagent/config/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,5 @@ def copy(self) -> "BaseInjector":
class BaseProvider(ABC):
"""Base provider class."""

def provide(self, settings: BaseSettings, injector: BaseInjector):
def provide(self, settings: BaseSettings, injector: BaseInjector) -> Any:
"""Provide the object instance given a config and injector."""
10 changes: 6 additions & 4 deletions aries_cloudagent/config/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import hashlib

from typing import Sequence, Union
from typing import Optional, Sequence, Union
from weakref import ReferenceType

from ..utils.classloader import DeferLoad
Expand Down Expand Up @@ -44,16 +44,18 @@ def __init__(
self,
instance_cls: Union[str, type],
*ctor_args,
init_method: str = None,
init_method: Optional[str] = None,
**ctor_kwargs
):
"""Initialize the class provider."""
self._ctor_args = ctor_args
self._ctor_kwargs = ctor_kwargs
self._init_method = init_method
if isinstance(instance_cls, str):
instance_cls = DeferLoad(instance_cls)
self._instance_cls = instance_cls
cls = DeferLoad(instance_cls)
else:
cls = instance_cls
self._instance_cls: Union[type, DeferLoad] = cls

def provide(self, config: BaseSettings, injector: BaseInjector):
"""Provide the object instance given a config and injector."""
Expand Down
12 changes: 9 additions & 3 deletions aries_cloudagent/core/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from abc import ABC, abstractmethod
from typing import Any, Mapping, Optional, Type
from weakref import ref

from .event_bus import EventBus, Event
from ..config.base import InjectionError
Expand All @@ -27,14 +28,19 @@ class Profile(ABC):
def __init__(
self,
*,
context: InjectionContext = None,
name: str = None,
context: Optional[InjectionContext] = None,
name: Optional[str] = None,
created: bool = False,
):
"""Initialize a base profile."""
self._context = context or InjectionContext()
context = context or InjectionContext()
scope = "profile"
if name:
scope += ":" + name
self._context = context.start_scope(scope)
self._created = created
self._name = name or Profile.DEFAULT_NAME
self._context.injector.bind_instance(Profile, ref(self))

@property
def backend(self) -> str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ async def create_proposal(
self, cred_ex_record: V20CredExRecord, proposal_data: Mapping
) -> CredFormatAttachment:
"""Create linked data proof credential proposal."""
manager = VcLdpManager(self.profile)
manager = self.profile.inject(VcLdpManager)
detail = LDProofVCDetail.deserialize(proposal_data)
assert detail.options and isinstance(detail.options, LDProofVCOptions)
assert detail.credential and isinstance(detail.credential, VerifiableCredential)
Expand Down Expand Up @@ -165,7 +165,7 @@ async def create_offer(
# but also when we create an offer (manager does some weird stuff)
offer_data = cred_proposal_message.attachment(LDProofCredFormatHandler.format)
detail = LDProofVCDetail.deserialize(offer_data)
manager = VcLdpManager(self.profile)
manager = self.profile.inject(VcLdpManager)
assert detail.options and isinstance(detail.options, LDProofVCOptions)
assert detail.credential and isinstance(detail.credential, VerifiableCredential)
try:
Expand Down Expand Up @@ -225,7 +225,7 @@ async def create_request(
)

detail = LDProofVCDetail.deserialize(request_data)
manager = VcLdpManager(self.profile)
manager = self.profile.inject(VcLdpManager)
assert detail.options and isinstance(detail.options, LDProofVCOptions)
assert detail.credential and isinstance(detail.credential, VerifiableCredential)
try:
Expand Down Expand Up @@ -291,7 +291,7 @@ async def issue_credential(
LDProofCredFormatHandler.format
)
detail = LDProofVCDetail.deserialize(detail_dict)
manager = VcLdpManager(self.profile)
manager = self.profile.inject(VcLdpManager)
assert detail.options and isinstance(detail.options, LDProofVCOptions)
assert detail.credential and isinstance(detail.credential, VerifiableCredential)
try:
Expand Down Expand Up @@ -382,7 +382,7 @@ async def store_credential(
credential = VerifiableCredential.deserialize(cred_dict, unknown=INCLUDE)

# Get signature suite, proof purpose and document loader
manager = VcLdpManager(self.profile)
manager = self.profile.inject(VcLdpManager)
try:
result = await manager.verify_credential(credential)
except VcLdpManagerError as err:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ async def asyncSetUp(self):
BaseVerificationKeyStrategy, DefaultVerificationKeyStrategy()
)

self.manager = VcLdpManager(self.profile)
self.context.injector.bind_instance(VcLdpManager, self.manager)
self.handler = LDProofCredFormatHandler(self.profile)

self.cred_proposal = V20CredProposal(
Expand Down Expand Up @@ -247,7 +249,7 @@ async def test_receive_proposal(self):

async def test_create_offer(self):
with mock.patch.object(
VcLdpManager,
self.manager,
"assert_can_issue_with_id_and_proof_type",
mock.CoroutineMock(),
) as mock_can_issue, patch.object(
Expand Down Expand Up @@ -287,7 +289,7 @@ async def test_create_offer_adds_bbs_context(self):
)

with mock.patch.object(
VcLdpManager,
self.manager,
"assert_can_issue_with_id_and_proof_type",
mock.CoroutineMock(),
), patch.object(test_module, "get_properties_without_context", return_value=[]):
Expand All @@ -312,7 +314,7 @@ async def test_create_offer_adds_ed25519_2020_context(self):
)

with mock.patch.object(
VcLdpManager,
self.manager,
"assert_can_issue_with_id_and_proof_type",
mock.CoroutineMock(),
), patch.object(test_module, "get_properties_without_context", return_value=[]):
Expand All @@ -334,7 +336,7 @@ async def test_create_offer_x_no_proposal(self):
async def test_create_offer_x_wrong_attributes(self):
missing_properties = ["foo"]
with mock.patch.object(
VcLdpManager,
self.manager,
"assert_can_issue_with_id_and_proof_type",
mock.CoroutineMock(),
), patch.object(
Expand Down Expand Up @@ -583,7 +585,7 @@ async def test_issue_credential(self):
)

with mock.patch.object(
VcLdpManager,
self.manager,
"issue",
mock.CoroutineMock(
return_value=VerifiableCredential.deserialize(LD_PROOF_VC)
Expand Down Expand Up @@ -841,7 +843,7 @@ async def test_store_credential(self):
self.holder.store_credential = mock.CoroutineMock()

with mock.patch.object(
VcLdpManager,
self.manager,
"verify_credential",
mock.CoroutineMock(return_value=DocumentVerificationResult(verified=True)),
) as mock_verify_credential:
Expand Down Expand Up @@ -886,15 +888,15 @@ async def test_store_credential_x_not_verified(self):
self.holder.store_credential = mock.CoroutineMock()

with mock.patch.object(
VcLdpManager,
self.manager,
"_get_suite",
mock.CoroutineMock(),
) as mock_get_suite, mock.patch.object(
VcLdpManager,
self.manager,
"verify_credential",
mock.CoroutineMock(return_value=DocumentVerificationResult(verified=False)),
) as mock_verify_credential, mock.patch.object(
VcLdpManager,
self.manager,
"_get_proof_purpose",
) as mock_get_proof_purpose, self.assertRaises(
V20CredFormatError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ async def verify_pres(self, pres_ex_record: V20PresExRecord) -> V20PresExRecord:
pres_request = pres_ex_record.pres_request.attachment(
DIFPresFormatHandler.format
)
manager = VcLdpManager(self._profile)
manager = self.profile.inject(VcLdpManager)

options = LDProofVCOptions.deserialize(pres_request["options"])
if not options.challenge:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,9 @@ async def asyncSetUp(self):
self.context.injector.bind_instance(DocumentLoader, custom_document_loader)
self.context.injector.bind_instance(BaseResponder, MockResponder())

self.manager = VcLdpManager(self.profile)
self.context.injector.bind_instance(VcLdpManager, self.manager)

self.handler = DIFPresFormatHandler(self.profile)
assert self.handler.profile

Expand Down Expand Up @@ -1140,7 +1143,7 @@ async def test_verify_pres_sequence(self):
)

with mock.patch.object(
VcLdpManager,
self.manager,
"verify_presentation",
mock.CoroutineMock(
return_value=PresentationVerificationResult(verified=True)
Expand All @@ -1150,7 +1153,7 @@ async def test_verify_pres_sequence(self):
assert output.verified

with mock.patch.object(
VcLdpManager,
self.manager,
"verify_presentation",
mock.CoroutineMock(
return_value=PresentationVerificationResult(verified=False)
Expand Down Expand Up @@ -1197,7 +1200,7 @@ async def test_verify_pres(self):
)

with mock.patch.object(
VcLdpManager,
self.manager,
"verify_presentation",
mock.CoroutineMock(
return_value=PresentationVerificationResult(verified=True)
Expand Down
29 changes: 14 additions & 15 deletions aries_cloudagent/protocols/present_proof/v2_0/tests/test_manager.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import json

from copy import deepcopy
import json
from time import time
from unittest import IsolatedAsyncioTestCase

from aries_cloudagent.tests import mock
from unittest import IsolatedAsyncioTestCase

from .. import manager as test_module
from .....core.in_memory import InMemoryProfile
from .....indy.holder import IndyHolder
from .....indy.models.xform import indy_proof_req_preview2indy_requested_creds
from .....indy.models.pres_preview import (
IndyPresAttrSpec,
IndyPresPreview,
IndyPresPredSpec,
IndyPresPreview,
)
from .....indy.models.xform import indy_proof_req_preview2indy_requested_creds
from .....indy.verifier import IndyVerifier
from .....ledger.base import BaseLedger
from .....ledger.multiple_ledger.ledger_requests_executor import (
Expand All @@ -24,23 +24,24 @@
from .....multitenant.base import BaseMultitenantManager
from .....multitenant.manager import MultitenantManager
from .....storage.error import StorageNotFoundError

from .....vc.ld_proofs import DocumentLoader
from .....vc.tests.document_loader import custom_document_loader
from .....vc.vc_ld.manager import VcLdpManager
from .....vc.vc_ld.validation_result import PresentationVerificationResult
from ...indy import pres_exch_handler as test_indy_util_module

from .. import manager as test_module
from ..formats.handler import V20PresFormatHandlerError
from ..formats.dif.handler import DIFPresFormatHandler
from ..formats.dif.tests.test_handler import (
DIF_PRES_REQUEST_B as DIF_PRES_REQ,
DIF_PRES,
DIF_PRES_REQUEST_B as DIF_PRES_REQ,
)
from ..formats.handler import V20PresFormatHandlerError
from ..formats.indy import handler as test_indy_handler
from ..manager import V20PresManager, V20PresManagerError
from ..message_types import (
ATTACHMENT_FORMAT,
PRES_20,
PRES_20_PROPOSAL,
PRES_20_REQUEST,
PRES_20,
)
from ..messages.pres import V20Pres
from ..messages.pres_format import V20PresFormat
Expand All @@ -49,10 +50,6 @@
from ..messages.pres_request import V20PresRequest
from ..models.pres_exchange import V20PresExRecord

from .....vc.vc_ld.validation_result import PresentationVerificationResult
from .....vc.tests.document_loader import custom_document_loader
from .....vc.ld_proofs import DocumentLoader

CONN_ID = "connection_id"
ISSUER_DID = "NcYxiDXkpYi6ov5FcYDi1e"
S_ID = f"{ISSUER_DID}:2:vidya:1.0"
Expand Down Expand Up @@ -484,6 +481,8 @@ async def asyncSetUp(self):
injector.bind_instance(IndyVerifier, self.verifier)

self.manager = V20PresManager(self.profile)
self.vc_manager = VcLdpManager(self.profile)
injector.bind_instance(VcLdpManager, self.vc_manager)

async def test_record_eq(self):
same = [
Expand Down
12 changes: 12 additions & 0 deletions aries_cloudagent/vc/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from ..config.injection_context import InjectionContext
from ..config.provider import ClassProvider
from ..core.profile import Profile


async def setup(context: InjectionContext):
"""Setup vc plugin."""
from .vc_ld.manager import VcLdpManager

context.injector.bind_provider(
VcLdpManager, ClassProvider(VcLdpManager, ClassProvider.Inject(Profile))
)
4 changes: 2 additions & 2 deletions aries_cloudagent/vc/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async def ldp_issue(request: web.BaseRequest):
options = LDProofVCOptions.deserialize(body["options"])

try:
manager = VcLdpManager(context.profile)
manager = context.inject(VcLdpManager)
vc = await manager.issue(credential, options)
except VcLdpManagerError as err:
return web.json_response({"error": str(err)}, status=400)
Expand Down Expand Up @@ -104,7 +104,7 @@ async def ldp_verify(request: web.BaseRequest):
vp = body.get("vp")
vc = body.get("vc")
try:
manager = VcLdpManager(context.profile)
manager = context.inject(VcLdpManager)
if vp:
vp = VerifiableCredential.deserialize(vp)
options = LDProofVCOptions.deserialize(body["options"])
Expand Down
26 changes: 26 additions & 0 deletions docs/generated/aries_cloudagent.anoncreds.default.did_indy.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
aries\_cloudagent.anoncreds.default.did\_indy package
=====================================================

.. automodule:: aries_cloudagent.anoncreds.default.did_indy
:members:
:undoc-members:
:show-inheritance:

Submodules
----------

aries\_cloudagent.anoncreds.default.did\_indy.registry module
-------------------------------------------------------------

.. automodule:: aries_cloudagent.anoncreds.default.did_indy.registry
:members:
:undoc-members:
:show-inheritance:

aries\_cloudagent.anoncreds.default.did\_indy.routes module
-----------------------------------------------------------

.. automodule:: aries_cloudagent.anoncreds.default.did_indy.routes
:members:
:undoc-members:
:show-inheritance:
Loading

0 comments on commit 2b894b2

Please sign in to comment.