Quantcast
Channel: Recent Gists from 84adam
Viewing all articles
Browse latest Browse all 35

Convert between bitcoin public key types: xpub/ypub/zpub

$
0
0
convert_xyz_pub.py
# convert_xyz_pub.py
# requirements: python3; base58 (`pip install base58`)
importbase58
defconvert_xyz_pub(in_key, out_key_type):
"""
Based on: https://gist.github.com/freenancial/d82fec076c13158fd34d1c4300b2b300
"""
xpub=b'\x04\x88\xb2\x1e'# mainnet P2PKH or P2SH
ypub=b'\x04\x9d\x7c\xb2'# mainnet P2WPKH in P2SH
zpub=b'\x04\xb2\x47\x46'# mainnet P2WPKH
# for other types of conversions,
# ... including to Zpub (P2WSH), Ypub (P2WSH in P2SH), and testnet (upub/Upub/vpub/Vpub),
# ... see: https://jlopp.github.io/xpub-converter/
out_key_types= ['x', 'y', 'z', 'X', 'Y', 'Z']
ifout_key_typenotinout_key_types:
raiseValueError(f"'{out_key_type}' not in allowed types: {out_key_types}")
ifout_key_type.lower() =='x':
p=xpub
elifout_key_type.lower() =='y':
p=ypub
elifout_key_type.lower() =='z':
p=zpub
out_key=base58.b58encode_check(p+base58.b58decode_check(in_key)[4:]).decode('ascii')
returnout_key
if__name__=='__main__':
print("Example xpub: ")
print('xpub6CUGRUonZSQ4TWtTMmzXdrXDtypWKiKrhko4egpiMZbpiaQL2jkwSB1icqYh2cfDfVxdx4df189oLKnC5fSwqPfgyP3hooxujYzAu3fDVmz')
xpub1='xpub6CUGRUonZSQ4TWtTMmzXdrXDtypWKiKrhko4egpiMZbpiaQL2jkwSB1icqYh2cfDfVxdx4df189oLKnC5fSwqPfgyP3hooxujYzAu3fDVmz'
print()
print("Converting... `convert_xyz(xpub1, 'y')`")
print("ypub: ")
print(convert_xyz(xpub1, 'y'))
print()
print("Converting... `convert_xyz(xpub1, 'z')`")
print("zpub: ")
print(convert_xyz(xpub1, 'z'))

Viewing all articles
Browse latest Browse all 35

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>