fedimint_mint_client/
lib.rs

1#![deny(clippy::pedantic)]
2#![allow(clippy::cast_possible_truncation)]
3#![allow(clippy::missing_errors_doc)]
4#![allow(clippy::missing_panics_doc)]
5#![allow(clippy::module_name_repetitions)]
6#![allow(clippy::must_use_candidate)]
7#![allow(clippy::return_self_not_must_use)]
8
9// Backup and restore logic
10pub mod backup;
11/// Modularized Cli for sending and receiving out-of-band ecash
12#[cfg(feature = "cli")]
13mod cli;
14/// Database keys used throughout the mint client module
15pub mod client_db;
16/// State machines for mint inputs
17mod input;
18/// State machines for out-of-band transmitted e-cash notes
19mod oob;
20/// State machines for mint outputs
21pub mod output;
22
23pub mod event;
24
25/// API client impl for mint-specific requests
26pub mod api;
27
28pub mod repair_wallet;
29
30use std::cmp::{Ordering, min};
31use std::collections::{BTreeMap, BTreeSet};
32use std::fmt;
33use std::fmt::{Display, Formatter};
34use std::io::Read;
35use std::str::FromStr;
36use std::sync::{Arc, RwLock};
37use std::time::Duration;
38
39use anyhow::{Context as _, anyhow, bail, ensure};
40use api::MintFederationApi;
41use async_stream::{stream, try_stream};
42use backup::recovery::{MintRecovery, RecoveryStateV2};
43use base64::Engine as _;
44use bitcoin_hashes::{Hash, HashEngine as BitcoinHashEngine, sha256, sha256t};
45use client_db::{
46    DbKeyPrefix, NoteKeyPrefix, RecoveryFinalizedKey, RecoveryStateKey, RecoveryStateV2Key,
47    ReusedNoteIndices, migrate_state_to_v2, migrate_to_v1,
48};
49use event::{NoteSpent, OOBNotesReissued, OOBNotesSpent, ReceivePaymentEvent, SendPaymentEvent};
50use fedimint_api_client::api::DynModuleApi;
51use fedimint_client_module::db::{ClientModuleMigrationFn, migrate_state};
52use fedimint_client_module::module::init::{
53    ClientModuleInit, ClientModuleInitArgs, ClientModuleRecoverArgs,
54};
55use fedimint_client_module::module::recovery::RecoveryProgress;
56use fedimint_client_module::module::{
57    ClientContext, ClientModule, IClientModule, OutPointRange, PrimaryModulePriority,
58    PrimaryModuleSupport,
59};
60use fedimint_client_module::oplog::{OperationLogEntry, UpdateStreamOrOutcome};
61use fedimint_client_module::sm::{Context, DynState, ModuleNotifier, State, StateTransition};
62use fedimint_client_module::transaction::{
63    ClientInput, ClientInputBundle, ClientInputSM, ClientOutput, ClientOutputBundle,
64    ClientOutputSM, TransactionBuilder,
65};
66use fedimint_client_module::{DynGlobalClientContext, sm_enum_variant_translation};
67use fedimint_core::base32::{FEDIMINT_PREFIX, encode_prefixed};
68use fedimint_core::config::{FederationId, FederationIdPrefix};
69use fedimint_core::core::{Decoder, IntoDynInstance, ModuleInstanceId, ModuleKind, OperationId};
70use fedimint_core::db::{
71    AutocommitError, Database, DatabaseTransaction, DatabaseVersion,
72    IDatabaseTransactionOpsCoreTyped,
73};
74use fedimint_core::encoding::{Decodable, DecodeError, Encodable};
75use fedimint_core::invite_code::InviteCode;
76use fedimint_core::module::registry::{ModuleDecoderRegistry, ModuleRegistry};
77use fedimint_core::module::{
78    AmountUnit, Amounts, ApiVersion, CommonModuleInit, ModuleCommon, ModuleInit, MultiApiVersion,
79};
80use fedimint_core::secp256k1::rand::prelude::IteratorRandom;
81use fedimint_core::secp256k1::rand::thread_rng;
82use fedimint_core::secp256k1::{All, Keypair, Secp256k1};
83use fedimint_core::util::{BoxFuture, BoxStream, NextOrPending, SafeUrl};
84use fedimint_core::{
85    Amount, OutPoint, PeerId, Tiered, TieredCounts, TieredMulti, TransactionId, apply,
86    async_trait_maybe_send, base32, push_db_pair_items,
87};
88use fedimint_derive_secret::{ChildId, DerivableSecret};
89use fedimint_logging::LOG_CLIENT_MODULE_MINT;
90pub use fedimint_mint_common as common;
91use fedimint_mint_common::config::{FeeConsensus, MintClientConfig};
92pub use fedimint_mint_common::*;
93use futures::future::try_join_all;
94use futures::{StreamExt, pin_mut};
95use hex::ToHex;
96use input::MintInputStateCreatedBundle;
97use itertools::Itertools as _;
98use oob::MintOOBStatesCreatedMulti;
99use output::MintOutputStatesCreatedMulti;
100use serde::{Deserialize, Serialize};
101use strum::IntoEnumIterator;
102use tbs::AggregatePublicKey;
103use thiserror::Error;
104use tracing::{debug, warn};
105
106use crate::backup::EcashBackup;
107use crate::client_db::{
108    CancelledOOBSpendKey, CancelledOOBSpendKeyPrefix, NextECashNoteIndexKey,
109    NextECashNoteIndexKeyPrefix, NoteKey,
110};
111use crate::input::{MintInputCommon, MintInputStateMachine, MintInputStates};
112use crate::oob::{MintOOBStateMachine, MintOOBStates};
113use crate::output::{
114    MintOutputCommon, MintOutputStateMachine, MintOutputStates, NoteIssuanceRequest,
115};
116
117const MINT_E_CASH_TYPE_CHILD_ID: ChildId = ChildId(0);
118
119#[derive(Clone)]
120struct PeerSelector {
121    latency: Arc<RwLock<BTreeMap<PeerId, Duration>>>,
122}
123
124impl PeerSelector {
125    fn new(peers: BTreeSet<PeerId>) -> Self {
126        let latency = peers
127            .into_iter()
128            .map(|peer| (peer, Duration::ZERO))
129            .collect();
130
131        Self {
132            latency: Arc::new(RwLock::new(latency)),
133        }
134    }
135
136    fn choose_peer(&self) -> PeerId {
137        let latency = self.latency.read().expect("poisoned");
138
139        let peer_a = latency.iter().choose(&mut thread_rng()).expect("no peers");
140        let peer_b = latency.iter().choose(&mut thread_rng()).expect("no peers");
141
142        if peer_a.1 <= peer_b.1 {
143            *peer_a.0
144        } else {
145            *peer_b.0
146        }
147    }
148
149    fn report(&self, peer: PeerId, duration: Duration) {
150        self.latency
151            .write()
152            .expect("poisoned")
153            .entry(peer)
154            .and_modify(|latency| *latency = *latency * 9 / 10 + duration / 10)
155            .or_insert(duration);
156    }
157
158    fn remove(&self, peer: PeerId) {
159        self.latency.write().expect("poisoned").remove(&peer);
160    }
161}
162
163/// Downloads a slice with a pre-fetched hash for verification
164async fn download_slice_with_hash(
165    module_api: DynModuleApi,
166    peer_selector: PeerSelector,
167    start: u64,
168    end: u64,
169    expected_hash: sha256::Hash,
170) -> Vec<RecoveryItem> {
171    const TIMEOUT: Duration = Duration::from_secs(30);
172
173    loop {
174        let peer = peer_selector.choose_peer();
175        let start_time = fedimint_core::time::now();
176
177        match tokio::time::timeout(TIMEOUT, module_api.fetch_recovery_slice(peer, start, end))
178            .await
179            .map_err(Into::into)
180            .and_then(|r| r)
181        {
182            Ok(data) => {
183                let elapsed = fedimint_core::time::now()
184                    .duration_since(start_time)
185                    .unwrap_or(Duration::ZERO);
186
187                peer_selector.report(peer, elapsed);
188
189                if data.consensus_hash::<sha256::Hash>() == expected_hash {
190                    return data;
191                }
192
193                peer_selector.remove(peer);
194            }
195            Err(..) => {
196                peer_selector.report(peer, TIMEOUT);
197            }
198        }
199    }
200}
201
202/// An encapsulation of [`FederationId`] and e-cash notes in the form of
203/// [`TieredMulti<SpendableNote>`] for the purpose of spending e-cash
204/// out-of-band. Also used for validating and reissuing such out-of-band notes.
205///
206/// ## Invariants
207/// * Has to contain at least one `Notes` item
208/// * Has to contain at least one `FederationIdPrefix` item
209#[derive(Clone, Debug, Encodable, PartialEq, Eq)]
210pub struct OOBNotes(Vec<OOBNotesPart>);
211
212/// For extendability [`OOBNotes`] consists of parts, where client can ignore
213/// ones they don't understand.
214#[derive(Clone, Debug, Decodable, Encodable, PartialEq, Eq)]
215enum OOBNotesPart {
216    Notes(TieredMulti<SpendableNote>),
217    FederationIdPrefix(FederationIdPrefix),
218    /// Invite code to join the federation by which the e-cash was issued
219    ///
220    /// Introduced in 0.3.0
221    Invite {
222        // This is a vec for future-proofness, in case we want to include multiple guardian APIs
223        peer_apis: Vec<(PeerId, SafeUrl)>,
224        federation_id: FederationId,
225    },
226    ApiSecret(String),
227    #[encodable_default]
228    Default {
229        variant: u64,
230        bytes: Vec<u8>,
231    },
232}
233
234impl OOBNotes {
235    pub fn new(
236        federation_id_prefix: FederationIdPrefix,
237        notes: TieredMulti<SpendableNote>,
238    ) -> Self {
239        Self(vec![
240            OOBNotesPart::FederationIdPrefix(federation_id_prefix),
241            OOBNotesPart::Notes(notes),
242        ])
243    }
244
245    pub fn new_with_invite(notes: TieredMulti<SpendableNote>, invite: &InviteCode) -> Self {
246        let mut data = vec![
247            // FIXME: once we can break compatibility with 0.2 we can remove the prefix in case an
248            // invite is present
249            OOBNotesPart::FederationIdPrefix(invite.federation_id().to_prefix()),
250            OOBNotesPart::Notes(notes),
251            OOBNotesPart::Invite {
252                peer_apis: vec![(invite.peer(), invite.url())],
253                federation_id: invite.federation_id(),
254            },
255        ];
256        if let Some(api_secret) = invite.api_secret() {
257            data.push(OOBNotesPart::ApiSecret(api_secret));
258        }
259        Self(data)
260    }
261
262    pub fn federation_id_prefix(&self) -> FederationIdPrefix {
263        self.0
264            .iter()
265            .find_map(|data| match data {
266                OOBNotesPart::FederationIdPrefix(prefix) => Some(*prefix),
267                OOBNotesPart::Invite { federation_id, .. } => Some(federation_id.to_prefix()),
268                _ => None,
269            })
270            .expect("Invariant violated: OOBNotes does not contain a FederationIdPrefix")
271    }
272
273    pub fn notes(&self) -> &TieredMulti<SpendableNote> {
274        self.0
275            .iter()
276            .find_map(|data| match data {
277                OOBNotesPart::Notes(notes) => Some(notes),
278                _ => None,
279            })
280            .expect("Invariant violated: OOBNotes does not contain any notes")
281    }
282
283    pub fn notes_json(&self) -> Result<serde_json::Value, serde_json::Error> {
284        let mut notes_map = serde_json::Map::new();
285        for notes in &self.0 {
286            match notes {
287                OOBNotesPart::Notes(notes) => {
288                    let notes_json = serde_json::to_value(notes)?;
289                    notes_map.insert("notes".to_string(), notes_json);
290                }
291                OOBNotesPart::FederationIdPrefix(prefix) => {
292                    notes_map.insert(
293                        "federation_id_prefix".to_string(),
294                        serde_json::to_value(prefix.to_string())?,
295                    );
296                }
297                OOBNotesPart::Invite {
298                    peer_apis,
299                    federation_id,
300                } => {
301                    let (peer_id, api) = peer_apis
302                        .first()
303                        .cloned()
304                        .expect("Decoding makes sure peer_apis isn't empty");
305                    notes_map.insert(
306                        "invite".to_string(),
307                        serde_json::to_value(InviteCode::new(
308                            api,
309                            peer_id,
310                            *federation_id,
311                            self.api_secret(),
312                        ))?,
313                    );
314                }
315                OOBNotesPart::ApiSecret(_) => { /* already covered inside `Invite` */ }
316                OOBNotesPart::Default { variant, bytes } => {
317                    notes_map.insert(
318                        format!("default_{variant}"),
319                        serde_json::to_value(bytes.encode_hex::<String>())?,
320                    );
321                }
322            }
323        }
324        Ok(serde_json::Value::Object(notes_map))
325    }
326
327    pub fn federation_invite(&self) -> Option<InviteCode> {
328        self.0.iter().find_map(|data| {
329            let OOBNotesPart::Invite {
330                peer_apis,
331                federation_id,
332            } = data
333            else {
334                return None;
335            };
336            let (peer_id, api) = peer_apis
337                .first()
338                .cloned()
339                .expect("Decoding makes sure peer_apis isn't empty");
340            Some(InviteCode::new(
341                api,
342                peer_id,
343                *federation_id,
344                self.api_secret(),
345            ))
346        })
347    }
348
349    fn api_secret(&self) -> Option<String> {
350        self.0.iter().find_map(|data| {
351            let OOBNotesPart::ApiSecret(api_secret) = data else {
352                return None;
353            };
354            Some(api_secret.clone())
355        })
356    }
357}
358
359impl Decodable for OOBNotes {
360    fn consensus_decode_partial<R: Read>(
361        r: &mut R,
362        _modules: &ModuleDecoderRegistry,
363    ) -> Result<Self, DecodeError> {
364        let inner =
365            Vec::<OOBNotesPart>::consensus_decode_partial(r, &ModuleDecoderRegistry::default())?;
366
367        // TODO: maybe write some macros for defining TLV structs?
368        if !inner
369            .iter()
370            .any(|data| matches!(data, OOBNotesPart::Notes(_)))
371        {
372            return Err(DecodeError::from_str(
373                "No e-cash notes were found in OOBNotes data",
374            ));
375        }
376
377        let maybe_federation_id_prefix = inner.iter().find_map(|data| match data {
378            OOBNotesPart::FederationIdPrefix(prefix) => Some(*prefix),
379            _ => None,
380        });
381
382        let maybe_invite = inner.iter().find_map(|data| match data {
383            OOBNotesPart::Invite {
384                federation_id,
385                peer_apis,
386            } => Some((federation_id, peer_apis)),
387            _ => None,
388        });
389
390        match (maybe_federation_id_prefix, maybe_invite) {
391            (Some(p), Some((ip, _))) => {
392                if p != ip.to_prefix() {
393                    return Err(DecodeError::from_str(
394                        "Inconsistent Federation ID provided in OOBNotes data",
395                    ));
396                }
397            }
398            (None, None) => {
399                return Err(DecodeError::from_str(
400                    "No Federation ID provided in OOBNotes data",
401                ));
402            }
403            _ => {}
404        }
405
406        if let Some((_, invite)) = maybe_invite
407            && invite.is_empty()
408        {
409            return Err(DecodeError::from_str("Invite didn't contain API endpoints"));
410        }
411
412        Ok(OOBNotes(inner))
413    }
414}
415
416const BASE64_URL_SAFE: base64::engine::GeneralPurpose = base64::engine::GeneralPurpose::new(
417    &base64::alphabet::URL_SAFE,
418    base64::engine::general_purpose::PAD,
419);
420
421impl FromStr for OOBNotes {
422    type Err = anyhow::Error;
423
424    /// Decode a set of out-of-band e-cash notes from a base64 or base32 string.
425    fn from_str(s: &str) -> Result<Self, Self::Err> {
426        let s: String = s.chars().filter(|&c| !c.is_whitespace()).collect();
427
428        let oob_notes_bytes = if let Ok(oob_notes_bytes) =
429            base32::decode_prefixed_bytes(FEDIMINT_PREFIX, &s)
430        {
431            oob_notes_bytes
432        } else if let Ok(oob_notes_bytes) = BASE64_URL_SAFE.decode(&s) {
433            oob_notes_bytes
434        } else if let Ok(oob_notes_bytes) = base64::engine::general_purpose::STANDARD.decode(&s) {
435            oob_notes_bytes
436        } else {
437            bail!("OOBNotes were not a well-formed base64(URL-safe) or base32 string");
438        };
439
440        let oob_notes =
441            OOBNotes::consensus_decode_whole(&oob_notes_bytes, &ModuleDecoderRegistry::default())?;
442
443        ensure!(!oob_notes.notes().is_empty(), "OOBNotes cannot be empty");
444
445        Ok(oob_notes)
446    }
447}
448
449impl Display for OOBNotes {
450    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
451        let bytes = Encodable::consensus_encode_to_vec(self);
452
453        f.write_str(&BASE64_URL_SAFE.encode(&bytes))
454    }
455}
456
457impl Serialize for OOBNotes {
458    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
459    where
460        S: serde::Serializer,
461    {
462        serializer.serialize_str(&self.to_string())
463    }
464}
465
466impl<'de> Deserialize<'de> for OOBNotes {
467    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
468    where
469        D: serde::Deserializer<'de>,
470    {
471        let s = String::deserialize(deserializer)?;
472        FromStr::from_str(&s).map_err(serde::de::Error::custom)
473    }
474}
475
476impl OOBNotes {
477    /// Returns the total value of all notes in msat as `Amount`
478    pub fn total_amount(&self) -> Amount {
479        self.notes().total_amount()
480    }
481}
482
483/// The high-level state of a reissue operation started with
484/// [`MintClientModule::reissue_external_notes`].
485#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
486pub enum ReissueExternalNotesState {
487    /// The operation has been created and is waiting to be accepted by the
488    /// federation.
489    Created,
490    /// We are waiting for blind signatures to arrive but can already assume the
491    /// transaction to be successful.
492    Issuing,
493    /// The operation has been completed successfully.
494    Done,
495    /// Some error happened and the operation failed.
496    Failed(String),
497}
498
499/// The high-level state of a raw e-cash spend operation started with
500/// [`MintClientModule::spend_notes_with_selector`].
501#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
502pub enum SpendOOBState {
503    /// The e-cash has been selected and given to the caller
504    Created,
505    /// The user requested a cancellation of the operation, we are waiting for
506    /// the outcome of the cancel transaction.
507    UserCanceledProcessing,
508    /// The user-requested cancellation was successful, we got all our money
509    /// back.
510    UserCanceledSuccess,
511    /// The user-requested cancellation failed, the e-cash notes have been spent
512    /// by someone else already.
513    UserCanceledFailure,
514    /// We tried to cancel the operation automatically after the timeout but
515    /// failed, indicating the recipient reissued the e-cash to themselves,
516    /// making the out-of-band spend **successful**.
517    Success,
518    /// We tried to cancel the operation automatically after the timeout and
519    /// succeeded, indicating the recipient did not reissue the e-cash to
520    /// themselves, meaning the out-of-band spend **failed**.
521    Refunded,
522}
523
524#[derive(Debug, Clone, Serialize, Deserialize)]
525pub struct MintOperationMeta {
526    pub variant: MintOperationMetaVariant,
527    pub amount: Amount,
528    pub extra_meta: serde_json::Value,
529}
530
531#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
532#[serde(rename_all = "snake_case")]
533pub enum MintOperationMetaVariant {
534    // TODO: add migrations for operation log and clean up schema
535    /// Either `legacy_out_point` or both `txid` and `out_point_indices` will be
536    /// present.
537    Reissuance {
538        // Removed in 0.3.0:
539        #[serde(skip_serializing, default, rename = "out_point")]
540        legacy_out_point: Option<OutPoint>,
541        // Introduced in 0.3.0:
542        #[serde(default)]
543        txid: Option<TransactionId>,
544        // Introduced in 0.3.0:
545        #[serde(default)]
546        out_point_indices: Vec<u64>,
547    },
548    SpendOOB {
549        requested_amount: Amount,
550        oob_notes: OOBNotes,
551    },
552}
553
554#[derive(Debug, Clone)]
555pub struct MintClientInit;
556
557const SLICE_SIZE: u64 = 10000;
558const PARALLEL_HASH_REQUESTS: usize = 10;
559const PARALLEL_SLICE_REQUESTS: usize = 10;
560
561impl MintClientInit {
562    #[allow(clippy::too_many_lines)]
563    async fn recover_from_slices(
564        &self,
565        args: &ClientModuleRecoverArgs<Self>,
566    ) -> anyhow::Result<()> {
567        // Try to load existing state or create new one if we can fetch recovery count
568        let mut state = if let Some(state) = args
569            .db()
570            .begin_transaction_nc()
571            .await
572            .get_value(&RecoveryStateV2Key)
573            .await
574        {
575            state
576        } else {
577            // Try to fetch recovery count - if this fails, the endpoint doesn't exist
578            let total_items = args.module_api().fetch_recovery_count().await?;
579
580            RecoveryStateV2::new(
581                total_items,
582                args.cfg().tbs_pks.tiers().copied().collect(),
583                args.module_root_secret(),
584            )
585        };
586
587        if state.next_index == state.total_items {
588            return Ok(());
589        }
590
591        let peer_selector = PeerSelector::new(args.api().all_peers().clone());
592
593        let mut recovery_stream = futures::stream::iter(
594            (state.next_index..state.total_items).step_by(SLICE_SIZE as usize),
595        )
596        .map(move |start| {
597            let api = args.module_api().clone();
598            let end = std::cmp::min(start + SLICE_SIZE, state.total_items);
599
600            async move { (start, end, api.fetch_recovery_slice_hash(start, end).await) }
601        })
602        .buffered(PARALLEL_HASH_REQUESTS)
603        .map(move |(start, end, hash)| {
604            download_slice_with_hash(
605                args.module_api().clone(),
606                peer_selector.clone(),
607                start,
608                end,
609                hash,
610            )
611        })
612        .buffered(PARALLEL_SLICE_REQUESTS);
613
614        let secret = args.module_root_secret().clone();
615
616        loop {
617            let items = recovery_stream
618                .next()
619                .await
620                .expect("mint recovery stream finished before recovery is complete");
621
622            for item in &items {
623                match item {
624                    RecoveryItem::Output { amount, nonce } => {
625                        state.handle_output(*amount, *nonce, &secret);
626                    }
627                    RecoveryItem::Input { nonce } => {
628                        state.handle_input(*nonce);
629                    }
630                }
631            }
632
633            state.next_index += items.len() as u64;
634
635            let mut dbtx = args.db().begin_transaction().await;
636
637            dbtx.insert_entry(&RecoveryStateV2Key, &state).await;
638
639            if state.next_index == state.total_items {
640                // Finalize recovery - create state machines for pending outputs
641                let finalized = state.finalize();
642
643                // Collect blind nonces to fetch outpoints from server
644                let blind_nonces: Vec<BlindNonce> = finalized
645                    .pending_notes
646                    .iter()
647                    .map(|(_, req)| BlindNonce(req.blinded_message()))
648                    .collect();
649
650                // Fetch outpoints for all blind nonces
651                let outpoints = if blind_nonces.is_empty() {
652                    vec![]
653                } else {
654                    args.module_api()
655                        .fetch_blind_nonce_outpoints(blind_nonces)
656                        .await
657                        .context("Failed to fetch blind nonce outpoints")?
658                };
659
660                // Create state machines for pending notes
661                let state_machines: Vec<MintClientStateMachines> = finalized
662                    .pending_notes
663                    .into_iter()
664                    .zip(outpoints)
665                    .map(|((amount, issuance_request), out_point)| {
666                        MintClientStateMachines::Output(MintOutputStateMachine {
667                            common: MintOutputCommon {
668                                operation_id: OperationId::new_random(),
669                                out_point_range: OutPointRange::new_single(
670                                    out_point.txid,
671                                    out_point.out_idx,
672                                )
673                                .expect("Can't overflow"),
674                            },
675                            state: MintOutputStates::Created(output::MintOutputStatesCreated {
676                                amount,
677                                issuance_request,
678                            }),
679                        })
680                    })
681                    .collect();
682
683                let state_machines = args.context().map_dyn(state_machines).collect();
684
685                args.context()
686                    .add_state_machines_dbtx(&mut dbtx.to_ref_nc(), state_machines)
687                    .await?;
688
689                // Restore NextECashNoteIndexKey
690                for (amount, note_idx) in finalized.next_note_idx {
691                    dbtx.insert_entry(&NextECashNoteIndexKey(amount), &note_idx.as_u64())
692                        .await;
693                }
694
695                dbtx.commit_tx().await;
696
697                return Ok(());
698            }
699
700            dbtx.commit_tx().await;
701
702            args.update_recovery_progress(RecoveryProgress {
703                complete: state.next_index.try_into().unwrap_or(u32::MAX),
704                total: state.total_items.try_into().unwrap_or(u32::MAX),
705            });
706        }
707    }
708}
709
710impl ModuleInit for MintClientInit {
711    type Common = MintCommonInit;
712
713    async fn dump_database(
714        &self,
715        dbtx: &mut DatabaseTransaction<'_>,
716        prefix_names: Vec<String>,
717    ) -> Box<dyn Iterator<Item = (String, Box<dyn erased_serde::Serialize + Send>)> + '_> {
718        let mut mint_client_items: BTreeMap<String, Box<dyn erased_serde::Serialize + Send>> =
719            BTreeMap::new();
720        let filtered_prefixes = DbKeyPrefix::iter().filter(|f| {
721            prefix_names.is_empty() || prefix_names.contains(&f.to_string().to_lowercase())
722        });
723
724        for table in filtered_prefixes {
725            match table {
726                DbKeyPrefix::Note => {
727                    push_db_pair_items!(
728                        dbtx,
729                        NoteKeyPrefix,
730                        NoteKey,
731                        SpendableNoteUndecoded,
732                        mint_client_items,
733                        "Notes"
734                    );
735                }
736                DbKeyPrefix::NextECashNoteIndex => {
737                    push_db_pair_items!(
738                        dbtx,
739                        NextECashNoteIndexKeyPrefix,
740                        NextECashNoteIndexKey,
741                        u64,
742                        mint_client_items,
743                        "NextECashNoteIndex"
744                    );
745                }
746                DbKeyPrefix::CancelledOOBSpend => {
747                    push_db_pair_items!(
748                        dbtx,
749                        CancelledOOBSpendKeyPrefix,
750                        CancelledOOBSpendKey,
751                        (),
752                        mint_client_items,
753                        "CancelledOOBSpendKey"
754                    );
755                }
756                DbKeyPrefix::RecoveryFinalized => {
757                    if let Some(val) = dbtx.get_value(&RecoveryFinalizedKey).await {
758                        mint_client_items.insert("RecoveryFinalized".to_string(), Box::new(val));
759                    }
760                }
761                DbKeyPrefix::RecoveryState
762                | DbKeyPrefix::ReusedNoteIndices
763                | DbKeyPrefix::RecoveryStateV2
764                | DbKeyPrefix::ExternalReservedStart
765                | DbKeyPrefix::CoreInternalReservedStart
766                | DbKeyPrefix::CoreInternalReservedEnd => {}
767            }
768        }
769
770        Box::new(mint_client_items.into_iter())
771    }
772}
773
774#[apply(async_trait_maybe_send!)]
775impl ClientModuleInit for MintClientInit {
776    type Module = MintClientModule;
777
778    fn supported_api_versions(&self) -> MultiApiVersion {
779        MultiApiVersion::try_from_iter([ApiVersion { major: 0, minor: 0 }])
780            .expect("no version conflicts")
781    }
782
783    async fn init(&self, args: &ClientModuleInitArgs<Self>) -> anyhow::Result<Self::Module> {
784        Ok(MintClientModule {
785            federation_id: *args.federation_id(),
786            cfg: args.cfg().clone(),
787            secret: args.module_root_secret().clone(),
788            secp: Secp256k1::new(),
789            notifier: args.notifier().clone(),
790            client_ctx: args.context(),
791            balance_update_sender: tokio::sync::watch::channel(()).0,
792        })
793    }
794
795    async fn recover(
796        &self,
797        args: &ClientModuleRecoverArgs<Self>,
798        snapshot: Option<&<Self::Module as ClientModule>::Backup>,
799    ) -> anyhow::Result<()> {
800        let mut dbtx = args.db().begin_transaction_nc().await;
801
802        // Check if V2 (slice-based) recovery state exists
803        if dbtx.get_value(&RecoveryStateV2Key).await.is_some() {
804            return self.recover_from_slices(args).await;
805        }
806
807        // Check if V1 (session-based) recovery state exists
808        if dbtx.get_value(&RecoveryStateKey).await.is_some() {
809            return args
810                .recover_from_history::<MintRecovery>(self, snapshot)
811                .await;
812        }
813
814        // No existing recovery state - determine which to use based on endpoint
815        // availability
816        if args.module_api().fetch_recovery_count().await.is_ok() {
817            // New endpoint available - use V2 slice-based recovery
818            self.recover_from_slices(args).await
819        } else {
820            // Old federation - use V1 session-based recovery
821            args.recover_from_history::<MintRecovery>(self, snapshot)
822                .await
823        }
824    }
825
826    fn get_database_migrations(&self) -> BTreeMap<DatabaseVersion, ClientModuleMigrationFn> {
827        let mut migrations: BTreeMap<DatabaseVersion, ClientModuleMigrationFn> = BTreeMap::new();
828        migrations.insert(DatabaseVersion(0), |dbtx, _, _| {
829            Box::pin(migrate_to_v1(dbtx))
830        });
831        migrations.insert(DatabaseVersion(1), |_, active_states, inactive_states| {
832            Box::pin(async { migrate_state(active_states, inactive_states, migrate_state_to_v2) })
833        });
834
835        migrations
836    }
837
838    fn used_db_prefixes(&self) -> Option<BTreeSet<u8>> {
839        Some(
840            DbKeyPrefix::iter()
841                .map(|p| p as u8)
842                .chain(
843                    DbKeyPrefix::ExternalReservedStart as u8
844                        ..=DbKeyPrefix::CoreInternalReservedEnd as u8,
845                )
846                .collect(),
847        )
848    }
849}
850
851/// The `MintClientModule` is responsible for handling e-cash minting
852/// operations. It interacts with the mint server to issue, reissue, and
853/// validate e-cash notes.
854///
855/// # Derivable Secret
856///
857/// The `DerivableSecret` is a cryptographic secret that can be used to derive
858/// other secrets. In the context of the `MintClientModule`, it is used to
859/// derive the blinding and spend keys for e-cash notes. The `DerivableSecret`
860/// is initialized when the `MintClientModule` is created and is kept private
861/// within the module.
862///
863/// # Blinding Key
864///
865/// The blinding key is derived from the `DerivableSecret` and is used to blind
866/// the e-cash note during the issuance process. This ensures that the mint
867/// server cannot link the e-cash note to the client that requested it,
868/// providing privacy for the client.
869///
870/// # Spend Key
871///
872/// The spend key is also derived from the `DerivableSecret` and is used to
873/// spend the e-cash note. Only the client that possesses the `DerivableSecret`
874/// can derive the correct spend key to spend the e-cash note. This ensures that
875/// only the owner of the e-cash note can spend it.
876pub struct MintClientModule {
877    federation_id: FederationId,
878    cfg: MintClientConfig,
879    secret: DerivableSecret,
880    secp: Secp256k1<All>,
881    notifier: ModuleNotifier<MintClientStateMachines>,
882    pub client_ctx: ClientContext<Self>,
883    balance_update_sender: tokio::sync::watch::Sender<()>,
884}
885
886impl fmt::Debug for MintClientModule {
887    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
888        f.debug_struct("MintClientModule")
889            .field("federation_id", &self.federation_id)
890            .field("cfg", &self.cfg)
891            .field("notifier", &self.notifier)
892            .field("client_ctx", &self.client_ctx)
893            .finish_non_exhaustive()
894    }
895}
896
897// TODO: wrap in Arc
898#[derive(Clone)]
899pub struct MintClientContext {
900    pub federation_id: FederationId,
901    pub client_ctx: ClientContext<MintClientModule>,
902    pub mint_decoder: Decoder,
903    pub tbs_pks: Tiered<AggregatePublicKey>,
904    pub peer_tbs_pks: BTreeMap<PeerId, Tiered<tbs::PublicKeyShare>>,
905    pub secret: DerivableSecret,
906    // FIXME: putting a DB ref here is an antipattern, global context should become more powerful
907    // but we need to consider it more carefully as its APIs will be harder to change.
908    pub module_db: Database,
909    /// Notifies subscribers when the balance changes
910    pub balance_update_sender: tokio::sync::watch::Sender<()>,
911}
912
913impl fmt::Debug for MintClientContext {
914    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
915        f.debug_struct("MintClientContext")
916            .field("federation_id", &self.federation_id)
917            .finish_non_exhaustive()
918    }
919}
920
921impl MintClientContext {
922    fn await_cancel_oob_payment(&self, operation_id: OperationId) -> BoxFuture<'static, ()> {
923        let db = self.module_db.clone();
924        Box::pin(async move {
925            db.wait_key_exists(&CancelledOOBSpendKey(operation_id))
926                .await;
927        })
928    }
929}
930
931impl Context for MintClientContext {
932    const KIND: Option<ModuleKind> = Some(KIND);
933}
934
935#[apply(async_trait_maybe_send!)]
936impl ClientModule for MintClientModule {
937    type Init = MintClientInit;
938    type Common = MintModuleTypes;
939    type Backup = EcashBackup;
940    type ModuleStateMachineContext = MintClientContext;
941    type States = MintClientStateMachines;
942
943    fn context(&self) -> Self::ModuleStateMachineContext {
944        MintClientContext {
945            federation_id: self.federation_id,
946            client_ctx: self.client_ctx.clone(),
947            mint_decoder: self.decoder(),
948            tbs_pks: self.cfg.tbs_pks.clone(),
949            peer_tbs_pks: self.cfg.peer_tbs_pks.clone(),
950            secret: self.secret.clone(),
951            module_db: self.client_ctx.module_db().clone(),
952            balance_update_sender: self.balance_update_sender.clone(),
953        }
954    }
955
956    fn input_fee(
957        &self,
958        amount: &Amounts,
959        _input: &<Self::Common as ModuleCommon>::Input,
960    ) -> Option<Amounts> {
961        Some(Amounts::new_bitcoin(
962            self.cfg.fee_consensus.fee(amount.get_bitcoin()),
963        ))
964    }
965
966    fn output_fee(
967        &self,
968        amount: &Amounts,
969        _output: &<Self::Common as ModuleCommon>::Output,
970    ) -> Option<Amounts> {
971        Some(Amounts::new_bitcoin(
972            self.cfg.fee_consensus.fee(amount.get_bitcoin()),
973        ))
974    }
975
976    #[cfg(feature = "cli")]
977    async fn handle_cli_command(
978        &self,
979        args: &[std::ffi::OsString],
980    ) -> anyhow::Result<serde_json::Value> {
981        cli::handle_cli_command(self, args).await
982    }
983
984    fn supports_backup(&self) -> bool {
985        true
986    }
987
988    async fn backup(&self) -> anyhow::Result<EcashBackup> {
989        self.client_ctx
990            .module_db()
991            .autocommit(
992                |dbtx_ctx, _| {
993                    Box::pin(async { self.prepare_plaintext_ecash_backup(dbtx_ctx).await })
994                },
995                None,
996            )
997            .await
998            .map_err(|e| match e {
999                AutocommitError::ClosureError { error, .. } => error,
1000                AutocommitError::CommitFailed { last_error, .. } => {
1001                    anyhow!("Commit to DB failed: {last_error}")
1002                }
1003            })
1004    }
1005
1006    fn supports_being_primary(&self) -> PrimaryModuleSupport {
1007        PrimaryModuleSupport::selected(PrimaryModulePriority::HIGH, [AmountUnit::BITCOIN])
1008    }
1009
1010    async fn create_final_inputs_and_outputs(
1011        &self,
1012        dbtx: &mut DatabaseTransaction<'_>,
1013        operation_id: OperationId,
1014        unit: AmountUnit,
1015        mut input_amount: Amount,
1016        mut output_amount: Amount,
1017    ) -> anyhow::Result<(
1018        ClientInputBundle<MintInput, MintClientStateMachines>,
1019        ClientOutputBundle<MintOutput, MintClientStateMachines>,
1020    )> {
1021        let consolidation_inputs = self.consolidate_notes(dbtx).await?;
1022
1023        if unit != AmountUnit::BITCOIN {
1024            bail!("Module can only handle Bitcoin");
1025        }
1026
1027        input_amount += consolidation_inputs
1028            .iter()
1029            .map(|input| input.0.amounts.get_bitcoin())
1030            .sum();
1031
1032        output_amount += consolidation_inputs
1033            .iter()
1034            .map(|input| self.cfg.fee_consensus.fee(input.0.amounts.get_bitcoin()))
1035            .sum();
1036
1037        let additional_inputs = self
1038            .create_sufficient_input(dbtx, output_amount.saturating_sub(input_amount))
1039            .await?;
1040
1041        input_amount += additional_inputs
1042            .iter()
1043            .map(|input| input.0.amounts.get_bitcoin())
1044            .sum();
1045
1046        output_amount += additional_inputs
1047            .iter()
1048            .map(|input| self.cfg.fee_consensus.fee(input.0.amounts.get_bitcoin()))
1049            .sum();
1050
1051        let outputs = self
1052            .create_output(
1053                dbtx,
1054                operation_id,
1055                2,
1056                input_amount.saturating_sub(output_amount),
1057            )
1058            .await;
1059
1060        Ok((
1061            create_bundle_for_inputs(
1062                [consolidation_inputs, additional_inputs].concat(),
1063                operation_id,
1064            ),
1065            outputs,
1066        ))
1067    }
1068
1069    async fn await_primary_module_output(
1070        &self,
1071        operation_id: OperationId,
1072        out_point: OutPoint,
1073    ) -> anyhow::Result<()> {
1074        self.await_output_finalized(operation_id, out_point).await
1075    }
1076
1077    async fn get_balance(&self, dbtx: &mut DatabaseTransaction<'_>, unit: AmountUnit) -> Amount {
1078        if unit != AmountUnit::BITCOIN {
1079            return Amount::ZERO;
1080        }
1081        self.get_note_counts_by_denomination(dbtx)
1082            .await
1083            .total_amount()
1084    }
1085
1086    async fn get_balances(&self, dbtx: &mut DatabaseTransaction<'_>) -> Amounts {
1087        Amounts::new_bitcoin(
1088            <Self as ClientModule>::get_balance(self, dbtx, AmountUnit::BITCOIN).await,
1089        )
1090    }
1091
1092    async fn subscribe_balance_changes(&self) -> BoxStream<'static, ()> {
1093        Box::pin(tokio_stream::wrappers::WatchStream::new(
1094            self.balance_update_sender.subscribe(),
1095        ))
1096    }
1097
1098    async fn leave(&self, dbtx: &mut DatabaseTransaction<'_>) -> anyhow::Result<()> {
1099        let balance = ClientModule::get_balances(self, dbtx).await;
1100
1101        for (unit, amount) in balance {
1102            if Amount::from_units(0) < amount {
1103                bail!("Outstanding balance: {amount}, unit: {unit:?}");
1104            }
1105        }
1106
1107        if !self.client_ctx.get_own_active_states().await.is_empty() {
1108            bail!("Pending operations")
1109        }
1110        Ok(())
1111    }
1112
1113    async fn handle_rpc(
1114        &self,
1115        method: String,
1116        request: serde_json::Value,
1117    ) -> BoxStream<'_, anyhow::Result<serde_json::Value>> {
1118        Box::pin(try_stream! {
1119            match method.as_str() {
1120                "reissue_external_notes" => {
1121                    let req: ReissueExternalNotesRequest = serde_json::from_value(request)?;
1122                    let result = self.reissue_external_notes(req.oob_notes, req.extra_meta).await?;
1123                    yield serde_json::to_value(result)?;
1124                }
1125                "subscribe_reissue_external_notes" => {
1126                    let req: SubscribeReissueExternalNotesRequest = serde_json::from_value(request)?;
1127                    let stream = self.subscribe_reissue_external_notes(req.operation_id).await?;
1128                    for await state in stream.into_stream() {
1129                        yield serde_json::to_value(state)?;
1130                    }
1131                }
1132                "spend_notes" => {
1133                    let req: SpendNotesRequest = serde_json::from_value(request)?;
1134                    let result = self.spend_notes_with_selector(
1135                        &SelectNotesWithExactAmount,
1136                        req.amount,
1137                        req.try_cancel_after,
1138                        req.include_invite,
1139                        req.extra_meta
1140                    ).await?;
1141                    yield serde_json::to_value(result)?;
1142                }
1143                "spend_notes_expert" => {
1144                    let req: SpendNotesExpertRequest = serde_json::from_value(request)?;
1145                    let result = self.spend_notes_with_selector(
1146                        &SelectNotesWithAtleastAmount,
1147                        req.min_amount,
1148                        req.try_cancel_after,
1149                        req.include_invite,
1150                        req.extra_meta
1151                    ).await?;
1152                    yield serde_json::to_value(result)?;
1153                }
1154                "validate_notes" => {
1155                    let req: ValidateNotesRequest = serde_json::from_value(request)?;
1156                    let result = self.validate_notes(&req.oob_notes)?;
1157                    yield serde_json::to_value(result)?;
1158                }
1159                "try_cancel_spend_notes" => {
1160                    let req: TryCancelSpendNotesRequest = serde_json::from_value(request)?;
1161                    let result = self.try_cancel_spend_notes(req.operation_id).await;
1162                    yield serde_json::to_value(result)?;
1163                }
1164                "subscribe_spend_notes" => {
1165                    let req: SubscribeSpendNotesRequest = serde_json::from_value(request)?;
1166                    let stream = self.subscribe_spend_notes(req.operation_id).await?;
1167                    for await state in stream.into_stream() {
1168                        yield serde_json::to_value(state)?;
1169                    }
1170                }
1171                "await_spend_oob_refund" => {
1172                    let req: AwaitSpendOobRefundRequest = serde_json::from_value(request)?;
1173                    let value = self.await_spend_oob_refund(req.operation_id).await;
1174                    yield serde_json::to_value(value)?;
1175                }
1176                "note_counts_by_denomination" => {
1177                    let mut dbtx = self.client_ctx.module_db().begin_transaction_nc().await;
1178                    let note_counts = self.get_note_counts_by_denomination(&mut dbtx).await;
1179                    yield serde_json::to_value(note_counts)?;
1180                }
1181                _ => {
1182                    Err(anyhow::format_err!("Unknown method: {}", method))?;
1183                    unreachable!()
1184                },
1185            }
1186        })
1187    }
1188}
1189
1190#[derive(Deserialize)]
1191struct ReissueExternalNotesRequest {
1192    oob_notes: OOBNotes,
1193    extra_meta: serde_json::Value,
1194}
1195
1196#[derive(Deserialize)]
1197struct SubscribeReissueExternalNotesRequest {
1198    operation_id: OperationId,
1199}
1200
1201/// Caution: if no notes of the correct denomination are available the next
1202/// bigger note will be selected. You might want to use `spend_notes` instead.
1203#[derive(Deserialize)]
1204struct SpendNotesExpertRequest {
1205    min_amount: Amount,
1206    try_cancel_after: Duration,
1207    include_invite: bool,
1208    extra_meta: serde_json::Value,
1209}
1210
1211#[derive(Deserialize)]
1212struct SpendNotesRequest {
1213    amount: Amount,
1214    try_cancel_after: Duration,
1215    include_invite: bool,
1216    extra_meta: serde_json::Value,
1217}
1218
1219#[derive(Deserialize)]
1220struct ValidateNotesRequest {
1221    oob_notes: OOBNotes,
1222}
1223
1224#[derive(Deserialize)]
1225struct TryCancelSpendNotesRequest {
1226    operation_id: OperationId,
1227}
1228
1229#[derive(Deserialize)]
1230struct SubscribeSpendNotesRequest {
1231    operation_id: OperationId,
1232}
1233
1234#[derive(Deserialize)]
1235struct AwaitSpendOobRefundRequest {
1236    operation_id: OperationId,
1237}
1238
1239#[derive(thiserror::Error, Debug, Clone)]
1240pub enum ReissueExternalNotesError {
1241    #[error("Federation ID does not match")]
1242    WrongFederationId,
1243    #[error("We already reissued these notes")]
1244    AlreadyReissued,
1245}
1246
1247impl MintClientModule {
1248    async fn create_sufficient_input(
1249        &self,
1250        dbtx: &mut DatabaseTransaction<'_>,
1251        min_amount: Amount,
1252    ) -> anyhow::Result<Vec<(ClientInput<MintInput>, SpendableNote)>> {
1253        if min_amount == Amount::ZERO {
1254            return Ok(vec![]);
1255        }
1256
1257        let selected_notes = Self::select_notes(
1258            dbtx,
1259            &SelectNotesWithAtleastAmount,
1260            min_amount,
1261            self.cfg.fee_consensus.clone(),
1262        )
1263        .await?;
1264
1265        for (amount, note) in selected_notes.iter_items() {
1266            debug!(target: LOG_CLIENT_MODULE_MINT, %amount, %note, "Spending note as sufficient input to fund a tx");
1267            MintClientModule::delete_spendable_note(&self.client_ctx, dbtx, amount, note).await;
1268        }
1269
1270        let sender = self.balance_update_sender.clone();
1271        dbtx.on_commit(move || sender.send_replace(()));
1272
1273        let inputs = self.create_input_from_notes(selected_notes)?;
1274
1275        assert!(!inputs.is_empty());
1276
1277        Ok(inputs)
1278    }
1279
1280    /// Returns the number of held e-cash notes per denomination
1281    #[deprecated(
1282        since = "0.5.0",
1283        note = "Use `get_note_counts_by_denomination` instead"
1284    )]
1285    pub async fn get_notes_tier_counts(&self, dbtx: &mut DatabaseTransaction<'_>) -> TieredCounts {
1286        self.get_note_counts_by_denomination(dbtx).await
1287    }
1288
1289    /// Pick [`SpendableNote`]s by given counts, when available
1290    ///
1291    /// Return the notes picked, and counts of notes that were not available.
1292    pub async fn get_available_notes_by_tier_counts(
1293        &self,
1294        dbtx: &mut DatabaseTransaction<'_>,
1295        counts: TieredCounts,
1296    ) -> (TieredMulti<SpendableNoteUndecoded>, TieredCounts) {
1297        dbtx.find_by_prefix(&NoteKeyPrefix)
1298            .await
1299            .fold(
1300                (TieredMulti::<SpendableNoteUndecoded>::default(), counts),
1301                |(mut notes, mut counts), (key, note)| async move {
1302                    let amount = key.amount;
1303                    if 0 < counts.get(amount) {
1304                        counts.dec(amount);
1305                        notes.push(amount, note);
1306                    }
1307
1308                    (notes, counts)
1309                },
1310            )
1311            .await
1312    }
1313
1314    // TODO: put "notes per denomination" default into cfg
1315    /// Creates a mint output close to the given `amount`, issuing e-cash
1316    /// notes such that the client holds `notes_per_denomination` notes of each
1317    /// e-cash note denomination held.
1318    pub async fn create_output(
1319        &self,
1320        dbtx: &mut DatabaseTransaction<'_>,
1321        operation_id: OperationId,
1322        notes_per_denomination: u16,
1323        exact_amount: Amount,
1324    ) -> ClientOutputBundle<MintOutput, MintClientStateMachines> {
1325        if exact_amount == Amount::ZERO {
1326            return ClientOutputBundle::new(vec![], vec![]);
1327        }
1328
1329        let denominations = represent_amount(
1330            exact_amount,
1331            &self.get_note_counts_by_denomination(dbtx).await,
1332            &self.cfg.tbs_pks,
1333            notes_per_denomination,
1334            &self.cfg.fee_consensus,
1335        );
1336
1337        let mut outputs = Vec::new();
1338        let mut issuance_requests = Vec::new();
1339
1340        for (amount, num) in denominations.iter() {
1341            for _ in 0..num {
1342                let (issuance_request, blind_nonce) = self.new_ecash_note(amount, dbtx).await;
1343
1344                debug!(
1345                    %amount,
1346                    "Generated issuance request"
1347                );
1348
1349                outputs.push(ClientOutput {
1350                    output: MintOutput::new_v0(amount, blind_nonce),
1351                    amounts: Amounts::new_bitcoin(amount),
1352                });
1353
1354                issuance_requests.push((amount, issuance_request));
1355            }
1356        }
1357
1358        let state_generator = Arc::new(move |out_point_range: OutPointRange| {
1359            assert_eq!(out_point_range.count(), issuance_requests.len());
1360            vec![MintClientStateMachines::Output(MintOutputStateMachine {
1361                common: MintOutputCommon {
1362                    operation_id,
1363                    out_point_range,
1364                },
1365                state: MintOutputStates::CreatedMulti(MintOutputStatesCreatedMulti {
1366                    issuance_requests: out_point_range
1367                        .into_iter()
1368                        .map(|out_point| out_point.out_idx)
1369                        .zip(issuance_requests.clone())
1370                        .collect(),
1371                }),
1372            })]
1373        });
1374
1375        ClientOutputBundle::new(
1376            outputs,
1377            vec![ClientOutputSM {
1378                state_machines: state_generator,
1379            }],
1380        )
1381    }
1382
1383    /// Returns the number of held e-cash notes per denomination
1384    pub async fn get_note_counts_by_denomination(
1385        &self,
1386        dbtx: &mut DatabaseTransaction<'_>,
1387    ) -> TieredCounts {
1388        dbtx.find_by_prefix(&NoteKeyPrefix)
1389            .await
1390            .fold(
1391                TieredCounts::default(),
1392                |mut acc, (key, _note)| async move {
1393                    acc.inc(key.amount, 1);
1394                    acc
1395                },
1396            )
1397            .await
1398    }
1399
1400    /// Returns the number of held e-cash notes per denomination
1401    #[deprecated(
1402        since = "0.5.0",
1403        note = "Use `get_note_counts_by_denomination` instead"
1404    )]
1405    pub async fn get_wallet_summary(&self, dbtx: &mut DatabaseTransaction<'_>) -> TieredCounts {
1406        self.get_note_counts_by_denomination(dbtx).await
1407    }
1408
1409    /// Estimates the total fees to spend all currently held notes.
1410    ///
1411    /// This is useful for calculating max withdrawable amounts, where all
1412    /// notes will be spent. Notes that are uneconomical to spend (fee >= value)
1413    /// are excluded from the calculation since the wallet won't spend them.
1414    pub async fn estimate_spend_all_fees(&self) -> Amount {
1415        let mut dbtx = self.client_ctx.module_db().begin_transaction_nc().await;
1416        let note_counts = self.get_note_counts_by_denomination(&mut dbtx).await;
1417
1418        note_counts
1419            .iter()
1420            .filter_map(|(amount, count)| {
1421                let note_fee = self.cfg.fee_consensus.fee(amount);
1422                if note_fee < amount {
1423                    note_fee.checked_mul(count as u64)
1424                } else {
1425                    None
1426                }
1427            })
1428            .fold(Amount::ZERO, |acc, fee| {
1429                acc.checked_add(fee).expect("fee sum overflow")
1430            })
1431    }
1432
1433    /// Wait for the e-cash notes to be retrieved. If this is not possible
1434    /// because another terminal state was reached an error describing the
1435    /// failure is returned.
1436    pub async fn await_output_finalized(
1437        &self,
1438        operation_id: OperationId,
1439        out_point: OutPoint,
1440    ) -> anyhow::Result<()> {
1441        let stream = self
1442            .notifier
1443            .subscribe(operation_id)
1444            .await
1445            .filter_map(|state| async {
1446                let MintClientStateMachines::Output(state) = state else {
1447                    return None;
1448                };
1449
1450                if state.common.txid() != out_point.txid
1451                    || !state
1452                        .common
1453                        .out_point_range
1454                        .out_idx_iter()
1455                        .contains(&out_point.out_idx)
1456                {
1457                    return None;
1458                }
1459
1460                match state.state {
1461                    MintOutputStates::Succeeded(_) => Some(Ok(())),
1462                    MintOutputStates::Aborted(_) => Some(Err(anyhow!("Transaction was rejected"))),
1463                    MintOutputStates::Failed(failed) => Some(Err(anyhow!(
1464                        "Failed to finalize transaction: {}",
1465                        failed.error
1466                    ))),
1467                    MintOutputStates::Created(_) | MintOutputStates::CreatedMulti(_) => None,
1468                }
1469            });
1470        pin_mut!(stream);
1471
1472        stream.next_or_pending().await
1473    }
1474
1475    /// Provisional implementation of note consolidation
1476    ///
1477    /// When a certain denomination crosses the threshold of notes allowed,
1478    /// spend some chunk of them as inputs.
1479    ///
1480    /// Return notes and the sume of their amount.
1481    pub async fn consolidate_notes(
1482        &self,
1483        dbtx: &mut DatabaseTransaction<'_>,
1484    ) -> anyhow::Result<Vec<(ClientInput<MintInput>, SpendableNote)>> {
1485        /// At how many notes of the same denomination should we try to
1486        /// consolidate
1487        const MAX_NOTES_PER_TIER_TRIGGER: usize = 8;
1488        /// Number of notes per tier to leave after threshold was crossed
1489        const MIN_NOTES_PER_TIER: usize = 4;
1490        /// Maximum number of notes to consolidate per one tx,
1491        /// to limit the size of a transaction produced.
1492        const MAX_NOTES_TO_CONSOLIDATE_IN_TX: usize = 20;
1493        // it's fine, it's just documentation
1494        #[allow(clippy::assertions_on_constants)]
1495        {
1496            assert!(MIN_NOTES_PER_TIER <= MAX_NOTES_PER_TIER_TRIGGER);
1497        }
1498
1499        let counts = self.get_note_counts_by_denomination(dbtx).await;
1500
1501        let should_consolidate = counts
1502            .iter()
1503            .any(|(_, count)| MAX_NOTES_PER_TIER_TRIGGER < count);
1504
1505        if !should_consolidate {
1506            return Ok(vec![]);
1507        }
1508
1509        let mut max_count = MAX_NOTES_TO_CONSOLIDATE_IN_TX;
1510
1511        let excessive_counts: TieredCounts = counts
1512            .iter()
1513            .map(|(amount, count)| {
1514                let take = (count.saturating_sub(MIN_NOTES_PER_TIER)).min(max_count);
1515
1516                max_count -= take;
1517                (amount, take)
1518            })
1519            .collect();
1520
1521        let (selected_notes, unavailable) = self
1522            .get_available_notes_by_tier_counts(dbtx, excessive_counts)
1523            .await;
1524
1525        debug_assert!(
1526            unavailable.is_empty(),
1527            "Can't have unavailable notes on a subset of all notes: {unavailable:?}"
1528        );
1529
1530        if !selected_notes.is_empty() {
1531            debug!(target: LOG_CLIENT_MODULE_MINT, note_num=selected_notes.count_items(), denominations_msats=?selected_notes.iter_items().map(|(amount, _)| amount.msats).collect::<Vec<_>>(), "Will consolidate excessive notes");
1532        }
1533
1534        let mut selected_notes_decoded = vec![];
1535        for (amount, note) in selected_notes.iter_items() {
1536            let spendable_note_decoded = note.decode()?;
1537            debug!(target: LOG_CLIENT_MODULE_MINT, %amount, %note, "Consolidating note");
1538            Self::delete_spendable_note(&self.client_ctx, dbtx, amount, &spendable_note_decoded)
1539                .await;
1540            selected_notes_decoded.push((amount, spendable_note_decoded));
1541        }
1542
1543        let sender = self.balance_update_sender.clone();
1544        dbtx.on_commit(move || sender.send_replace(()));
1545
1546        self.create_input_from_notes(selected_notes_decoded.into_iter().collect())
1547    }
1548
1549    /// Create a mint input from external, potentially untrusted notes
1550    #[allow(clippy::type_complexity)]
1551    pub fn create_input_from_notes(
1552        &self,
1553        notes: TieredMulti<SpendableNote>,
1554    ) -> anyhow::Result<Vec<(ClientInput<MintInput>, SpendableNote)>> {
1555        let mut inputs_and_notes = Vec::new();
1556
1557        for (amount, spendable_note) in notes.into_iter_items() {
1558            let key = self
1559                .cfg
1560                .tbs_pks
1561                .get(amount)
1562                .ok_or(anyhow!("Invalid amount tier: {amount}"))?;
1563
1564            let note = spendable_note.note();
1565
1566            if !note.verify(*key) {
1567                bail!("Invalid note");
1568            }
1569
1570            inputs_and_notes.push((
1571                ClientInput {
1572                    input: MintInput::new_v0(amount, note),
1573                    keys: vec![spendable_note.spend_key],
1574                    amounts: Amounts::new_bitcoin(amount),
1575                },
1576                spendable_note,
1577            ));
1578        }
1579
1580        Ok(inputs_and_notes)
1581    }
1582
1583    async fn spend_notes_oob(
1584        &self,
1585        dbtx: &mut DatabaseTransaction<'_>,
1586        notes_selector: &impl NotesSelector,
1587        amount: Amount,
1588        try_cancel_after: Duration,
1589    ) -> anyhow::Result<(
1590        OperationId,
1591        Vec<MintClientStateMachines>,
1592        TieredMulti<SpendableNote>,
1593    )> {
1594        ensure!(
1595            amount > Amount::ZERO,
1596            "zero-amount out-of-band spends are not supported"
1597        );
1598
1599        let selected_notes =
1600            Self::select_notes(dbtx, notes_selector, amount, FeeConsensus::zero()).await?;
1601
1602        let operation_id = spendable_notes_to_operation_id(&selected_notes);
1603
1604        for (amount, note) in selected_notes.iter_items() {
1605            debug!(target: LOG_CLIENT_MODULE_MINT, %amount, %note, "Spending note as oob");
1606            MintClientModule::delete_spendable_note(&self.client_ctx, dbtx, amount, note).await;
1607        }
1608
1609        let sender = self.balance_update_sender.clone();
1610        dbtx.on_commit(move || sender.send_replace(()));
1611
1612        let state_machines = vec![MintClientStateMachines::OOB(MintOOBStateMachine {
1613            operation_id,
1614            state: MintOOBStates::CreatedMulti(MintOOBStatesCreatedMulti {
1615                spendable_notes: selected_notes.clone().into_iter_items().collect(),
1616                timeout: fedimint_core::time::now() + try_cancel_after,
1617            }),
1618        })];
1619
1620        Ok((operation_id, state_machines, selected_notes))
1621    }
1622
1623    pub async fn await_spend_oob_refund(&self, operation_id: OperationId) -> SpendOOBRefund {
1624        Box::pin(
1625            self.notifier
1626                .subscribe(operation_id)
1627                .await
1628                .filter_map(|state| async {
1629                    let MintClientStateMachines::OOB(state) = state else {
1630                        return None;
1631                    };
1632
1633                    match state.state {
1634                        MintOOBStates::TimeoutRefund(refund) => Some(SpendOOBRefund {
1635                            user_triggered: false,
1636                            transaction_ids: vec![refund.refund_txid],
1637                        }),
1638                        MintOOBStates::UserRefund(refund) => Some(SpendOOBRefund {
1639                            user_triggered: true,
1640                            transaction_ids: vec![refund.refund_txid],
1641                        }),
1642                        MintOOBStates::UserRefundMulti(refund) => Some(SpendOOBRefund {
1643                            user_triggered: true,
1644                            transaction_ids: vec![refund.refund_txid],
1645                        }),
1646                        MintOOBStates::Created(_) | MintOOBStates::CreatedMulti(_) => None,
1647                    }
1648                }),
1649        )
1650        .next_or_pending()
1651        .await
1652    }
1653
1654    /// Select notes with `requested_amount` using `notes_selector`.
1655    async fn select_notes(
1656        dbtx: &mut DatabaseTransaction<'_>,
1657        notes_selector: &impl NotesSelector,
1658        requested_amount: Amount,
1659        fee_consensus: FeeConsensus,
1660    ) -> anyhow::Result<TieredMulti<SpendableNote>> {
1661        let note_stream = dbtx
1662            .find_by_prefix_sorted_descending(&NoteKeyPrefix)
1663            .await
1664            .map(|(key, note)| (key.amount, note));
1665
1666        notes_selector
1667            .select_notes(note_stream, requested_amount, fee_consensus)
1668            .await?
1669            .into_iter_items()
1670            .map(|(amt, snote)| Ok((amt, snote.decode()?)))
1671            .collect::<anyhow::Result<TieredMulti<_>>>()
1672    }
1673
1674    async fn get_all_spendable_notes(
1675        dbtx: &mut DatabaseTransaction<'_>,
1676    ) -> TieredMulti<SpendableNoteUndecoded> {
1677        (dbtx
1678            .find_by_prefix(&NoteKeyPrefix)
1679            .await
1680            .map(|(key, note)| (key.amount, note))
1681            .collect::<Vec<_>>()
1682            .await)
1683            .into_iter()
1684            .collect()
1685    }
1686
1687    async fn get_next_note_index(
1688        &self,
1689        dbtx: &mut DatabaseTransaction<'_>,
1690        amount: Amount,
1691    ) -> NoteIndex {
1692        NoteIndex(
1693            dbtx.get_value(&NextECashNoteIndexKey(amount))
1694                .await
1695                .unwrap_or(0),
1696        )
1697    }
1698
1699    /// Derive the note `DerivableSecret` from the Mint's `secret` the `amount`
1700    /// tier and `note_idx`
1701    ///
1702    /// Static to help re-use in other places, that don't have a whole [`Self`]
1703    /// available
1704    ///
1705    /// # E-Cash Note Creation
1706    ///
1707    /// When creating an e-cash note, the `MintClientModule` first derives the
1708    /// blinding and spend keys from the `DerivableSecret`. It then creates a
1709    /// `NoteIssuanceRequest` containing the blinded spend key and sends it to
1710    /// the mint server. The mint server signs the blinded spend key and
1711    /// returns it to the client. The client can then unblind the signed
1712    /// spend key to obtain the e-cash note, which can be spent using the
1713    /// spend key.
1714    pub fn new_note_secret_static(
1715        secret: &DerivableSecret,
1716        amount: Amount,
1717        note_idx: NoteIndex,
1718    ) -> DerivableSecret {
1719        assert_eq!(secret.level(), 2);
1720        debug!(?secret, %amount, %note_idx, "Deriving new mint note");
1721        secret
1722            .child_key(MINT_E_CASH_TYPE_CHILD_ID) // TODO: cache
1723            .child_key(ChildId(note_idx.as_u64()))
1724            .child_key(ChildId(amount.msats))
1725    }
1726
1727    /// We always keep track of an incrementing index in the database and use
1728    /// it as part of the derivation path for the note secret. This ensures that
1729    /// we never reuse the same note secret twice.
1730    async fn new_note_secret(
1731        &self,
1732        amount: Amount,
1733        dbtx: &mut DatabaseTransaction<'_>,
1734    ) -> DerivableSecret {
1735        let new_idx = self.get_next_note_index(dbtx, amount).await;
1736        dbtx.insert_entry(&NextECashNoteIndexKey(amount), &new_idx.next().as_u64())
1737            .await;
1738        Self::new_note_secret_static(&self.secret, amount, new_idx)
1739    }
1740
1741    pub async fn new_ecash_note(
1742        &self,
1743        amount: Amount,
1744        dbtx: &mut DatabaseTransaction<'_>,
1745    ) -> (NoteIssuanceRequest, BlindNonce) {
1746        let secret = self.new_note_secret(amount, dbtx).await;
1747        NoteIssuanceRequest::new(&self.secp, &secret)
1748    }
1749
1750    /// Try to reissue e-cash notes received from a third party to receive them
1751    /// in our wallet. The progress and outcome can be observed using
1752    /// [`MintClientModule::subscribe_reissue_external_notes`].
1753    /// Can return error of type [`ReissueExternalNotesError`]
1754    pub async fn reissue_external_notes<M: Serialize + Send>(
1755        &self,
1756        oob_notes: OOBNotes,
1757        extra_meta: M,
1758    ) -> anyhow::Result<OperationId> {
1759        let notes = oob_notes.notes().clone();
1760        let federation_id_prefix = oob_notes.federation_id_prefix();
1761
1762        ensure!(
1763            notes.total_amount() > Amount::ZERO,
1764            "Reissuing zero-amount e-cash isn't supported"
1765        );
1766
1767        if federation_id_prefix != self.federation_id.to_prefix() {
1768            bail!(ReissueExternalNotesError::WrongFederationId);
1769        }
1770
1771        let operation_id = OperationId(
1772            notes
1773                .consensus_hash::<sha256t::Hash<OOBReissueTag>>()
1774                .to_byte_array(),
1775        );
1776
1777        let amount = notes.total_amount();
1778        let mint_inputs = self.create_input_from_notes(notes)?;
1779
1780        let tx = TransactionBuilder::new().with_inputs(
1781            self.client_ctx
1782                .make_dyn(create_bundle_for_inputs(mint_inputs, operation_id)),
1783        );
1784
1785        let extra_meta = serde_json::to_value(extra_meta)
1786            .expect("MintClientModule::reissue_external_notes extra_meta is serializable");
1787        let operation_meta_gen = move |change_range: OutPointRange| MintOperationMeta {
1788            variant: MintOperationMetaVariant::Reissuance {
1789                legacy_out_point: None,
1790                txid: Some(change_range.txid()),
1791                out_point_indices: change_range
1792                    .into_iter()
1793                    .map(|out_point| out_point.out_idx)
1794                    .collect(),
1795            },
1796            amount,
1797            extra_meta: extra_meta.clone(),
1798        };
1799
1800        self.client_ctx
1801            .finalize_and_submit_transaction(
1802                operation_id,
1803                MintCommonInit::KIND.as_str(),
1804                operation_meta_gen,
1805                tx,
1806            )
1807            .await
1808            .context(ReissueExternalNotesError::AlreadyReissued)?;
1809
1810        let mut dbtx = self.client_ctx.module_db().begin_transaction().await;
1811
1812        self.client_ctx
1813            .log_event(&mut dbtx, OOBNotesReissued { amount })
1814            .await;
1815
1816        self.client_ctx
1817            .log_event(
1818                &mut dbtx,
1819                ReceivePaymentEvent {
1820                    operation_id,
1821                    amount,
1822                },
1823            )
1824            .await;
1825
1826        dbtx.commit_tx().await;
1827
1828        Ok(operation_id)
1829    }
1830
1831    /// Subscribe to updates on the progress of a reissue operation started with
1832    /// [`MintClientModule::reissue_external_notes`].
1833    pub async fn subscribe_reissue_external_notes(
1834        &self,
1835        operation_id: OperationId,
1836    ) -> anyhow::Result<UpdateStreamOrOutcome<ReissueExternalNotesState>> {
1837        let operation = self.mint_operation(operation_id).await?;
1838        let (txid, out_points) = match operation.meta::<MintOperationMeta>().variant {
1839            MintOperationMetaVariant::Reissuance {
1840                legacy_out_point,
1841                txid,
1842                out_point_indices,
1843            } => {
1844                // Either txid or legacy_out_point will be present, so we should always
1845                // have a source for the txid
1846                let txid = txid
1847                    .or(legacy_out_point.map(|out_point| out_point.txid))
1848                    .context("Empty reissuance not permitted, this should never happen")?;
1849
1850                let out_points = out_point_indices
1851                    .into_iter()
1852                    .map(|out_idx| OutPoint { txid, out_idx })
1853                    .chain(legacy_out_point)
1854                    .collect::<Vec<_>>();
1855
1856                (txid, out_points)
1857            }
1858            MintOperationMetaVariant::SpendOOB { .. } => bail!("Operation is not a reissuance"),
1859        };
1860
1861        let client_ctx = self.client_ctx.clone();
1862
1863        Ok(self.client_ctx.outcome_or_updates(operation, operation_id, move || {
1864            stream! {
1865                yield ReissueExternalNotesState::Created;
1866
1867                match client_ctx
1868                    .transaction_updates(operation_id)
1869                    .await
1870                    .await_tx_accepted(txid)
1871                    .await
1872                {
1873                    Ok(()) => {
1874                        yield ReissueExternalNotesState::Issuing;
1875                    }
1876                    Err(e) => {
1877                        yield ReissueExternalNotesState::Failed(format!("Transaction not accepted {e:?}"));
1878                        return;
1879                    }
1880                }
1881
1882                for out_point in out_points {
1883                    if let Err(e) = client_ctx.self_ref().await_output_finalized(operation_id, out_point).await {
1884                        yield ReissueExternalNotesState::Failed(e.to_string());
1885                        return;
1886                    }
1887                }
1888                yield ReissueExternalNotesState::Done;
1889            }}
1890        ))
1891    }
1892
1893    /// Fetches and removes notes of *at least* amount `min_amount` from the
1894    /// wallet to be sent to the recipient out of band. These spends can be
1895    /// canceled by calling [`MintClientModule::try_cancel_spend_notes`] as long
1896    /// as the recipient hasn't reissued the e-cash notes themselves yet.
1897    ///
1898    /// The client will also automatically attempt to cancel the operation after
1899    /// `try_cancel_after` time has passed. This is a safety mechanism to avoid
1900    /// users forgetting about failed out-of-band transactions. The timeout
1901    /// should be chosen such that the recipient (who is potentially offline at
1902    /// the time of receiving the e-cash notes) had a reasonable timeframe to
1903    /// come online and reissue the notes themselves.
1904    #[deprecated(
1905        since = "0.5.0",
1906        note = "Use `spend_notes_with_selector` instead, with `SelectNotesWithAtleastAmount` to maintain the same behavior"
1907    )]
1908    pub async fn spend_notes<M: Serialize + Send>(
1909        &self,
1910        min_amount: Amount,
1911        try_cancel_after: Duration,
1912        include_invite: bool,
1913        extra_meta: M,
1914    ) -> anyhow::Result<(OperationId, OOBNotes)> {
1915        self.spend_notes_with_selector(
1916            &SelectNotesWithAtleastAmount,
1917            min_amount,
1918            try_cancel_after,
1919            include_invite,
1920            extra_meta,
1921        )
1922        .await
1923    }
1924
1925    /// Fetches and removes notes from the wallet to be sent to the recipient
1926    /// out of band. The note selection algorithm is determined by
1927    /// `note_selector`. See the [`NotesSelector`] trait for available
1928    /// implementations.
1929    ///
1930    /// These spends can be canceled by calling
1931    /// [`MintClientModule::try_cancel_spend_notes`] as long
1932    /// as the recipient hasn't reissued the e-cash notes themselves yet.
1933    ///
1934    /// The client will also automatically attempt to cancel the operation after
1935    /// `try_cancel_after` time has passed. This is a safety mechanism to avoid
1936    /// users forgetting about failed out-of-band transactions. The timeout
1937    /// should be chosen such that the recipient (who is potentially offline at
1938    /// the time of receiving the e-cash notes) had a reasonable timeframe to
1939    /// come online and reissue the notes themselves.
1940    pub async fn spend_notes_with_selector<M: Serialize + Send>(
1941        &self,
1942        notes_selector: &impl NotesSelector,
1943        requested_amount: Amount,
1944        try_cancel_after: Duration,
1945        include_invite: bool,
1946        extra_meta: M,
1947    ) -> anyhow::Result<(OperationId, OOBNotes)> {
1948        let federation_id_prefix = self.federation_id.to_prefix();
1949        let extra_meta = serde_json::to_value(extra_meta)
1950            .expect("MintClientModule::spend_notes extra_meta is serializable");
1951
1952        self.client_ctx
1953            .module_db()
1954            .autocommit(
1955                |dbtx, _| {
1956                    let extra_meta = extra_meta.clone();
1957                    Box::pin(async {
1958                        let (operation_id, states, notes) = self
1959                            .spend_notes_oob(
1960                                dbtx,
1961                                notes_selector,
1962                                requested_amount,
1963                                try_cancel_after,
1964                            )
1965                            .await?;
1966
1967                        let oob_notes = if include_invite {
1968                            OOBNotes::new_with_invite(
1969                                notes,
1970                                &self.client_ctx.get_invite_code().await,
1971                            )
1972                        } else {
1973                            OOBNotes::new(federation_id_prefix, notes)
1974                        };
1975
1976                        self.client_ctx
1977                            .add_state_machines_dbtx(
1978                                dbtx,
1979                                self.client_ctx.map_dyn(states).collect(),
1980                            )
1981                            .await?;
1982                        self.client_ctx
1983                            .add_operation_log_entry_dbtx(
1984                                dbtx,
1985                                operation_id,
1986                                MintCommonInit::KIND.as_str(),
1987                                MintOperationMeta {
1988                                    variant: MintOperationMetaVariant::SpendOOB {
1989                                        requested_amount,
1990                                        oob_notes: oob_notes.clone(),
1991                                    },
1992                                    amount: oob_notes.total_amount(),
1993                                    extra_meta,
1994                                },
1995                            )
1996                            .await;
1997                        self.client_ctx
1998                            .log_event(
1999                                dbtx,
2000                                OOBNotesSpent {
2001                                    requested_amount,
2002                                    spent_amount: oob_notes.total_amount(),
2003                                    timeout: try_cancel_after,
2004                                    include_invite,
2005                                },
2006                            )
2007                            .await;
2008
2009                        self.client_ctx
2010                            .log_event(
2011                                dbtx,
2012                                SendPaymentEvent {
2013                                    operation_id,
2014                                    amount: oob_notes.total_amount(),
2015                                    oob_notes: encode_prefixed(FEDIMINT_PREFIX, &oob_notes),
2016                                },
2017                            )
2018                            .await;
2019
2020                        Ok((operation_id, oob_notes))
2021                    })
2022                },
2023                Some(100),
2024            )
2025            .await
2026            .map_err(|e| match e {
2027                AutocommitError::ClosureError { error, .. } => error,
2028                AutocommitError::CommitFailed { last_error, .. } => {
2029                    anyhow!("Commit to DB failed: {last_error}")
2030                }
2031            })
2032    }
2033
2034    /// Send e-cash notes for the requested amount.
2035    ///
2036    /// When this method removes ecash notes from the local database it will do
2037    /// so atomically with creating a `SendPaymentEvent` that contains the notes
2038    /// in out of band serilaized from. Hence it is critical for the integrator
2039    /// to display this event to ensure the user always has access to his funds.
2040    ///
2041    /// This method operates in two modes:
2042    ///
2043    /// 1. **Offline mode**: If exact notes are available in the wallet, they
2044    ///    are spent immediately without contacting the federation. A
2045    ///    `SendPaymentEvent` is emitted and the notes are returned.
2046    ///
2047    /// 2. **Online mode**: If exact notes are not available, the method
2048    ///    contacts the federation to trigger a reissuance transaction to obtain
2049    ///    the proper denominations. The method will block until the reissuance
2050    ///    completes, at which point a `SendPaymentEvent` is emitted and the
2051    ///    notes are returned.
2052    ///
2053    /// If the method enters online mode and is cancelled, e.g. the future is
2054    /// dropped, before the reissue transaction is confirmed, any reissued notes
2055    /// will be returned to the wallet and we do not emit a `SendPaymentEvent`.
2056    ///
2057    /// Before selection of the ecash notes the amount is rounded up to the
2058    /// nearest multiple of 512 msat.
2059    pub async fn send_oob_notes<M: Serialize + Send>(
2060        &self,
2061        amount: Amount,
2062        extra_meta: M,
2063    ) -> anyhow::Result<OOBNotes> {
2064        // Round up to the nearest multiple of 512 msat
2065        let amount = Amount::from_msats(amount.msats.div_ceil(512) * 512);
2066
2067        let extra_meta = serde_json::to_value(extra_meta)
2068            .expect("MintClientModule::send_oob_notes extra_meta is serializable");
2069
2070        // Try to spend exact notes from our current balance
2071        let oob_notes: Option<OOBNotes> = self
2072            .client_ctx
2073            .module_db()
2074            .autocommit(
2075                |dbtx, _| {
2076                    let extra_meta = extra_meta.clone();
2077                    Box::pin(async {
2078                        self.try_spend_exact_notes_dbtx(
2079                            dbtx,
2080                            amount,
2081                            self.federation_id,
2082                            extra_meta,
2083                        )
2084                        .await
2085                        .map(Ok::<OOBNotes, anyhow::Error>)
2086                        .transpose()
2087                    })
2088                },
2089                Some(100),
2090            )
2091            .await
2092            .expect("Failed to commit dbtx after 100 retries");
2093
2094        if let Some(oob_notes) = oob_notes {
2095            return Ok(oob_notes);
2096        }
2097
2098        // Verify we're online
2099        self.client_ctx
2100            .global_api()
2101            .session_count()
2102            .await
2103            .context("Cannot reach federation to reissue notes")?;
2104
2105        let operation_id = OperationId::new_random();
2106
2107        // Create outputs for reissuance using the existing create_output function
2108        let output_bundle = self
2109            .create_output(
2110                &mut self.client_ctx.module_db().begin_transaction_nc().await,
2111                operation_id,
2112                1, // notes_per_denomination
2113                amount,
2114            )
2115            .await;
2116
2117        // Combine the output bundle state machines with the send state machine
2118        let combined_bundle = ClientOutputBundle::new(
2119            output_bundle.outputs().to_vec(),
2120            output_bundle.sms().to_vec(),
2121        );
2122
2123        let outputs = self.client_ctx.make_client_outputs(combined_bundle);
2124
2125        let em_clone = extra_meta.clone();
2126
2127        // Submit reissuance transaction with the state machines
2128        let out_point_range = self
2129            .client_ctx
2130            .finalize_and_submit_transaction(
2131                operation_id,
2132                MintCommonInit::KIND.as_str(),
2133                move |change_range: OutPointRange| MintOperationMeta {
2134                    variant: MintOperationMetaVariant::Reissuance {
2135                        legacy_out_point: None,
2136                        txid: Some(change_range.txid()),
2137                        out_point_indices: change_range
2138                            .into_iter()
2139                            .map(|out_point| out_point.out_idx)
2140                            .collect(),
2141                    },
2142                    amount,
2143                    extra_meta: em_clone.clone(),
2144                },
2145                TransactionBuilder::new().with_outputs(outputs),
2146            )
2147            .await
2148            .context("Failed to submit reissuance transaction")?;
2149
2150        // Wait for outputs to be finalized
2151        self.client_ctx
2152            .await_primary_module_outputs(operation_id, out_point_range.into_iter().collect())
2153            .await
2154            .context("Failed to await output finalization")?;
2155
2156        // Recursively call send_oob_notes to try again with the reissued notes
2157        Box::pin(self.send_oob_notes(amount, extra_meta)).await
2158    }
2159
2160    /// Try to spend exact notes from the current balance.
2161    /// Returns `Some(OOBNotes)` if exact notes are available, `None` otherwise.
2162    async fn try_spend_exact_notes_dbtx(
2163        &self,
2164        dbtx: &mut DatabaseTransaction<'_>,
2165        amount: Amount,
2166        federation_id: FederationId,
2167        extra_meta: serde_json::Value,
2168    ) -> Option<OOBNotes> {
2169        let selected_notes = Self::select_notes(
2170            dbtx,
2171            &SelectNotesWithExactAmount,
2172            amount,
2173            FeeConsensus::zero(),
2174        )
2175        .await
2176        .ok()?;
2177
2178        // Remove notes from our database
2179        for (note_amount, note) in selected_notes.iter_items() {
2180            MintClientModule::delete_spendable_note(&self.client_ctx, dbtx, note_amount, note)
2181                .await;
2182        }
2183
2184        let sender = self.balance_update_sender.clone();
2185        dbtx.on_commit(move || sender.send_replace(()));
2186
2187        let operation_id = spendable_notes_to_operation_id(&selected_notes);
2188
2189        let oob_notes = OOBNotes::new(federation_id.to_prefix(), selected_notes);
2190
2191        // Log the send operation with notes immediately available
2192        self.client_ctx
2193            .add_operation_log_entry_dbtx(
2194                dbtx,
2195                operation_id,
2196                MintCommonInit::KIND.as_str(),
2197                MintOperationMeta {
2198                    variant: MintOperationMetaVariant::SpendOOB {
2199                        requested_amount: amount,
2200                        oob_notes: oob_notes.clone(),
2201                    },
2202                    amount: oob_notes.total_amount(),
2203                    extra_meta,
2204                },
2205            )
2206            .await;
2207
2208        self.client_ctx
2209            .log_event(
2210                dbtx,
2211                SendPaymentEvent {
2212                    operation_id,
2213                    amount: oob_notes.total_amount(),
2214                    oob_notes: encode_prefixed(FEDIMINT_PREFIX, &oob_notes),
2215                },
2216            )
2217            .await;
2218
2219        Some(oob_notes)
2220    }
2221
2222    /// Validate the given notes and return the total amount of the notes.
2223    /// Validation checks that:
2224    /// - the federation ID is correct
2225    /// - the note has a valid signature
2226    /// - the spend key is correct.
2227    pub fn validate_notes(&self, oob_notes: &OOBNotes) -> anyhow::Result<Amount> {
2228        let federation_id_prefix = oob_notes.federation_id_prefix();
2229        let notes = oob_notes.notes().clone();
2230
2231        if federation_id_prefix != self.federation_id.to_prefix() {
2232            bail!("Federation ID does not match");
2233        }
2234
2235        let tbs_pks = &self.cfg.tbs_pks;
2236
2237        for (idx, (amt, snote)) in notes.iter_items().enumerate() {
2238            let key = tbs_pks
2239                .get(amt)
2240                .ok_or_else(|| anyhow!("Note {idx} uses an invalid amount tier {amt}"))?;
2241
2242            let note = snote.note();
2243            if !note.verify(*key) {
2244                bail!("Note {idx} has an invalid federation signature");
2245            }
2246
2247            let expected_nonce = Nonce(snote.spend_key.public_key());
2248            if note.nonce != expected_nonce {
2249                bail!("Note {idx} cannot be spent using the supplied spend key");
2250            }
2251        }
2252
2253        Ok(notes.total_amount())
2254    }
2255
2256    /// Contacts the mint and checks if the supplied notes were already spent.
2257    ///
2258    /// **Caution:** This reduces privacy and can lead to race conditions. **DO
2259    /// NOT** rely on it for receiving funds unless you really know what you are
2260    /// doing.
2261    pub async fn check_note_spent(&self, oob_notes: &OOBNotes) -> anyhow::Result<bool> {
2262        use crate::api::MintFederationApi;
2263
2264        let api_client = self.client_ctx.module_api();
2265        let any_spent = try_join_all(oob_notes.notes().iter().flat_map(|(_, notes)| {
2266            notes
2267                .iter()
2268                .map(|note| api_client.check_note_spent(note.nonce()))
2269        }))
2270        .await?
2271        .into_iter()
2272        .any(|spent| spent);
2273
2274        Ok(any_spent)
2275    }
2276
2277    /// Try to cancel a spend operation started with
2278    /// [`MintClientModule::spend_notes_with_selector`]. If the e-cash notes
2279    /// have already been spent this operation will fail which can be
2280    /// observed using [`MintClientModule::subscribe_spend_notes`].
2281    pub async fn try_cancel_spend_notes(&self, operation_id: OperationId) {
2282        let mut dbtx = self.client_ctx.module_db().begin_transaction().await;
2283        dbtx.insert_entry(&CancelledOOBSpendKey(operation_id), &())
2284            .await;
2285        if let Err(e) = dbtx.commit_tx_result().await {
2286            warn!("We tried to cancel the same OOB spend multiple times concurrently: {e}");
2287        }
2288    }
2289
2290    /// Subscribe to updates on the progress of a raw e-cash spend operation
2291    /// started with [`MintClientModule::spend_notes_with_selector`].
2292    pub async fn subscribe_spend_notes(
2293        &self,
2294        operation_id: OperationId,
2295    ) -> anyhow::Result<UpdateStreamOrOutcome<SpendOOBState>> {
2296        let operation = self.mint_operation(operation_id).await?;
2297        if !matches!(
2298            operation.meta::<MintOperationMeta>().variant,
2299            MintOperationMetaVariant::SpendOOB { .. }
2300        ) {
2301            bail!("Operation is not a out-of-band spend");
2302        }
2303
2304        let client_ctx = self.client_ctx.clone();
2305
2306        Ok(self
2307            .client_ctx
2308            .outcome_or_updates(operation, operation_id, move || {
2309                stream! {
2310                    yield SpendOOBState::Created;
2311
2312                    let self_ref = client_ctx.self_ref();
2313
2314                    let refund = self_ref
2315                        .await_spend_oob_refund(operation_id)
2316                        .await;
2317
2318                    if refund.user_triggered {
2319                        yield SpendOOBState::UserCanceledProcessing;
2320                    }
2321
2322                    let mut success = true;
2323
2324                    for txid in refund.transaction_ids {
2325                        debug!(
2326                            target: LOG_CLIENT_MODULE_MINT,
2327                            %txid,
2328                            operation_id=%operation_id.fmt_short(),
2329                            "Waiting for oob refund txid"
2330                        );
2331                        if client_ctx
2332                            .transaction_updates(operation_id)
2333                            .await
2334                            .await_tx_accepted(txid)
2335                            .await.is_err() {
2336                                success = false;
2337                            }
2338                    }
2339
2340                    debug!(
2341                        target: LOG_CLIENT_MODULE_MINT,
2342                        operation_id=%operation_id.fmt_short(),
2343                        %success,
2344                        "Done waiting for all refund oob txids"
2345                     );
2346
2347                    match (refund.user_triggered, success) {
2348                        (true, true) => {
2349                            yield SpendOOBState::UserCanceledSuccess;
2350                        },
2351                        (true, false) => {
2352                            yield SpendOOBState::UserCanceledFailure;
2353                        },
2354                        (false, true) => {
2355                            yield SpendOOBState::Refunded;
2356                        },
2357                        (false, false) => {
2358                            yield SpendOOBState::Success;
2359                        }
2360                    }
2361                }
2362            }))
2363    }
2364
2365    async fn mint_operation(&self, operation_id: OperationId) -> anyhow::Result<OperationLogEntry> {
2366        let operation = self.client_ctx.get_operation(operation_id).await?;
2367
2368        if operation.operation_module_kind() != MintCommonInit::KIND.as_str() {
2369            bail!("Operation is not a mint operation");
2370        }
2371
2372        Ok(operation)
2373    }
2374
2375    async fn delete_spendable_note(
2376        client_ctx: &ClientContext<MintClientModule>,
2377        dbtx: &mut DatabaseTransaction<'_>,
2378        amount: Amount,
2379        note: &SpendableNote,
2380    ) {
2381        client_ctx
2382            .log_event(
2383                dbtx,
2384                NoteSpent {
2385                    nonce: note.nonce(),
2386                },
2387            )
2388            .await;
2389        dbtx.remove_entry(&NoteKey {
2390            amount,
2391            nonce: note.nonce(),
2392        })
2393        .await
2394        .expect("Must deleted existing spendable note");
2395    }
2396
2397    pub async fn advance_note_idx(&self, amount: Amount) -> anyhow::Result<DerivableSecret> {
2398        let db = self.client_ctx.module_db().clone();
2399
2400        Ok(db
2401            .autocommit(
2402                |dbtx, _| {
2403                    Box::pin(async {
2404                        Ok::<DerivableSecret, anyhow::Error>(
2405                            self.new_note_secret(amount, dbtx).await,
2406                        )
2407                    })
2408                },
2409                None,
2410            )
2411            .await?)
2412    }
2413
2414    /// Returns secrets for the note indices that were reused by previous
2415    /// clients with same client secret.
2416    pub async fn reused_note_secrets(&self) -> Vec<(Amount, NoteIssuanceRequest, BlindNonce)> {
2417        self.client_ctx
2418            .module_db()
2419            .begin_transaction_nc()
2420            .await
2421            .get_value(&ReusedNoteIndices)
2422            .await
2423            .unwrap_or_default()
2424            .into_iter()
2425            .map(|(amount, note_idx)| {
2426                let secret = Self::new_note_secret_static(&self.secret, amount, note_idx);
2427                let (request, blind_nonce) =
2428                    NoteIssuanceRequest::new(fedimint_core::secp256k1::SECP256K1, &secret);
2429                (amount, request, blind_nonce)
2430            })
2431            .collect()
2432    }
2433}
2434
2435pub fn spendable_notes_to_operation_id(
2436    spendable_selected_notes: &TieredMulti<SpendableNote>,
2437) -> OperationId {
2438    OperationId(
2439        spendable_selected_notes
2440            .consensus_hash::<sha256t::Hash<OOBSpendTag>>()
2441            .to_byte_array(),
2442    )
2443}
2444
2445#[derive(Debug, Serialize, Deserialize, Clone)]
2446pub struct SpendOOBRefund {
2447    pub user_triggered: bool,
2448    pub transaction_ids: Vec<TransactionId>,
2449}
2450
2451/// Defines a strategy for selecting e-cash notes given a specific target amount
2452/// and fee per note transaction input.
2453#[apply(async_trait_maybe_send!)]
2454pub trait NotesSelector<Note = SpendableNoteUndecoded>: Send + Sync {
2455    /// Select notes from stream for `requested_amount`.
2456    /// The stream must produce items in non- decreasing order of amount.
2457    async fn select_notes(
2458        &self,
2459        // FIXME: async trait doesn't like maybe_add_send
2460        #[cfg(not(target_family = "wasm"))] stream: impl futures::Stream<Item = (Amount, Note)> + Send,
2461        #[cfg(target_family = "wasm")] stream: impl futures::Stream<Item = (Amount, Note)>,
2462        requested_amount: Amount,
2463        fee_consensus: FeeConsensus,
2464    ) -> anyhow::Result<TieredMulti<Note>>;
2465}
2466
2467/// Select notes with total amount of *at least* `request_amount`. If more than
2468/// requested amount of notes are returned it was because exact change couldn't
2469/// be made, and the next smallest amount will be returned.
2470///
2471/// The caller can request change from the federation.
2472pub struct SelectNotesWithAtleastAmount;
2473
2474#[apply(async_trait_maybe_send!)]
2475impl<Note: Send> NotesSelector<Note> for SelectNotesWithAtleastAmount {
2476    async fn select_notes(
2477        &self,
2478        #[cfg(not(target_family = "wasm"))] stream: impl futures::Stream<Item = (Amount, Note)> + Send,
2479        #[cfg(target_family = "wasm")] stream: impl futures::Stream<Item = (Amount, Note)>,
2480        requested_amount: Amount,
2481        fee_consensus: FeeConsensus,
2482    ) -> anyhow::Result<TieredMulti<Note>> {
2483        Ok(select_notes_from_stream(stream, requested_amount, fee_consensus).await?)
2484    }
2485}
2486
2487/// Select notes with total amount of *exactly* `request_amount`. If the amount
2488/// cannot be represented with the available denominations an error is returned,
2489/// this **does not** mean that the balance is too low.
2490pub struct SelectNotesWithExactAmount;
2491
2492#[apply(async_trait_maybe_send!)]
2493impl<Note: Send> NotesSelector<Note> for SelectNotesWithExactAmount {
2494    async fn select_notes(
2495        &self,
2496        #[cfg(not(target_family = "wasm"))] stream: impl futures::Stream<Item = (Amount, Note)> + Send,
2497        #[cfg(target_family = "wasm")] stream: impl futures::Stream<Item = (Amount, Note)>,
2498        requested_amount: Amount,
2499        fee_consensus: FeeConsensus,
2500    ) -> anyhow::Result<TieredMulti<Note>> {
2501        let notes = select_notes_from_stream(stream, requested_amount, fee_consensus).await?;
2502
2503        if notes.total_amount() != requested_amount {
2504            bail!(
2505                "Could not select notes with exact amount. Requested amount: {}. Selected amount: {}",
2506                requested_amount,
2507                notes.total_amount()
2508            );
2509        }
2510
2511        Ok(notes)
2512    }
2513}
2514
2515// We are using a greedy algorithm to select notes. We start with the largest
2516// then proceed to the lowest tiers/denominations.
2517// But there is a catch: we don't know if there are enough notes in the lowest
2518// tiers, so we need to save a big note in case the sum of the following
2519// small notes are not enough.
2520async fn select_notes_from_stream<Note>(
2521    stream: impl futures::Stream<Item = (Amount, Note)>,
2522    requested_amount: Amount,
2523    fee_consensus: FeeConsensus,
2524) -> Result<TieredMulti<Note>, InsufficientBalanceError> {
2525    if requested_amount == Amount::ZERO {
2526        return Ok(TieredMulti::default());
2527    }
2528    let mut stream = Box::pin(stream);
2529    let mut selected = vec![];
2530    // This is the big note we save in case the sum of the following small notes are
2531    // not sufficient to cover the pending amount
2532    // The tuple is (amount, note, checkpoint), where checkpoint is the index where
2533    // the note should be inserted on the selected vector if it is needed
2534    let mut last_big_note_checkpoint: Option<(Amount, Note, usize)> = None;
2535    let mut pending_amount = requested_amount;
2536    let mut previous_amount: Option<Amount> = None; // used to assert descending order
2537    loop {
2538        if let Some((note_amount, note)) = stream.next().await {
2539            assert!(
2540                previous_amount.is_none_or(|previous| previous >= note_amount),
2541                "notes are not sorted in descending order"
2542            );
2543            previous_amount = Some(note_amount);
2544
2545            if note_amount <= fee_consensus.fee(note_amount) {
2546                continue;
2547            }
2548
2549            match note_amount.cmp(&(pending_amount + fee_consensus.fee(note_amount))) {
2550                Ordering::Less => {
2551                    // keep adding notes until we have enough
2552                    pending_amount += fee_consensus.fee(note_amount);
2553                    pending_amount -= note_amount;
2554                    selected.push((note_amount, note));
2555                }
2556                Ordering::Greater => {
2557                    // probably we don't need this big note, but we'll keep it in case the
2558                    // following small notes don't add up to the
2559                    // requested amount
2560                    last_big_note_checkpoint = Some((note_amount, note, selected.len()));
2561                }
2562                Ordering::Equal => {
2563                    // exactly enough notes, return
2564                    selected.push((note_amount, note));
2565
2566                    let notes: TieredMulti<Note> = selected.into_iter().collect();
2567
2568                    assert!(
2569                        notes.total_amount().msats
2570                            >= requested_amount.msats
2571                                + notes
2572                                    .iter()
2573                                    .map(|note| fee_consensus.fee(note.0))
2574                                    .sum::<Amount>()
2575                                    .msats
2576                    );
2577
2578                    return Ok(notes);
2579                }
2580            }
2581        } else {
2582            assert!(pending_amount > Amount::ZERO);
2583            if let Some((big_note_amount, big_note, checkpoint)) = last_big_note_checkpoint {
2584                // the sum of the small notes don't add up to the pending amount, remove
2585                // them
2586                selected.truncate(checkpoint);
2587                // and use the big note to cover it
2588                selected.push((big_note_amount, big_note));
2589
2590                let notes: TieredMulti<Note> = selected.into_iter().collect();
2591
2592                assert!(
2593                    notes.total_amount().msats
2594                        >= requested_amount.msats
2595                            + notes
2596                                .iter()
2597                                .map(|note| fee_consensus.fee(note.0))
2598                                .sum::<Amount>()
2599                                .msats
2600                );
2601
2602                // so now we have enough to cover the requested amount, return
2603                return Ok(notes);
2604            }
2605
2606            let total_amount = requested_amount.saturating_sub(pending_amount);
2607            // not enough notes, return
2608            return Err(InsufficientBalanceError {
2609                requested_amount,
2610                total_amount,
2611            });
2612        }
2613    }
2614}
2615
2616#[derive(Debug, Clone, Error)]
2617pub struct InsufficientBalanceError {
2618    pub requested_amount: Amount,
2619    pub total_amount: Amount,
2620}
2621
2622impl std::fmt::Display for InsufficientBalanceError {
2623    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
2624        write!(
2625            f,
2626            "Insufficient balance: requested {} but only {} available",
2627            self.requested_amount, self.total_amount
2628        )
2629    }
2630}
2631
2632/// Old and no longer used, will be deleted in the future
2633#[derive(Debug, Clone, Eq, PartialEq, Hash, Decodable, Encodable)]
2634enum MintRestoreStates {
2635    #[encodable_default]
2636    Default { variant: u64, bytes: Vec<u8> },
2637}
2638
2639/// Old and no longer used, will be deleted in the future
2640#[derive(Debug, Clone, Eq, PartialEq, Hash, Decodable, Encodable)]
2641pub struct MintRestoreStateMachine {
2642    operation_id: OperationId,
2643    state: MintRestoreStates,
2644}
2645
2646#[derive(Debug, Clone, Eq, PartialEq, Hash, Decodable, Encodable)]
2647pub enum MintClientStateMachines {
2648    Output(MintOutputStateMachine),
2649    Input(MintInputStateMachine),
2650    OOB(MintOOBStateMachine),
2651    // Removed in https://github.com/fedimint/fedimint/pull/4035 , now ignored
2652    Restore(MintRestoreStateMachine),
2653}
2654
2655impl IntoDynInstance for MintClientStateMachines {
2656    type DynType = DynState;
2657
2658    fn into_dyn(self, instance_id: ModuleInstanceId) -> Self::DynType {
2659        DynState::from_typed(instance_id, self)
2660    }
2661}
2662
2663impl State for MintClientStateMachines {
2664    type ModuleContext = MintClientContext;
2665
2666    fn transitions(
2667        &self,
2668        context: &Self::ModuleContext,
2669        global_context: &DynGlobalClientContext,
2670    ) -> Vec<StateTransition<Self>> {
2671        match self {
2672            MintClientStateMachines::Output(issuance_state) => {
2673                sm_enum_variant_translation!(
2674                    issuance_state.transitions(context, global_context),
2675                    MintClientStateMachines::Output
2676                )
2677            }
2678            MintClientStateMachines::Input(redemption_state) => {
2679                sm_enum_variant_translation!(
2680                    redemption_state.transitions(context, global_context),
2681                    MintClientStateMachines::Input
2682                )
2683            }
2684            MintClientStateMachines::OOB(oob_state) => {
2685                sm_enum_variant_translation!(
2686                    oob_state.transitions(context, global_context),
2687                    MintClientStateMachines::OOB
2688                )
2689            }
2690            MintClientStateMachines::Restore(_) => {
2691                sm_enum_variant_translation!(vec![], MintClientStateMachines::Restore)
2692            }
2693        }
2694    }
2695
2696    fn operation_id(&self) -> OperationId {
2697        match self {
2698            MintClientStateMachines::Output(issuance_state) => issuance_state.operation_id(),
2699            MintClientStateMachines::Input(redemption_state) => redemption_state.operation_id(),
2700            MintClientStateMachines::OOB(oob_state) => oob_state.operation_id(),
2701            MintClientStateMachines::Restore(r) => r.operation_id,
2702        }
2703    }
2704}
2705
2706/// A [`Note`] with associated secret key that allows to proof ownership (spend
2707/// it)
2708#[derive(Clone, Copy, PartialEq, Eq, Hash, Deserialize, Serialize, Encodable, Decodable)]
2709pub struct SpendableNote {
2710    pub signature: tbs::Signature,
2711    pub spend_key: Keypair,
2712}
2713
2714impl fmt::Debug for SpendableNote {
2715    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
2716        f.debug_struct("SpendableNote")
2717            .field("nonce", &self.nonce())
2718            .field("signature", &self.signature)
2719            .field("spend_key", &self.spend_key)
2720            .finish()
2721    }
2722}
2723impl fmt::Display for SpendableNote {
2724    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
2725        self.nonce().fmt(f)
2726    }
2727}
2728
2729impl SpendableNote {
2730    pub fn nonce(&self) -> Nonce {
2731        Nonce(self.spend_key.public_key())
2732    }
2733
2734    fn note(&self) -> Note {
2735        Note {
2736            nonce: self.nonce(),
2737            signature: self.signature,
2738        }
2739    }
2740
2741    pub fn to_undecoded(&self) -> SpendableNoteUndecoded {
2742        SpendableNoteUndecoded {
2743            signature: self
2744                .signature
2745                .consensus_encode_to_vec()
2746                .try_into()
2747                .expect("Encoded size always correct"),
2748            spend_key: self.spend_key,
2749        }
2750    }
2751}
2752
2753/// A version of [`SpendableNote`] that didn't decode the `signature` yet
2754///
2755/// **Note**: signature decoding from raw bytes is faliable, as not all bytes
2756/// are valid signatures. Therefore this type must not be used for external
2757/// data, and should be limited to optimizing reading from internal database.
2758///
2759/// The signature bytes will be validated in [`Self::decode`].
2760///
2761/// Decoding [`tbs::Signature`] is somewhat CPU-intensive (see benches in this
2762/// crate), and when most of the result will be filtered away or completely
2763/// unused, it makes sense to skip/delay decoding.
2764#[derive(Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, Serialize)]
2765pub struct SpendableNoteUndecoded {
2766    // Need to keep this in sync with `tbs::Signature`, but there's a test
2767    // verifying they serialize and decode the same.
2768    #[serde(serialize_with = "serdect::array::serialize_hex_lower_or_bin")]
2769    pub signature: [u8; 48],
2770    pub spend_key: Keypair,
2771}
2772
2773impl fmt::Display for SpendableNoteUndecoded {
2774    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
2775        self.nonce().fmt(f)
2776    }
2777}
2778
2779impl fmt::Debug for SpendableNoteUndecoded {
2780    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
2781        f.debug_struct("SpendableNote")
2782            .field("nonce", &self.nonce())
2783            .field("signature", &"[raw]")
2784            .field("spend_key", &self.spend_key)
2785            .finish()
2786    }
2787}
2788
2789impl SpendableNoteUndecoded {
2790    fn nonce(&self) -> Nonce {
2791        Nonce(self.spend_key.public_key())
2792    }
2793
2794    pub fn decode(self) -> anyhow::Result<SpendableNote> {
2795        Ok(SpendableNote {
2796            signature: Decodable::consensus_decode_partial_from_finite_reader(
2797                &mut self.signature.as_slice(),
2798                &ModuleRegistry::default(),
2799            )?,
2800            spend_key: self.spend_key,
2801        })
2802    }
2803}
2804
2805/// An index used to deterministically derive [`Note`]s
2806///
2807/// We allow converting it to u64 and incrementing it, but
2808/// messing with it should be somewhat restricted to prevent
2809/// silly errors.
2810#[derive(
2811    Copy,
2812    Clone,
2813    Debug,
2814    Serialize,
2815    Deserialize,
2816    PartialEq,
2817    Eq,
2818    Encodable,
2819    Decodable,
2820    Default,
2821    PartialOrd,
2822    Ord,
2823)]
2824pub struct NoteIndex(u64);
2825
2826impl NoteIndex {
2827    pub fn next(self) -> Self {
2828        Self(self.0 + 1)
2829    }
2830
2831    fn prev(self) -> Option<Self> {
2832        self.0.checked_sub(0).map(Self)
2833    }
2834
2835    pub fn as_u64(self) -> u64 {
2836        self.0
2837    }
2838
2839    // Private. If it turns out it is useful outside,
2840    // we can relax and convert to `From<u64>`
2841    // Actually used in tests RN, so cargo complains in non-test builds.
2842    #[allow(unused)]
2843    pub fn from_u64(v: u64) -> Self {
2844        Self(v)
2845    }
2846
2847    pub fn advance(&mut self) {
2848        *self = self.next();
2849    }
2850}
2851
2852impl std::fmt::Display for NoteIndex {
2853    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2854        self.0.fmt(f)
2855    }
2856}
2857
2858struct OOBSpendTag;
2859
2860impl sha256t::Tag for OOBSpendTag {
2861    fn engine() -> sha256::HashEngine {
2862        let mut engine = sha256::HashEngine::default();
2863        engine.input(b"oob-spend");
2864        engine
2865    }
2866}
2867
2868struct OOBReissueTag;
2869
2870impl sha256t::Tag for OOBReissueTag {
2871    fn engine() -> sha256::HashEngine {
2872        let mut engine = sha256::HashEngine::default();
2873        engine.input(b"oob-reissue");
2874        engine
2875    }
2876}
2877
2878/// Determines the denominations to use when representing an amount
2879///
2880/// Algorithm tries to leave the user with a target number of
2881/// `denomination_sets` starting at the lowest denomination.  `self`
2882/// gives the denominations that the user already has.
2883pub fn represent_amount<K>(
2884    amount: Amount,
2885    current_denominations: &TieredCounts,
2886    tiers: &Tiered<K>,
2887    denomination_sets: u16,
2888    fee_consensus: &FeeConsensus,
2889) -> TieredCounts {
2890    let mut remaining_amount = amount;
2891    let mut denominations = TieredCounts::default();
2892
2893    // try to hit the target `denomination_sets`
2894    for tier in tiers.tiers() {
2895        let notes = current_denominations.get(*tier);
2896        let missing_notes = u64::from(denomination_sets).saturating_sub(notes as u64);
2897        let possible_notes = remaining_amount / (*tier + fee_consensus.fee(*tier));
2898
2899        let add_notes = min(possible_notes, missing_notes);
2900        denominations.inc(*tier, add_notes as usize);
2901        remaining_amount -= (*tier + fee_consensus.fee(*tier)) * add_notes;
2902    }
2903
2904    // if there is a remaining amount, add denominations with a greedy algorithm
2905    for tier in tiers.tiers().rev() {
2906        let res = remaining_amount / (*tier + fee_consensus.fee(*tier));
2907        remaining_amount -= (*tier + fee_consensus.fee(*tier)) * res;
2908        denominations.inc(*tier, res as usize);
2909    }
2910
2911    let represented: u64 = denominations
2912        .iter()
2913        .map(|(k, v)| (k + fee_consensus.fee(k)).msats * (v as u64))
2914        .sum();
2915
2916    assert!(represented <= amount.msats);
2917    assert!(represented + fee_consensus.fee(Amount::from_msats(1)).msats >= amount.msats);
2918
2919    denominations
2920}
2921
2922pub(crate) fn create_bundle_for_inputs(
2923    inputs_and_notes: Vec<(ClientInput<MintInput>, SpendableNote)>,
2924    operation_id: OperationId,
2925) -> ClientInputBundle<MintInput, MintClientStateMachines> {
2926    let mut inputs = Vec::new();
2927    let mut input_states = Vec::new();
2928
2929    for (input, spendable_note) in inputs_and_notes {
2930        input_states.push((input.amounts.clone(), spendable_note));
2931        inputs.push(input);
2932    }
2933
2934    let input_sm = Arc::new(move |out_point_range: OutPointRange| {
2935        debug_assert_eq!(out_point_range.into_iter().count(), input_states.len());
2936
2937        vec![MintClientStateMachines::Input(MintInputStateMachine {
2938            common: MintInputCommon {
2939                operation_id,
2940                out_point_range,
2941            },
2942            state: MintInputStates::CreatedBundle(MintInputStateCreatedBundle {
2943                notes: input_states
2944                    .iter()
2945                    .map(|(amounts, note)| (amounts.expect_only_bitcoin(), *note))
2946                    .collect(),
2947            }),
2948        })]
2949    });
2950
2951    ClientInputBundle::new(
2952        inputs,
2953        vec![ClientInputSM {
2954            state_machines: input_sm,
2955        }],
2956    )
2957}
2958
2959#[cfg(test)]
2960mod tests {
2961    use std::fmt::Display;
2962    use std::str::FromStr;
2963
2964    use bitcoin_hashes::Hash;
2965    use fedimint_core::base32::FEDIMINT_PREFIX;
2966    use fedimint_core::config::FederationId;
2967    use fedimint_core::encoding::Decodable;
2968    use fedimint_core::invite_code::InviteCode;
2969    use fedimint_core::module::registry::ModuleRegistry;
2970    use fedimint_core::{
2971        Amount, OutPoint, PeerId, Tiered, TieredCounts, TieredMulti, TransactionId,
2972    };
2973    use fedimint_mint_common::config::FeeConsensus;
2974    use itertools::Itertools;
2975    use serde_json::json;
2976
2977    use crate::{
2978        MintOperationMetaVariant, OOBNotes, OOBNotesPart, SpendableNote, SpendableNoteUndecoded,
2979        represent_amount, select_notes_from_stream,
2980    };
2981
2982    #[test]
2983    fn represent_amount_targets_denomination_sets() {
2984        fn tiers(tiers: Vec<u64>) -> Tiered<()> {
2985            tiers
2986                .into_iter()
2987                .map(|tier| (Amount::from_sats(tier), ()))
2988                .collect()
2989        }
2990
2991        fn denominations(denominations: Vec<(Amount, usize)>) -> TieredCounts {
2992            TieredCounts::from_iter(denominations)
2993        }
2994
2995        let starting = notes(vec![
2996            (Amount::from_sats(1), 1),
2997            (Amount::from_sats(2), 3),
2998            (Amount::from_sats(3), 2),
2999        ])
3000        .summary();
3001        let tiers = tiers(vec![1, 2, 3, 4]);
3002
3003        // target 3 tiers will fill out the 1 and 3 denominations
3004        assert_eq!(
3005            represent_amount(
3006                Amount::from_sats(6),
3007                &starting,
3008                &tiers,
3009                3,
3010                &FeeConsensus::zero()
3011            ),
3012            denominations(vec![(Amount::from_sats(1), 3), (Amount::from_sats(3), 1),])
3013        );
3014
3015        // target 2 tiers will fill out the 1 and 4 denominations
3016        assert_eq!(
3017            represent_amount(
3018                Amount::from_sats(6),
3019                &starting,
3020                &tiers,
3021                2,
3022                &FeeConsensus::zero()
3023            ),
3024            denominations(vec![(Amount::from_sats(1), 2), (Amount::from_sats(4), 1)])
3025        );
3026    }
3027
3028    #[test_log::test(tokio::test)]
3029    async fn select_notes_avg_test() {
3030        let max_amount = Amount::from_sats(1_000_000);
3031        let tiers = Tiered::gen_denominations(2, max_amount);
3032        let tiered = represent_amount::<()>(
3033            max_amount,
3034            &TieredCounts::default(),
3035            &tiers,
3036            3,
3037            &FeeConsensus::zero(),
3038        );
3039
3040        let mut total_notes = 0;
3041        for multiplier in 1..100 {
3042            let stream = reverse_sorted_note_stream(tiered.iter().collect());
3043            let select = select_notes_from_stream(
3044                stream,
3045                Amount::from_sats(multiplier * 1000),
3046                FeeConsensus::zero(),
3047            )
3048            .await;
3049            total_notes += select.unwrap().into_iter_items().count();
3050        }
3051        assert_eq!(total_notes / 100, 10);
3052    }
3053
3054    #[test_log::test(tokio::test)]
3055    async fn select_notes_returns_exact_amount_with_minimum_notes() {
3056        let f = || {
3057            reverse_sorted_note_stream(vec![
3058                (Amount::from_sats(1), 10),
3059                (Amount::from_sats(5), 10),
3060                (Amount::from_sats(20), 10),
3061            ])
3062        };
3063        assert_eq!(
3064            select_notes_from_stream(f(), Amount::from_sats(7), FeeConsensus::zero())
3065                .await
3066                .unwrap(),
3067            notes(vec![(Amount::from_sats(1), 2), (Amount::from_sats(5), 1)])
3068        );
3069        assert_eq!(
3070            select_notes_from_stream(f(), Amount::from_sats(20), FeeConsensus::zero())
3071                .await
3072                .unwrap(),
3073            notes(vec![(Amount::from_sats(20), 1)])
3074        );
3075    }
3076
3077    #[test_log::test(tokio::test)]
3078    async fn select_notes_returns_next_smallest_amount_if_exact_change_cannot_be_made() {
3079        let stream = reverse_sorted_note_stream(vec![
3080            (Amount::from_sats(1), 1),
3081            (Amount::from_sats(5), 5),
3082            (Amount::from_sats(20), 5),
3083        ]);
3084        assert_eq!(
3085            select_notes_from_stream(stream, Amount::from_sats(7), FeeConsensus::zero())
3086                .await
3087                .unwrap(),
3088            notes(vec![(Amount::from_sats(5), 2)])
3089        );
3090    }
3091
3092    #[test_log::test(tokio::test)]
3093    async fn select_notes_uses_big_note_if_small_amounts_are_not_sufficient() {
3094        let stream = reverse_sorted_note_stream(vec![
3095            (Amount::from_sats(1), 3),
3096            (Amount::from_sats(5), 3),
3097            (Amount::from_sats(20), 2),
3098        ]);
3099        assert_eq!(
3100            select_notes_from_stream(stream, Amount::from_sats(39), FeeConsensus::zero())
3101                .await
3102                .unwrap(),
3103            notes(vec![(Amount::from_sats(20), 2)])
3104        );
3105    }
3106
3107    #[test_log::test(tokio::test)]
3108    async fn select_notes_returns_error_if_amount_is_too_large() {
3109        let stream = reverse_sorted_note_stream(vec![(Amount::from_sats(10), 1)]);
3110        let error = select_notes_from_stream(stream, Amount::from_sats(100), FeeConsensus::zero())
3111            .await
3112            .unwrap_err();
3113        assert_eq!(error.total_amount, Amount::from_sats(10));
3114    }
3115
3116    fn reverse_sorted_note_stream(
3117        notes: Vec<(Amount, usize)>,
3118    ) -> impl futures::Stream<Item = (Amount, String)> {
3119        futures::stream::iter(
3120            notes
3121                .into_iter()
3122                // We are creating `number` dummy notes of `amount` value
3123                .flat_map(|(amount, number)| vec![(amount, "dummy note".into()); number])
3124                .sorted()
3125                .rev(),
3126        )
3127    }
3128
3129    fn notes(notes: Vec<(Amount, usize)>) -> TieredMulti<String> {
3130        notes
3131            .into_iter()
3132            .flat_map(|(amount, number)| vec![(amount, "dummy note".into()); number])
3133            .collect()
3134    }
3135
3136    #[test]
3137    fn decoding_empty_oob_notes_fails() {
3138        let empty_oob_notes =
3139            OOBNotes::new(FederationId::dummy().to_prefix(), TieredMulti::default());
3140        let oob_notes_string = empty_oob_notes.to_string();
3141
3142        let res = oob_notes_string.parse::<OOBNotes>();
3143
3144        assert!(res.is_err(), "An empty OOB notes string should not parse");
3145    }
3146
3147    fn test_roundtrip_serialize_str<T, F>(data: T, assertions: F)
3148    where
3149        T: FromStr + Display + crate::Encodable + crate::Decodable,
3150        <T as FromStr>::Err: std::fmt::Debug,
3151        F: Fn(T),
3152    {
3153        let data_parsed = data.to_string().parse().expect("Deserialization failed");
3154
3155        assertions(data_parsed);
3156
3157        let data_parsed = crate::base32::encode_prefixed(FEDIMINT_PREFIX, &data)
3158            .parse()
3159            .expect("Deserialization failed");
3160
3161        assertions(data_parsed);
3162
3163        assertions(data);
3164    }
3165
3166    #[test]
3167    fn notes_encode_decode() {
3168        let federation_id_1 =
3169            FederationId(bitcoin_hashes::sha256::Hash::from_byte_array([0x21; 32]));
3170        let federation_id_prefix_1 = federation_id_1.to_prefix();
3171        let federation_id_2 =
3172            FederationId(bitcoin_hashes::sha256::Hash::from_byte_array([0x42; 32]));
3173        let federation_id_prefix_2 = federation_id_2.to_prefix();
3174
3175        let notes = vec![(
3176            Amount::from_sats(1),
3177            SpendableNote::consensus_decode_hex("a5dd3ebacad1bc48bd8718eed5a8da1d68f91323bef2848ac4fa2e6f8eed710f3178fd4aef047cc234e6b1127086f33cc408b39818781d9521475360de6b205f3328e490a6d99d5e2553a4553207c8bd", &ModuleRegistry::default()).unwrap(),
3178        )]
3179        .into_iter()
3180        .collect::<TieredMulti<_>>();
3181
3182        // Can decode inviteless notes
3183        let notes_no_invite = OOBNotes::new(federation_id_prefix_1, notes.clone());
3184        test_roundtrip_serialize_str(notes_no_invite, |oob_notes| {
3185            assert_eq!(oob_notes.notes(), &notes);
3186            assert_eq!(oob_notes.federation_id_prefix(), federation_id_prefix_1);
3187            assert_eq!(oob_notes.federation_invite(), None);
3188        });
3189
3190        // Can decode notes with invite
3191        let invite = InviteCode::new(
3192            "wss://foo.bar".parse().unwrap(),
3193            PeerId::from(0),
3194            federation_id_1,
3195            None,
3196        );
3197        let notes_invite = OOBNotes::new_with_invite(notes.clone(), &invite);
3198        test_roundtrip_serialize_str(notes_invite, |oob_notes| {
3199            assert_eq!(oob_notes.notes(), &notes);
3200            assert_eq!(oob_notes.federation_id_prefix(), federation_id_prefix_1);
3201            assert_eq!(oob_notes.federation_invite(), Some(invite.clone()));
3202        });
3203
3204        // Can decode notes without federation id prefix, so we can optionally remove it
3205        // in the future
3206        let notes_no_prefix = OOBNotes(vec![
3207            OOBNotesPart::Notes(notes.clone()),
3208            OOBNotesPart::Invite {
3209                peer_apis: vec![(PeerId::from(0), "wss://foo.bar".parse().unwrap())],
3210                federation_id: federation_id_1,
3211            },
3212        ]);
3213        test_roundtrip_serialize_str(notes_no_prefix, |oob_notes| {
3214            assert_eq!(oob_notes.notes(), &notes);
3215            assert_eq!(oob_notes.federation_id_prefix(), federation_id_prefix_1);
3216        });
3217
3218        // Rejects notes with inconsistent federation id
3219        let notes_inconsistent = OOBNotes(vec![
3220            OOBNotesPart::Notes(notes),
3221            OOBNotesPart::Invite {
3222                peer_apis: vec![(PeerId::from(0), "wss://foo.bar".parse().unwrap())],
3223                federation_id: federation_id_1,
3224            },
3225            OOBNotesPart::FederationIdPrefix(federation_id_prefix_2),
3226        ]);
3227        let notes_inconsistent_str = notes_inconsistent.to_string();
3228        assert!(notes_inconsistent_str.parse::<OOBNotes>().is_err());
3229    }
3230
3231    #[test]
3232    fn spendable_note_undecoded_sanity() {
3233        // TODO: add more hex dumps to the loop
3234        #[allow(clippy::single_element_loop)]
3235        for note_hex in [
3236            "a5dd3ebacad1bc48bd8718eed5a8da1d68f91323bef2848ac4fa2e6f8eed710f3178fd4aef047cc234e6b1127086f33cc408b39818781d9521475360de6b205f3328e490a6d99d5e2553a4553207c8bd",
3237        ] {
3238            let note =
3239                SpendableNote::consensus_decode_hex(note_hex, &ModuleRegistry::default()).unwrap();
3240            let note_undecoded =
3241                SpendableNoteUndecoded::consensus_decode_hex(note_hex, &ModuleRegistry::default())
3242                    .unwrap()
3243                    .decode()
3244                    .unwrap();
3245            assert_eq!(note, note_undecoded,);
3246            assert_eq!(
3247                serde_json::to_string(&note).unwrap(),
3248                serde_json::to_string(&note_undecoded).unwrap(),
3249            );
3250        }
3251    }
3252
3253    #[test]
3254    fn reissuance_meta_compatibility_02_03() {
3255        let dummy_outpoint = OutPoint {
3256            txid: TransactionId::all_zeros(),
3257            out_idx: 0,
3258        };
3259
3260        let old_meta_json = json!({
3261            "reissuance": {
3262                "out_point": dummy_outpoint
3263            }
3264        });
3265
3266        let old_meta: MintOperationMetaVariant =
3267            serde_json::from_value(old_meta_json).expect("parsing old reissuance meta failed");
3268        assert_eq!(
3269            old_meta,
3270            MintOperationMetaVariant::Reissuance {
3271                legacy_out_point: Some(dummy_outpoint),
3272                txid: None,
3273                out_point_indices: vec![],
3274            }
3275        );
3276
3277        let new_meta_json = serde_json::to_value(MintOperationMetaVariant::Reissuance {
3278            legacy_out_point: None,
3279            txid: Some(dummy_outpoint.txid),
3280            out_point_indices: vec![0],
3281        })
3282        .expect("serializing always works");
3283        assert_eq!(
3284            new_meta_json,
3285            json!({
3286                "reissuance": {
3287                    "txid": dummy_outpoint.txid,
3288                    "out_point_indices": [dummy_outpoint.out_idx],
3289                }
3290            })
3291        );
3292    }
3293}