From 72835423cbf5fbfbe4bce9b161a39cd417a0d87c Mon Sep 17 00:00:00 2001 From: yse <70684173+hydra-yse@users.noreply.github.com> Date: Mon, 18 Mar 2024 10:41:41 +0100 Subject: [PATCH] feat: add `get_info` method to wallet (#12) * feat: initial `get_info` wallet method * feat: add current active address to info --- cli/src/commands.rs | 14 +++++++++----- lib/src/model.rs | 6 ++++++ lib/src/wallet.rs | 30 +++++++++++++++++++----------- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/cli/src/commands.rs b/cli/src/commands.rs index 8922a9d..f79611d 100644 --- a/cli/src/commands.rs +++ b/cli/src/commands.rs @@ -17,7 +17,7 @@ pub(crate) enum Command { /// Receive lbtc and send btc through a swap ReceivePayment { amount_sat: u64 }, /// Get the balance of the currently loaded wallet - GetBalance, + GetInfo, } #[derive(Helper, Completer, Hinter, Validator)] @@ -56,9 +56,13 @@ pub(crate) async fn handle_command( response.txid )) } - Command::GetBalance {} => Ok(format!( - "Current balance: {} sat", - wallet.total_balance_sat(true)? - )), + Command::GetInfo {} => { + let info = wallet.get_info(true)?; + + Ok(format!( + "Current Balance: {} sat\nPublic Key: {}\nLiquid Address: {}", + info.balance_sat, info.pubkey, info.active_address + )) + } } } diff --git a/lib/src/model.rs b/lib/src/model.rs index 7179272..bc10ee1 100644 --- a/lib/src/model.rs +++ b/lib/src/model.rs @@ -96,3 +96,9 @@ impl From for SwapError { } } } + +pub struct WalletInfo { + pub balance_sat: u64, + pub pubkey: String, + pub active_address: String, +} diff --git a/lib/src/wallet.rs b/lib/src/wallet.rs index 64e2375..17bb26e 100644 --- a/lib/src/wallet.rs +++ b/lib/src/wallet.rs @@ -29,7 +29,8 @@ use lwk_wollet::{ }; use crate::{ - ClaimDetails, SendPaymentResponse, SwapError, SwapLbtcResponse, SwapStatus, WalletOptions, + ClaimDetails, SendPaymentResponse, SwapError, SwapLbtcResponse, SwapStatus, WalletInfo, + WalletOptions, }; const DEFAULT_DB_DIR: &str = ".wollet"; @@ -41,6 +42,7 @@ pub struct Wallet { network: ElementsNetwork, wallet: Arc>, pending_claims: Arc>>, + active_address: Option, } #[allow(dead_code)] @@ -66,6 +68,7 @@ impl Wallet { electrum_url, signer: opts.signer, pending_claims: Default::default(), + active_address: None, }); Wallet::track_claims(&wallet)?; @@ -101,12 +104,12 @@ impl Wallet { full_scan_with_electrum_client(&mut wallet, &mut electrum_client) } - fn address(&self, index: Option) -> Result
{ + fn address(&self) -> Result
{ let wallet = self.wallet.lock().unwrap(); - Ok(wallet.address(index)?.address().clone()) + Ok(wallet.address(self.active_address)?.address().clone()) } - pub fn total_balance_sat(&self, with_scan: bool) -> Result { + fn total_balance_sat(&self, with_scan: bool) -> Result { if with_scan { self.scan()?; } @@ -114,6 +117,14 @@ impl Wallet { Ok(balance.values().sum()) } + pub fn get_info(&self, with_scan: bool) -> Result { + Ok(WalletInfo { + balance_sat: self.total_balance_sat(with_scan)?, + pubkey: self.signer.xpub().public_key.to_string(), + active_address: self.address()?.to_string(), + }) + } + fn get_signer(&self) -> SwSigner { self.signer.clone() } @@ -234,7 +245,7 @@ impl Wallet { &claim_details.redeem_script, &claim_details.blinding_str, )?, - self.address(None) + self.address() .map_err(|_| SwapError::WalletError)? .to_string(), network_config, @@ -331,12 +342,9 @@ impl Wallet { let network_config = self.get_network_config(); debug!("{:?}", script.fetch_utxo(&network_config)); - let mut tx = LBtcSwapTx::new_claim( - script.clone(), - self.address(None)?.to_string(), - &network_config, - ) - .expect("Expecting valid tx"); + let mut tx = + LBtcSwapTx::new_claim(script.clone(), self.address()?.to_string(), &network_config) + .expect("Expecting valid tx"); let keypair: Keypair = recovery.try_into().unwrap(); let preimage: Preimage = recovery.try_into().unwrap();