mirror of
https://github.com/dashpay/dash.git
synced 2024-12-26 04:22:55 +01:00
99dec80fbb
825fcae484f31182041dfacbf820e818d759b130 [tests] Replace bytes literals with hex literals (John Newbery) 64eca45100536579a3849631e59d4277bbc25be1 [tests] Fix pep8 style violations in address.py (John Newbery) b230f8b3f3adcb1e2ae299094f9ae0a8bc7cc3d0 [tests] Correct docstring for address.py (John Newbery) ea70e6a2ca0e183ef40cdb9b3b86f39e94366015 [tests] Tidy up imports in address.py (John Newbery) 7f639df0b8a15aaeccedab00b634925f568c2c9a [tests] Remove unused optional verify_checksum parameter (John Newbery) 011e784f74411bd5d5dbccfd3af39e0937fd8933 [tests] Rename segwit encode and decode functions (John Newbery) e4557133f595f357df5e16ae4f2f19c579631396 [tests] Move bech32 unit tests to test framework (John Newbery) Pull request description: Lots of small fixes: - moving unit tests to test_framework implementation files - renaming functions to be clearer - removing multiple imports - removing unreadable byte literals from the code - fixing pep8 violations - correcting out-of-date docstring ACKs for top commit: jonatack: re-ACK 825fcae484f31182041dfacbf820e818d759b130 per `git range-diff a0a422c 7edcdcd 825fcae` and verified `wallet_address_types.py` and `wallet_basic.py --descriptors` (the failure on one travis job) are green locally. MarcoFalke: ACK 825fcae484f31182041dfacbf820e818d759b130 fanquake: ACK 825fcae484f31182041dfacbf820e818d759b130 - looks ok to me. Tree-SHA512: aea509c27c1bcb94bef11205b6a79836c39c62249672815efc9822f411bc2e2336ceb3d72b3b861c3f4054a08e16edb28c6edd3aa5eff72eec1d60ea6ca82dc4
122 lines
4.0 KiB
Python
122 lines
4.0 KiB
Python
#!/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.
|
|
|
|
#
|
|
# address.py
|
|
#
|
|
# This file encodes and decodes BASE58 P2PKH and P2SH addresses
|
|
#
|
|
|
|
import unittest
|
|
|
|
from .script import hash160, hash256, CScript
|
|
from .util import assert_equal, hex_str_to_bytes
|
|
|
|
# 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_DESCRIPTOR = 'addr(yVg3NBUHNEhgDceqwVUjsZHreC5PBHnUo9)#e5kt0jtk'
|
|
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):
|
|
"""Converts a base58-encoded string to its data and version.
|
|
|
|
Throws if the base58 checksum is invalid."""
|
|
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
|
|
|
|
# Assert if the checksum is invalid
|
|
assert_equal(hash256(res[:-4])[:4], res[-4:])
|
|
|
|
return res[1:-4], int(res[0])
|
|
|
|
|
|
def keyhash_to_p2pkh(hash, main=False):
|
|
assert len(hash) == 20
|
|
version = 76 if main else 140
|
|
return byte_to_base58(hash, version)
|
|
|
|
def scripthash_to_p2sh(hash, main=False):
|
|
assert len(hash) == 20
|
|
version = 16 if main else 19
|
|
return byte_to_base58(hash, version)
|
|
|
|
def key_to_p2pkh(key, main=False):
|
|
key = check_key(key)
|
|
return keyhash_to_p2pkh(hash160(key), main)
|
|
|
|
def script_to_p2sh(script, main=False):
|
|
script = check_script(script)
|
|
return scripthash_to_p2sh(hash160(script), main)
|
|
|
|
def check_key(key):
|
|
if (type(key) is str):
|
|
key = hex_str_to_bytes(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
|
|
if (type(script) is bytes or type(script) is CScript):
|
|
return script
|
|
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(bytes.fromhex('1f8ea1702a7bd4941bca0941b852c4bbfedb2e05'), 111)
|
|
check_base58(bytes.fromhex('3a0b05f4d7f66c3ba7009f453530296c845cc9cf'), 111)
|
|
check_base58(bytes.fromhex('41c1eaf111802559bad61b60d62b1f897c63928a'), 111)
|
|
check_base58(bytes.fromhex('0041c1eaf111802559bad61b60d62b1f897c63928a'), 111)
|
|
check_base58(bytes.fromhex('000041c1eaf111802559bad61b60d62b1f897c63928a'), 111)
|
|
check_base58(bytes.fromhex('00000041c1eaf111802559bad61b60d62b1f897c63928a'), 111)
|
|
check_base58(bytes.fromhex('1f8ea1702a7bd4941bca0941b852c4bbfedb2e05'), 0)
|
|
check_base58(bytes.fromhex('3a0b05f4d7f66c3ba7009f453530296c845cc9cf'), 0)
|
|
check_base58(bytes.fromhex('41c1eaf111802559bad61b60d62b1f897c63928a'), 0)
|
|
check_base58(bytes.fromhex('0041c1eaf111802559bad61b60d62b1f897c63928a'), 0)
|
|
check_base58(bytes.fromhex('000041c1eaf111802559bad61b60d62b1f897c63928a'), 0)
|
|
check_base58(bytes.fromhex('00000041c1eaf111802559bad61b60d62b1f897c63928a'), 0)
|