def test_p2pkh_input(self): outpoint = simple.outpoint( tx_id=helpers.P2PKH['human']['ins'][0]['hash'], index=helpers.P2PKH['human']['ins'][0]['index']) self.assertEqual( simple.p2pkh_input( outpoint=outpoint, sig=helpers.P2PKH['human']['ins'][0]['signature'], pubkey=helpers.P2PKH['human']['ins'][0]['pubkey'], ), helpers.P2PKH['ser']['ins'][0]['input'])
def test_legacy_tx(self): outpoint = simple.outpoint( tx_id=helpers.P2PKH['human']['ins'][0]['hash'], index=helpers.P2PKH['human']['ins'][0]['index']) tx_in = simple.p2pkh_input( outpoint=outpoint, sig=helpers.P2PKH['human']['ins'][0]['signature'], pubkey=helpers.P2PKH['human']['ins'][0]['pubkey'], sequence=helpers.P2PKH['human']['ins'][0]['sequence']) tx_out = simple.output(helpers.P2PKH['human']['outs'][0]['value'], helpers.P2PKH['human']['outs'][0]['addr']) tx_return_output = txn.make_op_return_output( helpers.P2PKH['human']['outs'][1]['memo']) tx = simple.legacy_tx([tx_in], [tx_out, tx_return_output]) self.assertEqual(tx, helpers.P2PKH['ser']['tx']['signed'])
unsigned_tx = simple.unsigned_legacy_tx( tx_ins=[sender_input], tx_outs=[sender_output, op_return_output], version=version, lock_time=locktime) sighash_all = 0x01 sighash = unsigned_tx.sighash_all( index=0, script=addr.to_output_script(sender_addr)) # Using instance of CKey from python-bitcoinlib to sign: # sig = sender.sign(sighash) + bytes([sighash_all]) # sig = sig.hex() sig = '30450221009e8c7f85d6491169df139f25d26633efe48e98738331a37a1694d655dccebdbd02201a6444cfb364e91279f8c9a8b09cdbdeb4bf6cc0f00f53b9356f852c3b3151dc01' # noqa: E501 signed_input = simple.p2pkh_input( outpoint=sender_outpoint, sig=sig, pubkey=sender_pubkey, sequence=sequence) tx_signed = unsigned_tx.copy(tx_ins=[signed_input]) print('tx_signed') print(tx_signed.hex()) print() print(tx_signed.tx_id.hex()) print() print(tx_signed.tx_id_le.hex()) print()
# Generate Signed Tx # https://blockchain.info/tx/1e7acd3d4715054c8fb0fdea25c5c704986006d2c6f30b0782e9b36a7ee072ef # With the p2pkh output script from address, create the the sighash to be signed sighash = tx.sighash_all(index=0, script=addresses.to_output_script(address)) # Declare SIGHASH_ALL type SIGHASH_ALL = 0x01 # Sign the tx with private key # Assumes private_key is of type class bitcoin.wallet.CKey from python-bitcoinlib sig = private_key.sign(sighash) + bytes([SIGHASH_ALL]) # Recreate tx input with script signature tx_signed_input = simple.p2pkh_input(outpoint=tx_outpoint, sig=sig.hex(), pubkey=public_key, sequence=0xFFFFFFFE) # Recreate tx with the signed tx input tx_signed = tx.copy(tx_ins=[tx_signed_input]) tx_signed_hex = tx_signed.hex() print(tx_signed_hex) # Transaction hash tx_hash = tx_signed.tx_id.hex() # Resources to decode transaction (tx_signed_hex) # https://blockchain.info/decode-tx # https://live.blockcypher.com/btc/decodetx/ # Resources to broadcast transaction (tx_signed_hex)