Skip to content

Commit 47d610c

Browse files
authored
Merge pull request #117 from Ximik/recover_wallet
Fix wallets recovered via mnemonic
2 parents 21a87cc + 92b32af commit 47d610c

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

client/src/config.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rand::{
1515
{thread_rng, Rng},
1616
};
1717
use serde::Deserialize;
18-
use spaces_protocol::bitcoin::Network;
18+
use spaces_protocol::{bitcoin::Network, constants::ChainAnchor};
1919

2020
use crate::{
2121
auth::{auth_token_from_cookie, auth_token_from_creds},
@@ -117,6 +117,16 @@ impl ExtendedNetwork {
117117
_ => Err(()),
118118
}
119119
}
120+
121+
pub fn genesis(&self) -> ChainAnchor {
122+
match self {
123+
ExtendedNetwork::Testnet => ChainAnchor::TESTNET(),
124+
ExtendedNetwork::Testnet4 => ChainAnchor::TESTNET4(),
125+
ExtendedNetwork::Regtest => ChainAnchor::REGTEST(),
126+
ExtendedNetwork::Mainnet => ChainAnchor::MAINNET(),
127+
_ => panic!("unsupported network"),
128+
}
129+
}
120130
}
121131

122132
impl Args {

client/src/rpc.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -513,14 +513,13 @@ impl WalletManager {
513513
.map_err(|_| anyhow!("Mnemonic generation error"))?;
514514

515515
let start_block = self.get_wallet_start_block(client).await?;
516-
self.setup_new_wallet(name.to_string(), mnemonic.to_string(), start_block)?;
516+
self.setup_new_wallet(name.to_string(), mnemonic.to_string(), Some(start_block.height))?;
517517
self.load_wallet(name).await?;
518518
Ok(mnemonic.to_string())
519519
}
520520

521-
pub async fn recover_wallet(&self, client: &reqwest::Client, name: &str, mnemonic: &str) -> anyhow::Result<()> {
522-
let start_block = self.get_wallet_start_block(client).await?;
523-
self.setup_new_wallet(name.to_string(), mnemonic.to_string(), start_block)?;
521+
pub async fn recover_wallet(&self, name: &str, mnemonic: &str) -> anyhow::Result<()> {
522+
self.setup_new_wallet(name.to_string(), mnemonic.to_string(), None)?;
524523
self.load_wallet(name).await?;
525524
Ok(())
526525
}
@@ -529,14 +528,14 @@ impl WalletManager {
529528
&self,
530529
name: String,
531530
mnemonic: String,
532-
start_block: BlockId,
531+
start_block_height: Option<u32>,
533532
) -> anyhow::Result<()> {
534533
let wallet_path = self.data_dir.join(&name);
535534
if wallet_path.exists() {
536535
return Err(anyhow!(format!("Wallet `{}` already exists", name)));
537536
}
538537

539-
let export = self.wallet_from_mnemonic(name.clone(), mnemonic, start_block)?;
538+
let export = self.wallet_from_mnemonic(name.clone(), mnemonic, start_block_height)?;
540539
fs::create_dir_all(&wallet_path)?;
541540
let wallet_export_path = wallet_path.join("wallet.json");
542541
let mut file = fs::File::create(wallet_export_path)?;
@@ -548,7 +547,7 @@ impl WalletManager {
548547
&self,
549548
name: String,
550549
mnemonic: String,
551-
start_block: BlockId,
550+
start_block_height: Option<u32>,
552551
) -> anyhow::Result<WalletExport> {
553552
let (network, _) = self.fallback_network();
554553
let xpriv = Self::descriptor_from_mnemonic(network, &mnemonic)?;
@@ -557,8 +556,14 @@ impl WalletManager {
557556
let tmp = bdk::Wallet::create(external, internal)
558557
.network(network)
559558
.create_wallet_no_persist()?;
559+
560+
let start_block_height = match start_block_height {
561+
Some(height) => height,
562+
None => self.network.genesis().height,
563+
};
564+
560565
let export =
561-
WalletExport::export_wallet(&tmp, &name, start_block.height).map_err(|e| anyhow!(e))?;
566+
WalletExport::export_wallet(&tmp, &name, start_block_height).map_err(|e| anyhow!(e))?;
562567

563568
Ok(export)
564569
}
@@ -946,7 +951,7 @@ impl RpcServer for RpcServerImpl {
946951

947952
async fn wallet_recover(&self, name: &str, mnemonic: String) -> Result<(), ErrorObjectOwned> {
948953
self.wallet_manager
949-
.recover_wallet(&self.client, name, &mnemonic)
954+
.recover_wallet(name, &mnemonic)
950955
.await
951956
.map_err(|error| {
952957
ErrorObjectOwned::owned(RPC_WALLET_NOT_LOADED, error.to_string(), None::<String>)

client/src/spaces.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,6 @@ impl Spaced {
257257
}
258258

259259
pub fn genesis(network: ExtendedNetwork) -> ChainAnchor {
260-
match network {
261-
ExtendedNetwork::Testnet => ChainAnchor::TESTNET(),
262-
ExtendedNetwork::Testnet4 => ChainAnchor::TESTNET4(),
263-
ExtendedNetwork::Regtest => ChainAnchor::REGTEST(),
264-
ExtendedNetwork::Mainnet => ChainAnchor::MAINNET(),
265-
_ => panic!("unsupported network"),
266-
}
260+
network.genesis()
267261
}
268262
}

0 commit comments

Comments
 (0)