mirror of
https://github.com/dashpay/dash.git
synced 2024-12-26 20:42:59 +01:00
Merge #19230: [TESTS] Move base58 to own module to break circular dependency
c75de5da5fdd3a304f9da3d8a2e0370d1723ddd0 [TESTS] Move base58 to own module to break circular dependency (Pieter Wuille) Pull request description: I encountered difficulties with the test framework in #17977. This fixes them, and I think the change is generally useful. ACKs for top commit: laanwj: Code review ACK c75de5da5fdd3a304f9da3d8a2e0370d1723ddd0 MarcoFalke: ACK c75de5da5fdd3a304f9da3d8a2e0370d1723ddd0 according to --color-moved=dimmed-zebra this is a move-only apart from the imports 👒 Tree-SHA512: 9e0493de3e279074f0c70e92c959b73ae30479ad6f2083a3c6bbf4b0191d65ef94854559a5b7c904f5dadc5e93129ed00f6dc0a8ccce6ba7921cd45f7119f74b
This commit is contained in:
parent
6668e21e1f
commit
eaf31ad2be
@ -8,61 +8,16 @@
|
|||||||
#
|
#
|
||||||
# This file encodes and decodes BASE58 P2PKH and P2SH addresses
|
# This file encodes and decodes BASE58 P2PKH and P2SH addresses
|
||||||
#
|
#
|
||||||
import unittest
|
|
||||||
|
|
||||||
from .script import hash256, hash160, CScript
|
from .base58 import byte_to_base58
|
||||||
|
from .script import hash160, CScript
|
||||||
from .util import hex_str_to_bytes
|
from .util import hex_str_to_bytes
|
||||||
from test_framework.util import assert_equal
|
|
||||||
|
|
||||||
# Note unlike in bitcoin, this address isn't bech32 since we don't (at this time) support bech32.
|
# Note unlike in bitcoin, this address isn't bech32 since we don't (at this time) support bech32.
|
||||||
ADDRESS_BCRT1_UNSPENDABLE = 'yVg3NBUHNEhgDceqwVUjsZHreC5PBHnUo9'
|
ADDRESS_BCRT1_UNSPENDABLE = 'yVg3NBUHNEhgDceqwVUjsZHreC5PBHnUo9'
|
||||||
ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR = 'addr(yVg3NBUHNEhgDceqwVUjsZHreC5PBHnUo9)#e5kt0jtk'
|
ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR = 'addr(yVg3NBUHNEhgDceqwVUjsZHreC5PBHnUo9)#e5kt0jtk'
|
||||||
ADDRESS_BCRT1_P2SH_OP_TRUE = '8zJctvfrzGZ5s1zQ3kagwyW1DsPYSQ4V2P'
|
ADDRESS_BCRT1_P2SH_OP_TRUE = '8zJctvfrzGZ5s1zQ3kagwyW1DsPYSQ4V2P'
|
||||||
|
|
||||||
chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
|
||||||
|
|
||||||
|
|
||||||
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()
|
|
||||||
str += checksum[:8]
|
|
||||||
value = int('0x'+str,0)
|
|
||||||
while value > 0:
|
|
||||||
result = chars[value % 58] + result
|
|
||||||
value //= 58
|
|
||||||
while (str[:2] == '00'):
|
|
||||||
result = chars[0] + result
|
|
||||||
str = str[2:]
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
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
|
||||||
@ -95,22 +50,3 @@ 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)
|
|
||||||
|
70
test/functional/test_framework/base58.py
Normal file
70
test/functional/test_framework/base58.py
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# Copyright (c) 2016-2020 The Bitcoin Core developers
|
||||||
|
# Distributed under the MIT software license, see the accompanying
|
||||||
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
"""Encode BASE58."""
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from .messages import hash256
|
||||||
|
from .util import hex_str_to_bytes, assert_equal
|
||||||
|
|
||||||
|
chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
||||||
|
|
||||||
|
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()
|
||||||
|
str += checksum[:8]
|
||||||
|
value = int('0x'+str,0)
|
||||||
|
while value > 0:
|
||||||
|
result = chars[value % 58] + result
|
||||||
|
value //= 58
|
||||||
|
while (str[:2] == '00'):
|
||||||
|
result = chars[0] + result
|
||||||
|
str = str[2:]
|
||||||
|
return result
|
||||||
|
|
||||||
|
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])
|
||||||
|
|
||||||
|
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)
|
@ -14,7 +14,7 @@ import random
|
|||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from test_framework.crypto import secp256k1
|
from test_framework.crypto import secp256k1
|
||||||
from .address import byte_to_base58
|
from .base58 import byte_to_base58
|
||||||
|
|
||||||
# Order of the secp256k1 curve
|
# Order of the secp256k1 curve
|
||||||
ORDER = secp256k1.GE.ORDER
|
ORDER = secp256k1.GE.ORDER
|
||||||
|
@ -70,7 +70,7 @@ TEST_EXIT_SKIPPED = 77
|
|||||||
# List of framework modules containing unit tests. Should be kept in sync with
|
# List of framework modules containing unit tests. Should be kept in sync with
|
||||||
# the output of `git grep unittest.TestCase ./test/functional/test_framework`
|
# the output of `git grep unittest.TestCase ./test/functional/test_framework`
|
||||||
TEST_FRAMEWORK_MODULES = [
|
TEST_FRAMEWORK_MODULES = [
|
||||||
"address",
|
"base58",
|
||||||
"crypto.bip324_cipher",
|
"crypto.bip324_cipher",
|
||||||
"blocktools",
|
"blocktools",
|
||||||
"crypto.chacha20",
|
"crypto.chacha20",
|
||||||
|
Loading…
Reference in New Issue
Block a user