Skip to content

Commit

Permalink
Merge branch 'hyperledger:main' into feature/event-bus-includes-metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
burdettadam authored Sep 30, 2021
2 parents e1c7a2b + 59bc646 commit 2d4ff9a
Show file tree
Hide file tree
Showing 35 changed files with 5,697 additions and 5,426 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ end-user and developer demos in the repo should include updates or extensions to

If you would like to propose a significant change, please open an issue first to discuss the work with the community.

All contributors retain the original copyright to their stuff, but by contributing to this project, you grant a world-wide, royalty-free, perpetual, irrevocable, non-exclusive, transferable license to all users **under the terms of the [license](./LICENSE) under which this project is distributed.**
Contributions are made pursuant to the Developer's Certificate of Origin, available at [https://developercertificate.org](https://developercertificate.org), and licensed under the Apache License, version 2.0 (Apache-2.0).
61 changes: 40 additions & 21 deletions aries_cloudagent/connections/base_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from pydid.verification_method import Ed25519VerificationKey2018

from ..core.error import BaseError
from ..core.profile import ProfileSession
from ..core.profile import Profile
from ..did.did_key import DIDKey
from ..protocols.connections.v1_0.messages.connection_invitation import (
ConnectionInvitation,
Expand Down Expand Up @@ -47,15 +47,15 @@ class BaseConnectionManager:
RECORD_TYPE_DID_KEY = "did_key"
SUPPORTED_KEY_TYPES = (Ed25519VerificationKey2018,)

def __init__(self, session: ProfileSession):
def __init__(self, profile: Profile):
"""
Initialize a BaseConnectionManager.
Args:
session: The profile session for this presentation
"""
self._logger = logging.getLogger(__name__)
self._session = session
self._profile = profile

async def create_did_document(
self,
Expand Down Expand Up @@ -96,7 +96,8 @@ async def create_did_document(
router_idx = 1
while router_id:
# look up routing connection information
router = await ConnRecord.retrieve_by_id(self._session, router_id)
async with self._profile.session() as session:
router = await ConnRecord.retrieve_by_id(session, router_id)
if ConnRecord.State.get(router.state) != ConnRecord.State.COMPLETED:
raise BaseConnectionManagerError(
f"Router connection not completed: {router_id}"
Expand Down Expand Up @@ -166,7 +167,7 @@ async def store_did_document(self, did_doc: DIDDoc):
did_doc: The `DIDDoc` instance to persist
"""
assert did_doc.did
storage: BaseStorage = self._session.inject(BaseStorage)

try:
stored_doc, record = await self.fetch_did_document(did_doc.did)
except StorageNotFoundError:
Expand All @@ -175,9 +176,15 @@ async def store_did_document(self, did_doc: DIDDoc):
did_doc.to_json(),
{"did": did_doc.did},
)
await storage.add_record(record)
async with self._profile.session() as session:
storage: BaseStorage = session.inject(BaseStorage)
await storage.add_record(record)
else:
await storage.update_record(record, did_doc.to_json(), {"did": did_doc.did})
async with self._profile.session() as session:
storage: BaseStorage = session.inject(BaseStorage)
await storage.update_record(
record, did_doc.to_json(), {"did": did_doc.did}
)
await self.remove_keys_for_did(did_doc.did)
for key in did_doc.pubkey.values():
if key.controller == did_doc.did:
Expand All @@ -191,17 +198,19 @@ async def add_key_for_did(self, did: str, key: str):
key: The verkey to be added
"""
record = StorageRecord(self.RECORD_TYPE_DID_KEY, key, {"did": did, "key": key})
storage = self._session.inject(BaseStorage)
await storage.add_record(record)
async with self._profile.session() as session:
storage: BaseStorage = session.inject(BaseStorage)
await storage.add_record(record)

async def find_did_for_key(self, key: str) -> str:
"""Find the DID previously associated with a key.
Args:
key: The verkey to look up
"""
storage = self._session.inject(BaseStorage)
record = await storage.find_record(self.RECORD_TYPE_DID_KEY, {"key": key})
async with self._profile.session() as session:
storage: BaseStorage = session.inject(BaseStorage)
record = await storage.find_record(self.RECORD_TYPE_DID_KEY, {"key": key})
return record.tags["did"]

async def remove_keys_for_did(self, did: str):
Expand All @@ -210,8 +219,9 @@ async def remove_keys_for_did(self, did: str):
Args:
did: The DID for which to remove keys
"""
storage = self._session.inject(BaseStorage)
await storage.delete_all_records(self.RECORD_TYPE_DID_KEY, {"did": did})
async with self._profile.session() as session:
storage: BaseStorage = session.inject(BaseStorage)
await storage.delete_all_records(self.RECORD_TYPE_DID_KEY, {"did": did})

async def resolve_invitation(self, did: str):
"""
Expand All @@ -225,9 +235,9 @@ async def resolve_invitation(self, did: str):
# prefix with did:sov: for backwards compatibility
did = f"did:sov:{did}"

resolver = self._session.inject(DIDResolver)
resolver = self._profile.inject(DIDResolver)
try:
doc_dict: dict = await resolver.resolve(self._session.profile, did)
doc_dict: dict = await resolver.resolve(self._profile, did)
doc: ResolvedDocument = pydid.deserialize_document(doc_dict, strict=True)
except ResolverError as error:
raise BaseConnectionManagerError(
Expand Down Expand Up @@ -284,9 +294,6 @@ async def fetch_connection_targets(
if not connection.my_did:
self._logger.debug("No local DID associated with connection")
return None

wallet = self._session.inject(BaseWallet)
my_info = await wallet.get_local_did(connection.my_did)
results = None

if (
Expand All @@ -299,7 +306,8 @@ async def fetch_connection_targets(
or connection.invitation_key
or not connection.their_did
):
invitation = await connection.retrieve_invitation(self._session)
async with self._profile.session() as session:
invitation = await connection.retrieve_invitation(session)
if isinstance(
invitation, ConnectionInvitation
): # conn protocol invitation
Expand Down Expand Up @@ -343,6 +351,11 @@ async def fetch_connection_targets(
recipient_keys,
routing_keys,
) = await self.resolve_invitation(did)

async with self._profile.session() as session:
wallet = session.inject(BaseWallet)
my_info = await wallet.get_local_did(connection.my_did)

results = [
ConnectionTarget(
did=connection.their_did,
Expand All @@ -359,6 +372,11 @@ async def fetch_connection_targets(
return None

did_doc, _ = await self.fetch_did_document(connection.their_did)

async with self._profile.session() as session:
wallet = session.inject(BaseWallet)
my_info = await wallet.get_local_did(connection.my_did)

results = self.diddoc_connection_targets(
did_doc, my_info.verkey, connection.their_label
)
Expand Down Expand Up @@ -408,6 +426,7 @@ async def fetch_did_document(self, did: str) -> Tuple[DIDDoc, StorageRecord]:
Args:
did: The DID to search for
"""
storage = self._session.inject(BaseStorage)
record = await storage.find_record(self.RECORD_TYPE_DID_DOC, {"did": did})
async with self._profile.session() as session:
storage = session.inject(BaseStorage)
record = await storage.find_record(self.RECORD_TYPE_DID_DOC, {"did": did})
return DIDDoc.from_json(record.value), record
13 changes: 7 additions & 6 deletions aries_cloudagent/connections/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
from ..protocols.coordinate_mediation.v1_0.models.mediation_record import (
MediationRecord,
)
from ..core.profile import ProfileSession
from ..core.profile import Profile

from .base_manager import BaseConnectionManagerError


async def mediation_record_if_id(
session: ProfileSession, mediation_id: str = None, or_default: bool = False
profile: Profile, mediation_id: str = None, or_default: bool = False
):
"""Validate mediation and return record.
Expand All @@ -20,11 +20,12 @@ async def mediation_record_if_id(
"""
mediation_record = None
if mediation_id:
mediation_record = await MediationRecord.retrieve_by_id(session, mediation_id)
async with profile.session() as session:
mediation_record = await MediationRecord.retrieve_by_id(
session, mediation_id
)
elif or_default:
mediation_record = await MediationManager(
session.profile
).get_default_mediator()
mediation_record = await MediationManager(profile).get_default_mediator()

if mediation_record:
if mediation_record.state != MediationRecord.STATE_GRANTED:
Expand Down
Loading

0 comments on commit 2d4ff9a

Please sign in to comment.