2024-05-24 10:38:48 +00:00
|
|
|
import json
|
|
|
|
import multicodec
|
|
|
|
import multiformats
|
|
|
|
import nacl.encoding
|
|
|
|
|
2024-05-31 18:15:14 +00:00
|
|
|
from pyvckit.did import generate_keys, generate_did, get_signing_key, gen_did_document
|
|
|
|
from pyvckit.sign_vc import sign
|
|
|
|
from pyvckit.sign_vp import sign_vp
|
|
|
|
from pyvckit.verify import verify_vc
|
|
|
|
from pyvckit.verify_vp import verify_vp
|
|
|
|
from pyvckit.utils import now
|
2024-05-24 10:38:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_generated_did_key():
|
|
|
|
key = generate_keys()
|
2024-05-27 09:41:33 +00:00
|
|
|
key_d = json.loads(key)
|
2024-05-24 10:38:48 +00:00
|
|
|
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')
|
2024-05-27 09:41:33 +00:00
|
|
|
k_x = key_d.get('x', '')
|
2024-05-24 10:38:48 +00:00
|
|
|
missing_padding = len(k_x) % 4
|
|
|
|
if missing_padding:
|
|
|
|
k_x += '=' * (4 - missing_padding)
|
|
|
|
|
2024-05-27 09:41:33 +00:00
|
|
|
assert key_d.get('kty') == 'OKP'
|
|
|
|
assert key_d.get('crv') == 'Ed25519'
|
|
|
|
assert key_d.get('kid') == 'Generated'
|
2024-05-24 10:38:48 +00:00
|
|
|
assert k_x == x
|
2024-05-27 09:41:33 +00:00
|
|
|
assert key_d.get('d') is not None
|
2024-05-30 09:35:25 +00:00
|
|
|
assert did.split(":")[:-1] == ['did', 'key']
|
|
|
|
|
|
|
|
|
|
|
|
def test_generated_did_web():
|
|
|
|
key = generate_keys()
|
|
|
|
key_d = json.loads(key)
|
|
|
|
url = "https://localhost/did-registry"
|
|
|
|
did = generate_did(key, url)
|
|
|
|
_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_d.get('x', '')
|
|
|
|
missing_padding = len(k_x) % 4
|
|
|
|
if missing_padding:
|
|
|
|
k_x += '=' * (4 - missing_padding)
|
|
|
|
|
|
|
|
assert key_d.get('kty') == 'OKP'
|
|
|
|
assert key_d.get('crv') == 'Ed25519'
|
|
|
|
assert key_d.get('kid') == 'Generated'
|
|
|
|
assert k_x == x
|
|
|
|
assert key_d.get('d') is not None
|
|
|
|
assert did.split(":")[:-1] == ['did', 'web', 'localhost', 'did-registry']
|
|
|
|
|
|
|
|
|
|
|
|
def test_generated_did_document():
|
|
|
|
key = generate_keys()
|
|
|
|
key_d = json.loads(key)
|
|
|
|
url = "https://localhost/did-registry"
|
|
|
|
did = generate_did(key, url)
|
|
|
|
definitive_url, document = gen_did_document(did, key_d)
|
|
|
|
pubkey = did.split(":")[-1]
|
|
|
|
doc_id = json.loads(document)["id"]
|
|
|
|
assert doc_id == did
|
|
|
|
assert definitive_url == f"{url}/{pubkey}/did.json"
|
2024-05-24 10:38:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_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 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
|
|
|
|
|
|
|
|
|
2024-05-28 08:17:54 +00:00
|
|
|
def test_verifiable_credential_fail():
|
|
|
|
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["id"] = "bar"
|
|
|
|
verified = verify_vc(json.dumps(vc))
|
|
|
|
assert not verified
|
|
|
|
|
|
|
|
|
2024-05-24 10:38:48 +00:00
|
|
|
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-27 09:41:33 +00:00
|
|
|
verified = verify_vp(json.dumps(vp))
|
2024-05-24 10:38:48 +00:00
|
|
|
assert verified
|
|
|
|
|
2024-05-28 08:17:54 +00:00
|
|
|
|
|
|
|
def test_verifiable_presentation_fail1():
|
|
|
|
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)
|
|
|
|
vp["verifiableCredential"][0]["id"] = "bar"
|
|
|
|
verified = verify_vp(json.dumps(vp))
|
|
|
|
assert not verified
|
|
|
|
|
|
|
|
|
|
|
|
def test_verifiable_presentation_fail2():
|
|
|
|
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)
|
|
|
|
vp["id"] = "http://example.org/presentations/3732"
|
|
|
|
verified = verify_vp(json.dumps(vp))
|
|
|
|
assert not verified
|
|
|
|
|