Skip to content

Instantly share code, notes, and snippets.

@jleo84
Created September 2, 2020 01:45
Show Gist options
  • Save jleo84/97fc58c6174f146642b2c215c20f88f5 to your computer and use it in GitHub Desktop.
Save jleo84/97fc58c6174f146642b2c215c20f88f5 to your computer and use it in GitHub Desktop.
Convert any Bitcoin key prefix to another (e.g.: xpub to ypub, zpriv to Zpriv)
import hashlib
import base58
import sys
prefix_dict = {
"xprv": "0488ade4", # Mainnet - P2PKH or P2SH - m/44'/0'
"yprv": "049d7878", # Mainnet - P2WPKH in P2SH - m/49'/0'
"zprv": "04b2430c", # Mainnet - P2WPKH - m/84'/0'
"Yprv": "0295b005", # Mainnet - Multi-signature P2WSH in P2SH
"Zprv": "02aa7a99", # Mainnet - Multi-signature P2WSH
"tprv": "04358394", # Testnet - P2PKH or P2SH - m/44'/1'
"uprv": "044a4e28", # Testnet - P2WPKH in P2SH - m/49'/1'
"vprv": "045f18bc", # Testnet - P2WPKH - m/84'/1'
"Uprv": "024285b5", # Testnet - Multi-signature P2WSH in P2SH
"Vprv": "02575048", # Testnet - Multi-signature P2WSH
'xpub': '0488b21e', # Mainnet - P2PKH or P2SH - m/44'/0'
'ypub': '049d7cb2', # Mainnet - P2WPKH in P2SH - m/49'/0'
'zpub': '04b24746', # Mainnet - P2WPKH - m/84'/0'
'Ypub': '0295b43f', # Mainnet - Multi-signature P2WSH in P2SH
'Zpub': '02aa7ed3', # Mainnet - Multi-signature P2WSH
'tpub': '043587cf', # Testnet - P2PKH or P2SH - m/44'/1'
'upub': '044a5262', # Testnet - P2WPKH in P2SH - m/49'/1'
'vpub': '045f1cf6', # Testnet - P2WPKH - m/84'/1'
'Upub': '024289ef', # Testnet - Multi-signature P2WSH in P2SH
'Vpub': '02575483' # Testnet - Multi-signature P2WSH
}
def convert_key(key, target_prefix):
decoded_key_bytes = base58.b58decode_check(key)
target_key_bytes = bytes.fromhex(prefix_dict[target_prefix]) + decoded_key_bytes[4:]
return base58.b58encode_check(target_key_bytes)
def main(key, target_prefix):
if not key or not target_prefix:
return "Insufficient parameters."
if key[1:4] != target_prefix[1:4]:
return "The key and the target prefix must be both public or both private."
if not key[:4] in prefix_dict:
return "Invalid target version."
if not target_prefix in prefix_dict:
return "Invalid target version."
return convert_key(key, target_prefix)
try:
key = sys.argv[1]
target_prefix = sys.argv[2]
result = main(key, target_prefix)
print(result)
except IndexError:
print("Insufficient parameters.")
# For tests:
# zpub6qwqZGWt6Gqm9i2bY2ErmJcTfRHHYX4LeUeszP33bSH3zgNdGiD7LEg57BQp2k5EQ4Qr5UAhgG1DMRSMZSA44UTNy1rLTqa3qaSnT9GmdsF
# xpub6CHJwwB3nukoT7eMsJfcM8RTKUzPfH5LpFcSRbFGqRXHtUkAmPsz67Mo4mVe2vmPanBEaWyakwJ7arDE83L2U16BELTVJ1w5J8KVfyMqtzE
# ypub6X7aFbqxwbJHJQqUhfTEZDWxVT8qbu4qjN8fCz9ADRuAwaZQ243YiB1w5yTE2qRJzRJ3Kza9DbefU8pnqjk3GEmn6g9usvkZZrP94VcBVdz
# yprvAJ8Dr6K57Djz5vm1bdvEC5aDwRJMCSLzN9D4QbjYf6NC4nEFUWjJANhTEghL3npior1TjTXW8vcuiaXoRGH8ZrEBn62qzPxbVKn6x5oY5vq
# xprv9yHxYRe9xYCWEdZtmH8byzUimT9uFpMVT2gqdCqfH5zK1gR2DrZjYK3KDUjk3tAoQCteyyvwgGGMqHvEhZs7mcYaukLRQV97DbiTZSJRaKC
# zprvAcxV9kyzFuHTwDx8RzhrQAfj7PSo94LVHFjHBzdS36k57t3UjAtrnSMbFtev3hUeDV8GUw84bayTbs9N8xh9N5uneRjGaJn5m3qkLdQwiaZ
# python key_converter.py zpub6qwqZGWt6Gqm9i2bY2ErmJcTfRHHYX4LeUeszP33bSH3zgNdGiD7LEg57BQp2k5EQ4Qr5UAhgG1DMRSMZSA44UTNy1rLTqa3qaSnT9GmdsF ypub
# python key_converter.py ypub6X7aFbqxwbJHJQqUhfTEZDWxVT8qbu4qjN8fCz9ADRuAwaZQ243YiB1w5yTE2qRJzRJ3Kza9DbefU8pnqjk3GEmn6g9usvkZZrP94VcBVdz xpub
# python key_converter.py xpub6CHJwwB3nukoT7eMsJfcM8RTKUzPfH5LpFcSRbFGqRXHtUkAmPsz67Mo4mVe2vmPanBEaWyakwJ7arDE83L2U16BELTVJ1w5J8KVfyMqtzE zpub
# python key_converter.py yprvAJ8Dr6K57Djz5vm1bdvEC5aDwRJMCSLzN9D4QbjYf6NC4nEFUWjJANhTEghL3npior1TjTXW8vcuiaXoRGH8ZrEBn62qzPxbVKn6x5oY5vq xprv
# python key_converter.py xprv9yHxYRe9xYCWEdZtmH8byzUimT9uFpMVT2gqdCqfH5zK1gR2DrZjYK3KDUjk3tAoQCteyyvwgGGMqHvEhZs7mcYaukLRQV97DbiTZSJRaKC zprv
# python key_converter.py zprvAcxV9kyzFuHTwDx8RzhrQAfj7PSo94LVHFjHBzdS36k57t3UjAtrnSMbFtev3hUeDV8GUw84bayTbs9N8xh9N5uneRjGaJn5m3qkLdQwiaZ yprv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment