-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.py
327 lines (286 loc) · 11.6 KB
/
tx.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
from io import BytesIO
from usefulfunctions import int_to_little_endian, encode_varint, SIGHASH_ALL, hash256
from script import p2pkh_script, Script
# making the intitial transaction object where we devide the transaction in the
# Tx
# TxIn
# TxOut
"""Txout {
amount
script_pubkey
}"""
class TxOut:
# this is the basic frame of the
def __init__(self, amount, script_pubkey):
self.amount = amount
self.script_pubkey = script_pubkey
def serialize(self):
result = int_to_little_endian(
self.amount, 8
) # amount in the bitcoin is the 8 bytes
result += self.script_pubkey.serialize() # script pubkey is also been seraliaze
return result
# currently script is not implemented
"""
TxIn {
prev_tx
prev_index
sequence
prevout
script_sig
}
"""
class TxIn:
def __init__(
self, prev_tx, prev_index, sequence, prevout, witness=None, script_sig=None
):
self.prev_tx = prev_tx
self.prev_index = prev_index
if (
script_sig is None
): # if the witness is there then this is "" so i set it to the none
self.script_sig = Script() # to initialize it in the case of the
else:
self.script_sig = script_sig
self.sequence = sequence
self.witness = witness
self.prevout = (
prevout # contain the vout info of the spend utxo this is of the type Txout
)
self._hash_prevouts = None
self._hash_sequence = None
self._hash_outputs = None
def serialize(self):
result = self.prev_tx[::-1] # little edian is the reverse
result += int_to_little_endian(self.prev_index, 4)
result += (
self.script_sig.serialize()
) # script is also been serialize according to the rule
result += int_to_little_endian(self.sequence, 4)
return result
def value(self):
return self.prevout.amount
def script_pubkey(self):
return self.prevout.script_pubkey
"""
Tx {
version
inputs
outputs
Loctime
}
"""
class Tx:
def __init__(self, version, tx_ins, tx_outs, locktime, segwit=None):
self.version = version
self.tx_ins = tx_ins # this is may be the array of the TxIn
self.tx_outs = tx_outs # this is may be the array of the aTxout
self.locktime = locktime
self.segwit = segwit # is this is segwit transaction or not
self._hash_prevouts = None # prevout transacation
self._hash_sequence = None
self._hash_outputs = None # all the output serialized hash
def id(self):
return self.hash().hex() # txid
def wtxid(self): # used to find the wxtid
return (
hash256(self.serialize_segwit())[::-1]
).hex() # used in the formation of the witness commitment
# tag::source5[]
def hash256for(self):
return hash256(self.serialize_legacy())
def hash(self):
return hash256(self.serialize_legacy())[::-1]
# currently only serialixe the transaction using the
def serialize(self):
# now there are two techniques of the seriliaztion
if self.segwit:
return self.serialize_segwit() # containing witness
else:
return self.serialize_legacy()
def serialize_legacy(
self,
): # this is using the seriliazation whihc does not containt the legacy
result = int_to_little_endian(self.version, 4)
result += encode_varint(len(self.tx_ins))
for tx_in in self.tx_ins:
result += tx_in.serialize()
result += encode_varint(len(self.tx_outs))
for tx_out in self.tx_outs:
result += tx_out.serialize()
result += int_to_little_endian(self.locktime, 4)
return result
def serialize_segwit(self):
result = int_to_little_endian(self.version, 4)
result += b"\x00\x01" # maker flag
result += encode_varint(len(self.tx_ins))
for tx_in in self.tx_ins:
result += tx_in.serialize()
result += encode_varint(len(self.tx_outs))
for tx_out in self.tx_outs:
result += tx_out.serialize()
for tx_in in self.tx_ins:
result += int_to_little_endian(len(tx_in.witness), 1)
for item in tx_in.witness: # witness transaction is added at the end
if type(item) == int:
result += int_to_little_endian(item, 1)
else:
result += encode_varint(len(item)) + (item)
result += int_to_little_endian(self.locktime, 4)
return result
def weightunit(self): # here we find the wu from which we find the
if self.segwit:
# finding for the segwit transaction
result = len(int_to_little_endian(self.version, 4)) * 4
result += len(b"\x00\x01") * 1 # this is the segwit data
result += (
len(encode_varint(len(self.tx_ins))) * 4
) # non segwit data is * 4 times
for tx_in in self.tx_ins:
result += len(tx_in.serialize()) * 4
result += len(encode_varint(len(self.tx_outs))) * 4
for tx_out in self.tx_outs:
result += len(tx_out.serialize()) * 4
for tx_in in self.tx_ins:
result += (
len(int_to_little_endian(len(tx_in.witness), 1)) * 1
) # segwit data in 1 times
for item in tx_in.witness:
if type(item) == int:
result += len(int_to_little_endian(item, 1)) * 1
else:
result += len(encode_varint(len(item)) + (item)) * 1
result += len(int_to_little_endian(self.locktime, 4)) * 4
return result
else:
# finding for the legacy transaction
result = int_to_little_endian(self.version, 4)
result += encode_varint(len(self.tx_ins))
for tx_in in self.tx_ins:
result += tx_in.serialize()
result += encode_varint(len(self.tx_outs))
for tx_out in self.tx_outs:
result += tx_out.serialize()
result += int_to_little_endian(self.locktime, 4)
return len(result) * 4
def fee(self): # calculating the fees
ini, out = 0, 0
for tx_in in self.tx_ins:
ini += tx_in.value()
for tx_out in self.tx_outs:
out += tx_out.amount
return ini - out
def sig_hash(
self, input_index, redeem_script=None
): # creating the sighash for each of the index
s = int_to_little_endian(self.version, 4)
s += encode_varint(len(self.tx_ins))
for i, tx_in in enumerate(self.tx_ins):
if i == input_index:
if redeem_script:
script_sig = redeem_script
else:
script_sig = tx_in.script_pubkey()
else:
script_sig = None
s += TxIn(
prev_tx=tx_in.prev_tx,
prev_index=tx_in.prev_index,
script_sig=script_sig,
sequence=tx_in.sequence,
prevout=None, # as prevout information is not stored on the
witness=None, # witness is also not signed
).serialize() # serilaize it
s += encode_varint(len(self.tx_outs))
for tx_out in self.tx_outs:
s += tx_out.serialize()
s += int_to_little_endian(self.locktime, 4)
s += int_to_little_endian(SIGHASH_ALL, 4)
h256 = hash256(s) # 2 round of the sha256
return int.from_bytes(h256, "big") # this is able to create the message
def hash_prevouts(self):
if self._hash_prevouts is None:
allprevouts = b""
allsequence = b""
for tx_in in self.tx_ins:
allprevouts += tx_in.prev_tx[::-1] + int_to_little_endian(
tx_in.prev_index, 4
)
allsequence += int_to_little_endian(tx_in.sequence, 4)
self._hash_prevouts = hash256(allprevouts)
self._hash_sequence = hash256(allsequence)
return self._hash_prevouts
def hash_sequence(self):
if self._hash_sequence is None:
self.hash_prevouts()
return self._hash_sequence
def hash_outputs(self): # output hash is finding out
if self._hash_outputs is None:
all_outputs = b""
for (
tx_out
) in self.tx_outs: # first creating and concate the all serialize txout
all_outputs += tx_out.serialize() # all the txn output is serialize
self._hash_outputs = hash256(
all_outputs
) # then we hash the output hash only
return self._hash_outputs # return the hashpoutput
def segwit_hash(
self, input_index, redeem_script=None, witness_script=None
): # creating the message hash for the segwit transaction
tx_in = self.tx_ins[input_index]
s = int_to_little_endian(self.version, 4)
s += self.hash_prevouts() + self.hash_sequence()
s += tx_in.prev_tx[::-1] + int_to_little_endian(tx_in.prev_index, 4)
if witness_script:
script_code = witness_script.serialize()
elif redeem_script: # if reedem script is there means
script_code = p2pkh_script(redeem_script.cmds[1]).serialize()
else:
script_code = p2pkh_script(tx_in.script_pubkey().cmds[1]).serialize()
s += script_code
s += int_to_little_endian(tx_in.value(), 8)
s += int_to_little_endian(tx_in.sequence, 4)
s += self.hash_outputs() # output hash is calulcated
s += int_to_little_endian(self.locktime, 4)
s += int_to_little_endian(SIGHASH_ALL, 4) # assuming that all is signed
return int.from_bytes(hash256(s), "big")
def verify_input(self, input_index):
tx_in = self.tx_ins[input_index]
script_pubkey = tx_in.script_pubkey()
if script_pubkey.is_p2sh_script_pubkey():
cmd = tx_in.script_sig.cmds[-1]
raw_redeem = int_to_little_endian(len(cmd), 1) + cmd
redeem_script = Script.parse(BytesIO(raw_redeem))
if redeem_script.is_p2wpkh_script_pubkey():
z = self.segwit_hash(input_index, redeem_script)
witness = tx_in.witness
elif redeem_script.is_p2wsh_script_pubkey():
cmd = tx_in.witness[-1]
raw_witness = encode_varint(len(cmd)) + cmd
witness_script = Script.parse(BytesIO(raw_witness))
z = self.segwit_hash(input_index, witness_script=witness_script)
witness = tx_in.witness
else:
z = self.sig_hash(input_index, redeem_script)
witness = None
else:
if script_pubkey.is_p2wpkh_script_pubkey():
z = self.segwit_hash(input_index)
witness = tx_in.witness
elif script_pubkey.is_p2wsh_script_pubkey():
cmd = tx_in.witness[-1]
raw_witness = encode_varint(len(cmd)) + cmd
witness_script = Script.parse(BytesIO(raw_witness))
z = self.segwit_hash(input_index, witness_script=witness_script)
witness = tx_in.witness
else:
z = self.sig_hash(input_index)
witness = None
combined = tx_in.script_sig + script_pubkey
return combined.evaluate(z, witness)
def verify(self):
for i in range(len(self.tx_ins)):
if not self.verify_input(i):
return False
return True