2023-11-10 05:45:12 +00:00
|
|
|
import asyncio
|
|
|
|
import datetime
|
2024-01-03 15:53:06 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2023-11-10 05:45:12 +00:00
|
|
|
import didkit
|
|
|
|
import json
|
|
|
|
import jinja2
|
2023-11-22 13:09:08 +00:00
|
|
|
from jinja2 import Environment, FileSystemLoader, select_autoescape
|
2024-01-03 15:53:06 +00:00
|
|
|
from ast import literal_eval
|
2023-11-22 13:09:08 +00:00
|
|
|
|
2023-11-10 05:45:12 +00:00
|
|
|
|
|
|
|
def generate_did_controller_key():
|
|
|
|
return didkit.generate_ed25519_key()
|
|
|
|
|
|
|
|
|
2023-11-28 08:45:06 +00:00
|
|
|
def keydid_from_controller_key(key):
|
2023-11-10 05:45:12 +00:00
|
|
|
return didkit.key_to_did("key", key)
|
|
|
|
|
|
|
|
|
|
|
|
def generate_generic_vc_id():
|
|
|
|
# TODO agree on a system for Verifiable Credential IDs
|
|
|
|
return "https://pangea.org/credentials/42"
|
|
|
|
|
|
|
|
|
2024-01-03 15:53:06 +00:00
|
|
|
def render_and_sign_credential(unsigned_vc: dict, jwk_issuer):
|
2023-11-10 05:45:12 +00:00
|
|
|
"""
|
|
|
|
Populates a VC template with data for issuance, and signs the result with the provided key.
|
|
|
|
|
|
|
|
The `vc_data` parameter must at a minimum include:
|
|
|
|
* issuer_did
|
|
|
|
* subject_did
|
|
|
|
* vc_id
|
|
|
|
and must include whatever other fields are relevant for the vc_template to be instantiated.
|
|
|
|
|
|
|
|
The following field(s) will be auto-generated if not passed in `vc_data`:
|
|
|
|
* issuance_date (to `datetime.datetime.now()`)
|
|
|
|
"""
|
|
|
|
async def inner():
|
|
|
|
signed_vc = await didkit.issue_credential(
|
2024-01-03 15:53:06 +00:00
|
|
|
json.dumps(unsigned_vc),
|
2023-11-10 05:45:12 +00:00
|
|
|
'{"proofFormat": "ldp"}',
|
|
|
|
jwk_issuer
|
|
|
|
)
|
|
|
|
return signed_vc
|
|
|
|
|
2024-01-03 15:53:06 +00:00
|
|
|
# if vc_data.get("issuance_date") is None:
|
|
|
|
# vc_data["issuance_date"] = datetime.datetime.now().replace(microsecond=0).isoformat()
|
2023-11-10 05:45:12 +00:00
|
|
|
|
|
|
|
return asyncio.run(inner())
|
|
|
|
|
|
|
|
|
2023-11-28 08:45:06 +00:00
|
|
|
def sign_credential(unsigned_vc: str, jwk_issuer):
|
|
|
|
"""
|
|
|
|
Signs the unsigned credential with the provided key.
|
|
|
|
"""
|
|
|
|
async def inner():
|
|
|
|
signed_vc = await didkit.issue_credential(
|
|
|
|
unsigned_vc,
|
|
|
|
'{"proofFormat": "ldp"}',
|
|
|
|
jwk_issuer
|
|
|
|
)
|
|
|
|
return signed_vc
|
|
|
|
|
|
|
|
return asyncio.run(inner())
|
|
|
|
|
|
|
|
|
|
|
|
def verify_credential(vc):
|
2023-11-10 05:45:12 +00:00
|
|
|
"""
|
|
|
|
Returns a (bool, str) tuple indicating whether the credential is valid.
|
2024-01-03 15:53:06 +00:00
|
|
|
Checks performed:
|
|
|
|
* The credential is valid in signature and form, and
|
|
|
|
* The credential validates itself against its declared schema.
|
2023-11-10 05:45:12 +00:00
|
|
|
If the boolean is true, the credential is valid and the second argument can be ignored.
|
2024-01-03 15:53:06 +00:00
|
|
|
If it is false, the VC is invalid and the second argument contains a string (which is a valid JSON object) with further information.
|
2023-11-10 05:45:12 +00:00
|
|
|
"""
|
|
|
|
async def inner():
|
2024-01-03 15:53:06 +00:00
|
|
|
str_res = await didkit.verify_credential(vc, '{"proofFormat": "ldp"}')
|
|
|
|
res = literal_eval(str_res)
|
|
|
|
ok = res["warnings"] == [] and res["errors"] == []
|
|
|
|
return ok, str_res
|
|
|
|
|
|
|
|
(ok, res) = asyncio.run(inner())
|
|
|
|
if not ok:
|
|
|
|
# The credential doesn't pass signature checks, so early return
|
|
|
|
return ok, res
|
|
|
|
return ok, res
|
2023-11-10 05:45:12 +00:00
|
|
|
|
2023-11-22 13:09:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def issue_verifiable_presentation(vc_list: list[str], jwk_holder: str, holder_did: str) -> str:
|
|
|
|
async def inner():
|
|
|
|
unsigned_vp = unsigned_vp_template.render(data)
|
|
|
|
signed_vp = await didkit.issue_presentation(
|
|
|
|
unsigned_vp,
|
|
|
|
'{"proofFormat": "ldp"}',
|
|
|
|
jwk_holder
|
|
|
|
)
|
|
|
|
return signed_vp
|
|
|
|
|
|
|
|
env = Environment(
|
|
|
|
loader=FileSystemLoader("vc_templates"),
|
|
|
|
autoescape=select_autoescape()
|
|
|
|
)
|
|
|
|
unsigned_vp_template = env.get_template("verifiable_presentation.json")
|
|
|
|
data = {
|
|
|
|
"holder_did": holder_did,
|
|
|
|
"verifiable_credential_list": "[" + ",".join(vc_list) + "]"
|
|
|
|
}
|
|
|
|
|
|
|
|
return asyncio.run(inner())
|
|
|
|
|
|
|
|
|
2023-11-27 06:26:02 +00:00
|
|
|
def verify_presentation(vp):
|
|
|
|
"""
|
|
|
|
Returns a (bool, str) tuple indicating whether the credential is valid.
|
|
|
|
If the boolean is true, the credential is valid and the second argument can be ignored.
|
|
|
|
If it is false, the VC is invalid and the second argument contains a JSON object with further information.
|
|
|
|
"""
|
|
|
|
async def inner():
|
|
|
|
proof_options = '{"proofFormat": "ldp"}'
|
2023-11-28 08:45:06 +00:00
|
|
|
return await didkit.verify_presentation(vp, proof_options)
|
2023-11-27 06:26:02 +00:00
|
|
|
|
|
|
|
return asyncio.run(inner())
|