Transaction Input
Overview
The TransactionInput class in the BSV TypeScript SDK represents an input to a Bitcoin transaction. Each input references an unspent transaction output (UTXO) from a previous transaction and includes an unlocking script (scriptSig) that proves the right to spend those coins. Understanding transaction inputs is fundamental to building and validating Bitcoin transactions.
Purpose
Reference unspent transaction outputs (UTXOs) from previous transactions
Store unlocking scripts (formerly called scriptSig) that prove spending authority
Maintain sequence numbers for transaction replacement and timelocks
Support SPV (Simplified Payment Verification) with merkle proofs
Handle various input types (P2PKH, P2PK, custom scripts, etc.)
Serialize and deserialize inputs for transaction broadcasting
Enable transaction signing and validation workflows
Basic Usage
import { Transaction, TransactionInput, PrivateKey, P2PKH } from '@bsv/sdk';
// Create a transaction with an input
const privKey = PrivateKey.fromWif('L5EY1SbTvvPNSdCYQe1EJHfXCBBT4PmnF6CDbzCm9iifZptUvDGB');
// Reference to previous transaction
const sourceTransaction = Transaction.fromHex('0100000001...');
const sourceOutputIndex = 0;
// Create input with unlocking script template
const input: TransactionInput = {
sourceTransaction,
sourceOutputIndex,
unlockingScriptTemplate: new P2PKH().unlock(privKey),
sequence: 0xffffffff // Default sequence number
};
// Create transaction and add input
const tx = new Transaction();
tx.addInput(input);
// The unlocking script will be populated during signing
await tx.sign();
console.log('Input added:', tx.inputs[0]);
console.log('Unlocking script:', tx.inputs[0].unlockingScript?.toHex());Key Features
1. Creating Transaction Inputs with Source References
Inputs reference previous transaction outputs (UTXOs):
2. Unlocking Script Templates
Unlocking scripts prove the right to spend UTXOs:
3. Sequence Numbers and Time Locks
Sequence numbers enable transaction replacement and relative time locks:
4. SPV with Merkle Proofs
Include merkle proofs for SPV validation:
API Reference
TransactionInput Interface
Creating Inputs
Input Properties
Serialization
Common Patterns
Pattern 1: Spending Multiple UTXOs
Consolidate multiple UTXOs into a single transaction:
Pattern 2: Multi-Signature Input
Create input requiring multiple signatures:
Pattern 3: UTXO Management and Coin Selection
Implement coin selection for optimal transaction building:
Security Considerations
UTXO Verification: Always verify that referenced UTXOs exist and are unspent before creating transactions.
Source Transaction Validation: When using full source transactions, validate the transaction before referencing it.
SPV Security: Merkle proofs provide SPV but require trusted chain tracker. Validate merkle roots against known block headers.
Private Key Management: Never expose private keys. Use secure key derivation and storage.
Sequence Number Usage: Be aware that non-final sequence numbers enable transaction replacement. Use 0xffffffff for final transactions.
Double Spend Prevention: Ensure UTXOs haven't been spent in another transaction before broadcasting.
Performance Considerations
Input Count: More inputs increase transaction size and fees. Use coin selection to minimize inputs.
UTXO Consolidation: Periodically consolidate small UTXOs to reduce future transaction costs.
SPV vs Full Transaction: Including full source transactions increases size. Use SPV proofs or BEEF format for efficiency.
Unlocking Script Size: Custom unlocking scripts should be as compact as possible to minimize fees.
Parallel Signing: When signing multiple inputs, consider parallel signature generation for better performance.
Related Components
Transaction - Build complete transactions with inputs
TransactionOutput - Outputs that become inputs
Script - Create unlocking scripts
ScriptTemplate - Templates for common unlock patterns
Signature - Sign transaction inputs
Code Examples
See complete working examples in:
Best Practices
Always include source transaction or TXID - Required for input validation
Use SPV proofs when possible for lighter weight transactions
Set appropriate sequence numbers - Use 0xffffffff for final transactions
Implement coin selection to optimize transaction fees and UTXO management
Verify UTXOs before spending - Ensure they exist and are unspent
Use BEEF format for efficient transaction package transmission
Handle errors gracefully - Invalid UTXOs should not crash your application
Never reuse addresses - Generate new change addresses for each transaction
Consolidate small UTXOs periodically to reduce future transaction costs
Test with small amounts before using real funds
Troubleshooting
Issue: Input references non-existent UTXO
Solution: Verify the source transaction exists and the output index is valid.
Issue: Insufficient funds
Solution: Ensure total input value exceeds output value plus fees.
Issue: Unlocking script doesn't match locking script
Solution: Ensure the unlocking script template matches the locking script type.
Issue: Sequence number prevents replacement
Solution: Use appropriate sequence number for your use case.
Further Reading
Bitcoin Transactions - Complete transaction documentation
UTXOs - Understanding unspent transaction outputs
Sequence Numbers - Transaction replacement and timelocks
SPV - Simplified Payment Verification
BEEF Format - BRC-62: Background Evaluation Extended Format
BSV SDK Documentation - Official SDK docs
Status
✅ Complete
Last updated
