Skip to content

Commit

Permalink
Add transaction support for writing DIDs
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Costanzo <[email protected]>
  • Loading branch information
ianco committed Jan 18, 2022
1 parent 61b9413 commit 7d02e9a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
4 changes: 4 additions & 0 deletions aries_cloudagent/ledger/indy.py
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,10 @@ async def register_nym(
request_json = await indy.ledger.build_nym_request(
public_info.did, did, verkey, alias, role
)
if endorser_did and not write_ledger:
request_json = await indy.ledger.append_request_endorser(
request_json, endorser_did
)
resp = await self._submit(
request_json, sign=True, sign_did=public_info, write_ledger=write_ledger
) # let ledger raise on insufficient privilege
Expand Down
3 changes: 3 additions & 0 deletions aries_cloudagent/ledger/indy_vdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,9 @@ async def register_nym(
except VdrError as err:
raise LedgerError("Exception when building nym request") from err

if endorser_did and not write_ledger:
nym_req.set_endorser(endorser_did)

resp = await self._submit(
nym_req, sign=True, sign_did=public_info, write_ledger=write_ledger
)
Expand Down
39 changes: 34 additions & 5 deletions aries_cloudagent/ledger/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
UUIDFour,
)

# from ..protocols.endorse_transaction.v1_0.manager import (
# TransactionManager,
# TransactionManagerError,
# )
from ..protocols.endorse_transaction.v1_0.manager import (
TransactionManager,
TransactionManagerError,
)
from ..protocols.endorse_transaction.v1_0.models.transaction_record import (
TransactionRecordSchema,
)
Expand All @@ -49,6 +49,7 @@
)
from .endpoint_type import EndpointType
from .error import BadLedgerRequestError, LedgerError, LedgerTransactionError
from .util import notify_did_event


class LedgerModulesResultSchema(OpenAPISchema):
Expand Down Expand Up @@ -223,6 +224,7 @@ async def register_ledger_nym(request: web.BaseRequest):
request: aiohttp request object
"""
context: AdminRequestContext = request["context"]
outbound_handler = request["outbound_message_router"]
async with context.profile.session() as session:
ledger = session.inject_or(BaseLedger)
if not ledger:
Expand Down Expand Up @@ -314,9 +316,36 @@ async def register_ledger_nym(request: web.BaseRequest):
)
)

if write_ledger:
meta_data = {"verkey": verkey, "alias": alias, "role": role}
if not create_transaction_for_endorser:
# Notify event
await notify_did_event(context.profile, did, meta_data)
return web.json_response({"success": success})
else:
transaction_mgr = TransactionManager(context.profile)
try:
transaction = await transaction_mgr.create_record(
messages_attach=txn["signed_txn"],
connection_id=connection_id,
meta_data=meta_data,
)
except StorageError as err:
raise web.HTTPBadRequest(reason=err.roll_up) from err

# if auto-request, send the request to the endorser
if context.settings.get_value("endorser.auto_request"):
try:
transaction, transaction_request = await transaction_mgr.create_request(
transaction=transaction,
# TODO see if we need to parameterize these params
# expires_time=expires_time,
# endorser_write_txn=endorser_write_txn,
)
except (StorageError, TransactionManagerError) as err:
raise web.HTTPBadRequest(reason=err.roll_up) from err

await outbound_handler(transaction_request, connection_id=connection_id)

return web.json_response({"success": success, "txn": txn})


Expand Down
16 changes: 16 additions & 0 deletions aries_cloudagent/ledger/util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
"""Ledger utilities."""

import re

from ..core.profile import Profile


TAA_ACCEPTED_RECORD_TYPE = "taa_accepted"

DID_EVENT_PREFIX = "acapy::DID::"
EVENT_LISTENER_PATTERN = re.compile(f"^{DID_EVENT_PREFIX}(.*)?$")


async def notify_did_event(profile: Profile, did: str, meta_data: dict):
"""Send notification for a DID post-process event."""
await profile.notify(
DID_EVENT_PREFIX + did,
meta_data,
)

0 comments on commit 7d02e9a

Please sign in to comment.