|
| 1 | +//! Inscription envelope script builder |
| 2 | +//! |
| 3 | +//! Creates the taproot script containing the inscription data following |
| 4 | +//! the Ordinals protocol format. |
| 5 | +
|
| 6 | +use miniscript::bitcoin::opcodes::all::{ |
| 7 | + OP_CHECKSIG, OP_ENDIF, OP_IF, OP_PUSHBYTES_0, OP_PUSHNUM_1, |
| 8 | +}; |
| 9 | +use miniscript::bitcoin::opcodes::OP_FALSE; |
| 10 | +use miniscript::bitcoin::script::{Builder, PushBytesBuf}; |
| 11 | +use miniscript::bitcoin::secp256k1::XOnlyPublicKey; |
| 12 | +use miniscript::bitcoin::ScriptBuf; |
| 13 | + |
| 14 | +/// Maximum size of a single data push in tapscript (520 bytes) |
| 15 | +const MAX_PUSH_SIZE: usize = 520; |
| 16 | + |
| 17 | +/// Split data into chunks of at most MAX_PUSH_SIZE bytes |
| 18 | +fn split_into_chunks(data: &[u8]) -> Vec<&[u8]> { |
| 19 | + data.chunks(MAX_PUSH_SIZE).collect() |
| 20 | +} |
| 21 | + |
| 22 | +/// Build an inscription envelope script |
| 23 | +/// |
| 24 | +/// The script follows the Ordinals protocol format: |
| 25 | +/// ```text |
| 26 | +/// <pubkey> OP_CHECKSIG OP_FALSE OP_IF |
| 27 | +/// "ord" |
| 28 | +/// OP_1 OP_1 <content_type> |
| 29 | +/// OP_0 <data_chunk_1> <data_chunk_2> ... |
| 30 | +/// OP_ENDIF |
| 31 | +/// ``` |
| 32 | +/// |
| 33 | +/// # Arguments |
| 34 | +/// * `internal_key` - The x-only public key for the taproot output |
| 35 | +/// * `content_type` - MIME type of the inscription (e.g., "text/plain", "image/png") |
| 36 | +/// * `data` - The inscription data |
| 37 | +/// |
| 38 | +/// # Returns |
| 39 | +/// A compiled Bitcoin script containing the inscription |
| 40 | +pub fn build_inscription_script( |
| 41 | + internal_key: &XOnlyPublicKey, |
| 42 | + content_type: &str, |
| 43 | + data: &[u8], |
| 44 | +) -> ScriptBuf { |
| 45 | + let mut builder = Builder::new(); |
| 46 | + |
| 47 | + // <pubkey> OP_CHECKSIG |
| 48 | + builder = builder.push_x_only_key(internal_key); |
| 49 | + builder = builder.push_opcode(OP_CHECKSIG); |
| 50 | + |
| 51 | + // OP_FALSE OP_IF (start inscription envelope) |
| 52 | + builder = builder.push_opcode(OP_FALSE); |
| 53 | + builder = builder.push_opcode(OP_IF); |
| 54 | + |
| 55 | + // "ord" - protocol identifier |
| 56 | + let ord_bytes = PushBytesBuf::try_from(b"ord".to_vec()).expect("ord is 3 bytes"); |
| 57 | + builder = builder.push_slice(ord_bytes); |
| 58 | + |
| 59 | + // OP_1 OP_1 - content type tag |
| 60 | + // Note: The ordinals decoder has a quirk where it expects two separate OP_1s |
| 61 | + // instead of a single OP_PUSHNUM_1 |
| 62 | + builder = builder.push_opcode(OP_PUSHNUM_1); |
| 63 | + builder = builder.push_opcode(OP_PUSHNUM_1); |
| 64 | + |
| 65 | + // <content_type> |
| 66 | + let content_type_bytes = |
| 67 | + PushBytesBuf::try_from(content_type.as_bytes().to_vec()).expect("content type too long"); |
| 68 | + builder = builder.push_slice(content_type_bytes); |
| 69 | + |
| 70 | + // OP_0 - body tag |
| 71 | + builder = builder.push_opcode(OP_PUSHBYTES_0); |
| 72 | + |
| 73 | + // Data chunks (split into MAX_PUSH_SIZE byte chunks) |
| 74 | + for chunk in split_into_chunks(data) { |
| 75 | + let chunk_bytes = PushBytesBuf::try_from(chunk.to_vec()).expect("chunk is <= 520 bytes"); |
| 76 | + builder = builder.push_slice(chunk_bytes); |
| 77 | + } |
| 78 | + |
| 79 | + // OP_ENDIF (end inscription envelope) |
| 80 | + builder = builder.push_opcode(OP_ENDIF); |
| 81 | + |
| 82 | + builder.into_script() |
| 83 | +} |
| 84 | + |
| 85 | +#[cfg(test)] |
| 86 | +mod tests { |
| 87 | + use super::*; |
| 88 | + use miniscript::bitcoin::hashes::Hash; |
| 89 | + use miniscript::bitcoin::secp256k1::{Secp256k1, SecretKey}; |
| 90 | + use miniscript::bitcoin::XOnlyPublicKey; |
| 91 | + |
| 92 | + fn test_pubkey() -> XOnlyPublicKey { |
| 93 | + let secp = Secp256k1::new(); |
| 94 | + let secret_key = SecretKey::from_slice(&[1u8; 32]).expect("32 bytes, within curve order"); |
| 95 | + let (xonly, _parity) = secret_key.x_only_public_key(&secp); |
| 96 | + xonly |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn test_build_inscription_script_simple() { |
| 101 | + let pubkey = test_pubkey(); |
| 102 | + let script = build_inscription_script(&pubkey, "text/plain", b"Hello, World!"); |
| 103 | + |
| 104 | + // Verify the script contains expected elements |
| 105 | + let script_bytes = script.as_bytes(); |
| 106 | + assert!(script_bytes.len() > 50); // Should have reasonable length |
| 107 | + |
| 108 | + // Check for "ord" in the script |
| 109 | + let script_hex = hex::encode(script_bytes); |
| 110 | + assert!(script_hex.contains(&hex::encode(b"ord"))); |
| 111 | + |
| 112 | + // Check for content type |
| 113 | + assert!(script_hex.contains(&hex::encode(b"text/plain"))); |
| 114 | + |
| 115 | + // Check for data |
| 116 | + assert!(script_hex.contains(&hex::encode(b"Hello, World!"))); |
| 117 | + } |
| 118 | + |
| 119 | + #[test] |
| 120 | + fn test_build_inscription_script_large_data() { |
| 121 | + let pubkey = test_pubkey(); |
| 122 | + // Create data larger than MAX_PUSH_SIZE |
| 123 | + let large_data = vec![0xABu8; 1000]; |
| 124 | + let script = build_inscription_script(&pubkey, "application/octet-stream", &large_data); |
| 125 | + |
| 126 | + // Script should be created successfully |
| 127 | + assert!(script.as_bytes().len() > 1000); |
| 128 | + } |
| 129 | +} |
0 commit comments