Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Commit

Permalink
SUCCESSFULLY PARSE PROFILE FROM IOS!
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon committed Dec 20, 2023
1 parent d515d4a commit bcea5f6
Show file tree
Hide file tree
Showing 8 changed files with 1,758 additions and 47 deletions.
1 change: 1 addition & 0 deletions profile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ serde = { version = "1.0.193", features = ["derive"] }
serde_json = { version = "1.0.108", features = ["preserve_order"] }
serde_with = { version = "3.0.0", features = ["hex"] }

iso8601-timestamp = { version = "0.2.13", features = ["serde"] }
serde_repr = "0.1.17"
strum = { version = "0.25.0", features = ["derive"] }
hierarchical_deterministic = { path = "../hierarchical_deterministic" }
Expand Down
72 changes: 71 additions & 1 deletion profile/src/v100/entity/account/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub struct Account {
/// An order set of `EntityFlag`s used to describe certain Off-ledger
/// user state about Accounts or Personas, such as if an entity is
/// marked as hidden or not.
#[serde(default)]
flags: RefCell<EntityFlags>,

/// The on ledger synced settings for this account, contains e.g.
Expand Down Expand Up @@ -337,7 +338,7 @@ impl Account {

#[cfg(test)]
mod tests {
use std::collections::BTreeSet;
use std::{collections::BTreeSet, str::FromStr};

use radix_engine_common::prelude::HashSet;
use wallet_kit_common::json::assert_eq_after_json_roundtrip;
Expand Down Expand Up @@ -709,6 +710,75 @@ mod tests {
);
}

#[test]
fn json_deserialization_works_without_flags_as_version_1_0_0_of_app() {
let json = serde_json::Value::from_str(
r#"
{
"securityState":
{
"unsecuredEntityControl":
{
"transactionSigning":
{
"badge":
{
"virtualSource":
{
"hierarchicalDeterministicPublicKey":
{
"publicKey":
{
"curve": "secp256k1",
"compressedData": "02f669a43024d90fde69351ccc53022c2f86708d9b3c42693640733c5778235da5"
},
"derivationPath":
{
"scheme": "bip44Olympia",
"path": "m/44H/1022H/0H/0/0H"
}
},
"discriminator": "hierarchicalDeterministicPublicKey"
},
"discriminator": "virtualSource"
},
"factorSourceID":
{
"fromHash":
{
"kind": "device",
"body": "8bfacfe888d4e3819c6e9528a1c8f680a4ba73e466d7af4ee204591093006589"
},
"discriminator": "fromHash"
}
},
"entityIndex": 3
},
"discriminator": "unsecured"
},
"networkID": 14,
"appearanceID": 3,
"displayName": "Olympia|Soft|0",
"onLedgerSettings":
{
"thirdPartyDeposits":
{
"depositRule": "acceptAll",
"assetsExceptionList":
[],
"depositorsAllowList":
[]
}
},
"address": "account_tdx_e_169s2cfz044euhc4yjg4xe4pg55w97rq2c6jh50zsdcpuz5gk6cag6v"
}
"#,
).unwrap();
let account = serde_json::from_value::<Account>(json).unwrap();
assert_eq!(account.display_name(), "Olympia|Soft|0"); // soundness
assert_eq!(account.flags().len(), 0); // assert Default value is empty flags.
}

#[test]
fn hash() {
assert_eq!(
Expand Down
7 changes: 6 additions & 1 deletion profile/src/v100/entity/display_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use wallet_kit_common::error::common_error::CommonError as Error;

#[nutype(
sanitize(trim),
validate(not_empty, len_char_max = 20),
validate(not_empty, len_char_max = 30),
derive(
Serialize,
Deserialize,
Expand Down Expand Up @@ -53,6 +53,11 @@ mod tests {
);
}

#[test]
fn max_is_ok() {
assert!(DisplayName::try_from("0|RDX|Dev Nano S|Some very lon").is_ok());
}

#[test]
fn valid_try_from() {
assert_eq!(
Expand Down
37 changes: 18 additions & 19 deletions profile/src/v100/factors/factor_source_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
};

use chrono::NaiveDateTime;
use iso8601_timestamp::Timestamp;
use serde::{Deserialize, Serialize};
use wallet_kit_common::utils::factory::now;

Expand All @@ -28,7 +29,7 @@ pub struct FactorSourceCommon {
crypto_parameters: RefCell<FactorSourceCryptoParameters>,

/// When this factor source for originally added by the user.
added_on: NaiveDateTime,
added_on: Timestamp,

/// Date of last usage of this factor source
///
Expand All @@ -38,7 +39,7 @@ pub struct FactorSourceCommon {
///
/// Has interior mutability (`Cell`) since every time this
/// factor source is used we should update this date.
last_used_on: RefCell<NaiveDateTime>,
last_used_on: RefCell<Timestamp>,

/// Flags which describe a certain state a FactorSource might be in, e.g. `Main` (BDFS).
///
Expand All @@ -54,7 +55,7 @@ impl FactorSourceCommon {
}

/// When this factor source for originally added by the user.
pub fn added_on(&self) -> NaiveDateTime {
pub fn added_on(&self) -> Timestamp {
self.added_on.clone()
}

Expand All @@ -64,7 +65,7 @@ impl FactorSourceCommon {
/// since we will update it every time this FactorSource
/// is used.
///
pub fn last_used_on(&self) -> NaiveDateTime {
pub fn last_used_on(&self) -> Timestamp {
self.last_used_on.borrow().clone()
}

Expand All @@ -86,7 +87,7 @@ impl FactorSourceCommon {
*self.crypto_parameters.borrow_mut() = new
}

pub fn set_last_used_on(&self, new: NaiveDateTime) {
pub fn set_last_used_on(&self, new: Timestamp) {
*self.last_used_on.borrow_mut() = new
}

Expand All @@ -98,8 +99,8 @@ impl FactorSourceCommon {
impl FactorSourceCommon {
pub fn with_values<I>(
crypto_parameters: FactorSourceCryptoParameters,
added_on: NaiveDateTime,
last_used_on: NaiveDateTime,
added_on: Timestamp,
last_used_on: Timestamp,
flags: I,
) -> Self
where
Expand All @@ -117,7 +118,7 @@ impl FactorSourceCommon {
where
I: IntoIterator<Item = FactorSourceFlag>,
{
let date: NaiveDateTime = now();
let date = Timestamp::now_utc();
Self::with_values(crypto_parameters, date, date, flags)
}

Expand Down Expand Up @@ -149,8 +150,7 @@ impl FactorSourceCommon {

/// A placeholder used to facilitate unit tests.
pub fn placeholder_main_babylon() -> Self {
let date =
NaiveDateTime::parse_from_str("2023-09-11T16:05:56", "%Y-%m-%dT%H:%M:%S").unwrap();
let date = Timestamp::parse("2023-09-11T16:05:56.000Z").unwrap();
FactorSourceCommon::with_values(
FactorSourceCryptoParameters::babylon(),
date.clone(),
Expand All @@ -161,8 +161,7 @@ impl FactorSourceCommon {

/// A placeholder used to facilitate unit tests.
pub fn placeholder_olympia() -> Self {
let date =
NaiveDateTime::parse_from_str("2023-09-11T16:05:56", "%Y-%m-%dT%H:%M:%S").unwrap();
let date = Timestamp::parse("2023-09-11T16:05:56.000Z").unwrap();
FactorSourceCommon::with_values(
FactorSourceCryptoParameters::olympia(),
date.clone(),
Expand All @@ -178,6 +177,7 @@ mod tests {
use std::collections::BTreeSet;

use chrono::NaiveDateTime;
use iso8601_timestamp::Timestamp;
use wallet_kit_common::{json::assert_eq_after_json_roundtrip, utils::factory::now};

use crate::v100::factors::{
Expand All @@ -197,14 +197,14 @@ mod tests {

#[test]
fn new_uses_now_as_date() {
let date0 = now();
let date0 = Timestamp::now_utc();
let model = FactorSourceCommon::new(FactorSourceCryptoParameters::default(), []);
let mut date1 = now();
let mut date1 = Timestamp::now_utc();
for _ in 0..10 {
// rust is too fast... lol.
date1 = now();
date1 = Timestamp::now_utc();
}
let do_test = |d: NaiveDateTime| {
let do_test = |d: Timestamp| {
assert!(d > date0);
assert!(d < date1);
};
Expand All @@ -214,8 +214,7 @@ mod tests {

#[test]
fn json_roundtrip() {
let date =
NaiveDateTime::parse_from_str("2023-09-11T16:05:56", "%Y-%m-%dT%H:%M:%S").unwrap();
let date = Timestamp::parse("2023-09-11T16:05:56.000Z").unwrap();
let model = FactorSourceCommon::with_values(
FactorSourceCryptoParameters::default(),
date.clone(),
Expand Down Expand Up @@ -268,7 +267,7 @@ mod tests {
#[test]
fn set_last_used_on() {
let sut = FactorSourceCommon::placeholder_main_babylon();
let d = now();
let d = Timestamp::now_utc();
assert_ne!(sut.last_used_on(), d);
sut.set_last_used_on(d);
assert_eq!(sut.last_used_on(), d);
Expand Down
Loading

0 comments on commit bcea5f6

Please sign in to comment.