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

  1. UTXO Verification: Always verify that referenced UTXOs exist and are unspent before creating transactions.

  2. Source Transaction Validation: When using full source transactions, validate the transaction before referencing it.

  3. SPV Security: Merkle proofs provide SPV but require trusted chain tracker. Validate merkle roots against known block headers.

  4. Private Key Management: Never expose private keys. Use secure key derivation and storage.

  5. Sequence Number Usage: Be aware that non-final sequence numbers enable transaction replacement. Use 0xffffffff for final transactions.

  6. Double Spend Prevention: Ensure UTXOs haven't been spent in another transaction before broadcasting.

Performance Considerations

  1. Input Count: More inputs increase transaction size and fees. Use coin selection to minimize inputs.

  2. UTXO Consolidation: Periodically consolidate small UTXOs to reduce future transaction costs.

  3. SPV vs Full Transaction: Including full source transactions increases size. Use SPV proofs or BEEF format for efficiency.

  4. Unlocking Script Size: Custom unlocking scripts should be as compact as possible to minimize fees.

  5. Parallel Signing: When signing multiple inputs, consider parallel signature generation for better performance.

Code Examples

See complete working examples in:

Best Practices

  1. Always include source transaction or TXID - Required for input validation

  2. Use SPV proofs when possible for lighter weight transactions

  3. Set appropriate sequence numbers - Use 0xffffffff for final transactions

  4. Implement coin selection to optimize transaction fees and UTXO management

  5. Verify UTXOs before spending - Ensure they exist and are unspent

  6. Use BEEF format for efficient transaction package transmission

  7. Handle errors gracefully - Invalid UTXOs should not crash your application

  8. Never reuse addresses - Generate new change addresses for each transaction

  9. Consolidate small UTXOs periodically to reduce future transaction costs

  10. 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

Status

✅ Complete

Last updated