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
Output Value Validation: Ensure output values don't exceed input values (accounting for fees).
Change Address Security: Never reuse addresses. Generate new addresses for change outputs.
Dust Outputs: Very small outputs (below dust threshold) may not be relayed by nodes. Minimum is typically 546 satoshis for P2PKH.
OP_RETURN Data: While BSV supports large OP_RETURN, verify miner policies for data size acceptance.
Locking Script Validation: Test custom locking scripts thoroughly before using with real funds.
Zero-Value Outputs: Only OP_RETURN outputs should have zero satoshis. Payment outputs must have positive values.
Performance Considerations
Output Count: More outputs increase transaction size and fees. Consolidate when possible.
Script Complexity: Complex locking scripts increase size. Keep scripts simple when possible.
OP_RETURN Size: Large OP_RETURN data increases transaction size proportionally. Consider overlay networks for very large data.
Change Output Management: Always include change output to avoid losing funds to fees.
Output Ordering: Output order doesn't affect validity but may affect privacy. Consider randomizing output order.
Related Components
Transaction - Build complete transactions with outputs
TransactionInput - Inputs that reference outputs
Script - Create locking scripts
ScriptTemplate - Templates for common locking patterns
P2PKH - Standard payment template
Code Examples
See complete working examples in:
Best Practices
Always include change output to avoid losing funds to fees
Never reuse addresses - generate new address for change
Validate total output value doesn't exceed input value
Use appropriate output types - P2PKH for payments, OP_RETURN for data
Set OP_RETURN outputs to 0 satoshis unless protocol requires otherwise
Check dust limits - ensure outputs meet minimum value requirements
Test custom scripts thoroughly before production use
Document OP_RETURN data format for protocol interoperability
Consider miner policies for large data outputs
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
Bitcoin Outputs - Transaction output structure
OP_RETURN - Data storage on blockchain
Dust - Understanding dust limits
Locking Scripts - Bitcoin Script for outputs
BRC-8 - Transaction envelopes
BSV SDK Documentation - Official SDK docs
Status
✅ Complete
Last updated
