miden-multisig-client 0.17.0

High-level SDK for interacting with multisig accounts on Miden via GUARDIAN
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
//! Recovery primitives for MultisigClient.
//!
//! After key-based recovery the local Miden store starts empty, so notes the
//! account was in the middle of consuming are gone. v2 `consume_notes`
//! proposals embed the serialized notes they consume, which makes
//! pending proposals opportunistic recovery material: this module rebuilds
//! importable notes from those embedded bytes plus a node-fetched inclusion
//! proof, without needing the node to hold the note body — so it works for
//! private notes too.

use std::collections::{BTreeMap, BTreeSet, HashSet};

use miden_client::ClientError;
use miden_client::note::{NoteFile, NoteSyncHint};
use miden_client::store::{InputNoteRecord, NoteFilter as StoreNoteFilter};
use miden_protocol::block::BlockNumber;
use miden_protocol::note::{Note, NoteDetailsCommitment, NoteId, NoteInclusionProof};

use super::MultisigClient;
use crate::proposal::{Proposal, ProposalMetadata};

/// Where a recovered note's bytes came from.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NoteImportSource {
    /// Embedded in a v2 `consume_notes` proposal (`consume_notes_notes`).
    Proposal,
    /// Discovered on chain by a tag-scoped historical scan
    /// ([`MultisigClient::backfill_public_notes_by_tag`]).
    Backfill,
}

impl NoteImportSource {
    /// Stable string form, shared with the TS SDK's `source` union.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Proposal => "proposal",
            Self::Backfill => "backfill",
        }
    }
}

impl std::fmt::Display for NoteImportSource {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Per-note result of a recovery import attempt.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NoteImportStatus {
    /// Note imported with its on-chain inclusion proof; it lands in the
    /// store's `Unverified` state and the next sync verifies it.
    Imported,
    /// The local store already tracks this note (not yet consumed).
    AlreadyPresent,
    /// The note is already consumed — either the local store tracked it as
    /// consumed, or the chain had nullified it and the import recorded it as
    /// consumption history rather than a consumable note.
    AlreadyConsumed,
    /// The chain does not know the note yet. Its details were recorded in
    /// `Expected` state with its tag tracked, so a later sync picks it up
    /// once it commits.
    NotCommitted,
    /// The embedded bytes could not be decoded into a note.
    Invalid,
    /// The import attempt failed (store or RPC error).
    Failed,
}

impl NoteImportStatus {
    /// Stable string form, shared with the TS SDK's `status` union.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Imported => "imported",
            Self::AlreadyPresent => "already-present",
            Self::AlreadyConsumed => "already-consumed",
            Self::NotCommitted => "not-committed",
            Self::Invalid => "invalid",
            Self::Failed => "failed",
        }
    }
}

impl std::fmt::Display for NoteImportStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Outcome of one unique embedded note's recovery import. A batch of outcomes
/// is the full report of [`MultisigClient::import_notes_from_proposals`]; no
/// per-note problem aborts the batch. A note embedded by several proposals is
/// deduplicated into a single outcome (its first occurrence).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NoteImportOutcome {
    /// The note ID hex when the bytes decoded, otherwise a positional
    /// reference into the proposal (`proposal <id> notes[<i>]`).
    pub identifier: String,
    /// Where the note bytes came from.
    pub source: NoteImportSource,
    /// What happened to this note.
    pub status: NoteImportStatus,
    /// Whether retrying the import later can change the status (transient
    /// RPC failures, notes not yet committed).
    pub retryable: bool,
    /// Human-readable detail for non-success statuses — or, on an `Imported`
    /// outcome, a warning that the post-import consumed-state check failed
    /// and a sync should confirm the note's status.
    pub reason: Option<String>,
}

/// Decodes, validates, and deduplicates the embedded notes of v2
/// `consume_notes` proposals. Undecodable entries, embeddings past the
/// declared `note_ids` list, and embeddings whose decoded ID disagrees with
/// the declared one are reported as `Invalid` outcomes with a positional
/// identifier; a note embedded by several proposals folds into its first
/// occurrence.
///
/// The per-index ID binding is enforced here even though the verify/execute
/// path checks it again before signing: recovery runs automatically over
/// synced proposals, so without the binding a malformed or adversarial
/// proposal could smuggle arbitrary notes (and, for uncommitted ones,
/// persistent expected records and tag subscriptions) into the local store.
/// Only notes the signed proposal actually declares are recovery material.
fn collect_notes<'a>(
    proposals: impl IntoIterator<Item = (&'a str, &'a ProposalMetadata)>,
    outcomes: &mut Vec<NoteImportOutcome>,
) -> Vec<Note> {
    let mut seen: HashSet<NoteId> = HashSet::new();
    let mut decoded = Vec::new();
    for (proposal_id, metadata) in proposals {
        if !metadata.is_consume_notes_v2() {
            continue;
        }
        for (index, serialized) in metadata.consume_notes_notes.iter().enumerate() {
            let identifier = format!("proposal {} notes[{}]", proposal_id, index);
            let Some(declared_id) = metadata.note_ids_hex.get(index) else {
                outcomes.push(NoteImportOutcome {
                    identifier,
                    source: NoteImportSource::Proposal,
                    status: NoteImportStatus::Invalid,
                    retryable: false,
                    reason: Some(
                        "embedded note has no matching entry in the proposal's declared \
                         note ids"
                            .to_string(),
                    ),
                });
                continue;
            };
            match serialized.to_note() {
                Ok(note) => {
                    if !crate::utils::hex_body_eq(&note.id().to_hex(), declared_id) {
                        outcomes.push(NoteImportOutcome {
                            identifier,
                            source: NoteImportSource::Proposal,
                            status: NoteImportStatus::Invalid,
                            retryable: false,
                            reason: Some(format!(
                                "embedded note decodes to {} but the proposal declares {}",
                                note.id().to_hex(),
                                declared_id
                            )),
                        });
                        continue;
                    }
                    if seen.insert(note.id()) {
                        decoded.push(note);
                    }
                }
                Err(e) => outcomes.push(NoteImportOutcome {
                    identifier,
                    source: NoteImportSource::Proposal,
                    status: NoteImportStatus::Invalid,
                    retryable: false,
                    reason: Some(format!("failed to decode embedded note: {}", e)),
                }),
            }
        }
    }
    decoded
}

/// Import errors are usually local store problems, but upstream
/// `import_notes` performs node RPC internally (nullifier checks, block
/// header fetches), so a transient RPC failure mid-import is retryable.
fn import_error_retryable(error: &ClientError) -> bool {
    matches!(error, ClientError::RpcError(rpc) if crate::rpc::is_transient_rpc_error(rpc))
}

impl MultisigClient {
    /// Looks up existing store records for the given notes, keyed by details
    /// commitment rather than note ID: records the store keeps without
    /// metadata (a note details import in `Expected` state, or a note
    /// observed as consumed on chain) have no note ID, and an ID lookup
    /// would keep re-importing them forever. Shared by the recovery
    /// primitives (proposal import and public backfill).
    // The Err variant is upstream's `miden_client::ClientError`, which this crate propagates
    // verbatim; boxing it here would only move the cost to the caller's `?`.
    #[allow(clippy::result_large_err)]
    pub(crate) async fn existing_records_by_commitment(
        &mut self,
        commitments: Vec<NoteDetailsCommitment>,
    ) -> std::result::Result<BTreeMap<NoteDetailsCommitment, InputNoteRecord>, ClientError> {
        Ok(self
            .miden_client
            .get_input_notes(StoreNoteFilter::DetailsCommitments(commitments))
            .await?
            .into_iter()
            .map(|record| (record.details_commitment(), record))
            .collect())
    }

    /// Imports one note with its inclusion proof and classifies the result.
    /// Upstream `import_notes` batches are atomic, which is why callers
    /// import individually — one bad note must not sink the rest. Returns
    /// the outcome and whether the import succeeded (input for the batched
    /// consumed-state re-check).
    pub(crate) async fn import_note_with_proof(
        &mut self,
        source: NoteImportSource,
        note: Note,
        proof: NoteInclusionProof,
    ) -> (NoteImportOutcome, bool) {
        let identifier = note.id().to_hex();
        let file = NoteFile::Committed { note, proof };
        match self
            .miden_client
            .import_notes(std::slice::from_ref(&file))
            .await
        {
            Ok(_) => (
                NoteImportOutcome {
                    identifier,
                    source,
                    status: NoteImportStatus::Imported,
                    retryable: false,
                    reason: None,
                },
                true,
            ),
            Err(e) => (
                NoteImportOutcome {
                    identifier,
                    source,
                    status: NoteImportStatus::Failed,
                    retryable: import_error_retryable(&e),
                    reason: Some(format!("failed to import note: {}", e)),
                },
                false,
            ),
        }
    }

    /// Re-classifies provisionally `Imported` outcomes from the records the
    /// import actually left behind. A note the chain had already nullified
    /// is stored as consumption history, not as a consumable note — reported
    /// as `AlreadyConsumed`. A note whose inclusion proof failed
    /// verification against the authenticated block header is stored in
    /// `Invalid` state by upstream while the import still returns `Ok` —
    /// reported as `Failed`, because "recovered" notes that can never be
    /// consumed must not count as recovered. One batched store read covers
    /// every imported note. A failed check downgrades nothing; it flags the
    /// outcome's classification as unconfirmed instead.
    pub(crate) async fn reclassify_consumed_imports(
        &mut self,
        imported: &[(usize, NoteDetailsCommitment)],
        outcomes: &mut [NoteImportOutcome],
    ) {
        use miden_client::store::InputNoteState;

        if imported.is_empty() {
            return;
        }
        let check = self
            .miden_client
            .get_input_notes(StoreNoteFilter::DetailsCommitments(
                imported.iter().map(|(_, commitment)| *commitment).collect(),
            ))
            .await;
        match check {
            Ok(records) => {
                let mut consumed: BTreeSet<NoteDetailsCommitment> = BTreeSet::new();
                let mut invalid: BTreeSet<NoteDetailsCommitment> = BTreeSet::new();
                for record in &records {
                    if record.is_consumed() {
                        consumed.insert(record.details_commitment());
                    } else if matches!(record.state(), InputNoteState::Invalid(_)) {
                        invalid.insert(record.details_commitment());
                    }
                }
                for (index, commitment) in imported {
                    if consumed.contains(commitment) {
                        outcomes[*index].status = NoteImportStatus::AlreadyConsumed;
                        outcomes[*index].reason = Some(
                            "note was already consumed on chain; recorded as consumption \
                             history"
                                .to_string(),
                        );
                    } else if invalid.contains(commitment) {
                        outcomes[*index].status = NoteImportStatus::Failed;
                        outcomes[*index].retryable = false;
                        outcomes[*index].reason = Some(
                            "the note's inclusion proof failed verification against the \
                             authenticated block header; the record is stored as invalid \
                             and the note is not consumable"
                                .to_string(),
                        );
                    }
                }
            }
            Err(e) => {
                // The imports themselves succeeded; stay `Imported` but
                // flag that the post-import state check is unknown.
                for (index, _) in imported {
                    outcomes[*index].reason = Some(format!(
                        "imported, but the post-import state check failed ({}); run sync \
                         to confirm the note's status",
                        e
                    ));
                }
            }
        }
    }
}

impl MultisigClient {
    /// Imports the notes embedded in v2 `consume_notes` proposals into the
    /// local Miden store, typically after key-based recovery
    /// rebuilt the proposal list but left the note store empty.
    ///
    /// Proposals are opportunistic recovery material, not a backup: v1
    /// proposals carry no note bytes, and proposals disappear once
    /// canonicalized, so only notes still mid-consumption are recoverable
    /// this way.
    ///
    /// Per note: decode the embedded bytes, skip notes the store already
    /// tracks, fetch the on-chain inclusion proof, and import the note
    /// individually (upstream `import_notes` batches are atomic, so one bad
    /// note must not sink the rest). A note the chain does not know yet is
    /// recorded in `Expected` state with its tag tracked so a later sync
    /// picks it up, and is reported as `NotCommitted`/retryable. A note the
    /// chain has already nullified is recorded as consumption history and
    /// reported `AlreadyConsumed`.
    ///
    /// The returned outcomes cover every unique embedded note — a note
    /// embedded by several proposals yields one outcome, not one per
    /// embedding — and no per-note problem aborts the batch, which is why
    /// this returns a plain `Vec` instead of `Result`.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let proposals = client.list_proposals().await?;
    /// for outcome in client.import_notes_from_proposals(&proposals).await {
    ///     println!("{}: {}", outcome.identifier, outcome.status);
    /// }
    /// client.sync().await?;
    /// ```
    pub(crate) async fn import_notes_from_proposals(
        &mut self,
        proposals: &[Proposal],
    ) -> Vec<NoteImportOutcome> {
        let mut outcomes = Vec::new();
        let decoded = collect_notes(
            proposals.iter().map(|p| (p.id.as_str(), &p.metadata)),
            &mut outcomes,
        );
        if decoded.is_empty() {
            return outcomes;
        }

        let commitments: Vec<NoteDetailsCommitment> =
            decoded.iter().map(Note::details_commitment).collect();
        let existing = match self.existing_records_by_commitment(commitments).await {
            Ok(existing) => existing,
            Err(e) => {
                let reason = format!("failed to read local store: {}", e);
                for note in &decoded {
                    outcomes.push(NoteImportOutcome {
                        identifier: note.id().to_hex(),
                        source: NoteImportSource::Proposal,
                        status: NoteImportStatus::Failed,
                        retryable: false,
                        reason: Some(reason.clone()),
                    });
                }
                return outcomes;
            }
        };

        let mut pending: Vec<Note> = Vec::new();
        for note in decoded {
            match existing.get(&note.details_commitment()) {
                Some(record) => {
                    let status = if record.is_consumed() {
                        NoteImportStatus::AlreadyConsumed
                    } else {
                        NoteImportStatus::AlreadyPresent
                    };
                    outcomes.push(NoteImportOutcome {
                        identifier: note.id().to_hex(),
                        source: NoteImportSource::Proposal,
                        status,
                        retryable: false,
                        reason: None,
                    });
                }
                None => pending.push(note),
            }
        }

        if pending.is_empty() {
            return outcomes;
        }

        // One round trip for all missing notes; only the import itself is
        // per-note. The node returns proofs for private notes too, so the
        // locally-held bytes are the only body this path ever needs.
        let ids: Vec<NoteId> = pending.iter().map(Note::id).collect();
        let fetched = match self.node_rpc_client().get_notes_by_id(&ids).await {
            Ok(fetched) => fetched,
            Err(e) => {
                let retryable = crate::rpc::is_transient_rpc_error(&e);
                let reason = format!("failed to fetch inclusion proofs: {}", e);
                for note in &pending {
                    outcomes.push(NoteImportOutcome {
                        identifier: note.id().to_hex(),
                        source: NoteImportSource::Proposal,
                        status: NoteImportStatus::Failed,
                        retryable,
                        reason: Some(reason.clone()),
                    });
                }
                return outcomes;
            }
        };
        let mut proofs: BTreeMap<NoteId, NoteInclusionProof> = fetched
            .iter()
            .map(|f| (f.id(), f.inclusion_proof().clone()))
            .collect();

        // Provisionally `Imported` outcomes, re-classified in one batched
        // consumed-state check below.
        let mut imported: Vec<(usize, NoteDetailsCommitment)> = Vec::new();

        for note in pending {
            let identifier = note.id().to_hex();
            let outcome = match proofs.remove(&note.id()) {
                Some(proof) => {
                    let details_commitment = note.details_commitment();
                    let (outcome, was_imported) = self
                        .import_note_with_proof(NoteImportSource::Proposal, note, proof)
                        .await;
                    if was_imported {
                        imported.push((outcomes.len(), details_commitment));
                    }
                    outcome
                }
                None => {
                    // The sync hint's tag makes the resulting `Expected`
                    // record reachable by sync (upstream registers a
                    // note-source tag record from it).
                    let tag = note.metadata().tag();
                    let file = NoteFile::ExpectedNote {
                        details: note.into(),
                        sync_hint: NoteSyncHint::new(BlockNumber::from(0u32), tag),
                    };
                    match self
                        .miden_client
                        .import_notes(std::slice::from_ref(&file))
                        .await
                    {
                        Ok(_) => NoteImportOutcome {
                            identifier,
                            source: NoteImportSource::Proposal,
                            status: NoteImportStatus::NotCommitted,
                            retryable: true,
                            reason: Some(
                                "note not yet committed on chain; recorded as expected so a \
                                 later sync picks it up"
                                    .to_string(),
                            ),
                        },
                        Err(e) => NoteImportOutcome {
                            identifier,
                            source: NoteImportSource::Proposal,
                            status: NoteImportStatus::Failed,
                            retryable: import_error_retryable(&e),
                            reason: Some(format!("failed to record expected note: {}", e)),
                        },
                    }
                }
            };
            outcomes.push(outcome);
        }

        self.reclassify_consumed_imports(&imported, &mut outcomes)
            .await;

        outcomes
    }
}

#[cfg(test)]
mod tests {
    use miden_protocol::account::delta::{AccountDelta, AccountVaultDelta};
    use miden_protocol::account::{AccountId, AccountStoragePatch};
    use miden_protocol::asset::FungibleAsset;
    use miden_protocol::crypto::rand::RandomCoin;
    use miden_protocol::note::NoteType;
    use miden_protocol::{Felt, Word};
    use miden_standards::note::P2idNote;

    use super::*;
    use crate::proposal::SerializedNote;

    fn build_test_note(seed: u64) -> Note {
        let sender = AccountId::from_hex("0x7b7b7b7a7b7b7b017b7b7b7b7b7b7b").unwrap();
        let target = AccountId::from_hex("0x1b1b1b1a1b1b1b011b1b1b1b1b1b1b").unwrap();
        let mut rng = RandomCoin::new(Word::from([Felt::new_unchecked(seed); 4]));
        P2idNote::builder()
            .sender(sender)
            .target(target)
            .asset(FungibleAsset::mock(1))
            .note_type(NoteType::Private)
            .generate_serial_number(&mut rng)
            .build()
            .unwrap()
            .into()
    }

    /// v2 metadata with explicit `(embedded bytes, declared note id)` pairs,
    /// for driving the binding check with mismatches and undecodable bytes.
    fn v2_metadata_entries(entries: Vec<(SerializedNote, String)>) -> ProposalMetadata {
        ProposalMetadata {
            consume_notes_metadata_version: Some(
                crate::proposal::CONSUME_NOTES_METADATA_VERSION_V2,
            ),
            note_ids_hex: entries.iter().map(|(_, id)| id.clone()).collect(),
            consume_notes_notes: entries.into_iter().map(|(note, _)| note).collect(),
            ..Default::default()
        }
    }

    /// Well-formed v2 metadata: embedded bytes with their real note ids
    /// declared, the shape proposal creation emits.
    fn v2_metadata(notes: &[&Note]) -> ProposalMetadata {
        v2_metadata_entries(
            notes
                .iter()
                .map(|note| (SerializedNote::from_note(note), note.id().to_hex()))
                .collect(),
        )
    }

    #[test]
    fn collects_decoded_notes_from_v2_metadata() {
        let note = build_test_note(1);
        let expected_id = note.id();
        let metadata = v2_metadata(&[&note]);

        let mut outcomes = Vec::new();
        let decoded = collect_notes([("p-1", &metadata)], &mut outcomes);
        assert!(outcomes.is_empty());
        assert_eq!(decoded.len(), 1);
        assert_eq!(decoded[0].id(), expected_id);
    }

    /// An embedded note whose decoded ID disagrees with the declared one is
    /// not recovery material: recovery runs automatically over synced
    /// proposals, so the binding is what keeps a malformed or adversarial
    /// proposal from smuggling arbitrary notes into the store.
    #[test]
    fn embedded_note_mismatching_the_declared_id_is_invalid() {
        let declared = build_test_note(1);
        let smuggled = build_test_note(2);
        let metadata = v2_metadata_entries(vec![(
            SerializedNote::from_note(&smuggled),
            declared.id().to_hex(),
        )]);

        let mut outcomes = Vec::new();
        let decoded = collect_notes([("p-1", &metadata)], &mut outcomes);
        assert!(decoded.is_empty(), "mismatched note must not be collected");
        assert_eq!(outcomes.len(), 1);
        assert_eq!(outcomes[0].status, NoteImportStatus::Invalid);
        assert!(!outcomes[0].retryable);
        assert!(outcomes[0].reason.as_deref().unwrap().contains("declares"));
    }

    /// An embedding past the declared note-id list has no binding to check
    /// against, so it is rejected rather than trusted.
    #[test]
    fn embedded_note_beyond_the_declared_ids_is_invalid() {
        let declared = build_test_note(1);
        let extra = build_test_note(2);
        let mut metadata = v2_metadata(&[&declared]);
        metadata
            .consume_notes_notes
            .push(SerializedNote::from_note(&extra));

        let mut outcomes = Vec::new();
        let decoded = collect_notes([("p-1", &metadata)], &mut outcomes);
        assert_eq!(decoded.len(), 1, "the bound note is still collected");
        assert_eq!(decoded[0].id(), declared.id());
        assert_eq!(outcomes.len(), 1);
        assert_eq!(outcomes[0].identifier, "proposal p-1 notes[1]");
        assert_eq!(outcomes[0].status, NoteImportStatus::Invalid);
        assert!(
            outcomes[0]
                .reason
                .as_deref()
                .unwrap()
                .contains("no matching entry")
        );
    }

    /// The binding compares hex bodies, so prefix/case differences in the
    /// wire form must not reject a genuinely matching note.
    #[test]
    fn declared_id_matching_ignores_hex_prefix_and_case() {
        let note = build_test_note(1);
        let metadata = v2_metadata_entries(vec![(
            SerializedNote::from_note(&note),
            note.id().to_hex().trim_start_matches("0x").to_uppercase(),
        )]);

        let mut outcomes = Vec::new();
        let decoded = collect_notes([("p-1", &metadata)], &mut outcomes);
        assert!(outcomes.is_empty(), "{outcomes:?}");
        assert_eq!(decoded.len(), 1);
    }

    #[test]
    fn skips_v1_metadata() {
        let note = build_test_note(1);
        // v1 wire shape never carries notes, but even a malformed hybrid must
        // not be treated as recovery material.
        let metadata = ProposalMetadata {
            consume_notes_metadata_version: None,
            consume_notes_notes: vec![SerializedNote::from_note(&note)],
            ..Default::default()
        };

        let mut outcomes = Vec::new();
        assert!(collect_notes([("p-1", &metadata)], &mut outcomes).is_empty());
        assert!(outcomes.is_empty());
    }

    #[test]
    fn malformed_note_is_isolated_with_positional_identifier() {
        let good = build_test_note(1);
        let metadata = v2_metadata_entries(vec![
            (
                SerializedNote::from_base64("!!! not base64 !!!".to_string()),
                "0xdead".to_string(),
            ),
            (SerializedNote::from_note(&good), good.id().to_hex()),
        ]);

        let mut outcomes = Vec::new();
        let decoded = collect_notes([("p-9", &metadata)], &mut outcomes);
        assert_eq!(decoded.len(), 1);
        assert_eq!(decoded[0].id(), good.id());
        assert_eq!(outcomes.len(), 1);
        assert_eq!(outcomes[0].identifier, "proposal p-9 notes[0]");
        assert_eq!(outcomes[0].status, NoteImportStatus::Invalid);
        assert!(!outcomes[0].retryable);
        assert!(
            outcomes[0]
                .reason
                .as_deref()
                .unwrap()
                .contains("failed to decode")
        );
    }

    #[test]
    fn duplicate_notes_across_proposals_are_deduplicated() {
        let shared = build_test_note(1);
        let other = build_test_note(2);
        let first = v2_metadata(&[&shared]);
        let second = v2_metadata(&[&shared, &other]);

        let mut outcomes = Vec::new();
        let decoded = collect_notes([("p-1", &first), ("p-2", &second)], &mut outcomes);
        assert!(outcomes.is_empty());
        let ids: Vec<_> = decoded.iter().map(Note::id).collect();
        assert_eq!(ids, vec![shared.id(), other.id()]);
    }

    fn v2_proposal(id: &str, entries: Vec<(SerializedNote, String)>) -> Proposal {
        let notes: Vec<SerializedNote> = entries.iter().map(|(note, _)| note.clone()).collect();
        let account_id = AccountId::from_hex("0x7b7b7b7a7b7b7b017b7b7b7b7b7b7b").unwrap();
        let delta = AccountDelta::new(
            account_id,
            AccountStoragePatch::default(),
            AccountVaultDelta::default(),
            None,
            miden_protocol::Felt::ZERO,
        )
        .unwrap();
        let tx_summary = miden_protocol::transaction::TransactionSummary::new(
            delta,
            miden_protocol::transaction::InputNotes::new(Vec::new()).unwrap(),
            miden_protocol::transaction::RawOutputNotes::new(Vec::new()).unwrap(),
            Word::default(),
            0,
            miden_protocol::transaction::TransactionSummaryUserParams::new(
                [miden_protocol::Felt::ZERO; 7],
            ),
        );
        Proposal {
            id: id.to_string(),
            nonce: 1,
            transaction_type: crate::proposal::TransactionType::ConsumeNotes {
                note_ids: vec![],
                metadata_version: Some(crate::proposal::CONSUME_NOTES_METADATA_VERSION_V2),
                notes: notes.clone(),
            },
            status: crate::proposal::ProposalStatus::Pending,
            tx_summary,
            signatures: vec![],
            metadata: v2_metadata_entries(entries),
        }
    }

    /// Exercises the method end to end against an unreachable node:
    /// decoding, deduplication, and invalid isolation must all happen before
    /// any network access, and the proof-fetch failure must surface as
    /// per-note retryable outcomes instead of aborting the batch. (The
    /// success-path branches are covered by the mock-chain behavioral tests
    /// below.)
    #[tokio::test]
    async fn unreachable_node_reports_per_note_failures_without_aborting() {
        let dir = tempfile::tempdir().unwrap();
        let mut client = crate::MultisigClient::builder()
            .miden_endpoint(miden_client::rpc::Endpoint::try_from("http://127.0.0.1:1").unwrap())
            .guardian_endpoint("http://127.0.0.1:1")
            .account_dir(dir.path())
            .generate_key()
            .build()
            .await
            .unwrap();

        let note = build_test_note(1);
        let proposals = vec![
            v2_proposal(
                "p-1",
                vec![
                    (SerializedNote::from_note(&note), note.id().to_hex()),
                    (
                        SerializedNote::from_base64("!!! corrupt !!!".to_string()),
                        "0xdead".to_string(),
                    ),
                ],
            ),
            // Duplicate embedding of the same note -> deduplicated.
            v2_proposal(
                "p-2",
                vec![(SerializedNote::from_note(&note), note.id().to_hex())],
            ),
        ];

        let outcomes = client.import_notes_from_proposals(&proposals).await;

        assert_eq!(outcomes.len(), 2, "dedup folds the duplicate embedding");
        let invalid = outcomes
            .iter()
            .find(|o| o.status == NoteImportStatus::Invalid)
            .expect("malformed note reported");
        assert_eq!(invalid.identifier, "proposal p-1 notes[1]");
        assert!(!invalid.retryable);

        let failed = outcomes
            .iter()
            .find(|o| o.identifier == note.id().to_hex())
            .expect("decodable note reported");
        assert_eq!(failed.status, NoteImportStatus::Failed);
        assert!(
            failed.retryable,
            "connection failure must classify retryable"
        );
        assert!(
            failed
                .reason
                .as_deref()
                .unwrap()
                .contains("failed to fetch inclusion proofs"),
            "failure must point at the proof fetch"
        );
    }

    // ---------------------------------------------------------------------
    // behavioral tests against the mock chain: the success-path branches the
    // public method routes through (committed public/private import,
    // uncommitted expected recording, store-tracked dedup).
    // ---------------------------------------------------------------------

    /// Mock-chain client with a synced store, ready for proof imports.
    async fn behavioral_client(
        dir: &std::path::Path,
        api: std::sync::Arc<miden_client::testing::mock::MockRpcApi>,
    ) -> crate::MultisigClient {
        let mut client = crate::client::test_support::offline_client_with_node(dir, api).await;
        client.miden_client.sync_state().await.unwrap();
        client
    }

    fn proposal_for(note: &Note) -> Proposal {
        v2_proposal(
            "p-live",
            vec![(SerializedNote::from_note(note), note.id().to_hex())],
        )
    }

    /// A committed PUBLIC note imports with its fetched proof and lands as a
    /// proof-backed consumable record; re-running reports it already
    /// present (the store-tracked dedup branch).
    #[tokio::test]
    async fn imports_a_committed_public_note_and_dedups_on_rerun() {
        use miden_protocol::note::NoteType as ProtoNoteType;
        use miden_protocol::transaction::RawOutputNote;

        let dir = tempfile::tempdir().unwrap();
        let target = crate::client::test_support::test_wallet(41);
        let note = crate::client::test_support::p2id_note_for(&target, 1, ProtoNoteType::Public);
        let api =
            crate::client::test_support::chain_with_notes(vec![RawOutputNote::Full(note.clone())]);
        let mut client = behavioral_client(dir.path(), api).await;

        let outcomes = client
            .import_notes_from_proposals(&[proposal_for(&note)])
            .await;
        assert_eq!(outcomes.len(), 1, "{outcomes:?}");
        assert_eq!(outcomes[0].status, NoteImportStatus::Imported);
        assert_eq!(outcomes[0].identifier, note.id().to_hex());

        let records = client
            .miden_client
            .get_input_notes(StoreNoteFilter::All)
            .await
            .unwrap();
        assert_eq!(records.len(), 1);
        assert!(
            records[0].inclusion_proof().is_some(),
            "the record must be proof-backed"
        );

        let rerun = client
            .import_notes_from_proposals(&[proposal_for(&note)])
            .await;
        assert_eq!(rerun.len(), 1);
        assert_eq!(rerun[0].status, NoteImportStatus::AlreadyPresent);
    }

    /// A committed PRIVATE note: the chain holds only the header, so the
    /// locally embedded bytes are the body and the fetched proof makes them
    /// importable — the branch that makes proposals recovery material for
    /// private notes at all.
    #[tokio::test]
    async fn imports_a_committed_private_note_from_local_bytes() {
        use miden_protocol::note::NoteType as ProtoNoteType;
        use miden_protocol::transaction::RawOutputNote;

        let dir = tempfile::tempdir().unwrap();
        let target = crate::client::test_support::test_wallet(42);
        let note = crate::client::test_support::p2id_note_for(&target, 2, ProtoNoteType::Private);
        let api =
            crate::client::test_support::chain_with_notes(vec![RawOutputNote::Full(note.clone())]);
        let mut client = behavioral_client(dir.path(), api).await;

        let outcomes = client
            .import_notes_from_proposals(&[proposal_for(&note)])
            .await;
        assert_eq!(outcomes.len(), 1, "{outcomes:?}");
        assert_eq!(outcomes[0].status, NoteImportStatus::Imported);

        let records = client
            .miden_client
            .get_input_notes(StoreNoteFilter::All)
            .await
            .unwrap();
        assert_eq!(records.len(), 1);
        assert!(records[0].inclusion_proof().is_some());
    }

    /// A note the chain does not know yet is recorded in `Expected` state
    /// with its sync-hint tag and reported `NotCommitted`/retryable.
    #[tokio::test]
    async fn uncommitted_note_is_recorded_as_expected_and_retryable() {
        use miden_protocol::note::NoteType as ProtoNoteType;

        let dir = tempfile::tempdir().unwrap();
        let target = crate::client::test_support::test_wallet(43);
        let note = crate::client::test_support::p2id_note_for(&target, 3, ProtoNoteType::Private);
        // Chain with no notes: the proof fetch finds nothing.
        let api = crate::client::test_support::chain_with_notes(vec![]);
        let mut client = behavioral_client(dir.path(), api).await;

        let outcomes = client
            .import_notes_from_proposals(&[proposal_for(&note)])
            .await;
        assert_eq!(outcomes.len(), 1, "{outcomes:?}");
        assert_eq!(outcomes[0].status, NoteImportStatus::NotCommitted);
        assert!(outcomes[0].retryable);

        let records = client
            .miden_client
            .get_input_notes(StoreNoteFilter::Expected)
            .await
            .unwrap();
        assert_eq!(records.len(), 1, "expected record must exist for sync");
    }

    /// A note the store already tracks as consumed is reported
    /// `AlreadyConsumed` without touching the network.
    #[tokio::test]
    async fn store_tracked_consumed_note_reports_already_consumed() {
        use miden_client::store::Store;
        use miden_client::store::input_note_states::ConsumedExternalNoteState;
        use miden_protocol::note::NoteType as ProtoNoteType;

        let dir = tempfile::tempdir().unwrap();
        let target = crate::client::test_support::test_wallet(44);
        let note = crate::client::test_support::p2id_note_for(&target, 4, ProtoNoteType::Private);
        let api = crate::client::test_support::chain_with_notes(vec![]);
        let (mut client, store) =
            crate::client::test_support::offline_client_with_node_parts(dir.path(), api).await;
        client.miden_client.sync_state().await.unwrap();

        let metadata = *note.metadata();
        let record = InputNoteRecord::new(
            note.clone().into(),
            note.attachments().clone(),
            None,
            ConsumedExternalNoteState {
                nullifier_block_height: BlockNumber::from(1u32),
                consumer_account: None,
                consumed_tx_order: None,
                metadata: Some(metadata),
            }
            .into(),
        );
        store.upsert_input_notes(&[record]).await.unwrap();

        let outcomes = client
            .import_notes_from_proposals(&[proposal_for(&note)])
            .await;
        assert_eq!(outcomes.len(), 1, "{outcomes:?}");
        assert_eq!(outcomes[0].status, NoteImportStatus::AlreadyConsumed);
    }

    #[test]
    fn status_and_source_render_stable_strings() {
        assert_eq!(NoteImportSource::Proposal.to_string(), "proposal");
        assert_eq!(NoteImportSource::Backfill.to_string(), "backfill");
        let expected = [
            (NoteImportStatus::Imported, "imported"),
            (NoteImportStatus::AlreadyPresent, "already-present"),
            (NoteImportStatus::AlreadyConsumed, "already-consumed"),
            (NoteImportStatus::NotCommitted, "not-committed"),
            (NoteImportStatus::Invalid, "invalid"),
            (NoteImportStatus::Failed, "failed"),
        ];
        for (status, s) in expected {
            assert_eq!(status.to_string(), s);
        }
    }
}