From 558d9b6bae4b2f0e1f7ed965b4a5e21f00189aea Mon Sep 17 00:00:00 2001 From: Darren Kellenschwiler Date: Thu, 7 Sep 2023 13:02:03 -0500 Subject: [PATCH] aligning the tests to this new concept Signed-off-by: Darren Kellenschwiler --- bscript/interpreter/opcodeparser.go | 18 ++++++++++++++++-- bscript/interpreter/opcodeparser_test.go | 8 ++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/bscript/interpreter/opcodeparser.go b/bscript/interpreter/opcodeparser.go index 0dcb89b1..2fe0e6e1 100644 --- a/bscript/interpreter/opcodeparser.go +++ b/bscript/interpreter/opcodeparser.go @@ -156,9 +156,23 @@ func (p *DefaultOpcodeParser) Parse(s *bscript.Script) (ParsedScript, error) { // This must be the final evaluated opcode, everything after is ignored. if conditionalBlock == 0 { parsedOps = append(parsedOps, parsedOp) - // we add the last data as unformatted blob so that subScript can be reconstructed + // we add any remaining data as an unformatted blob so that subScript can be reconstructed + // but only if there is more length to this script. If it ends in OpReturn then stop there. + totalLen := len(script) + if (i + 1) > totalLen { + return parsedOps, nil + } + if (i + 2) > totalLen { + // we have a single byte of data + parsedOps = append(parsedOps, ParsedOpcode{op: opcode{ + name: "Unformatted Data", + val: script[i+1], + length: 1, + }}) + return parsedOps, nil + } parsedOps = append(parsedOps, ParsedOpcode{op: opcode{ - name: "Unformatteded Data", + name: "Unformatted Data", val: script[i+1], length: len(script[i+1:]), }, Data: script[i+2:]}) diff --git a/bscript/interpreter/opcodeparser_test.go b/bscript/interpreter/opcodeparser_test.go index d8951121..0e4b65b8 100644 --- a/bscript/interpreter/opcodeparser_test.go +++ b/bscript/interpreter/opcodeparser_test.go @@ -69,6 +69,14 @@ func TestParse(t *testing.T) { }, Data: nil, }, + ParsedOpcode{ + op: opcode{ + val: 0, + name: "Unformatted Data", + length: 3, + }, + Data: []byte{36, 220}, + }, }, }, }