Skip to main content

cdk_common/wallet/saga/
swap.rs

1//! Swap saga types
2
3use cashu::BlindedMessage;
4use serde::{Deserialize, Serialize};
5
6use crate::{Amount, Error};
7
8/// States specific to swap saga (wallet-side)
9#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize)]
10#[serde(rename_all = "snake_case")]
11pub enum SwapSagaState {
12    /// Input proofs reserved, swap request prepared, ready to execute
13    ProofsReserved,
14    /// Swap request sent to mint, awaiting signatures for new proofs
15    SwapRequested,
16}
17
18impl std::fmt::Display for SwapSagaState {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        match self {
21            SwapSagaState::ProofsReserved => write!(f, "proofs_reserved"),
22            SwapSagaState::SwapRequested => write!(f, "swap_requested"),
23        }
24    }
25}
26
27impl std::str::FromStr for SwapSagaState {
28    type Err = Error;
29    fn from_str(s: &str) -> Result<Self, Self::Err> {
30        match s {
31            "proofs_reserved" => Ok(SwapSagaState::ProofsReserved),
32            "swap_requested" => Ok(SwapSagaState::SwapRequested),
33            _ => Err(Error::InvalidOperationState),
34        }
35    }
36}
37
38/// Operation-specific data for Swap operations
39#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
40pub struct SwapOperationData {
41    /// Input amount
42    pub input_amount: Amount,
43    /// Output amount
44    pub output_amount: Amount,
45    /// Derivation counter start
46    pub counter_start: Option<u32>,
47    /// Derivation counter end
48    pub counter_end: Option<u32>,
49    /// Blinded messages for recovery
50    ///
51    /// Stored so that if a crash occurs after the mint accepts the swap,
52    /// we can use these to query the mint for signatures and reconstruct proofs.
53    #[serde(default, skip_serializing_if = "Option::is_none")]
54    pub blinded_messages: Option<Vec<BlindedMessage>>,
55}