fix render templates

This commit is contained in:
Cayo Puigdefabregas 2024-06-04 09:31:07 +02:00
parent 10a2d680af
commit fda78d7358
7 changed files with 56 additions and 53 deletions

File diff suppressed because one or more lines are too long

View File

@ -71,7 +71,7 @@ def generate_keys():
def gen_did_document(did, keys): def gen_did_document(did, keys):
if did[:8] != "did:web:": if did[:8] != "did:web:":
return "", "" return "", ""
document = did_document_tmpl.copy() document = json.loads(did_document_tmpl)
webdid_owner = did+"#owner" webdid_owner = did+"#owner"
webdid_revocation = did+"#revocation" webdid_revocation = did+"#revocation"
document["id"] = did document["id"] = did

View File

@ -11,7 +11,7 @@ from pyvckit.sign import sign_proof
def sign(credential, key, issuer_did): def sign(credential, key, issuer_did):
document = json.loads(credential) document = json.loads(credential)
_did = issuer_did + "#" + issuer_did.split("did:key:")[1] _did = issuer_did + "#" + issuer_did.split("did:key:")[1]
proof = proof_tmpl.copy() proof = json.loads(proof_tmpl)
proof['verificationMethod'] = _did proof['verificationMethod'] = _did
proof['created'] = now() proof['created'] = now()
@ -31,7 +31,7 @@ def main():
did = generate_did(key) did = generate_did(key)
signing_key = get_signing_key(key) signing_key = get_signing_key(key)
credential = credential_tmpl.copy() credential = json.loads(credential_tmpl)
credential["issuer"] = did credential["issuer"] = did
credential["issuanceDate"] = now() credential["issuanceDate"] = now()
cred = json.dumps(credential) cred = json.dumps(credential)

View File

@ -8,12 +8,12 @@ from pyvckit.sign import sign_proof
def sign_vp(signing_key, holder_did, vc): def sign_vp(signing_key, holder_did, vc):
presentation = presentation_tmpl.copy() presentation = json.loads(presentation_tmpl)
presentation["verifiableCredential"].append(json.loads(vc)) presentation["verifiableCredential"].append(json.loads(vc))
presentation["holder"] = holder_did presentation["holder"] = holder_did
_did = holder_did + "#" + holder_did.split("did:key:")[1] _did = holder_did + "#" + holder_did.split("did:key:")[1]
proof = proof_tmpl.copy() proof = json.loads(proof_tmpl)
proof['verificationMethod'] = _did proof['verificationMethod'] = _did
proof['created'] = now() proof['created'] = now()

View File

@ -1,35 +1,37 @@
# templates # templates
credential_tmpl = { credential_tmpl = """{
"@context": "https://www.w3.org/2018/credentials/v1", "@context": "https://www.w3.org/2018/credentials/v1",
"id": "http://example.org/credentials/3731", "id": "http://example.org/credentials/3731",
"type": ["VerifiableCredential"], "type": [
"VerifiableCredential"
],
"credentialSubject": { "credentialSubject": {
"id": "did:key:z6MkgGXSJoacuuNdwU1rGfPpFH72GACnzykKTxzCCTZs6Z2M", "id": "did:key:z6MkgGXSJoacuuNdwU1rGfPpFH72GACnzykKTxzCCTZs6Z2M"
}, },
"issuer": None, "issuer": "",
"issuanceDate": None "issuanceDate": ""
} }"""
proof_tmpl = { proof_tmpl = """{
'@context':'https://w3id.org/security/v2', "@context":"https://w3id.org/security/v2",
'type': 'Ed25519Signature2018', "type": "Ed25519Signature2018",
'proofPurpose': 'assertionMethod', "proofPurpose": "assertionMethod",
'verificationMethod': None, "verificationMethod": "",
'created': None "created": ""
} }"""
presentation_tmpl = { presentation_tmpl = """{
"@context": ["https://www.w3.org/2018/credentials/v1"], "@context": ["https://www.w3.org/2018/credentials/v1"],
"id": "http://example.org/presentations/3731", "id": "http://example.org/presentations/3731",
"type": ["VerifiablePresentation"], "type": ["VerifiablePresentation"],
"holder": "", "holder": "",
"verifiableCredential": [] "verifiableCredential": []
} }"""
did_document_tmpl = { did_document_tmpl = """{
"@context": [ "@context": [
"https://www.w3.org/ns/did/v1", "https://www.w3.org/ns/did/v1",
{ {
@ -64,4 +66,4 @@ did_document_tmpl = {
"serviceEndpoint": "data:application/octet-stream;base64,eJyzMmAAAwADKABr" "serviceEndpoint": "data:application/octet-stream;base64,eJyzMmAAAwADKABr"
} }
] ]
} }"""

View File

@ -9,7 +9,7 @@ test_requires = [
setup( setup(
name="pyvckit", name="pyvckit",
version="0.0.6", version="0.0.7",
packages=['pyvckit'], packages=['pyvckit'],
install_requires=[ install_requires=[
"jsonref", "jsonref",

View File

@ -147,6 +147,36 @@ def test_verifiable_credential():
assert verified assert verified
def test_verifiable_presentation():
key = generate_keys()
did = generate_did(key)
signing_key = get_signing_key(key)
credential = {
"@context": "https://www.w3.org/2018/credentials/v1",
"id": "http://example.org/credentials/3732",
"type": ["VerifiableCredential"],
"credentialSubject": {
"id": "did:key:z6MkgGXSJoacuuNdwU1rGfPpFH72GACnzykKTxzCCTZs6Z2M",
},
"issuer": did,
"issuanceDate": now()
}
cred = json.dumps(credential)
vc = sign(cred, signing_key, did)
vc_json = json.dumps(vc)
assert verify_vc(json.dumps(vc))
holder_key = generate_keys()
holder_did = generate_did(holder_key)
holder_signing_key = get_signing_key(holder_key)
vp = sign_vp(holder_signing_key, holder_did, vc_json)
verified = verify_vp(json.dumps(vp))
assert verified
def test_verifiable_credential_fail(): def test_verifiable_credential_fail():
key = generate_keys() key = generate_keys()
did = generate_did(key) did = generate_did(key)
@ -171,35 +201,6 @@ def test_verifiable_credential_fail():
assert not verified assert not verified
def test_verifiable_presentation():
key = generate_keys()
did = generate_did(key)
signing_key = get_signing_key(key)
credential = {
"@context": "https://www.w3.org/2018/credentials/v1",
"id": "http://example.org/credentials/3731",
"type": ["VerifiableCredential"],
"credentialSubject": {
"id": "did:key:z6MkgGXSJoacuuNdwU1rGfPpFH72GACnzykKTxzCCTZs6Z2M",
},
"issuer": did,
"issuanceDate": now()
}
cred = json.dumps(credential)
vc = sign(cred, signing_key, did)
vc_json = json.dumps(vc)
holder_key = generate_keys()
holder_did = generate_did(holder_key)
holder_signing_key = get_signing_key(holder_key)
vp = sign_vp(holder_signing_key, holder_did, vc_json)
verified = verify_vp(json.dumps(vp))
assert verified
def test_verifiable_presentation_fail1(): def test_verifiable_presentation_fail1():
key = generate_keys() key = generate_keys()
did = generate_did(key) did = generate_did(key)