Skip to content

Commit

Permalink
feat: make VcLdpManager pluggable
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Bluhm <dbluhm@pm.me>
  • Loading branch information
dbluhm committed Jan 10, 2024
1 parent f3fd68c commit ea9e953
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 35 deletions.
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

0 comments on commit ea9e953

Please sign in to comment.