Skip to content

Commit

Permalink
Add impl of Personas (without PersonaData)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinnstah committed Jan 15, 2024
1 parent 402c627 commit 94c819c
Show file tree
Hide file tree
Showing 59 changed files with 1,236 additions and 357 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,20 @@ mod tests {

#[test]
fn word() {
assert_eq!(BIP39Word::english("zoo").unwrap().word, "zoo");
assert_eq!(
BIP39Word::english("zoo")
.unwrap()
.word,
"zoo"
);
}

#[test]
fn language_of_zoo_is_english() {
assert_eq!(
BIP39Word::english("zoo").unwrap().language,
BIP39Word::english("zoo")
.unwrap()
.language,
BIP39Language::English
);
}
Expand All @@ -83,7 +90,14 @@ mod tests {

#[test]
fn index_of_zoo_is_2047() {
assert_eq!(BIP39Word::english("zoo").unwrap().index.clone().inner, 2047);
assert_eq!(
BIP39Word::english("zoo")
.unwrap()
.index
.clone()
.inner,
2047
);
}

#[test]
Expand Down
72 changes: 62 additions & 10 deletions profile/src/hierarchical_deterministic/bip39/mnemonic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ impl Mnemonic {
bip39::Mnemonic::from_str(&self.phrase()).unwrap()
}
pub fn phrase(&self) -> String {
self.words.iter().map(|w| w.word.to_string()).join(" ")
self.words
.iter()
.map(|w| w.word.to_string())
.join(" ")
}
pub fn from_phrase(phrase: &str) -> Result<Self, Error> {
bip39::Mnemonic::from_str(phrase)
Expand All @@ -58,7 +61,8 @@ impl Mnemonic {
}

pub fn to_seed(&self, passphrase: &str) -> Seed {
self.internal().to_seed(passphrase)
self.internal()
.to_seed(passphrase)
}
}

Expand Down Expand Up @@ -166,18 +170,66 @@ mod tests {
let zoo: Mnemonic = "zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong"
.try_into()
.unwrap();
assert_eq!(zoo.words[0].index.clone().inner, 2047);
assert_eq!(zoo.words[1].index.clone().inner, 2047);
assert_eq!(zoo.words[10].index.clone().inner, 2047);
assert_eq!(zoo.words[11].index.clone().inner, 2037);
assert_eq!(
zoo.words[0]
.index
.clone()
.inner,
2047
);
assert_eq!(
zoo.words[1]
.index
.clone()
.inner,
2047
);
assert_eq!(
zoo.words[10]
.index
.clone()
.inner,
2047
);
assert_eq!(
zoo.words[11]
.index
.clone()
.inner,
2037
);

let abandon: Mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
.try_into()
.unwrap();
assert_eq!(abandon.words[0].index.clone().inner, 0);
assert_eq!(abandon.words[1].index.clone().inner, 0);
assert_eq!(abandon.words[10].index.clone().inner, 0);
assert_eq!(abandon.words[11].index.clone().inner, 3);
assert_eq!(
abandon.words[0]
.index
.clone()
.inner,
0
);
assert_eq!(
abandon.words[1]
.index
.clone()
.inner,
0
);
assert_eq!(
abandon.words[10]
.index
.clone()
.inner,
0
);
assert_eq!(
abandon.words[11]
.index
.clone()
.inner,
3
);
}

#[test]
Expand Down
16 changes: 12 additions & 4 deletions profile/src/hierarchical_deterministic/bip44/bip44_like_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,23 @@ mod tests {

#[test]
fn inequality_different_accounts() {
let a: BIP44LikePath = "m/44H/1022H/0H/0/0H".try_into().unwrap();
let b: BIP44LikePath = "m/44H/1022H/1H/0/0H".try_into().unwrap();
let a: BIP44LikePath = "m/44H/1022H/0H/0/0H"
.try_into()
.unwrap();
let b: BIP44LikePath = "m/44H/1022H/1H/0/0H"
.try_into()
.unwrap();
assert!(a != b);
}

#[test]
fn inequality_different_index() {
let a: BIP44LikePath = "m/44H/1022H/0H/0/0H".try_into().unwrap();
let b: BIP44LikePath = "m/44H/1022H/0H/0/1H".try_into().unwrap();
let a: BIP44LikePath = "m/44H/1022H/0H/0/0H"
.try_into()
.unwrap();
let b: BIP44LikePath = "m/44H/1022H/0H/0/1H"
.try_into()
.unwrap();
assert!(a != b);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::HDPathError;
use crate::{CommonError, HDPathError};
use serde::{de, Deserializer, Serialize, Serializer};

use crate::HasPlaceholder;
Expand Down Expand Up @@ -37,6 +37,17 @@ impl IsEntityPath for AccountPath {
}
}

impl TryFrom<CAP26Path> for AccountPath {
type Error = HDPathError;

fn try_from(value: CAP26Path) -> Result<Self, Self::Error> {
value
.as_account_path()
.ok_or(HDPathError::ExpectedAccountPathGotSomethingElse)
.cloned()
}
}

impl TryFrom<&HDPath> for AccountPath {
type Error = HDPathError;

Expand Down Expand Up @@ -267,27 +278,41 @@ mod tests {
fn fails_when_index_is_too_large() {
assert_eq!(
AccountPath::from_str("m/44H/1022H/1H/525H/1460H/4294967296H"),
Err(HDPathError::InvalidBIP32Path("m/44H/1022H/1H/525H/1460H/4294967296H".to_string()))
Err(HDPathError::InvalidBIP32Path(
"m/44H/1022H/1H/525H/1460H/4294967296H".to_string()
))
)
}

#[test]
fn inequality_different_index() {
let a: AccountPath = "m/44H/1022H/1H/525H/1460H/0H".try_into().unwrap();
let b: AccountPath = "m/44H/1022H/1H/525H/1460H/1H".try_into().unwrap();
let a: AccountPath = "m/44H/1022H/1H/525H/1460H/0H"
.try_into()
.unwrap();
let b: AccountPath = "m/44H/1022H/1H/525H/1460H/1H"
.try_into()
.unwrap();
assert!(a != b);
}
#[test]
fn inequality_different_network_id() {
let a: AccountPath = "m/44H/1022H/1H/525H/1460H/0H".try_into().unwrap();
let b: AccountPath = "m/44H/1022H/2H/525H/1460H/0H".try_into().unwrap();
let a: AccountPath = "m/44H/1022H/1H/525H/1460H/0H"
.try_into()
.unwrap();
let b: AccountPath = "m/44H/1022H/2H/525H/1460H/0H"
.try_into()
.unwrap();
assert!(a != b);
}

#[test]
fn inequality_different_key_kind() {
let a: AccountPath = "m/44H/1022H/1H/525H/1460H/0H".try_into().unwrap();
let b: AccountPath = "m/44H/1022H/1H/525H/1678H/0H".try_into().unwrap();
let a: AccountPath = "m/44H/1022H/1H/525H/1460H/0H"
.try_into()
.unwrap();
let b: AccountPath = "m/44H/1022H/1H/525H/1678H/0H"
.try_into()
.unwrap();
assert!(a != b);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::HDPathError;
use crate::{CommonError, HDPathError};
use serde::{de, Deserializer, Serialize, Serializer};

use crate::HasPlaceholder;
Expand Down Expand Up @@ -37,6 +37,17 @@ impl IsEntityPath for IdentityPath {
}
}

impl TryFrom<CAP26Path> for IdentityPath {
type Error = HDPathError;

fn try_from(value: CAP26Path) -> Result<Self, Self::Error> {
value
.as_identity_path()
.ok_or(HDPathError::ExpectedIdentityPathGotSomethingElse)
.cloned()
}
}

impl TryFrom<&HDPath> for IdentityPath {
type Error = HDPathError;

Expand Down Expand Up @@ -279,27 +290,41 @@ mod tests {
fn fails_when_index_is_too_large() {
assert_eq!(
IdentityPath::from_str("m/44H/1022H/1H/618H/1460H/4294967296H"),
Err(HDPathError::InvalidBIP32Path("m/44H/1022H/1H/618H/1460H/4294967296H".to_string()))
Err(HDPathError::InvalidBIP32Path(
"m/44H/1022H/1H/618H/1460H/4294967296H".to_string()
))
)
}

#[test]
fn inequality_different_index() {
let a: IdentityPath = "m/44H/1022H/1H/618H/1460H/0H".try_into().unwrap();
let b: IdentityPath = "m/44H/1022H/1H/618H/1460H/1H".try_into().unwrap();
let a: IdentityPath = "m/44H/1022H/1H/618H/1460H/0H"
.try_into()
.unwrap();
let b: IdentityPath = "m/44H/1022H/1H/618H/1460H/1H"
.try_into()
.unwrap();
assert!(a != b);
}
#[test]
fn inequality_different_network_id() {
let a: IdentityPath = "m/44H/1022H/1H/618H/1460H/0H".try_into().unwrap();
let b: IdentityPath = "m/44H/1022H/2H/618H/1460H/0H".try_into().unwrap();
let a: IdentityPath = "m/44H/1022H/1H/618H/1460H/0H"
.try_into()
.unwrap();
let b: IdentityPath = "m/44H/1022H/2H/618H/1460H/0H"
.try_into()
.unwrap();
assert!(a != b);
}

#[test]
fn inequality_different_key_kind() {
let a: IdentityPath = "m/44H/1022H/1H/618H/1460H/0H".try_into().unwrap();
let b: IdentityPath = "m/44H/1022H/1H/618H/1678H/0H".try_into().unwrap();
let a: IdentityPath = "m/44H/1022H/1H/618H/1460H/0H"
.try_into()
.unwrap();
let b: IdentityPath = "m/44H/1022H/1H/618H/1678H/0H"
.try_into()
.unwrap();
assert!(a != b);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{CAP26KeyKind, CAP26Repr, HDPathValue, NetworkID};
use crate::{CAP26KeyKind, CAP26Path, CAP26Repr, HDPathError, HDPathValue, NetworkID};

pub trait IsEntityPath: CAP26Repr {
pub trait IsEntityPath: CAP26Repr + Into<CAP26Path> + TryFrom<CAP26Path> {
fn network_id(&self) -> NetworkID;
fn key_kind(&self) -> CAP26KeyKind;
fn index(&self) -> HDPathValue;
Expand Down
33 changes: 19 additions & 14 deletions profile/src/hierarchical_deterministic/cap26/cap26_repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,18 @@ pub trait CAP26Repr: Derivation {
if path.depth() != 6 {
return Err(InvalidDepthOfCAP26Path);
}
let network_id =
HDPath::parse_try_map(
&components,
2,
Box::new(|v| {
if v <= u8::MAX as u32 {
let d = v as u8;
NetworkID::from_repr(d).ok_or(UnsupportedNetworkID(d))
} else {
Err(InvalidNetworkIDExceedsLimit(v))
}
}),
)?;
let network_id = HDPath::parse_try_map(
&components,
2,
Box::new(|v| {
if v <= u8::MAX as u32 {
let d = v as u8;
NetworkID::from_repr(d).ok_or(UnsupportedNetworkID(d))
} else {
Err(InvalidNetworkIDExceedsLimit(v))
}
}),
)?;
let entity_kind = HDPath::parse_try_map(
&components,
3,
Expand All @@ -62,7 +61,13 @@ pub trait CAP26Repr: Derivation {

let index = HDPath::parse_try_map(&components, 5, Box::new(|v| Ok(v)))?;

return Ok(Self::__with_path_and_components(path, network_id, entity_kind, key_kind, index));
return Ok(Self::__with_path_and_components(
path,
network_id,
entity_kind,
key_kind,
index,
));
}

#[cfg(not(tarpaulin_include))] // false negative, this is in fact heavily tested.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ pub trait Derivation: Sized {

#[cfg(not(tarpaulin_include))] // false negative
fn last_component(&self) -> &HDPathComponent {
self.hd_path().components.last().unwrap()
self.hd_path()
.components
.last()
.unwrap()
}
}
Loading

0 comments on commit 94c819c

Please sign in to comment.