Skip to content

Commit

Permalink
update register_auto_id to return identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrank-summit committed Apr 11, 2024
1 parent a8c7ec9 commit 1196479
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 52 deletions.
59 changes: 16 additions & 43 deletions auto_identity/registry.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from typing import Optional
from typing import Optional, NamedTuple
from cryptography import x509
from cryptography.hazmat.primitives.asymmetric import ed25519
from substrateinterface import ExtrinsicReceipt, Keypair, SubstrateInterface, exceptions
from .utils import der_encode_signature_algorithm_oid


class RegistrationResult(NamedTuple):
receipt: Optional[ExtrinsicReceipt]
identifier: Optional[int]


class Registry():
"""
Registry class for managing identities on Autonomys identity domains.
Expand Down Expand Up @@ -51,7 +56,7 @@ def _compose_call(self, call_function: str, call_params: dict) -> Optional[Extri
print("Failed to send: {}".format(e))
return None

def register_auto_id(self, certificate: x509.Certificate, issuer_id=None):
def register_auto_id(self, certificate: x509.Certificate, issuer_id=None) -> RegistrationResult:
"""
Register a certificate in the registry.
Expand All @@ -60,7 +65,8 @@ def register_auto_id(self, certificate: x509.Certificate, issuer_id=None):
issuer_id: The issuer ID. If None, the ID will be self-issued.
Returns:
Optional[ExtrinsicReceipt]: The receipt of the extrinsic if successful, None otherwise.
RegistrationResult: A named tuple containing the receipt of the extrinsic and the identifier if successful,
or None for both fields if unsuccessful.
"""

base_certificate = {
Expand All @@ -82,45 +88,12 @@ def register_auto_id(self, certificate: x509.Certificate, issuer_id=None):

receipt = self._compose_call(
call_function="register_auto_id", call_params=req)
return receipt

def get_auto_id(self, identifier):
"""
Get an auto identity from the registry.
Args:
subject_name (str): The subject name of the certificate.
Returns:
The auto entity with the provided subject name.
"""

result = self.registry.query('auto-id', 'AutoIds', [identifier])

if result is None:
return None

# TODO map result to AutoEntity type
auto_entity = result

return auto_entity

def verify(self, subject_name: str, public_key: ed25519.Ed25519PublicKey):
"""
Verify a certificate from the registry.
Args:
subject_name (str): The subject name of the certificate.
public_key (ed25519.Ed25519PublicKey): The public key to verify.
Returns:
bool: True if the certificate is valid, False otherwise.
"""
entity = self.get_auto_id(subject_name)

if entity is None:
return False

# TODO check public key and subject name match certificate, and that it is within the validity period
if receipt.is_success:
for event in receipt.triggered_events:
event_data = event['event'].serialize()
if event_data.get('event_id') == 'NewAutoIdRegistered':
identifier = event_data['attributes']
return RegistrationResult(receipt=receipt, identifier=identifier)

return True
return RegistrationResult(receipt=receipt, identifier=None)
12 changes: 3 additions & 9 deletions examples/register_auto_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,10 @@

def register(certificate, registry, issuer_id=None):
# Attempt to register the certificate
receipt = registry.register_auto_id(certificate, issuer_id)
receipt, identifer = registry.register_auto_id(certificate, issuer_id)
if receipt.is_success:
for event in receipt.triggered_events:
event_data = event['event'].serialize()
print(event_data)
if event_data.get('event_id') == 'NewAutoIdRegistered':
auto_id_identifier = event_data['attributes']
print(
f"New Auto Id registered with identifier: {auto_id_identifier}")
return auto_id_identifier
print(f"Registration successful. {identifer}")
return identifer
else:
print("Registration failed.")

Expand Down

0 comments on commit 1196479

Please sign in to comment.