Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion client/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use rand::{
{thread_rng, Rng},
};
use serde::Deserialize;
use spaces_protocol::bitcoin::Network;
use spaces_protocol::{bitcoin::Network, constants::ChainAnchor};

use crate::{
auth::{auth_token_from_cookie, auth_token_from_creds},
Expand Down Expand Up @@ -117,6 +117,16 @@ impl ExtendedNetwork {
_ => Err(()),
}
}

pub fn genesis(&self) -> ChainAnchor {
match self {
ExtendedNetwork::Testnet => ChainAnchor::TESTNET(),
ExtendedNetwork::Testnet4 => ChainAnchor::TESTNET4(),
ExtendedNetwork::Regtest => ChainAnchor::REGTEST(),
ExtendedNetwork::Mainnet => ChainAnchor::MAINNET(),
_ => panic!("unsupported network"),
}
}
}

impl Args {
Expand Down
23 changes: 14 additions & 9 deletions client/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,14 +513,13 @@ impl WalletManager {
.map_err(|_| anyhow!("Mnemonic generation error"))?;

let start_block = self.get_wallet_start_block(client).await?;
self.setup_new_wallet(name.to_string(), mnemonic.to_string(), start_block)?;
self.setup_new_wallet(name.to_string(), mnemonic.to_string(), Some(start_block.height))?;
self.load_wallet(name).await?;
Ok(mnemonic.to_string())
}

pub async fn recover_wallet(&self, client: &reqwest::Client, name: &str, mnemonic: &str) -> anyhow::Result<()> {
let start_block = self.get_wallet_start_block(client).await?;
self.setup_new_wallet(name.to_string(), mnemonic.to_string(), start_block)?;
pub async fn recover_wallet(&self, name: &str, mnemonic: &str) -> anyhow::Result<()> {
self.setup_new_wallet(name.to_string(), mnemonic.to_string(), None)?;
self.load_wallet(name).await?;
Ok(())
}
Expand All @@ -529,14 +528,14 @@ impl WalletManager {
&self,
name: String,
mnemonic: String,
start_block: BlockId,
start_block_height: Option<u32>,
) -> anyhow::Result<()> {
let wallet_path = self.data_dir.join(&name);
if wallet_path.exists() {
return Err(anyhow!(format!("Wallet `{}` already exists", name)));
}

let export = self.wallet_from_mnemonic(name.clone(), mnemonic, start_block)?;
let export = self.wallet_from_mnemonic(name.clone(), mnemonic, start_block_height)?;
fs::create_dir_all(&wallet_path)?;
let wallet_export_path = wallet_path.join("wallet.json");
let mut file = fs::File::create(wallet_export_path)?;
Expand All @@ -548,7 +547,7 @@ impl WalletManager {
&self,
name: String,
mnemonic: String,
start_block: BlockId,
start_block_height: Option<u32>,
) -> anyhow::Result<WalletExport> {
let (network, _) = self.fallback_network();
let xpriv = Self::descriptor_from_mnemonic(network, &mnemonic)?;
Expand All @@ -557,8 +556,14 @@ impl WalletManager {
let tmp = bdk::Wallet::create(external, internal)
.network(network)
.create_wallet_no_persist()?;

let start_block_height = match start_block_height {
Some(height) => height,
None => self.network.genesis().height,
};

let export =
WalletExport::export_wallet(&tmp, &name, start_block.height).map_err(|e| anyhow!(e))?;
WalletExport::export_wallet(&tmp, &name, start_block_height).map_err(|e| anyhow!(e))?;

Ok(export)
}
Expand Down Expand Up @@ -946,7 +951,7 @@ impl RpcServer for RpcServerImpl {

async fn wallet_recover(&self, name: &str, mnemonic: String) -> Result<(), ErrorObjectOwned> {
self.wallet_manager
.recover_wallet(&self.client, name, &mnemonic)
.recover_wallet(name, &mnemonic)
.await
.map_err(|error| {
ErrorObjectOwned::owned(RPC_WALLET_NOT_LOADED, error.to_string(), None::<String>)
Expand Down
8 changes: 1 addition & 7 deletions client/src/spaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,6 @@ impl Spaced {
}

pub fn genesis(network: ExtendedNetwork) -> ChainAnchor {
match network {
ExtendedNetwork::Testnet => ChainAnchor::TESTNET(),
ExtendedNetwork::Testnet4 => ChainAnchor::TESTNET4(),
ExtendedNetwork::Regtest => ChainAnchor::REGTEST(),
ExtendedNetwork::Mainnet => ChainAnchor::MAINNET(),
_ => panic!("unsupported network"),
}
network.genesis()
}
}
Loading