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

Parses 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

Parses 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

Adds a transaction to the BEEF package.

Parameters:

  • tx: Transaction - The transaction to add

Example:

addMerklePath(path: MerklePath): void

Adds a merkle proof to the BEEF package.

Parameters:

  • path: MerklePath - The merkle path for SPV validation

Example:

getTransactions(): Transaction[]

Returns all transactions in the BEEF package.

Returns: Transaction[] - Array of transactions

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[]

Serializes the BEEF package to binary format.

Returns: number[] - Binary BEEF data

toHex(): string

Serializes 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

  1. Merkle Proof Validation: Always verify merkle proofs with a trusted chain tracker before accepting BEEF packages. Invalid proofs indicate fraudulent or unconfirmed transactions.

  2. Transaction Dependency Verification: Verify that all referenced input transactions are included in the BEEF package or have valid merkle proofs.

  3. Double-Spend Detection: BEEF packages don't prevent double-spends. Always verify transactions against your UTXO set and check for conflicts.

  4. Size Limits: Implement reasonable size limits for BEEF packages to prevent DoS attacks through oversized packages.

  5. Chain Tracker Trust: The security of BEEF verification depends on the reliability of your chain tracker implementation. Use trusted data sources.

  6. Package Integrity: Verify the integrity of BEEF packages during transmission using checksums or signatures to detect tampering.

Performance Considerations

  1. Version Selection: Use Version 2 BEEF for large transaction packages to benefit from optimizations. Version 1 is simpler for small packages.

  2. Binary Format: Use binary serialization for storage and transmission. Hex encoding adds 100% size overhead.

  3. Compression: BEEF packages compress well with gzip. Implement compression for long-term storage or network transmission.

  4. Incremental Parsing: For large BEEF packages, parse transactions incrementally rather than loading the entire package into memory.

  5. Caching: Cache merkle proof verifications to avoid redundant chain tracker queries for the same block heights.

  6. Batch Processing: When processing multiple BEEF packages, batch chain tracker queries for better performance.

Code Examples

See complete working examples in:

Best Practices

  1. Always include merkle proofs for confirmed transactions to enable SPV validation

  2. Order transactions by dependency (parents before children) for easier validation

  3. Use Version 2 BEEF for production systems with large transaction packages

  4. Implement size limits (e.g., 10MB) to prevent resource exhaustion

  5. Compress BEEF packages for storage and network transmission

  6. Verify package integrity before processing to detect tampering

  7. Cache merkle root verifications to improve performance

  8. Use trusted chain trackers with fallback options for reliability

  9. Implement atomic validation - accept or reject entire packages, not partial

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

Status

✅ Complete

Last updated