pyvckit/tests/test_certificate.py

142 lines
4.1 KiB
Python
Raw Normal View History

2024-05-24 10:38:48 +00:00
import json
import multicodec
import multiformats
import nacl.encoding
2024-05-24 11:39:50 +00:00
from did import generate_keys, generate_did, get_signing_key
2024-05-24 10:38:48 +00:00
from sign_vc import sign
from sign_vp import sign_vp
2024-05-24 20:29:14 +00:00
from verify import verify_vc
2024-05-24 10:38:48 +00:00
from verify_vp import verify_vp
2024-05-24 11:39:50 +00:00
from utils import now
2024-05-24 10:38:48 +00:00
def test_generated_did_key():
key = generate_keys()
did = generate_did(key)
_did = did.split("#")[0]
pub = _did.split(":")[-1]
mc = multiformats.multibase.decode(pub)
public_key_bytes = multicodec.remove_prefix(mc)
x = nacl.encoding.URLSafeBase64Encoder.encode(public_key_bytes).decode('utf-8')
k_x = key.get('x', '')
missing_padding = len(k_x) % 4
if missing_padding:
k_x += '=' * (4 - missing_padding)
assert key.get('kty') == 'OKP'
assert key.get('crv') == 'Ed25519'
assert key.get('kid') == 'Generated'
assert k_x == x
assert key.get('d') is not None
def test_credential():
2024-05-24 11:39:50 +00:00
# import pdb; pdb.set_trace()
2024-05-24 10:38:48 +00:00
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()
}
2024-05-24 11:39:50 +00:00
cred = json.dumps(credential)
vc = sign(cred, signing_key, did)
2024-05-24 10:38:48 +00:00
header = 'eyJhbGciOiJFZERTQSIsImNyaXQiOlsiYjY0Il0sImI2NCI6ZmFsc2V9'
assert vc.get('proof', {}).get('jws') is not None
assert header in vc.get('proof', {}).get('jws')
assert did in vc.get('proof', {}).get('verificationMethod')
def test_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()
}
2024-05-24 11:39:50 +00:00
cred = json.dumps(credential)
vc = sign(cred, signing_key, did)
2024-05-24 10:38:48 +00:00
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)
header = 'eyJhbGciOiJFZERTQSIsImNyaXQiOlsiYjY0Il0sImI2NCI6ZmFsc2V9'
assert vp.get('proof', {}).get('jws') is not None
assert header in vp.get('proof', {}).get('jws')
assert holder_did in vp.get('proof', {}).get('verificationMethod')
def test_verifiable_credential():
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()
}
2024-05-24 11:39:50 +00:00
cred = json.dumps(credential)
vc = sign(cred, signing_key, did)
2024-05-24 20:29:14 +00:00
verified = verify_vc(json.dumps(vc))
2024-05-24 10:38:48 +00:00
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/3731",
"type": ["VerifiableCredential"],
"credentialSubject": {
"id": "did:key:z6MkgGXSJoacuuNdwU1rGfPpFH72GACnzykKTxzCCTZs6Z2M",
},
"issuer": did,
"issuanceDate": now()
}
2024-05-24 11:39:50 +00:00
cred = json.dumps(credential)
vc = sign(cred, signing_key, did)
2024-05-24 10:38:48 +00:00
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)
2024-05-24 20:29:14 +00:00
verified = verify_vc(json.dumps(vp))
2024-05-24 10:38:48 +00:00
assert verified