2023-10-24 08:19:55 +00:00
|
|
|
import asyncio
|
|
|
|
import didkit
|
|
|
|
import json
|
|
|
|
from jinja2 import Environment, FileSystemLoader, select_autoescape
|
2023-11-10 05:45:12 +00:00
|
|
|
import idhub_ssikit
|
2023-10-24 08:19:55 +00:00
|
|
|
|
2023-11-22 13:09:08 +00:00
|
|
|
|
2023-12-08 14:49:53 +00:00
|
|
|
def issue_vc_test(vc_name):
|
2023-11-22 13:09:08 +00:00
|
|
|
jwk_issuer = didkit.generate_ed25519_key()
|
|
|
|
jwk_subject = didkit.generate_ed25519_key()
|
|
|
|
|
|
|
|
did_issuer = didkit.key_to_did("key", jwk_issuer)
|
|
|
|
did_subject = didkit.key_to_did("key", jwk_subject)
|
|
|
|
|
|
|
|
env = Environment(
|
|
|
|
loader=FileSystemLoader("vc_templates"),
|
|
|
|
autoescape=select_autoescape()
|
|
|
|
)
|
2023-12-12 15:57:37 +00:00
|
|
|
unsigned_vc_template = env.get_template(f"{vc_name}.json")
|
|
|
|
data_raw = open(f"vc_templates/{vc_name}--data.py").read()
|
|
|
|
data = eval(data_raw)
|
|
|
|
data["issuerDid"] = did_issuer
|
|
|
|
data["subjectDid"] = did_subject
|
2023-11-22 13:09:08 +00:00
|
|
|
signed_credential = idhub_ssikit.render_and_sign_credential(
|
|
|
|
unsigned_vc_template,
|
|
|
|
jwk_issuer,
|
|
|
|
data
|
|
|
|
)
|
2023-11-28 08:45:06 +00:00
|
|
|
print(signed_credential)
|
|
|
|
|
|
|
|
|
|
|
|
def issue_vc_test_2():
|
|
|
|
jwk_issuer = didkit.generate_ed25519_key()
|
|
|
|
jwk_subject = didkit.generate_ed25519_key()
|
|
|
|
|
|
|
|
did_issuer = didkit.key_to_did("key", jwk_issuer)
|
|
|
|
did_subject = didkit.key_to_did("key", jwk_subject)
|
2023-11-22 13:09:08 +00:00
|
|
|
|
2023-11-28 08:45:06 +00:00
|
|
|
env = Environment(
|
|
|
|
loader=FileSystemLoader("vc_templates"),
|
|
|
|
autoescape=select_autoescape()
|
|
|
|
)
|
|
|
|
unsigned_vc_template = env.get_template("membership-card.json")
|
|
|
|
data = {
|
|
|
|
"vc_id": "http://example.org/credentials/3731",
|
|
|
|
"issuer_did": did_issuer,
|
|
|
|
"subject_did": did_subject,
|
|
|
|
"issuance_date": "2020-08-19T21:41:50Z",
|
|
|
|
"validUntil": "2020-08-19T21:41:50Z",
|
|
|
|
"membershipType": "lareputa"
|
|
|
|
}
|
|
|
|
signed_credential = idhub_ssikit.render_and_sign_credential(
|
|
|
|
unsigned_vc_template,
|
|
|
|
jwk_issuer,
|
|
|
|
data
|
|
|
|
)
|
2023-11-22 13:09:08 +00:00
|
|
|
print(signed_credential)
|
2023-11-28 08:45:06 +00:00
|
|
|
verification_result = idhub_ssikit.verify_credential(signed_credential)
|
|
|
|
print(verification_result)
|
|
|
|
def replace(s, position, character):
|
|
|
|
return s[:position] + character + s[position+1:]
|
|
|
|
signed_credential = replace(signed_credential, 2843, "k")
|
|
|
|
verification_result = idhub_ssikit.verify_credential(signed_credential)
|
|
|
|
print(verification_result)
|
|
|
|
|
2023-11-22 13:09:08 +00:00
|
|
|
|
|
|
|
|
2023-11-28 08:45:06 +00:00
|
|
|
def issue_and_sign_vp_test():
|
2023-11-27 06:26:02 +00:00
|
|
|
"""
|
|
|
|
In this example execution two Verifiable Credentials associated with a single Holder are issued and then
|
|
|
|
combined into a single Verifiable Presentation.
|
|
|
|
The Verifiable Credentials are of two different models. The use-case is meant to mimic
|
|
|
|
- Holder being a physical person,
|
|
|
|
- Issuer A being "Pare Manel" foundation,
|
|
|
|
- Issuer B being "EXO" foundation,
|
|
|
|
- Verifier (not pictured) being "Som Connexio", which wants verifiable data of the Holder from both Issuers.
|
|
|
|
"""
|
2023-11-22 13:09:08 +00:00
|
|
|
jwk_issuer = didkit.generate_ed25519_key()
|
|
|
|
jwk_issuer2 = didkit.generate_ed25519_key()
|
|
|
|
jwk_subject = didkit.generate_ed25519_key()
|
|
|
|
|
|
|
|
did_issuer = didkit.key_to_did("key", jwk_issuer)
|
|
|
|
did_issuer2 = didkit.key_to_did("key", jwk_issuer2)
|
|
|
|
did_subject = didkit.key_to_did("key", jwk_subject)
|
|
|
|
print(did_issuer)
|
|
|
|
print(did_issuer2)
|
|
|
|
print(did_subject)
|
|
|
|
|
|
|
|
env = Environment(
|
|
|
|
loader=FileSystemLoader("vc_templates"),
|
|
|
|
autoescape=select_autoescape()
|
|
|
|
)
|
|
|
|
unsigned_vc_template = env.get_template("member.json")
|
|
|
|
data = {
|
|
|
|
"vc_id": "http://example.org/credentials/3731",
|
|
|
|
"issuer_did": did_issuer,
|
|
|
|
"subject_did": did_subject,
|
|
|
|
"issuance_date": "2020-08-19T21:41:50Z",
|
|
|
|
"subject_is_member_of": "Pangea"
|
|
|
|
}
|
|
|
|
signed_credential = idhub_ssikit.render_and_sign_credential(
|
|
|
|
unsigned_vc_template,
|
|
|
|
jwk_issuer,
|
|
|
|
data
|
|
|
|
)
|
|
|
|
data2 = data
|
|
|
|
data2["issuer_did"] = did_issuer2
|
|
|
|
signed_credential2 = idhub_ssikit.render_and_sign_credential(
|
|
|
|
unsigned_vc_template,
|
|
|
|
jwk_issuer2,
|
|
|
|
data2
|
|
|
|
)
|
|
|
|
signed_presentation = idhub_ssikit.issue_verifiable_presentation([signed_credential, signed_credential2], jwk_subject, did_subject)
|
2023-11-28 08:45:06 +00:00
|
|
|
print("##############--- SIGNED PRESENTATION ---##############")
|
2023-11-22 13:09:08 +00:00
|
|
|
print(signed_presentation)
|
2023-11-28 08:45:06 +00:00
|
|
|
print("##############--- ------------------- ---##############")
|
|
|
|
|
|
|
|
res = idhub_ssikit.verify_presentation(signed_presentation)
|
|
|
|
print(res)
|
|
|
|
|