Created
January 26, 2020 12:26
-
-
Save matejcik/fddb5a88dbf7649cc5ecde8d81192d45 to your computer and use it in GitHub Desktop.
Cassacius mini private key to WIF in pure Python 3.6
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 hashlib | |
MINIKEY = "S6c56bnXQiBjk9mqSYE7ykVQ7NzrRy" | |
ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' | |
def sha256(x): | |
return hashlib.sha256(x).digest() | |
def base58check_encode(x): | |
checksum = sha256(sha256(x))[:4] | |
data = x + checksum | |
num = int.from_bytes(data, "big") | |
encode = [] | |
while (num > 0): | |
num, res = divmod(num, len(ALPHABET)) | |
encode.append(ALPHABET[res]) | |
return "".join(reversed(encode)) | |
def minikey_to_privkey(mk): | |
assert mk[0] == "S", "not a mini key (invalid start character)" | |
assert len(mk) == 30, "not a mini key (invalid length)" | |
digest = sha256(mk.encode() + b"?") | |
assert digest[0] == 0, f"not a mini key (invalid hash: {digest.hex()})" | |
return sha256(mk.encode()) | |
def privkey_to_wif(pk): | |
return base58check_encode(b"\x80" + pk) | |
privkey = minikey_to_privkey(MINIKEY) | |
print("private key hex:", privkey.hex()) | |
wif = privkey_to_wif(privkey) | |
print("wif format:", wif) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment