-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxput.go
340 lines (301 loc) · 7.85 KB
/
xput.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
package bpu
import (
"encoding/base64"
"encoding/hex"
"unicode"
"github.com/bitcoin-sv/go-sdk/script"
)
// "XPut" refers to "inut or output"
// and is a struct that contains the "tape"
// which is divided up according to the "splitConfig"
func (x *XPut) fromScript(config ParseConfig, scrpt *script.Script, idx uint8) error {
var tapeI uint8
var cellI uint8
if scrpt != nil {
parts, err := script.DecodeScript(*scrpt)
if err != nil {
return err
}
requireMet := make(map[int]bool)
splitterRequirementMet := make(map[int]bool)
var prevSplitter bool
var isSplitter bool
// // If we encounter an invalid opcode as the first byte
// // of the script, skip processing the rest of the script
// if len(parts) > 0 && len(parts[0]) == 1 {
// // make sure it exists in the map
// if util.OpCodeValues[parts[0][0]] == "OP_INVALIDOPCODE" || util.OpCodeValues[parts[0][0]] == "" {
// return fmt.Errorf("script begins with invalid opcode: %x", parts[0][0])
// }
// }
// In shallow mode we should take only the first N parts + the last N parts and concat them
// this way we catch p2pkh prefix addresses etc, but we don't have to process the entire script
if config.Mode != nil && *config.Mode != "deep" {
// In shallow mode we truncate anything over 255 pushdatas (not bytes)
if len(parts) > 255 {
// take the first 128 parts and the last 128 parts and concat them
parts = append(parts[:128], parts[len(parts)-128:]...)
}
}
for cIdx, part := range parts {
for configIndex, req := range config.SplitConfig {
var reqOmitted = true
if req.Require != nil {
reqOmitted = false
// Look through previous parts to see if the required token is found
chunksToCheck := parts[:cIdx]
for _, c := range chunksToCheck {
if c.Op == *req.Require || (len(c.Data) == 1 && c.Data[0] == *req.Require) {
requireMet[configIndex] = true
break
}
}
}
splitterRequirementMet[configIndex] = reqOmitted || requireMet[configIndex]
}
if prevSplitter {
cellI = 0
// when multiple consecutive splits ocur this can be incremented too far before splitting
// this will make sure tape is only incremented one past its length
if len(x.Tape) > int(tapeI) {
tapeI++
}
// prevSplitter = false
}
tapeI, cellI, isSplitter, err = x.processChunk(
part, config, uint8(cIdx), idx, splitterRequirementMet, tapeI, cellI,
)
if err != nil {
return err
}
prevSplitter = isSplitter
}
}
return nil
}
func (x *XPut) processChunk(chunk *script.ScriptChunk, o ParseConfig, chunkIndex uint8,
_ uint8, requireMet map[int]bool, tapeI, cellI uint8) (uint8, uint8, bool, error) {
if x.Tape == nil {
x.Tape = make([]Tape, 0)
}
isSplitter := false
var op *uint8
var ops *string
var isOpType = false
var splitter *IncludeType
var h *string
var s *string
var b *string
hexStr := hex.EncodeToString(chunk.Data)
// Check for valid opcodes
var opByte byte
if chunk.Op == 0 || chunk.Op > script.OpPUSHDATA4 {
opByte = chunk.Op
if opCodeStr, ok := script.OpCodeValues[opByte]; ok {
isOpType = true
op = &opByte
ops = &opCodeStr
}
}
// Check if the byte is a printable ASCII character
if !isOpType || unicode.IsPrint(rune(opByte)) {
str := string(chunk.Data)
s = &str
b64 := base64.StdEncoding.EncodeToString(chunk.Data)
b = &b64
h = &hexStr
}
// Split config provided
if o.SplitConfig != nil {
for configIndex, setting := range o.SplitConfig {
if ops != nil {
// Check if this is a manual separator that happens to also be an opcode
var splitOpStrPtr *string
if splitOpByte, ok := script.OpCodeStrings[*ops]; ok {
splitOpStr := string([]byte{splitOpByte})
splitOpStrPtr = &splitOpStr
}
// or an actual op splitter
if setting.Token != nil && (setting.Token.Op != nil && *setting.Token.Op == *op) ||
(setting.Token.Ops != nil && setting.Token.Ops == ops) ||
(setting.Token.S != nil && splitOpStrPtr != nil && *setting.Token.S == *splitOpStrPtr) {
if setting.Require == nil || requireMet[configIndex] {
splitter = setting.Include
isSplitter = true
}
}
} else {
// Script type
if setting.Token != nil && (s != nil && setting.Token.S != nil && *setting.Token.S == *s) ||
(b != nil && setting.Token.B != nil && *setting.Token.B == *b) {
if setting.Require == nil || requireMet[configIndex] {
splitter = setting.Include
isSplitter = true
}
}
}
}
}
if o.Transform != nil {
if isSplitter {
return x.processSplitterChunk(o, splitter, cellI, isOpType, op, ops, s, h, b, chunkIndex, tapeI, hexStr)
}
}
return x.processScriptChunk(o, isOpType, op, ops, s, h, b, chunkIndex, cellI, tapeI, hexStr)
}
// process script chunk (non splitter)
func (x *XPut) processScriptChunk(
o ParseConfig,
isOpType bool,
op *uint8,
ops *string,
s *string,
h *string,
b *string,
chunkIndex uint8,
cellI uint8,
tapeI uint8,
hexStr string,
) (uint8, uint8, bool, error) {
t := *o.Transform
var err error
var cell []Cell
var item *Cell
if isOpType {
item, err = t(
Cell{Op: op, Ops: ops, S: s,
H: h,
B: b, II: chunkIndex, I: cellI},
hexStr,
)
} else {
item, err = t(
Cell{B: b, S: s, H: h, II: chunkIndex, I: cellI},
hexStr,
)
}
if err != nil {
return tapeI, cellI, false, err
}
cellI++
if len(x.Tape) == 0 {
// create a new tape including the cell
cell = append(cell, *item)
outTape := append(x.Tape, Tape{Cell: cell, I: cellI})
x.Tape = outTape
} else {
// create new tape if needed
if len(x.Tape) == int(tapeI) {
x.Tape = append(x.Tape, Tape{
I: tapeI,
Cell: make([]Cell, 0),
})
}
cell = append(x.Tape[tapeI].Cell, *item)
// add the cell to the tape
x.Tape[tapeI].Cell = cell
}
return tapeI, cellI, false, nil
}
// process script chunk (splitter)
func (x *XPut) processSplitterChunk(
o ParseConfig,
splitter *IncludeType,
cellI uint8,
isOpType bool,
op *uint8,
ops *string,
s *string,
h *string,
b *string,
chunkIndex uint8,
tapeI uint8,
hexStr string,
) (uint8, uint8, bool, error) {
var err error
t := *o.Transform
cell := make([]Cell, 0)
var item *Cell
if splitter == nil {
// Don't include the separator by default, just make a new tape and reset cell
// cell = make([]Cell, 0)
cellI = 0
// tapeI++
} else if *splitter == IncludeL {
if isOpType {
item, err = t(Cell{
Op: op,
Ops: ops,
S: s,
H: h,
B: b,
I: cellI,
II: chunkIndex,
}, hexStr)
} else {
item, err = t(Cell{
S: s,
B: b,
H: h,
I: cellI,
II: chunkIndex,
}, hexStr)
}
if err != nil {
return tapeI, cellI, false, err
}
cell = append(cell, *item)
//cellI++
// if theres an existing tape, add item to it...
if len(x.Tape) > 0 {
x.Tape[len(x.Tape)-1].Cell = append(x.Tape[len(x.Tape)-1].Cell, cell...)
} else {
// otherwise make a new tape
outTapes := append(x.Tape, Tape{Cell: cell, I: tapeI})
x.Tape = outTapes
// tapeI++
}
cellI = 0
// cell = make([]Cell, 0)
} else if *splitter == IncludeC {
outTapes := append(x.Tape, Tape{Cell: cell, I: tapeI})
x.Tape = outTapes
//tapeI++
// item, err := t(Cell{
// Op: op,
// Ops: ops,
// S: s,
// H: h,
// B: b,
// I: cellI,
// II: chunkIndex,
// }, hexStr)
// if err != nil {
// return tapeI, cellI, false, err
// }
// cell = []Cell{*item}
cellI = 1
} else if *splitter == IncludeR {
outTapes := append(x.Tape, Tape{Cell: cell, I: tapeI})
x.Tape = outTapes
//tapeI++
item, err := t(Cell{
Op: op,
Ops: ops,
S: s,
H: h,
B: b,
I: cellI,
II: chunkIndex,
}, hexStr)
if err != nil {
return tapeI, cellI, false, err
}
cell = []Cell{*item}
outTapes = append(outTapes, Tape{Cell: cell, I: tapeI})
x.Tape = outTapes
//cell = make([]Cell, 0)
cellI = 0
}
return tapeI, cellI, true, nil
}