-
Notifications
You must be signed in to change notification settings - Fork 4
/
txs.py
129 lines (84 loc) · 4.96 KB
/
txs.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
from bitcoinutils.transactions import Transaction, TxOutput, TxInput, Sequence, TYPE_RELATIVE_TIMELOCK, TYPE_ABSOLUTE_TIMELOCK
from bitcoinutils.script import Script
from identity import Id
import init
import scripts
import consts
init.init_network()
### Transactions for lightning
def get_standard_ct(tx_in: TxInput, id_l: Id, id_r: Id, hashed_secret, val_l: int, val_r: int, fee: int, l: bool, timelock) -> Transaction:
if l:
tx_out0 = TxOutput(int(val_l-fee/2), scripts.get_script_lightning_locked(id_l, id_r, hashed_secret, timelock)) # output to l
tx_out1 = TxOutput(int(val_r-fee/2), id_r.p2pkh) # output to r
else:
tx_out0 = TxOutput(int(val_r-fee/2), scripts.get_script_lightning_locked(id_r, id_l, hashed_secret, timelock)) # output to r
tx_out1 = TxOutput(int(val_l-fee/2), id_l.p2pkh) # output to l
tx = Transaction([tx_in], [tx_out0, tx_out1])
scriptFToutput = scripts.get_script_ft_output(id_l, id_r)
sig_l = id_l.sk.sign_input(tx, 0, scriptFToutput)
sig_r = id_r.sk.sign_input(tx, 0, scriptFToutput)
tx_in.script_sig = Script([sig_r, sig_l])
return tx
def get_standard_ct_spend(tx_in: TxInput, payee: Id, script_ct: Script, val: int, fee: int)-> Transaction:
tx_out = TxOutput(val-fee, payee.p2pkh)
tx = Transaction([tx_in], [tx_out])
sig = payee.sk.sign_input(tx, 0 , Script(script_ct))
tx_in.script_sig = Script([sig])
return tx
def get_standard_ct_punish(tx_in: TxInput, payee: Id, script_ct: Script, secret, val: int, fee: int)-> Transaction:
tx_out = TxOutput(val-fee, payee.p2pkh)
tx = Transaction([tx_in], [tx_out])
sig = payee.sk.sign_input(tx, 0 , Script(script_ct))
tx_in.script_sig = Script([secret, sig])
return tx
### Generalized construction
def get_gen_ct_tx(tx_in: TxInput, id_l: Id, id_r: Id, id_as_l: Id, hashed_secret_rev_l, id_as_r: Id, hashed_secret_rev_r, val: int, fee: int, timelock: int) -> Transaction:
timelock = 0x2
tx_out = TxOutput(val-fee, scripts.get_script_split(id_l, id_r, id_as_l, hashed_secret_rev_l, id_as_r, hashed_secret_rev_r, timelock))
tx = Transaction([tx_in], [tx_out])
scriptFToutput = scripts.get_script_ft_output(id_l, id_r)
sig_l = id_l.sk.sign_input(tx, 0 , scriptFToutput)
sig_r = id_r.sk.sign_input(tx, 0 , scriptFToutput)
tx_in.script_sig = Script([sig_r, sig_l])
return tx
def get_gen_split_tx(tx_in: TxInput, id_l: Id, id_r: Id, script: Script, val_l: int, val_r, fee: int) -> Transaction:
tx_out0 = TxOutput(int(val_l-0.5*fee), id_l.p2pkh)
tx_out1 = TxOutput(int(val_r-0.5*fee), id_r.p2pkh)
tx = Transaction([tx_in], [tx_out0, tx_out1])
sig_l = id_l.sk.sign_input(tx, 0, Script(script))
sig_r = id_r.sk.sign_input(tx, 0, Script(script))
tx_in.script_sig = Script([sig_r, sig_l])
return tx
def get_gen_punish_tx(tx_in: TxInput, payee: Id, script: Script, id_as: Id, secret_rev, val: int, fee: int, l: bool) -> Transaction:
tx_out = TxOutput(val-fee, payee.p2pkh)
tx = Transaction([tx_in], [tx_out])
sig = payee.sk.sign_input(tx, 0, Script(script))
sig_as = id_as.sk.sign_input(tx, 0, Script(script))
if l:
tx_in.script_sig = Script([secret_rev, sig_as, 0x0, sig])
else:
tx_in.script_sig = Script([secret_rev, sig_as, sig, 0x0])
return tx
### Test HTLC construction (just for measuring)
def get_htlc(tx_in: TxInput, id_l: Id, id_r: Id, secret, hashed_secret, val_l: int, val_r: int, fee: int, l: bool, timelock) -> Transaction:
tx_out = TxOutput(int(val_l-fee/2), scripts.get_script_lightning_locked(id_l, id_r, hashed_secret, timelock)) # output to l
tx = Transaction([tx_in], [tx_out])
scriptFToutput = scripts.get_script_ft_output(id_l, id_r)
sig = id_l.sk.sign_input(tx, 0, scriptFToutput) # referenced tx is ft
tx_in.script_sig = Script([sig, secret])
return tx
def get_htlc_ct(tx_in: TxInput, id_l: Id, id_r: Id, hashed_secret, hashed_secret2, val_l: int, val_r: int, x, fee: int, l: bool, timelock) -> Transaction:
if l:
tx_out0 = TxOutput(int(val_l-fee/2), scripts.get_script_lightning_locked(id_l, id_r, hashed_secret, timelock)) # output to l
tx_out1 = TxOutput(int(val_l-fee/2), scripts.get_htlc_script(id_l, id_r, hashed_secret, hashed_secret2, timelock)) # HTLC output
tx_out2 = TxOutput(int(val_r-fee/2), id_r.p2pkh) # output to r
else:
tx_out0 = TxOutput(int(val_r-fee/2), scripts.get_script_lightning_locked(id_r, id_l, hashed_secret, timelock)) # output to r
tx_out1 = TxOutput(int(val_l-fee/2), scripts.get_htlc_script(id_l, id_r, hashed_secret, hashed_secret2, timelock)) # HTLC output
tx_out2 = TxOutput(int(val_l-fee/2), id_l.p2pkh) # output to l
tx = Transaction([tx_in], [tx_out0, tx_out1, tx_out2])
scriptFToutput = scripts.get_script_ft_output(id_l, id_r)
sig_l = id_l.sk.sign_input(tx, 0, scriptFToutput)
sig_r = id_r.sk.sign_input(tx, 0, scriptFToutput)
tx_in.script_sig = Script([sig_r, sig_l])
return tx