-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayment_channel.py
70 lines (56 loc) · 1.39 KB
/
payment_channel.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
import pprint
from src.payment import Payment
from src.Registry.registry import Registry
def two_party_exchange_with_fee():
X = Registry()
xh = X.hash()
print(f"User X: {xh}")
Y = Registry()
yh = Y.hash()
print(f"User Y: {yh}")
# F always exists as the fee owner
F = Registry()
fh = F.hash()
print(f"Fee: {fh}")
# this is going to act like our onchain merkle tree
print("\nInitial State:")
values = {
xh: 100,
yh: 25,
fh: 0,
}
pprint.pp(values)
# x wants to send b to y and pay fee to f
b = 15
fee = 1
# calculate the final states
x = values[xh] - fee - b
y = values[yh] + b
f = values[fh] + fee
# the payment proof
payment = Payment(
# sign the value
X.boneh_lynn_shacham_signature(xh),
# receiver
Y,
# initial values
(values[xh], values[yh], values[fh]),
# final values
(x, y, f),
)
print("\nIs the payment valid?")
print(payment)
is_valid = payment.prove()
print(is_valid)
if is_valid is True:
del values[xh]
X.rerandomize()
xh = X.hash()
print(f"\nUser X Re-Randomized: {xh}")
values[xh] = x
values[yh] = y
values[fh] = f
print("\nFinal State:")
pprint.pp(values)
if __name__ == "__main__":
two_party_exchange_with_fee()