mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-09 10:43:19 -05:00
Merge #18965: tests: implement base58_decode
60ed33904c
tests: implement base58_decode (10xcryptodev) Pull request description: implements TODO: def base58_decode ACKs for top commit: ryanofsky: Code review ACK60ed33904c
. Just suggested changes since last review. Thank you for taking suggestions! Tree-SHA512: b3c06b4df041a6d88033cd077a093813a688e42d0b9aa777c715e5fd69cfba7b1bf984428bd98417d3c15232d3d48bc9c163317564f9e1d562db6611c21e2c10
This commit is contained in:
commit
dec067f5a0
2 changed files with 49 additions and 1 deletions
|
@ -5,12 +5,15 @@
|
||||||
"""Encode and decode BASE58, P2PKH and P2SH addresses."""
|
"""Encode and decode BASE58, P2PKH and P2SH addresses."""
|
||||||
|
|
||||||
import enum
|
import enum
|
||||||
|
import unittest
|
||||||
|
|
||||||
from .script import hash256, hash160, sha256, CScript, OP_0
|
from .script import hash256, hash160, sha256, CScript, OP_0
|
||||||
from .util import hex_str_to_bytes
|
from .util import hex_str_to_bytes
|
||||||
|
|
||||||
from . import segwit_addr
|
from . import segwit_addr
|
||||||
|
|
||||||
|
from test_framework.util import assert_equal
|
||||||
|
|
||||||
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
|
||||||
|
@ -41,7 +44,32 @@ def byte_to_base58(b, version):
|
||||||
str = str[2:]
|
str = str[2:]
|
||||||
return result
|
return result
|
||||||
|
|
||||||
# TODO: def base58_decode
|
|
||||||
|
def base58_to_byte(s, verify_checksum=True):
|
||||||
|
if not s:
|
||||||
|
return b''
|
||||||
|
n = 0
|
||||||
|
for c in s:
|
||||||
|
n *= 58
|
||||||
|
assert c in chars
|
||||||
|
digit = chars.index(c)
|
||||||
|
n += digit
|
||||||
|
h = '%x' % n
|
||||||
|
if len(h) % 2:
|
||||||
|
h = '0' + h
|
||||||
|
res = n.to_bytes((n.bit_length() + 7) // 8, 'big')
|
||||||
|
pad = 0
|
||||||
|
for c in s:
|
||||||
|
if c == chars[0]:
|
||||||
|
pad += 1
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
res = b'\x00' * pad + res
|
||||||
|
if verify_checksum:
|
||||||
|
assert_equal(hash256(res[:-4])[:4], res[-4:])
|
||||||
|
|
||||||
|
return res[1:-4], int(res[0])
|
||||||
|
|
||||||
|
|
||||||
def keyhash_to_p2pkh(hash, main = False):
|
def keyhash_to_p2pkh(hash, main = False):
|
||||||
assert len(hash) == 20
|
assert len(hash) == 20
|
||||||
|
@ -100,3 +128,22 @@ def check_script(script):
|
||||||
if (type(script) is bytes or type(script) is CScript):
|
if (type(script) is bytes or type(script) is CScript):
|
||||||
return script
|
return script
|
||||||
assert False
|
assert False
|
||||||
|
|
||||||
|
|
||||||
|
class TestFrameworkScript(unittest.TestCase):
|
||||||
|
def test_base58encodedecode(self):
|
||||||
|
def check_base58(data, version):
|
||||||
|
self.assertEqual(base58_to_byte(byte_to_base58(data, version)), (data, version))
|
||||||
|
|
||||||
|
check_base58(b'\x1f\x8e\xa1p*{\xd4\x94\x1b\xca\tA\xb8R\xc4\xbb\xfe\xdb.\x05', 111)
|
||||||
|
check_base58(b':\x0b\x05\xf4\xd7\xf6l;\xa7\x00\x9fE50)l\x84\\\xc9\xcf', 111)
|
||||||
|
check_base58(b'A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111)
|
||||||
|
check_base58(b'\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111)
|
||||||
|
check_base58(b'\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111)
|
||||||
|
check_base58(b'\0\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 111)
|
||||||
|
check_base58(b'\x1f\x8e\xa1p*{\xd4\x94\x1b\xca\tA\xb8R\xc4\xbb\xfe\xdb.\x05', 0)
|
||||||
|
check_base58(b':\x0b\x05\xf4\xd7\xf6l;\xa7\x00\x9fE50)l\x84\\\xc9\xcf', 0)
|
||||||
|
check_base58(b'A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0)
|
||||||
|
check_base58(b'\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0)
|
||||||
|
check_base58(b'\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0)
|
||||||
|
check_base58(b'\0\0\0A\xc1\xea\xf1\x11\x80%Y\xba\xd6\x1b`\xd6+\x1f\x89|c\x92\x8a', 0)
|
||||||
|
|
|
@ -67,6 +67,7 @@ TEST_EXIT_PASSED = 0
|
||||||
TEST_EXIT_SKIPPED = 77
|
TEST_EXIT_SKIPPED = 77
|
||||||
|
|
||||||
TEST_FRAMEWORK_MODULES = [
|
TEST_FRAMEWORK_MODULES = [
|
||||||
|
"address",
|
||||||
"script",
|
"script",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue