Skip to content

Commit ad52849

Browse files
committed
Add util function to rebuild wallet
1 parent 9020ae7 commit ad52849

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

wallet/src/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ impl Iterator for BuilderIterator<'_> {
460460
let mut amounts = Vec::with_capacity(params.opens.len());
461461

462462
for req in params.opens {
463-
let tap = Builder::create_open_tap_data(self.wallet.network, &req.name)
463+
let tap = Builder::create_open_tap_data(self.wallet.config.network, &req.name)
464464
.context("could not initialize tap data for name");
465465
if tap.is_err() {
466466
return Some(Err(tap.unwrap_err()));
@@ -472,7 +472,7 @@ impl Iterator for BuilderIterator<'_> {
472472
let mut contexts = Vec::with_capacity(params.executes.len());
473473
for execute in params.executes {
474474
let signing_info = SpaceScriptSigningInfo::new(
475-
self.wallet.network,
475+
self.wallet.config.network,
476476
execute.script.to_nop_script(),
477477
);
478478
if signing_info.is_err() {

wallet/src/lib.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ const WALLET_SPACE_MAGIC: &[u8; 12] = b"WALLET_SPACE";
5252
const WALLET_COIN_MAGIC: &[u8; 12] = b"WALLET_COINS";
5353

5454
pub struct SpacesWallet {
55-
pub name: String,
56-
pub data_dir: PathBuf,
57-
pub start_block: u32,
58-
pub network: Network,
55+
pub config: WalletConfig,
5956
pub coins: bdk_wallet::wallet::Wallet,
6057
pub spaces: bdk_wallet::wallet::Wallet,
6158
pub coins_db: bdk_file_store::Store<ChangeSet>,
@@ -169,12 +166,12 @@ impl WalletExport {
169166

170167
impl SpacesWallet {
171168
pub fn name(&self) -> &str {
172-
&self.name
169+
&self.config.name
173170
}
174171

175172
pub fn new(config: WalletConfig) -> anyhow::Result<Self> {
176173
if !config.data_dir.exists() {
177-
std::fs::create_dir_all(config.data_dir.clone())?;
174+
fs::create_dir_all(config.data_dir.clone())?;
178175
}
179176

180177
let spaces_path = config.data_dir.join("spaces.db");
@@ -212,10 +209,7 @@ impl SpacesWallet {
212209
)?;
213210

214211
let wallet = Self {
215-
name: config.name,
216-
start_block: config.start_block,
217-
data_dir: config.data_dir,
218-
network: config.network,
212+
config,
219213
coins: coins_wallet,
220214
spaces: spaces_wallet,
221215
coins_db,
@@ -226,6 +220,15 @@ impl SpacesWallet {
226220
Ok(wallet)
227221
}
228222

223+
pub fn rebuild(self) -> anyhow::Result<Self> {
224+
let config = self.config;
225+
drop(self.spaces_db);
226+
drop(self.coins_db);
227+
fs::remove_file(config.data_dir.join("spaces.db"))?;
228+
fs::remove_file(config.data_dir.join("coins.db"))?;
229+
Ok(SpacesWallet::new(config)?)
230+
}
231+
229232
pub fn get_info(&self) -> WalletInfo {
230233
let mut descriptors = Vec::with_capacity(4);
231234

@@ -263,8 +266,8 @@ impl SpacesWallet {
263266
});
264267

265268
WalletInfo {
266-
label: self.name.clone(),
267-
start_block: self.start_block,
269+
label: self.config.name.clone(),
270+
start_block: self.config.start_block,
268271
tip: self.coins.local_chain().tip().height(),
269272
descriptors,
270273
}
@@ -297,8 +300,8 @@ impl SpacesWallet {
297300
WalletExport {
298301
descriptor,
299302
spaces_descriptor,
300-
block_height: self.start_block,
301-
label: self.name.clone(),
303+
block_height: self.config.start_block,
304+
label: self.config.name.clone(),
302305
}
303306
}
304307

@@ -603,14 +606,14 @@ impl SpacesWallet {
603606
}
604607

605608
fn get_signing_info(&self, script: &ScriptBuf) -> Option<Vec<u8>> {
606-
let script_info_dir = self.data_dir.join("script_solutions");
609+
let script_info_dir = self.config.data_dir.join("script_solutions");
607610
let filename = hex::encode(script.as_bytes());
608611
let file_path = script_info_dir.join(filename);
609612
std::fs::read(file_path).ok()
610613
}
611614

612615
fn save_signing_info(&self, script: ScriptBuf, raw: Vec<u8>) -> anyhow::Result<()> {
613-
let script_info_dir = self.data_dir.join("script_solutions");
616+
let script_info_dir = self.config.data_dir.join("script_solutions");
614617
std::fs::create_dir_all(&script_info_dir)
615618
.context("could not create script_info directory")?;
616619
let filename = hex::encode(script.as_bytes());
@@ -620,7 +623,7 @@ impl SpacesWallet {
620623
}
621624

622625
fn clear_unused_signing_info(&self) {
623-
let script_info_dir = self.data_dir.join("script_solutions");
626+
let script_info_dir = self.config.data_dir.join("script_solutions");
624627
let one_week_ago = SystemTime::now() - Duration::from_secs(7 * 24 * 60 * 60);
625628

626629
let entries = match fs::read_dir(&script_info_dir) {

0 commit comments

Comments
 (0)