def generate_address(xpub, idx, cash=True): '''Generate a bitcoin cash or bitcoin legacy address from the extended public key at the given index''' # Optional dependencies if unused import pycoin.key import cashaddr subkey = pycoin.key.Key.from_text(xpub).subkey(0).subkey(idx) if cash: return cashaddr.encode('bitcoincash', 0, subkey.hash160()) return subkey.address()
def convert_address(address): '''Convert an address back and forth between cash and legacy formats''' # Optional dependencies if unused import pycoin.key import cashaddr if address[0] in '13': subkey = pycoin.key.Key.from_text(address) return cashaddr.encode('bitcoincash', 0, subkey.hash160()) elif address[0] in 'qpQP': subkey = cashaddr.decode('bitcoincash:' + address.lower())[2] return pycoin.key.Key(hash160=subkey).address() else: raise ValueError('Unsupported address format')