Skip to content

Commit

Permalink
Fixes in Python and Node wrappers
Browse files Browse the repository at this point in the history
Signed-off-by: artem.ivanov <[email protected]>
  • Loading branch information
Artemkaaas committed Dec 2, 2023
1 parent 7fd4987 commit 4e8113e
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 96 deletions.
24 changes: 24 additions & 0 deletions wrappers/javascript/packages/anoncreds-nodejs/tests/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,10 @@ describe('API W3C', () => {
expect('mock:uri').toEqual(legacyCredential.schemaId)
expect('mock:uri').toEqual(legacyCredential.credentialDefinitionId)

const legacyCredentialFrom = Credential.fromW3C({ credential })
expect('mock:uri').toEqual(legacyCredentialFrom.schemaId)
expect('mock:uri').toEqual(legacyCredentialFrom.credentialDefinitionId)

const w3cCredential = W3CCredential.fromLegacy({ credential: legacyCredential, credentialDefinition })
expect('mock:uri').toEqual(w3cCredential.schemaId)
expect('mock:uri').toEqual(w3cCredential.credentialDefinitionId)
Expand All @@ -651,6 +655,26 @@ describe('API W3C', () => {
expect('mock:uri').toEqual(convertedW3CCredential.schemaId)
expect('mock:uri').toEqual(convertedW3CCredential.credentialDefinitionId)

convertedW3CCredential.addNonAnonCredsIntegrityProof({
type: 'Ed25519Signature2020',
created: '2021-11-13T18:19:39Z',
verificationMethod: 'did:sov:3avoBCqDMFHFaKUHug9s8W#key-1',
proofPurpose: 'assertionMethod',
proofValue: 'z58DAdFfa9SkqZMVPxAQpic7ndSayn1PzZs6ZjWp1CktyGesjuTSwRdoWhAfGFCF5bppETSTojQCrfFPP2oumHKtz'
})

const id = 'http://example.com/credentials/3732'
const subjectId = 'did:example:ebfeb1f712ebc6f1c276e12ec21'

convertedW3CCredential.setId(id)
convertedW3CCredential.setSubjectId(subjectId)
convertedW3CCredential.addContext('https://www.w3.org/2018/credentials/examples/v1')
convertedW3CCredential.addType('UniversityDegreeCredential')
const convertedW3CCredentialJson = convertedW3CCredential.toJson()

expect(id).toEqual(convertedW3CCredentialJson.id)
expect(subjectId).toEqual(convertedW3CCredentialJson.credentialSubject.id)

const credentialReceived = credential.process({
credentialDefinition,
credentialRequestMetadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export type CredentialToW3COptions = {
credentialDefinition: CredentialDefinition | JsonObject
}

export type CredentialFromW3COptions = {
credential: W3CCredential
}

export class Credential extends AnoncredsObject {
public static create(options: CreateCredentialOptions) {
let credential
Expand Down Expand Up @@ -170,7 +174,7 @@ export class Credential extends AnoncredsObject {
return credential
}

public static fromW3C(credential: W3CCredential) {
return new Credential(anoncreds.credentialFromW3C({ objectHandle: credential.handle }).handle)
public static fromW3C(options: CredentialFromW3COptions) {
return new Credential(anoncreds.credentialFromW3C({ objectHandle: options.credential.handle }).handle)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,48 +179,53 @@ export class W3CCredential extends AnoncredsObject {
return credential
}

public addNonAnonCredsIntegrityProof(proof: string): W3CCredential {
return new W3CCredential(
anoncreds.w3cCredentialAddNonAnonCredsIntegrityProof({
objectHandle: this.handle,
proof
}).handle
)
public addNonAnonCredsIntegrityProof(proof: JsonObject) {
const credential = anoncreds.w3cCredentialAddNonAnonCredsIntegrityProof({
objectHandle: this.handle,
proof: JSON.stringify(proof)
})

this.handle.clear()
this.handle = credential
}

public setId(id: string): W3CCredential {
return new W3CCredential(
anoncreds.w3cCredentialSetId({
objectHandle: this.handle,
id
}).handle
)
public setId(id: string) {
const credential = anoncreds.w3cCredentialSetId({
objectHandle: this.handle,
id
})

this.handle.clear()
this.handle = credential
}

public setSubjectId(id: string): W3CCredential {
return new W3CCredential(
anoncreds.w3cCredentialSetSubjectId({
objectHandle: this.handle,
id
}).handle
)
public setSubjectId(id: string) {
const credential = anoncreds.w3cCredentialSetSubjectId({
objectHandle: this.handle,
id
})

this.handle.clear()
this.handle = credential
}

public addContext(context: string): W3CCredential {
return new W3CCredential(
anoncreds.w3cCredentialAddContext({
objectHandle: this.handle,
context
}).handle
)
public addContext(context: string) {
const credential = anoncreds.w3cCredentialAddContext({
objectHandle: this.handle,
context
})

this.handle.clear()
this.handle = credential
}

public addType(type_: string): W3CCredential {
return new W3CCredential(
anoncreds.w3cCredentialAddType({
objectHandle: this.handle,
type_
}).handle
)
public addType(type_: string) {
const credential = anoncreds.w3cCredentialAddType({
objectHandle: this.handle,
type_
})

this.handle.clear()
this.handle = credential
}
}
4 changes: 2 additions & 2 deletions wrappers/python/anoncreds/bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1093,13 +1093,13 @@ def w3c_credential_add_non_anoncreds_integrity_proof(
return result


def w3c_set_credential_id(
def w3c_credential_set_id(
cred: ObjectHandle,
id: str,
) -> ObjectHandle:
result = ObjectHandle()
do_call(
"anoncreds_w3c_set_credential_id",
"anoncreds_w3c_credential_set_id",
cred,
encode_str(id),
byref(result),
Expand Down
103 changes: 48 additions & 55 deletions wrappers/python/anoncreds/types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from typing import Mapping, Optional, Sequence, Tuple, Union

from . import bindings
Expand Down Expand Up @@ -363,61 +364,6 @@ def from_w3c(cls, cred: "W3CCredential") -> "Credential":
)
)

def add_non_anoncreds_integrity_proof(
self,
proof: str
) -> "W3CCredential":
return W3CCredential(
bindings.w3c_credential_add_non_anoncreds_integrity_proof(
self.handle,
proof,
)
)

def set_credential_id(
self,
id: str
) -> "W3CCredential":
return W3CCredential(
bindings.w3c_credential_set_credential_id(
self.handle,
id,
)
)

def set_subject_id(
self,
id: str
) -> "W3CCredential":
return W3CCredential(
bindings.w3c_credential_set_subject_id(
self.handle,
id,
)
)

def add_context(
self,
context: str
) -> "W3CCredential":
return W3CCredential(
bindings.w3c_credential_add_context(
self.handle,
context,
)
)

def add_type(
self,
type: str
) -> "W3CCredential":
return W3CCredential(
bindings.w3c_credential_add_type(
self.handle,
type,
)
)


class W3CCredential(bindings.AnoncredsObject):
GET_ATTR = "anoncreds_w3c_credential_get_attribute"
Expand Down Expand Up @@ -529,6 +475,53 @@ def to_legacy(
def from_legacy(cls, cred: "Credential", cred_def: Union[str, CredentialDefinition]) -> "W3CCredential":
return cred.to_w3c(cred_def)

def add_non_anoncreds_integrity_proof(
self,
proof: Union[str, dict]
) -> "W3CCredential":
if isinstance(proof, dict):
proof = json.dumps(proof)
self.handle = bindings.w3c_credential_add_non_anoncreds_integrity_proof(
self.handle,
proof,
)

def set_id(
self,
id: str
) -> "W3CCredential":
self.handle = bindings.w3c_credential_set_id(
self.handle,
id,
)

def set_subject_id(
self,
id: str
) -> "W3CCredential":
self.handle = bindings.w3c_credential_set_subject_id(
self.handle,
id,
)

def add_context(
self,
context: str
) -> "W3CCredential":
self.handle = bindings.w3c_credential_add_context(
self.handle,
context,
)

def add_type(
self,
type: str
) -> "W3CCredential":
self.handle = bindings.w3c_credential_add_type(
self.handle,
type,
)


class PresentationRequest(bindings.AnoncredsObject):
@classmethod
Expand Down
28 changes: 26 additions & 2 deletions wrappers/python/demo/w3c_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,37 @@
print(recv_cred.to_json())

legacy_cred = recv_cred.to_legacy()
print("Legacy Credential")
print("Legacy Credential `to_legacy`")
print(legacy_cred.to_json())

legacy_cred = Credential.from_w3c(recv_cred)
print("Legacy Credential `from_w3c`")
print(legacy_cred.to_json())

w3c_cred = legacy_cred.to_w3c(cred_def_pub)
print("W3C converted Credential")
print("W3C converted Credential `to_w3c`")
print(w3c_cred.to_json())

w3c_cred_restored = W3CCredential.from_legacy(legacy_cred, cred_def_pub)
print("W3C restored Credential `from_legacy`")
print(w3c_cred_restored.to_json())


w3c_cred_restored.add_non_anoncreds_integrity_proof({
"type": "Ed25519Signature2020",
"created": "2021-11-13T18:19:39Z",
"verificationMethod": "did:sov:3avoBCqDMFHFaKUHug9s8W#key-1",
"proofPurpose": "assertionMethod",
"proofValue": "z58DAdFfa9SkqZMVPxAQpic7ndSayn1PzZs6ZjWp1CktyGesjuTSwRdoWhAfGFCF5bppETSTojQCrfFPP2oumHKtz"
})
w3c_cred_restored.set_id("http://example.com/credentials/3732")
w3c_cred_restored.set_subject_id("did:example:ebfeb1f712ebc6f1c276e12ec21")
w3c_cred_restored.add_context("https://www.w3.org/2018/credentials/examples/v1")
w3c_cred_restored.add_type("UniversityDegreeCredential")

print("W3C extended Credential")
print(w3c_cred_restored.to_json())

time_after_creating_cred = time_create_rev_status_list + 1
issued_rev_status_list = revocation_status_list.update(
cred_def_pub,
Expand Down

0 comments on commit 4e8113e

Please sign in to comment.