Skip to content

Commit de8026a

Browse files
authored
feat: expose note export and imports as a struct (0xMiden#1383)
1 parent 5ce7067 commit de8026a

File tree

20 files changed

+485
-33
lines changed

20 files changed

+485
-33
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* Implemented shared source manager for better error reporting ([#1275](https://github.com/0xMiden/miden-client/pull/1275)).
1616
* Added `getMapEntries` method to `AccountStorage` in web client for iterating storage map entries ([#1323](https://github.com/0xMiden/miden-client/pull/1323)).
1717
* Refactored code into their own files and added `ProvenTransaction` and `TransactionStoreUpdate` bindings for the WebClient ([#1408](https://github.com/0xMiden/miden-client/pull/1408)).
18+
* Added `NoteFile` type, used for exporting and importing `Notes`([#1378](https://github.com/0xMiden/miden-client/pull/1383))
1819

1920
### Changes
2021

crates/rust-client/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ mod errors;
151151
// RE-EXPORTS
152152
// ================================================================================================
153153

154+
pub mod notes {
155+
pub use miden_objects::note::NoteFile;
156+
}
157+
154158
/// Provides types and utilities for working with Miden Assembly.
155159
pub mod assembly {
156160
pub use miden_objects::assembly::debuginfo::SourceManagerSync;

crates/web-client/js/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ const {
3636
NoteAndArgsArray,
3737
NoteAssets,
3838
NoteConsumability,
39+
NoteDetails,
3940
NoteExecutionHint,
4041
NoteExecutionMode,
4142
NoteFilter,
43+
NoteFile,
4244
NoteFilterTypes,
4345
NoteId,
4446
NoteIdAndArgs,
@@ -115,9 +117,11 @@ export {
115117
NoteAndArgsArray,
116118
NoteAssets,
117119
NoteConsumability,
120+
NoteDetails,
118121
NoteExecutionHint,
119122
NoteExecutionMode,
120123
NoteFilter,
124+
NoteFile,
121125
NoteFilterTypes,
122126
NoteId,
123127
NoteIdAndArgs,

crates/web-client/js/types/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ export {
3838
NoteAndArgsArray,
3939
NoteAssets,
4040
NoteConsumability,
41+
NoteDetails,
4142
NoteExecutionHint,
4243
NoteExecutionMode,
44+
NoteFile,
4345
NoteFilter,
4446
NoteFilterTypes,
4547
NoteId,

crates/web-client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@demox-labs/miden-sdk",
3-
"version": "0.12.0-next.28",
3+
"version": "0.12.0-next.29",
44
"description": "Miden Wasm SDK",
55
"collaborators": [
66
"Miden",

crates/web-client/src/export.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use miden_client::Word;
22
use miden_client::account::AccountFile as NativeAccountFile;
33
use miden_client::store::NoteExportType;
4-
use miden_client::utils::{Serializable, get_public_keys_from_account};
4+
use miden_client::utils::get_public_keys_from_account;
55
use wasm_bindgen::prelude::*;
66

77
use crate::models::account_file::AccountFile;
88
use crate::models::account_id::AccountId;
9+
use crate::models::note_file::NoteFile;
910
use crate::{WebClient, js_error_with_context};
1011

1112
#[wasm_bindgen]
@@ -15,16 +16,26 @@ impl WebClient {
1516
&mut self,
1617
note_id: String,
1718
export_type: String,
18-
) -> Result<JsValue, JsValue> {
19+
) -> Result<NoteFile, JsValue> {
1920
if let Some(client) = self.get_mut_inner() {
2021
let note_id = Word::try_from(note_id)
21-
.map_err(|err| js_error_with_context(err, "failed to parse input note id"))?
22+
.map_err(|err| {
23+
js_error_with_context(
24+
err,
25+
"error exporting note file: failed to parse input note id",
26+
)
27+
})?
2228
.into();
2329

2430
let output_note = client
2531
.get_output_note(note_id)
2632
.await
27-
.map_err(|err| js_error_with_context(err, "failed to get output notes"))?
33+
.map_err(|err| {
34+
js_error_with_context(
35+
err,
36+
"error exporting note file: failed to get output notes",
37+
)
38+
})?
2839
.ok_or(JsValue::from_str("No output note found"))?;
2940

3041
let export_type = match export_type.as_str() {
@@ -44,12 +55,7 @@ impl WebClient {
4455
js_error_with_context(err, "failed to convert output note to note file")
4556
})?;
4657

47-
let input_note_bytes = note_file.to_bytes();
48-
49-
let serialized_input_note_bytes = serde_wasm_bindgen::to_value(&input_note_bytes)
50-
.map_err(|_| JsValue::from_str("Serialization error"))?;
51-
52-
Ok(serialized_input_note_bytes)
58+
Ok(note_file.into())
5359
} else {
5460
Err(JsValue::from_str("Client not initialized"))
5561
}

crates/web-client/src/import.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
use miden_client::account::{AccountFile as NativeAccountFile, AccountId as NativeAccountId};
22
use miden_client::auth::AuthSecretKey;
3-
use miden_client::note::NoteFile;
4-
use miden_client::utils::Deserializable;
5-
use serde_wasm_bindgen::from_value;
63
use wasm_bindgen::prelude::*;
74

85
use crate::helpers::generate_wallet;
96
use crate::models::account::Account;
107
use crate::models::account_file::AccountFile;
118
use crate::models::account_id::AccountId as JsAccountId;
129
use crate::models::account_storage_mode::AccountStorageMode;
10+
use crate::models::note_file::NoteFile;
11+
use crate::models::note_id::NoteId;
1312
use crate::{WebClient, js_error_with_context};
1413

1514
#[wasm_bindgen]
@@ -88,20 +87,13 @@ impl WebClient {
8887
}
8988

9089
#[wasm_bindgen(js_name = "importNoteFile")]
91-
pub async fn import_note_file(&mut self, note_bytes: JsValue) -> Result<JsValue, JsValue> {
90+
pub async fn import_note_file(&mut self, note_file: NoteFile) -> Result<NoteId, JsValue> {
9291
if let Some(client) = self.get_mut_inner() {
93-
let note_bytes_result: Vec<u8> =
94-
from_value(note_bytes).map_err(|err| err.to_string())?;
95-
96-
let note_file =
97-
NoteFile::read_from_bytes(&note_bytes_result).map_err(|err| err.to_string())?;
98-
99-
let imported = client
100-
.import_note(note_file)
92+
Ok(client
93+
.import_note(note_file.into())
10194
.await
102-
.map_err(|err| js_error_with_context(err, "failed to import note"))?;
103-
104-
Ok(JsValue::from_str(&imported.to_string()))
95+
.map_err(|err| js_error_with_context(err, "failed to import note"))?
96+
.into())
10597
} else {
10698
Err(JsValue::from_str("Client not initialized"))
10799
}

crates/web-client/src/models/input_note.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use super::note_location::NoteLocation;
88

99
#[derive(Clone)]
1010
#[wasm_bindgen]
11-
pub struct InputNote(NativeInputNote);
11+
pub struct InputNote(pub(crate) NativeInputNote);
1212

1313
#[wasm_bindgen]
1414
impl InputNote {

crates/web-client/src/models/input_note_record.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
use miden_client::store::InputNoteRecord as NativeInputNoteRecord;
2+
use miden_client::transaction::InputNote as NativeInputNote;
23
use wasm_bindgen::prelude::*;
34

45
use super::input_note_state::InputNoteState;
56
use super::note_details::NoteDetails;
67
use super::note_id::NoteId;
78
use super::note_inclusion_proof::NoteInclusionProof;
89
use super::note_metadata::NoteMetadata;
10+
use crate::js_error_with_context;
11+
use crate::models::input_note::InputNote;
912

1013
#[derive(Clone)]
1114
#[wasm_bindgen]
@@ -57,6 +60,14 @@ impl InputNoteRecord {
5760
pub fn is_processing(&self) -> bool {
5861
self.0.is_processing()
5962
}
63+
64+
#[wasm_bindgen(js_name = "toInputNote")]
65+
pub fn to_input_note(&self) -> Result<InputNote, JsValue> {
66+
let input_note: NativeInputNote = self.0.clone().try_into().map_err(|err| {
67+
js_error_with_context(err, "could not create InputNote from InputNoteRecord")
68+
})?;
69+
Ok(InputNote(input_note))
70+
}
6071
}
6172

6273
// CONVERSIONS

crates/web-client/src/models/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub mod note_assets;
6363
pub mod note_details;
6464
pub mod note_execution_hint;
6565
pub mod note_execution_mode;
66+
pub mod note_file;
6667
pub mod note_filter;
6768
pub mod note_header;
6869
pub mod note_id;

0 commit comments

Comments
 (0)