Skip to content

Instantly share code, notes, and snippets.

@justmoon
Created June 28, 2013 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justmoon/5886477 to your computer and use it in GitHub Desktop.
Save justmoon/5886477 to your computer and use it in GitHub Desktop.
Decode Bitcoin address from Ripple InvoiceID
import Crypto.Hash.SHA256 as SHA256
__b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
__b58base = len(__b58chars)
def b58encode(v):
""" encode v, which is a string of bytes, to base58.
"""
long_value = 0L
for (i, c) in enumerate(v[::-1]):
long_value += ord(c) << (8*i) # 2x speedup vs. exponentiation
result = ''
while long_value >= __b58base:
div, mod = divmod(long_value, __b58base)
result = __b58chars[mod] + result
long_value = div
result = __b58chars[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
invoice = "001B7C17C34F801615B547A3B2CAB67BF37CDE0C141ADF6ACB00000000000000"
bytes = invoice.decode('hex')[0:21]
h3 = SHA256.new(SHA256.new(bytes).digest()).digest()
print b58encode(bytes+h3[0:4])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment