1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use cdk_common::wallet::{Transaction, TransactionDirection, TransactionId};
use cdk_common::Proofs;
use crate::{Error, Wallet};
impl Wallet {
/// List transactions
pub async fn list_transactions(
&self,
direction: Option<TransactionDirection>,
) -> Result<Vec<Transaction>, Error> {
let mut transactions = self
.localstore
.list_transactions(
Some(self.mint_url.clone()),
direction,
Some(self.unit.clone()),
)
.await?;
transactions.sort();
Ok(transactions)
}
/// Get transaction by ID
pub async fn get_transaction(&self, id: TransactionId) -> Result<Option<Transaction>, Error> {
let transaction = self.localstore.get_transaction(id).await?;
Ok(transaction)
}
/// Get proofs for a transaction by transaction ID
///
/// This retrieves all proofs associated with a transaction by looking up
/// the transaction's Y values and fetching the corresponding proofs.
pub async fn get_proofs_for_transaction(&self, id: TransactionId) -> Result<Proofs, Error> {
let transaction = self
.localstore
.get_transaction(id)
.await?
.ok_or(Error::TransactionNotFound)?;
let proofs = self
.localstore
.get_proofs_by_ys(transaction.ys)
.await?
.into_iter()
.map(|p| p.proof)
.collect();
Ok(proofs)
}
/// Revert a transaction by reclaiming unspent proofs.
///
/// For transactions created by the saga pattern (with `saga_id` set), this
/// function loads the associated send saga and calls `revoke()` on it, which
/// properly handles the saga lifecycle including state transitions and cleanup.
///
/// For legacy transactions (without `saga_id`), this function checks the proofs
/// with the mint and marks any spent proofs accordingly. Unspent proofs are
/// left in their current state for manual recovery.
///
/// # Errors
///
/// Returns an error if:
/// - The transaction is not found
/// - The transaction is not outgoing
/// - The saga is not in a revocable state (e.g., already completed)
/// - The token has already been claimed by the recipient
pub async fn revert_transaction(&self, id: TransactionId) -> Result<(), Error> {
let tx = self
.localstore
.get_transaction(id)
.await?
.ok_or(Error::TransactionNotFound)?;
if tx.direction != TransactionDirection::Outgoing {
return Err(Error::InvalidTransactionDirection);
}
// Check if this is a saga-managed transaction
if let Some(saga_id) = &tx.saga_id {
// Use the existing revoke_send method which properly handles the saga
// Discard the returned amount - we just care about success/failure
let _ = self.revoke_send(*saga_id).await?;
Ok(())
} else {
// Legacy transaction without saga - check proofs and mark spent ones
// We don't attempt to swap for legacy transactions to avoid
// interfering with any potential in-flight operations
let pending_spent_proofs: Proofs = self
.get_pending_spent_proofs()
.await?
.into_iter()
.filter(|p| match p.y() {
Ok(y) => tx.ys.contains(&y),
Err(_) => false,
})
.collect();
if pending_spent_proofs.is_empty() {
return Ok(());
}
// Just check and mark spent - don't attempt swap for legacy transactions
self.check_proofs_spent(pending_spent_proofs).await?;
Ok(())
}
}
}