Skip to content

Commit

Permalink
Fix compatibility errors
Browse files Browse the repository at this point in the history
  • Loading branch information
alecalve committed Jan 9, 2017
1 parent bdd6338 commit 434b931
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
38 changes: 20 additions & 18 deletions blockchain_parser/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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:
Expand Down
10 changes: 8 additions & 2 deletions blockchain_parser/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 434b931

Please sign in to comment.