This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 735
/
write_did_and_query_verkey.py
155 lines (124 loc) · 6.54 KB
/
write_did_and_query_verkey.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""
Example demonstrating how to add DID with the role of Trust Anchor as Steward.
Uses seed to obtain Steward's DID which already exists on the ledger.
Then it generates new DID/Verkey pair for Trust Anchor.
Using Steward's DID, NYM transaction request is built to add Trust Anchor's DID and Verkey
on the ledger with the role of Trust Anchor.
Once the NYM is successfully written on the ledger, it generates new DID/Verkey pair that represents
a client, which are used to create GET_NYM request to query the ledger and confirm Trust Anchor's Verkey.
For the sake of simplicity, a single wallet is used. In the real world scenario, three different wallets
would be used and DIDs would be exchanged using some channel of communication
"""
import asyncio
import json
import pprint
from indy import pool, ledger, wallet, did
from indy.error import IndyError, ErrorCode
from utils import get_pool_genesis_txn_path, PROTOCOL_VERSION
pool_name = 'pool'
genesis_file_path = get_pool_genesis_txn_path(pool_name)
wallet_config = json.dumps({"id": "wallet"})
wallet_credentials = json.dumps({"key": "wallet_key"})
def print_log(value_color="", value_noncolor=""):
"""set the colors for text."""
HEADER = '\033[92m'
ENDC = '\033[0m'
print(HEADER + value_color + ENDC + str(value_noncolor))
async def write_nym_and_query_verkey():
try:
await pool.set_protocol_version(PROTOCOL_VERSION)
# 1.
print_log('\n1. Creates a new local pool ledger configuration that is used '
'later when connecting to ledger.\n')
pool_config = json.dumps({'genesis_txn': str(genesis_file_path)})
try:
await pool.create_pool_ledger_config(config_name=pool_name, config=pool_config)
except IndyError as ex:
if ex.error_code == ErrorCode.PoolLedgerConfigAlreadyExistsError:
pass
# 2.
print_log('\n2. Open pool ledger and get handle from libindy\n')
pool_handle = await pool.open_pool_ledger(config_name=pool_name, config=None)
# 3.
print_log('\n3. Creating new secure wallet\n')
try:
await wallet.create_wallet(wallet_config, wallet_credentials)
except IndyError as ex:
if ex.error_code == ErrorCode.WalletAlreadyExistsError:
pass
# 4.
print_log('\n4. Open wallet and get handle from libindy\n')
wallet_handle = await wallet.open_wallet(wallet_config, wallet_credentials)
# 5.
print_log('\n5. Generating and storing steward DID and verkey\n')
steward_seed = '000000000000000000000000Steward1'
did_json = json.dumps({'seed': steward_seed})
steward_did, steward_verkey = await did.create_and_store_my_did(wallet_handle, did_json)
print_log('Steward DID: ', steward_did)
print_log('Steward Verkey: ', steward_verkey)
# 6.
print_log('\n6. Generating and storing trust anchor DID and verkey\n')
trust_anchor_did, trust_anchor_verkey = await did.create_and_store_my_did(wallet_handle, "{}")
print_log('Trust anchor DID: ', trust_anchor_did)
print_log('Trust anchor Verkey: ', trust_anchor_verkey)
# 7.
print_log('\n7. Building NYM request to add Trust Anchor to the ledger\n')
nym_transaction_request = await ledger.build_nym_request(submitter_did=steward_did,
target_did=trust_anchor_did,
ver_key=trust_anchor_verkey,
alias=None,
role='TRUST_ANCHOR')
print_log('NYM transaction request: ')
pprint.pprint(json.loads(nym_transaction_request))
# 8.
print_log('\n8. Sending NYM request to the ledger\n')
nym_transaction_response = await ledger.sign_and_submit_request(pool_handle=pool_handle,
wallet_handle=wallet_handle,
submitter_did=steward_did,
request_json=nym_transaction_request)
print_log('NYM transaction response: ')
pprint.pprint(json.loads(nym_transaction_response))
# 9.
print_log('\n9. Generating and storing DID and verkey representing a Client '
'that wants to obtain Trust Anchor Verkey\n')
client_did, client_verkey = await did.create_and_store_my_did(wallet_handle, "{}")
print_log('Client DID: ', client_did)
print_log('Client Verkey: ', client_verkey)
# 10.
print_log('\n10. Building the GET_NYM request to query trust anchor verkey\n')
get_nym_request = await ledger.build_get_nym_request(submitter_did=client_did,
target_did=trust_anchor_did)
print_log('GET_NYM request: ')
pprint.pprint(json.loads(get_nym_request))
# 11.
print_log('\n11. Sending the Get NYM request to the ledger\n')
get_nym_response_json = await ledger.submit_request(pool_handle=pool_handle,
request_json=get_nym_request)
get_nym_response = json.loads(get_nym_response_json)
print_log('GET_NYM response: ')
pprint.pprint(get_nym_response)
# 12.
print_log('\n12. Comparing Trust Anchor verkey as written by Steward and as retrieved in GET_NYM '
'response submitted by Client\n')
print_log('Written by Steward: ', trust_anchor_verkey)
verkey_from_ledger = json.loads(get_nym_response['result']['data'])['verkey']
print_log('Queried from ledger: ', verkey_from_ledger)
print_log('Matching: ', verkey_from_ledger == trust_anchor_verkey)
# 13.
print_log('\n13. Closing wallet and pool\n')
await wallet.close_wallet(wallet_handle)
await pool.close_pool_ledger(pool_handle)
# 14.
print_log('\n14. Deleting created wallet\n')
await wallet.delete_wallet(wallet_config, wallet_credentials)
# 15.
print_log('\n15. Deleting pool ledger config\n')
await pool.delete_pool_ledger_config(pool_name)
except IndyError as e:
print('Error occurred: %s' % e)
def main():
loop = asyncio.get_event_loop()
loop.run_until_complete(write_nym_and_query_verkey())
loop.close()
if __name__ == '__main__':
main()