Converts a base58-encoded string to its data and version. Throws if the base58 checksum is invalid.
(s)
| 71 | |
| 72 | |
| 73 | def 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 | |
| 103 | def keyhash_to_p2pkh(hash, main=False, version=235): |
no test coverage detected