forked from libsv/go-bt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signaturehash.go
152 lines (119 loc) · 4.26 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
package bt
import (
"encoding/binary"
"encoding/hex"
"errors"
"github.com/libsv/go-bt/crypto"
"github.com/libsv/go-bt/sighash"
)
// TODO: change to "serialize tx"
// GetInputSignatureHash gets the preimage of the specified input and hashes it.
func (tx *Tx) GetInputSignatureHash(inputNumber uint32, sigHashFlag sighash.Flag) ([]byte, error) {
buf, err := tx.GetInputPreimage(inputNumber, sigHashFlag)
if err != nil {
return nil, err
}
ret := crypto.Sha256d(buf)
return ReverseBytes(ret), nil
}
// GetInputPreimage serializes the transaction based on the input index and the SIGHASH flag
// see https://github.com/bitcoin-sv/bitcoin-sv/blob/master/doc/abc/replay-protected-sighash.md#digest-algorithm
func (tx *Tx) GetInputPreimage(inputNumber uint32, sigHashFlag sighash.Flag) ([]byte, error) {
// todo: test if input exists (return error)
in := tx.Inputs[inputNumber]
if in.PreviousTxID == "" {
return nil, errors.New("'PreviousTxID' not supplied")
}
if in.PreviousTxScript == nil {
return nil, errors.New("'PreviousTxScript' not supplied")
}
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.getPreviousOutHash()
}
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.getSequenceHash()
}
if (sigHashFlag&31) != sighash.Single && (sigHashFlag&31) != sighash.None {
// This will be executed in the usual BSV case (where sigHashType = SighashAllForkID)
hashOutputs = tx.getOutputsHash(-1)
} else if (sigHashFlag&31) == sighash.Single && inputNumber < uint32(len(tx.Outputs)) {
// This will *not* be executed in the usual BSV case (where sigHashType = SighashAllForkID)
hashOutputs = tx.getOutputsHash(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)
txid, _ := hex.DecodeString(in.PreviousTxID[:])
buf = append(buf, ReverseBytes(txid)...)
oi := make([]byte, 4)
binary.LittleEndian.PutUint32(oi, in.PreviousTxOutIndex)
buf = append(buf, oi...)
// scriptCode of the input (serialized as scripts inside CTxOuts)
buf = append(buf, VarInt(uint64(len(*in.PreviousTxScript)))...)
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
}
func (tx *Tx) getPreviousOutHash() []byte {
buf := make([]byte, 0)
for _, in := range tx.Inputs {
// todo: error ignored?
txid, _ := hex.DecodeString(in.PreviousTxID[:])
buf = append(buf, ReverseBytes(txid)...)
oi := make([]byte, 4)
binary.LittleEndian.PutUint32(oi, in.PreviousTxOutIndex)
buf = append(buf, oi...)
}
return crypto.Sha256d(buf)
}
func (tx *Tx) getSequenceHash() []byte {
buf := make([]byte, 0)
for _, in := range tx.Inputs {
oi := make([]byte, 4)
binary.LittleEndian.PutUint32(oi, in.SequenceNumber)
buf = append(buf, oi...)
}
return crypto.Sha256d(buf)
}
func (tx *Tx) getOutputsHash(n int32) []byte {
buf := make([]byte, 0)
if n == -1 {
for _, out := range tx.Outputs {
buf = append(buf, out.GetBytesForSigHash()...)
}
} else {
buf = append(buf, tx.Outputs[n].GetBytesForSigHash()...)
}
return crypto.Sha256d(buf)
}