Skip to content

Commit

Permalink
feat: add transaction persistence to lwk instance (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
hydra-yse authored Apr 8, 2024
1 parent 2a23170 commit 04d0c5b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
6 changes: 6 additions & 0 deletions cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub(crate) enum Command {
ListPayments,
/// Get the balance of the currently loaded wallet
GetInfo,
/// Empties the encrypted wallet transaction cache
EmptyCache,
}

#[derive(Helper, Completer, Hinter, Validator)]
Expand Down Expand Up @@ -102,5 +104,9 @@ pub(crate) fn handle_command(

Ok(payments_str)
}
Command::EmptyCache => Ok(match wallet.empty_wallet_cache() {
Ok(_) => "Cache emptied successfully".to_string(),
Err(e) => format!("Could not empty cache. Err: {e}"),
}),
}
}
60 changes: 46 additions & 14 deletions lib/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
fs,
path::PathBuf,
sync::{Arc, Mutex},
thread,
time::Duration,
Expand All @@ -19,8 +20,11 @@ use log::{debug, warn};
use lwk_common::{singlesig_desc, Signer, Singlesig};
use lwk_signer::{AnySigner, SwSigner};
use lwk_wollet::{
elements::Address, full_scan_with_electrum_client, BlockchainBackend, ElectrumClient,
ElectrumUrl, ElementsNetwork, NoPersist, Wollet as LwkWollet, WolletDescriptor,
elements::Address,
full_scan_with_electrum_client,
hashes::{sha256t_hash_newtype, Hash},
BlockchainBackend, ElectrumClient, ElectrumUrl, ElementsNetwork, FsPersister,
Wollet as LwkWollet, WolletDescriptor,
};

use crate::{
Expand All @@ -29,27 +33,28 @@ use crate::{
WalletInfo, WalletOptions, CLAIM_ABSOLUTE_FEES, DEFAULT_DATA_DIR,
};

sha256t_hash_newtype! {
struct DirectoryIdTag = hash_str("LWK-FS-Directory-Id/1.0");

#[hash_newtype(forward)]
struct DirectoryIdHash(_);
}

pub struct Wallet {
signer: SwSigner,
electrum_url: ElectrumUrl,
network: Network,
wallet: Arc<Mutex<LwkWollet>>,
active_address: Option<u32>,
swap_persister: Persister,
data_dir_path: String,
}

impl Wallet {
pub fn init(mnemonic: &str, data_dir: Option<String>, network: Network) -> Result<Arc<Wallet>> {
let is_mainnet = network == Network::Liquid;
let signer = SwSigner::new(mnemonic, is_mainnet)?;
let descriptor_str = singlesig_desc(
&signer,
Singlesig::Wpkh,
lwk_common::DescriptorBlindingKey::Slip77,
is_mainnet,
)
.map_err(|e| anyhow!("Invalid descriptor: {e}"))?;
let descriptor: WolletDescriptor = descriptor_str.parse()?;
let descriptor = Wallet::get_descriptor(&signer, network)?;

Wallet::new(WalletOptions {
signer,
Expand All @@ -64,18 +69,18 @@ impl Wallet {
let network = opts.network;
let elements_network: ElementsNetwork = opts.network.into();
let electrum_url = opts.get_electrum_url();
let data_dir_path = opts.data_dir_path.unwrap_or(DEFAULT_DATA_DIR.to_string());

let lwk_persister = NoPersist::new();
let lwk_persister = FsPersister::new(&data_dir_path, network.into(), &opts.descriptor)?;
let wallet = Arc::new(Mutex::new(LwkWollet::new(
elements_network,
lwk_persister,
opts.descriptor,
)?));

let persister_path = opts.data_dir_path.unwrap_or(DEFAULT_DATA_DIR.to_string());
fs::create_dir_all(&persister_path)?;
fs::create_dir_all(&data_dir_path)?;

let swap_persister = Persister::new(&persister_path);
let swap_persister = Persister::new(&data_dir_path);
swap_persister.init()?;

let wallet = Arc::new(Wallet {
Expand All @@ -85,13 +90,26 @@ impl Wallet {
signer: opts.signer,
active_address: None,
swap_persister,
data_dir_path,
});

Wallet::track_claims(&wallet)?;

Ok(wallet)
}

fn get_descriptor(signer: &SwSigner, network: Network) -> Result<WolletDescriptor> {
let is_mainnet = network == Network::Liquid;
let descriptor_str = singlesig_desc(
signer,
Singlesig::Wpkh,
lwk_common::DescriptorBlindingKey::Slip77,
is_mainnet,
)
.map_err(|e| anyhow!("Invalid descriptor: {e}"))?;
Ok(descriptor_str.parse()?)
}

fn track_claims(self: &Arc<Wallet>) -> Result<()> {
let cloned = self.clone();

Expand Down Expand Up @@ -451,4 +469,18 @@ impl Wallet {

Ok(txid)
}

pub fn empty_wallet_cache(&self) -> Result<()> {
let mut path = PathBuf::from(self.data_dir_path.clone());
path.push(Into::<ElementsNetwork>::into(self.network).as_str());
path.push("enc_cache");

let descriptor = Wallet::get_descriptor(&self.get_signer(), self.network)?;
path.push(DirectoryIdHash::hash(descriptor.to_string().as_bytes()).to_string());

fs::remove_dir_all(&path)?;
fs::create_dir_all(path)?;

Ok(())
}
}

0 comments on commit 04d0c5b

Please sign in to comment.