script_to_address function

remove unnecessary imports
This commit is contained in:
4tochka 2019-03-09 15:39:48 +04:00
parent 20fbe331e6
commit c3639b7cd8
8 changed files with 50 additions and 27 deletions

View File

@ -5,4 +5,3 @@ from .transaction import *
from .block import *
from .address import *
from .wallet import *
version = "2.0.1"

View File

@ -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 *

View File

@ -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 *

View File

@ -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)

View File

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

View File

@ -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.

View File

@ -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 *

View File

@ -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")