1use miden_assembly::{
2 Assembler, DefaultSourceManager, LibraryPath,
3 ast::{Module, ModuleKind},
4};
5use miden_crypto::dsa::rpo_falcon512::Polynomial;
6use rand::{RngCore, rngs::StdRng};
7use std::sync::Arc;
8use tokio::time::{Duration, sleep};
9
10use miden_client::{
11 Client, ClientError, Felt, Word,
12 account::{
13 Account, AccountBuilder, AccountId, AccountStorageMode, AccountType,
14 component::{BasicFungibleFaucet, BasicWallet, RpoFalcon512},
15 },
16 asset::{Asset, FungibleAsset, TokenSymbol},
17 auth::AuthSecretKey,
18 builder::ClientBuilder,
19 crypto::{FeltRng, SecretKey},
20 keystore::FilesystemKeyStore,
21 note::{
22 Note, NoteAssets, NoteExecutionHint, NoteExecutionMode, NoteInputs, NoteMetadata,
23 NoteRecipient, NoteRelevance, NoteScript, NoteTag, NoteType,
24 },
25 rpc::{Endpoint, TonicRpcClient},
26 store::InputNoteRecord,
27 transaction::{OutputNote, TransactionKernel, TransactionRequestBuilder, TransactionScript},
28};
29use miden_lib::note::utils;
30use miden_objects::{Hasher, NoteError, assembly::Library};
31use serde::de::value::Error;
32
33pub async fn instantiate_client(
44 endpoint: Endpoint,
45 store_path: Option<&str>,
46) -> Result<Client, ClientError> {
47 let timeout_ms = 10_000;
48 let rpc_api = Arc::new(TonicRpcClient::new(&endpoint, timeout_ms));
49
50 let client = ClientBuilder::new()
51 .with_rpc(rpc_api.clone())
52 .with_filesystem_keystore("./keystore")
53 .with_sqlite_store(store_path.unwrap_or("./store.sqlite3"))
54 .in_debug_mode(true)
55 .build()
56 .await?;
57
58 Ok(client)
59}
60
61pub async fn delete_keystore_and_store(store_path: Option<&str>) {
69 let store_path = store_path.unwrap_or("./store.sqlite3");
70 if tokio::fs::metadata(store_path).await.is_ok() {
71 if let Err(e) = tokio::fs::remove_file(store_path).await {
72 eprintln!("failed to remove {}: {}", store_path, e);
73 } else {
74 println!("cleared sqlite store: {}", store_path);
75 }
76 } else {
77 println!("store not found: {}", store_path);
78 }
79
80 let keystore_dir = "./keystore";
81 match tokio::fs::read_dir(keystore_dir).await {
82 Ok(mut dir) => {
83 while let Ok(Some(entry)) = dir.next_entry().await {
84 let file_path = entry.path();
85 if let Err(e) = tokio::fs::remove_file(&file_path).await {
86 eprintln!("failed to remove {}: {}", file_path.display(), e);
87 } else {
88 println!("removed file: {}", file_path.display());
89 }
90 }
91 }
92 Err(e) => eprintln!("failed to read directory {}: {}", keystore_dir, e),
93 }
94}
95
96const N: usize = 512;
107fn mul_modulo_p(a: Polynomial<Felt>, b: Polynomial<Felt>) -> [u64; 1024] {
108 let mut c = [0; 2 * N];
109 for i in 0..N {
110 for j in 0..N {
111 c[i + j] += a.coefficients[i].as_int() * b.coefficients[j].as_int();
112 }
113 }
114 c
115}
116
117fn to_elements(poly: Polynomial<Felt>) -> Vec<Felt> {
127 poly.coefficients.to_vec()
128}
129
130pub fn generate_advice_stack_from_signature(h: Polynomial<Felt>, s2: Polynomial<Felt>) -> Vec<u64> {
141 let pi = mul_modulo_p(h.clone(), s2.clone());
142
143 let mut polynomials = to_elements(h.clone());
145 polynomials.extend(to_elements(s2.clone()));
146 polynomials.extend(pi.iter().map(|a| Felt::new(*a)));
147
148 let digest_polynomials = Hasher::hash_elements(&polynomials);
150 let challenge = (digest_polynomials[0], digest_polynomials[1]);
151 let mut advice_stack = vec![challenge.0.as_int(), challenge.1.as_int()];
152
153 let polynomials: Vec<u64> = polynomials.iter().map(|&e| e.into()).collect();
155 advice_stack.extend_from_slice(&polynomials);
156
157 advice_stack
158}
159
160pub fn create_library(
171 account_code: String,
172 library_path: &str,
173) -> Result<miden_assembly::Library, Box<dyn std::error::Error>> {
174 let assembler: Assembler = TransactionKernel::assembler().with_debug_mode(true);
175 let source_manager = Arc::new(DefaultSourceManager::default());
176 let module = Module::parser(ModuleKind::Library).parse_str(
177 LibraryPath::new(library_path)?,
178 account_code,
179 &source_manager,
180 )?;
181 let library = assembler.clone().assemble_library([module])?;
182 Ok(library)
183}
184
185pub async fn create_basic_account(
196 client: &mut Client,
197 keystore: FilesystemKeyStore<StdRng>,
198) -> Result<(miden_client::account::Account, SecretKey), ClientError> {
199 let mut init_seed = [0_u8; 32];
200 client.rng().fill_bytes(&mut init_seed);
201
202 let key_pair = SecretKey::with_rng(client.rng());
203 let anchor_block = client.get_latest_epoch_block().await.unwrap();
204 let builder = AccountBuilder::new(init_seed)
205 .anchor((&anchor_block).try_into().unwrap())
206 .account_type(AccountType::RegularAccountUpdatableCode)
207 .storage_mode(AccountStorageMode::Public)
208 .with_component(RpoFalcon512::new(key_pair.public_key().clone()))
209 .with_component(BasicWallet);
210 let (account, seed) = builder.build().unwrap();
211 client.add_account(&account, Some(seed), false).await?;
212 keystore
213 .add_key(&AuthSecretKey::RpoFalcon512(key_pair.clone()))
214 .unwrap();
215
216 Ok((account, key_pair))
217}
218
219pub async fn create_basic_faucet(
230 client: &mut Client,
231 keystore: FilesystemKeyStore<StdRng>,
232) -> Result<miden_client::account::Account, ClientError> {
233 let mut init_seed = [0u8; 32];
234 client.rng().fill_bytes(&mut init_seed);
235 let key_pair = SecretKey::with_rng(client.rng());
236 let anchor_block = client.get_latest_epoch_block().await.unwrap();
237 let symbol = TokenSymbol::new("MID").unwrap();
238 let decimals = 8;
239 let max_supply = Felt::new(1_000_000);
240 let builder = AccountBuilder::new(init_seed)
241 .anchor((&anchor_block).try_into().unwrap())
242 .account_type(AccountType::FungibleFaucet)
243 .storage_mode(AccountStorageMode::Public)
244 .with_component(RpoFalcon512::new(key_pair.public_key()))
245 .with_component(BasicFungibleFaucet::new(symbol, decimals, max_supply).unwrap());
246 let (account, seed) = builder.build().unwrap();
247 client.add_account(&account, Some(seed), false).await?;
248 keystore
249 .add_key(&AuthSecretKey::RpoFalcon512(key_pair))
250 .unwrap();
251 Ok(account)
252}
253
254pub async fn setup_accounts_and_faucets(
271 client: &mut Client,
272 keystore: FilesystemKeyStore<StdRng>,
273 num_accounts: usize,
274 num_faucets: usize,
275 balances: Vec<Vec<u64>>,
276) -> Result<(Vec<Account>, Vec<Account>), ClientError> {
277 let mut accounts = Vec::with_capacity(num_accounts);
278 for i in 0..num_accounts {
279 let (account, _) = create_basic_account(client, keystore.clone()).await?;
280 println!("Created Account #{i} => ID: {:?}", account.id());
281 accounts.push(account);
282 }
283
284 let mut faucets = Vec::with_capacity(num_faucets);
285 for j in 0..num_faucets {
286 let faucet = create_basic_faucet(client, keystore.clone()).await?;
287 println!("Created Faucet #{j} => ID: {:?}", faucet.id());
288 faucets.push(faucet);
289 }
290
291 client.sync_state().await?;
292
293 for (acct_index, account) in accounts.iter().enumerate() {
294 for (faucet_index, faucet) in faucets.iter().enumerate() {
295 let amount_to_mint = balances[acct_index][faucet_index];
296 if amount_to_mint == 0 {
297 continue;
298 }
299
300 println!(
301 "Minting {amount_to_mint} tokens from Faucet #{faucet_index} to Account #{acct_index}"
302 );
303
304 let fungible_asset = FungibleAsset::new(faucet.id(), amount_to_mint).unwrap();
305 let tx_req = TransactionRequestBuilder::new().build_mint_fungible_asset(
306 fungible_asset,
307 account.id(),
308 NoteType::Public,
309 client.rng(),
310 )
311 .unwrap();
312
313 let tx_exec = client.new_transaction(faucet.id(), tx_req).await?;
314 client.submit_transaction(tx_exec.clone()).await?;
315
316 let minted_note = if let OutputNote::Full(note) = tx_exec.created_notes().get_note(0) {
317 note.clone()
318 } else {
319 panic!("Expected OutputNote::Full, but got something else");
320 };
321
322 wait_for_notes(client, account, 1).await?;
323 client.sync_state().await?;
324
325 let consume_req = TransactionRequestBuilder::new()
326 .with_authenticated_input_notes([(minted_note.id(), None)])
327 .build()
328 .unwrap();
329
330 let tx_exec = client.new_transaction(account.id(), consume_req).await?;
331 client.submit_transaction(tx_exec).await?;
332 client.sync_state().await?;
333 }
334 }
335
336 Ok((accounts, faucets))
337}
338
339pub async fn mint_from_faucet_for_account(
357 client: &mut Client,
358 account: &Account,
359 faucet: &Account,
360 amount: u64,
361 tx_script: Option<TransactionScript>, ) -> Result<(), ClientError> {
363 if amount == 0 {
364 return Ok(());
365 }
366
367 let asset = FungibleAsset::new(faucet.id(), amount).unwrap();
368 let mint_req = TransactionRequestBuilder::new().build_mint_fungible_asset(
369 asset,
370 account.id(),
371 NoteType::Public,
372 client.rng(),
373 ).unwrap();
374
375 let mint_exec = client.new_transaction(faucet.id(), mint_req).await?;
376 client.submit_transaction(mint_exec.clone()).await?;
377
378 let minted_note = match mint_exec.created_notes().get_note(0) {
379 OutputNote::Full(note) => note.clone(),
380 _ => panic!("Expected full minted note"),
381 };
382
383 wait_for_notes(client, account, 1).await?;
384 client.sync_state().await?;
385
386 let consume_req = if let Some(script) = tx_script {
387 TransactionRequestBuilder::new()
388 .with_authenticated_input_notes([(minted_note.id(), None)])
389 .with_custom_script(script)
390 .build()?
391 } else {
392 TransactionRequestBuilder::new()
393 .with_authenticated_input_notes([(minted_note.id(), None)])
394 .build()?
395 };
396
397 let consume_exec = client.new_transaction(account.id(), consume_req).await?;
398 client.submit_transaction(consume_exec.clone()).await?;
399 client.sync_state().await?;
400
401 Ok(())
402}
403
404pub async fn create_public_note(
422 client: &mut Client,
423 note_code: String,
424 account_library: Option<Library>,
425 creator_account: Account,
426 assets: Option<NoteAssets>,
427 note_inputs: Option<NoteInputs>,
428) -> Result<Note, Error> {
429 let assembler = if let Some(library) = account_library {
430 TransactionKernel::assembler()
431 .with_library(&library)
432 .unwrap()
433 } else {
434 TransactionKernel::assembler()
435 }
436 .with_debug_mode(true);
437
438 let rng = client.rng();
439 let serial_num = rng.draw_word();
440 let note_script = NoteScript::compile(note_code, assembler.clone()).unwrap();
441
442 let note_inputs = note_inputs.unwrap_or_else(|| NoteInputs::new([].to_vec()).unwrap());
443 let assets = assets.unwrap_or_else(|| NoteAssets::new(vec![]).unwrap());
444
445 let recipient = NoteRecipient::new(serial_num, note_script, note_inputs.clone());
446 let tag = NoteTag::for_public_use_case(0, 0, NoteExecutionMode::Local).unwrap();
447 let metadata = NoteMetadata::new(
448 creator_account.id(),
449 NoteType::Public,
450 tag,
451 NoteExecutionHint::always(),
452 Felt::new(0),
453 )
454 .unwrap();
455
456 let note = Note::new(assets, metadata, recipient);
457
458 let note_req = TransactionRequestBuilder::new()
459 .with_own_output_notes(vec![OutputNote::Full(note.clone())])
460 .build()
461 .unwrap();
462 let tx_result = client
463 .new_transaction(creator_account.id(), note_req)
464 .await
465 .unwrap();
466
467 let _ = client.submit_transaction(tx_result).await;
468 client.sync_state().await.unwrap();
469
470 Ok(note)
471}
472
473pub async fn wait_for_note(
487 client: &mut Client,
488 account_id: &Account,
489 expected: &Note,
490) -> Result<(), ClientError> {
491 loop {
492 client.sync_state().await?;
493
494 let notes: Vec<(InputNoteRecord, Vec<(AccountId, NoteRelevance)>)> =
495 client.get_consumable_notes(Some(account_id.id())).await?;
496
497 let found = notes.iter().any(|(rec, _)| rec.id() == expected.id());
498
499 if found {
500 println!("✅ note found {}", expected.id().to_hex());
501 break;
502 }
503
504 println!("Note {} not found. Waiting...", expected.id().to_hex());
505 sleep(Duration::from_secs(3)).await;
506 }
507 Ok(())
508}
509
510pub async fn wait_for_notes(
524 client: &mut Client,
525 account_id: &miden_client::account::Account,
526 expected: usize,
527) -> Result<(), ClientError> {
528 loop {
529 client.sync_state().await?;
530 let notes = client.get_consumable_notes(Some(account_id.id())).await?;
531 if notes.len() >= expected {
532 break;
533 }
534 println!(
535 "{} consumable notes found for account {}. Waiting...",
536 notes.len(),
537 account_id.id().to_hex()
538 );
539 sleep(Duration::from_secs(3)).await;
540 }
541 Ok(())
542}
543
544pub fn create_tx_script(
555 script_code: String,
556 library: Option<Library>,
557) -> Result<TransactionScript, Error> {
558 let assembler = TransactionKernel::assembler();
559
560 let assembler = match library {
561 Some(lib) => assembler.with_library(lib),
562 None => Ok(assembler.with_debug_mode(true)),
563 }
564 .unwrap();
565 let tx_script = TransactionScript::compile(script_code, [], assembler).unwrap();
566
567 Ok(tx_script)
568}
569
570pub fn create_exact_p2id_note(
585 sender: AccountId,
586 target: AccountId,
587 assets: Vec<Asset>,
588 note_type: NoteType,
589 aux: Felt,
590 serial_num: Word,
591) -> Result<Note, NoteError> {
592 let recipient = utils::build_p2id_recipient(target, serial_num)?;
593 let tag = NoteTag::from_account_id(target, NoteExecutionMode::Local)?;
594
595 let metadata = NoteMetadata::new(sender, note_type, tag, NoteExecutionHint::always(), aux)?;
596 let vault = NoteAssets::new(assets)?;
597
598 Ok(Note::new(vault, metadata, recipient))
599}