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

Fix handling of the optional BIP39 passphrase flag #2489

Merged
merged 5 commits into from
Feb 5, 2024
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
2 changes: 2 additions & 0 deletions .changelog/unreleased/bug-fixes/2489-bip39-optional-derive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Wallet: respect the optional bip39-flag for key derivation.
([\#2489](https://github.com/anoma/namada/pull/2489))
4 changes: 4 additions & 0 deletions crates/apps/src/lib/cli/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ fn shielded_key_derive(
unsafe_dont_encrypt,
derivation_path,
allow_non_compliant,
prompt_bip39_passphrase,
use_device,
..
}: args::KeyDerive,
Expand All @@ -214,6 +215,7 @@ fn shielded_key_derive(
alias_force,
derivation_path,
None,
prompt_bip39_passphrase,
encryption_password,
)
.unwrap_or_else(|| {
Expand Down Expand Up @@ -440,6 +442,7 @@ async fn transparent_key_and_address_derive(
unsafe_dont_encrypt,
derivation_path,
allow_non_compliant,
prompt_bip39_passphrase,
use_device,
..
}: args::KeyDerive,
Expand Down Expand Up @@ -470,6 +473,7 @@ async fn transparent_key_and_address_derive(
alias_force,
derivation_path,
None,
prompt_bip39_passphrase,
encryption_password,
)
.unwrap_or_else(|| {
Expand Down
19 changes: 17 additions & 2 deletions crates/sdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,20 @@ impl<U: WalletIo> Wallet<U> {
alias_force: bool,
derivation_path: DerivationPath,
mnemonic_passphrase: Option<(Mnemonic, Zeroizing<String>)>,
prompt_bip39_passphrase: bool,
password: Option<Zeroizing<String>>,
) -> Option<(String, ExtendedSpendingKey)> {
let (mnemonic, passphrase) =
if let Some(mnemonic_passphrase) = mnemonic_passphrase {
mnemonic_passphrase
} else {
(U::read_mnemonic_code()?, U::read_mnemonic_passphrase(false))
let mnemonic = U::read_mnemonic_code()?;
let passphrase = if prompt_bip39_passphrase {
U::read_mnemonic_passphrase(false)
} else {
Zeroizing::default()
};
(mnemonic, passphrase)
};
let seed = Seed::new(&mnemonic, &passphrase);
let spend_key =
Expand All @@ -545,20 +552,28 @@ impl<U: WalletIo> Wallet<U> {
/// provided, will prompt for password from stdin.
/// Stores the key in decrypted key cache and returns the alias of the key
/// and a reference-counting pointer to the key.
#[allow(clippy::too_many_arguments)]
pub fn derive_store_key_from_mnemonic_code(
&mut self,
scheme: SchemeType,
alias: Option<String>,
alias_force: bool,
derivation_path: DerivationPath,
mnemonic_passphrase: Option<(Mnemonic, Zeroizing<String>)>,
prompt_bip39_passphrase: bool,
password: Option<Zeroizing<String>>,
) -> Option<(String, common::SecretKey)> {
let (mnemonic, passphrase) =
if let Some(mnemonic_passphrase) = mnemonic_passphrase {
mnemonic_passphrase
} else {
(U::read_mnemonic_code()?, U::read_mnemonic_passphrase(false))
let mnemonic = U::read_mnemonic_code()?;
let passphrase = if prompt_bip39_passphrase {
U::read_mnemonic_passphrase(false)
} else {
Zeroizing::default()
};
(mnemonic, passphrase)
};
let seed = Seed::new(&mnemonic, &passphrase);
let sk = derive_hd_secret_key(
Expand Down
16 changes: 9 additions & 7 deletions crates/sdk/src/wallet/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ impl Store {
.unwrap_or_else(|| Address::Implicit(ImplicitAddress(pkh.clone())));
if !force {
if self.pkhs.contains_key(&pkh) {
println!("The key already exists.");
let alias = self.pkhs.get(&pkh).unwrap();
println!("The key already exists with alias {}", alias);
return None;
} else if let Some(alias) = self.addresses.get_by_right(&address) {
println!(
Expand All @@ -324,7 +325,7 @@ impl Store {

// abort if the alias is reserved
if Alias::is_reserved(&alias).is_some() {
println!("The alias {} is reserved", alias);
println!("The alias {} is reserved.", alias);
return None;
}

Expand Down Expand Up @@ -371,7 +372,7 @@ impl Store {
) -> Option<Alias> {
// abort if the alias is reserved
if Alias::is_reserved(&alias).is_some() {
println!("The alias {} is reserved", alias);
println!("The alias {} is reserved.", alias);
return None;
}
// abort if the alias is empty
Expand Down Expand Up @@ -412,7 +413,7 @@ impl Store {
) -> Option<Alias> {
// abort if the alias is reserved
if Alias::is_reserved(&alias).is_some() {
println!("The alias {} is reserved", alias);
println!("The alias {} is reserved.", alias);
return None;
}

Expand Down Expand Up @@ -449,7 +450,8 @@ impl Store {
.unwrap_or_else(|| Address::Implicit(ImplicitAddress(pkh.clone())));
if !force {
if self.pkhs.contains_key(&pkh) {
println!("The key already exists.");
let alias = self.pkhs.get(&pkh).unwrap();
println!("The key already exists with alias {}", alias);
return None;
} else if let Some(alias) = self.addresses.get_by_right(&address) {
println!(
Expand Down Expand Up @@ -496,7 +498,7 @@ impl Store {
) -> Option<Alias> {
// abort if the alias is reserved
if Alias::is_reserved(&alias).is_some() {
println!("The alias {} is reserved", alias);
println!("The alias {} is reserved.", alias);
return None;
}

Expand Down Expand Up @@ -534,7 +536,7 @@ impl Store {
) -> Option<Alias> {
// abort if the alias is reserved
if Alias::is_reserved(&alias).is_some() {
println!("The alias {} is reserved", alias);
println!("The alias {} is reserved.", alias);
return None;
}
// abort if the address already exists in the wallet
Expand Down
Loading