|
|
@ -239,73 +239,76 @@ def hash_160_to_bc_address(h160, addrtype = 0): |
|
|
|
vh160 = chr(addrtype) + h160 |
|
|
|
h = Hash(vh160) |
|
|
|
addr = vh160 + h[0:4] |
|
|
|
return b58encode(addr) |
|
|
|
return base_encode(addr, base=58) |
|
|
|
|
|
|
|
def bc_address_to_hash_160(addr): |
|
|
|
bytes = b58decode(addr, 25) |
|
|
|
bytes = base_decode(addr, 25, base=58) |
|
|
|
return ord(bytes[0]), bytes[1:21] |
|
|
|
|
|
|
|
|
|
|
|
__b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' |
|
|
|
__b58base = len(__b58chars) |
|
|
|
assert len(__b58chars) == 58 |
|
|
|
|
|
|
|
__b43chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ$*+-./:' |
|
|
|
assert len(__b43chars) == 43 |
|
|
|
|
|
|
|
def b58encode(v): |
|
|
|
""" encode v, which is a string of bytes, to base58.""" |
|
|
|
|
|
|
|
def base_encode(v, base): |
|
|
|
""" encode v, which is a string of bytes, to base58.""" |
|
|
|
if base == 58: |
|
|
|
chars = __b58chars |
|
|
|
elif base == 43: |
|
|
|
chars = __b43chars |
|
|
|
long_value = 0L |
|
|
|
for (i, c) in enumerate(v[::-1]): |
|
|
|
long_value += (256**i) * ord(c) |
|
|
|
|
|
|
|
result = '' |
|
|
|
while long_value >= __b58base: |
|
|
|
div, mod = divmod(long_value, __b58base) |
|
|
|
result = __b58chars[mod] + result |
|
|
|
while long_value >= base: |
|
|
|
div, mod = divmod(long_value, base) |
|
|
|
result = chars[mod] + result |
|
|
|
long_value = div |
|
|
|
result = __b58chars[long_value] + result |
|
|
|
|
|
|
|
result = chars[long_value] + result |
|
|
|
# Bitcoin does a little leading-zero-compression: |
|
|
|
# leading 0-bytes in the input become leading-1s |
|
|
|
nPad = 0 |
|
|
|
for c in v: |
|
|
|
if c == '\0': nPad += 1 |
|
|
|
else: break |
|
|
|
|
|
|
|
return (__b58chars[0]*nPad) + result |
|
|
|
return (chars[0]*nPad) + result |
|
|
|
|
|
|
|
|
|
|
|
def b58decode(v, length): |
|
|
|
def base_decode(v, length, base): |
|
|
|
""" decode v into a string of len bytes.""" |
|
|
|
if base == 58: |
|
|
|
chars = __b58chars |
|
|
|
elif base == 43: |
|
|
|
chars = __b43chars |
|
|
|
long_value = 0L |
|
|
|
for (i, c) in enumerate(v[::-1]): |
|
|
|
long_value += __b58chars.find(c) * (__b58base**i) |
|
|
|
|
|
|
|
long_value += chars.find(c) * (base**i) |
|
|
|
result = '' |
|
|
|
while long_value >= 256: |
|
|
|
div, mod = divmod(long_value, 256) |
|
|
|
result = chr(mod) + result |
|
|
|
long_value = div |
|
|
|
result = chr(long_value) + result |
|
|
|
|
|
|
|
nPad = 0 |
|
|
|
for c in v: |
|
|
|
if c == __b58chars[0]: nPad += 1 |
|
|
|
if c == chars[0]: nPad += 1 |
|
|
|
else: break |
|
|
|
|
|
|
|
result = chr(0)*nPad + result |
|
|
|
if length is not None and len(result) != length: |
|
|
|
return None |
|
|
|
|
|
|
|
return result |
|
|
|
|
|
|
|
|
|
|
|
def EncodeBase58Check(vchIn): |
|
|
|
hash = Hash(vchIn) |
|
|
|
return b58encode(vchIn + hash[0:4]) |
|
|
|
return base_encode(vchIn + hash[0:4], base=58) |
|
|
|
|
|
|
|
|
|
|
|
def DecodeBase58Check(psz): |
|
|
|
vchRet = b58decode(psz, None) |
|
|
|
vchRet = base_decode(psz, None, base=58) |
|
|
|
key = vchRet[0:-4] |
|
|
|
csum = vchRet[-4:] |
|
|
|
hash = Hash(key) |
|
|
|