/
bcsigutils.py
168 lines (130 loc) · 4.44 KB
/
bcsigutils.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
from bcdataio import Reader, Writer
from io import BytesIO
from hashing import shasha, sharip
from txndecoder import Script
from binascii import b2a_hex
def decode_signature(sigdata):
""" extract the r and s values from a signature """
if len(sigdata)==0x41:
return sigdata[0:32], sigdata[32:64], sigdata[64]
r = Reader(BytesIO(sigdata))
seqtag = r.readbyte()
if seqtag != 0x30:
raise Exception("not a signature")
rslen = r.readbyte()
inttag = r.readbyte()
if inttag != 0x02: raise Exception("not a signature")
rlen = r.readbyte()
rval = r.readbytes(rlen)
inttag = r.readbyte()
if inttag != 0x02: raise Exception("not a signature")
slen = r.readbyte()
sval = r.readbytes(slen)
# note: for BitcoinCash this has many different values, or None
sighashtype = r.readbyte() or 1 # 1,2,3 or 0x81,0x82,0x83
def make32bytes(x):
if len(x)==32:
return x
if len(x)<32:
return x.rjust(32, b"\x00")
return x[-32:]
return make32bytes(rval), make32bytes(sval), sighashtype
def messagehash(txn, hashtype, inputindex, outscript):
""" Calculate the message hash given the transaction, crackinfo and inputindex """
_anyonecanpay = hashtype&0x80
_single = (hashtype&31) == 3
_none = (hashtype&31) == 2
if _single and inputindex >= len(txn.outputs):
# this is a documented bug!!
return b"\x01" + b"\x00"*31
txndup = txn.copy()
if _anyonecanpay:
txndup.inputs = [ txndup.inputs[inputindex] ]
for i, inp in enumerate(txndup.inputs):
if _anyonecanpay or i==inputindex:
inp.script = outscript
else:
inp.script = Script()
if (_single or _none):
inp.sequence_number = 0
if _none:
txndup.outputs = []
elif _single:
txndup.outputs = txndup.outputs[:inputindex+1]
for i, out in enumerate(txndup.outputs):
if i!=inputindex:
out.script = Script()
out.btcvalue = 2**64-1
bio = BytesIO()
w = Writer(bio)
txndup.encode(w, exclude_witness=True)
w.writedword(hashtype) # the hashtype
data = bio.getvalue()
#print("hashing %s" % b2a_hex(data))
return shasha(data)
def calcPrevOutsHash(txn):
bio = BytesIO()
w = Writer(bio)
for inp in txn.inputs:
w.writebytes(inp.txn_hash)
w.writedword(inp.output_index)
return shasha(bio.getvalue())
def calcSequenceHash(txn):
bio = BytesIO()
w = Writer(bio)
for inp in txn.inputs:
w.writedword(inp.sequence_number)
return shasha(bio.getvalue())
def calcOutputsHash(txn):
bio = BytesIO()
w = Writer(bio)
for out in txn.outputs:
out.encode(w)
return shasha(bio.getvalue())
def calcSingleHash(txn, inputindex):
if inputindex < len(txn.outputs):
bio = BytesIO()
w = Writer(bio)
txn.outputs[inputindex].encode(w)
return shasha(bio.getvalue())
else:
return b"\x00"*32
def witnesshash(txn, hashtype, inputindex, btcvalue, outscript):
_anyonecanpay = hashtype&0x80
_single = (hashtype&31) == 3
_none = (hashtype&31) == 2
bio = BytesIO()
w = Writer(bio)
w.writedword(txn.version) # nVersion
if _anyonecanpay:
w.writebytes(b"\x00"*32)
else:
w.writebytes(calcPrevOutsHash(txn)) # hashPrevouts
if _anyonecanpay or _single or _none:
w.writebytes(b"\x00"*32)
else:
w.writebytes(calcSequenceHash(txn)) # hashSequence
inp = txn.inputs[inputindex]
w.writebytes(inp.txn_hash) # outpoint.hash
w.writedword(inp.output_index) # outpoint.index
if txn.witness:
wit = txn.witness[inputindex]
if not wit.encode_scriptcode(w): # scriptcode
return messagehash(txn, hashtype, inputindex, outscript)
else:
outscript.encode(w)
w.writeqword(btcvalue) # value
w.writedword(inp.sequence_number) # nSequence
if _single:
if inputindex < len(txn.outputs):
w.writebytes(calcSingleHash(txn, inputindex))
else:
w.writebytes(b"\x00"*32)
elif _none:
w.writebytes(b"\x00"*32)
else:
w.writebytes(calcOutputsHash(txn)) # hashOutputs
w.writedword(txn.locktime) # nLocktime
w.writedword(hashtype) # hashtype
data = bio.getvalue()
return shasha(data)