Transaction Output

Overview

The TransactionOutput class in the BSV TypeScript SDK represents an output in a Bitcoin transaction. Each output specifies an amount of satoshis and a locking script that defines the conditions under which those coins can be spent. Outputs create new UTXOs (Unspent Transaction Outputs) that can be referenced as inputs in future transactions.

Purpose

  • Define where coins are sent in a transaction with locking scripts

  • Specify the amount of satoshis for each output

  • Create spendable UTXOs for future transactions

  • Support standard payment types (P2PKH, P2PK, OP_RETURN)

  • Enable change outputs with automatic value calculation

  • Handle custom locking conditions for smart contracts

  • Serialize and deserialize outputs for transaction broadcasting

Basic Usage

import { Transaction, P2PKH, Script, OP } from '@bsv/sdk';

const tx = new Transaction();

// Add a standard P2PKH payment output
tx.addOutput({
  lockingScript: new P2PKH().lock('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'),
  satoshis: 10000
});

// Add a change output (amount calculated automatically)
tx.addOutput({
  lockingScript: new P2PKH().lock(changeAddress),
  change: true
});

// Add an OP_RETURN data output
tx.addOutput({
  lockingScript: new Script()
    .writeOpCode(OP.OP_FALSE)
    .writeOpCode(OP.OP_RETURN)
    .writeBin(Buffer.from('Hello BSV')),
  satoshis: 0
});

console.log('Outputs:', tx.outputs.length);
console.log('Total output value:', tx.outputs.reduce((sum, o) => sum + o.satoshis, 0));

Key Features

1. Standard Payment Outputs

Create outputs for sending payments to addresses:

2. Change Outputs

Automatically calculate change from transaction inputs:

3. OP_RETURN Data Outputs

Store arbitrary data on the blockchain:

4. Custom Locking Scripts

Create outputs with custom spending conditions:

API Reference

TransactionOutput Interface

Creating Outputs

Output Properties

Serialization

Common Patterns

Pattern 1: Payment Distribution

Split payment among multiple recipients:

Pattern 2: Data and Payment Combined

Store data while making payments:

Pattern 3: Atomic Swap Output Structure

Create outputs for atomic swap transactions:

Security Considerations

  1. Output Value Validation: Ensure output values don't exceed input values (accounting for fees).

  2. Change Address Security: Never reuse addresses. Generate new addresses for change outputs.

  3. Dust Outputs: Very small outputs (below dust threshold) may not be relayed by nodes. Minimum is typically 546 satoshis for P2PKH.

  4. OP_RETURN Data: While BSV supports large OP_RETURN, verify miner policies for data size acceptance.

  5. Locking Script Validation: Test custom locking scripts thoroughly before using with real funds.

  6. Zero-Value Outputs: Only OP_RETURN outputs should have zero satoshis. Payment outputs must have positive values.

Performance Considerations

  1. Output Count: More outputs increase transaction size and fees. Consolidate when possible.

  2. Script Complexity: Complex locking scripts increase size. Keep scripts simple when possible.

  3. OP_RETURN Size: Large OP_RETURN data increases transaction size proportionally. Consider overlay networks for very large data.

  4. Change Output Management: Always include change output to avoid losing funds to fees.

  5. Output Ordering: Output order doesn't affect validity but may affect privacy. Consider randomizing output order.

Code Examples

See complete working examples in:

Best Practices

  1. Always include change output to avoid losing funds to fees

  2. Never reuse addresses - generate new address for change

  3. Validate total output value doesn't exceed input value

  4. Use appropriate output types - P2PKH for payments, OP_RETURN for data

  5. Set OP_RETURN outputs to 0 satoshis unless protocol requires otherwise

  6. Check dust limits - ensure outputs meet minimum value requirements

  7. Test custom scripts thoroughly before production use

  8. Document OP_RETURN data format for protocol interoperability

  9. Consider miner policies for large data outputs

  10. Implement proper error handling for output creation failures

Troubleshooting

Issue: Output value exceeds input value

Solution: Ensure total outputs (including fees) don't exceed inputs.

Issue: Dust output rejected

Solution: Ensure outputs meet minimum dust threshold (546 satoshis for P2PKH).

Issue: Change output has negative value

Solution: Ensure sufficient input value to cover outputs and fees.

Issue: OP_RETURN too large

Solution: Check miner policies and consider splitting data or using overlay networks.

Further Reading

Status

✅ Complete

Last updated