Skip to content

Commit a6ea0e8

Browse files
committed
Allow parsing of op return
1 parent 0f7ceee commit a6ea0e8

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

script/script_chunk.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/binary"
55
"encoding/hex"
66
"fmt"
7+
"slices"
78
)
89

910
type ScriptChunk struct {
@@ -232,10 +233,16 @@ func DecodeScriptHex(s string) ([]*ScriptChunk, error) {
232233
return DecodeScript(b)
233234
}
234235

236+
type DecodeOptions int
237+
238+
const (
239+
DecodeOptionsParseOpReturn DecodeOptions = 0
240+
)
241+
235242
// DecodeScript takes bytes and decodes the opcodes in it
236243
// returning an array of opcode parts (which could be opcodes or data
237244
// pushed to the stack).
238-
func DecodeScript(b []byte) ([]*ScriptChunk, error) {
245+
func DecodeScript(b []byte, options ...DecodeOptions) ([]*ScriptChunk, error) {
239246
var ops []*ScriptChunk
240247
conditionalBlock := 0
241248
for len(b) > 0 {
@@ -249,11 +256,11 @@ func DecodeScript(b []byte) ([]*ScriptChunk, error) {
249256
conditionalBlock--
250257
b = b[1:]
251258
case OpRETURN:
252-
if conditionalBlock == 0 {
259+
if (slices.Contains(options, DecodeOptionsParseOpReturn)) || conditionalBlock > 0 {
260+
b = b[1:]
261+
} else {
253262
op.Data = b
254263
b = nil
255-
} else {
256-
b = b[1:]
257264
}
258265
case OpPUSHDATA1:
259266
if len(b) < 2 {

0 commit comments

Comments
 (0)