2016-10-14 20:29:39 +02:00
|
|
|
#!/usr/bin/env python3
|
2019-02-21 02:03:13 +01:00
|
|
|
# Copyright (c) 2016-2019 The Bitcoin Core developers
|
2016-10-14 20:29:39 +02:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2017-01-18 00:34:40 +01:00
|
|
|
"""Encode and decode BASE58, P2PKH and P2SH addresses."""
|
2016-10-14 20:29:39 +02:00
|
|
|
|
2019-07-25 23:11:31 +02:00
|
|
|
import enum
|
|
|
|
|
2016-10-14 20:29:39 +02:00
|
|
|
from .script import hash256, hash160, sha256, CScript, OP_0
|
2019-02-18 16:35:48 +01:00
|
|
|
from .util import hex_str_to_bytes
|
2016-10-14 20:29:39 +02:00
|
|
|
|
2017-08-27 09:41:46 +02:00
|
|
|
from . import segwit_addr
|
|
|
|
|
2020-02-07 17:59:29 +01:00
|
|
|
ADDRESS_BCRT1_UNSPENDABLE = 'rlbc1qjqmxmkpmxt80xz4y3746zgt0q3u3ferrtwx46m'
|
2018-09-23 17:34:51 +02:00
|
|
|
|
2019-07-25 23:11:31 +02:00
|
|
|
|
|
|
|
class AddressType(enum.Enum):
|
|
|
|
bech32 = 'bech32'
|
|
|
|
p2sh_segwit = 'p2sh-segwit'
|
|
|
|
legacy = 'legacy' # P2PKH
|
|
|
|
|
|
|
|
|
2016-10-14 20:29:39 +02:00
|
|
|
chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
|
|
|
|
2018-09-23 17:34:51 +02:00
|
|
|
|
2016-10-14 20:29:39 +02:00
|
|
|
def byte_to_base58(b, version):
|
|
|
|
result = ''
|
2019-02-18 16:35:48 +01:00
|
|
|
str = b.hex()
|
|
|
|
str = chr(version).encode('latin-1').hex() + str
|
|
|
|
checksum = hash256(hex_str_to_bytes(str)).hex()
|
2016-10-14 20:29:39 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
# TODO: def base58_decode
|
|
|
|
|
|
|
|
def keyhash_to_p2pkh(hash, main = False):
|
2019-02-19 23:43:44 +01:00
|
|
|
assert len(hash) == 20
|
2016-10-14 20:29:39 +02:00
|
|
|
version = 0 if main else 111
|
|
|
|
return byte_to_base58(hash, version)
|
|
|
|
|
|
|
|
def scripthash_to_p2sh(hash, main = False):
|
2019-02-19 23:43:44 +01:00
|
|
|
assert len(hash) == 20
|
2016-10-14 20:29:39 +02:00
|
|
|
version = 5 if main else 196
|
|
|
|
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)
|
|
|
|
|
2017-08-27 09:41:46 +02:00
|
|
|
def key_to_p2sh_p2wpkh(key, main = False):
|
|
|
|
key = check_key(key)
|
|
|
|
p2shscript = CScript([OP_0, hash160(key)])
|
|
|
|
return script_to_p2sh(p2shscript, main)
|
|
|
|
|
|
|
|
def program_to_witness(version, program, main = False):
|
|
|
|
if (type(program) is str):
|
|
|
|
program = hex_str_to_bytes(program)
|
|
|
|
assert 0 <= version <= 16
|
|
|
|
assert 2 <= len(program) <= 40
|
|
|
|
assert version > 0 or len(program) in [20, 32]
|
2019-09-19 16:31:07 +02:00
|
|
|
return segwit_addr.encode("lbc" if main else "rlbc", version, program)
|
2017-08-27 09:41:46 +02:00
|
|
|
|
|
|
|
def script_to_p2wsh(script, main = False):
|
|
|
|
script = check_script(script)
|
|
|
|
return program_to_witness(0, sha256(script), main)
|
|
|
|
|
|
|
|
def key_to_p2wpkh(key, main = False):
|
|
|
|
key = check_key(key)
|
|
|
|
return program_to_witness(0, hash160(key), main)
|
|
|
|
|
|
|
|
def script_to_p2sh_p2wsh(script, main = False):
|
|
|
|
script = check_script(script)
|
|
|
|
p2shscript = CScript([OP_0, sha256(script)])
|
|
|
|
return script_to_p2sh(p2shscript, main)
|
|
|
|
|
2016-10-14 20:29:39 +02:00
|
|
|
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
|
2019-02-19 23:43:44 +01:00
|
|
|
assert False
|
2016-10-14 20:29:39 +02:00
|
|
|
|
|
|
|
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
|
2019-02-19 23:43:44 +01:00
|
|
|
assert False
|