Skip to content

Commit

Permalink
test: Adding web client note tests
Browse files Browse the repository at this point in the history
  • Loading branch information
julian-demox committed Oct 17, 2024
1 parent 9029693 commit 1c90071
Show file tree
Hide file tree
Showing 19 changed files with 968 additions and 748 deletions.
7 changes: 7 additions & 0 deletions crates/web-client/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ const {
NoteAssets,
NoteExecutionHint,
NoteExecutionMode,
NoteFilter,
NoteFilterTypes,
NoteId,
NoteIdAndArgs,
NoteIdAndArgsArray,
NoteInputs,
NoteMetadata,
NoteRecipient,
NoteState,
NoteTag,
NoteType,
OutputNote,
Expand Down Expand Up @@ -51,18 +54,22 @@ export {
NoteAssets,
NoteExecutionHint,
NoteExecutionMode,
NoteFilter,
NoteFilterTypes,
NoteId,
NoteIdAndArgs,
NoteIdAndArgsArray,
NoteInputs,
NoteMetadata,
NoteRecipient,
NoteState,
NoteTag,
NoteType,
OutputNote,
OutputNotesArray,
Rpo256,
TestUtils,
TransactionFilter,
TransactionRequest,
TransactionScriptInputPair,
TransactionScriptInputPairArray,
Expand Down
3 changes: 3 additions & 0 deletions crates/web-client/js/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ export {
NoteAssets,
NoteExecutionHint,
NoteExecutionMode,
NoteFilter,
NoteFilterTypes,
NoteId,
NoteIdAndArgs,
NoteIdAndArgsArray,
NoteInputs,
NoteMetadata,
NoteRecipient,
NoteState,
NoteTag,
NoteType,
OutputNote,
Expand Down
2 changes: 1 addition & 1 deletion crates/web-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
"scripts": {
"build": "rimraf dist && rollup -c rollup.config.js && cpr js/types dist && node clean.js",
"test": "node --loader ts-node/esm --loader esm ./node_modules/mocha/bin/mocha --file ./test/mocha.global.setup.mjs",
"test": "node --loader ts-node/esm ./node_modules/mocha/bin/mocha --file ./test/mocha.global.setup.mjs",
"test:logs": "cross-env DEBUG_MODE=true node --loader ts-node/esm --loader esm ./node_modules/mocha/bin/mocha --file ./test/mocha.global.setup.mjs",
"test:clean": "npm install && MIDEN_WEB_TESTING=true npm run build && node --loader ts-node/esm --loader esm ./node_modules/mocha/bin/mocha --file ./test/mocha.global.setup.mjs"
},
Expand Down
38 changes: 38 additions & 0 deletions crates/web-client/src/models/input_note_record.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use miden_client::store::InputNoteRecord as NativeInputNoteRecord;
use wasm_bindgen::prelude::*;

use super::{note_details::NoteDetails, note_id::NoteId, note_state::NoteState};

#[derive(Clone)]
#[wasm_bindgen]
pub struct InputNoteRecord(NativeInputNoteRecord);

#[wasm_bindgen]
impl InputNoteRecord {
pub fn id(&self) -> NoteId {
self.0.id().into()
}

pub fn state(&self) -> NoteState {
self.0.state().into()
}

pub fn details(&self) -> NoteDetails {
self.0.details().into()
}
}

// CONVERSIONS
// ================================================================================================

impl From<NativeInputNoteRecord> for InputNoteRecord {
fn from(native_note: NativeInputNoteRecord) -> Self {
InputNoteRecord(native_note)
}
}

impl From<&NativeInputNoteRecord> for InputNoteRecord {
fn from(native_note: &NativeInputNoteRecord) -> Self {
InputNoteRecord(native_note.clone())
}
}
3 changes: 3 additions & 0 deletions crates/web-client/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ pub mod executed_transaction;
pub mod felt;
pub mod fungible_asset;
pub mod input_note;
pub mod input_note_record;
pub mod input_notes;
pub mod merkle_path;
pub mod note;
pub mod note_assets;
pub mod note_details;
pub mod note_execution_hint;
pub mod note_execution_mode;
pub mod note_filter;
pub mod note_header;
pub mod note_id;
pub mod note_inclusion_proof;
Expand All @@ -57,6 +59,7 @@ pub mod note_location;
pub mod note_metadata;
pub mod note_recipient;
pub mod note_script;
pub mod note_state;
pub mod note_tag;
pub mod note_type;
pub mod output_note;
Expand Down
20 changes: 20 additions & 0 deletions crates/web-client/src/models/note_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ impl NoteDetails {
pub fn new(note_assets: &NoteAssets, note_recipient: &NoteRecipient) -> NoteDetails {
NoteDetails(NativeNoteDetails::new(note_assets.into(), note_recipient.into()))
}

pub fn assets(&self) -> NoteAssets {
self.0.assets().into()
}

pub fn recipient(&self) -> NoteRecipient {
self.0.recipient().into()
}
}

impl From<NoteDetails> for NativeNoteDetails {
Expand All @@ -27,6 +35,18 @@ impl From<&NoteDetails> for NativeNoteDetails {
}
}

impl From<NativeNoteDetails> for NoteDetails {
fn from(note_details: NativeNoteDetails) -> NoteDetails {
NoteDetails(note_details)
}
}

impl From<&NativeNoteDetails> for NoteDetails {
fn from(note_details: &NativeNoteDetails) -> NoteDetails {
NoteDetails(note_details.clone())
}
}

#[derive(Clone)]
#[wasm_bindgen]
pub struct NoteDetailsArray(Vec<NoteDetails>);
Expand Down
103 changes: 103 additions & 0 deletions crates/web-client/src/models/note_filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use miden_client::store::NoteFilter as NativeNoteFilter;
use wasm_bindgen::prelude::*;

use super::note_id::NoteId;

// TODO: Add nullfiier support

#[derive(Clone)]
#[wasm_bindgen]
pub struct NoteFilter {
note_type: NoteFilterTypes,
note_ids: Option<Vec<NoteId>>,
}

#[wasm_bindgen]
impl NoteFilter {
#[wasm_bindgen(constructor)]
pub fn new(note_type: NoteFilterTypes, note_ids: Option<Vec<NoteId>>) -> NoteFilter {
NoteFilter { note_type, note_ids }
}
}

#[derive(Clone)]
#[wasm_bindgen]
pub enum NoteFilterTypes {
All,
Consumed,
Committed,
Expected,
Processing,
Ignored,
List,
Unique,
Nullifiers,
Unverified,
}

// CONVERSIONS
// ================================================================================================

impl From<NoteFilter> for NativeNoteFilter {
fn from(filter: NoteFilter) -> Self {
match filter.note_type {
NoteFilterTypes::All => NativeNoteFilter::All,
NoteFilterTypes::Consumed => NativeNoteFilter::Consumed,
NoteFilterTypes::Committed => NativeNoteFilter::Committed,
NoteFilterTypes::Expected => NativeNoteFilter::Expected,
NoteFilterTypes::Processing => NativeNoteFilter::Processing,
NoteFilterTypes::Ignored => NativeNoteFilter::Ignored,
NoteFilterTypes::List => {
let note_ids =
filter.note_ids.unwrap_or_else(|| panic!("Note IDs required for List filter"));
NativeNoteFilter::List(note_ids.iter().map(|note| note.into()).collect())
},
NoteFilterTypes::Unique => {
let note_ids =
filter.note_ids.unwrap_or_else(|| panic!("Note ID required for Unique filter"));

if note_ids.len() != 1 {
panic!("Only one Note ID can be provided");
}

NativeNoteFilter::Unique(note_ids.first().unwrap().into())
},
NoteFilterTypes::Nullifiers => NativeNoteFilter::Nullifiers(vec![]),
NoteFilterTypes::Unverified => NativeNoteFilter::Unverified,
}
}
}

impl From<&NoteFilter> for NativeNoteFilter {
fn from(filter: &NoteFilter) -> Self {
match filter.note_type {
NoteFilterTypes::All => NativeNoteFilter::All,
NoteFilterTypes::Consumed => NativeNoteFilter::Consumed,
NoteFilterTypes::Committed => NativeNoteFilter::Committed,
NoteFilterTypes::Expected => NativeNoteFilter::Expected,
NoteFilterTypes::Processing => NativeNoteFilter::Processing,
NoteFilterTypes::Ignored => NativeNoteFilter::Ignored,
NoteFilterTypes::List => {
let note_ids = filter
.note_ids
.clone()
.unwrap_or_else(|| panic!("Note IDs required for List filter"));
NativeNoteFilter::List(note_ids.iter().map(|note| note.into()).collect())
},
NoteFilterTypes::Unique => {
let note_ids = filter
.note_ids
.clone()
.unwrap_or_else(|| panic!("Note ID required for Unique filter"));

if note_ids.len() != 1 {
panic!("Only one Note ID can be provided");
}

NativeNoteFilter::Unique(note_ids.first().unwrap().into())
},
NoteFilterTypes::Nullifiers => NativeNoteFilter::Nullifiers(vec![]),
NoteFilterTypes::Unverified => NativeNoteFilter::Unverified,
}
}
}
55 changes: 55 additions & 0 deletions crates/web-client/src/models/note_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use miden_client::store::NoteState as NativeNoteState;
use wasm_bindgen::prelude::*;

#[derive(Clone)]
#[wasm_bindgen]
pub enum NoteState {
Expected,
Unverified,
Committed,
Invalid,
ProcessingAuthenticated,
ProcessingUnauthenticated,
ConsumedAuthenticatedLocal,
ConsumedUnauthenticatedLocal,
ConsumedExternal,
}

// CONVERSIONS
// ================================================================================================

impl From<NativeNoteState> for NoteState {
fn from(native_note: NativeNoteState) -> Self {
match native_note {
NativeNoteState::Expected(_) => NoteState::Expected,
NativeNoteState::Unverified(_) => NoteState::Unverified,
NativeNoteState::Committed(_) => NoteState::Committed,
NativeNoteState::Invalid(_) => NoteState::Invalid,
NativeNoteState::ProcessingAuthenticated(_) => NoteState::ProcessingAuthenticated,
NativeNoteState::ProcessingUnauthenticated(_) => NoteState::ProcessingUnauthenticated,
NativeNoteState::ConsumedAuthenticatedLocal(_) => NoteState::ConsumedAuthenticatedLocal,
NativeNoteState::ConsumedUnauthenticatedLocal(_) => {
NoteState::ConsumedUnauthenticatedLocal
},
NativeNoteState::ConsumedExternal(_) => NoteState::ConsumedExternal,
}
}
}

impl From<&NativeNoteState> for NoteState {
fn from(native_note: &NativeNoteState) -> Self {
match native_note {
NativeNoteState::Expected(_) => NoteState::Expected,
NativeNoteState::Unverified(_) => NoteState::Unverified,
NativeNoteState::Committed(_) => NoteState::Committed,
NativeNoteState::Invalid(_) => NoteState::Invalid,
NativeNoteState::ProcessingAuthenticated(_) => NoteState::ProcessingAuthenticated,
NativeNoteState::ProcessingUnauthenticated(_) => NoteState::ProcessingUnauthenticated,
NativeNoteState::ConsumedAuthenticatedLocal(_) => NoteState::ConsumedAuthenticatedLocal,
NativeNoteState::ConsumedUnauthenticatedLocal(_) => {
NoteState::ConsumedUnauthenticatedLocal
},
NativeNoteState::ConsumedExternal(_) => NoteState::ConsumedExternal,
}
}
}
Loading

0 comments on commit 1c90071

Please sign in to comment.