From 434b931ed6ed7d749d2b306f7a46d114907ef052 Mon Sep 17 00:00:00 2001 From: Antoine Le Calvez Date: Mon, 9 Jan 2017 01:26:44 +0000 Subject: [PATCH] Fix compatibility errors --- blockchain_parser/script.py | 38 +++++++++++++++++++------------------ blockchain_parser/utils.py | 10 ++++++++-- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/blockchain_parser/script.py b/blockchain_parser/script.py index 8085281..ed32d43 100644 --- a/blockchain_parser/script.py +++ b/blockchain_parser/script.py @@ -11,6 +11,7 @@ from bitcoin.core.script import * from binascii import b2a_hex +from .utils import to_int def is_public_key(hex_data): @@ -22,11 +23,11 @@ def is_public_key(hex_data): return False # Uncompressed public key - if len(hex_data) == 65 and hex_data[0] == 4: + if len(hex_data) == 65 and to_int(hex_data[0]) == 4: return True # Compressed public key - if len(hex_data) == 33 and hex_data[0] in [2, 3]: + if len(hex_data) == 33 and to_int(hex_data[0]) in [2, 3]: return True return False @@ -65,15 +66,12 @@ def operations(self): - a CScriptOP - bytes data pushed to the stack - an int pushed to the stack - If the script is invalid (some coinbase scripts are), a list containing - one operation (INVALID_SCRIPT) is returned + If the script is invalid (some coinbase scripts are), an exception is + thrown """ if self._operations is None: # Some coinbase scripts are garbage, they could not be valid - try: - self._operations = list(self.script) - except CScriptInvalidError: - self._operations = ["INVALID_SCRIPT"] + self._operations = list(self.script) return self._operations @@ -82,12 +80,16 @@ def value(self): """Returns a string representation of the script""" if self._value is None: representations = [] - for operation in self.operations: - if type(operation) == bytes: - representations.append(b2a_hex(operation).decode("ascii")) - else: - representations.append(str(operation)) - self._value = " ".join(representations) + try: + for operation in self.operations: + if isinstance(operation, bytes): + representations.append(b2a_hex(operation).decode("ascii")) + else: + representations.append(str(operation)) + + self._value = " ".join(representations) + except CScriptInvalidError as e: + self._value = "INVALID_SCRIPT" return self._value @@ -104,10 +106,10 @@ def is_pubkey(self): def is_pubkeyhash(self): return len(self.hex) == 25 \ - and self.hex[0] == OP_DUP \ - and self.hex[1] == OP_HASH160 \ - and self.hex[-2] == OP_EQUALVERIFY \ - and self.hex[-1] == OP_CHECKSIG + and self.operations[0] == OP_DUP \ + and self.operations[1] == OP_HASH160 \ + and self.operations[-2] == OP_EQUALVERIFY \ + and self.operations[-1] == OP_CHECKSIG def is_multisig(self): if len(self.operations) < 4: diff --git a/blockchain_parser/utils.py b/blockchain_parser/utils.py index 9b083bf..5de93f3 100644 --- a/blockchain_parser/utils.py +++ b/blockchain_parser/utils.py @@ -11,10 +11,16 @@ from binascii import hexlify import hashlib - +import sys import struct +if sys.version > '3': + to_int = lambda x: int(x) +else: + to_int = ord + + def btc_ripemd160(data): h1 = hashlib.sha256(data).digest() r160 = hashlib.new("ripemd160") @@ -42,7 +48,7 @@ def decode_uint64(data): def decode_varint(data): assert(len(data) > 0) - size = int(data[0]) + size = to_int(data[0]) assert(size <= 255) if size < 253: