Skip to content
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
68 changes: 67 additions & 1 deletion acapy_agent/anoncreds/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,73 @@
from ..core.event_bus import Event
from .models.revocation import RevRegDef

SCHEMA_FINISHED_EVENT = "anoncreds::schema::finished"
CRED_DEF_FINISHED_EVENT = "anoncreds::credential-definition::finished"
REV_REG_DEF_FINISHED_EVENT = "anoncreds::revocation-registry-definition::finished"
REV_LIST_FINISHED_EVENT = "anoncreds::revocation-list::finished"

SCHEMA_FINISHED_PATTERN = re.compile(SCHEMA_FINISHED_EVENT)
CRED_DEF_FINISHED_PATTERN = re.compile(CRED_DEF_FINISHED_EVENT)
REV_REG_DEF_FINISHED_PATTERN = re.compile(REV_REG_DEF_FINISHED_EVENT)
REV_LIST_FINISHED_PATTERN = re.compile(REV_LIST_FINISHED_EVENT)


class SchemaFinishedPayload(NamedTuple):
"""Payload of schema finished event."""

schema_id: str
issuer_id: str
name: str
version: str
attr_names: list
options: dict


class SchemaFinishedEvent(Event):
"""Event for schema finished."""

event_topic = SCHEMA_FINISHED_EVENT

def __init__(
self,
payload: SchemaFinishedPayload,
):
"""Initialize an instance.

Args:
payload: SchemaFinishedPayload

"""
self._topic = self.event_topic
self._payload = payload

@classmethod
def with_payload(
cls,
schema_id: str,
issuer_id: str,
name: str,
version: str,
attr_names: list,
options: Optional[dict] = None,
):
"""With payload."""
payload = SchemaFinishedPayload(
schema_id=schema_id,
issuer_id=issuer_id,
name=name,
version=version,
attr_names=attr_names,
options=options or {},
)
return cls(payload)

@property
def payload(self) -> SchemaFinishedPayload:
"""Return payload."""
return self._payload


class CredDefFinishedPayload(NamedTuple):
"""Payload of cred def finished event."""

Expand All @@ -23,6 +81,7 @@ class CredDefFinishedPayload(NamedTuple):
issuer_id: str
support_revocation: bool
max_cred_num: int
tag: str
options: dict


Expand All @@ -49,11 +108,18 @@ def with_payload(
issuer_id: str,
support_revocation: bool,
max_cred_num: int,
tag: str,
options: Optional[dict] = None,
):
"""With payload."""
payload = CredDefFinishedPayload(
schema_id, cred_def_id, issuer_id, support_revocation, max_cred_num, options
schema_id,
cred_def_id,
issuer_id,
support_revocation,
max_cred_num,
tag,
options,
)
return cls(payload)

Expand Down
46 changes: 38 additions & 8 deletions acapy_agent/anoncreds/issuer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from ..core.profile import Profile
from .base import AnonCredsSchemaAlreadyExists, BaseAnonCredsError
from .error_messages import ANONCREDS_PROFILE_REQUIRED_MSG
from .events import CredDefFinishedEvent
from .events import CredDefFinishedEvent, SchemaFinishedEvent
from .models.credential_definition import CredDef, CredDefResult
from .models.schema import AnonCredsSchema, SchemaResult, SchemaState
from .registry import AnonCredsRegistry
Expand Down Expand Up @@ -152,6 +152,17 @@ async def store_schema(
"state": result.schema_state.state,
},
)
if result.schema_state.state == STATE_FINISHED:
await self.notify(
SchemaFinishedEvent.with_payload(
schema_id=result.schema_state.schema_id,
issuer_id=result.schema_state.schema.issuer_id,
name=result.schema_state.schema.name,
version=result.schema_state.schema.version,
attr_names=result.schema_state.schema.attr_names,
options={},
)
)
except AskarError as err:
raise AnonCredsIssuerError("Error storing schema") from err

Expand Down Expand Up @@ -236,9 +247,23 @@ async def create_and_register_schema(
async def finish_schema(self, job_id: str, schema_id: str):
"""Mark a schema as finished."""
async with self.profile.transaction() as txn:
await self._finish_registration(txn, CATEGORY_SCHEMA, job_id, schema_id)
entry = await self._finish_registration(
txn, CATEGORY_SCHEMA, job_id, schema_id
)
await txn.commit()

schema = AnonCredsSchema.from_json(entry.value)
await self.notify(
SchemaFinishedEvent.with_payload(
schema_id=schema_id,
issuer_id=schema.issuer_id,
name=schema.name,
version=schema.version,
attr_names=schema.attr_names,
options={},
)
)

async def get_created_schemas(
self,
name: Optional[str] = None,
Expand Down Expand Up @@ -413,14 +438,18 @@ async def store_credential_definition(
)
await txn.commit()
if cred_def_result.credential_definition_state.state == STATE_FINISHED:
cred_def = (
cred_def_result.credential_definition_state.credential_definition
)
await self.notify(
CredDefFinishedEvent.with_payload(
schema_result.schema_id,
identifier,
cred_def_result.credential_definition_state.credential_definition.issuer_id,
support_revocation,
max_cred_num,
options,
schema_id=schema_result.schema_id,
cred_def_id=identifier,
issuer_id=cred_def.issuer_id,
support_revocation=support_revocation,
max_cred_num=max_cred_num,
tag=cred_def.tag,
options=options,
)
)
except AskarError as err:
Expand Down Expand Up @@ -453,6 +482,7 @@ async def finish_cred_def(
issuer_id=cred_def.issuer_id,
support_revocation=support_revocation,
max_cred_num=max_cred_num,
tag=cred_def.tag,
options=options,
)
)
Expand Down
Loading
Loading