vls_common/
lib.rs

1#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
2
3extern crate alloc;
4
5use alloc::string::String;
6use alloc::vec::Vec;
7use bitcoin::bip32::{ChildNumber, DerivationPath};
8
9#[macro_use]
10pub mod macros;
11
12pub trait HexEncode {
13    fn to_hex(&self) -> String;
14}
15
16impl<T: hex::ToHex> HexEncode for T {
17    fn to_hex(&self) -> String {
18        self.encode_hex()
19    }
20}
21
22pub fn to_derivation_path<T: Into<u32> + Copy>(child_index: &[T]) -> DerivationPath {
23    child_index
24        .iter()
25        .map(|index| ChildNumber::from((*index).into()))
26        .collect::<Vec<ChildNumber>>()
27        .into()
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33
34    #[test]
35    pub fn test_index_derivation_path() {
36        let path: Vec<u32> = vec![1, 11 | (1 << 31), 3, 4];
37
38        let derivation_path = to_derivation_path(&path);
39        assert_eq!("1/11'/3/4", derivation_path.to_string());
40    }
41}