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

Enable Passphrase for Restore Command #1669

Merged
merged 16 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from 14 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
5 changes: 4 additions & 1 deletion src/subcommand/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ impl Preview {
thread::sleep(Duration::from_millis(50));
}

super::wallet::create::run(options.clone())?;
super::wallet::Wallet::Create(super::wallet::create::Create {
passphrase: "".into(),
})
.run(options.clone())?;

let rpc_client = options.bitcoin_rpc_client_for_wallet_command(false)?;

Expand Down
6 changes: 3 additions & 3 deletions src/subcommand/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use {
};

pub mod balance;
pub(crate) mod create;
pub mod create;
pub(crate) mod inscribe;
pub mod inscriptions;
pub mod outputs;
Expand All @@ -31,7 +31,7 @@ pub(crate) enum Wallet {
#[clap(about = "Get wallet balance")]
Balance,
#[clap(about = "Create new wallet")]
Create,
Create(create::Create),
#[clap(about = "Create inscription")]
Inscribe(inscribe::Inscribe),
#[clap(about = "List wallet inscriptions")]
Expand All @@ -54,7 +54,7 @@ impl Wallet {
pub(crate) fn run(self, options: Options) -> Result {
match self {
Self::Balance => balance::run(options),
Self::Create => create::run(options),
Self::Create(create) => create.run(options),
Self::Inscribe(inscribe) => inscribe.run(options),
Self::Inscriptions => inscriptions::run(options),
Self::Receive => receive::run(options),
Expand Down
29 changes: 22 additions & 7 deletions src/subcommand/wallet/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,32 @@ use super::*;
#[derive(Serialize)]
struct Output {
mnemonic: Mnemonic,
passphrase: Option<String>,
}
raphjaph marked this conversation as resolved.
Show resolved Hide resolved

pub(crate) fn run(options: Options) -> Result {
let mut entropy = [0; 16];
rand::thread_rng().fill_bytes(&mut entropy);
#[derive(Debug, Parser)]
pub(crate) struct Create {
#[clap(long, default_value = "", help = "Use <PASSPHRASE> to derive wallet seed.")]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are using the specific language of BIP 39 here.

pub(crate) passphrase: String,
}

impl Create {
pub(crate) fn run(self, options: Options) -> Result {
let mut entropy = [0; 16];
rand::thread_rng().fill_bytes(&mut entropy);

let mnemonic = Mnemonic::from_entropy(&entropy)?;
let mnemonic = Mnemonic::from_entropy(&entropy)?;

initialize_wallet(&options, mnemonic.to_seed(""))?;
initialize_wallet(
&options,
mnemonic.to_seed(self.passphrase.clone()),
)?;

print_json(Output { mnemonic })?;
print_json(Output {
mnemonic,
passphrase: Some(self.passphrase),
})?;

Ok(())
Ok(())
}
}
7 changes: 6 additions & 1 deletion src/subcommand/wallet/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ use super::*;
pub(crate) struct Restore {
#[clap(help = "Restore wallet from <MNEMONIC>")]
mnemonic: Mnemonic,
#[clap(long, default_value = "", help = "Use <PASSPHRASE> when deriving wallet")]
pub(crate) passphrase: String,
}

impl Restore {
pub(crate) fn run(self, options: Options) -> Result {
initialize_wallet(&options, self.mnemonic.to_seed(""))?;
initialize_wallet(
&options,
self.mnemonic.to_seed(self.passphrase),
)?;

Ok(())
}
Expand Down
28 changes: 28 additions & 0 deletions tests/wallet/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,31 @@ fn restore_generates_same_descriptors() {

assert_eq!(rpc_server.descriptors(), descriptors);
}

#[test]
fn restore_generates_same_descriptors_with_passphrase() {
let passphrase = "foo";
let (mnemonic, descriptors) = {
let rpc_server = test_bitcoincore_rpc::spawn();

let Create { mnemonic } = CommandBuilder::new(["wallet", "create", "--passphrase", passphrase])
.rpc_server(&rpc_server)
.output::<Create>();

(mnemonic, rpc_server.descriptors())
};

let rpc_server = test_bitcoincore_rpc::spawn();

CommandBuilder::new([
"wallet",
"restore",
"--passphrase",
passphrase,
&mnemonic.to_string(),
])
.rpc_server(&rpc_server)
.run();

assert_eq!(rpc_server.descriptors(), descriptors);
}