def validate_key(key): '''Check the validity of a key or an address''' # Optional dependencies if unused import pycoin.key import cashaddr if ':' in key: key = key.split(':')[1] if key[0] in '13x': try: pycoin.key.Key.from_text(key) except pycoin.encoding.EncodingError: return False elif key[0] in 'qpQP': try: cashaddr.decode('bitcoincash:' + key.lower()) except ValueError: return False else: return False return True
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')
scriptsig = minpush(bcsig) + minpush(redeemscript) print("Preimage (%d bytes): %s" % (len(preimage), preimage.hex())) print("Compressed pubkey (33 bytes): %s" % (cpubkey.hex(), )) print("Redeemscript (%d bytes): %s" % (len(redeemscript), redeemscript.hex())) print(" hash: %s" % (rshash.hex(), )) print(" ", cashaddr.encode_full('bitcoincash', cashaddr.SCRIPT_TYPE, rshash)) print(" ", cashaddr.encode_full('bchtest', cashaddr.SCRIPT_TYPE, rshash)) print("ScriptSig (%d bytes): %s" % (len(scriptsig), scriptsig.hex())) # Make transaction # funding info inp = dict( prevout_hash=bytes.fromhex( '898e8031163df578acb91931c2da2d0c9ebed6d00b173fd2c1e2556cd5698b11'), prevout_n=1, prevout_value=30000000, scriptsig=scriptsig, sequence=0xffffffff, ) out = dict(value=30000000 - 1000, scriptpubkey=b''.join( (b'\x76\xa9\x14', cashaddr.decode( 'bchtest:qq4j8hh0qac4dy8fe9xyhmtvu9spu9w58unf74hkkd')[2], b'\x88\xac'))) tx = bitcoin.SimpleTx(1, [inp], [out], 0) print("Transaction:", tx.to_bytes().hex())