Simple Tx
Understanding and Creating Transactions
Creating and Signing a Transaction
from bsv import PrivateKey, PublicKey, ARC, P2PKH, Transaction
priv_key = PrivateKey.fromWif('...') # Your P2PKH private key
change_priv_key = PrivateKey.fromWif('...') # Change private key (never re-use addresses)
recipient_address = '1Fd5F7XR8LYHPmshLNs8cXSuVAAQzGp7Hc' # Address of the recipient
tx = Transaction()
# Add the input
tx.add_input(
TransactionInput(
source_transaction=Transaction.from_hex('...'), # The source transaction where the output you are spending was created
source_output_index=0,
unlocking_script_template=P2PKH().unlock(priv_key), # The script template you are using to unlock the output, in this case P2PKH
)
)
# Pay an output to a recipient using the P2PKH locking template
tx.add_output(
TransactionOutput(
locking_script=P2PKH().lock(recipient_address),
satoshis=2500
)
)
# Send remainder back the change address
tx.add_output(
TransactionOutput(
lockingScript=P2PKH().lock(change_priv_key.address()),
change=True
)
)
# Now we can compute the fee and sign the transaction
tx.fee()
tx.sign()
# Finally, we broadcast it with ARC.
# get your api key from https://console.taal.com
api_key = 'mainnet_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' # replace
await tx.broadcast(ARC('https://api.taal.com/arc', api_key))Handling Hex Locking Scripts
Last updated
