From 7db376637eaeb4d17a6e372310acd1ed6a5e1bfb Mon Sep 17 00:00:00 2001 From: Psifour Date: Sat, 11 Feb 2023 01:24:00 -0600 Subject: [PATCH 01/11] Implement Restore Passphrase --- src/subcommand/wallet/restore.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/subcommand/wallet/restore.rs b/src/subcommand/wallet/restore.rs index 508effeb2a..86030aca14 100644 --- a/src/subcommand/wallet/restore.rs +++ b/src/subcommand/wallet/restore.rs @@ -4,11 +4,17 @@ use super::*; pub(crate) struct Restore { #[clap(help = "Restore wallet from ")] mnemonic: Mnemonic, + #[clap( + long, + short = 'p', + help = "Use when deriving wallet" + )] + pub(crate) passphrase: Option, } 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(()) } From ac377c27a7946ad2dea4e09ce7056dc81f566dca Mon Sep 17 00:00:00 2001 From: Psifour Date: Sat, 11 Feb 2023 23:44:10 -0600 Subject: [PATCH 02/11] Linting --- src/subcommand/wallet/restore.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/subcommand/wallet/restore.rs b/src/subcommand/wallet/restore.rs index 86030aca14..39c6d145aa 100644 --- a/src/subcommand/wallet/restore.rs +++ b/src/subcommand/wallet/restore.rs @@ -4,17 +4,16 @@ use super::*; pub(crate) struct Restore { #[clap(help = "Restore wallet from ")] mnemonic: Mnemonic, - #[clap( - long, - short = 'p', - help = "Use when deriving wallet" - )] + #[clap(long, short = 'p', help = "Use when deriving wallet")] pub(crate) passphrase: Option, } impl Restore { pub(crate) fn run(self, options: Options) -> Result { - initialize_wallet(&options, self.mnemonic.to_seed(self.passphrase.unwrap_or("".into())))?; + initialize_wallet( + &options, + self.mnemonic.to_seed(self.passphrase.unwrap_or("".into())), + )?; Ok(()) } From 82224232e3d684693a0f3670496235807ec78026 Mon Sep 17 00:00:00 2001 From: Psifour Date: Tue, 14 Feb 2023 00:52:53 -0600 Subject: [PATCH 03/11] Finalize Passphrase Support --- src/subcommand/preview.rs | 3 ++- src/subcommand/wallet.rs | 6 +++--- src/subcommand/wallet/create.rs | 28 +++++++++++++++++++++------- src/subcommand/wallet/restore.rs | 2 +- tests/wallet/restore.rs | 28 ++++++++++++++++++++++++++++ 5 files changed, 55 insertions(+), 12 deletions(-) diff --git a/src/subcommand/preview.rs b/src/subcommand/preview.rs index aa3aa08413..2383d31067 100644 --- a/src/subcommand/preview.rs +++ b/src/subcommand/preview.rs @@ -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)?; diff --git a/src/subcommand/wallet.rs b/src/subcommand/wallet.rs index 18a46ef04c..0404f2ad24 100644 --- a/src/subcommand/wallet.rs +++ b/src/subcommand/wallet.rs @@ -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; @@ -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")] @@ -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), diff --git a/src/subcommand/wallet/create.rs b/src/subcommand/wallet/create.rs index 0f83056e6b..859dfe593f 100644 --- a/src/subcommand/wallet/create.rs +++ b/src/subcommand/wallet/create.rs @@ -3,17 +3,31 @@ use super::*; #[derive(Serialize)] struct Output { mnemonic: Mnemonic, + passphrase: Option, +} +#[derive(Debug, Parser)] +pub(crate) struct Create { + #[clap(long, help = "Use when deriving wallet")] + pub(crate) passphrase: Option, } -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())), + )?; - print_json(Output { mnemonic })?; + print_json(Output { + mnemonic, + passphrase: self.passphrase, + })?; - Ok(()) + Ok(()) + } } diff --git a/src/subcommand/wallet/restore.rs b/src/subcommand/wallet/restore.rs index 39c6d145aa..f1ba8445e9 100644 --- a/src/subcommand/wallet/restore.rs +++ b/src/subcommand/wallet/restore.rs @@ -4,7 +4,7 @@ use super::*; pub(crate) struct Restore { #[clap(help = "Restore wallet from ")] mnemonic: Mnemonic, - #[clap(long, short = 'p', help = "Use when deriving wallet")] + #[clap(long, help = "Use when deriving wallet")] pub(crate) passphrase: Option, } diff --git a/tests/wallet/restore.rs b/tests/wallet/restore.rs index cee6122125..f84515f724 100644 --- a/tests/wallet/restore.rs +++ b/tests/wallet/restore.rs @@ -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"; + let (mnemonic, descriptors) = { + let rpc_server = test_bitcoincore_rpc::spawn(); + + let Create { mnemonic } = CommandBuilder::new(["wallet", "create", "--passphrase", passphrase]) + .rpc_server(&rpc_server) + .output::(); + + (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); +} From 49e6895eefdfae0116a1ea9257bde149af00d0ba Mon Sep 17 00:00:00 2001 From: raphjaph Date: Fri, 17 Feb 2023 21:58:16 +0100 Subject: [PATCH 04/11] Update src/subcommand/wallet/create.rs --- src/subcommand/wallet/create.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/subcommand/wallet/create.rs b/src/subcommand/wallet/create.rs index 859dfe593f..4b77085c12 100644 --- a/src/subcommand/wallet/create.rs +++ b/src/subcommand/wallet/create.rs @@ -5,6 +5,7 @@ struct Output { mnemonic: Mnemonic, passphrase: Option, } + #[derive(Debug, Parser)] pub(crate) struct Create { #[clap(long, help = "Use when deriving wallet")] From 2e2ca16d6b0067f9297b5e042466e7a90d05391c Mon Sep 17 00:00:00 2001 From: raphjaph Date: Fri, 17 Feb 2023 22:02:19 +0100 Subject: [PATCH 05/11] Update src/subcommand/wallet/create.rs --- src/subcommand/wallet/create.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/subcommand/wallet/create.rs b/src/subcommand/wallet/create.rs index 4b77085c12..85257914f7 100644 --- a/src/subcommand/wallet/create.rs +++ b/src/subcommand/wallet/create.rs @@ -8,8 +8,8 @@ struct Output { #[derive(Debug, Parser)] pub(crate) struct Create { - #[clap(long, help = "Use when deriving wallet")] - pub(crate) passphrase: Option, + #[clap(long, default_value = "", help = "Use to derive wallet seed.")] + pub(crate) passphrase: String, } impl Create { From d9aacfac536a2a06b3e56bdbb7440c4ba23c29f5 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Fri, 17 Feb 2023 22:04:18 +0100 Subject: [PATCH 06/11] Update src/subcommand/wallet/create.rs --- src/subcommand/wallet/create.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/subcommand/wallet/create.rs b/src/subcommand/wallet/create.rs index 85257914f7..c957d5ec65 100644 --- a/src/subcommand/wallet/create.rs +++ b/src/subcommand/wallet/create.rs @@ -21,7 +21,7 @@ impl Create { initialize_wallet( &options, - mnemonic.to_seed(self.passphrase.clone().unwrap_or("".into())), + mnemonic.to_seed(self.passphrase), )?; print_json(Output { From da9e52eece03b32ede059e5d7c61a5cac87abe78 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Fri, 17 Feb 2023 22:21:17 +0100 Subject: [PATCH 07/11] quick fix --- Cargo.lock | 1 + Cargo.toml | 1 + src/subcommand/preview.rs | 6 ++++-- src/subcommand/wallet/create.rs | 4 ++-- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 13ca03b08d..cebdc5db6f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2189,6 +2189,7 @@ dependencies = [ "ord-bitcoincore-rpc", "pretty_assertions", "pulldown-cmark", + "rand 0.8.5", "redb", "regex", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index 945ed67866..6570704a3c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,6 +39,7 @@ mime_guess = "2.0.4" miniscript = "9.0.0" mp4 = "0.13.0" ord-bitcoincore-rpc = "0.16.5" +rand = "0.8.5" redb = "0.13.0" regex = "1.6.0" rss = "2.0.1" diff --git a/src/subcommand/preview.rs b/src/subcommand/preview.rs index 2383d31067..1b1cd09104 100644 --- a/src/subcommand/preview.rs +++ b/src/subcommand/preview.rs @@ -61,8 +61,10 @@ impl Preview { thread::sleep(Duration::from_millis(50)); } - super::wallet::Wallet::Create(super::wallet::create::Create { passphrase: None }) - .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)?; diff --git a/src/subcommand/wallet/create.rs b/src/subcommand/wallet/create.rs index c957d5ec65..a40b2e02f9 100644 --- a/src/subcommand/wallet/create.rs +++ b/src/subcommand/wallet/create.rs @@ -21,12 +21,12 @@ impl Create { initialize_wallet( &options, - mnemonic.to_seed(self.passphrase), + mnemonic.to_seed(self.passphrase.clone()), )?; print_json(Output { mnemonic, - passphrase: self.passphrase, + passphrase: Some(self.passphrase), })?; Ok(()) From 1d31183abfbb88dda2319f36c85ccc1fc68a9b23 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Fri, 17 Feb 2023 22:21:45 +0100 Subject: [PATCH 08/11] quick fix --- Cargo.lock | 1 - Cargo.toml | 1 - 2 files changed, 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cebdc5db6f..13ca03b08d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2189,7 +2189,6 @@ dependencies = [ "ord-bitcoincore-rpc", "pretty_assertions", "pulldown-cmark", - "rand 0.8.5", "redb", "regex", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index 6570704a3c..945ed67866 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,7 +39,6 @@ mime_guess = "2.0.4" miniscript = "9.0.0" mp4 = "0.13.0" ord-bitcoincore-rpc = "0.16.5" -rand = "0.8.5" redb = "0.13.0" regex = "1.6.0" rss = "2.0.1" From 1568f99abb92d2b679a49adb8543887f8109e06c Mon Sep 17 00:00:00 2001 From: raphjaph Date: Fri, 17 Feb 2023 22:23:30 +0100 Subject: [PATCH 09/11] quick fix --- src/subcommand/wallet/restore.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/subcommand/wallet/restore.rs b/src/subcommand/wallet/restore.rs index f1ba8445e9..6a1f644930 100644 --- a/src/subcommand/wallet/restore.rs +++ b/src/subcommand/wallet/restore.rs @@ -4,15 +4,15 @@ use super::*; pub(crate) struct Restore { #[clap(help = "Restore wallet from ")] mnemonic: Mnemonic, - #[clap(long, help = "Use when deriving wallet")] - pub(crate) passphrase: Option, + #[clap(long, default_value = "", help = "Use when deriving wallet")] + pub(crate) passphrase: String, } impl Restore { pub(crate) fn run(self, options: Options) -> Result { initialize_wallet( &options, - self.mnemonic.to_seed(self.passphrase.unwrap_or("".into())), + self.mnemonic.to_seed(self.passphrase), )?; Ok(()) From d5e3ce16837b561a28e6c9f34efb5c4caa23ffec Mon Sep 17 00:00:00 2001 From: raphjaph Date: Fri, 17 Feb 2023 22:24:30 +0100 Subject: [PATCH 10/11] Update tests/wallet/restore.rs --- tests/wallet/restore.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/wallet/restore.rs b/tests/wallet/restore.rs index f84515f724..fa91b6edf0 100644 --- a/tests/wallet/restore.rs +++ b/tests/wallet/restore.rs @@ -23,7 +23,7 @@ fn restore_generates_same_descriptors() { #[test] fn restore_generates_same_descriptors_with_passphrase() { - let passphrase = "unittest"; + let passphrase = "foo"; let (mnemonic, descriptors) = { let rpc_server = test_bitcoincore_rpc::spawn(); From bb36e83f06d43c0422fe124a6d9bc1de2a2d2a9a Mon Sep 17 00:00:00 2001 From: raphjaph Date: Fri, 17 Feb 2023 23:02:10 +0100 Subject: [PATCH 11/11] quick fix --- src/subcommand/wallet/create.rs | 11 ++++++----- src/subcommand/wallet/restore.rs | 11 ++++++----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/subcommand/wallet/create.rs b/src/subcommand/wallet/create.rs index a40b2e02f9..a236f8ee80 100644 --- a/src/subcommand/wallet/create.rs +++ b/src/subcommand/wallet/create.rs @@ -8,7 +8,11 @@ struct Output { #[derive(Debug, Parser)] pub(crate) struct Create { - #[clap(long, default_value = "", help = "Use to derive wallet seed.")] + #[clap( + long, + default_value = "", + help = "Use to derive wallet seed." + )] pub(crate) passphrase: String, } @@ -19,10 +23,7 @@ impl Create { let mnemonic = Mnemonic::from_entropy(&entropy)?; - initialize_wallet( - &options, - mnemonic.to_seed(self.passphrase.clone()), - )?; + initialize_wallet(&options, mnemonic.to_seed(self.passphrase.clone()))?; print_json(Output { mnemonic, diff --git a/src/subcommand/wallet/restore.rs b/src/subcommand/wallet/restore.rs index 6a1f644930..3e54bfc016 100644 --- a/src/subcommand/wallet/restore.rs +++ b/src/subcommand/wallet/restore.rs @@ -4,16 +4,17 @@ use super::*; pub(crate) struct Restore { #[clap(help = "Restore wallet from ")] mnemonic: Mnemonic, - #[clap(long, default_value = "", help = "Use when deriving wallet")] + #[clap( + long, + default_value = "", + help = "Use when deriving wallet" + )] pub(crate) passphrase: String, } impl Restore { pub(crate) fn run(self, options: Options) -> Result { - initialize_wallet( - &options, - self.mnemonic.to_seed(self.passphrase), - )?; + initialize_wallet(&options, self.mnemonic.to_seed(self.passphrase))?; Ok(()) }