From aeddbb5d711631045e15d9dfc77c8144a2b51a4b Mon Sep 17 00:00:00 2001 From: onyinyang Date: Sun, 17 Dec 2023 23:43:16 -0500 Subject: [PATCH] Add error handling and Invitation expiry getter for wasm --- crates/lox-utils/src/lib.rs | 5 +++++ crates/lox-wasm/index.js | 8 ++++++-- crates/lox-wasm/src/lib.rs | 27 ++++++++++++++++++++++----- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/crates/lox-utils/src/lib.rs b/crates/lox-utils/src/lib.rs index 62b04ae..bb0bde2 100644 --- a/crates/lox-utils/src/lib.rs +++ b/crates/lox-utils/src/lib.rs @@ -84,6 +84,11 @@ pub struct LoxCredential { pub invitation: Option, } +#[derive(Debug, Deserialize, Serialize)] +pub struct IssuedInvitation { + pub invitation: Invitation, +} + // This should also check the pubkey pub fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> { invite.try_into() diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index dd25abf..52e781f 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -16,7 +16,7 @@ import init, { handle_check_blockage, blockage_migration, handle_blockage_migration, - set_panic_hook, get_last_upgrade_time, get_trust_level, get_invites_remaining, get_invite_expiry} from "./pkg/lox_wasm.js"; + set_panic_hook, get_last_upgrade_time, get_trust_level, get_invites_remaining, get_issued_invite_expiry, get_received_invite_expiry} from "./pkg/lox_wasm.js"; let pubkeys = await simple_request("/pubkeys"); console.log("Got pubkeys: " + pubkeys); @@ -47,7 +47,7 @@ let info_two = get_trust_level(open_lox_cred); console.log("Last upgrade time: "+info_two); let info_three = get_invites_remaining(open_lox_cred); console.log("Last upgrade time: "+info_three); -let info_four = get_invite_expiry(open_lox_cred); +let info_four = get_issued_invite_expiry(open_lox_cred); console.log("Last upgrade time: "+info_four); let requested_trust_promo = trust_promotion(open_lox_cred, pubkeys); @@ -142,6 +142,10 @@ let prepared_invitation = prepare_invite(lox_cred); // Trusted Invitation Request let requested_invitation = redeem_invite(prepared_invitation, pubkeys); // Redeem an Invitation cred + +info_four = get_received_invite_expiry(prepared_invitation); +console.log("Last upgrade time: "+info_four); + let lox_cred_from_invite = await init().then(() => { set_panic_hook(); let cred = requested_cred("/redeem", requested_invitation).then((response)=> { diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 1c4d325..51abf22 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -520,7 +520,6 @@ pub fn get_last_upgrade_time(lox_cred_str: String) -> String { serde_json::to_string(&date_time).unwrap() } - #[wasm_bindgen] pub fn get_trust_level(lox_cred_str: String) -> String { let lox_cred: lox_utils::LoxCredential = serde_json::from_str(&lox_cred_str).unwrap(); @@ -544,14 +543,32 @@ pub fn get_invites_remaining(lox_cred_str: String) -> String { } #[wasm_bindgen] -pub fn get_invite_expiry(lox_cred_str: String) -> String { +pub fn get_issued_invite_expiry(lox_cred_str: String) -> String { let lox_cred: lox_utils::LoxCredential = serde_json::from_str(&lox_cred_str).unwrap(); - let expiry = (scalar_u32(&lox_cred.invitation.unwrap().date).unwrap()+15) as i32; - let date_time = JulianDay::new(expiry as i32).to_date(); + match lox_cred.invitation { + Some(invitation) => { + let expiry = (scalar_u32(&invitation.date).unwrap() + 15) as i32; + let date_time = JulianDay::new(expiry).to_date(); + println!("Datetime is: {:?}", date_time); + log(&format!( + "Invitation Expiry {}", + serde_json::to_string(&date_time).unwrap() + )); + serde_json::to_string(&date_time).unwrap() + } + None => serde_json::to_string("No Invitation Issued").unwrap(), + } +} + +#[wasm_bindgen] +pub fn get_received_invite_expiry(invite_cred_str: String) -> String { + let invite_cred: lox_utils::IssuedInvitation = serde_json::from_str(&invite_cred_str).unwrap(); + let expiry = (scalar_u32(&invite_cred.invitation.date).unwrap() + 15) as i32; + let date_time = JulianDay::new(expiry).to_date(); println!("Datetime is: {:?}", date_time); log(&format!( "Invitation Expiry {}", serde_json::to_string(&date_time).unwrap() )); serde_json::to_string(&date_time).unwrap() -} \ No newline at end of file +}