bip32: add from_32_slice method to DerivationPath

ChildNumber has a `From<u32>` impl, but converting `&[u32]` to a
`DerivationPath` was still difficult.
This commit is contained in:
Marko Bencun 2024-06-24 13:49:57 +02:00
parent b904de375a
commit 47cba7a655
No known key found for this signature in database
GPG Key ID: 804538928C37EAE8
1 changed files with 13 additions and 0 deletions

View File

@ -453,6 +453,19 @@ impl DerivationPath {
/// assert_eq!(path.to_u32_vec(), vec![84 + HARDENED, HARDENED, HARDENED, 0, 1]); /// assert_eq!(path.to_u32_vec(), vec![84 + HARDENED, HARDENED, HARDENED, 0, 1]);
/// ``` /// ```
pub fn to_u32_vec(&self) -> Vec<u32> { self.into_iter().map(|&el| el.into()).collect() } pub fn to_u32_vec(&self) -> Vec<u32> { 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 { impl fmt::Display for DerivationPath {