bitcoin-embed
A library for embedding arbitrary data and TLV-encoded messages in Bitcoin transactions. Supports OP_RETURN outputs, witness script envelopes, and taproot annexes.
Installation
[]
= "0.1.0"
Features
-
Embedding Extraction: Extract data from Bitcoin transactions with detailed location information. Supports:
- OP_RETURN outputs
- Taproot annexes
OP_FALSE OP_IF ... OP_ENDIFwitness envelopes (supports P2TR and P2WSH)
Note: P2WSH envelopes require inputs with at least 2 witness elements
-
TLV Message Encoding: Efficiently encode and decode a series of tagged messages
-
Script Embedding: Embed arbitrary data in Bitcoin script using an
OP_FALSE OP_IF ... OP_ENDIFscript envelope
Message Encoding Scheme
The library implements an efficient binary encoding scheme for tagged messages:
- Space-Efficient: Tags and message lengths are encoded as LEB128 variable-length integers to minimize bytes
- Tag Deduplication: Repeated consecutive tags use a special marker (0) instead of repeating the full tag
- Explicit Termination: The initial LEB128 integer represents
2 * tag + (1 if terminal tag else 0)to efficiently encode the tag and indicate termination - Compact Format: The final message doesn't include an explicit length, saving bytes
This encoding scheme is valuable for embedding data in Bitcoin transactions where multiple messages must be encoded in the same location. It allows for up to $2^{127}-1$ unique tags while minimizing the overhead needed to encode.
Basic Usage
Embedding Data
use ;
use envelope;
// Create an envelope with data
let builder = new;
let builder_with_data = append_bytes_to_builder;
Extracting Embedded Data
use Embedding;
// Extract all embedded data from a transaction
let tx = /* A Transaction object */;
let embeddings = from_transaction;
for embed in embeddings
Working with Tagged Messages
use Message;
// Create a message with a tag and data
let msg = new.unwrap;
// Encode multiple messages
let msg2 = /** A second message */
let encoded = encode;
// Decode messages from bytes
let decoded = decode.unwrap;
License
This project is licensed under the CC0-1.0 License.
Author
Joshua Doman joshsdoman@gmail.com