-
Notifications
You must be signed in to change notification settings - Fork 6
✨ SMT Storage #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
KyrylR
merged 3 commits into
BlockstreamResearch:main
from
LesterEvSe:feature/smt-storage
Feb 5, 2026
Merged
✨ SMT Storage #49
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| use crate::commands::NETWORK; | ||
| use crate::explorer::{broadcast_tx, fetch_utxo}; | ||
| use crate::modules::utils::derive_keypair; | ||
| use clap::Subcommand; | ||
| use contracts::smt_storage::{ | ||
| DEPTH, SMTWitness, SparseMerkleTree, finalize_get_storage_transaction, get_path_bits, | ||
| get_smt_storage_compiled_program, smt_storage_taproot_spend_info, | ||
| }; | ||
| use simplicityhl::elements::pset::serialize::Serialize; | ||
| use simplicityhl::simplicity::elements::OutPoint; | ||
| use simplicityhl::simplicity::elements::Script; | ||
| use simplicityhl::simplicity::elements::taproot::TaprootSpendInfo; | ||
| use simplicityhl::simplicity::hex::DisplayHex; | ||
| use simplicityhl::tracker::TrackerLogLevel; | ||
| use simplicityhl_core::{create_p2pk_signature, finalize_p2pk_transaction, hash_script}; | ||
|
|
||
| fn parse_hex_32(s: &str) -> Result<[u8; 32], String> { | ||
| let bytes = hex::decode(s).map_err(|_| "Invalid hex string".to_string())?; | ||
|
|
||
| if bytes.len() != 32 { | ||
| return Err(format!( | ||
| "Expected 32 bytes (64 hex characters), got {}", | ||
| bytes.len() | ||
| )); | ||
| } | ||
|
|
||
| let mut array = [0u8; 32]; | ||
| array.copy_from_slice(&bytes); | ||
| Ok(array) | ||
| } | ||
|
|
||
| fn parse_bit_path(s: &str) -> Result<[bool; DEPTH], String> { | ||
| if s.len() != DEPTH { | ||
| return Err(format!("Expected 7 bits, got {}", s.len())); | ||
| } | ||
|
|
||
| let mut path = [false; DEPTH]; | ||
|
|
||
| for (ind, char) in s.char_indices() { | ||
| if char == 'r' { | ||
| path[ind] = true; | ||
| } else if char == 'l' { | ||
| path[ind] = false; | ||
| } else { | ||
| return Err(String::from( | ||
| "Expected only 'r' and 'l' symbols, got something else.", | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| Ok(path) | ||
| } | ||
|
|
||
| /// SMT Storage contract utilities | ||
| #[derive(Subcommand, Debug)] | ||
| pub enum SMTStorage { | ||
| /// Lock collateral on the storage contract (Mint/Fund operation) | ||
| GetStorageAddress { | ||
| /// The initial 32-byte data payload to store in the tree at the specified path | ||
| #[arg(long = "storage-bytes", value_parser = parse_hex_32)] | ||
| storage_bytes: [u8; 32], | ||
| /// The path in the Merkle Tree use for the contract logic (e.g., "rrll...") | ||
| #[arg(long = "path", value_parser = parse_bit_path)] | ||
| path: [bool; DEPTH], | ||
|
|
||
| /// Account that will pay for transaction fees and that owns a tokens to send | ||
| #[arg(long = "account-index", default_value_t = 0)] | ||
| account_index: u32, | ||
| }, | ||
| /// Build tx transferring an asset UTXO to recipient (LBTC UTXO pays fees) and updating state | ||
| TransferFromStorageAddress { | ||
| /// Transaction id (hex) and output index (vout) of the ASSET UTXO you will spend | ||
| #[arg(long = "storage-utxo")] | ||
| storage_utxo: OutPoint, | ||
| /// Transaction id (hex) and output index (vout) of the LBTC UTXO used to pay fees (P2PK) | ||
| #[arg(long = "fee-utxo")] | ||
| fee_utxo: OutPoint, | ||
| /// Miner fee in satoshis (LBTC) | ||
| #[arg(long = "fee-sats")] | ||
| fee_amount: u64, | ||
|
|
||
| /// The current 32-byte data payload stored in the contract (Pre-state) | ||
| #[arg(long = "storage-bytes", value_parser = parse_hex_32)] | ||
| storage_bytes: [u8; 32], | ||
| /// The new 32-byte data payload to replace the old one (Post-state) | ||
| #[arg(long = "changed-bytes", value_parser = parse_hex_32)] | ||
| changed_bytes: [u8; 32], | ||
| /// The Merkle path used to generate the witness for the state transition | ||
| #[arg(long = "path", value_parser = parse_bit_path)] | ||
| path: [bool; DEPTH], | ||
|
|
||
| /// Account that will pay for transaction fees and that owns a tokens to send | ||
| #[arg(long = "account-index", default_value_t = 0)] | ||
| account_index: u32, | ||
| /// When set, broadcast the built transaction via Esplora and print txid | ||
| #[arg(long = "broadcast")] | ||
| broadcast: bool, | ||
| }, | ||
| } | ||
|
|
||
| impl SMTStorage { | ||
| /// Handle basic CLI subcommand execution. | ||
| /// | ||
| /// # Errors | ||
| /// Returns error if the subcommand operation fails. | ||
| /// | ||
| /// # Panics | ||
| /// Panics if asset entropy conversion fails. | ||
| pub async fn handle(&self) -> anyhow::Result<()> { | ||
| match self { | ||
| Self::GetStorageAddress { | ||
| storage_bytes, | ||
| path, | ||
| account_index, | ||
| } => { | ||
| let keypair = derive_keypair(*account_index); | ||
| let public_key = keypair.x_only_public_key().0; | ||
|
|
||
| let address = contracts::sdk::get_storage_address( | ||
| &public_key, | ||
| storage_bytes, | ||
| *path, | ||
| NETWORK, | ||
| )?; | ||
|
|
||
| let mut script_hash: [u8; 32] = hash_script(&address.script_pubkey()); | ||
| script_hash.reverse(); | ||
|
|
||
| println!("X Only Public Key: {public_key}"); | ||
| println!("P2PK Address: {address}"); | ||
| println!("Script hash: {}", hex::encode(script_hash)); | ||
|
|
||
| Ok(()) | ||
| } | ||
| Self::TransferFromStorageAddress { | ||
| storage_utxo: storage_utxo_outpoint, | ||
| fee_utxo: fee_utxo_outpoint, | ||
| fee_amount, | ||
| storage_bytes, | ||
| changed_bytes, | ||
| path, | ||
| account_index, | ||
| broadcast, | ||
| } => { | ||
| let keypair = derive_keypair(*account_index); | ||
| let public_key = keypair.x_only_public_key().0; | ||
|
|
||
| let storage_tx_out = fetch_utxo(*storage_utxo_outpoint).await?; | ||
| let fee_tx_out = fetch_utxo(*fee_utxo_outpoint).await?; | ||
|
|
||
| let mut smt = SparseMerkleTree::new(); | ||
| let merkle_hashes = smt.update(storage_bytes, *path); | ||
|
|
||
| let merkle_data = | ||
| std::array::from_fn(|i| (merkle_hashes[DEPTH - i - 1], path[DEPTH - i - 1])); | ||
|
|
||
| let witness = SMTWitness::new( | ||
| &public_key.serialize(), | ||
| storage_bytes, | ||
| get_path_bits(path, true), | ||
| &merkle_data, | ||
| ); | ||
| smt.update(changed_bytes, *path); | ||
|
|
||
| let program = get_smt_storage_compiled_program(); | ||
| let cmr = program.commit().cmr(); | ||
|
|
||
| let old_spend_info: TaprootSpendInfo = | ||
| smt_storage_taproot_spend_info(public_key, storage_bytes, &merkle_data, cmr); | ||
|
|
||
| let new_spend_info = | ||
| smt_storage_taproot_spend_info(public_key, changed_bytes, &merkle_data, cmr); | ||
| let new_script_pubkey = Script::new_v1_p2tr_tweaked(new_spend_info.output_key()); | ||
|
|
||
| let pst = contracts::sdk::transfer_asset_with_storage( | ||
| (*storage_utxo_outpoint, storage_tx_out.clone()), | ||
| (*fee_utxo_outpoint, fee_tx_out.clone()), | ||
| *fee_amount, | ||
| &new_script_pubkey, | ||
| )?; | ||
|
|
||
| let tx = pst.extract_tx()?; | ||
| let utxos = vec![storage_tx_out, fee_tx_out]; | ||
|
|
||
| let tx = finalize_get_storage_transaction( | ||
| tx, | ||
| &old_spend_info, | ||
| &witness, | ||
| &program, | ||
| &utxos, | ||
| 0, | ||
| NETWORK, | ||
| TrackerLogLevel::None, | ||
| )?; | ||
|
|
||
| let signature = create_p2pk_signature(&tx, &utxos, &keypair, 1, NETWORK)?; | ||
| let tx = finalize_p2pk_transaction( | ||
| tx, | ||
| &utxos, | ||
| &public_key, | ||
| &signature, | ||
| 1, | ||
| NETWORK, | ||
| TrackerLogLevel::None, | ||
| )?; | ||
|
|
||
| if *broadcast { | ||
| println!("Broadcasted txid: {}", broadcast_tx(&tx).await?); | ||
| } else { | ||
| println!("{}", tx.serialize().to_lower_hex_string()); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| use simplicityhl::elements::schnorr::XOnlyPublicKey; | ||
| use simplicityhl::simplicity::elements::Address; | ||
| use simplicityhl::simplicity::elements::Script; | ||
| use simplicityhl::simplicity::elements::taproot::TaprootSpendInfo; | ||
| use simplicityhl_core::SimplicityNetwork; | ||
|
|
||
| use crate::error::TransactionBuildError; | ||
| use crate::smt_storage::{ | ||
| DEPTH, SparseMerkleTree, get_smt_storage_compiled_program, smt_storage_taproot_spend_info, | ||
| }; | ||
|
|
||
| /// Derives the Taproot address for the SMT storage contract based on its initial state. | ||
| /// | ||
| /// This function calculates the script pubkey by committing to the Simplicity program | ||
| /// configured with the provided `storage_bytes` (root hash) and `path`. It then | ||
| /// encodes this script into a network-specific address. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an error if: | ||
| /// - The function signature requires a `Result` for consistency with the builder API, | ||
| /// though the current implementation primarily panics on failure rather than returning `Err`. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// Panics if: | ||
| /// - The generated script is invalid for address creation (e.g., invalid witness program). | ||
| pub fn get_storage_address( | ||
| storage_key: &XOnlyPublicKey, | ||
| storage_bytes: &[u8; 32], | ||
| path: [bool; DEPTH], | ||
| network: SimplicityNetwork, | ||
| ) -> Result<Address, TransactionBuildError> { | ||
| let mut smt = SparseMerkleTree::new(); | ||
| let merkle_hashes = smt.update(storage_bytes, path); | ||
|
|
||
| let merkle_data = std::array::from_fn(|i| (merkle_hashes[DEPTH - i - 1], path[DEPTH - i - 1])); | ||
|
|
||
| let program = get_smt_storage_compiled_program(); | ||
| let cmr = program.commit().cmr(); | ||
|
|
||
| let mint_spend_info: TaprootSpendInfo = | ||
| smt_storage_taproot_spend_info(*storage_key, storage_bytes, &merkle_data, cmr); | ||
|
|
||
| let mint_script_pubkey = Script::new_v1_p2tr_tweaked(mint_spend_info.output_key()); | ||
|
|
||
| Ok(Address::from_script(&mint_script_pubkey, None, network.address_params()).unwrap()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| mod get_storage_address; | ||
| mod transfer_from_storage_address; | ||
|
|
||
| pub use get_storage_address::*; | ||
| pub use transfer_from_storage_address::*; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This command should not broadcast anything, just print the address to which user can send money