add electrum backend in CoordinatorWallet struct

This commit is contained in:
f321x
2024-07-04 09:27:17 +00:00
parent 279d7d167a
commit 68707c2566

View File

@ -11,13 +11,14 @@ use bdk::{
wallet::verify::*, wallet::verify::*,
KeychainKind, SyncOptions, Wallet, KeychainKind, SyncOptions, Wallet,
}; };
use std::fmt;
use std::str::FromStr; use std::str::FromStr;
use utils::*; use utils::*;
#[derive(Clone, Debug)] #[derive(Clone)]
pub struct CoordinatorWallet { pub struct CoordinatorWallet {
pub wallet: Arc<Mutex<Wallet<Tree>>>, pub wallet: Arc<Mutex<Wallet<Tree>>>,
// database: Arc<Mutex<Tree>>, pub backend: Arc<ElectrumBlockchain>,
} }
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
@ -50,7 +51,7 @@ impl CoordinatorWallet {
dbg!(wallet.get_balance()?); dbg!(wallet.get_balance()?);
Ok(CoordinatorWallet { Ok(CoordinatorWallet {
wallet: Arc::new(Mutex::new(wallet)), wallet: Arc::new(Mutex::new(wallet)),
// database: Arc::new(Mutex::new(sled_db)), backend: Arc::new(backend),
}) })
} }
@ -67,22 +68,19 @@ impl CoordinatorWallet {
requirements: &BondRequirements, requirements: &BondRequirements,
) -> Result<()> { ) -> Result<()> {
let input_sum: u64; let input_sum: u64;
let blockchain = &*self.backend;
let tx: Transaction = deserialize(&hex::decode(bond)?)?; let tx: Transaction = deserialize(&hex::decode(bond)?)?;
{ {
let blockchain = ElectrumBlockchain::from(Client::new(
&env::var("ELECTRUM_BACKEND")
.context("Parsing ELECTRUM_BACKEND from .env failed, is it set?")?,
)?);
let wallet = self.wallet.lock().await; let wallet = self.wallet.lock().await;
// we need to test this with signed and invalid/unsigned transactions // we need to test this with signed and invalid/unsigned transactions
// checks signatures and inputs // checks signatures and inputs
if let Err(e) = verify_tx(&tx, &*wallet.database(), &blockchain) { if let Err(e) = verify_tx(&tx, &*wallet.database(), blockchain) {
return Err(anyhow!(e)); return Err(anyhow!(e));
} }
// check if the tx has the correct input amounts (have to be >= trading amount) // check if the tx has the correct input amounts (have to be >= trading amount)
input_sum = match tx.input_sum(&blockchain, &*wallet.database()) { input_sum = match tx.input_sum(blockchain, &*wallet.database()) {
Ok(amount) => { Ok(amount) => {
if amount < requirements.min_input_sum_sat { if amount < requirements.min_input_sum_sat {
return Err(anyhow!("Bond input sum too small")); return Err(anyhow!("Bond input sum too small"));
@ -113,3 +111,14 @@ impl CoordinatorWallet {
Ok(()) Ok(())
} }
} }
impl fmt::Debug for CoordinatorWallet {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CoordinatorWallet")
.field("wallet", &self.wallet)
// Since ElectrumBlockchain doesn't implement Debug, we can't automatically derive it.
// Instead, we can print a placeholder or simply omit it from the debug output.
.field("backend", &"ElectrumBlockchain (Debug not implemented)")
.finish()
}
}