From 61edbeec34b6f7223ca68a52ef66f8877dc87d0e Mon Sep 17 00:00:00 2001 From: Ignacio Amigo Date: Tue, 17 Sep 2024 12:43:47 -0300 Subject: [PATCH 1/3] refactor: Factor out note scripts --- miden-lib/src/notes/mod.rs | 54 ++++------------------------------ miden-lib/src/notes/scripts.rs | 22 ++++++++++++++ miden-lib/src/notes/utils.rs | 34 +++++++++++++++++++++ 3 files changed, 61 insertions(+), 49 deletions(-) create mode 100644 miden-lib/src/notes/scripts.rs diff --git a/miden-lib/src/notes/mod.rs b/miden-lib/src/notes/mod.rs index a70ac907a..033a81e49 100644 --- a/miden-lib/src/notes/mod.rs +++ b/miden-lib/src/notes/mod.rs @@ -12,7 +12,9 @@ use miden_objects::{ vm::Program, Felt, NoteError, Word, }; +use utils::build_swap_tag; +pub mod scripts; pub mod utils; // STANDARDIZED SCRIPTS @@ -36,10 +38,7 @@ pub fn create_p2id_note( aux: Felt, rng: &mut R, ) -> Result { - let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/note_scripts/P2ID.masb")); - let program = - Program::read_from_bytes(bytes).map_err(NoteError::NoteScriptDeserializationError)?; - let note_script = NoteScript::new(program); + let note_script = scripts::p2id(); let inputs = NoteInputs::new(vec![target.into()])?; let tag = NoteTag::from_account_id(target, NoteExecutionMode::Local)?; @@ -72,10 +71,7 @@ pub fn create_p2idr_note( recall_height: u32, rng: &mut R, ) -> Result { - let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/note_scripts/P2IDR.masb")); - let program = - Program::read_from_bytes(bytes).map_err(NoteError::NoteScriptDeserializationError)?; - let note_script = NoteScript::new(program); + let note_script = scripts::p2idr(); let inputs = NoteInputs::new(vec![target.into(), recall_height.into()])?; let tag = NoteTag::from_account_id(target, NoteExecutionMode::Local)?; @@ -104,10 +100,7 @@ pub fn create_swap_note( aux: Felt, rng: &mut R, ) -> Result<(Note, NoteDetails), NoteError> { - let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/note_scripts/SWAP.masb")); - let program = - Program::read_from_bytes(bytes).map_err(NoteError::NoteScriptDeserializationError)?; - let note_script = NoteScript::new(program); + let note_script = scripts::swap(); let payback_serial_num = rng.draw_word(); let payback_recipient = utils::build_p2id_recipient(sender, payback_serial_num)?; @@ -145,40 +138,3 @@ pub fn create_swap_note( Ok((note, payback_note)) } - -// HELPER FUNCTIONS -// ================================================================================================ - -/// Returns a note tag for a swap note with the specified parameters. -/// -/// Use case ID for the returned tag is set to 0. -/// -/// Tag payload is constructed by taking asset tags (8 bits of faucet ID) and concatenating them -/// together as offered_asset_tag + requested_asset tag. -/// -/// Network execution hint for the returned tag is set to `Local`. -pub fn build_swap_tag( - note_type: NoteType, - offered_asset: &Asset, - requested_asset: &Asset, -) -> Result { - const SWAP_USE_CASE_ID: u16 = 0; - - // get bits 4..12 from faucet IDs of both assets, these bits will form the tag payload; the - // reason we skip the 4 most significant bits is that these encode metadata of underlying - // faucets and are likely to be the same for many different faucets. - - let offered_asset_id: u64 = offered_asset.faucet_id().into(); - let offered_asset_tag = (offered_asset_id >> 52) as u8; - - let requested_asset_id: u64 = requested_asset.faucet_id().into(); - let requested_asset_tag = (requested_asset_id >> 52) as u8; - - let payload = ((offered_asset_tag as u16) << 8) | (requested_asset_tag as u16); - - let execution = NoteExecutionMode::Local; - match note_type { - NoteType::Public => NoteTag::for_public_use_case(SWAP_USE_CASE_ID, payload, execution), - _ => NoteTag::for_local_use_case(SWAP_USE_CASE_ID, payload), - } -} diff --git a/miden-lib/src/notes/scripts.rs b/miden-lib/src/notes/scripts.rs new file mode 100644 index 000000000..75a0e6f9f --- /dev/null +++ b/miden-lib/src/notes/scripts.rs @@ -0,0 +1,22 @@ +use miden_objects::{notes::NoteScript, utils::Deserializable, vm::Program}; + +pub fn p2id() -> NoteScript { + let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/note_scripts/P2ID.masb")); + let program = Program::read_from_bytes(bytes).expect("Shipped P2ID script is well-formed"); + + NoteScript::new(program) +} + +pub fn p2idr() -> NoteScript { + let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/note_scripts/P2IDR.masb")); + let program = Program::read_from_bytes(bytes).expect("Shipped P2IDR script is well-formed"); + + NoteScript::new(program) +} + +pub fn swap() -> NoteScript { + let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/note_scripts/SWAP.masb")); + let program = Program::read_from_bytes(bytes).expect("Shipped SWAP script is well-formed"); + + NoteScript::new(program) +} diff --git a/miden-lib/src/notes/utils.rs b/miden-lib/src/notes/utils.rs index 12ad0dbbe..9ab3280df 100644 --- a/miden-lib/src/notes/utils.rs +++ b/miden-lib/src/notes/utils.rs @@ -22,3 +22,37 @@ pub fn build_p2id_recipient( Ok(NoteRecipient::new(serial_num, note_script, note_inputs)) } + +/// Returns a note tag for a swap note with the specified parameters. +/// +/// Use case ID for the returned tag is set to 0. +/// +/// Tag payload is constructed by taking asset tags (8 bits of faucet ID) and concatenating them +/// together as offered_asset_tag + requested_asset tag. +/// +/// Network execution hint for the returned tag is set to `Local`. +pub fn build_swap_tag( + note_type: NoteType, + offered_asset: &Asset, + requested_asset: &Asset, +) -> Result { + const SWAP_USE_CASE_ID: u16 = 0; + + // get bits 4..12 from faucet IDs of both assets, these bits will form the tag payload; the + // reason we skip the 4 most significant bits is that these encode metadata of underlying + // faucets and are likely to be the same for many different faucets. + + let offered_asset_id: u64 = offered_asset.faucet_id().into(); + let offered_asset_tag = (offered_asset_id >> 52) as u8; + + let requested_asset_id: u64 = requested_asset.faucet_id().into(); + let requested_asset_tag = (requested_asset_id >> 52) as u8; + + let payload = ((offered_asset_tag as u16) << 8) | (requested_asset_tag as u16); + + let execution = NoteExecutionMode::Local; + match note_type { + NoteType::Public => NoteTag::for_public_use_case(SWAP_USE_CASE_ID, payload, execution), + _ => NoteTag::for_local_use_case(SWAP_USE_CASE_ID, payload), + } +} From edebb9357e200753547c660a1c9da4f191418e8b Mon Sep 17 00:00:00 2001 From: Ignacio Amigo Date: Tue, 17 Sep 2024 12:46:05 -0300 Subject: [PATCH 2/3] Comments --- miden-lib/src/notes/mod.rs | 4 +--- miden-lib/src/notes/scripts.rs | 3 +++ miden-lib/src/notes/utils.rs | 3 ++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/miden-lib/src/notes/mod.rs b/miden-lib/src/notes/mod.rs index 033a81e49..e954828e5 100644 --- a/miden-lib/src/notes/mod.rs +++ b/miden-lib/src/notes/mod.rs @@ -6,10 +6,8 @@ use miden_objects::{ crypto::rand::FeltRng, notes::{ Note, NoteAssets, NoteDetails, NoteExecutionHint, NoteExecutionMode, NoteInputs, - NoteMetadata, NoteRecipient, NoteScript, NoteTag, NoteType, + NoteMetadata, NoteRecipient, NoteTag, NoteType, }, - utils::Deserializable, - vm::Program, Felt, NoteError, Word, }; use utils::build_swap_tag; diff --git a/miden-lib/src/notes/scripts.rs b/miden-lib/src/notes/scripts.rs index 75a0e6f9f..044c17aea 100644 --- a/miden-lib/src/notes/scripts.rs +++ b/miden-lib/src/notes/scripts.rs @@ -1,5 +1,6 @@ use miden_objects::{notes::NoteScript, utils::Deserializable, vm::Program}; +/// Returns a P2ID (Pay-to-ID) note script. pub fn p2id() -> NoteScript { let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/note_scripts/P2ID.masb")); let program = Program::read_from_bytes(bytes).expect("Shipped P2ID script is well-formed"); @@ -7,6 +8,7 @@ pub fn p2id() -> NoteScript { NoteScript::new(program) } +/// Returns a P2IDR (Pay-to-ID with recall) note script. pub fn p2idr() -> NoteScript { let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/note_scripts/P2IDR.masb")); let program = Program::read_from_bytes(bytes).expect("Shipped P2IDR script is well-formed"); @@ -14,6 +16,7 @@ pub fn p2idr() -> NoteScript { NoteScript::new(program) } +/// Returns a SWAP (Pay-to-ID with recall) note script. pub fn swap() -> NoteScript { let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/note_scripts/SWAP.masb")); let program = Program::read_from_bytes(bytes).expect("Shipped SWAP script is well-formed"); diff --git a/miden-lib/src/notes/utils.rs b/miden-lib/src/notes/utils.rs index 9ab3280df..9a4539680 100644 --- a/miden-lib/src/notes/utils.rs +++ b/miden-lib/src/notes/utils.rs @@ -1,6 +1,7 @@ use miden_objects::{ accounts::AccountId, - notes::{NoteInputs, NoteRecipient, NoteScript}, + assets::Asset, + notes::{NoteExecutionMode, NoteInputs, NoteRecipient, NoteScript, NoteTag, NoteType}, utils::Deserializable, vm::Program, NoteError, Word, From 5368543d133e90f1fa52cc8131d52145ad1c3bc7 Mon Sep 17 00:00:00 2001 From: Ignacio Amigo Date: Tue, 17 Sep 2024 13:03:05 -0300 Subject: [PATCH 3/3] CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 903f554d5..cf34f1519 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## 0.6.0 (TBD) +- Made note scripts public (#880). - [BREAKING] Renamed the `TransactionProver` struct to `LocalTransactionProver` and added the `TransactionProver` trait (#865). - Implemented `Display`, `TryFrom<&str>` and `FromStr` for `AccountStorageMode` (#861). - Implemented offset based storage access (#843).