simplify and clean code

This commit is contained in:
Cayo Puigdefabregas 2024-06-06 14:12:09 +02:00
parent 1ec9b9ce9e
commit 17b0bb8536
7 changed files with 76 additions and 92 deletions

View File

@ -1,7 +1,11 @@
import json
import hashlib import hashlib
import nacl.signing import nacl.signing
import nacl.encoding import nacl.encoding
from pyld import jsonld from pyld import jsonld
from pyvckit.utils import now
from pyvckit.did import get_signing_key
from pyvckit.templates import proof_tmpl
from pyvckit.document_loader import requests_document_loader from pyvckit.document_loader import requests_document_loader
@ -68,3 +72,16 @@ def sign_proof(document, proof, key):
return proof return proof
def sign(credential, key, issuer_did):
signing_key = get_signing_key(key)
document = json.loads(credential)
_did = issuer_did + "#" + issuer_did.split(":")[-1]
proof = json.loads(proof_tmpl)
proof['verificationMethod'] = _did
proof['created'] = now()
sign_proof(document, proof, signing_key)
del proof['@context']
document['proof'] = proof
return document

View File

@ -1,25 +1,13 @@
import json import json
import argparse import argparse
from pyvckit.utils import now from pyvckit.utils import now
from pyvckit.did import generate_did, get_signing_key, key_read from pyvckit.did import generate_did, key_read
from pyvckit.templates import credential_tmpl, proof_tmpl from pyvckit.templates import credential_tmpl
from pyvckit.sign import sign_proof from pyvckit.sign import sign
# source: https://github.com/mmlab-aueb/PyEd25519Signature2018/blob/master/signer.py # source: https://github.com/mmlab-aueb/PyEd25519Signature2018/blob/master/signer.py
def sign(credential, key, issuer_did):
document = json.loads(credential)
_did = issuer_did + "#" + issuer_did.split(":")[-1]
proof = json.loads(proof_tmpl)
proof['verificationMethod'] = _did
proof['created'] = now()
sign_proof(document, proof, key)
del proof['@context']
document['proof'] = proof
return document
def main(): def main():
parser=argparse.ArgumentParser(description='Generates a new credential') parser=argparse.ArgumentParser(description='Generates a new credential')
@ -29,14 +17,13 @@ def main():
if args.key_path: if args.key_path:
key = key_read(args.key_path) key = key_read(args.key_path)
did = generate_did(key) did = generate_did(key)
signing_key = get_signing_key(key)
credential = json.loads(credential_tmpl) credential = json.loads(credential_tmpl)
credential["issuer"]["id"] = did credential["issuer"]["id"] = did
credential["issuanceDate"] = now() credential["issuanceDate"] = now()
cred = json.dumps(credential) cred = json.dumps(credential)
vc = sign(cred, signing_key, did) vc = sign(cred, key, did)
print(json.dumps(vc, separators=(',', ':'))) print(json.dumps(vc, separators=(',', ':')))

View File

@ -1,26 +1,16 @@
import json import json
import argparse import argparse
from pyvckit.utils import now from pyvckit.templates import presentation_tmpl
from pyvckit.templates import presentation_tmpl, proof_tmpl from pyvckit.did import key_read, generate_did
from pyvckit.did import key_read, generate_did, get_signing_key from pyvckit.sign import sign
from pyvckit.sign import sign_proof
def sign_vp(signing_key, holder_did, vc): def get_presentation(vc, holder_did):
presentation = json.loads(presentation_tmpl) 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
return json.dumps(presentation)
_did = holder_did + "#" + holder_did.split(":")[-1]
proof = json.loads(proof_tmpl)
proof['verificationMethod'] = _did
proof['created'] = now()
sign_proof(presentation, proof, signing_key)
del proof['@context']
presentation['proof'] = proof
return presentation
def main(): def main():
@ -39,8 +29,8 @@ def main():
key = key_read(args.key_path) key = key_read(args.key_path)
did = generate_did(key) did = generate_did(key)
signing_key = get_signing_key(key) unsigned_vp = get_presentation(vc, did)
vp = sign_vp(signing_key, did, vc) vp = sign(unsigned_vp, key, did)
print(json.dumps(vp, separators=(',', ':'))) print(json.dumps(vp, separators=(',', ':')))
return return

View File

@ -108,3 +108,16 @@ def verify_vc(credential):
return True return True
def verify_vp(presentation):
vp = json.loads(presentation)
if not verify_vc(presentation):
return False
for vc in vp['verifiableCredential']:
vc_str = json.dumps(vc)
if not verify_vc(vc_str):
return False
return True

View File

@ -1,6 +1,6 @@
import json
import argparse import argparse
from pyvckit.verify import verify_vc
from pyvckit.verify import verify_vp
def get_presentation(path_presentation): def get_presentation(path_presentation):
@ -9,20 +9,6 @@ def get_presentation(path_presentation):
return vc return vc
def verify_vp(presentation):
vp = json.loads(presentation)
if not verify_vc(presentation):
return False
for vc in vp['verifiableCredential']:
vc_str = json.dumps(vc)
if not verify_vc(vc_str):
return False
return True
def main(): def main():
parser=argparse.ArgumentParser(description='Verify a presentation') parser=argparse.ArgumentParser(description='Verify a presentation')
parser.add_argument("presentation_path") parser.add_argument("presentation_path")

View File

@ -3,11 +3,11 @@ import multicodec
import multiformats import multiformats
import nacl.encoding import nacl.encoding
from pyvckit.did import generate_keys, generate_did, get_signing_key, gen_did_document from pyvckit.did import generate_keys, generate_did, gen_did_document
from pyvckit.sign_vc import sign from pyvckit.sign import sign
from pyvckit.sign_vp import sign_vp from pyvckit.sign_vp import get_presentation
from pyvckit.verify import verify_vc from pyvckit.verify import verify_vc
from pyvckit.verify_vp import verify_vp from pyvckit.verify import verify_vp
from pyvckit.utils import now from pyvckit.utils import now
@ -71,7 +71,6 @@ def test_generated_did_document():
def test_credential(): def test_credential():
key = generate_keys() key = generate_keys()
did = generate_did(key) did = generate_did(key)
signing_key = get_signing_key(key)
credential = { credential = {
"@context": "https://www.w3.org/2018/credentials/v1", "@context": "https://www.w3.org/2018/credentials/v1",
@ -89,7 +88,7 @@ def test_credential():
cred = json.dumps(credential) cred = json.dumps(credential)
vc = sign(cred, signing_key, did) vc = sign(cred, key, did)
header = 'eyJhbGciOiJFZERTQSIsImNyaXQiOlsiYjY0Il0sImI2NCI6ZmFsc2V9' header = 'eyJhbGciOiJFZERTQSIsImNyaXQiOlsiYjY0Il0sImI2NCI6ZmFsc2V9'
assert vc.get('proof', {}).get('jws') is not None assert vc.get('proof', {}).get('jws') is not None
assert header in vc.get('proof', {}).get('jws') assert header in vc.get('proof', {}).get('jws')
@ -99,7 +98,6 @@ def test_credential():
def test_presentation(): def test_presentation():
key = generate_keys() key = generate_keys()
did = generate_did(key) did = generate_did(key)
signing_key = get_signing_key(key)
credential = { credential = {
"@context": "https://www.w3.org/2018/credentials/v1", "@context": "https://www.w3.org/2018/credentials/v1",
@ -117,13 +115,13 @@ def test_presentation():
cred = json.dumps(credential) cred = json.dumps(credential)
vc = sign(cred, signing_key, did) vc = sign(cred, key, did)
vc_json = json.dumps(vc) vc_json = json.dumps(vc)
holder_key = generate_keys() holder_key = generate_keys()
holder_did = generate_did(holder_key) holder_did = generate_did(holder_key)
holder_signing_key = get_signing_key(holder_key) unsigned_vp = get_presentation(vc_json, holder_did)
vp = sign_vp(holder_signing_key, holder_did, vc_json) vp = sign(unsigned_vp, holder_key, holder_did)
header = 'eyJhbGciOiJFZERTQSIsImNyaXQiOlsiYjY0Il0sImI2NCI6ZmFsc2V9' header = 'eyJhbGciOiJFZERTQSIsImNyaXQiOlsiYjY0Il0sImI2NCI6ZmFsc2V9'
assert vp.get('proof', {}).get('jws') is not None assert vp.get('proof', {}).get('jws') is not None
assert header in vp.get('proof', {}).get('jws') assert header in vp.get('proof', {}).get('jws')
@ -133,7 +131,6 @@ def test_presentation():
def test_verifiable_credential(): def test_verifiable_credential():
key = generate_keys() key = generate_keys()
did = generate_did(key) did = generate_did(key)
signing_key = get_signing_key(key)
credential = { credential = {
"@context": "https://www.w3.org/2018/credentials/v1", "@context": "https://www.w3.org/2018/credentials/v1",
@ -151,7 +148,7 @@ def test_verifiable_credential():
cred = json.dumps(credential) cred = json.dumps(credential)
vc = sign(cred, signing_key, did) vc = sign(cred, key, did)
verified = verify_vc(json.dumps(vc)) verified = verify_vc(json.dumps(vc))
assert verified assert verified
@ -159,7 +156,6 @@ def test_verifiable_credential():
def test_verifiable_presentation(): def test_verifiable_presentation():
key = generate_keys() key = generate_keys()
did = generate_did(key) did = generate_did(key)
signing_key = get_signing_key(key)
credential = { credential = {
"@context": "https://www.w3.org/2018/credentials/v1", "@context": "https://www.w3.org/2018/credentials/v1",
@ -177,14 +173,14 @@ def test_verifiable_presentation():
cred = json.dumps(credential) cred = json.dumps(credential)
vc = sign(cred, signing_key, did) vc = sign(cred, key, did)
vc_json = json.dumps(vc) vc_json = json.dumps(vc)
assert verify_vc(json.dumps(vc)) assert verify_vc(json.dumps(vc))
holder_key = generate_keys() holder_key = generate_keys()
holder_did = generate_did(holder_key) holder_did = generate_did(holder_key)
holder_signing_key = get_signing_key(holder_key) unsigned_vp = get_presentation(vc_json, holder_did)
vp = sign_vp(holder_signing_key, holder_did, vc_json) vp = sign(unsigned_vp, holder_key, holder_did)
verified = verify_vp(json.dumps(vp)) verified = verify_vp(json.dumps(vp))
assert verified assert verified
@ -192,7 +188,6 @@ def test_verifiable_presentation():
def test_verifiable_credential_fail(): def test_verifiable_credential_fail():
key = generate_keys() key = generate_keys()
did = generate_did(key) did = generate_did(key)
signing_key = get_signing_key(key)
credential = { credential = {
"@context": "https://www.w3.org/2018/credentials/v1", "@context": "https://www.w3.org/2018/credentials/v1",
@ -210,7 +205,7 @@ def test_verifiable_credential_fail():
cred = json.dumps(credential) cred = json.dumps(credential)
vc = sign(cred, signing_key, did) vc = sign(cred, key, did)
vc["id"] = "bar" vc["id"] = "bar"
verified = verify_vc(json.dumps(vc)) verified = verify_vc(json.dumps(vc))
assert not verified assert not verified
@ -219,7 +214,6 @@ def test_verifiable_credential_fail():
def test_verifiable_presentation_fail1(): def test_verifiable_presentation_fail1():
key = generate_keys() key = generate_keys()
did = generate_did(key) did = generate_did(key)
signing_key = get_signing_key(key)
credential = { credential = {
"@context": "https://www.w3.org/2018/credentials/v1", "@context": "https://www.w3.org/2018/credentials/v1",
@ -237,13 +231,13 @@ def test_verifiable_presentation_fail1():
cred = json.dumps(credential) cred = json.dumps(credential)
vc = sign(cred, signing_key, did) vc = sign(cred, key, did)
vc_json = json.dumps(vc) vc_json = json.dumps(vc)
holder_key = generate_keys() holder_key = generate_keys()
holder_did = generate_did(holder_key) holder_did = generate_did(holder_key)
holder_signing_key = get_signing_key(holder_key) unsigned_vp = get_presentation(vc_json, holder_did)
vp = sign_vp(holder_signing_key, holder_did, vc_json) vp = sign(unsigned_vp, holder_key, holder_did)
vp["verifiableCredential"][0]["id"] = "bar" vp["verifiableCredential"][0]["id"] = "bar"
verified = verify_vp(json.dumps(vp)) verified = verify_vp(json.dumps(vp))
assert not verified assert not verified
@ -252,7 +246,6 @@ def test_verifiable_presentation_fail1():
def test_verifiable_presentation_fail2(): def test_verifiable_presentation_fail2():
key = generate_keys() key = generate_keys()
did = generate_did(key) did = generate_did(key)
signing_key = get_signing_key(key)
credential = { credential = {
"@context": "https://www.w3.org/2018/credentials/v1", "@context": "https://www.w3.org/2018/credentials/v1",
@ -270,13 +263,13 @@ def test_verifiable_presentation_fail2():
cred = json.dumps(credential) cred = json.dumps(credential)
vc = sign(cred, signing_key, did) vc = sign(cred, key, did)
vc_json = json.dumps(vc) vc_json = json.dumps(vc)
holder_key = generate_keys() holder_key = generate_keys()
holder_did = generate_did(holder_key) holder_did = generate_did(holder_key)
holder_signing_key = get_signing_key(holder_key) unsigned_vp = get_presentation(vc_json, holder_did)
vp = sign_vp(holder_signing_key, holder_did, vc_json) vp = sign(unsigned_vp, holder_key, holder_did)
vp["id"] = "http://example.org/presentations/3732" vp["id"] = "http://example.org/presentations/3732"
verified = verify_vp(json.dumps(vp)) verified = verify_vp(json.dumps(vp))
assert not verified assert not verified

View File

@ -6,12 +6,13 @@ import didkit
# import nacl.encoding # import nacl.encoding
from ast import literal_eval from ast import literal_eval
from pyvckit.sign_vc import sign # from pyvckit.sign_vc import sign
from pyvckit.sign_vp import sign_vp from pyvckit.sign import sign
from pyvckit.sign_vp import get_presentation
from pyvckit.verify import verify_vc from pyvckit.verify import verify_vc
from pyvckit.verify_vp import verify_vp from pyvckit.verify import verify_vp
from pyvckit.utils import now from pyvckit.utils import now
from pyvckit.did import generate_keys, generate_did, get_signing_key from pyvckit.did import generate_keys, generate_did
def verify_credential(vc): def verify_credential(vc):
@ -96,7 +97,6 @@ def test_key_from_pyvckit():
def test_pyvckit_credential_validated_from_didkit(): def test_pyvckit_credential_validated_from_didkit():
key = generate_keys() key = generate_keys()
did = generate_did(key) did = generate_did(key)
signing_key = get_signing_key(key)
credential = { credential = {
"@context": "https://www.w3.org/2018/credentials/v1", "@context": "https://www.w3.org/2018/credentials/v1",
@ -113,7 +113,7 @@ def test_pyvckit_credential_validated_from_didkit():
cred = json.dumps(credential) cred = json.dumps(credential)
vc = sign(cred, signing_key, did) vc = sign(cred, key, did)
result = verify_credential(json.dumps(vc)) result = verify_credential(json.dumps(vc))
assert result == '{"checks":["proof"],"warnings":[],"errors":[]}' assert result == '{"checks":["proof"],"warnings":[],"errors":[]}'
@ -144,7 +144,6 @@ def test_didkit_credential_validated_from_pyvckit():
def test_pyvckit_presentation_validated_from_didkit(): def test_pyvckit_presentation_validated_from_didkit():
key = generate_keys() key = generate_keys()
did = generate_did(key) did = generate_did(key)
signing_key = get_signing_key(key)
credential = { credential = {
"@context": "https://www.w3.org/2018/credentials/v1", "@context": "https://www.w3.org/2018/credentials/v1",
@ -161,13 +160,13 @@ def test_pyvckit_presentation_validated_from_didkit():
cred = json.dumps(credential) cred = json.dumps(credential)
vc = sign(cred, signing_key, did) vc = sign(cred, key, did)
vc_json = json.dumps(vc) vc_json = json.dumps(vc)
holder_key = generate_keys() holder_key = generate_keys()
holder_did = generate_did(holder_key) holder_did = generate_did(holder_key)
holder_signing_key = get_signing_key(holder_key) unsigned_vp = get_presentation(vc_json, holder_did)
vp = sign_vp(holder_signing_key, holder_did, vc_json) vp = sign(unsigned_vp, holder_key, holder_did)
result = verify_presentation(json.dumps(vp)) result = verify_presentation(json.dumps(vp))
assert result assert result
@ -176,7 +175,6 @@ def test_pyvckit_presentation_validated_from_didkit():
def test_fail_pyvckit_presentation_validated_from_didkit(): def test_fail_pyvckit_presentation_validated_from_didkit():
key = generate_keys() key = generate_keys()
did = generate_did(key) did = generate_did(key)
signing_key = get_signing_key(key)
credential = { credential = {
"@context": "https://www.w3.org/2018/credentials/v1", "@context": "https://www.w3.org/2018/credentials/v1",
@ -193,13 +191,13 @@ def test_fail_pyvckit_presentation_validated_from_didkit():
cred = json.dumps(credential) cred = json.dumps(credential)
vc = sign(cred, signing_key, did) vc = sign(cred, key, did)
vc_json = json.dumps(vc) vc_json = json.dumps(vc)
holder_key = generate_keys() holder_key = generate_keys()
holder_did = generate_did(holder_key) holder_did = generate_did(holder_key)
holder_signing_key = get_signing_key(holder_key) unsigned_vp = get_presentation(vc_json, holder_did)
vp = sign_vp(holder_signing_key, holder_did, vc_json) vp = sign(unsigned_vp, holder_key, holder_did)
vp["verifiableCredential"][0]["id"] = "bar" vp["verifiableCredential"][0]["id"] = "bar"
vp_fail = json.dumps(vp) vp_fail = json.dumps(vp)