Skip to content

Commit

Permalink
Allow supplying passphrase for ord wallet create and `ord wallet re…
Browse files Browse the repository at this point in the history
…store` (ordinals#1669)
  • Loading branch information
Psifour authored Feb 17, 2023
1 parent 9c27697 commit 23b4900
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 12 deletions.
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
30 changes: 23 additions & 7 deletions src/subcommand/wallet/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,33 @@ use super::*;
#[derive(Serialize)]
struct Output {
mnemonic: Mnemonic,
passphrase: Option<String>,
}

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."
)]
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(())
}
}
8 changes: 7 additions & 1 deletion src/subcommand/wallet/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ 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);
}

0 comments on commit 23b4900

Please sign in to comment.