feat: add get_info method to wallet (#12)

* feat: initial `get_info` wallet method
* feat: add current active address to info
This commit is contained in:
yse
2024-03-18 10:41:41 +01:00
committed by GitHub
parent ffef9587c9
commit 72835423cb
3 changed files with 34 additions and 16 deletions

View File

@@ -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
))
}
}
}

View File

@@ -96,3 +96,9 @@ impl From<S5Error> for SwapError {
}
}
}
pub struct WalletInfo {
pub balance_sat: u64,
pub pubkey: String,
pub active_address: String,
}

View File

@@ -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<Mutex<LwkWollet>>,
pending_claims: Arc<Mutex<HashSet<ClaimDetails>>>,
active_address: Option<u32>,
}
#[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<u32>) -> Result<Address> {
fn address(&self) -> Result<Address> {
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<u64> {
fn total_balance_sat(&self, with_scan: bool) -> Result<u64> {
if with_scan {
self.scan()?;
}
@@ -114,6 +117,14 @@ impl Wallet {
Ok(balance.values().sum())
}
pub fn get_info(&self, with_scan: bool) -> Result<WalletInfo> {
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();