-
Notifications
You must be signed in to change notification settings - Fork 26
/
signaturehash.go
279 lines (233 loc) · 9.27 KB
/
signaturehash.go
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
package bt
import (
"bytes"
"encoding/binary"
"github.com/libsv/go-bk/crypto"
"github.com/libsv/go-bt/v2/bscript"
"github.com/libsv/go-bt/v2/sighash"
)
// defaultHex is used to fix a bug in the original client (see if statement in the CalcInputSignatureHash func)
var defaultHex = []byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
type sigHashFunc func(inputIdx uint32, shf sighash.Flag) ([]byte, error)
// sigStrat will decide which tx serialisation to use.
// The legacy serialisation will be used for txs pre-fork
// whereas the new serialisation will be used for post-fork
// txs (and they should include the sighash_forkid flag).
func (tx *Tx) sigStrat(shf sighash.Flag) sigHashFunc {
if shf.Has(sighash.ForkID) {
return tx.CalcInputPreimage
}
return tx.CalcInputPreimageLegacy
}
// CalcInputSignatureHash serialised the transaction and returns the hash digest
// to be signed. BitCoin (SV) uses a different signature hashing algorithm
// after the UAHF fork for replay protection.
//
// see https://github.com/bitcoin-sv/bitcoin-sv/blob/master/doc/abc/replay-protected-sighash.md#digest-algorithm
func (tx *Tx) CalcInputSignatureHash(inputNumber uint32, sigHashFlag sighash.Flag) ([]byte, error) {
sigHashFn := tx.sigStrat(sigHashFlag)
buf, err := sigHashFn(inputNumber, sigHashFlag)
if err != nil {
return nil, err
}
// A bug in the original Satoshi client implementation means specifying
// an index that is out of range results in a signature hash of 1 (as an
// uint256 little endian). The original intent appeared to be to
// indicate failure, but unfortunately, it was never checked and thus is
// treated as the actual signature hash. This buggy behaviour is now
// part of the consensus and a hard fork would be required to fix it.
//
// Due to this, if the tx signature returned matches this special case value,
// we skip the double hashing as to not interfere.
if bytes.Equal(defaultHex, buf) {
return buf, nil
}
return crypto.Sha256d(buf), nil
}
// CalcInputPreimage serialises the transaction based on the input index and the SIGHASH flag
// and returns the preimage before double hashing (SHA256d).
//
// see https://github.com/bitcoin-sv/bitcoin-sv/blob/master/doc/abc/replay-protected-sighash.md#digest-algorithm
func (tx *Tx) CalcInputPreimage(inputNumber uint32, sigHashFlag sighash.Flag) ([]byte, error) {
if tx.InputIdx(int(inputNumber)) == nil {
return nil, ErrInputNoExist
}
in := tx.InputIdx(int(inputNumber))
if len(in.PreviousTxID()) == 0 {
return nil, ErrEmptyPreviousTxID
}
if in.PreviousTxScript == nil {
return nil, ErrEmptyPreviousTxScript
}
hashPreviousOuts := make([]byte, 32)
hashSequence := make([]byte, 32)
hashOutputs := make([]byte, 32)
if sigHashFlag&sighash.AnyOneCanPay == 0 {
// This will be executed in the usual BSV case (where sigHashType = SighashAllForkID)
hashPreviousOuts = tx.PreviousOutHash()
}
if sigHashFlag&sighash.AnyOneCanPay == 0 &&
(sigHashFlag&31) != sighash.Single &&
(sigHashFlag&31) != sighash.None {
// This will be executed in the usual BSV case (where sigHashType = SighashAllForkID)
hashSequence = tx.SequenceHash()
}
if (sigHashFlag&31) != sighash.Single && (sigHashFlag&31) != sighash.None {
// This will be executed in the usual BSV case (where sigHashType = SighashAllForkID)
hashOutputs = tx.OutputsHash(-1)
} else if (sigHashFlag&31) == sighash.Single && inputNumber < uint32(tx.OutputCount()) {
// This will *not* be executed in the usual BSV case (where sigHashType = SighashAllForkID)
hashOutputs = tx.OutputsHash(int32(inputNumber))
}
buf := make([]byte, 0)
// Version
v := make([]byte, 4)
binary.LittleEndian.PutUint32(v, tx.Version)
buf = append(buf, v...)
// Input previousOuts/nSequence (none/all, depending on flags)
buf = append(buf, hashPreviousOuts...)
buf = append(buf, hashSequence...)
// outpoint (32-byte hash + 4-byte little endian)
buf = append(buf, ReverseBytes(in.PreviousTxID())...)
oi := make([]byte, 4)
binary.LittleEndian.PutUint32(oi, in.PreviousTxOutIndex)
buf = append(buf, oi...)
// scriptCode of the input (serialised as scripts inside CTxOuts)
buf = append(buf, VarInt(uint64(len(*in.PreviousTxScript))).Bytes()...)
buf = append(buf, *in.PreviousTxScript...)
// value of the output spent by this input (8-byte little endian)
sat := make([]byte, 8)
binary.LittleEndian.PutUint64(sat, in.PreviousTxSatoshis)
buf = append(buf, sat...)
// nSequence of the input (4-byte little endian)
seq := make([]byte, 4)
binary.LittleEndian.PutUint32(seq, in.SequenceNumber)
buf = append(buf, seq...)
// Outputs (none/one/all, depending on flags)
buf = append(buf, hashOutputs...)
// LockTime
lt := make([]byte, 4)
binary.LittleEndian.PutUint32(lt, tx.LockTime)
buf = append(buf, lt...)
// sighashType
// writer.writeUInt32LE(sighashType >>> 0)
st := make([]byte, 4)
binary.LittleEndian.PutUint32(st, uint32(sigHashFlag)>>0)
buf = append(buf, st...)
return buf, nil
}
// CalcInputPreimageLegacy serialises the transaction based on the input index and the SIGHASH flag
// and returns the preimage before double hashing (SHA256d), in the legacy format.
//
// see https://wiki.bitcoinsv.io/index.php/Legacy_Sighash_Algorithm
func (tx *Tx) CalcInputPreimageLegacy(inputNumber uint32, shf sighash.Flag) ([]byte, error) {
if tx.InputIdx(int(inputNumber)) == nil {
return nil, ErrInputNoExist
}
in := tx.InputIdx(int(inputNumber))
if len(in.PreviousTxID()) == 0 {
return nil, ErrEmptyPreviousTxID
}
if in.PreviousTxScript == nil {
return nil, ErrEmptyPreviousTxScript
}
// The SigHashSingle signature type signs only the corresponding input
// and output (the output with the same index number as the input).
//
// Since transactions can have more inputs than outputs, this means it
// is improper to use SigHashSingle on input indices that don't have a
// corresponding output.
//
// A bug in the original Satoshi client implementation means specifying
// an index that is out of range results in a signature hash of 1 (as an
// uint256 little endian). The original intent appeared to be to
// indicate failure, but unfortunately, it was never checked and thus is
// treated as the actual signature hash. This buggy behaviour is now
// part of the consensus and a hard fork would be required to fix it.
//
// Due to this, care must be taken by software that creates transactions
// which make use of SigHashSingle because it can lead to an extremely
// dangerous situation where the invalid inputs will end up signing a
// hash of 1. This in turn presents an opportunity for attackers to
// cleverly construct transactions which can steal those coins provided
// they can reuse signatures.
if shf.HasWithMask(sighash.Single) && int(inputNumber) > len(tx.Outputs)-1 {
return defaultHex, nil
}
txCopy := tx.Clone()
for i := range txCopy.Inputs {
if i == int(inputNumber) {
txCopy.Inputs[i].PreviousTxScript = tx.Inputs[inputNumber].PreviousTxScript
} else {
txCopy.Inputs[i].UnlockingScript = &bscript.Script{}
txCopy.Inputs[i].PreviousTxScript = &bscript.Script{}
}
}
if shf.HasWithMask(sighash.None) {
txCopy.Outputs = txCopy.Outputs[0:0]
for i := range txCopy.Inputs {
if i != int(inputNumber) {
txCopy.Inputs[i].SequenceNumber = 0
}
}
} else if shf.HasWithMask(sighash.Single) {
txCopy.Outputs = txCopy.Outputs[:inputNumber+1]
for i := 0; i < int(inputNumber); i++ {
txCopy.Outputs[i].Satoshis = 18446744073709551615 // -1 but underflowed
txCopy.Outputs[i].LockingScript = &bscript.Script{}
}
for i := range txCopy.Inputs {
if i != int(inputNumber) {
txCopy.Inputs[i].SequenceNumber = 0
}
}
}
if shf&sighash.AnyOneCanPay != 0 {
txCopy.Inputs = txCopy.Inputs[inputNumber : inputNumber+1]
}
buf := make([]byte, 0)
// Version
v := make([]byte, 4)
binary.LittleEndian.PutUint32(v, tx.Version)
buf = append(buf, v...)
buf = append(buf, VarInt(uint64(len(txCopy.Inputs))).Bytes()...)
for _, in := range txCopy.Inputs {
buf = append(buf, ReverseBytes(in.PreviousTxID())...)
oi := make([]byte, 4)
binary.LittleEndian.PutUint32(oi, in.PreviousTxOutIndex)
buf = append(buf, oi...)
buf = append(buf, VarInt(uint64(len(*in.PreviousTxScript))).Bytes()...)
buf = append(buf, *in.PreviousTxScript...)
sq := make([]byte, 4)
binary.LittleEndian.PutUint32(sq, in.SequenceNumber)
buf = append(buf, sq...)
}
buf = append(buf, VarInt(uint64(len(txCopy.Outputs))).Bytes()...)
for _, out := range txCopy.Outputs {
st := make([]byte, 8)
binary.LittleEndian.PutUint64(st, out.Satoshis)
buf = append(buf, st...)
buf = append(buf, VarInt(uint64(len(*out.LockingScript))).Bytes()...)
buf = append(buf, *out.LockingScript...)
}
// LockTime
lt := make([]byte, 4)
binary.LittleEndian.PutUint32(lt, tx.LockTime)
buf = append(buf, lt...)
sh := make([]byte, 4)
binary.LittleEndian.PutUint32(sh, uint32(shf)>>0)
return append(buf, sh...), nil
}
// OutputsHash returns a bytes slice of the requested output, used for generating
// the txs signature hash. If n is -1, it will create the byte slice from all outputs.
func (tx *Tx) OutputsHash(n int32) []byte {
buf := make([]byte, 0)
if n == -1 {
for _, out := range tx.Outputs {
buf = append(buf, out.BytesForSigHash()...)
}
} else {
buf = append(buf, tx.Outputs[n].BytesForSigHash()...)
}
return crypto.Sha256d(buf)
}