Skip to content

Commit

Permalink
Python: Added classes for W3CCredentialOffer and W3CCredentialRequest
Browse files Browse the repository at this point in the history
Signed-off-by: artem.ivanov <[email protected]>
  • Loading branch information
Artemkaaas committed Dec 4, 2023
1 parent 6d64da4 commit 3004b69
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 8 deletions.
2 changes: 2 additions & 0 deletions wrappers/python/anoncreds/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
RevocationStatusList,
RevocationRegistryDefinition,
RevocationRegistryDefinitionPrivate,
W3CCredentialOffer,
W3CCredentialRequest,
W3CCredential,
W3CPresentation,
)
Expand Down
37 changes: 37 additions & 0 deletions wrappers/python/anoncreds/bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,20 @@ def create_credential_offer(
return cred_offer


def create_w3c_credential_offer(
schema_id: str, cred_def_id: str, key_proof: ObjectHandle
) -> ObjectHandle:
cred_offer = ObjectHandle()
do_call(
"anoncreds_create_w3c_credential_offer",
encode_str(schema_id),
encode_str(cred_def_id),
key_proof,
byref(cred_offer),
)
return cred_offer


def create_credential_request(
entropy: Optional[str],
prover_did: Optional[str],
Expand All @@ -732,6 +746,29 @@ def create_credential_request(
return (cred_req, cred_req_metadata)


def create_w3c_credential_request(
entropy: Optional[str],
prover_did: Optional[str],
cred_def: ObjectHandle,
link_secret: str,
link_secret_id: str,
cred_offer: ObjectHandle,
) -> Tuple[ObjectHandle, ObjectHandle]:
cred_req, cred_req_metadata = ObjectHandle(), ObjectHandle()
do_call(
"anoncreds_create_w3c_credential_request",
encode_str(entropy),
encode_str(prover_did),
cred_def,
encode_str(link_secret),
encode_str(link_secret_id),
cred_offer,
byref(cred_req),
byref(cred_req_metadata),
)
return (cred_req, cred_req_metadata)


def create_link_secret() -> str:
result = StrBuffer()
do_call(
Expand Down
64 changes: 60 additions & 4 deletions wrappers/python/anoncreds/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,27 @@ def load(cls, value: Union[dict, str, bytes, memoryview]) -> "CredentialOffer":
)


class W3CCredentialOffer(bindings.AnoncredsObject):
@classmethod
def create(
cls,
schema_id: str,
cred_def_id: str,
key_proof: Union[str, KeyCorrectnessProof],
) -> "W3CCredentialOffer":
if not isinstance(key_proof, bindings.AnoncredsObject):
key_proof = KeyCorrectnessProof.load(key_proof)
return W3CCredentialOffer(
bindings.create_w3c_credential_offer(schema_id, cred_def_id, key_proof.handle)
)

@classmethod
def load(cls, value: Union[dict, str, bytes, memoryview]) -> "W3CCredentialOffer":
return W3CCredentialOffer(
bindings._object_from_json("anoncreds_credential_offer_from_json", value)
)


class CredentialRequest(bindings.AnoncredsObject):
@classmethod
def create(
Expand Down Expand Up @@ -137,6 +158,41 @@ def load(cls, value: Union[dict, str, bytes, memoryview]) -> "CredentialRequest"
)



class W3CCredentialRequest(bindings.AnoncredsObject):
@classmethod
def create(
cls,
entropy: Optional[str],
prover_did: Optional[str],
cred_def: Union[str, CredentialDefinition],
link_secret: str,
link_secret_id: str,
cred_offer: Union[str, W3CCredentialOffer],
) -> Tuple["W3CCredentialRequest", "CredentialRequestMetadata"]:
if not isinstance(cred_def, bindings.AnoncredsObject):
cred_def = CredentialDefinition.load(cred_def)
if not isinstance(cred_offer, bindings.AnoncredsObject):
cred_offer = W3CCredentialOffer.load(cred_offer)
cred_def_handle, cred_def_metadata = bindings.create_w3c_credential_request(
entropy,
prover_did,
cred_def.handle,
link_secret,
link_secret_id,
cred_offer.handle,
)
return W3CCredentialRequest(cred_def_handle), CredentialRequestMetadata(
cred_def_metadata
)

@classmethod
def load(cls, value: Union[dict, str, bytes, memoryview]) -> "W3CCredentialRequest":
return W3CCredentialRequest(
bindings._object_from_json("anoncreds_credential_request_from_json", value)
)


class CredentialRequestMetadata(bindings.AnoncredsObject):
@classmethod
def load(
Expand Down Expand Up @@ -373,8 +429,8 @@ def create(
cls,
cred_def: Union[str, CredentialDefinition],
cred_def_private: Union[str, CredentialDefinitionPrivate],
cred_offer: Union[str, CredentialOffer],
cred_request: Union[str, CredentialRequest],
cred_offer: Union[str, W3CCredentialOffer],
cred_request: Union[str, W3CCredentialRequest],
attr_raw_values: Mapping[str, str],
revocation_config: Optional["CredentialRevocationConfig"] = None,
encoding: Optional[str] = None,
Expand All @@ -384,9 +440,9 @@ def create(
if not isinstance(cred_def_private, bindings.AnoncredsObject):
cred_def_private = CredentialDefinitionPrivate.load(cred_def_private)
if not isinstance(cred_offer, bindings.AnoncredsObject):
cred_offer = CredentialOffer.load(cred_offer)
cred_offer = W3CCredentialOffer.load(cred_offer)
if not isinstance(cred_request, bindings.AnoncredsObject):
cred_request = CredentialRequest.load(cred_request)
cred_request = W3CCredentialRequest.load(cred_request)
cred = bindings.create_w3c_credential(
cred_def.handle,
cred_def_private.handle,
Expand Down
8 changes: 4 additions & 4 deletions wrappers/python/demo/w3c_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
Credential,
W3CCredential,
CredentialDefinition,
CredentialOffer,
CredentialRequest,
W3CCredentialOffer,
W3CCredentialRequest,
CredentialRevocationConfig,
CredentialRevocationState,
PresentationRequest,
Expand Down Expand Up @@ -50,9 +50,9 @@
link_secret = create_link_secret()
link_secret_id = "default"

cred_offer = CredentialOffer.create(schema_id, cred_def_id, cred_def_correctness)
cred_offer = W3CCredentialOffer.create(schema_id, cred_def_id, cred_def_correctness)

cred_request, cred_request_metadata = CredentialRequest.create(
cred_request, cred_request_metadata = W3CCredentialRequest.create(
entropy, None, cred_def_pub, link_secret, link_secret_id, cred_offer
)

Expand Down

0 comments on commit 3004b69

Please sign in to comment.