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 7 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
3 changes: 2 additions & 1 deletion src/subcommand/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ impl Preview {
thread::sleep(Duration::from_millis(50));
}

super::wallet::create::run(options.clone())?;
super::wallet::Wallet::Create(super::wallet::create::Create { passphrase: None })
.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
28 changes: 21 additions & 7 deletions src/subcommand/wallet/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,31 @@ use super::*;
#[derive(Serialize)]
struct Output {
mnemonic: Mnemonic,
passphrase: Option<String>,
}
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Debug, Parser)]
pub(crate) struct Create {
#[clap(long, help = "Use <PASSPHRASE> when deriving wallet")]
pub(crate) 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);
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().unwrap_or("".into())),
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
)?;

print_json(Output { mnemonic })?;
print_json(Output {
mnemonic,
passphrase: 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, help = "Use <PASSPHRASE> when deriving wallet")]
pub(crate) passphrase: Option<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.unwrap_or("".into())),
)?;

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 = "unittest";
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
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);
}