-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstorage_demo.py
More file actions
128 lines (98 loc) · 2.96 KB
/
storage_demo.py
File metadata and controls
128 lines (98 loc) · 2.96 KB
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
#!/usr/bin/env python3
"""
DHT storage — immutable and mutable key-value records.
Requires a live DHT connection (use_public_bootstrap=True).
Usage:
python storage_demo.py put <text>
python storage_demo.py get <hash_hex>
python storage_demo.py mput <text>
python storage_demo.py mget <public_key_hex>
"""
import sys
sys.path.insert(0, ".")
from hyperdht import HyperDHT, KeyPair
def cmd_put(text):
"""Store immutable data. Target = BLAKE2b(value)."""
dht = HyperDHT(use_public_bootstrap=True)
dht.bind()
value = text.encode()
target = HyperDHT.hash(value)
print(f"Storing: {text!r}")
print(f"Target: {target.hex()}")
def on_done(err):
if err:
print(f"Error: {err}")
else:
print(f"Stored! Retrieve with:")
print(f" python storage_demo.py get {target.hex()}")
dht.destroy()
dht.on_bootstrapped(
lambda: dht.immutable_put(value, on_done=on_done))
dht.run()
def cmd_get(hash_hex):
"""Retrieve immutable data by content hash."""
dht = HyperDHT(use_public_bootstrap=True)
dht.bind()
target = bytes.fromhex(hash_hex)
found = []
def on_value(data):
found.append(data)
print(f"Found: {data!r}")
def on_done(err):
if not found:
print("Not found on the DHT")
dht.destroy()
dht.on_bootstrapped(
lambda: dht.immutable_get(target, on_value=on_value, on_done=on_done))
dht.run()
def cmd_mput(text):
"""Store signed mutable data. Target = BLAKE2b(publicKey)."""
dht = HyperDHT(use_public_bootstrap=True)
dht.bind()
kp = KeyPair.generate()
value = text.encode()
print(f"Storing: {text!r}")
print(f"Signed by: {kp.public_key.hex()[:32]}...")
def on_done(err):
if err:
print(f"Error: {err}")
else:
print(f"Stored! Retrieve with:")
print(f" python storage_demo.py mget {kp.public_key.hex()}")
dht.destroy()
dht.on_bootstrapped(
lambda: dht.mutable_put(kp, value, seq=1, on_done=on_done))
dht.run()
def cmd_mget(pk_hex):
"""Retrieve latest signed mutable value for a public key."""
dht = HyperDHT(use_public_bootstrap=True)
dht.bind()
pk = bytes.fromhex(pk_hex)
found = []
def on_value(seq, data, sig):
found.append(data)
print(f"Found (seq={seq}): {data!r}")
def on_done(err):
if not found:
print("Not found on the DHT")
dht.destroy()
dht.on_bootstrapped(
lambda: dht.mutable_get(pk, on_value=on_value, on_done=on_done))
dht.run()
if __name__ == "__main__":
if len(sys.argv) < 3:
print(__doc__)
sys.exit(1)
cmd = sys.argv[1]
arg = " ".join(sys.argv[2:])
if cmd == "put":
cmd_put(arg)
elif cmd == "get":
cmd_get(arg)
elif cmd == "mput":
cmd_mput(arg)
elif cmd == "mget":
cmd_mget(arg)
else:
print(__doc__)
sys.exit(1)