-
Notifications
You must be signed in to change notification settings - Fork 2
/
txHash.go
366 lines (312 loc) · 10.3 KB
/
txHash.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package lbtcTransaction
import (
"encoding/hex"
"errors"
"fmt"
owcrypt "github.com/nbit99/go-owcrypt"
)
type NormalTx struct {
Address string
SigType byte
SigPub SignaturePubkey
}
type MultiTx struct {
Pubkey string
SigType byte
SigPub SignaturePubkey
}
type TxHash struct {
Hash string
NRequired byte
Normal *NormalTx
Multi []MultiTx
}
func (tx TxHash) IsMultisig() bool {
if tx.NRequired == 0 {
return false
}
return true
}
func (tx TxHash) GetTxHashHex() string {
return tx.Hash
}
func (tx TxHash) GetNormalTxAddress() string {
return tx.Normal.Address
}
func (tx TxHash) GetMultiTxPubkeys() []string {
var ret []string
for _, p := range tx.Multi {
ret = append(ret, p.Pubkey)
}
return ret
}
func newTxHash(hash, lockscript, redeem []byte, inType, sigType byte, addressPrefix AddressPrefix) (*TxHash, error) {
var p2pkhPrefixByte []byte
var p2wpkhPrefixByte []byte
p2pkhPrefixByte = addressPrefix.P2PKHPrefix
p2wpkhPrefixByte = addressPrefix.P2WPKHPrefix
if inType == TypeP2PKH {
return &TxHash{hex.EncodeToString(hash), 0, &NormalTx{EncodeCheck(p2pkhPrefixByte, lockscript[3:23]), sigType, SignaturePubkey{nil, nil}}, nil}, nil
} else if inType == TypeP2WPKH {
return &TxHash{hex.EncodeToString(hash), 0, &NormalTx{EncodeCheck(p2wpkhPrefixByte, lockscript[2:22]), sigType, SignaturePubkey{nil, nil}}, nil}, nil
} else if inType == TypeMultiSig {
nRequired, pubkeys, err := getMultiDetails(redeem)
if err != nil {
return nil, err
}
var multiTx []MultiTx
for _, p := range pubkeys {
multiTx = append(multiTx, MultiTx{p, sigType, SignaturePubkey{nil, nil}})
}
return &TxHash{hex.EncodeToString(hash), nRequired, nil, multiTx}, nil
}
return nil, nil
}
func checkScriptType(scriptPubkey, redeemScript string) ([]byte, []byte, byte, error) {
script, err := hex.DecodeString(scriptPubkey)
if err != nil {
return nil, nil, 0, errors.New("Invalid scriptPubkey data!")
}
if len(script) == 25 && script[0] == OpCodeDup && script[1] == OpCodeHash160 && script[2] == 0x14 && script[23] == OpCodeEqualVerify && script[24] == OpCodeCheckSig {
//if redeemScript != "" {
// return nil, nil, 0, errors.New("Found redeemScript when unlock a p2pkh input!")
//}
return script, nil, TypeP2PKH, nil
} else if len(script) == 23 && script[0] == OpCodeHash160 && script[1] == 0x14 && script[22] == OpCodeEqual {
redeem, err := hex.DecodeString(redeemScript)
if err != nil {
return nil, nil, 0, errors.New("Invalid redeemScript for a P2SH type input!")
}
if len(redeem) == 22 && redeem[0] == 0x00 && redeem[1] == 0x14 {
return script, redeem, TypeP2WPKH, nil
}
if len(redeem) >= 37 && redeem[len(redeem)-1] == OpCheckMultiSig {
return script, redeem, TypeMultiSig, nil
}
} else if len(script) == 22 && script[0] == 0x00 && script[1] == 0x14 {
if redeemScript != "" {
return nil, nil, 0, errors.New("Found redeemScript when unlock a bech32 input!")
}
return script, nil, TypeBech32, nil
}
return nil, nil, 0, errors.New("Unknown type of lockScript!")
}
func (t Transaction) calcSegwitSerializationHashes() ([]byte, []byte, []byte) {
hashPrevouts := []byte{}
hashSequence := []byte{}
hashOutputs := []byte{}
for _, vin := range t.Vins {
hashPrevouts = append(hashPrevouts, vin.TxID...)
hashPrevouts = append(hashPrevouts, vin.Vout...)
hashSequence = append(hashSequence, vin.sequence...)
}
for _, vout := range t.Vouts {
hashOutputs = append(hashOutputs, vout.amount...)
hashOutputs = append(hashOutputs, byte(len(vout.lockScript)))
hashOutputs = append(hashOutputs, vout.lockScript...)
}
return owcrypt.Hash(hashPrevouts, 0, owcrypt.HASH_ALG_DOUBLE_SHA256),
owcrypt.Hash(hashSequence, 0, owcrypt.HASH_ALG_DOUBLE_SHA256),
owcrypt.Hash(hashOutputs, 0, owcrypt.HASH_ALG_DOUBLE_SHA256)
}
func genScriptCodeFromRedeemScript(redeemBytes []byte) ([]byte, error) {
ret := []byte{}
if redeemBytes[0] == 0x00 && redeemBytes[1] == 0x14 {
ret = redeemBytes[2:]
if len(ret) != 0x14 {
return nil, errors.New("Invalid redeem script!")
}
ret = append([]byte{byte(len(ret))}, ret...)
ret = append([]byte{OpCodeDup, OpCodeHash160}, ret...)
ret = append(ret, OpCodeEqualVerify, OpCodeCheckSig)
} else {
ret = redeemBytes
}
return ret, nil
}
func (t Transaction) getSegwitBytesForSig(reddemBytes, txid, vout, sequence []byte, sigType byte, amount uint64) ([]byte, error) {
sigBytes := []byte{}
sigBytes = append(sigBytes, t.Version...)
hashPrevouts, hashSequence, hashOutputs := t.calcSegwitSerializationHashes()
sigBytes = append(sigBytes, hashPrevouts...)
sigBytes = append(sigBytes, hashSequence...)
sigBytes = append(sigBytes, txid...)
sigBytes = append(sigBytes, vout...)
scriptCode, err := genScriptCodeFromRedeemScript(reddemBytes)
if err != nil {
return nil, err
}
sigBytes = append(sigBytes, byte(len(scriptCode)))
sigBytes = append(sigBytes, scriptCode...)
if amount == 0 {
return nil, errors.New("Invalid amount of input!")
}
sigBytes = append(sigBytes, uint64ToLittleEndianBytes(amount)...)
sigBytes = append(sigBytes, sequence...)
sigBytes = append(sigBytes, hashOutputs...)
sigBytes = append(sigBytes, t.LockTime...)
//sigBytes = append(sigBytes, uint32ToLittleEndianBytes(1)...)
return sigBytes, nil
}
func (t Transaction) getBytesForSig(lockBytes, redeemBytes []byte, inType, sigType byte, index int, amount uint64, SegwitON bool) ([]byte, error) {
sigBytes := []byte{}
var err error
//if inType == TypeP2PKH {
// if sigType == SigHashAll {
// t.Vins[index].scriptPub = lockBytes
// sigBytes, err = t.encodeToBytes(SegwitON)
// if err != nil {
// return nil, err
// }
// } else {
// // TODO
// return nil, errors.New("The sigType inputed is not supported yet!")
// }
//} else
if inType == TypeP2WPKH || inType == TypeP2PKH {
if sigType == SigHashAll {
t.Vins[index].scriptPub = lockBytes
sigBytes, err = t.encodeToBytes(SegwitON)
//sigBytes, err = t.getSegwitBytesForSig(lockBytes, t.Vins[index].TxID, t.Vins[index].Vout, t.Vins[index].sequence, sigType, amount)
if err != nil {
return nil, err
}
} else {
// TODO
return nil, errors.New("The sigType inputed is not supported yet!")
}
} else if inType == TypeBech32 {
if sigType == SigHashAll {
sigBytes, err = t.getSegwitBytesForSig(lockBytes, t.Vins[index].TxID, t.Vins[index].Vout, t.Vins[index].sequence, sigType, amount)
if err != nil {
return nil, err
}
} else {
// TODO
return nil, errors.New("The sigType inputed is not supported yet!")
}
} else if inType == TypeMultiSig {
if sigType == SigHashAll {
if SegwitON {
sigBytes, err = t.getSegwitBytesForSig(redeemBytes, t.Vins[index].TxID, t.Vins[index].Vout, t.Vins[index].sequence, sigType, amount)
if err != nil {
return nil, err
}
} else {
t.Vins[index].scriptPub = redeemBytes
sigBytes, err = t.encodeToBytes(SegwitON)
if err != nil {
return nil, err
}
}
} else {
// TODO
return nil, errors.New("The sigType inputed is not supported yet!")
}
}
sigBytes = append(sigBytes, uint32ToLittleEndianBytes(uint32(SigHashAll))...)
fmt.Printf("before preimage:%x \n", sigBytes)
//sigBytes = append(sigBytes, []byte("0x4354424c")...)
sigBytes = append(sigBytes, uint32ToLittleEndianBytes(0x4354424c)...)
//preimage = preimage + int_to_hex(0x4354424c, 4)
fmt.Printf(" after preimage:%x \n", sigBytes)
return sigBytes, nil
}
func (t Transaction) getHashesForSig(unlockData []TxUnlock, SegwitON bool, addressPrefix AddressPrefix) ([]TxHash, error) {
hashes := []TxHash{}
if t.Vins == nil || len(t.Vins) != len(unlockData) {
return nil, errors.New("The number of Keys and UTXOs are not match!")
}
if t.Vouts == nil || len(t.Vouts) == 0 {
return nil, errors.New("No output found!")
}
for i := 0; i < len(unlockData); i++ {
for j := 0; j < len(unlockData); j++ {
t.Vins[j].setEmpty()
}
lockBytes, redeemBytes, inType, err := checkScriptType(unlockData[i].LockScript, unlockData[i].RedeemScript)
if err != nil {
return nil, err
}
sigBytes, err := t.getBytesForSig(lockBytes, redeemBytes, inType, unlockData[i].SigType, i, unlockData[i].Amount, SegwitON)
if err != nil {
return nil, err
}
hash := owcrypt.Hash(sigBytes, 0, owcrypt.HASH_ALG_DOUBLE_SHA256)
txHash, err := newTxHash(hash, lockBytes, redeemBytes, inType, unlockData[i].SigType, addressPrefix)
if err != nil {
return nil, err
}
hashes = append(hashes, *txHash)
}
return hashes, nil
}
func (t TxHash) encodeToScript(redeem []byte, SegwitON bool) ([]byte, error) {
if t.NRequired == 0 {
if t.Normal.SigPub.Signature == nil || len(t.Normal.SigPub.Signature) != 64 {
return nil, errors.New("Invalid signature data!")
}
if t.Normal.SigPub.Pubkey == nil || len(t.Normal.SigPub.Pubkey) != 33 {
return nil, errors.New("Invalid pubkey data!")
}
return t.Normal.SigPub.encodeToScript(t.Normal.SigType), nil
}
count := byte(0)
for _, s := range t.Multi {
if s.SigPub.Pubkey == nil && s.SigPub.Signature == nil {
continue
}
if len(s.SigPub.Pubkey) != 33 || len(s.SigPub.Signature) != 64 {
return nil, errors.New("Invalid signature or pubkey data for multisig!")
}
count++
}
if count < t.NRequired {
return nil, errors.New("The multisig transaction is not complete signed yet!")
}
var ret []byte
var sigs []byte
if redeem == nil {
return nil, errors.New("Missing redeem for multisig!")
}
redeemLen := len(redeem)
ret = append(ret, redeem...)
if !SegwitON {
if redeemLen < 0x4C {
ret = append([]byte{byte(redeemLen)}, ret...)
} else if redeemLen <= 0xFF {
ret = append([]byte{byte(redeemLen)}, ret...)
ret = append([]byte{OpPushData1}, ret...)
} else if redeemLen <= 0xFFFF {
ret = append(uint16ToLittleEndianBytes(uint16(redeemLen)), ret...)
ret = append([]byte{OpPushData2}, ret...)
} else {
return nil, errors.New("MultiSig redeem data is too long!")
}
} else {
ret = append([]byte{byte(redeemLen)}, ret...)
}
count = 0
for _, s := range t.Multi {
if s.SigPub.Signature == nil {
continue
}
sigs = append(sigs, s.SigPub.encodeSignatureToScript(s.SigType)...)
count++
if count == t.NRequired {
break
}
}
ret = append(sigs, ret...)
ret = append([]byte{0x00}, ret...)
if !SegwitON {
length := len(ret)
if length < 0xFD {
ret = append([]byte{byte(length)}, ret...)
} else {
ret = append(uint16ToLittleEndianBytes(uint16(length)), ret...)
ret = append([]byte{0xFD}, ret...)
}
}
return ret, nil
}