From 9946c27d674e797b448c95f79e3050de50305818 Mon Sep 17 00:00:00 2001 From: ThomasV Date: Wed, 30 Jan 2013 23:40:53 +0400 Subject: [PATCH] deserialize p2sh transactions --- backends/bitcoind/deserialize.py | 21 +++++++++++---------- utils/__init__.py | 18 +----------------- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/backends/bitcoind/deserialize.py b/backends/bitcoind/deserialize.py index a1e873c..c84a009 100644 --- a/backends/bitcoind/deserialize.py +++ b/backends/bitcoind/deserialize.py @@ -225,8 +225,7 @@ def parse_TxOut(vds, i): d = {} d['value'] = vds.read_int64() scriptPubKey = vds.read_bytes(vds.read_compact_size()) - d['address'] = extract_public_key(scriptPubKey) - #d['script'] = decode_script(scriptPubKey) + d['address'] = get_address_from_output_script(scriptPubKey) d['raw_output_script'] = scriptPubKey.encode('hex') d['index'] = i return d @@ -336,15 +335,10 @@ def match_decoded(decoded, to_match): return True -def extract_public_key(bytes): - decoded = list(script_GetOp(bytes)) - # non-generated TxIn transactions push a signature - # (seventy-something bytes) and then their public key - # (65 bytes) onto the stack: - match = [opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4] - if match_decoded(decoded, match): - return public_key_to_bc_address(decoded[1][1]) + +def get_address_from_output_script(bytes): + decoded = [ x for x in script_GetOp(bytes) ] # The Genesis Block, self-payments, and pay-by-IP-address payments look like: # 65 BYTES:... CHECKSIG @@ -369,5 +363,12 @@ def extract_public_key(bytes): if match_decoded(decoded, match): return hash_160_to_bc_address(decoded[2][1]) + # p2sh + match = [ opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUAL ] + if match_decoded(decoded, match): + addr = hash_160_to_bc_address(decoded[1][1],5) + print "p2sh", addr + return addr + #raise BaseException("address not found in script") see ce35795fb64c268a52324b884793b3165233b1e6d678ccaadf760628ec34d76b return "None" diff --git a/utils/__init__.py b/utils/__init__.py index a01d0e1..f6e64f2 100644 --- a/utils/__init__.py +++ b/utils/__init__.py @@ -90,7 +90,6 @@ def header_from_string(s): ############ functions from pywallet ##################### -addrtype = 0 def hash_160(public_key): @@ -108,7 +107,7 @@ def public_key_to_bc_address(public_key): return hash_160_to_bc_address(hash_160(public_key)) -def hash_160_to_bc_address(h160): +def hash_160_to_bc_address(h160, addrtype = 0): if h160 == 'None': return 'None' vh160 = chr(addrtype) + h160 @@ -194,21 +193,6 @@ def DecodeBase58Check(psz): return key -def PrivKeyToSecret(privkey): - return privkey[9:9+32] - - -def SecretToASecret(secret): - vchIn = chr(addrtype+128) + secret - return EncodeBase58Check(vchIn) - - -def ASecretToSecret(key): - vch = DecodeBase58Check(key) - if vch and vch[0] == chr(addrtype+128): - return vch[1:] - else: - return False ########### end pywallet functions ####################### -- 1.7.1