A transaction in Bitcoin is a transfer of value between Bitcoin wallets that gets recorded on the Bitcoin blockchain. Transactions are the fundamental building blocks of the Bitcoin network, allowing users to send and receive Bitcoin securely and transparently.

Components of a Bitcoin Transaction

A typical Bitcoin transaction consists of several components:

  • Inputs:

    Inputs are references to previous transactions from which the sender is spending Bitcoin. Each input contains a transaction ID and an index that points to the specific output being spent.

  • Outputs:

    Outputs specify where the Bitcoin is being sent. Each output includes a value (amount of Bitcoin) and a locking script (usually a public key hash) that specifies the recipient's address.

  • Amount:

    The amount of Bitcoin being transferred in the transaction.

  • Transaction ID:

    Each transaction has a unique identifier (TXID) that is generated by hashing the transaction data.

  • Signature:

    The sender signs the transaction with their private key to prove ownership of the funds being sent. This is essential for preventing unauthorized spending.

How Transactions Work

The process of creating and executing a Bitcoin transaction involves the following steps:

  1. Creating a Transaction:

    The sender constructs a transaction by specifying the inputs (previous outputs being spent), outputs (recipient addresses), and the amount of Bitcoin to transfer.

  2. Signing the Transaction:

    The sender signs the transaction with their private key, creating a digital signature that verifies their authority to spend the Bitcoin.

  3. Broadcasting the Transaction:

    The signed transaction is broadcast to the Bitcoin network, where it is propagated to other nodes.

  4. Validation:

    Nodes in the network validate the transaction by checking the signatures, ensuring the inputs are valid, and confirming that the sender has sufficient funds.

  5. Inclusion in a Block:

    Once validated, the transaction is included in a new block by miners. The block is then added to the blockchain, making the transaction permanent.

Sample Code: Creating a Simple Bitcoin Transaction

The following sample code demonstrates how to create a simple Bitcoin transaction using JavaScript with the bitcoinjs-lib library:


const bitcoin = require('bitcoinjs-lib');
const bitcoinMessage = require('bitcoinjs-message');

// Define network (mainnet or testnet)
const network = bitcoin.networks.testnet;

// Create a new key pair
const keyPair = bitcoin.ECPair.makeRandom({ network });
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey, network });

// Create a transaction
const txb = new bitcoin.TransactionBuilder(network);
const amountToSend = 100000; // Amount in satoshis (0.001 BTC)
const fee = 1000; // Transaction fee in satoshis

// Add input (previous transaction output)
txb.addInput('previous_transaction_id', 0); // Replace with actual TXID and index

// Add output (recipient address)
txb.addOutput('recipient_address', amountToSend); // Replace with actual recipient address

// Sign the transaction
txb.sign(0, keyPair);

// Build the transaction
const transaction = txb.build();

// Get the transaction hex
const transactionHex = transaction.toHex();

console.log('Transaction Hex:', transactionHex);

Conclusion

In summary, a Bitcoin transaction is a secure and verifiable transfer of value between wallets that gets recorded on the blockchain.