diff --git a/src/silentpayments.rs b/src/silentpayments.rs index 53086c7..079e041 100644 --- a/src/silentpayments.rs +++ b/src/silentpayments.rs @@ -3,11 +3,12 @@ use std::collections::HashMap; use anyhow::{Error, Result}; use serde::{Deserialize, Serialize}; use sp_client::bitcoin::absolute::Height; +use sp_client::bitcoin::hashes::Hash; use sp_client::bitcoin::secp256k1::{PublicKey, SecretKey}; use tsify::Tsify; use rand::{thread_rng, Rng}; -use sp_client::bitcoin::{Amount, OutPoint, Transaction, TxOut, XOnlyPublicKey}; +use sp_client::bitcoin::{Amount, BlockHash, OutPoint, Transaction, TxOut, Txid, XOnlyPublicKey}; use sp_client::{FeeRate, OutputSpendStatus, OwnedOutput, Recipient, SilentPaymentUnsignedTransaction, SpClient}; use sp_client::silentpayments::utils::receiving::calculate_ecdh_shared_secret; @@ -80,10 +81,22 @@ impl SpWallet { } } + fn check_inputs(&mut self, tx: &Transaction) { + for input in &tx.input { + if let Some(output) = self.outputs.get(&input.previous_output) { + if output.spend_status != OutputSpendStatus::Unspent { + log::debug!("Input is already spent: {:?}", input.previous_output); + continue; + } else { + self.mark_output_spent(&input.previous_output, &tx.txid()); + } + } } } pub fn update_with_transaction(&mut self, tx: &Transaction, public_tweak: &PublicKey, height: u32) -> Result> { + // Check if we have outputs that are spent by this transaction + self.check_inputs(tx); let receiver = &self.get_sp_client().sp_receiver; let p2tr_outs: Vec<(usize, &TxOut)> = tx .output