Skip to content

Commit

Permalink
Atomic derive_store_spending_key_from_mnemonic_code
Browse files Browse the repository at this point in the history
  • Loading branch information
karbyshev committed Mar 7, 2024
1 parent 3f8ec1e commit 1f3edc7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
1 change: 1 addition & 0 deletions crates/apps/src/lib/cli/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ fn shielded_key_derive(
prompt_bip39_passphrase,
encryption_password,
)
.expect("Failed to update the wallet storage.")
.unwrap_or_else(|| {
edisplay_line!(io, "Failed to derive a key.");
display_line!(io, "No changes are persisted. Exiting.");
Expand Down
70 changes: 45 additions & 25 deletions crates/sdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,26 +843,19 @@ impl<U: WalletIo> Wallet<U> {
Some(derive_hd_secret_key(
scheme,
seed.as_bytes(),
derivation_path.clone(),
derivation_path,
))
}

/// Restore a spending key from the user mnemonic code (read from stdin)
/// using a given ZIP32 derivation path and insert it into the store with
/// the provided alias, converted to lower case.
/// The key is encrypted with the provided password. If no password
/// 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.
pub fn derive_store_spending_key_from_mnemonic_code(
&mut self,
alias: String,
alias_force: bool,
/// XXX OK
/// Derive a spending key from the user mnemonic code (read from stdin)
/// using a given ZIP32 derivation path.
pub fn derive_spending_key_from_mnemonic_code(
&self,
derivation_path: DerivationPath,
mnemonic_passphrase: Option<(Mnemonic, Zeroizing<String>)>,
prompt_bip39_passphrase: bool,
password: Option<Zeroizing<String>>,
) -> Option<(String, ExtendedSpendingKey)> {
) -> Option<ExtendedSpendingKey> {
let (mnemonic, passphrase) =
if let Some(mnemonic_passphrase) = mnemonic_passphrase {
mnemonic_passphrase
Expand All @@ -876,17 +869,7 @@ impl<U: WalletIo> Wallet<U> {
(mnemonic, passphrase)
};
let seed = Seed::new(&mnemonic, &passphrase);
let spend_key =
derive_hd_spending_key(seed.as_bytes(), derivation_path.clone());

self.insert_spending_key(
alias,
alias_force,
spend_key,
password,
Some(derivation_path),
)
.map(|alias| (alias, spend_key))
Some(derive_hd_spending_key(seed.as_bytes(), derivation_path))
}

/// Generate a spending key similarly to how it's done for keypairs
Expand Down Expand Up @@ -1442,4 +1425,41 @@ impl<U: WalletIo + WalletStorage> Wallet<U> {
.flatten()
.transpose()
}

// XXX OK
/// Derive a spending key from the user mnemonic code (read from stdin)
/// using a given ZIP32 derivation path and insert it into the store with
/// the provided alias, converted to lower case.
/// The key is encrypted with the provided password. If no password
/// provided, will prompt for password from stdin.
/// Stores the key in decrypted key cache and returns the alias of the key
/// and the derived spending key.
pub fn derive_store_spending_key_from_mnemonic_code(
&mut self,
alias: String,
alias_force: bool,
derivation_path: DerivationPath,
mnemonic_passphrase: Option<(Mnemonic, Zeroizing<String>)>,
prompt_bip39_passphrase: bool,
password: Option<Zeroizing<String>>,
) -> Result<Option<(String, ExtendedSpendingKey)>, LoadStoreError> {
self.derive_spending_key_from_mnemonic_code(
derivation_path.clone(),
mnemonic_passphrase,
prompt_bip39_passphrase,
)
.map(|spend_key| {
self.insert_spending_key_atomic(
alias,
alias_force,
spend_key,
password,
Some(derivation_path),
)
.map(|o| o.map(|alias| (alias, spend_key)))
.transpose()
})
.flatten()
.transpose()
}
}

0 comments on commit 1f3edc7

Please sign in to comment.