From 47cba7a6551709107ac74f9509851e123adfc9c3 Mon Sep 17 00:00:00 2001 From: Marko Bencun Date: Mon, 24 Jun 2024 13:49:57 +0200 Subject: [PATCH] bip32: add from_32_slice method to DerivationPath ChildNumber has a `From` impl, but converting `&[u32]` to a `DerivationPath` was still difficult. --- bitcoin/src/bip32.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/bitcoin/src/bip32.rs b/bitcoin/src/bip32.rs index 6ace63289..a3667d640 100644 --- a/bitcoin/src/bip32.rs +++ b/bitcoin/src/bip32.rs @@ -453,6 +453,19 @@ impl DerivationPath { /// assert_eq!(path.to_u32_vec(), vec![84 + HARDENED, HARDENED, HARDENED, 0, 1]); /// ``` pub fn to_u32_vec(&self) -> Vec { self.into_iter().map(|&el| el.into()).collect() } + + /// Creates a derivation path from a slice of u32s. + /// ``` + /// use bitcoin::bip32::DerivationPath; + /// + /// const HARDENED: u32 = 0x80000000; + /// let expected = vec![84 + HARDENED, HARDENED, HARDENED, 0, 1]; + /// let path = DerivationPath::from_u32_slice(expected.as_slice()); + /// assert_eq!(path.to_u32_vec(), expected); + /// ``` + pub fn from_u32_slice(numbers: &[u32]) -> Self { + numbers.iter().map(|&n| ChildNumber::from(n)).collect() + } } impl fmt::Display for DerivationPath {