/
txHash.go
311 lines (276 loc) · 8.51 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
package neoTransaction
import (
"encoding/hex"
"errors"
"github.com/blocktree/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 prefixStr string
//var p2pkhPrefixByte []byte
//var p2wpkhPrefixByte []byte
//
//prefixStr = addressPrefix.Bech32Prefix
//
//p2pkhPrefixByte = addressPrefix.P2PKHPrefix
//p2wpkhPrefixByte = addressPrefix.P2WPKHPrefix
//
//if inType == TypeP2PKH {
// return &TxHash{hex.EncodeToString(hash), 0, &NormalTx{nil, 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 == TypeBech32 {
// return &TxHash{hex.EncodeToString(hash), 0, &NormalTx{Bech32Encode(prefixStr, BTCBech32Alphabet, lockscript[2:]), 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)
return nil, nil, nil
}
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...)
return sigBytes, nil
}
func (t Transaction) getBytesForSig() ([]byte, error) {
sigBytes := []byte{}
var err error
sigBytes, err = t.encodeToBytes()
if err != nil {
return nil, err
}
return sigBytes, nil
}
func (t Transaction) getHashesForSig() ([]TxHash, error) {
hashes := []TxHash{}
if t.Vouts == nil || len(t.Vouts) == 0 {
return nil, errors.New("No output found!")
}
emptyTrans := t.cloneEmpty()
emptyTransBytes, err := emptyTrans.encodeToBytes()
if err != nil {
return nil, err
}
hash := owcrypt.Hash(emptyTransBytes, 0, owcrypt.HASH_ALG_SHA256)
for _, script := range t.Scripts {
pubKey, err := script.GetPubKeyByVerificationScript()
if err != nil {
return nil, err
}
sign, err := script.GetSignatureByInvocationScript()
hashes = append(hashes, TxHash{
hex.EncodeToString(hash),
0,
&NormalTx{
"",
0,
SignaturePubkey{
sign,
pubKey,
},
},
nil,
})
}
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
}