MCPcopy Index your code
hub / github.com/petertodd/python-bitcoinlib / decode

Function decode

bitcoin/base58.py:55–80  ·  view source on GitHub ↗

Decode a base58-encoding string, returning bytes

(s)

Source from the content-addressed store, hash-verified

53 return B58_DIGITS[0] * pad + res
54
55def 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
83class Base58ChecksumError(Base58Error):

Callers 2

__new__Method · 0.70
test_encode_decodeMethod · 0.50

Calls 1

InvalidBase58ErrorClass · 0.85

Tested by 1

test_encode_decodeMethod · 0.40