-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.py
71 lines (61 loc) · 4.11 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
import os
from nucypher_kms import KMS
# Share secret with yourself (Without IPFS)
user1 = KMS(ursula_url="localhost:11500", dir_name="user1", passphrase="&W=nqr2N:,[2}sAr")
label, data_source_public_key, data = user1.encrypt_data(plaintext="sample plaintext")
print("encrypted data: {}".format(data))
pubkeys = user1.pubkeys
policy_info = user1.share_data_access(pubkeys=pubkeys, label=label)
result = user1.decrypt_data(data_source_public_key=data_source_public_key, data=data, policy_info=policy_info)
print("decrypted data: {}".format(result))
# Share secret with another user (Without IPFS)
user1 = KMS(ursula_url="localhost:11500", dir_name="user1", passphrase="&W=nqr2N:,[2}sAr")
user2 = KMS(ursula_url="localhost:11500", dir_name="user2", passphrase="6Yd5M-d=rZ4Ny?Nx")
label, data_source_public_key, data = user1.encrypt_data(plaintext="sample plaintext")
print("encrypted data: {}".format(data))
pubkeys = user2.pubkeys
policy_info = user1.share_data_access(pubkeys=pubkeys, label=label)
result = user2.decrypt_data(data_source_public_key=data_source_public_key, data=data, policy_info=policy_info)
print("decrypted data: {}".format(result))
# Share secret with another user (With IPFS)
# Start ipfs daemon v0.7.0 locally before running the code (https://docs.ipfs.io/how-to/command-line-quick-start/#install-ipfs)
user1 = KMS(ursula_url="localhost:11500", dir_name="user1", passphrase="&W=nqr2N:,[2}sAr",
ipfs_addr="/ip4/127.0.0.1/tcp/5001/http")
user2 = KMS(ursula_url="localhost:11500", dir_name="user2", passphrase="6Yd5M-d=rZ4Ny?Nx",
ipfs_addr="/ip4/127.0.0.1/tcp/5001/http")
label, data_source_public_key, hash_key = user1.upload_data(plaintext="sample plaintext test", storage="ipfs")
print("hash key: {}".format(hash_key))
pubkeys = user2.pubkeys
policy_info = user1.share_data_access(pubkeys=pubkeys, label=label)
shareable_code = user1.get_shareable_code(hash_key=hash_key, data_source_public_key=data_source_public_key,
policy_info=policy_info, storage="ipfs")
print("Shareable code for user2: {}".format(shareable_code))
result = user2.fetch_data(shareable_code=shareable_code, storage="ipfs")
print("decrypted data: {}".format(result))
# Share secret with another user (With Sia Skynet)
user1 = KMS(ursula_url="localhost:11500", dir_name="user1", passphrase="&W=nqr2N:,[2}sAr")
user2 = KMS(ursula_url="localhost:11500", dir_name="user2", passphrase="6Yd5M-d=rZ4Ny?Nx")
label, data_source_public_key, hash_key = user1.upload_data(plaintext="sample plaintext test", storage="skynet")
print("hash key: {}".format(hash_key))
pubkeys = user2.pubkeys
policy_info = user1.share_data_access(pubkeys=pubkeys, label=label)
shareable_code = user1.get_shareable_code(hash_key=hash_key, data_source_public_key=data_source_public_key,
policy_info=policy_info, storage="skynet")
print("Shareable code for user2: {}".format(shareable_code))
result = user2.fetch_data(shareable_code=shareable_code, storage="skynet")
print("decrypted data: {}".format(result))
# Share secret with another user (With Arweave)
# Generate arweave wallet keyfile and store it in locally. It should have sufficient balance (https://www.arweave.org/wallet)
user1 = KMS(ursula_url="localhost:11500", dir_name="user1", passphrase="&W=nqr2N:,[2}sAr",
arweave_wallet_file_path=os.path.expanduser("~/arweave.json"))
user2 = KMS(ursula_url="localhost:11500", dir_name="user2", passphrase="6Yd5M-d=rZ4Ny?Nx",
arweave_wallet_file_path=os.path.expanduser("~/arweave.json"))
label, data_source_public_key, hash_key = user1.upload_data(plaintext="sample plaintext test", storage="arweave")
print("hash key: {}".format(hash_key))
pubkeys = user2.pubkeys
policy_info = user1.share_data_access(pubkeys=pubkeys, label=label)
shareable_code = user1.get_shareable_code(hash_key=hash_key, data_source_public_key=data_source_public_key,
policy_info=policy_info, storage="arweave")
print("Shareable code for user2: {}".format(shareable_code))
result = user2.fetch_data(shareable_code=shareable_code, storage="arweave")
print("decrypted data: {}".format(result))