Skip to content

Commit

Permalink
feat: pwd base85
Browse files Browse the repository at this point in the history
  • Loading branch information
ethicnology committed Nov 14, 2024
1 parent bf7b4df commit 3278146
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ This work is sponsored by [Bull Bitcoin](https://bullbitcoin.com) [<img
- [x] [HEX](https://github.com/bitcoin/bips/blob/master/bip-0085.mediawiki#hex)
- [x] [DRNG](https://github.com/bitcoin/bips/blob/master/bip-0085.mediawiki#bip85-drng)
- [x] [PWD base64](https://github.com/bitcoin/bips/blob/master/bip-0085.mediawiki#user-content-PWD_BASE64)
- [ ] [PWD base85](https://github.com/bitcoin/bips/blob/master/bip-0085.mediawiki#user-content-PWD_BASE85)
- [ ] [RSA](https://github.com/bitcoin/bips/blob/master/bip-0085.mediawiki#user-content-RSA)
- [ ] [RSA GPG](https://github.com/bitcoin/bips/blob/master/bip-0085.mediawiki#user-content-RSA_GPG)
- [x] [PWD base85](https://github.com/bitcoin/bips/blob/master/bip-0085.mediawiki#user-content-PWD_BASE85)
- [ ] [DICE](https://github.com/bitcoin/bips/blob/master/bip-0085.mediawiki#user-content-DICE)


Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub mod error;
pub mod hex;
pub mod mnemonic;
pub mod pwd_base64;
pub mod pwd_base85;
pub mod wif;
pub mod xprv;

Expand All @@ -37,6 +38,7 @@ pub use error::Error;
pub use hex::*;
pub use mnemonic::*;
pub use pwd_base64::*;
pub use pwd_base85::*;
pub use wif::*;
pub use xprv::*;

Expand Down
30 changes: 30 additions & 0 deletions src/pwd_base85.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use super::Error;
use bitcoin::bip32::{ChildNumber, DerivationPath};
use bitcoin::{bip32::Xpriv, key::Secp256k1, secp256k1};

/// The `length` can be from 10 to 80 and defines number of bytes derived.
///
/// See [specs](https://github.com/bitcoin/bips/blob/master/bip-0085.mediawiki#pwd-base85) for more info.
pub fn to_pwd_base85<C: secp256k1::Signing>(
secp: &Secp256k1<C>,
root: &Xpriv,
length: u32,
index: u32,
) -> Result<String, Error> {
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)
}
15 changes: 15 additions & 0 deletions tests/pwd_base85_test.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 3278146

Please sign in to comment.