Decode a base58-encoding string, returning bytes
(s)
| 53 | return B58_DIGITS[0] * pad + res |
| 54 | |
| 55 | def decode(s): |
| 56 | """Decode a base58-encoding string, returning bytes""" |
| 57 | if not s: |
| 58 | return b'' |
| 59 | |
| 60 | # Convert the string to an integer |
| 61 | n = 0 |
| 62 | for c in s: |
| 63 | n *= 58 |
| 64 | if c not in B58_DIGITS: |
| 65 | raise InvalidBase58Error('Character %r is not a valid base58 character' % c) |
| 66 | digit = B58_DIGITS.index(c) |
| 67 | n += digit |
| 68 | |
| 69 | # Convert the integer to bytes |
| 70 | h = '%x' % n |
| 71 | if len(h) % 2: |
| 72 | h = '0' + h |
| 73 | res = binascii.unhexlify(h.encode('utf8')) |
| 74 | |
| 75 | # Add padding back. |
| 76 | pad = 0 |
| 77 | for c in s[:-1]: |
| 78 | if c == B58_DIGITS[0]: pad += 1 |
| 79 | else: break |
| 80 | return b'\x00' * pad + res |
| 81 | |
| 82 | |
| 83 | class Base58ChecksumError(Base58Error): |