Skip to content

Commit

Permalink
Merge rust-bitcoin#2909: bip32: add From<&'a [u32]> for DerivationPath
Browse files Browse the repository at this point in the history
a7731b2 api: Run just check-api (Marko Bencun)
47cba7a bip32: add from_32_slice method to DerivationPath (Marko Bencun)

Pull request description:

  ChildNumber already has a `From<u32>` impl, but converting `&[u32]` to a `DerivationPath` was still difficult.

ACKs for top commit:
  Kixunil:
    ACK a7731b2
  tcharding:
    ACK a7731b2

Tree-SHA512: 2db94ee035e686102b8201f637422bb96bd79858aeffdb007594d722902d31a20f27e61b88fae8c854c80f785d9e7837b0158a046639ff8cc2d20d8883391842
  • Loading branch information
apoelstra committed Jun 29, 2024
2 parents 7ca7128 + a7731b2 commit ae90d19
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/bitcoin/all-features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7584,6 +7584,7 @@ pub fn bitcoin::bip32::DerivationPath::from(numbers: &'a [bitcoin::bip32::ChildN
pub fn bitcoin::bip32::DerivationPath::from(numbers: alloc::vec::Vec<bitcoin::bip32::ChildNumber>) -> Self
pub fn bitcoin::bip32::DerivationPath::from_iter<T>(iter: T) -> Self where T: core::iter::traits::collect::IntoIterator<Item = bitcoin::bip32::ChildNumber>
pub fn bitcoin::bip32::DerivationPath::from_str(path: &str) -> core::result::Result<bitcoin::bip32::DerivationPath, bitcoin::bip32::Error>
pub fn bitcoin::bip32::DerivationPath::from_u32_slice(numbers: &[u32]) -> Self
pub fn bitcoin::bip32::DerivationPath::hardened_children(&self) -> bitcoin::bip32::DerivationPathIterator<'_>
pub fn bitcoin::bip32::DerivationPath::hash<__H: core::hash::Hasher>(&self, state: &mut __H)
pub fn bitcoin::bip32::DerivationPath::index(&self, index: I) -> &Self::Output
Expand Down
1 change: 1 addition & 0 deletions api/bitcoin/default-features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7237,6 +7237,7 @@ pub fn bitcoin::bip32::DerivationPath::from(numbers: &'a [bitcoin::bip32::ChildN
pub fn bitcoin::bip32::DerivationPath::from(numbers: alloc::vec::Vec<bitcoin::bip32::ChildNumber>) -> Self
pub fn bitcoin::bip32::DerivationPath::from_iter<T>(iter: T) -> Self where T: core::iter::traits::collect::IntoIterator<Item = bitcoin::bip32::ChildNumber>
pub fn bitcoin::bip32::DerivationPath::from_str(path: &str) -> core::result::Result<bitcoin::bip32::DerivationPath, bitcoin::bip32::Error>
pub fn bitcoin::bip32::DerivationPath::from_u32_slice(numbers: &[u32]) -> Self
pub fn bitcoin::bip32::DerivationPath::hardened_children(&self) -> bitcoin::bip32::DerivationPathIterator<'_>
pub fn bitcoin::bip32::DerivationPath::hash<__H: core::hash::Hasher>(&self, state: &mut __H)
pub fn bitcoin::bip32::DerivationPath::index(&self, index: I) -> &Self::Output
Expand Down
1 change: 1 addition & 0 deletions api/bitcoin/no-features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6596,6 +6596,7 @@ pub fn bitcoin::bip32::DerivationPath::from(numbers: &'a [bitcoin::bip32::ChildN
pub fn bitcoin::bip32::DerivationPath::from(numbers: alloc::vec::Vec<bitcoin::bip32::ChildNumber>) -> Self
pub fn bitcoin::bip32::DerivationPath::from_iter<T>(iter: T) -> Self where T: core::iter::traits::collect::IntoIterator<Item = bitcoin::bip32::ChildNumber>
pub fn bitcoin::bip32::DerivationPath::from_str(path: &str) -> core::result::Result<bitcoin::bip32::DerivationPath, bitcoin::bip32::Error>
pub fn bitcoin::bip32::DerivationPath::from_u32_slice(numbers: &[u32]) -> Self
pub fn bitcoin::bip32::DerivationPath::hardened_children(&self) -> bitcoin::bip32::DerivationPathIterator<'_>
pub fn bitcoin::bip32::DerivationPath::hash<__H: core::hash::Hasher>(&self, state: &mut __H)
pub fn bitcoin::bip32::DerivationPath::index(&self, index: I) -> &Self::Output
Expand Down
13 changes: 13 additions & 0 deletions bitcoin/src/bip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,19 @@ impl DerivationPath {
/// 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() }

/// 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 {
Expand Down

0 comments on commit ae90d19

Please sign in to comment.