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

Commit 60e1660

Browse files
committed
add derive child key for Private Keys
1 parent fc8b848 commit 60e1660

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/crypto.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ impl SecretKey {
468468
///
469469
/// Mnemonics generated by the Android and iOS wallets will work
470470
///
471-
/// Returned key will support deriving child keys with `.derive_child()`
471+
/// Returned key will support deriving child keys with `.derive_child_key(index)`
472472
pub fn from_mnemonic_with_passphrase(mnemonic: &str, passphrase: &str) -> Result<Self, Error> {
473473
let mnemonic = Mnemonic::from_phrase(mnemonic, Language::English)?;
474474

@@ -495,6 +495,22 @@ impl SecretKey {
495495
Ok(secret_key)
496496
}
497497

498+
/// Derive a new private key at the given wallet index.
499+
///
500+
/// Currently fails if the key was not generated with `generate_mnemonic` or `generated_mnemonic_with_passphrase`
501+
/// or reconstructed with `from_mnemonic` or `from_mnemonic_with_passphrase`
502+
pub fn derive_child_key(&self, index: u32) -> Result<Self, Error> {
503+
let chain_code = self.chain_code
504+
.map_or(Err(err_msg("this Ed25519 private key does not support key derivation")), |cc| {Ok(cc)})?;
505+
506+
let (key_bytes, chain_code) = Self::derive_child_key_bytes(self.as_bytes(), &chain_code, &index)?;
507+
508+
Ok(SecretKey{
509+
value: ed25519_dalek::SecretKey::from_bytes(&key_bytes)?,
510+
chain_code: Some(chain_code)
511+
})
512+
}
513+
498514
/// Format a `SecretKey` as a vec of bytes in ASN.1 format.
499515
pub fn to_encoded_bytes(&self) -> Vec<u8> {
500516
der_encode(&PrivateKeyInfo {

0 commit comments

Comments
 (0)