aboutsummaryrefslogtreecommitdiff
path: root/test/functional/test_framework
diff options
context:
space:
mode:
authorSebastian Falbesoner <sebastian.falbesoner@gmail.com>2021-07-31 21:23:16 +0200
committerSebastian Falbesoner <sebastian.falbesoner@gmail.com>2021-08-01 19:26:51 +0200
commitca6c154ef116e8d2e0484cdb1af13b34a0c86c17 (patch)
tree4b38a878a5b2fd0f49a2cc0b0e1948b8119b40e9 /test/functional/test_framework
parentf2e41d11097dde1c841bcaa8615886c984f71632 (diff)
downloadbitcoin-ca6c154ef116e8d2e0484cdb1af13b34a0c86c17.tar.xz
test: refactor: remove `hex_str_to_bytes` helper
Use the built-in class method bytes.fromhex() instead, which is available since Python 3.0.
Diffstat (limited to 'test/functional/test_framework')
-rw-r--r--test/functional/test_framework/address.py10
-rw-r--r--test/functional/test_framework/blocktools.py7
-rwxr-xr-xtest/functional/test_framework/messages.py4
-rwxr-xr-xtest/functional/test_framework/script_util.py7
-rw-r--r--test/functional/test_framework/util.py7
-rw-r--r--test/functional/test_framework/wallet.py3
-rwxr-xr-xtest/functional/test_framework/wallet_util.py3
7 files changed, 16 insertions, 25 deletions
diff --git a/test/functional/test_framework/address.py b/test/functional/test_framework/address.py
index 360962b8da..fe733e9368 100644
--- a/test/functional/test_framework/address.py
+++ b/test/functional/test_framework/address.py
@@ -12,7 +12,7 @@ import unittest
from .script import hash256, hash160, sha256, CScript, OP_0
from .segwit_addr import encode_segwit_address
-from .util import assert_equal, hex_str_to_bytes
+from .util import assert_equal
ADDRESS_BCRT1_UNSPENDABLE = 'bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj'
ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR = 'addr(bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj)#juyq9d97'
@@ -33,7 +33,7 @@ def byte_to_base58(b, version):
result = ''
str = b.hex()
str = chr(version).encode('latin-1').hex() + str
- checksum = hash256(hex_str_to_bytes(str)).hex()
+ checksum = hash256(bytes.fromhex(str)).hex()
str += checksum[:8]
value = int('0x' + str, 0)
while value > 0:
@@ -100,7 +100,7 @@ def key_to_p2sh_p2wpkh(key, main=False):
def program_to_witness(version, program, main=False):
if (type(program) is str):
- program = hex_str_to_bytes(program)
+ program = bytes.fromhex(program)
assert 0 <= version <= 16
assert 2 <= len(program) <= 40
assert version > 0 or len(program) in [20, 32]
@@ -121,14 +121,14 @@ def script_to_p2sh_p2wsh(script, main=False):
def check_key(key):
if (type(key) is str):
- key = hex_str_to_bytes(key) # Assuming this is hex string
+ key = bytes.fromhex(key) # Assuming this is hex string
if (type(key) is bytes and (len(key) == 33 or len(key) == 65)):
return key
assert False
def check_script(script):
if (type(script) is str):
- script = hex_str_to_bytes(script) # Assuming this is hex string
+ script = bytes.fromhex(script) # Assuming this is hex string
if (type(script) is bytes or type(script) is CScript):
return script
assert False
diff --git a/test/functional/test_framework/blocktools.py b/test/functional/test_framework/blocktools.py
index 81e931e976..5e6c9f37e8 100644
--- a/test/functional/test_framework/blocktools.py
+++ b/test/functional/test_framework/blocktools.py
@@ -24,7 +24,6 @@ from .messages import (
CTxInWitness,
CTxOut,
hash256,
- hex_str_to_bytes,
ser_uint256,
tx_from_hex,
uint256_from_str,
@@ -214,7 +213,7 @@ def witness_script(use_p2wsh, pubkey):
pkscript = key_to_p2wpkh_script(pubkey)
else:
# 1-of-1 multisig
- witness_script = CScript([OP_1, hex_str_to_bytes(pubkey), OP_1, OP_CHECKMULTISIG])
+ witness_script = CScript([OP_1, bytes.fromhex(pubkey), OP_1, OP_CHECKMULTISIG])
pkscript = script_to_p2wsh_script(witness_script)
return pkscript.hex()
@@ -223,7 +222,7 @@ def create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount):
Optionally wrap the segwit output using P2SH."""
if use_p2wsh:
- program = CScript([OP_1, hex_str_to_bytes(pubkey), OP_1, OP_CHECKMULTISIG])
+ program = CScript([OP_1, bytes.fromhex(pubkey), OP_1, OP_CHECKMULTISIG])
addr = script_to_p2sh_p2wsh(program) if encode_p2sh else script_to_p2wsh(program)
else:
addr = key_to_p2sh_p2wpkh(pubkey) if encode_p2sh else key_to_p2wpkh(pubkey)
@@ -246,7 +245,7 @@ def send_to_witness(use_p2wsh, node, utxo, pubkey, encode_p2sh, amount, sign=Tru
else:
if (insert_redeem_script):
tx = tx_from_hex(tx_to_witness)
- tx.vin[0].scriptSig += CScript([hex_str_to_bytes(insert_redeem_script)])
+ tx.vin[0].scriptSig += CScript([bytes.fromhex(insert_redeem_script)])
tx_to_witness = tx.serialize().hex()
return node.sendrawtransaction(tx_to_witness)
diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py
index 065e8961ae..0523627cef 100755
--- a/test/functional/test_framework/messages.py
+++ b/test/functional/test_framework/messages.py
@@ -30,7 +30,7 @@ import struct
import time
from test_framework.siphash import siphash256
-from test_framework.util import hex_str_to_bytes, assert_equal
+from test_framework.util import assert_equal
MAX_LOCATOR_SZ = 101
MAX_BLOCK_BASE_SIZE = 1000000
@@ -197,7 +197,7 @@ def from_hex(obj, hex_string):
Note that there is no complementary helper like e.g. `to_hex` for the
inverse operation. To serialize a message object to a hex string, simply
use obj.serialize().hex()"""
- obj.deserialize(BytesIO(hex_str_to_bytes(hex_string)))
+ obj.deserialize(BytesIO(bytes.fromhex(hex_string)))
return obj
diff --git a/test/functional/test_framework/script_util.py b/test/functional/test_framework/script_util.py
index 457be6b0e6..5d1d7ea45c 100755
--- a/test/functional/test_framework/script_util.py
+++ b/test/functional/test_framework/script_util.py
@@ -4,7 +4,6 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Useful Script constants and utils."""
from test_framework.script import CScript, hash160, sha256, OP_0, OP_DUP, OP_HASH160, OP_CHECKSIG, OP_EQUAL, OP_EQUALVERIFY
-from test_framework.util import hex_str_to_bytes
# To prevent a "tx-size-small" policy rule error, a transaction has to have a
# non-witness size of at least 82 bytes (MIN_STANDARD_TX_NONWITNESS_SIZE in
@@ -49,7 +48,7 @@ def key_to_p2sh_p2wpkh_script(key, main = False):
def program_to_witness_script(version, program, main = False):
if isinstance(program, str):
- program = hex_str_to_bytes(program)
+ program = bytes.fromhex(program)
assert 0 <= version <= 16
assert 2 <= len(program) <= 40
assert version > 0 or len(program) in [20, 32]
@@ -70,14 +69,14 @@ def script_to_p2sh_p2wsh_script(script, main = False):
def check_key(key):
if isinstance(key, str):
- key = hex_str_to_bytes(key) # Assuming this is hex string
+ key = bytes.fromhex(key) # Assuming this is hex string
if isinstance(key, bytes) and (len(key) == 33 or len(key) == 65):
return key
assert False
def check_script(script):
if isinstance(script, str):
- script = hex_str_to_bytes(script) # Assuming this is hex string
+ script = bytes.fromhex(script) # Assuming this is hex string
if isinstance(script, bytes) or isinstance(script, CScript):
return script
assert False
diff --git a/test/functional/test_framework/util.py b/test/functional/test_framework/util.py
index a9a6adcfc8..54f2fdee21 100644
--- a/test/functional/test_framework/util.py
+++ b/test/functional/test_framework/util.py
@@ -5,7 +5,6 @@
"""Helpful routines for regression testing."""
from base64 import b64encode
-from binascii import unhexlify
from decimal import Decimal, ROUND_DOWN
from subprocess import CalledProcessError
import hashlib
@@ -214,10 +213,6 @@ def count_bytes(hex_string):
return len(bytearray.fromhex(hex_string))
-def hex_str_to_bytes(hex_str):
- return unhexlify(hex_str.encode('ascii'))
-
-
def str_to_b64str(string):
return b64encode(string.encode('utf-8')).decode('ascii')
@@ -517,7 +512,7 @@ def gen_return_txouts():
from .messages import CTxOut
txout = CTxOut()
txout.nValue = 0
- txout.scriptPubKey = hex_str_to_bytes(script_pubkey)
+ txout.scriptPubKey = bytes.fromhex(script_pubkey)
for _ in range(128):
txouts.append(txout)
return txouts
diff --git a/test/functional/test_framework/wallet.py b/test/functional/test_framework/wallet.py
index 47ec6b0be2..609553c6d0 100644
--- a/test/functional/test_framework/wallet.py
+++ b/test/functional/test_framework/wallet.py
@@ -27,7 +27,6 @@ from test_framework.script import (
)
from test_framework.util import (
assert_equal,
- hex_str_to_bytes,
satoshi_round,
)
@@ -73,7 +72,7 @@ class MiniWallet:
self._scriptPubKey = bytes(CScript([pub_key.get_bytes(), OP_CHECKSIG]))
elif mode == MiniWalletMode.ADDRESS_OP_TRUE:
self._address = ADDRESS_BCRT1_P2WSH_OP_TRUE
- self._scriptPubKey = hex_str_to_bytes(self._test_node.validateaddress(self._address)['scriptPubKey'])
+ self._scriptPubKey = bytes.fromhex(self._test_node.validateaddress(self._address)['scriptPubKey'])
def scan_blocks(self, *, start=1, num):
"""Scan the blocks for self._address outputs and add them to self._utxos"""
diff --git a/test/functional/test_framework/wallet_util.py b/test/functional/test_framework/wallet_util.py
index acbc040741..1ee55aa3b7 100755
--- a/test/functional/test_framework/wallet_util.py
+++ b/test/functional/test_framework/wallet_util.py
@@ -27,7 +27,6 @@ from test_framework.script_util import (
script_to_p2sh_script,
script_to_p2wsh_script,
)
-from test_framework.util import hex_str_to_bytes
Key = namedtuple('Key', ['privkey',
'pubkey',
@@ -93,7 +92,7 @@ def get_multisig(node):
addr = node.getaddressinfo(node.getnewaddress())
addrs.append(addr['address'])
pubkeys.append(addr['pubkey'])
- script_code = CScript([OP_2] + [hex_str_to_bytes(pubkey) for pubkey in pubkeys] + [OP_3, OP_CHECKMULTISIG])
+ script_code = CScript([OP_2] + [bytes.fromhex(pubkey) for pubkey in pubkeys] + [OP_3, OP_CHECKMULTISIG])
witness_script = script_to_p2wsh_script(script_code)
return Multisig(privkeys=[node.dumpprivkey(addr) for addr in addrs],
pubkeys=pubkeys,