Last active
December 4, 2024 08:04
-
-
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'))) |
Hi how to do the invert ?
can anyone help me how to use this code
@radhefoot12345 install base58
, copy this into a script, update your testnet private key (tn
variable), run it. It will print command to run (with bitcoin-cli) on your mainnet.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome code!