From 202a946fc3f9bf281fdbf57f66a55550deda765e Mon Sep 17 00:00:00 2001 From: Sebastian Geisler Date: Wed, 19 Aug 2020 16:08:09 +0200 Subject: [PATCH] Allow easy concatenation of bip32 derivation paths Currently one has to convert the path into a Vec, extend it and finally convert it back again. --- src/util/bip32.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/util/bip32.rs b/src/util/bip32.rs index 3fd8fc2b..61229327 100644 --- a/src/util/bip32.rs +++ b/src/util/bip32.rs @@ -326,6 +326,28 @@ impl DerivationPath { pub fn hardened_children(&self) -> DerivationPathIterator { 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>(&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 {