2 Bitcoin base58 encoding and decoding. 4 Based on https://bitcointalk.org/index.php?topic=1026.0 (public domain) 19 __b58chars =
'123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' 20 __b58base = len(__b58chars)
24 """ encode v, which is a string of bytes, to base58. 27 for (i, c)
in enumerate(v[::-1]):
28 long_value += (256**i) *
ord(c)
31 while long_value >= __b58base:
32 div, mod = divmod(long_value, __b58base)
33 result = __b58chars[mod] + result
35 result = __b58chars[long_value] + result
41 if c ==
'\0': nPad += 1
44 return (__b58chars[0]*nPad) + result
47 """ decode v into a string of len bytes 50 for (i, c)
in enumerate(v[::-1]):
51 long_value += __b58chars.find(c) * (__b58base**i)
54 while long_value >= 256:
55 div, mod = divmod(long_value, 256)
56 result =
chr(mod) + result
58 result =
chr(long_value) + result
62 if c == __b58chars[0]: nPad += 1
65 result =
chr(0)*nPad + result
66 if length
is not None and len(result) != length:
72 """Return 32-bit checksum based on SHA256""" 73 return SHA256.new(SHA256.new(v).digest()).digest()[0:4]
76 """b58encode a string, with 32-bit checksum""" 80 """decode a base58 string, check and remove checksum""" 85 if result[-4:] ==
checksum(result[:-4]):
91 """ Returns None if strAddress is invalid. Otherwise returns integer version of address. """ 93 if addr
is None or len(addr)!=21:
return None 97 if __name__ ==
'__main__':
100 _ohai =
'o hai'.encode(
'ascii')
102 assert _tmp ==
'DYB3oMS' 104 print(
"Tests passed")
def get_bcaddress_version(strAddress)
def b58decode(v, length=None)