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
bf7b4df
commit 3278146
Showing
5 changed files
with
49 additions
and
3 deletions.
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,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) | ||
} |
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_base85(&Secp256k1::new(), &root, 12, 0).unwrap(); | ||
let expected = "_s`{TW89)i4`"; | ||
assert_eq!(expected, pwd_base64); | ||
} |