forked from rikitau/rust-bip85
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d7e3f6
commit bf7b4df
Showing
5 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |