-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
81 lines (74 loc) · 2.51 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from didself import registry
from jwcrypto import jwk, jws
import json
# DID creation
# Generate DID and initial secret key
did_key = jwk.JWK.generate(kty='EC', crv='P-256')
# Initialize registry
owner_registry = registry.DIDSelfRegistry(did_key)
# Generate the DID document
did_key_dict = did_key.export_public(as_dict=True)
did = "did:self:" + did_key.thumbprint()
did_document = {
'id': did,
'authentication': [{
'id': '#key1',
'type': "JsonWebKey2020",
'publicKeyJwk': did_key_dict
}],
}
owner_registry.create(did_document)
#-------------Dumping-------------------
document, proof = owner_registry.read()
print("DID document:")
print(json.dumps(document, indent=2))
print("DID document proof:")
print(proof)
document_proof = jws.JWS()
document_proof.deserialize(proof)
payload = json.loads(document_proof.objects['payload'].decode())
print("Document proof payload:")
print(json.dumps(payload, indent=2))
print("Document proof signature:")
print(document_proof.objects['signature'].hex())
print("----------------------------------")
# Change the authentication key
authentication_jwk = jwk.JWK.generate(kty='OKP', crv='Ed25519')
did_document = {
'id': did,
'authentication': [{
'id': '#key2',
'type': "JsonWebKey2020",
'publicKeyJwk': authentication_jwk.export_public(as_dict=True)
}]
}
owner_registry.update(did_document)
#-------------Dumping-------------------
document, proof = owner_registry.read()
print("DID document:")
print(json.dumps(document, indent=2))
print("DID document proof:")
print(proof)
document_proof = jws.JWS()
document_proof.deserialize(proof)
payload = json.loads(document_proof.objects['payload'].decode())
print("Document proof payload:")
print(json.dumps(payload, indent=2))
print("Document proof signature:")
print(document_proof.objects['signature'].hex())
print("----------------------------------")
#-------------------x509-------------------
x509 = owner_registry.exportX509(authentication_jwk.export_to_pem(password=None))
print("X509 certificate:\n", x509[0].decode())
print("X509 certificate:\n", x509[1].decode())
#------------------JWS----------------------
x5c = owner_registry.exportX509(authentication_jwk.export_to_pem(password=None),"DER")
jws_header_dict = {
'alg': "EdDSA",
'x5c': x5c
}
jws_header = json.dumps(jws_header_dict)
jws_payload="hello world"
proof = jws.JWS(jws_payload.encode('utf-8'))
proof.add_signature(authentication_jwk, None, jws_header,None)
print("JWS\n", proof.serialize(compact=True))