Simple Tx
This guide walks you through the steps of creating a simple Bitcoin transaction. To get started, let's explain some basic concepts around Bitcoin transactions.
Understanding and Creating Transactions
Transactions in Bitcoin are mechanisms for transferring value and invoking smart contract logic. The Transaction class in the BSV SDK encapsulates the creation, signing, and broadcasting of transactions, also enabling the use of Bitcoin's scripting language for locking and unlocking coins.
Creating and Signing a Transaction
Consider the scenario where you need to create a transaction. The process involves specifying inputs (where the bitcoins are coming from) and outputs (where they're going). Here's a simplified example:
Code Walk Through
Private Key and Address
PrivateKeyFromWif is used to extract the private key from a Wallet Import Format (WIF) string. This private key is then used to generate a corresponding public key.
NewAddressFromPublicKey generates a Bitcoin address from the public key. The second argument true indicates that the address will be in the compressed format.
Source Transaction
sourceRawtx: A hexadecimal string representing a raw Bitcoin transaction. This transaction serves as the input to the new transaction you are creating.
NewTransactionFromHex: Converts the raw transaction hex string into a Transaction object.
sourceMerklePathHex: This hex string represents the Merkle path, which is used for verifying the inclusion of the transaction in a block.
NewMerklePathFromHex: Converts the Merkle path hex string into a MerklePath object.
sourceTransaction.MerklePath = merklePath: Associates the Merkle path with the source transaction.
New Transaction
NewTransaction creates a new, empty transaction object that will be populated with inputs and outputs.
Unlocking Script
p2pkh.Unlock generates an unlocking script (scriptSig) from the private key. This script will be used to prove ownership of the bitcoins being spent.
The Unlock method creates a template for the unlocking script, which can be used to satisfy the conditions of a P2PKH locking script.
Transaction Input
AddInput adds an input to the transaction. The input references the source transaction using its transaction ID (SourceTXID) and specifies the output index (SourceTxOutIndex).
The input also includes the unlockingScriptTemplate, which is necessary to unlock the bitcoins in the referenced output.
Locking Script
p2pkh.Lock creates a locking script (scriptPubKey) for the recipient’s address. This script ensures that only the recipient, who can provide a valid unlocking script, can spend the bitcoins.
Transaction Output
AddOutput adds an output to the transaction. The output specifies the locking script and the amount of bitcoins to send (in satoshis).
Sign Transaction
tx.Sign signs the transaction. This involves generating digital signatures for each input, which are included in the unlocking scripts. The signatures prove that the sender has the authority to spend the bitcoins.
Transaction Formats
Last updated