MCPcopy Create free account
hub / github.com/ElementsProject/elements / base58_to_byte

Function base58_to_byte

test/functional/test_framework/address.py:73–100  ·  view source on GitHub ↗

Converts a base58-encoded string to its data and version. Throws if the base58 checksum is invalid.

(s)

Source from the content-addressed store, hash-verified

71
72
73def base58_to_byte(s):
74 """Converts a base58-encoded string to its data and version.
75
76 Throws if the base58 checksum is invalid."""
77 if not s:
78 return b''
79 n = 0
80 for c in s:
81 n *= 58
82 assert c in chars
83 digit = chars.index(c)
84 n += digit
85 h = '%x' % n
86 if len(h) % 2:
87 h = '0' + h
88 res = n.to_bytes((n.bit_length() + 7) // 8, 'big')
89 pad = 0
90 for c in s:
91 if c == chars[0]:
92 pad += 1
93 else:
94 break
95 res = b'\x00' * pad + res
96
97 # Assert if the checksum is invalid
98 assert_equal(hash256(res[:-4])[:4], res[-4:])
99
100 return res[1:-4], int(res[0])
101
102
103def keyhash_to_p2pkh(hash, main=False, version=235):

Callers 4

address_to_scriptpubkeyFunction · 0.90
check_base58Method · 0.85

Calls 3

assert_equalFunction · 0.70
hash256Function · 0.70
indexMethod · 0.45

Tested by

no test coverage detected