use miden_client::transaction::{NoteArgs, TransactionRequest, TransactionRequestBuilder};
use miden_protocol::note::{Note, NoteId};
use miden_protocol::{Felt, Word};
use crate::MidenSdkClient;
use crate::error::{MultisigError, Result};
pub(crate) async fn fetch_notes_from_store(
client: &MidenSdkClient,
note_ids: &[NoteId],
) -> Result<Vec<Note>> {
let mut notes: Vec<Note> = Vec::with_capacity(note_ids.len());
for note_id in note_ids {
let input_note_record = client
.get_input_note(*note_id)
.await
.map_err(|e| MultisigError::MidenClient(format!("failed to fetch note: {}", e)))?
.ok_or(MultisigError::LegacyConsumeNotesNoteMissing { note_id: *note_id })?;
let note: Note = input_note_record.try_into().map_err(|e| {
MultisigError::InvalidConfig(format!("failed to convert note record to note: {:?}", e))
})?;
notes.push(note);
}
Ok(notes)
}
pub fn build_consume_notes_transaction_request_from_notes<I>(
notes: Vec<Note>,
salt: Word,
signature_advice: I,
) -> Result<TransactionRequest>
where
I: IntoIterator<Item = (Word, Vec<Felt>)>,
{
if notes.is_empty() {
return Err(MultisigError::InvalidConfig(
"no notes specified for consumption".to_string(),
));
}
let note_and_args: Vec<(Note, Option<NoteArgs>)> =
notes.into_iter().map(|n| (n, None)).collect();
let mut builder = TransactionRequestBuilder::new()
.input_notes(note_and_args)
.auth_arg(salt);
for (key, values) in signature_advice {
builder = builder.extend_advice_map([(key, values)]);
}
builder.build().map_err(|e| {
MultisigError::TransactionExecution(format!("failed to build transaction request: {}", e))
})
}
pub async fn build_consume_notes_transaction_request<I>(
client: &MidenSdkClient,
note_ids: Vec<NoteId>,
salt: Word,
signature_advice: I,
) -> Result<TransactionRequest>
where
I: IntoIterator<Item = (Word, Vec<Felt>)>,
{
if note_ids.is_empty() {
return Err(MultisigError::InvalidConfig(
"no notes specified for consumption".to_string(),
));
}
let notes = fetch_notes_from_store(client, ¬e_ids).await?;
build_consume_notes_transaction_request_from_notes(notes, salt, signature_advice)
}