mirror of
https://github.com/bitcoin/bitcoin.git
synced 2024-05-17 23:56:39 +00:00
[tests] Rename segwit encode and decode functions
These functions can be exported to other modules, so be explicit that they're encoding and decoding segwit addresses
This commit is contained in:
parent
e4557133f5
commit
011e784f74
@ -8,9 +8,9 @@ import enum
|
|||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from .script import hash256, hash160, sha256, CScript, OP_0
|
from .script import hash256, hash160, sha256, CScript, OP_0
|
||||||
|
from .segwit_addr import encode_segwit_address
|
||||||
from .util import hex_str_to_bytes
|
from .util import hex_str_to_bytes
|
||||||
|
|
||||||
from . import segwit_addr
|
|
||||||
|
|
||||||
from test_framework.util import assert_equal
|
from test_framework.util import assert_equal
|
||||||
|
|
||||||
@ -100,7 +100,7 @@ def program_to_witness(version, program, main = False):
|
|||||||
assert 0 <= version <= 16
|
assert 0 <= version <= 16
|
||||||
assert 2 <= len(program) <= 40
|
assert 2 <= len(program) <= 40
|
||||||
assert version > 0 or len(program) in [20, 32]
|
assert version > 0 or len(program) in [20, 32]
|
||||||
return segwit_addr.encode("bc" if main else "bcrt", version, program)
|
return encode_segwit_address("bc" if main else "bcrt", version, program)
|
||||||
|
|
||||||
def script_to_p2wsh(script, main = False):
|
def script_to_p2wsh(script, main = False):
|
||||||
script = check_script(script)
|
script = check_script(script)
|
||||||
|
@ -84,7 +84,7 @@ def convertbits(data, frombits, tobits, pad=True):
|
|||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
def decode(hrp, addr):
|
def decode_segwit_address(hrp, addr):
|
||||||
"""Decode a segwit address."""
|
"""Decode a segwit address."""
|
||||||
hrpgot, data = bech32_decode(addr)
|
hrpgot, data = bech32_decode(addr)
|
||||||
if hrpgot != hrp:
|
if hrpgot != hrp:
|
||||||
@ -99,10 +99,10 @@ def decode(hrp, addr):
|
|||||||
return (data[0], decoded)
|
return (data[0], decoded)
|
||||||
|
|
||||||
|
|
||||||
def encode(hrp, witver, witprog):
|
def encode_segwit_address(hrp, witver, witprog):
|
||||||
"""Encode a segwit address."""
|
"""Encode a segwit address."""
|
||||||
ret = bech32_encode(hrp, [witver] + convertbits(witprog, 8, 5))
|
ret = bech32_encode(hrp, [witver] + convertbits(witprog, 8, 5))
|
||||||
if decode(hrp, ret) == (None, None):
|
if decode_segwit_address(hrp, ret) == (None, None):
|
||||||
return None
|
return None
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
@ -111,8 +111,8 @@ class TestFrameworkScript(unittest.TestCase):
|
|||||||
def test_python_bech32(addr):
|
def test_python_bech32(addr):
|
||||||
hrp = addr[:4]
|
hrp = addr[:4]
|
||||||
self.assertEqual(hrp, "bcrt")
|
self.assertEqual(hrp, "bcrt")
|
||||||
(witver, witprog) = decode(hrp, addr)
|
(witver, witprog) = decode_segwit_address(hrp, addr)
|
||||||
self.assertEqual(encode(hrp, witver, witprog), addr)
|
self.assertEqual(encode_segwit_address(hrp, witver, witprog), addr)
|
||||||
|
|
||||||
# P2WPKH
|
# P2WPKH
|
||||||
test_python_bech32('bcrt1qthmht0k2qnh3wy7336z05lu2km7emzfpm3wg46')
|
test_python_bech32('bcrt1qthmht0k2qnh3wy7336z05lu2km7emzfpm3wg46')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user