2024-05-24 10:38:48 +00:00
|
|
|
import json
|
2024-05-24 16:18:16 +00:00
|
|
|
import argparse
|
2024-05-24 10:38:48 +00:00
|
|
|
|
2024-05-24 16:18:16 +00:00
|
|
|
from utils import now
|
|
|
|
from templates import presentation_tmpl, proof_tmpl
|
|
|
|
from did import key_read, generate_did, get_signing_key
|
|
|
|
from sign import sign_proof
|
2024-05-24 10:38:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def sign_vp(signing_key, holder_did, vc):
|
2024-05-24 16:18:16 +00:00
|
|
|
presentation = presentation_tmpl.copy()
|
|
|
|
presentation["verifiableCredential"].append(json.loads(vc))
|
2024-05-24 10:38:48 +00:00
|
|
|
presentation["holder"] = holder_did
|
2024-05-24 16:18:16 +00:00
|
|
|
|
|
|
|
_did = holder_did + "#" + holder_did.split("did:key:")[1]
|
|
|
|
proof = proof_tmpl.copy()
|
|
|
|
proof['verificationMethod'] = _did
|
|
|
|
proof['created'] = now()
|
|
|
|
|
2024-05-24 10:38:48 +00:00
|
|
|
sign_proof(presentation, proof, signing_key)
|
|
|
|
del proof['@context']
|
|
|
|
presentation['proof'] = proof
|
|
|
|
return presentation
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2024-05-24 16:18:16 +00:00
|
|
|
parser=argparse.ArgumentParser(description='Generates a new credential')
|
|
|
|
parser.add_argument("-k", "--key-path", required=True)
|
|
|
|
parser.add_argument("-c", "--credential-path", required=True)
|
|
|
|
args=parser.parse_args()
|
2024-05-24 10:38:48 +00:00
|
|
|
|
2024-05-24 16:18:16 +00:00
|
|
|
if args.key_path and args.credential_path:
|
|
|
|
with open(args.credential_path, "r") as f:
|
|
|
|
vc = f.read()
|
2024-05-24 10:38:48 +00:00
|
|
|
|
2024-05-24 16:18:16 +00:00
|
|
|
if not vc:
|
|
|
|
print("You need pass a credential.")
|
|
|
|
return
|
2024-05-24 10:38:48 +00:00
|
|
|
|
2024-05-24 16:18:16 +00:00
|
|
|
key = key_read(args.key_path)
|
|
|
|
did = generate_did(key)
|
|
|
|
signing_key = get_signing_key(key)
|
|
|
|
vp = sign_vp(signing_key, did, vc)
|
|
|
|
print(json.dumps(vp, separators=(',', ':')))
|
2024-05-24 10:38:48 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|