Skip to content

Commit

Permalink
feat: PWD Base64
Browse files Browse the repository at this point in the history
  • Loading branch information
ethicnology committed Nov 14, 2024
1 parent 7d7e3f6 commit bf7b4df
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ all-languages = [
bitcoin = "0.32.4"
bip39 = { version = "2.1.0", optional = true }
sha3 = "0.10.8"
base64 = "0.22.1"

[[example]]
name = "mnemonic"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ This work is sponsored by [Bull Bitcoin](https://bullbitcoin.com) [<img
- [x] [XPRV](https://github.com/bitcoin/bips/blob/master/bip-0085.mediawiki#user-content-XPRV)
- [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)
- [ ] [PWD base64](https://github.com/bitcoin/bips/blob/master/bip-0085.mediawiki#user-content-PWD_BASE64)
- [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)
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ pub mod drng;
pub mod error;
pub mod hex;
pub mod mnemonic;
pub mod pwd_base64;
pub mod wif;
pub mod xprv;

pub use drng::*;
pub use error::Error;
pub use hex::*;
pub use mnemonic::*;
pub use pwd_base64::*;
pub use wif::*;
pub use xprv::*;

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

/// The `length` can be from 20 to 86 and defines number of bytes derived.
///
/// See [specs](https://github.com/bitcoin/bips/blob/master/bip-0085.mediawiki#pwd-base64) for more info.
pub fn to_pwd_base64<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: 707764 };
if length < 20 || length > 86 {
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 = BASE64_STANDARD.encode(data);
pwd.truncate(length.try_into().unwrap());
Ok(pwd)
}
15 changes: 15 additions & 0 deletions tests/pwd_base64_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_base64(&Secp256k1::new(), &root, 21, 0).unwrap();
let expected = "dKLoepugzdVJvdL56ogNV";
assert_eq!(expected, pwd_base64);
}

0 comments on commit bf7b4df

Please sign in to comment.