Merge pull request #459 from sgeisler/2020-08-extend-derivation-path

Allow easy concatenation of bip32 derivation paths
This commit is contained in:
Andrew Poelstra 2020-10-07 21:34:17 +00:00 committed by GitHub
commit 3748e8faf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 0 deletions

View File

@ -326,6 +326,28 @@ impl DerivationPath {
pub fn hardened_children(&self) -> DerivationPathIterator { pub fn hardened_children(&self) -> DerivationPathIterator {
DerivationPathIterator::start_from(&self, ChildNumber::Hardened{ index: 0 }) DerivationPathIterator::start_from(&self, ChildNumber::Hardened{ index: 0 })
} }
/// Concatenate `self` with `path` and return the resulting new path.
///
/// ```
/// use bitcoin::util::bip32::{DerivationPath, ChildNumber};
/// use std::str::FromStr;
///
/// let base = DerivationPath::from_str("m/42").unwrap();
///
/// let deriv_1 = base.extend(DerivationPath::from_str("m/0/1").unwrap());
/// let deriv_2 = base.extend(&[
/// ChildNumber::from_normal_idx(0).unwrap(),
/// ChildNumber::from_normal_idx(1).unwrap()
/// ]);
///
/// assert_eq!(deriv_1, deriv_2);
/// ```
pub fn extend<T: AsRef<[ChildNumber]>>(&self, path: T) -> DerivationPath {
let mut new_path = self.clone();
new_path.0.extend_from_slice(path.as_ref());
new_path
}
} }
impl fmt::Display for DerivationPath { impl fmt::Display for DerivationPath {