-
Notifications
You must be signed in to change notification settings - Fork 4
/
didcomm_cli.py
103 lines (87 loc) · 3.34 KB
/
didcomm_cli.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import click
from didcomm.errors import DIDCommError
from didcomm.pack_encrypted import PackEncryptedConfig
from didcomm.secrets.secrets_resolver_demo import SecretsResolverDemo
from peerdid.errors import MalformedPeerDIDError
from peerdid.types import VerificationMaterialFormatPeerDID
from didcomm_demo.didcomm_demo import DIDCommDemo
secrets_resolver = SecretsResolverDemo()
def set_secrets_resolver(resolver: SecretsResolverDemo):
global secrets_resolver
secrets_resolver = resolver
@click.group()
def cli():
pass
@cli.command()
@click.option('--auth-keys-count', default=1, help='Number of authentication keys')
@click.option('--agreement-keys-count', default=1, help='Number of agreement keys')
@click.option('--service-endpoint', default=None, help='Optional service endpoint')
@click.option('--service-routing-key', default=[], multiple=True, help='Optional service routing keys')
def create_peer_did(auth_keys_count, agreement_keys_count, service_endpoint, service_routing_key):
click.echo()
demo = DIDCommDemo(secrets_resolver)
try:
did = demo.create_peer_did(
auth_keys_count=auth_keys_count,
agreement_keys_count=agreement_keys_count,
service_endpoint=service_endpoint,
service_routing_keys=service_routing_key
)
click.echo(f"{did}")
except (ValueError, TypeError) as e:
click.echo(f"{e}")
click.echo()
@cli.command()
@click.argument('did')
@click.option('--format', type=click.Choice(['jwk', 'multibase'], case_sensitive=False),
default="jwk",
help='DID Doc format (JWK or Multibase)')
def resolve_peer_did(did, format):
click.echo()
format = VerificationMaterialFormatPeerDID.JWK if format == "jwk" else VerificationMaterialFormatPeerDID.MULTIBASE
try:
did_doc_json = DIDCommDemo.resolve_peer_did(did, format)
click.echo(f"{did_doc_json}")
except MalformedPeerDIDError as e:
click.echo(f"{e}")
click.echo()
@cli.command()
@click.argument('msg')
@click.option('--to', required=True, help="Receiver's DID")
@click.option('--from', 'frm', default=None, help="Sender's DID. Anonymous encryption is used if not set.")
@click.option('--sign-from', default=None,
help="Sender's DID for optional signing. The message is not signed if not set.")
@click.option('--protect-sender-id', default=True,
help="Whether the sender's ID (DID) must be hidden. True by default.")
def pack(msg, to, frm, sign_from, protect_sender_id):
click.echo()
demo = DIDCommDemo(secrets_resolver)
try:
res = demo.pack(
msg=msg,
to=to,
frm=frm,
sign_frm=sign_from,
config=PackEncryptedConfig(protect_sender_id=protect_sender_id)
)
click.echo(f"{res.packed_msg}")
except DIDCommError as e:
click.echo(f"{e}")
click.echo()
@cli.command()
@click.argument('msg')
def unpack(msg):
click.echo()
try:
demo = DIDCommDemo(secrets_resolver)
initial_msg, frm, to, _ = demo.unpack(msg)
click.echo()
if frm:
click.echo(f"authcrypted '{initial_msg}' from {frm} to {to}")
else:
click.echo(f"anoncrypted '{initial_msg}' to {to}")
except DIDCommError as e:
click.echo(f"{e}")
click.echo()
if __name__ == '__main__':
cli()