Created
June 28, 2013 17:30
-
-
Save justmoon/5886477 to your computer and use it in GitHub Desktop.
Decode Bitcoin address from Ripple InvoiceID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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