Explorar el Código

Reclaim unspent proofs by reverting transaction (#774)

* Reclaim unspent proofs by reverting transaction

* Change fn signatore to return unit
David Caseria hace 3 semanas
padre
commit
30d6b20c99
Se han modificado 2 ficheros con 29 adiciones y 0 borrados
  1. 3 0
      crates/cdk-common/src/error.rs
  2. 26 0
      crates/cdk/src/wallet/transactions.rs

+ 3 - 0
crates/cdk-common/src/error.rs

@@ -234,6 +234,9 @@ pub enum Error {
     /// Invalid transaction id
     #[error("Invalid transaction id")]
     InvalidTransactionId,
+    /// Transaction not found
+    #[error("Transaction not found")]
+    TransactionNotFound,
     /// Custom Error
     #[error("`{0}`")]
     Custom(String),

+ 26 - 0
crates/cdk/src/wallet/transactions.rs

@@ -28,4 +28,30 @@ impl Wallet {
 
         Ok(transaction)
     }
+
+    /// Revert a transaction
+    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);
+        }
+
+        let pending_spent_proofs = self
+            .get_pending_spent_proofs()
+            .await?
+            .into_iter()
+            .filter(|p| match p.y() {
+                Ok(y) => tx.ys.contains(&y),
+                Err(_) => false,
+            })
+            .collect::<Vec<_>>();
+
+        self.reclaim_unspent(pending_spent_proofs).await?;
+        Ok(())
+    }
 }