pyvckit/sign_vc.py

48 lines
1.2 KiB
Python
Raw Normal View History

2024-05-24 10:38:48 +00:00
import json
2024-05-24 15:22:35 +00:00
import argparse
2024-05-24 11:39:50 +00:00
from utils import now
2024-05-24 15:22:35 +00:00
from did import generate_did, get_signing_key, key_read
2024-05-24 11:39:50 +00:00
from templates import credential_tmpl, proof_tmpl
from sign import sign_proof
2024-05-24 10:38:48 +00:00
# source: https://github.com/mmlab-aueb/PyEd25519Signature2018/blob/master/signer.py
2024-05-24 11:39:50 +00:00
def sign(credential, key, issuer_did):
document = json.loads(credential)
2024-05-24 10:38:48 +00:00
_did = issuer_did + "#" + issuer_did.split("did:key:")[1]
2024-05-24 11:39:50 +00:00
proof = proof_tmpl.copy()
proof['verificationMethod'] = _did
proof['created'] = now()
2024-05-24 10:38:48 +00:00
sign_proof(document, proof, key)
del proof['@context']
document['proof'] = proof
return document
2024-05-24 11:39:50 +00:00
def main():
2024-05-24 15:22:35 +00:00
parser=argparse.ArgumentParser(description='Generates a new credential')
parser.add_argument("-k", "--key-path", required=True)
args=parser.parse_args()
2024-05-24 10:38:48 +00:00
2024-05-24 15:22:35 +00:00
if args.key_path:
key = key_read(args.key_path)
did = generate_did(key)
signing_key = get_signing_key(key)
2024-05-24 10:38:48 +00:00
2024-05-24 15:22:35 +00:00
credential = credential_tmpl.copy()
credential["issuer"] = did
credential["issuanceDate"] = now()
cred = json.dumps(credential)
2024-05-24 10:38:48 +00:00
2024-05-24 15:22:35 +00:00
vc = sign(cred, signing_key, did)
print(json.dumps(vc, separators=(',', ':')))
return
2024-05-24 10:38:48 +00:00
2024-05-24 11:39:50 +00:00
if __name__ == "__main__":
main()