| Crates.io | bitcoin-embed |
| lib.rs | bitcoin-embed |
| version | 0.1.0 |
| created_at | 2025-06-17 02:14:31.570382+00 |
| updated_at | 2025-06-17 02:14:31.570382+00 |
| description | A library for embedding arbitrary data and TLV-encoded messages in Bitcoin transactions |
| homepage | https://github.com/joshdoman/bitcoin-embed |
| repository | https://github.com/joshdoman/bitcoin-embed |
| max_upload_size | |
| id | 1715105 |
| size | 73,179 |
A library for embedding arbitrary data and TLV-encoded messages in Bitcoin transactions. Supports OP_RETURN outputs, witness script envelopes, and taproot annexes.
[dependencies]
bitcoin-embed = "0.1.0"
Embedding Extraction: Extract data from Bitcoin transactions with detailed location information. Supports:
OP_FALSE OP_IF ... OP_ENDIF witness 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_ENDIF script envelope
The library implements an efficient binary encoding scheme for tagged messages:
2 * tag + (1 if terminal tag else 0) to efficiently encode the tag and indicate terminationThis 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.
use bitcoin::{Transaction, script::Builder};
use bitcoin_embed::envelope;
// Create an envelope with data
let builder = Builder::new();
let builder_with_data = envelope::append_bytes_to_builder(b"Hello, Bitcoin!", builder);
use bitcoin_embed::Embedding;
// Extract all embedded data from a transaction
let tx = /* A Transaction object */;
let embeddings = Embedding::from_transaction(&tx);
for embed in embeddings {
match embed.location {
// Handle OP_RETURN data
EmbeddingLocation::OpReturn { output } => {
println!("Found OP_RETURN data at output {}: {:?}", output, embed.bytes);
},
// Handle taproot annex data
EmbeddingLocation::TaprootAnnex { input } => {
println!("Found taproot annex data at input {}: {:?}", input, embed.bytes);
},
// Handle envelope data (P2WSH or Tapscript)
EmbeddingLocation::WitnessEnvelope { input, index, script_type, .. } => {
println!("Found envelope data at input {} (index {}): {:?}",
input, index, embed.bytes);
println!("Script type: {:?}", script_type);
}
}
}
use bitcoin_embed::message::Message;
// Create a message with a tag and data
let msg = Message::new(42, b"Tagged data".to_vec()).unwrap();
// Encode multiple messages
let msg2 = /** A second message */
let encoded = Message::encode(vec![msg, msg2]);
// Decode messages from bytes
let decoded = Message::decode(&encoded).unwrap();
This project is licensed under the CC0-1.0 License.
Joshua Doman joshsdoman@gmail.com