Skip to content

Instantly share code, notes, and snippets.

@ymgve
Created July 17, 2019 17:16
Show Gist options
  • Save ymgve/fa50dc807d83b6011346fa0f3bcfba7e to your computer and use it in GitHub Desktop.
Save ymgve/fa50dc807d83b6011346fa0f3bcfba7e to your computer and use it in GitHub Desktop.
import hashlib, sys
b58ab = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
def b58csum(s):
return hashlib.sha256(hashlib.sha256(s).digest()).digest()[0:4]
def b58decode(s, checksum=True):
idx = 0
while s[idx] == "1":
idx += 1
n = 0
for c in s[idx:]:
n = n * 58 + b58ab.index(c)
res = long2byte(n)
res = idx * "\x00" + res
if checksum:
res, cs = res[:-4], res[-4:]
assert cs == b58csum(res), "base58 checksum failed"
return res
def b58encode(s, checksum=True):
if checksum:
s += b58csum(s)
idx = 0
while s[idx] == "\x00":
idx += 1
n = byte2long(s)
res = ""
while n > 0:
res = b58ab[n % 58] + res
n /= 58
return "1" * idx + res
def byte2long(s):
res = 0
for c in s:
res = (res << 8) | ord(c)
return res
def long2byte(n, sz=None):
res = ""
while n > 0:
res = chr(n & 0xff) + res
n >>= 8
if sz is not None:
res = res.rjust(sz, "\x00")
return res
priv = b58decode(sys.argv[1])
print "BTC private key", sys.argv[1]
print "BTC raw private key", priv.encode("hex")
print "BVX private key", b58encode("\xca" + priv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment