From 2432e59d2a16cb3c2553cf33434f4ddfb9d65b4a Mon Sep 17 00:00:00 2001 From: 4tochka Date: Thu, 14 Mar 2019 14:47:14 +0400 Subject: [PATCH] assert remove --- ...ckchain.py => __to_remove_blockchain__.py} | 0 pybtc/address.py | 5 +- pybtc/block.py | 2 +- pybtc/functions/address.py | 6 +- pybtc/functions/script.py | 4 +- pybtc/transaction.py | 131 +++++++++--------- pybtc/wallet.py | 9 +- 7 files changed, 74 insertions(+), 83 deletions(-) rename pybtc/{blockchain.py => __to_remove_blockchain__.py} (100%) diff --git a/pybtc/blockchain.py b/pybtc/__to_remove_blockchain__.py similarity index 100% rename from pybtc/blockchain.py rename to pybtc/__to_remove_blockchain__.py diff --git a/pybtc/address.py b/pybtc/address.py index ee8354d..0a61a73 100644 --- a/pybtc/address.py +++ b/pybtc/address.py @@ -113,6 +113,7 @@ class PublicKey(): public_key = private_to_public_key(key.key, compressed=key.compressed, hex=False) + #: public key in bytes (bytes) self.key = public_key #: public key in HEX (string) @@ -265,6 +266,6 @@ class ScriptAddress(): a = private_to_public_key(a) if len(a) != 33: raise TypeError("invalid public key list element size") - script += int_to_var_int(len(a)) + a - script += bytes([0x50 + m]) + OP_CHECKMULTISIG + script += b"%s%s" % (int_to_var_int(len(a)), a) + script += b"%s%s" % (bytes([0x50 + m]),OP_CHECKMULTISIG) return cls(script, testnet=testnet, witness_version=witness_version) diff --git a/pybtc/block.py b/pybtc/block.py index 007a1d7..175e2ea 100644 --- a/pybtc/block.py +++ b/pybtc/block.py @@ -1,5 +1,5 @@ from .transaction import Transaction -from struct import pack, unpack +from struct import unpack from .functions import * class Block(dict): diff --git a/pybtc/functions/address.py b/pybtc/functions/address.py index cf60baf..5b48c8f 100644 --- a/pybtc/functions/address.py +++ b/pybtc/functions/address.py @@ -35,7 +35,7 @@ def hash_to_address(address_hash, testnet=False, script_hash=False, witness_vers prefix = TESTNET_ADDRESS_BYTE_PREFIX else: prefix = MAINNET_ADDRESS_BYTE_PREFIX - address_hash = prefix + address_hash + address_hash = b"%s%s" % (prefix, address_hash) address_hash += double_sha256(address_hash)[:4] return encode_base58(address_hash) else: @@ -47,7 +47,7 @@ def hash_to_address(address_hash, testnet=False, script_hash=False, witness_vers prefix = TESTNET_SCRIPT_ADDRESS_BYTE_PREFIX else: prefix = MAINNET_SCRIPT_ADDRESS_BYTE_PREFIX - address_hash = prefix + address_hash + address_hash = b"%s%s" % (prefix, address_hash) address_hash += double_sha256(address_hash)[:4] return encode_base58(address_hash) @@ -265,7 +265,7 @@ def is_address_valid(address, testnet=False): d = rebase_32_to_5(payload) address_hash = d[:-6] checksum = d[-6:] - checksum2 = bech32_polymod(stripped_prefix + address_hash + b"\x00" * 6) + checksum2 = bech32_polymod(b"%s%s%s" % (stripped_prefix, address_hash, b"\x00" * 6)) checksum2 = rebase_8_to_5(checksum2.to_bytes(5, "big"))[2:] if checksum != checksum2: return False diff --git a/pybtc/functions/script.py b/pybtc/functions/script.py index d7af383..9c52fa2 100644 --- a/pybtc/functions/script.py +++ b/pybtc/functions/script.py @@ -13,7 +13,7 @@ from .address import hash_to_address def public_key_to_pubkey_script(key, hex=True): if isinstance(key, str): - key = bytes.from_hex(key) + key = bytes.fromhex(key) s = b"%s%s%s" % (bytes([len(key)]), key, OP_CHECKSIG) return s.hex() if hex else s @@ -218,7 +218,7 @@ def delete_from_script(script, sub_script): """ Decode OPCODE or subscript from script. - :param script: traget script in bytes or HEX encoded string. + :param script: target script in bytes or HEX encoded string. :param sub_script: sub_script which is necessary to remove from target script in bytes or HEX encoded string. :return: script in bytes or HEX encoded string corresponding to the format of target script. """ diff --git a/pybtc/transaction.py b/pybtc/transaction.py index 3237e3f..a7b3897 100644 --- a/pybtc/transaction.py +++ b/pybtc/transaction.py @@ -115,7 +115,7 @@ class Transaction(dict): if sw: self["segwit"] = True self["hash"] = double_sha256(b) - self["txId"] = double_sha256(b[:4] + b[6:sw] + b[-4:]) + self["txId"] = double_sha256(b"%s%s%s" % (b[:4], b[6:sw],b[-4:])) else: self["segwit"] = False self["txId"] = double_sha256(b) @@ -125,7 +125,7 @@ class Transaction(dict): def decode(self, testnet=None): """ - change Transacion object representation to "decoded" human readable format + change Transaction object representation to "decoded" human readable format :param bool testnet: (optional) address type for "decoded" transaction representation, by default None. if None used type from transaction property "format". @@ -433,6 +433,8 @@ class Transaction(dict): script = address_to_script(address) elif type(address) in (Address, ScriptAddress): script = address_to_script(address.address) + else: + raise TypeError("address invalid") if script_pub_key: if script_pub_key != script: raise Exception("address not match script") @@ -478,12 +480,14 @@ class Transaction(dict): if address is None and script_pub_key is None: raise Exception("unable to add output, address or script required") if type(amount) != int: - raise Exception("unable to add output, amount type error") - assert amount >= 0 and amount <= MAX_AMOUNT + raise TypeError("unable to add output, amount type error") + if amount < 0 or amount > MAX_AMOUNT: + raise Exception("unable to add output, amount value error") if script_pub_key: - if type(script_pub_key) == str: + if isinstance(script_pub_key, str): script_pub_key = bytes.fromhex(script_pub_key) - assert type(script_pub_key) == bytes + if not isinstance(script_pub_key, bytes): + raise TypeError("unable to add output, script_pub_key type error") else: if type(address) == Address: address = address.address @@ -891,59 +895,54 @@ class Transaction(dict): return r 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"]) - - if n >= tx_in_count: - if self["format"] == "raw": - return b'\x01' + b'\x00' * 31 - else: - return rh2s(b'\x01' + b'\x00' * 31) + try: + self["vIn"][n] + except: + raise Exception("sig_hash error, input not exist") # check script_pub_key for input if script_pub_key is not None: script_code = script_pub_key else: - assert "scriptPubKey" in self["vIn"][n] + if "scriptPubKey" not in self["vIn"][n]: + raise Exception("sig_hash error, scriptPubKey required") script_code = self["vIn"][n]["scriptPubKey"] - if type(script_code) == str: + if isinstance(script_code, str): script_code = bytes.fromhex(script_code) - assert type(script_code) == bytes + if not isinstance(script_code,bytes): + raise Exception("sig_hash error, script_code type error") # remove opcode separators - script_code = delete_from_script(script_code, BYTE_OPCODE["OP_CODESEPARATOR"]) - pm = bytearray() - if ((sighash_type & 31) == SIGHASH_SINGLE) and (n >= (len(self["vOut"]))): if self["format"] == "raw": - return b'\x01' + b'\x00' * 31 - else: - return rh2s(b'\x01' + b'\x00' * 31) - - pm += struct.pack(' n and (sighash_type & 31) == SIGHASH_SINGLE: continue if (sighash_type & 31) == SIGHASH_SINGLE and (n != i): - pm += b'\xff' * 8 + b'\x00' + pm += b"%s%s" % (b'\xff' * 8, b'\x00') else: - pm += self["vOut"][i]["value"].to_bytes(8, 'little') - pm += int_to_var_int(len(script_pub_key)) + script_pub_key - - pm += self["lockTime"].to_bytes(4, 'little') - pm += struct.pack(b"= 0 - tx_in_count = len(self["vIn"]) - - if n >= tx_in_count: - if self["format"] == "raw": - return b'\x01' + b'\x00' * 31 - else: - return rh2s(b'\x01' + b'\x00' * 31) + try: + self["vIn"][n] + except: + raise Exception("sig_hash error, input not exist") # check script_pub_key for input if script_pub_key is not None: script_code = script_pub_key else: - assert "scriptPubKey" in self["vIn"][n] + if "scriptPubKey" not in self["vIn"][n]: + raise Exception("sig_hash error, scriptPubKey required") script_code = self["vIn"][n]["scriptPubKey"] - if type(script_code) == str: + if isinstance(script_code, str): script_code = bytes.fromhex(script_code) - assert type(script_code) == bytes + if not isinstance(script_code,bytes): + raise Exception("sig_hash error, script_code type error") # remove opcode separators pm = bytearray() @@ -1009,11 +1004,11 @@ class Transaction(dict): if type(tx_id) == str: tx_id = s2rh(tx_id) if not (sighash_type & SIGHASH_ANYONECANPAY): - hp += tx_id + struct.pack('