From 9f39599182b73d8a298e118916f4ca43a1b947e8 Mon Sep 17 00:00:00 2001 From: 4tochka Date: Mon, 1 Apr 2019 21:22:44 +0300 Subject: [PATCH] fix parse and decode invalid OP_RETURN script --- pybtc/functions/script.py | 78 +++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/pybtc/functions/script.py b/pybtc/functions/script.py index 8d8f546..e73c6f6 100644 --- a/pybtc/functions/script.py +++ b/pybtc/functions/script.py @@ -75,8 +75,9 @@ def parse_script(script, segwit=True): if script[1] == l - 2: return {"nType": 3, "type": "NULL_DATA", "reqSigs": 0, "data": script[2:]} elif script[1] == OPCODE["OP_PUSHDATA1"]: - if script[2] == l - 3 and script[2] <= 80: - return {"nType": 3, "type": "NULL_DATA", "reqSigs": 0, "data": script[3:]} + if l > 2: + if script[2] == l - 3 and script[2] <= 80: + return {"nType": 3, "type": "NULL_DATA", "reqSigs": 0, "data": script[3:]} return {"nType": 8, "type": "NULL_DATA_NON_STANDARD", "reqSigs": 0, "script": script} if script[0] >= 81 and script[0] <= 96: if script[-1] == 174: @@ -183,43 +184,48 @@ def decode_script(script, asm=False): s = 0 result = [] append = result.append - while l - s > 0: - if script[s] < 0x4c and script[s]: - if asm: - append(script[s + 1:s + 1 + script[s]].hex()) - else: - append('[%s]' % script[s]) - s += script[s] + 1 - continue + try: + while l - s > 0: + if script[s] < 0x4c and script[s]: + if asm: + append(script[s + 1:s + 1 + script[s]].hex()) + else: + append('[%s]' % script[s]) + s += script[s] + 1 + continue - if script[s] == OPCODE["OP_PUSHDATA1"]: - ld = script[s + 1] - if asm: - append(script[s + 1:s + 1 + ld].hex()) + if script[s] == OPCODE["OP_PUSHDATA1"]: + if asm: + ld = script[s + 1] + append(script[s + 1:s + 1 + ld].hex()) + else: + append(RAW_OPCODE[script[s]]) + ld = script[s + 1] + append('[%s]' % ld) + s += 1 + script[s + 1] + 1 + elif script[s] == OPCODE["OP_PUSHDATA2"]: + if asm: + ld = unpack('