Skip to content

Commit

Permalink
wallet: add support for initiating from seed file
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Platt authored and torkelrogstad committed Feb 14, 2025
1 parent 263dc83 commit 256a91a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ publish.workspace = true
version.workspace = true

[dependencies]
bdk_wallet = { workspace = true }
bip300301 = { workspace = true }
bip300301_enforcer_lib = { path = "../lib", default-features = false }
bitcoin = { workspace = true }
Expand Down
26 changes: 22 additions & 4 deletions app/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{future::Future, net::SocketAddr, path::Path, time::Duration};

use bdk_wallet::bip39::{Language, Mnemonic};
use bip300301::MainClient;
use clap::Parser;
use either::Either;
Expand Down Expand Up @@ -539,11 +540,28 @@ async fn main() -> Result<()> {
)
.await?;

if !wallet.is_initialized().await && cli.wallet_opts.auto_create {
let (mnemonic, auto_create) = match (
cli.wallet_opts.mnemonic_path.clone(),
cli.wallet_opts.auto_create,
) {
(Some(mnemonic_path), _) => {
tracing::debug!("Reading mnemonic from file: {}", mnemonic_path.display());

let mnemonic_str = std::fs::read_to_string(mnemonic_path)
.map_err(|err| miette!("failed to read mnemonic file: {}", err))?;

let mnemonic = Mnemonic::parse_in(Language::English, &mnemonic_str)
.map_err(|err| miette!("invalid mnemonic: {}", err))?;

(Some(mnemonic), true)
}
(_, true) => (None, true),
_ => (None, false),
};

if !wallet.is_initialized().await && auto_create {
tracing::info!("auto-creating new wallet");
let mnemonic = None;
let password = None;
wallet.create_wallet(mnemonic, password).await?;
wallet.create_wallet(mnemonic, None).await?;
}

Either::Right(wallet)
Expand Down
10 changes: 9 additions & 1 deletion lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,11 @@ pub enum WalletSyncSource {
pub struct WalletConfig {
/// If no existing wallet is found, automatically create and load
/// a new, unencrypted wallet from a randomly generated BIP39 mnemonic.
#[arg(long = "wallet-auto-create", default_value_t = false)]
#[arg(
long = "wallet-auto-create",
default_value_t = false,
conflicts_with = "wallet-seed-file"
)]
pub auto_create: bool,
/// URL of the Esplora server to use for the wallet.
///
Expand Down Expand Up @@ -271,6 +275,10 @@ pub struct WalletConfig {
/// The source of the wallet sync.
#[arg(long = "wallet-sync-source", default_value_t = WalletSyncSource::Electrum, value_enum)]
pub sync_source: WalletSyncSource,

/// Path to a file containing exactly 12 space-separated BIP39 mnemonic words.
#[arg(long = "wallet-seed-file", conflicts_with = "wallet-auto-create")]
pub mnemonic_path: Option<PathBuf>,
}

const DEFAULT_SERVE_RPC_ADDR: SocketAddr =
Expand Down
2 changes: 1 addition & 1 deletion lib/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ use crate::{
mod cusf_block_producer;
pub mod error;
mod mine;
mod mnemonic;
pub mod mnemonic;
mod sync;
mod util;

Expand Down

0 comments on commit 256a91a

Please sign in to comment.