python-bitcoin-utils icon indicating copy to clipboard operation
python-bitcoin-utils copied to clipboard

from_raw on Script class

Open clon19 opened this issue 3 years ago • 4 comments

I think there is an error on the from_raw function on the Script class. I discovered that the redeem script from a P2SH transaction was not given the right codes. If I disable the CODE_OPS [OP_PUSHDATA1, OP_PUSHDATA2 and OP_PUSHDATA4] and increase the index by one on the 4c, 4d, and 4e cases on the from_raw function it works. if not, it pushes a OP_PUSHDATAx on tne commands and doesn´t read the right amount of bytes to read.

clon19 avatar May 18 '22 01:05 clon19

Hi @clon19 ! I will look at it when I have some available time. In the meantime if you are willing make a pull request to fix it.

karask avatar May 23 '22 06:05 karask

I just tried a couple of P2SH and they worked fine.

Could you give me the P2SH that caused the problem? If I can replicate it I can fix it.

karask avatar May 28 '22 07:05 karask

try with tx 7e2aa1a680763d2bbae7fb3976c2a0995366a272c4707a3c2ea589ea843a05f1

this works for me

def from_raw(scriptraw, has_segwit=False): """ Imports a Script commands list from raw hexadecimal data Attributes ---------- txinputraw : string (hex) The hexadecimal raw string representing the Script commands has_segwit : boolean Is the Tx Input segwit or not """ #print("sr",scriptraw) scriptraw = to_bytes(scriptraw) commands = [] index = 0 while index < len(scriptraw): byte = scriptraw[index] if bytes([byte]) in CODE_OPS: commands.append(CODE_OPS[bytes([byte])]) index = index + 1 #handle the 3 special bytes 0x4c,0x4d,0x4e if the transaction is not segwit type elif has_segwit == False and bytes([byte]) == b'\x4c': #print(scriptraw[index + 1]) bytes_to_read = int.from_bytes(scriptraw[index + 1:index + 2], "little") index = index + 2 commands.append(scriptraw[index: index + bytes_to_read].hex()) index = index + bytes_to_read elif has_segwit == False and bytes([byte]) == b'\x4d': bytes_to_read = int.from_bytes(scriptraw[index + 1:index + 3], "little") index = index + 3 commands.append(scriptraw[index: index + bytes_to_read].hex()) index = index + bytes_to_read elif has_segwit == False and bytes([byte]) == b'\x4e': bytes_to_read = int.from_bytes(scriptraw[index + 1:index + 5], "little") index = index + 5 commands.append(scriptraw[index: index + bytes_to_read].hex()) index = index + bytes_to_read else: data_size, size = vi_to_int(scriptraw[index:index + 9]) commands.append(scriptraw[index + size:index + size + data_size].hex()) index = index + data_size + size

    return Script(script=commands)

clon19 avatar Jun 09 '22 01:06 clon19

I am not sure what is the problem.

In the tx you've sent (https://www.blockchain.com/btc/tx/7e2aa1a680763d2bbae7fb3976c2a0995366a272c4707a3c2ea589ea843a05f1) both pkscripts and the sigscript work fine with .from_raw

karask avatar Jun 10 '22 12:06 karask

Closing.

karask avatar Sep 12 '22 07:09 karask