Last active
May 18, 2023 19:08
-
-
Save Warchant/92f635b178e575315cd9b4553a9c17f6 to your computer and use it in GitHub Desktop.
Convert testnet private key to mainnet private key in Bitcoin
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
#!/usr/bin/env python | |
import base58 | |
import hashlib | |
main = bytes([128]) | |
test = bytes([239]) | |
def decodeSecret(key, network): | |
assert isinstance(network, bytes), network | |
k = base58.b58decode_check(key) | |
len_correct = len(k) == 32 + len(network) or (len(k) == 33 + len(network) and k[-1] == 1) | |
assert len_correct, "Bad length: {}".format(len(k)) | |
prefix_ok = k[0:1] == network | |
assert prefix_ok, "Bad prefix: {}. Expected {}".format(k[0:1], network) | |
is_compressed = len(k) == 33 + len(network) | |
return k[len(network):len(network) + 32], is_compressed | |
def encode_secret(key, network, is_compressed): | |
assert isinstance(network, bytes), network | |
out = bytearray([]) | |
out += network | |
out += key | |
if is_compressed: | |
out += bytes([1]) | |
assert len(out) <= 34, len(out) | |
ret = base58.b58encode_check(out) | |
return ret | |
def tn2mn(key): | |
data, compressed = decodeSecret(key, test) | |
return encode_secret(data, main, compressed) | |
tn = "cViwbQfYbBvaKuh4wAPTrTWLXAwyLn2PudoNAkua6VWaoHtyQPSN" | |
mn = tn2mn(tn) | |
print("vbitcoin-cli importprivkey {}".format(mn.decode('ascii'))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome code!