Skip to content

Commit

Permalink
refactor: move WKN to its file, update imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Fumuran committed Mar 5, 2025
1 parent 94361be commit a3d0ef0
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 103 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/miden-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ thiserror = { workspace = true }
[dev-dependencies]
miden-objects = { workspace = true, features = ["testing"] }
vm-processor = { workspace = true, features = ["testing"] }
assembly = { workspace = true, features = ["testing"] }

[build-dependencies]
assembly = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/miden-lib/src/account/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
account::components::{
basic_fungible_faucet_library, basic_wallet_library, rpo_falcon_512_library,
},
note::WellKnownNote,
note::well_known_note::WellKnownNote,
AuthScheme,
};

Expand Down
6 changes: 1 addition & 5 deletions crates/miden-lib/src/account/interface/test.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use alloc::{string::ToString, sync::Arc, vec::Vec};

use assembly::{
ast::{Module, ModuleKind},
DefaultSourceManager,
};
use miden_objects::{
account::{AccountBuilder, AccountComponent, AccountType, StorageSlot},
assembly::{Assembler, LibraryPath},
assembly::{Assembler, DefaultSourceManager, LibraryPath, Module, ModuleKind},
asset::{FungibleAsset, NonFungibleAsset, TokenSymbol},
block::BlockNumber,
crypto::{
Expand Down
94 changes: 3 additions & 91 deletions crates/miden-lib/src/note/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,20 @@ use alloc::vec::Vec;

use miden_objects::{
account::AccountId,
assembly::{ProcedureName, QualifiedProcedureName},
asset::Asset,
block::BlockNumber,
crypto::rand::FeltRng,
note::{
Note, NoteAssets, NoteDetails, NoteExecutionHint, NoteExecutionMode, NoteInputs,
NoteMetadata, NoteRecipient, NoteScript, NoteTag, NoteType,
NoteMetadata, NoteRecipient, NoteTag, NoteType,
},
Digest, Felt, NoteError, Word,
Felt, NoteError, Word,
};
use scripts::{p2id, p2id_commitment, p2idr, p2idr_commitment, swap, swap_commitment};
use utils::build_swap_tag;

use crate::account::{
components::basic_wallet_library,
interface::{component_proc_digests, AccountComponentInterface, AccountInterface},
};

pub mod scripts;
pub mod utils;
pub mod well_known_note;

// STANDARDIZED SCRIPTS
// ================================================================================================
Expand Down Expand Up @@ -144,85 +138,3 @@ pub fn create_swap_note<R: FeltRng>(

Ok((note, payback_note))
}

// WELL KNOWN NOTES
// ================================================================================================

/// The enum holding the types of basic well-known notes provided by the `miden-lib`.
pub enum WellKnownNote {
P2ID,
P2IDR,
SWAP,
}

impl WellKnownNote {
/// Returns a [WellKnownNote] instance based on the note script of the provided [Note]. Returns
/// `None` if the provided note is not a basic well-known note.
pub fn from_note(note: &Note) -> Option<Self> {
let note_script_commitment = note.script().hash();

if note_script_commitment == p2id_commitment() {
return Some(Self::P2ID);
}
if note_script_commitment == p2idr_commitment() {
return Some(Self::P2IDR);
}
if note_script_commitment == swap_commitment() {
return Some(Self::SWAP);
}

None
}

/// Returns the note script of the current [WellKnownNote] instance.
pub fn script(&self) -> NoteScript {
match self {
Self::P2ID => p2id(),
Self::P2IDR => p2idr(),
Self::SWAP => swap(),
}
}

/// Returns the script commitment of the current [WellKnownNote] instance.
pub fn script_root(&self) -> Digest {
match self {
Self::P2ID => p2id_commitment(),
Self::P2IDR => p2idr_commitment(),
Self::SWAP => swap_commitment(),
}
}

/// Returns a boolean value indicating whether this [WellKnownNote] is compatible with the
/// provided [AccountInterface].
pub fn is_compatible_with(&self, account_interface: &AccountInterface) -> bool {
if account_interface.interfaces().contains(&AccountComponentInterface::BasicWallet) {
return true;
}

let interface_proc_digests = component_proc_digests(account_interface.interfaces());
match self {
Self::P2ID | &Self::P2IDR => {
// Get the hash of the "receive_asset" procedure and check that this procedure is
// presented in the provided account interfaces. P2ID and P2IDR notes requires only
// this procedure to be consumed by the account.
let receive_asset_proc_name = QualifiedProcedureName::new(
Default::default(),
ProcedureName::new("receive_asset").unwrap(),
);
let node_id = basic_wallet_library().get_export_node_id(&receive_asset_proc_name);
let receive_asset_digest = basic_wallet_library().mast_forest()[node_id].digest();

interface_proc_digests.contains(&receive_asset_digest)
},
Self::SWAP => {
// Make sure that all procedures from the basic wallet library are presented in the
// provided account interfaces. SWAP note requires the whole basic wallet interface
// to be consumed by the account.
basic_wallet_library()
.mast_forest()
.procedure_digests()
.all(|proc_digest| interface_proc_digests.contains(&proc_digest))
},
}
}
}
6 changes: 3 additions & 3 deletions crates/miden-lib/src/note/scripts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn p2id() -> NoteScript {

/// Returns the P2ID (Pay-to-ID) note script commitment.
pub fn p2id_commitment() -> Digest {
P2ID_SCRIPT.clone().hash()
P2ID_SCRIPT.hash()
}

/// Returns the P2IDR (Pay-to-ID with recall) note script.
Expand All @@ -43,7 +43,7 @@ pub fn p2idr() -> NoteScript {

/// Returns the P2IDR (Pay-to-ID with recall) note script commitment.
pub fn p2idr_commitment() -> Digest {
P2IDR_SCRIPT.clone().hash()
P2IDR_SCRIPT.hash()
}

/// Returns the SWAP (Swap note) note script.
Expand All @@ -53,5 +53,5 @@ pub fn swap() -> NoteScript {

/// Returns the SWAP (Swap note) note script commitment.
pub fn swap_commitment() -> Digest {
SWAP_SCRIPT.clone().hash()
SWAP_SCRIPT.hash()
}
95 changes: 95 additions & 0 deletions crates/miden-lib/src/note/well_known_note.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use miden_objects::{
assembly::{ProcedureName, QualifiedProcedureName},
note::{Note, NoteScript},
Digest,
};

use crate::{
account::{
components::basic_wallet_library,
interface::{component_proc_digests, AccountComponentInterface, AccountInterface},
},
note::scripts::{p2id, p2id_commitment, p2idr, p2idr_commitment, swap, swap_commitment},
};

// WELL KNOWN NOTE
// ================================================================================================

/// The enum holding the types of basic well-known notes provided by the `miden-lib`.
pub enum WellKnownNote {
P2ID,
P2IDR,
SWAP,
}

impl WellKnownNote {
/// Returns a [WellKnownNote] instance based on the note script of the provided [Note]. Returns
/// `None` if the provided note is not a basic well-known note.
pub fn from_note(note: &Note) -> Option<Self> {
let note_script_commitment = note.script().hash();

if note_script_commitment == p2id_commitment() {
return Some(Self::P2ID);
}
if note_script_commitment == p2idr_commitment() {
return Some(Self::P2IDR);
}
if note_script_commitment == swap_commitment() {
return Some(Self::SWAP);
}

None
}

/// Returns the note script of the current [WellKnownNote] instance.
pub fn script(&self) -> NoteScript {
match self {
Self::P2ID => p2id(),
Self::P2IDR => p2idr(),
Self::SWAP => swap(),
}
}

/// Returns the script commitment of the current [WellKnownNote] instance.
pub fn script_root(&self) -> Digest {
match self {
Self::P2ID => p2id_commitment(),
Self::P2IDR => p2idr_commitment(),
Self::SWAP => swap_commitment(),
}
}

/// Returns a boolean value indicating whether this [WellKnownNote] is compatible with the
/// provided [AccountInterface].
pub fn is_compatible_with(&self, account_interface: &AccountInterface) -> bool {
if account_interface.interfaces().contains(&AccountComponentInterface::BasicWallet) {
return true;
}

let interface_proc_digests = component_proc_digests(account_interface.interfaces());
match self {
Self::P2ID | &Self::P2IDR => {
// Get the hash of the "receive_asset" procedure and check that this procedure is
// presented in the provided account interfaces. P2ID and P2IDR notes requires only
// this procedure to be consumed by the account.
let receive_asset_proc_name = QualifiedProcedureName::new(
Default::default(),
ProcedureName::new("receive_asset").unwrap(),
);
let node_id = basic_wallet_library().get_export_node_id(&receive_asset_proc_name);
let receive_asset_digest = basic_wallet_library().mast_forest()[node_id].digest();

interface_proc_digests.contains(&receive_asset_digest)
},
Self::SWAP => {
// Make sure that all procedures from the basic wallet library are presented in the
// provided account interfaces. SWAP note requires the whole basic wallet interface
// to be consumed by the account.
basic_wallet_library()
.mast_forest()
.procedure_digests()
.all(|proc_digest| interface_proc_digests.contains(&proc_digest))
},
}
}
}
2 changes: 1 addition & 1 deletion crates/miden-objects/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub use vm_core::{

pub mod assembly {
pub use assembly::{
ast::{Module, ProcedureName, QualifiedProcedureName},
ast::{Module, ModuleKind, ProcedureName, QualifiedProcedureName},
mast, Assembler, AssemblyError, Compile, DefaultSourceManager, KernelLibrary, Library,
LibraryNamespace, LibraryPath, SourceManager, Version,
};
Expand Down

0 comments on commit a3d0ef0

Please sign in to comment.