Skip to content

Commit

Permalink
refactor: to_hex return hex string
Browse files Browse the repository at this point in the history
  • Loading branch information
ethicnology committed Nov 14, 2024
1 parent 5696e54 commit 7d7e3f6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 24 deletions.
7 changes: 3 additions & 4 deletions src/hex.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use super::Error;
use bitcoin::bip32::{ChildNumber, DerivationPath};
use bitcoin::hex::DisplayHex;
use bitcoin::{bip32::Xpriv, key::Secp256k1, secp256k1};

/// Derive binary entropy of certain length from the root inner
///
/// The `length` can be from 16 to 64 and defines number of bytes derived.
///
/// See [specs](https://github.com/bitcoin/bips/blob/master/bip-0085.mediawiki#hex) for more info.
Expand All @@ -12,7 +11,7 @@ pub fn to_hex<C: secp256k1::Signing>(
root: &Xpriv,
length: u32,
index: u32,
) -> Result<Vec<u8>, Error> {
) -> Result<String, Error> {
const BIP85_HEX_INDEX: ChildNumber = ChildNumber::Hardened { index: 128169 };
if length < 16 || length > 64 {
return Err(Error::InvalidLength(length));
Expand All @@ -26,5 +25,5 @@ pub fn to_hex<C: secp256k1::Signing>(
ChildNumber::from_hardened_idx(index).unwrap(),
]);
let data = crate::derive(secp, root, &path)?;
Ok(data[0..length as usize].to_vec())
Ok(data[0..length as usize].to_lower_hex_string())
}
23 changes: 3 additions & 20 deletions tests/bip85_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,30 +84,13 @@ fn test_xprv() {
#[test]
fn test_hex() {
let root = Xpriv::from_str(
"xprv9s21ZrQH143K2LBWUUQRFXhucrQqBpKdRRxNVq2zBqsx8HVqFk2uYo8kmbaL\
LHRdqtQpUm98uKfu3vca1LqdGhUtyoFnCNkfmXRyPXLjbKb",
"xprv9s21ZrQH143K2LBWUUQRFXhucrQqBpKdRRxNVq2zBqsx8HVqFk2uYo8kmbaLLHRdqtQpUm98uKfu3vca1LqdGhUtyoFnCNkfmXRyPXLjbKb",
)
.unwrap();
let secp = Secp256k1::new();
let derived = to_hex(&secp, &root, 64, 0).unwrap();
let expected = vec![
0x49, 0x2d, 0xb4, 0x69, 0x8c, 0xf3, 0xb7, 0x3a, 0x5a, 0x24, 0x99, 0x8a, 0xa3, 0xe9, 0xd7,
0xfa, 0x96, 0x27, 0x5d, 0x85, 0x72, 0x4a, 0x91, 0xe7, 0x1a, 0xa2, 0xd6, 0x45, 0x44, 0x2f,
0x87, 0x85, 0x55, 0xd0, 0x78, 0xfd, 0x1f, 0x1f, 0x67, 0xe3, 0x68, 0x97, 0x6f, 0x04, 0x13,
0x7b, 0x1f, 0x7a, 0x0d, 0x19, 0x23, 0x21, 0x36, 0xca, 0x50, 0xc4, 0x46, 0x14, 0xaf, 0x72,
0xb5, 0x58, 0x2a, 0x5c,
];

assert_eq!(expected, derived);

let derived = to_hex(&secp, &root, 35, 0).unwrap();
assert_eq!(derived.len(), 35);

let derived = to_hex(&secp, &root, 15, 0);
assert_eq!(derived, Err(Error::InvalidLength(15)));

let derived = to_hex(&secp, &root, 65, 0);
assert_eq!(derived, Err(Error::InvalidLength(65)));
let expected = "492db4698cf3b73a5a24998aa3e9d7fa96275d85724a91e71aa2d645442f878555d078fd1f1f67e368976f04137b1f7a0d19232136ca50c44614af72b5582a5c";
assert_eq!(expected, &derived);
}

#[cfg(feature = "mnemonic")]
Expand Down

0 comments on commit 7d7e3f6

Please sign in to comment.