From 6d15e429c7cc27605e03c4efa6d253e4e6b96cd1 Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Tue, 11 Sep 2018 16:07:39 +0200 Subject: [PATCH] Improve OP_RETURN decoding --- bchain/coins/btc/bitcoinparser.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/bchain/coins/btc/bitcoinparser.go b/bchain/coins/btc/bitcoinparser.go index 02814f89..41f9f2ad 100644 --- a/bchain/coins/btc/bitcoinparser.go +++ b/bchain/coins/btc/bitcoinparser.go @@ -87,8 +87,22 @@ func (p *BitcoinParser) addressToOutputScript(address string) ([]byte, error) { // TryParseOPReturn tries to process OP_RETURN script and return its string representation func TryParseOPReturn(script []byte) string { if len(script) > 1 && script[0] == txscript.OP_RETURN { - l := int(script[1]) - data := script[2:] + // trying 2 variants of OP_RETURN data + // 1) OP_RETURN OP_PUSHDATA1 + // 2) OP_RETURN + var data []byte + var l int + if script[1] == txscript.OP_PUSHDATA1 && len(script) > 2 { + l = int(script[2]) + data = script[3:] + if l != len(data) { + l = int(script[1]) + data = script[2:] + } + } else { + l = int(script[1]) + data = script[2:] + } if l == len(data) { isASCII := true for _, c := range data { @@ -101,7 +115,7 @@ func TryParseOPReturn(script []byte) string { if isASCII { ed = "(" + string(data) + ")" } else { - ed = hex.EncodeToString([]byte{byte(l)}) + " " + hex.EncodeToString(data) + ed = hex.EncodeToString(data) } return "OP_RETURN " + ed }