From 3278146b1a2dda19ee2e9b8d3a4edaddf55fc99d Mon Sep 17 00:00:00 2001 From: ethicnology Date: Thu, 14 Nov 2024 10:31:35 -0500 Subject: [PATCH] feat: pwd base85 --- Cargo.toml | 1 + README.md | 4 +--- src/lib.rs | 2 ++ src/pwd_base85.rs | 30 ++++++++++++++++++++++++++++++ tests/pwd_base85_test.rs | 15 +++++++++++++++ 5 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 src/pwd_base85.rs create mode 100644 tests/pwd_base85_test.rs diff --git a/Cargo.toml b/Cargo.toml index 160e543..3630957 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,6 +41,7 @@ bitcoin = "0.32.4" bip39 = { version = "2.1.0", optional = true } sha3 = "0.10.8" base64 = "0.22.1" +base85 = "2.0.0" [[example]] name = "mnemonic" diff --git a/README.md b/README.md index 4616b2f..bb5b8b3 100644 --- a/README.md +++ b/README.md @@ -18,9 +18,7 @@ This work is sponsored by [Bull Bitcoin](https://bullbitcoin.com) [( + secp: &Secp256k1, + root: &Xpriv, + length: u32, + index: u32, +) -> Result { + const BIP85_PWD_B64_APPLICATION_NUMBER: ChildNumber = ChildNumber::Hardened { index: 707785 }; + if length < 10 || length > 80 { + return Err(Error::InvalidLength(length)); + } + if index >= 0x80000000 { + return Err(Error::InvalidIndex(index)); + } + let path = DerivationPath::from(vec![ + BIP85_PWD_B64_APPLICATION_NUMBER, + ChildNumber::from_hardened_idx(length).unwrap(), + ChildNumber::from_hardened_idx(index).unwrap(), + ]); + let data = crate::derive(secp, root, &path)?; + let mut pwd = base85::encode(&data); + pwd.truncate(length.try_into().unwrap()); + Ok(pwd) +} diff --git a/tests/pwd_base85_test.rs b/tests/pwd_base85_test.rs new file mode 100644 index 0000000..4c32be2 --- /dev/null +++ b/tests/pwd_base85_test.rs @@ -0,0 +1,15 @@ +use std::str::FromStr; + +use bitcoin::{bip32::Xpriv, key::Secp256k1}; + +#[test] +fn test_pwd_base64() { + let root = Xpriv::from_str( + "xprv9s21ZrQH143K2LBWUUQRFXhucrQqBpKdRRxNVq2zBqsx8HVqFk2uYo8kmbaLLHRdqtQpUm98uKfu3vca1LqdGhUtyoFnCNkfmXRyPXLjbKb", + ) + .unwrap(); + + let pwd_base64 = bip85_fork::to_pwd_base85(&Secp256k1::new(), &root, 12, 0).unwrap(); + let expected = "_s`{TW89)i4`"; + assert_eq!(expected, pwd_base64); +}