forked from hyperledger-archives/indy-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_secure_msg.py
64 lines (54 loc) · 2.08 KB
/
send_secure_msg.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
import asyncio
import time
import re
from indy import crypto, did, wallet
async def prep(wallet_handle, my_vk, their_vk, msg):
msg = bytes(msg, "utf-8")
encrypted = await crypto.auth_crypt(wallet_handle, my_vk, their_vk, msg)
# encrypted = await crypto.anon_crypt(their_vk, msg)
print('encrypted = %s' % repr(encrypted))
with open('message.dat', 'wb') as f:
f.write(encrypted)
print('prepping %s' % msg)
async def init():
me = input('Who are you? ').strip()
wallet_config = '{"id": "%s-wallet"}' % me
wallet_credentials = '{"key": "%s-wallet-key"}' % me
# 1. Create Wallet and Get Wallet Handle
try:
await wallet.create_wallet(wallet_config, wallet_credentials)
except:
pass
wallet_handle = await wallet.open_wallet(wallet_config, wallet_credentials)
print('wallet = %s' % wallet_handle)
(my_did, my_vk) = await did.create_and_store_my_did(wallet_handle, "{}")
print('my_did and verkey = %s %s' % (my_did, my_vk))
their = input("Other party's DID and verkey? ").strip().split(' ')
return wallet_handle, my_did, my_vk, their[0], their[1]
async def read(wallet_handle, my_vk):
with open('message.dat', 'rb') as f:
encrypted = f.read()
decrypted = await crypto.auth_decrypt(wallet_handle, my_vk, encrypted)
# decrypted = await crypto.anon_decrypt(wallet_handle, my_vk, encrypted)
print(decrypted)
async def demo():
wallet_handle, my_did, my_vk, their_did, their_vk = await init()
while True:
argv = input('> ').strip().split(' ')
cmd = argv[0].lower()
rest = ' '.join(argv[1:])
if re.match(cmd, 'prep'):
await prep(wallet_handle, my_vk, their_vk, rest)
elif re.match(cmd, 'read'):
await read(wallet_handle, my_vk)
elif re.match(cmd, 'quit'):
break
else:
print('Huh?')
if __name__ == '__main__':
try:
loop = asyncio.get_event_loop()
loop.run_until_complete(demo())
time.sleep(1) # waiting for libindy thread complete
except KeyboardInterrupt:
print('')