Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wallet: add support for initiating from seed file #172

Merged
merged 1 commit into from
Feb 14, 2025
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
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 = "mnemonic_path"
)]
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 = "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