forked from libsv/go-bt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.go
More file actions
318 lines (259 loc) · 7.09 KB
/
script.go
File metadata and controls
318 lines (259 loc) · 7.09 KB
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
package bscript
import (
"encoding/hex"
"errors"
"fmt"
"strings"
"github.com/bitcoinsv/bsvd/bsvec"
"github.com/libsv/go-bt/crypto"
)
// Script type
type Script []byte
// NewFromHexString creates a new script from a hex encoded string.
func NewFromHexString(s string) (*Script, error) {
b, err := hex.DecodeString(s)
if err != nil {
return nil, err
}
return NewFromBytes(b), nil
}
// NewFromBytes wraps a byte slice with the Script type.
func NewFromBytes(b []byte) *Script {
s := Script(b)
return &s
}
// NewFromASM creates a new script from a BitCoin ASM formatted string.
func NewFromASM(str string) (*Script, error) {
sections := strings.Split(str, " ")
s := &Script{}
for _, section := range sections {
if val, ok := opCodeStrings[section]; ok {
s.AppendOpCode(val)
} else {
if err := s.AppendPushDataHexString(section); err != nil {
return nil, errors.New("invalid opcode data")
}
}
}
return s, nil
}
// NewP2PKHFromPubKeyEC takes a public key hex string (in
// compressed format) and creates a P2PKH script from it.
func NewP2PKHFromPubKeyEC(pubKey *bsvec.PublicKey) (*Script, error) {
pubKeyBytes := pubKey.SerializeCompressed()
return NewP2PKHFromPubKeyBytes(pubKeyBytes)
}
// NewP2PKHFromPubKeyStr takes a public key hex string (in
// compressed format) and creates a P2PKH script from it.
func NewP2PKHFromPubKeyStr(pubKey string) (*Script, error) {
pubKeyBytes, err := hex.DecodeString(pubKey)
if err != nil {
return nil, err
}
return NewP2PKHFromPubKeyBytes(pubKeyBytes)
}
// NewP2PKHFromPubKeyBytes takes public key bytes (in
// compressed format) and creates a P2PKH script from it.
func NewP2PKHFromPubKeyBytes(pubKeyBytes []byte) (*Script, error) {
hash := crypto.Hash160(pubKeyBytes)
b := []byte{
OpDUP,
OpHASH160,
0x14,
}
b = append(b, hash...)
b = append(b, OpEQUALVERIFY)
b = append(b, OpCHECKSIG)
s := Script(b)
return &s, nil
}
// NewP2PKHFromPubKeyHashStr takes a public key hex string (in
// compressed format) and creates a P2PKH script from it.
func NewP2PKHFromPubKeyHashStr(pubKeyHash string) (*Script, error) {
hash, err := hex.DecodeString(pubKeyHash)
if err != nil {
return nil, err
}
b := []byte{
OpDUP,
OpHASH160,
0x14,
}
b = append(b, hash...)
b = append(b, OpEQUALVERIFY)
b = append(b, OpCHECKSIG)
s := Script(b)
return &s, nil
}
// NewP2PKHFromAddress takes an address
// and creates a P2PKH script from it.
func NewP2PKHFromAddress(addr string) (*Script, error) {
a, err := NewAddressFromString(addr)
if err != nil {
return nil, err
}
var publicKeyHashBytes []byte
if publicKeyHashBytes, err = hex.DecodeString(a.PublicKeyHash); err != nil {
return nil, err
}
s := &Script{}
s.AppendOpCode(OpDUP)
s.AppendOpCode(OpHASH160)
if err = s.AppendPushData(publicKeyHashBytes); err != nil {
return nil, err
}
s.AppendOpCode(OpEQUALVERIFY)
s.AppendOpCode(OpCHECKSIG)
return s, nil
}
// AppendPushData takes data bytes and appends them to the script
// with proper PUSHDATA prefixes
func (s *Script) AppendPushData(d []byte) error {
p, err := EncodeParts([][]byte{d})
if err != nil {
return err
}
*s = append(*s, p...)
return nil
}
// AppendPushDataHexString takes a hex string and appends them to the
// script with proper PUSHDATA prefixes
func (s *Script) AppendPushDataHexString(str string) error {
h, err := hex.DecodeString(str)
if err != nil {
return err
}
return s.AppendPushData(h)
}
// AppendPushDataString takes a string and appends its UTF-8 encoding
// to the script with proper PUSHDATA prefixes
func (s *Script) AppendPushDataString(str string) error {
return s.AppendPushData([]byte(str))
}
// AppendPushDataArray takes an array of data bytes and appends them
// to the script with proper PUSHDATA prefixes
func (s *Script) AppendPushDataArray(d [][]byte) error {
p, err := EncodeParts(d)
if err != nil {
return err
}
*s = append(*s, p...)
return nil
}
// AppendPushDataStrings takes an array of strings and appends their
// UTF-8 encoding to the script with proper PUSHDATA prefixes
func (s *Script) AppendPushDataStrings(strs []string) error {
dataBytes := make([][]byte, 0)
for _, str := range strs {
strBytes := []byte(str)
dataBytes = append(dataBytes, strBytes)
}
return s.AppendPushDataArray(dataBytes)
}
// AppendOpCode appends an opcode type to the script
func (s *Script) AppendOpCode(o uint8) {
*s = append(*s, o)
}
// ToString returns hex string of script.
func (s *Script) ToString() string { // TODO: change to HexString?
return hex.EncodeToString(*s)
}
// ToASM returns the string ASM opcodes of the script.
func (s *Script) ToASM() (string, error) {
parts, err := DecodeParts(*s)
// if err != nil, we will append [error] to the ASM script below (as done in the node).
var asmScript string
for _, p := range parts {
if len(p) == 1 {
asmScript = asmScript + " " + opCodeValues[p[0]]
} else {
asmScript = asmScript + " " + hex.EncodeToString(p)
}
}
if err != nil {
asmScript += " [error]"
}
return strings.TrimSpace(asmScript), nil
}
// IsP2PKH returns true if this is a pay to pubkey hash output script.
func (s *Script) IsP2PKH() bool {
b := []byte(*s)
return len(b) == 25 &&
b[0] == OpDUP &&
b[1] == OpHASH160 &&
b[2] == 0x14 &&
b[23] == OpEQUALVERIFY &&
b[24] == OpCHECKSIG
}
// IsP2PK returns true if this is a public key output script.
func (s *Script) IsP2PK() bool {
parts, err := DecodeParts(*s)
if err != nil {
return false
}
if len(parts) == 2 &&
len(parts[0]) > 0 &&
parts[1][0] == OpCHECKSIG {
pubkey := parts[0]
version := pubkey[0]
if (version == 0x04 || version == 0x06 || version == 0x07) && len(pubkey) == 65 {
return true
} else if (version == 0x03 || version == 0x02) && len(pubkey) == 33 {
return true
}
}
return false
}
// IsP2SH returns true if this is a p2sh output script.
// TODO: remove all p2sh stuff from repo
func (s *Script) IsP2SH() bool {
b := []byte(*s)
return len(b) == 23 &&
b[0] == OpHASH160 &&
b[1] == 0x14 &&
b[22] == OpEQUAL
}
// IsData returns true if this is a data output script. This
// means the script starts with OP_RETURN or OP_FALSE OP_RETURN.
func (s *Script) IsData() bool {
b := []byte(*s)
return b[0] == 0x6a ||
b[0] == 0x00 && b[1] == 0x6a
}
// IsMultisigOut returns true if this is a multisig output script.
func (s *Script) IsMultisigOut() bool {
parts, err := DecodeParts(*s)
if err != nil {
return false
}
if len(parts) < 3 {
return false
}
if !isSmallIntOp(parts[0][0]) {
return false
}
for i := 1; i < len(parts)-2; i++ {
if len(parts[i]) < 1 {
return false
}
}
return isSmallIntOp(parts[len(parts)-2][0]) &&
parts[len(parts)-1][0] == OpCHECKMULTISIG
}
func isSmallIntOp(opcode byte) bool {
return opcode == OpZERO || (opcode >= OpONE && opcode <= Op16)
}
// GetPublicKeyHash returns a public key hash byte array if the script is a P2PKH script
func (s *Script) GetPublicKeyHash() ([]byte, error) {
if s == nil || len(*s) == 0 {
return nil, fmt.Errorf("script is empty")
}
if (*s)[0] != 0x76 || (*s)[1] != 0xa9 {
return nil, fmt.Errorf("not a P2PKH")
}
parts, err := DecodeParts((*s)[2:])
if err != nil {
return nil, err
}
return parts[0], nil
}