Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve api documentation and error handling #2690

Merged
merged 6 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 2 additions & 3 deletions aries_cloudagent/anoncreds/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
from .models.anoncreds_revocation import (
GetRevListResult,
GetRevRegDefResult,
RevRegDef,
RevRegDefResult,
RevList,
RevListResult,
RevRegDef,
RevRegDefResult,
)
from .models.anoncreds_schema import AnonCredsSchema, GetSchemaResult, SchemaResult


T = TypeVar("T")


Expand Down
12 changes: 10 additions & 2 deletions aries_cloudagent/anoncreds/default/legacy_indy/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,15 +636,23 @@ async def register_revocation_list(
profile, rev_list, rev_reg_def.type, rev_reg_entry
)

seq_no = None
try:
seq_no = rev_entry_res["result"]["txnMetadata"]["seqNo"]
jamshale marked this conversation as resolved.
Show resolved Hide resolved
except KeyError:
LOGGER.warning("Failed to parse sequence number from ledger response")

return RevListResult(
job_id=None,
revocation_list_state=RevListState(
state=RevListState.STATE_FINISHED,
revocation_list=rev_list,
),
registration_metadata={},
revocation_list_metadata={
"seqNo": rev_entry_res["result"]["txnMetadata"]["seqNo"],
revocation_list_metadata={}
if seq_no is None
else {
"seqNo": seq_no,
},
)

Expand Down
4 changes: 1 addition & 3 deletions aries_cloudagent/anoncreds/issuer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
from time import time
from typing import Optional, Sequence

from aries_askar import AskarError

from anoncreds import (
AnoncredsError,
Credential,
CredentialDefinition,
CredentialOffer,
Schema,
)

from aries_askar import AskarError

from ..askar.profile_anon import (
AskarAnoncredsProfile,
Expand Down
30 changes: 23 additions & 7 deletions aries_cloudagent/anoncreds/models/anoncreds_cred_def.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
"""Anoncreds cred def OpenAPI validators."""
from typing import Optional
from typing_extensions import Literal

from anoncreds import CredentialDefinition
from marshmallow import EXCLUDE, fields
from marshmallow.validate import OneOf
from typing_extensions import Literal

from ...messaging.models.base import BaseModel, BaseModelSchema
from ...messaging.valid import NUM_STR_WHOLE_VALIDATE, NUM_STR_WHOLE_EXAMPLE

from ...messaging.valid import (
INDY_CRED_DEF_ID_EXAMPLE,
INDY_OR_KEY_DID_EXAMPLE,
INDY_SCHEMA_ID_EXAMPLE,
NUM_STR_WHOLE_EXAMPLE,
NUM_STR_WHOLE_VALIDATE,
)

NUM_STR_WHOLE = {"validate": NUM_STR_WHOLE_VALIDATE, "example": NUM_STR_WHOLE_EXAMPLE}

Expand Down Expand Up @@ -239,12 +244,18 @@ class Meta:
issuer_id = fields.Str(
description="Issuer Identifier of the credential definition or schema",
data_key="issuerId",
example=INDY_OR_KEY_DID_EXAMPLE,
)
schema_id = fields.Str(
data_key="schemaId",
description="Schema identifier",
example=INDY_SCHEMA_ID_EXAMPLE,
)
schema_id = fields.Str(data_key="schemaId", description="Schema identifier")
type = fields.Str(validate=OneOf(["CL"]))
tag = fields.Str(
description="""The tag value passed in by the Issuer to
an AnonCred's Credential Definition create and store implementation."""
an AnonCred's Credential Definition create and store implementation.""",
example="default",
)
value = fields.Nested(CredDefValueSchema())

Expand Down Expand Up @@ -303,7 +314,9 @@ class Meta:
)
)
credential_definition_id = fields.Str(
description="credential definition id", allow_none=True
description="credential definition id",
allow_none=True,
example=INDY_CRED_DEF_ID_EXAMPLE,
)
credential_definition = fields.Nested(
CredDefSchema(), description="credential definition"
Expand Down Expand Up @@ -403,7 +416,10 @@ class Meta:
model_class = GetCredDefResult
unknown = EXCLUDE

credential_definition_id = fields.Str(description="credential definition id")
credential_definition_id = fields.Str(
description="credential definition id",
example=INDY_CRED_DEF_ID_EXAMPLE,
)
credential_definition = fields.Nested(
CredDefSchema(), description="credential definition"
)
Expand Down
46 changes: 37 additions & 9 deletions aries_cloudagent/anoncreds/models/anoncreds_revocation.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
"""Anoncreds cred def OpenAPI validators."""
from typing import Any, Dict, List, Optional
from typing_extensions import Literal

from anoncreds import RevocationRegistryDefinition, RevocationStatusList
from marshmallow import EXCLUDE, fields
from marshmallow.validate import OneOf
from typing_extensions import Literal

from aries_cloudagent.messaging.valid import (
INDY_CRED_DEF_ID_EXAMPLE,
INDY_ISO8601_DATETIME_EXAMPLE,
INDY_OR_KEY_DID_EXAMPLE,
INDY_RAW_PUBLIC_KEY_EXAMPLE,
INDY_REV_REG_ID_EXAMPLE,
)

from ...messaging.models.base import BaseModel, BaseModelSchema

Expand Down Expand Up @@ -52,10 +60,17 @@ class Meta:
model_class = RevRegDefValue
unknown = EXCLUDE

public_keys = fields.Dict(data_key="publicKeys")
max_cred_num = fields.Int(data_key="maxCredNum")
tails_location = fields.Str(data_key="tailsLocation")
tails_hash = fields.Str(data_key="tailsHash")
public_keys = fields.Dict(
data_key="publicKeys", example=INDY_RAW_PUBLIC_KEY_EXAMPLE
)
max_cred_num = fields.Int(data_key="maxCredNum", example=666)
tails_location = fields.Str(
data_key="tailsLocation",
example="https://tails-server.com/hash/7Qen9RDyemMuV7xGQvp7NjwMSpyHieJyBakycxN7dX7P",
)
tails_hash = fields.Str(
data_key="tailsHash", example="7Qen9RDyemMuV7xGQvp7NjwMSpyHieJyBakycxN7dX7P"
)


class RevRegDef(BaseModel):
Expand Down Expand Up @@ -116,13 +131,17 @@ class Meta:
issuer_id = fields.Str(
description="Issuer Identifier of the credential definition or schema",
data_key="issuerId",
example=INDY_OR_KEY_DID_EXAMPLE,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little hesitant about the only examples showing as old Indy values. Is it possible to provide more than one example so we could do something like did:example:123?

Also, I believe most of these fields should be in the metadata kwarg now after updates to marshmallow, as a side note. That change probably belongs in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had thought about that. I was thinking when we add more methods we could figure something out for the other examples. For the metadata stuff I saw both methods used and wasn't sure the correct way.

)
type = fields.Str(data_key="revocDefType")
cred_def_id = fields.Str(
description="Credential definition identifier",
data_key="credDefId",
example=INDY_CRED_DEF_ID_EXAMPLE,
)
tag = fields.Str(
description="tag for the revocation registry definition", example="default"
)
tag = fields.Str(description="tag for the revocation registry definition")
value = fields.Nested(RevRegDefValueSchema())


Expand Down Expand Up @@ -184,7 +203,8 @@ class Meta:
)
)
revocation_registry_definition_id = fields.Str(
description="revocation registry definition id"
description="revocation registry definition id",
example=INDY_REV_REG_ID_EXAMPLE,
)
revocation_registry_definition = fields.Nested(
RevRegDefSchema(), description="revocation registry definition"
Expand Down Expand Up @@ -362,20 +382,28 @@ class Meta:
issuer_id = fields.Str(
description="Issuer Identifier of the credential definition or schema",
data_key="issuerId",
example=INDY_OR_KEY_DID_EXAMPLE,
)
rev_reg_def_id = fields.Str(
description="",
description="The ID of the revocation registry definition",
data_key="revRegDefId",
example=INDY_REV_REG_ID_EXAMPLE,
)
revocation_list = fields.List(
fields.Int(),
description="Bit list representing revoked credentials",
data_key="revocationList",
example=[0, 1, 1, 0],
)
current_accumulator = fields.Str(
description="The current accumalator value",
example="21 118...1FB",
data_key="currentAccumulator",
)
current_accumulator = fields.Str(data_key="currentAccumulator")
timestamp = fields.Int(
description="Timestamp at which revocation list is applicable",
required=False,
example=INDY_ISO8601_DATETIME_EXAMPLE,
)


Expand Down
22 changes: 15 additions & 7 deletions aries_cloudagent/anoncreds/models/anoncreds_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

from typing import Any, Dict, List, Optional

from anoncreds import Schema
from marshmallow import EXCLUDE, fields
from marshmallow.validate import OneOf

from anoncreds import Schema
from aries_cloudagent.messaging.valid import (
INDY_OR_KEY_DID_EXAMPLE,
INDY_SCHEMA_ID_EXAMPLE,
)

from ...messaging.models.base import BaseModel, BaseModelSchema

Expand Down Expand Up @@ -60,6 +64,7 @@ class Meta:
issuer_id = fields.Str(
description="Issuer Identifier of the credential definition or schema",
data_key="issuerId",
example=INDY_OR_KEY_DID_EXAMPLE,
)
attr_names = fields.List(
fields.Str(
Expand All @@ -69,10 +74,8 @@ class Meta:
description="Schema attribute names",
data_key="attrNames",
)
name = fields.Str(
description="Schema name",
)
version = fields.Str(description="Schema version")
name = fields.Str(description="Schema name", example="Example schema")
version = fields.Str(description="Schema version", example="1.0")


class GetSchemaResult(BaseModel):
Expand Down Expand Up @@ -127,7 +130,9 @@ class Meta:
unknown = EXCLUDE

schema_value = fields.Nested(AnonCredsSchemaSchema(), data_key="schema")
schema_id = fields.Str(data_key="schemaId", description="Schema identifier")
schema_id = fields.Str(
description="Schema identifier", example=INDY_SCHEMA_ID_EXAMPLE
)
resolution_metadata = fields.Dict()
schema_metadata = fields.Dict()

Expand Down Expand Up @@ -179,7 +184,10 @@ class Meta:
]
)
)
schema_id = fields.Str(description="Schema identifier")
schema_id = fields.Str(
description="Schema identifier",
example=INDY_SCHEMA_ID_EXAMPLE,
)
schema_value = fields.Nested(AnonCredsSchemaSchema(), data_key="schema")


Expand Down
9 changes: 9 additions & 0 deletions aries_cloudagent/anoncreds/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ async def _resolver_for_identifier(self, identifier: str) -> BaseAnonCredsResolv
for resolver in self.resolvers
if await resolver.supports(identifier)
]
if len(resolvers) == 0:
raise AnonCredsResolutionError(
f"No resolver available for identifier {identifier}"
)
if len(resolvers) > 1:
raise AnonCredsResolutionError(
f"More than one resolver found for identifier {identifier}"
Expand All @@ -67,6 +71,11 @@ async def _registrar_for_identifier(
for registrar in self.registrars
if await registrar.supports(identifier)
]
if len(registrars) == 0:
raise AnonCredsRegistrationError(
f"No registrar available for identifier {identifier}"
)

if len(registrars) > 1:
raise AnonCredsRegistrationError(
f"More than one registrar found for identifier {identifier}"
Expand Down
Loading