mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-09 10:43:19 -05:00
refactor: move address_to_scriptpubkey to address.py
The COINBASE_MATURITY constant in blocktools.py is imported in wallet.py. However, importing address_to_scriptpubkey to blocktools.py will generate a circular import error. Since the method is related to addresses, it is best to move it to address.py, which will also fix the circular import error. Update imports of address_to_scriptpubkey accordingly.
This commit is contained in:
parent
b759cefe93
commit
4142d19d74
3 changed files with 22 additions and 21 deletions
|
@ -3,12 +3,12 @@
|
||||||
# Distributed under the MIT software license, see the accompanying
|
# Distributed under the MIT software license, see the accompanying
|
||||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
"""Test the scantxoutset rpc call."""
|
"""Test the scantxoutset rpc call."""
|
||||||
|
from test_framework.address import address_to_scriptpubkey
|
||||||
from test_framework.messages import COIN
|
from test_framework.messages import COIN
|
||||||
from test_framework.test_framework import BitcoinTestFramework
|
from test_framework.test_framework import BitcoinTestFramework
|
||||||
from test_framework.util import assert_equal, assert_raises_rpc_error
|
from test_framework.util import assert_equal, assert_raises_rpc_error
|
||||||
from test_framework.wallet import (
|
from test_framework.wallet import (
|
||||||
MiniWallet,
|
MiniWallet,
|
||||||
address_to_scriptpubkey,
|
|
||||||
getnewdestination,
|
getnewdestination,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -21,11 +21,17 @@ from .script import (
|
||||||
taproot_construct,
|
taproot_construct,
|
||||||
)
|
)
|
||||||
from .util import assert_equal
|
from .util import assert_equal
|
||||||
|
from test_framework.script_util import (
|
||||||
|
keyhash_to_p2pkh_script,
|
||||||
|
program_to_witness_script,
|
||||||
|
scripthash_to_p2sh_script,
|
||||||
|
)
|
||||||
from test_framework.segwit_addr import (
|
from test_framework.segwit_addr import (
|
||||||
decode_segwit_address,
|
decode_segwit_address,
|
||||||
encode_segwit_address,
|
encode_segwit_address,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
ADDRESS_BCRT1_UNSPENDABLE = 'bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj'
|
ADDRESS_BCRT1_UNSPENDABLE = 'bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj'
|
||||||
ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR = 'addr(bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj)#juyq9d97'
|
ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR = 'addr(bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj)#juyq9d97'
|
||||||
# Coins sent to this address can be spent with a witness stack of just OP_TRUE
|
# Coins sent to this address can be spent with a witness stack of just OP_TRUE
|
||||||
|
@ -172,6 +178,21 @@ def bech32_to_bytes(address):
|
||||||
return version, bytearray(payload)
|
return version, bytearray(payload)
|
||||||
|
|
||||||
|
|
||||||
|
def address_to_scriptpubkey(address):
|
||||||
|
"""Converts a given address to the corresponding output script (scriptPubKey)."""
|
||||||
|
version, payload = bech32_to_bytes(address)
|
||||||
|
if version is not None:
|
||||||
|
return program_to_witness_script(version, payload) # testnet segwit scriptpubkey
|
||||||
|
payload, version = base58_to_byte(address)
|
||||||
|
if version == 111: # testnet pubkey hash
|
||||||
|
return keyhash_to_p2pkh_script(payload)
|
||||||
|
elif version == 196: # testnet script hash
|
||||||
|
return scripthash_to_p2sh_script(payload)
|
||||||
|
# TODO: also support other address formats
|
||||||
|
else:
|
||||||
|
assert False
|
||||||
|
|
||||||
|
|
||||||
class TestFrameworkScript(unittest.TestCase):
|
class TestFrameworkScript(unittest.TestCase):
|
||||||
def test_base58encodedecode(self):
|
def test_base58encodedecode(self):
|
||||||
def check_base58(data, version):
|
def check_base58(data, version):
|
||||||
|
|
|
@ -13,8 +13,6 @@ from typing import (
|
||||||
Optional,
|
Optional,
|
||||||
)
|
)
|
||||||
from test_framework.address import (
|
from test_framework.address import (
|
||||||
base58_to_byte,
|
|
||||||
bech32_to_bytes,
|
|
||||||
create_deterministic_address_bcrt1_p2tr_op_true,
|
create_deterministic_address_bcrt1_p2tr_op_true,
|
||||||
key_to_p2pkh,
|
key_to_p2pkh,
|
||||||
key_to_p2sh_p2wpkh,
|
key_to_p2sh_p2wpkh,
|
||||||
|
@ -49,9 +47,6 @@ from test_framework.script_util import (
|
||||||
key_to_p2pkh_script,
|
key_to_p2pkh_script,
|
||||||
key_to_p2sh_p2wpkh_script,
|
key_to_p2sh_p2wpkh_script,
|
||||||
key_to_p2wpkh_script,
|
key_to_p2wpkh_script,
|
||||||
keyhash_to_p2pkh_script,
|
|
||||||
program_to_witness_script,
|
|
||||||
scripthash_to_p2sh_script,
|
|
||||||
)
|
)
|
||||||
from test_framework.util import (
|
from test_framework.util import (
|
||||||
assert_equal,
|
assert_equal,
|
||||||
|
@ -412,18 +407,3 @@ def getnewdestination(address_type='bech32m'):
|
||||||
else:
|
else:
|
||||||
assert False
|
assert False
|
||||||
return pubkey, scriptpubkey, address
|
return pubkey, scriptpubkey, address
|
||||||
|
|
||||||
|
|
||||||
def address_to_scriptpubkey(address):
|
|
||||||
"""Converts a given address to the corresponding output script (scriptPubKey)."""
|
|
||||||
version, payload = bech32_to_bytes(address)
|
|
||||||
if version is not None:
|
|
||||||
return program_to_witness_script(version, payload) # testnet segwit scriptpubkey
|
|
||||||
payload, version = base58_to_byte(address)
|
|
||||||
if version == 111: # testnet pubkey hash
|
|
||||||
return keyhash_to_p2pkh_script(payload)
|
|
||||||
elif version == 196: # testnet script hash
|
|
||||||
return scripthash_to_p2sh_script(payload)
|
|
||||||
# TODO: also support other address formats
|
|
||||||
else:
|
|
||||||
assert False
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue