BEEF
Overview
The Beef class in the BSV TypeScript SDK implements BRC-62, the Background Evaluation Extended Format for Bitcoin transaction packages. BEEF provides a standardized way to bundle transactions with their dependencies and merkle proofs, enabling efficient SPV validation without requiring access to the full blockchain. It creates atomic transaction envelopes that include all necessary proof data for independent verification.
Purpose
Bundle transactions with their input dependencies into atomic packages
Include merkle proofs for SPV validation of transaction ancestry
Enable offline transaction verification without blockchain access
Reduce data transmission overhead for transaction chains
Standardize transaction package format for interoperability
Support both Version 1 (simple) and Version 2 (optimized) BEEF formats
Basic Usage
Creating a BEEF Package
import { Beef, Transaction, MerklePath } from '@bsv/sdk';
// Create a BEEF package
const beef = new Beef();
// Add transactions to the package
const parentTx = Transaction.fromHex('0100000001...');
const childTx = Transaction.fromHex('0100000001...');
beef.addTransaction(parentTx);
beef.addTransaction(childTx);
// Add merkle proof for SPV validation
const merklePath = MerklePath.fromHex('fed7c509000a02fddd01...');
beef.addMerklePath(merklePath);
// Serialize to hex for transmission
const beefHex = beef.toHex();
console.log('BEEF package:', beefHex);
// Parse BEEF package
const parsedBeef = Beef.fromHex(beefHex);
console.log('Transactions:', parsedBeef.getTransactions());Verifying a BEEF Package
Key Features
1. Transaction Bundle Creation
BEEF packages bundle transactions with their dependencies for atomic transmission:
2. Merkle Proof Integration
BEEF includes merkle proofs for SPV validation of transaction ancestry:
3. Version 1 and Version 2 Formats
BEEF supports two formats for different optimization levels:
4. Atomic Transaction Validation
BEEF enables atomic validation of entire transaction chains:
API Reference
Constructor
Creates a new BEEF package.
Parameters:
version?: number- BEEF format version (1 or 2, default: 2)
Static Methods
Beef.fromBinary(binary: number[]): Beef
Beef.fromBinary(binary: number[]): BeefParses a BEEF package from binary format.
Parameters:
binary: number[]- The binary BEEF data
Returns: Beef - The parsed BEEF package
Example:
Beef.fromHex(hex: string): Beef
Beef.fromHex(hex: string): BeefParses a BEEF package from hexadecimal string.
Parameters:
hex: string- The hexadecimal BEEF string
Returns: Beef - The parsed BEEF package
Example:
Instance Methods
addTransaction(tx: Transaction): void
addTransaction(tx: Transaction): voidAdds a transaction to the BEEF package.
Parameters:
tx: Transaction- The transaction to add
Example:
addMerklePath(path: MerklePath): void
addMerklePath(path: MerklePath): voidAdds a merkle proof to the BEEF package.
Parameters:
path: MerklePath- The merkle path for SPV validation
Example:
getTransactions(): Transaction[]
getTransactions(): Transaction[]Returns all transactions in the BEEF package.
Returns: Transaction[] - Array of transactions
verify(chainTracker: ChainTracker): Promise<boolean>
verify(chainTracker: ChainTracker): Promise<boolean>Verifies the BEEF package merkle proofs.
Parameters:
chainTracker: ChainTracker- Interface for verifying merkle roots
Returns: Promise<boolean> - True if all proofs are valid
toBinary(): number[]
toBinary(): number[]Serializes the BEEF package to binary format.
Returns: number[] - Binary BEEF data
toHex(): string
toHex(): stringSerializes the BEEF package to hexadecimal string.
Returns: string - Hexadecimal BEEF string
Instance Properties
Common Patterns
Pattern 1: Payment Channel Transaction Bundle
Creating BEEF packages for payment channel updates:
Pattern 2: Atomic Multi-Party Transaction
Using BEEF for atomic multi-party transactions:
Pattern 3: Transaction Chain Archival
Using BEEF for long-term transaction chain storage:
Security Considerations
Merkle Proof Validation: Always verify merkle proofs with a trusted chain tracker before accepting BEEF packages. Invalid proofs indicate fraudulent or unconfirmed transactions.
Transaction Dependency Verification: Verify that all referenced input transactions are included in the BEEF package or have valid merkle proofs.
Double-Spend Detection: BEEF packages don't prevent double-spends. Always verify transactions against your UTXO set and check for conflicts.
Size Limits: Implement reasonable size limits for BEEF packages to prevent DoS attacks through oversized packages.
Chain Tracker Trust: The security of BEEF verification depends on the reliability of your chain tracker implementation. Use trusted data sources.
Package Integrity: Verify the integrity of BEEF packages during transmission using checksums or signatures to detect tampering.
Performance Considerations
Version Selection: Use Version 2 BEEF for large transaction packages to benefit from optimizations. Version 1 is simpler for small packages.
Binary Format: Use binary serialization for storage and transmission. Hex encoding adds 100% size overhead.
Compression: BEEF packages compress well with gzip. Implement compression for long-term storage or network transmission.
Incremental Parsing: For large BEEF packages, parse transactions incrementally rather than loading the entire package into memory.
Caching: Cache merkle proof verifications to avoid redundant chain tracker queries for the same block heights.
Batch Processing: When processing multiple BEEF packages, batch chain tracker queries for better performance.
Related Components
Transaction - Create and manage transactions
MerklePath - Work with merkle proofs
SPV - Simplified Payment Verification
TransactionInput - Handle transaction inputs
Code Examples
See complete working examples in:
Best Practices
Always include merkle proofs for confirmed transactions to enable SPV validation
Order transactions by dependency (parents before children) for easier validation
Use Version 2 BEEF for production systems with large transaction packages
Implement size limits (e.g., 10MB) to prevent resource exhaustion
Compress BEEF packages for storage and network transmission
Verify package integrity before processing to detect tampering
Cache merkle root verifications to improve performance
Use trusted chain trackers with fallback options for reliability
Implement atomic validation - accept or reject entire packages, not partial
Archive BEEF packages for audit trails and dispute resolution
Troubleshooting
Issue: BEEF verification fails with valid transactions
Solution: Ensure chain tracker has access to correct merkle roots.
Issue: BEEF package too large
Solution: Use Version 2 format and compression.
Issue: Transaction ordering errors
Solution: Add transactions in dependency order (parents first).
Issue: Missing merkle proofs
Solution: Fetch proofs from blockchain services before creating BEEF.
Further Reading
BRC-62: BEEF Specification - Official BEEF format specification
BRC-8: Transaction Envelopes - Transaction envelope format
BRC-67: SPV Validation - SPV validation rules
Merkle Trees - Understanding merkle trees in Bitcoin
SPV - Simplified Payment Verification overview
BSV SDK Documentation - Official SDK documentation
Status
✅ Complete
Last updated
