File: _base58.py

package info (click to toggle)
python-ledger-bitcoin 0.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 716 kB
  • sloc: python: 9,357; makefile: 2
file content (152 lines) | stat: -rw-r--r-- 4,051 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""
Base 58 conversion utilities
****************************
"""

#
# base58.py
#
# Revision here from https://github.com/bitcoin-core/HWI/blob/3fe369d0379212fae1c72729a179d133b0adc872/hwilib/_base58.py
#
# Original source: git://github.com/joric/brutus.git
# which was forked from git://github.com/samrushing/caesure.git
#
# Distributed under the MIT/X11 software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
#

from binascii import hexlify, unhexlify
from typing import List

from .common import hash256
from .errors import BadArgumentError


b58_digits: str = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'


def encode(b: bytes) -> str:
    """
    Encode bytes to a base58-encoded string
    :param b: Bytes to encode
    :return: Base58 encoded string of ``b``
    """

    # Convert big-endian bytes to integer
    n: int = int('0x0' + hexlify(b).decode('utf8'), 16)

    # Divide that integer into base58
    temp: List[str] = []
    while n > 0:
        n, r = divmod(n, 58)
        temp.append(b58_digits[r])
    res: str = ''.join(temp[::-1])

    # Encode leading zeros as base58 zeros
    czero: int = 0
    pad: int = 0
    for c in b:
        if c == czero:
            pad += 1
        else:
            break
    return b58_digits[0] * pad + res

def decode(s: str) -> bytes:
    """
    Decode a base58-encoding string, returning bytes
    :param s: Base48 string to decode
    :return: Bytes encoded by ``s``
    """
    if not s:
        return b''

    # Convert the string to an integer
    n: int = 0
    for c in s:
        n *= 58
        if c not in b58_digits:
            raise BadArgumentError('Character %r is not a valid base58 character' % c)
        digit = b58_digits.index(c)
        n += digit

    # Convert the integer to bytes
    h: str = '%x' % n
    if len(h) % 2:
        h = '0' + h
    res = unhexlify(h.encode('utf8'))

    # Add padding back.
    pad = 0
    for c in s[:-1]:
        if c == b58_digits[0]:
            pad += 1
        else:
            break
    return b'\x00' * pad + res

def get_xpub_fingerprint(s: str) -> bytes:
    """
    Get the parent fingerprint from an extended public key
    :param s: The extended pubkey
    :return: The parent fingerprint bytes
    """
    data = decode(s)
    fingerprint = data[5:9]
    return fingerprint

def get_xpub_fingerprint_hex(xpub: str) -> str:
    """
    Get the parent fingerprint as a hex string from an extended public key
    :param s: The extended pubkey
    :return: The parent fingerprint as a hex string
    """
    data = decode(xpub)
    fingerprint = data[5:9]
    return hexlify(fingerprint).decode()

def to_address(b: bytes, version: bytes) -> str:
    """
    Base58 Check Encode the data with the version number.
    Used to encode legacy style addresses.
    :param b: The data to encode
    :param version: The version number to encode with
    :return: The Base58 Check Encoded string
    """
    data = version + b
    checksum = hash256(data)[0:4]
    data += checksum
    return encode(data)

def xpub_to_pub_hex(xpub: str) -> str:
    """
    Get the public key as a string from the extended public key.
    :param xpub: The extended pubkey
    :return: The pubkey hex string
    """
    data = decode(xpub)
    pubkey = data[-37:-4]
    return hexlify(pubkey).decode()


def xpub_to_xonly_pub_hex(xpub: str) -> str:
    """
    Get the public key as a string from the extended public key.
    :param xpub: The extended pubkey
    :return: The pubkey hex string
    """
    data = decode(xpub)
    pubkey = data[-36:-4]
    return hexlify(pubkey).decode()


def xpub_main_2_test(xpub: str) -> str:
    """
    Convert an extended pubkey from mainnet version to testnet version.
    :param xpub: The extended pubkey
    :return: The extended pubkey re-encoded using testnet version bytes
    """
    data = decode(xpub)
    test_data = b'\x04\x35\x87\xCF' + data[4:-4]
    checksum = hash256(test_data)[0:4]
    return encode(test_data + checksum)