Skip to content

Commit

Permalink
refactor: Factor out note scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
igamigo committed Sep 17, 2024
1 parent 76a6635 commit 61edbee
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 49 deletions.
54 changes: 5 additions & 49 deletions miden-lib/src/notes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,10 +38,7 @@ pub fn create_p2id_note<R: FeltRng>(
aux: Felt,
rng: &mut R,
) -> Result<Note, NoteError> {
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)?;
Expand Down Expand Up @@ -72,10 +71,7 @@ pub fn create_p2idr_note<R: FeltRng>(
recall_height: u32,
rng: &mut R,
) -> Result<Note, NoteError> {
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)?;
Expand Down Expand Up @@ -104,10 +100,7 @@ pub fn create_swap_note<R: FeltRng>(
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)?;
Expand Down Expand Up @@ -145,40 +138,3 @@ pub fn create_swap_note<R: FeltRng>(

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<NoteTag, NoteError> {
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),
}
}
22 changes: 22 additions & 0 deletions miden-lib/src/notes/scripts.rs
Original file line number Diff line number Diff line change
@@ -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)
}
34 changes: 34 additions & 0 deletions miden-lib/src/notes/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NoteTag, NoteError> {
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),
}
}

0 comments on commit 61edbee

Please sign in to comment.