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

Initial code migration from anoncreds-rs branch #2596

Merged
merged 20 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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 .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ RUN curl -sSL https://install.python-poetry.org | python3 - \
COPY pyproject.toml poetry.lock ./
RUN poetry config virtualenvs.create false \
&& poetry install --no-root --no-interaction --all-extras \
&& rm -rf /root/.cache/pypoetry
&& rm -rf /root/.cache/pypoetry
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
repos:
- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
rev: v9.5.0
Expand Down
42 changes: 42 additions & 0 deletions aries_cloudagent/anoncreds/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import logging

from ..config.injection_context import InjectionContext
from ..config.provider import ClassProvider

from .registry import AnonCredsRegistry

LOGGER = logging.getLogger(__name__)


async def setup(context: InjectionContext):
"""Set up default resolvers."""
registry = context.inject_or(AnonCredsRegistry)
if not registry:
LOGGER.error("No AnonCredsRegistry instance found in context!!!")
return

indy_registry = ClassProvider(
"aries_cloudagent.anoncreds.default.did_indy.registry.DIDIndyRegistry",
# supported_identifiers=[],
# method_name="did:indy",
).provide(context.settings, context.injector)
await indy_registry.setup(context)
registry.register(indy_registry)

web_registry = ClassProvider(
"aries_cloudagent.anoncreds.default.did_web.registry.DIDWebRegistry",
# supported_identifiers=[],
# method_name="did:web",
).provide(context.settings, context.injector)
await web_registry.setup(context)
registry.register(web_registry)

legacy_indy_registry = ClassProvider(
"aries_cloudagent.anoncreds.default.legacy_indy.registry.LegacyIndyRegistry",
# supported_identifiers=[],
# method_name="",
).provide(context.settings, context.injector)
await legacy_indy_registry.setup(context)
registry.register(legacy_indy_registry)

# TODO: add context.settings
190 changes: 190 additions & 0 deletions aries_cloudagent/anoncreds/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
"""Base Registry."""
from abc import ABC, abstractmethod
from typing import Generic, Optional, Pattern, Sequence, TypeVar

from ..config.injection_context import InjectionContext
from ..core.error import BaseError
from ..core.profile import Profile
from .models.anoncreds_cred_def import (
CredDef,
CredDefResult,
GetCredDefResult,
)
from .models.anoncreds_revocation import (
GetRevListResult,
GetRevRegDefResult,
RevRegDef,
RevRegDefResult,
RevList,
RevListResult,
)
from .models.anoncreds_schema import AnonCredsSchema, GetSchemaResult, SchemaResult


T = TypeVar("T")


class BaseAnonCredsError(BaseError):
"""Base error class for AnonCreds."""


class AnonCredsObjectNotFound(BaseAnonCredsError):
"""Raised when object is not found in resolver."""

def __init__(
self, message: Optional[str] = None, resolution_metadata: Optional[dict] = None
):
"""Constructor."""
super().__init__(message, resolution_metadata)
self.resolution_metadata = resolution_metadata


class AnonCredsRegistrationError(BaseAnonCredsError):
"""Raised when registering an AnonCreds object fails."""


class AnonCredsObjectAlreadyExists(AnonCredsRegistrationError, Generic[T]):
"""Raised when an AnonCreds object already exists."""

def __init__(
self,
message: str,
obj_id: str,
obj: T = None,
*args,
**kwargs,
):
"""Initialize an instance.
Args:
message: Message
obj_id: Object ID
obj: Object
TODO: update this docstring - Anoncreds-break.
"""
super().__init__(message, obj_id, obj, *args, **kwargs)
self._message = message
self.obj_id = obj_id
self.obj = obj

@property
def message(self):
"""Message."""
return f"{self._message}: {self.obj_id}, {self.obj}"


class AnonCredsSchemaAlreadyExists(AnonCredsObjectAlreadyExists[AnonCredsSchema]):
"""Raised when a schema already exists."""

@property
def schema_id(self):
"""Get Schema Id."""
return self.obj_id

@property
def schema(self):
"""Get Schema."""
return self.obj


class AnonCredsResolutionError(BaseAnonCredsError):
"""Raised when resolving an AnonCreds object fails."""


class BaseAnonCredsHandler(ABC):
"""Base Anon Creds Handler."""

@property
@abstractmethod
def supported_identifiers_regex(self) -> Pattern:
"""Regex to match supported identifiers."""

async def supports(self, identifier: str) -> bool:
"""Determine whether this registry supports the given identifier."""
return bool(self.supported_identifiers_regex.match(identifier))

@abstractmethod
async def setup(self, context: InjectionContext):
"""Class Setup method."""


class BaseAnonCredsResolver(BaseAnonCredsHandler):
"""Base Anon Creds Resolver."""

@abstractmethod
async def get_schema(self, profile: Profile, schema_id: str) -> GetSchemaResult:
"""Get a schema from the registry."""

@abstractmethod
async def get_credential_definition(
self, profile: Profile, credential_definition_id: str
) -> GetCredDefResult:
"""Get a credential definition from the registry."""

@abstractmethod
async def get_revocation_registry_definition(
self, profile: Profile, revocation_registry_id: str
) -> GetRevRegDefResult:
"""Get a revocation registry definition from the registry."""

@abstractmethod
async def get_revocation_list(
self, profile: Profile, revocation_registry_id: str, timestamp: int
) -> GetRevListResult:
"""Get a revocation list from the registry."""


class BaseAnonCredsRegistrar(BaseAnonCredsHandler):
"""Base Anon Creds Registrar."""

@abstractmethod
async def register_schema(
self,
profile: Profile,
schema: AnonCredsSchema,
options: Optional[dict] = None,
) -> SchemaResult:
"""Register a schema on the registry."""

@abstractmethod
async def register_credential_definition(
self,
profile: Profile,
schema: GetSchemaResult,
credential_definition: CredDef,
options: Optional[dict] = None,
) -> CredDefResult:
"""Register a credential definition on the registry."""

@abstractmethod
async def register_revocation_registry_definition(
self,
profile: Profile,
revocation_registry_definition: RevRegDef,
options: Optional[dict] = None,
) -> RevRegDefResult:
"""Register a revocation registry definition on the registry."""

@abstractmethod
async def register_revocation_list(
self,
profile: Profile,
rev_reg_def: RevRegDef,
rev_list: RevList,
options: Optional[dict] = None,
) -> RevListResult:
"""Register a revocation list on the registry."""

@abstractmethod
async def update_revocation_list(
self,
profile: Profile,
rev_reg_def: RevRegDef,
prev_list: RevList,
curr_list: RevList,
revoked: Sequence[int],
options: Optional[dict] = None,
) -> RevListResult:
"""Update a revocation list on the registry."""
Empty file.
Empty file.
119 changes: 119 additions & 0 deletions aries_cloudagent/anoncreds/default/did_indy/registry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
"""DID Indy Registry."""
import logging
import re
from typing import Optional, Pattern, Sequence

from ....config.injection_context import InjectionContext
from ....core.profile import Profile
from ...models.anoncreds_cred_def import (
CredDef,
CredDefResult,
GetCredDefResult,
)
from ...models.anoncreds_revocation import (
GetRevListResult,
GetRevRegDefResult,
RevRegDef,
RevRegDefResult,
RevList,
RevListResult,
)
from ...models.anoncreds_schema import AnonCredsSchema, GetSchemaResult, SchemaResult
from ...base import BaseAnonCredsRegistrar, BaseAnonCredsResolver

LOGGER = logging.getLogger(__name__)


class DIDIndyRegistry(BaseAnonCredsResolver, BaseAnonCredsRegistrar):
"""DIDIndyRegistry."""

def __init__(self):
"""Initialize an instance.
Args:
TODO: update this docstring - Anoncreds-break.
"""
self._supported_identifiers_regex = re.compile(r"^did:indy:.*$")

@property
def supported_identifiers_regex(self) -> Pattern:
"""Supported Identifiers regex."""
return self._supported_identifiers_regex
# TODO: fix regex (too general)

async def setup(self, context: InjectionContext):
"""Setup."""
print("Successfully registered DIDIndyRegistry")

async def get_schema(self, profile: Profile, schema_id: str) -> GetSchemaResult:
"""Get a schema from the registry."""
raise NotImplementedError()

async def register_schema(
self,
profile: Profile,
schema: AnonCredsSchema,
options: Optional[dict],
) -> SchemaResult:
"""Register a schema on the registry."""
raise NotImplementedError()

async def get_credential_definition(
self, profile: Profile, credential_definition_id: str
) -> GetCredDefResult:
"""Get a credential definition from the registry."""
raise NotImplementedError()

async def register_credential_definition(
self,
profile: Profile,
schema: GetSchemaResult,
credential_definition: CredDef,
options: Optional[dict] = None,
) -> CredDefResult:
"""Register a credential definition on the registry."""
raise NotImplementedError()

async def get_revocation_registry_definition(
self, profile: Profile, revocation_registry_id: str
) -> GetRevRegDefResult:
"""Get a revocation registry definition from the registry."""
raise NotImplementedError()

async def register_revocation_registry_definition(
self,
profile: Profile,
revocation_registry_definition: RevRegDef,
options: Optional[dict] = None,
) -> RevRegDefResult:
"""Register a revocation registry definition on the registry."""
raise NotImplementedError()

async def get_revocation_list(
self, profile: Profile, revocation_registry_id: str, timestamp: int
) -> GetRevListResult:
"""Get a revocation list from the registry."""
raise NotImplementedError()

async def register_revocation_list(
self,
profile: Profile,
rev_reg_def: RevRegDef,
rev_list: RevList,
options: Optional[dict] = None,
) -> RevListResult:
"""Register a revocation list on the registry."""
raise NotImplementedError()

async def update_revocation_list(
self,
profile: Profile,
rev_reg_def: RevRegDef,
prev_list: RevList,
curr_list: RevList,
revoked: Sequence[int],
options: Optional[dict] = None,
) -> RevListResult:
"""Update a revocation list on the registry."""
raise NotImplementedError()
1 change: 1 addition & 0 deletions aries_cloudagent/anoncreds/default/did_indy/routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Routes for DID Indy Registry."""
Empty file.
Loading