Shia 🥩 - The BSV BEEF Protocol Reference Implementation 🐄
A Rust crate implementing the BSV BEEF (Background Evaluation Extended Format) protocol (BRC-62) for Simplified Payment Verification (SPV) on Bitcoin SV. It supports parsing, serialization, building from transaction DAGs, and verification of transactions with Merkle proofs using BUMP (BRC-74). Designed for integration into BSV wallet stacks, with script evaluation via the rust-sv crate. Includes support for atomic BEEF (BRC-95) and hooks for Paymail envelopes (BRC-70).
Features 🔧
- Parsing & Serialization: Deserialize from hex/binary and serialize BEEF data, including atomic prefixes and variable-length tx sections.
- Building: Construct BEEF from subject transaction, ancestors (TXID → Tx map), and bump proofs (TXID → Bump map) with topological sorting via Kahn's algorithm (cycle detection included).
- Verification: Full SPV validation including Merkle root checks against block headers, UTXO tracking, value balance (fees ≥ 0, skipping coinbase), and script execution (P2PKH, multisig, etc., via
rust-sv). - Atomic BEEF Support: Enforce BRC-95 atomicity—only subject tx + direct ancestors, no siblings or unrelated txs—for focused micropayment proofs.
- BUMP Integration: Unique Merkle proofs for tx inclusion, with deduplication during build.
- Extensibility:
BlockHeadersClienttrait for custom header oracles (e.g., local index, remote Pulse API). Optional Paymail envelope wrapping (feature-gated). - Error Handling: Detailed errors via
thiserror(e.g.,Parse("Extra bytes"),Verification("Missing UTXO"),AtomicMismatch). - Dependencies: Lean stack with
nourfor BSV-specific tx/script handling (P2P, sighash, etc.).
Installation 📦
Add to your Cargo.toml:
[]
= "0.1.0"
For development (from GitHub, latest on dev branch):
[]
= { = "https://github.com/murphsicles/shia", = "dev" }
For Paymail support (optional):
[]
= { = "0.1.0", = ["paymail"] }
- Requires Rust 1.85+ for edition 2024 features.
- CI workflow tested up to Rust 1.91. ✅
- No external services needed—pure offline SPV.
Quick Start 📖
Basic Parsing & Round-Trip
use Beef;
use hex;
let beef_hex = "efbe00010001"; // Minimal: version (0100BEEF LE) + nBumps=0 + nTxs=1 + tx raw + flag=00
let tx_raw = hex!;
let minimal_beef_hex = format!;
let beef = from_hex.expect;
let serialized = beef.serialize.expect;
assert_eq!;
Building BEEF from DAG
use ;
use HashMap;
use hex;
let subject_raw = hex!;
let subject_tx = from_raw.unwrap;
// Example ancestor (parent tx)
let ancestor_raw = hex!; // Parent tx raw bytes
let ancestor_tx = from_raw.unwrap;
let ancestors = from;
// Example bump for subject
let bump = new; // Simplified
let bump_map = from;
let beef = build.unwrap; // Non-atomic
assert_eq!; // Subject + ancestor
SPV Verification
use BlockHeadersClient;
// Implement trait for your header source (e.g., local DB, remote API)
;
let mock_client = MyHeadersClient;
beef.verify.expect;
Atomic BEEF (BRC-95)
For single-subject graphs (e.g., micropayments), enable atomic mode:
let atomic_beef = build.unwrap;
assert!;
atomic_beef.verify.expect;
// Validation enforces: txs.len() == ancestors.len(), all trace to subject
TxID & Merkle Hash Convention
BSV txids are the reversed bytes of double-SHA256(raw tx) for display/hex (big-endian convention). merkle_hash() returns unreversed for BUMP leaves.
Example (Genesis Coinbase):
let genesis_raw = hex!;
let tx = from_raw.unwrap;
let txid = tx.txid; // [0x4a, 0x5e, ... 0x3b] → "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"
let merkle = tx.merkle_hash; // Unreversed for proofs
Structure 📁
src/tx.rs: BSV Transaction wrapper—parsing (from_raw), hashing (txid/merkle_hash), script verification (verify_scriptswith UTXO map).src/bump.rs: BRC-74 Bump struct—serialization, Merkle root computation for tx hashes.src/beef.rs: Core Beef struct—build (build), parse (from_hex/deserialize), verify (verify), serialize.src/atomic.rs: BRC-95 validation—validate_atomicensures ancestry-only graphs.src/client.rs:BlockHeadersClienttrait for Merkle root checks (e.g., against Teranode/Galaxy).src/utils.rs: VarInt read/write, double-SHA256 helpers.src/errors.rs: Comprehensive errors (Parse, Verification, ScriptEval, AtomicMismatch, etc.).
Tests 🧪
Run with cargo test --lib for unit tests (parsing, building, verification, scripts). Doctests via cargo test --doc. CI (GitHub Actions) covers Rustfmt, Clippy, and full suite on push/PR. 100% coverage on core paths; includes genesis tx, P2PKH eval, topo-sort cycles.
Example failing case (pre-fix): Extra bytes in tx raw → Parse("Extra bytes after transaction").
Roadmap 🗺️
- v0.2.0: BRC-96 txid-only extension (compact agreed-tx representation).
- Paymail Full: Default-enabled BRC-70 envelopes for p2p payments.
- Async/Streaming: Non-blocking verify for high-volume wallets.
- Bindings: WASM/JS for web SPV, C FFI for C++ nodes.
- Contributions: PRs for BRC updates, more script ops, or header clients welcome!
Dependencies 📚
- Core:
byteorder,hex,thiserror,anyhow,sha2. - BSV:
sv(from GitHub:git+https://github.com/murphsicles/rust-sv.git). - No runtime deps on services—offline-first.
License 📄
MIT License. See LICENSE for details.
Contributing 🤝
- Fork & clone:
git clone https://github.com/murphsicles/shia. - Branch:
git checkout -b feature/my-update. - Test:
cargo test --lib --doc. - Lint:
cargo fmt && cargo clippy. - PR to
devbranch with description/tests.
Issues/PRs at GitHub. Focus on BSV scalability!
Contact 📧
murphsicles – Built for the BSV Freedom Stack & Galaxy Node architecture. Questions? Open an issue!