Skip to content
This repository has been archived by the owner on Sep 22, 2023. It is now read-only.

feat: toolbox can retreive ACA-Py records #141

Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/code-quality-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ env:
jobs:
format:
name: Format and Lint Check
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.6
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ env:
jobs:
test:
name: Tests
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
strategy:
matrix:
python-version: [3.6, 3.7, 3.8, 3.9]
Expand Down
48 changes: 46 additions & 2 deletions acapy_plugin_toolbox/credential_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from aries_cloudagent.messaging.util import canon
from aries_cloudagent.protocols.problem_report.v1_0.message import ProblemReport
from aries_cloudagent.storage.error import StorageNotFoundError
from aries_cloudagent.storage.base import BaseStorage
from marshmallow import fields

from aries_cloudagent.revocation.error import (
Expand Down Expand Up @@ -54,6 +55,8 @@
CRED_DEF_LIST: "acapy_plugin_toolbox.credential_definitions.CredDefList",
}

CRED_DEF_SENT_RECORD_TYPE = "cred_def_sent"

LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -448,7 +451,48 @@ class CredDefGetListHandler(BaseHandler):
async def handle(self, context: RequestContext, responder: BaseResponder):
"""Handle get schema list request."""
session = await context.session()
records = await CredDefRecord.query(session, {})
cred_def_list = CredDefList(results=records)
storage = session.inject(BaseStorage)

toolbox_records = [
cred_def_records
for cred_def_records in await CredDefRecord.query(session, {})
]

toolbox_record_ids = [cred_def.cred_def_id for cred_def in toolbox_records]

acapy_record_ids = [
storage_record.tags["cred_def_id"]
for storage_record in await storage.find_all_records(
CRED_DEF_SENT_RECORD_TYPE
)
]

unknown_record_ids = set(acapy_record_ids) - set(toolbox_record_ids)

if unknown_record_ids:
ledger: BaseLedger = session.inject(BaseLedger)
async with ledger:
cred_defs = [
await ledger.get_credential_definition(cred_def_id)
for cred_def_id in unknown_record_ids
]

cred_def_records = [
CredDefRecord(
cred_def_id=cred_def["id"],
schema_id=cred_def["schema_id"],
attributes=cred_def["attrNames"],
author=CredDefRecord.AUTHOR_SELF,
state=CredDefRecord.STATE_WRITTEN,
)
for cred_def in cred_defs
]

for record in cred_def_records:
await record.save(session)

toolbox_records.extend(cred_def_records)

cred_def_list = CredDefList(results=toolbox_records)
cred_def_list.assign_thread_from(context.message)
await responder.send_reply(cred_def_list)
52 changes: 50 additions & 2 deletions acapy_plugin_toolbox/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
BaseResponder,
RequestContext,
)
from aries_cloudagent.messaging.schemas.routes import add_schema_non_secrets_record
from aries_cloudagent.messaging.models.base_record import BaseRecord, BaseRecordSchema
from aries_cloudagent.ledger.base import BaseLedger
from aries_cloudagent.indy.issuer import IndyIssuer
from aries_cloudagent.storage.error import StorageNotFoundError
from aries_cloudagent.storage.base import BaseStorage

from .util import generate_model_schema, admin_only

PROTOCOL = "did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/admin-schemas/0.1"
SCHEMA_SENT_RECORD_TYPE = "schema_sent"

SEND_SCHEMA = "{}/send-schema".format(PROTOCOL)
SCHEMA_ID = "{}/schema-id".format(PROTOCOL)
Expand Down Expand Up @@ -177,6 +180,9 @@ async def handle(self, context: RequestContext, responder: BaseResponder):
author=SchemaRecord.AUTHOR_SELF,
)
await schema.save(session, reason="Committed to ledger")
await add_schema_non_secrets_record(
profile=context.profile, schema_id=schema_id
)

result = SchemaID(schema_id=schema_id)
result.assign_thread_from(context.message)
Expand Down Expand Up @@ -256,8 +262,50 @@ class SchemaGetListHandler(BaseHandler):
@admin_only
async def handle(self, context: RequestContext, responder: BaseResponder):
"""Handle get schema list request."""

session = await context.session()
records = await SchemaRecord.query(session, {})
schema_list = SchemaList(results=records)
storage = session.inject(BaseStorage)

toolbox_records = [
schema_records for schema_records in await SchemaRecord.query(session, {})
]

toolbox_record_ids = [schema.schema_id for schema in toolbox_records]

acapy_record_ids = [
storage_record.tags["schema_id"]
for storage_record in await storage.find_all_records(
SCHEMA_SENT_RECORD_TYPE
)
]

unknown_record_ids = set(acapy_record_ids) - set(toolbox_record_ids)

if unknown_record_ids:
ledger: BaseLedger = session.inject(BaseLedger)
async with ledger:
schemas = [
await ledger.get_schema(schema_id)
for schema_id in unknown_record_ids
]

schema_records = [
SchemaRecord(
schema_id=schema["id"],
schema_name=schema["name"],
attributes=schema["attrNames"],
schema_version=schema["version"],
author=SchemaRecord.AUTHOR_SELF,
state=SchemaRecord.STATE_WRITTEN,
)
for schema in schemas
]

for record in schema_records:
await record.save(session)

toolbox_records.extend(schema_records)

schema_list = SchemaList(results=toolbox_records)
schema_list.assign_thread_from(context.message)
await responder.send_reply(schema_list)