Skip to content
This repository has been archived by the owner on May 8, 2021. It is now read-only.

Commit

Permalink
add derive child key for Private Keys
Browse files Browse the repository at this point in the history
  • Loading branch information
QuestofIranon committed Sep 14, 2019
1 parent fc8b848 commit 60e1660
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ impl SecretKey {
///
/// Mnemonics generated by the Android and iOS wallets will work
///
/// Returned key will support deriving child keys with `.derive_child()`
/// Returned key will support deriving child keys with `.derive_child_key(index)`
pub fn from_mnemonic_with_passphrase(mnemonic: &str, passphrase: &str) -> Result<Self, Error> {
let mnemonic = Mnemonic::from_phrase(mnemonic, Language::English)?;

Expand All @@ -495,6 +495,22 @@ impl SecretKey {
Ok(secret_key)
}

/// Derive a new private key at the given wallet index.
///
/// Currently fails if the key was not generated with `generate_mnemonic` or `generated_mnemonic_with_passphrase`
/// or reconstructed with `from_mnemonic` or `from_mnemonic_with_passphrase`
pub fn derive_child_key(&self, index: u32) -> Result<Self, Error> {
let chain_code = self.chain_code
.map_or(Err(err_msg("this Ed25519 private key does not support key derivation")), |cc| {Ok(cc)})?;

let (key_bytes, chain_code) = Self::derive_child_key_bytes(self.as_bytes(), &chain_code, &index)?;

Ok(SecretKey{
value: ed25519_dalek::SecretKey::from_bytes(&key_bytes)?,
chain_code: Some(chain_code)
})
}

/// Format a `SecretKey` as a vec of bytes in ASN.1 format.
pub fn to_encoded_bytes(&self) -> Vec<u8> {
der_encode(&PrivateKeyInfo {
Expand Down

0 comments on commit 60e1660

Please sign in to comment.