diff --git a/pybtc/__init__.py b/pybtc/__init__.py index 3e923ba..7a69692 100644 --- a/pybtc/__init__.py +++ b/pybtc/__init__.py @@ -5,4 +5,3 @@ from .transaction import * from .block import * from .address import * from .wallet import * -version = "2.0.1" diff --git a/pybtc/functions/address.py b/pybtc/functions/address.py index 5ec30e9..cf60baf 100644 --- a/pybtc/functions/address.py +++ b/pybtc/functions/address.py @@ -1,16 +1,10 @@ import os import sys -import time -import random -from secp256k1 import ffi parentPath = os.path.abspath("../..") if parentPath not in sys.path: sys.path.insert(0, parentPath) -from pybtc.constants import * -from .hash import * from pybtc.opcodes import * -from .encode import * from .key import * from .hash import * diff --git a/pybtc/functions/bip32.py b/pybtc/functions/bip32.py index a2274c0..c9a77c6 100644 --- a/pybtc/functions/bip32.py +++ b/pybtc/functions/bip32.py @@ -1,13 +1,4 @@ -import os -import hmac import struct -from secp256k1 import ffi -from struct import pack, unpack -from hashlib import pbkdf2_hmac -from binascii import hexlify, unhexlify -from pybtc.constants import * -from .encode import * -from .hash import * from .key import * diff --git a/pybtc/functions/bip39_mnemonic.py b/pybtc/functions/bip39_mnemonic.py index 6a4303d..af03b3d 100644 --- a/pybtc/functions/bip39_mnemonic.py +++ b/pybtc/functions/bip39_mnemonic.py @@ -1,8 +1,6 @@ import os import sys import time -import random -from secp256k1 import ffi parentPath = os.path.abspath("../..") if parentPath not in sys.path: sys.path.insert(0, parentPath) diff --git a/pybtc/functions/key.py b/pybtc/functions/key.py index 345a3ce..68be77c 100644 --- a/pybtc/functions/key.py +++ b/pybtc/functions/key.py @@ -1,14 +1,11 @@ import os import sys -import time -import random from secp256k1 import ffi parentPath = os.path.abspath("../..") if parentPath not in sys.path: sys.path.insert(0, parentPath) from pybtc.constants import * -from .hash import * from .encode import * from .hash import * from .bip39_mnemonic import generate_entropy diff --git a/pybtc/functions/script.py b/pybtc/functions/script.py index 40f84ac..6ef8934 100644 --- a/pybtc/functions/script.py +++ b/pybtc/functions/script.py @@ -1,8 +1,5 @@ import os import sys -import time -import random -import struct from secp256k1 import ffi parentPath = os.path.abspath("../..") if parentPath not in sys.path: @@ -10,11 +7,9 @@ if parentPath not in sys.path: from pybtc.opcodes import * from pybtc.constants import * -from .hash import * -from .encode import * from .tools import * from .hash import * - +from .address import hash_to_address def public_key_to_pubkey_script(key, hex=True): if isinstance(key, str): @@ -132,6 +127,26 @@ def parse_script(script, segwit=True): return {"nType": 7, "type": "NON_STANDARD", "reqSigs": req_sigs, "script": script} +def script_to_address(script, testnet=False): + """ + Decode script to address (base58/bech32 format). + + :param script: script in bytes string or HEX encoded string format. + :param testnet: (optional) flag for testnet network, by default is False. + :return: address in base58/bech32 format or None. + """ + d = parse_script(script) + if "addressHash" in d: + witness_version = 0 if d["nType"] in (5, 6) else None + script_hash = True if d["nType"] in (1, 6) else False + return hash_to_address(d["addressHash"], testnet=testnet, + script_hash=script_hash, witness_version=witness_version) + return None + + + + + def decode_script(script, asm=False): """ Decode script to ASM format or to human readable OPCODES string. diff --git a/pybtc/test/__init__.py b/pybtc/test/__init__.py index ae0f618..48b5123 100644 --- a/pybtc/test/__init__.py +++ b/pybtc/test/__init__.py @@ -8,6 +8,7 @@ from .transaction_constructor import * from .sighash import * from .block import * from .mnemonic import * +from .script_functions import * # from .script_deserialize import * # from .create_transaction import * diff --git a/pybtc/test/script_functions.py b/pybtc/test/script_functions.py new file mode 100644 index 0000000..fdab7c4 --- /dev/null +++ b/pybtc/test/script_functions.py @@ -0,0 +1,28 @@ +import unittest +import os, sys +parentPath = os.path.abspath("..") +if parentPath not in sys.path: + sys.path.insert(0, parentPath) + +from pybtc.functions import * + + + +class ScriptFunctionsTests(unittest.TestCase): + @classmethod + def setUpClass(cls): + print("\nTesting script functions:\n") + + def test_script_to_address(self): + self.assertEqual(script_to_address("76a914f18e5346e6efe17246306ce82f11ca53542fe00388ac"), + "1P2EMAeiSJEfCrtjC6ovdWaGWW1Mb6azpX") + self.assertEqual(script_to_address("a9143f4eecba122ad73039d481c8d37f99cb4f887cd887"), + "37Tm3Qz8Zw2VJrheUUhArDAoq58S6YrS3g") + self.assertEqual(script_to_address("76a914a307d67484911deee457779b17505cedd20e1fe988ac", testnet=1), + "mvNyptwisQTmwL3vN8VMaVUrA3swVCX83c") + self.assertEqual(script_to_address("0014751e76e8199196d454941c45d1b3a323f1433bd6", testnet=0), + "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4") + self.assertEqual(script_to_address("0020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d"), + "bc1qwqdg6squsna38e46795at95yu9atm8azzmyvckulcc7kytlcckxswvvzej") + +