From 0e85e91a1afb5c795a1d2606e2076c1ffe05ae22 Mon Sep 17 00:00:00 2001 From: 4tochka Date: Thu, 12 Jul 2018 19:43:44 +0400 Subject: [PATCH] added sighash preimage flag --- pybtc/transaction.py | 49 +++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/pybtc/transaction.py b/pybtc/transaction.py index 368d3d9..77b66fe 100644 --- a/pybtc/transaction.py +++ b/pybtc/transaction.py @@ -870,7 +870,7 @@ class Transaction(dict): r += [redeem_script] return r - def sig_hash(self, n, script_pub_key=None, sighash_type=SIGHASH_ALL): + def sig_hash(self, n, script_pub_key=None, sighash_type=SIGHASH_ALL, preimage=False): # check n assert n >= 0 tx_in_count = len(self["vIn"]) @@ -893,7 +893,7 @@ class Transaction(dict): # remove opcode separators script_code = delete_from_script(script_code, BYTE_OPCODE["OP_CODESEPARATOR"]) - preimage = bytearray() + pm = bytearray() if ((sighash_type & 31) == SIGHASH_SINGLE) and (n >= (len(self["vOut"]))): if self["format"] == "raw": @@ -901,8 +901,8 @@ class Transaction(dict): else: return rh2s(b'\x01' + b'\x00' * 31) - preimage += struct.pack(' n and (sighash_type & 31) == SIGHASH_SINGLE: continue if (sighash_type & 31) == SIGHASH_SINGLE and (n != i): - preimage += b'\xff' * 8 + b'\x00' + pm += b'\xff' * 8 + b'\x00' else: - preimage += self["vOut"][i]["value"].to_bytes(8, 'little') - preimage += int_to_var_int(len(script_pub_key)) + script_pub_key + pm += self["vOut"][i]["value"].to_bytes(8, 'little') + pm += int_to_var_int(len(script_pub_key)) + script_pub_key - preimage += self["lockTime"].to_bytes(4, 'little') - preimage += struct.pack(b"= 0 tx_in_count = len(self["vIn"]) @@ -971,9 +973,9 @@ class Transaction(dict): assert type(script_code) == bytes # remove opcode separators - preimage = bytearray() + pm = bytearray() # 1. nVersion of the transaction (4-byte little endian) - preimage += struct.pack('