use chrono::Utc; use julianday::JulianDay; use lox_library::cred::{Invitation, Migration}; use lox_library::proto::{ blockage_migration, check_blockage, issue_invite, level_up, migration, open_invite, redeem_invite, trust_promotion, }; use lox_library::scalar_u32; use std::panic; use wasm_bindgen::prelude::*; // Returns today's Julian date as a u32 value fn today() -> u32 { let naive_now = Utc::now().date_naive(); JulianDay::from(naive_now).inner().try_into().unwrap() } #[wasm_bindgen] extern "C" { #[wasm_bindgen(js_namespace = console)] pub fn log(s: &str); } #[wasm_bindgen] pub fn set_panic_hook() { panic::set_hook(Box::new(console_error_panic_hook::hook)); } // Receives an invite and prepares an open_invite request, returning the // Request and State #[wasm_bindgen] pub fn open_invite(invite: &[u8]) -> Result { log(&format!("Using invite: {:?}", invite)); let token = match lox_utils::validate(invite) { Ok(token) => token, Err(e) => return Err(JsValue::from(e.to_string())), }; let (request, state) = open_invite::request(&token); let req_state = lox_utils::OpenReqState { request, state }; log(&format!( "Formatted open invite request: {}", serde_json::to_string(&req_state).unwrap() )); Ok(serde_json::to_string(&req_state).unwrap()) } #[wasm_bindgen] pub fn handle_new_lox_credential( open_lox_result: String, open_lox_response: String, lox_pub: String, ) -> Result { let req_state: lox_utils::OpenReqState = serde_json::from_str(&open_lox_result).unwrap(); let deserialized_state = req_state.state; let deserialized_response = serde_json::from_str(&open_lox_response).unwrap(); let pubkeys: lox_utils::PubKeys = serde_json::from_str(&lox_pub).unwrap(); let lox_cred = match open_invite::handle_response( deserialized_state, deserialized_response, &pubkeys.lox_pub, ) { Ok(lox_cred) => lox_cred, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); } }; let lox_cred = lox_utils::LoxCredential { lox_credential: lox_cred.0, bridgeline: Some(lox_cred.1), invitation: None, }; log(&format!( "Got new Lox Credential: {}", serde_json::to_string(&lox_cred.lox_credential).unwrap() )); log(&format!( "Got new bridgeline: {}", serde_json::to_string(&lox_cred.bridgeline).unwrap() )); Ok(serde_json::to_string(&lox_cred).unwrap()) } #[wasm_bindgen] pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> Result { let lox_cred: lox_utils::LoxCredential = serde_json::from_str(&open_lox_cred).unwrap(); let pubkeys: lox_utils::PubKeys = serde_json::from_str(&lox_pub).unwrap(); let tp_result = match trust_promotion::request(&lox_cred.lox_credential, &pubkeys.lox_pub, today()) { Ok(tp_result) => tp_result, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); } }; let req_state = lox_utils::TrustReqState { request: tp_result.0, state: tp_result.1, }; log(&format!( "Formatted Trust Promotion request: {}", serde_json::to_string(&req_state).unwrap() )); Ok(serde_json::to_string(&req_state).unwrap()) } #[wasm_bindgen] pub fn handle_trust_promotion( trust_promo_request: String, trust_promo_response: String, ) -> Result { let req_state: lox_utils::TrustReqState = serde_json::from_str(&trust_promo_request).unwrap(); let deserialized_state = req_state.state; let deserialized_response = serde_json::from_str(&trust_promo_response).unwrap(); let migration_cred = match trust_promotion::handle_response(deserialized_state, deserialized_response) { Ok(migration_cred) => migration_cred, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); } }; log(&format!( "Got new Migration Credential: {}", serde_json::to_string(&migration_cred).unwrap() )); Ok(serde_json::to_string(&migration_cred).unwrap()) } #[wasm_bindgen] pub fn trust_migration( open_lox_cred: String, trust_promo_cred: String, lox_pub: String, ) -> Result { let lox_cred: lox_utils::LoxCredential = serde_json::from_str(&open_lox_cred).unwrap(); let pubkeys: lox_utils::PubKeys = serde_json::from_str(&lox_pub).unwrap(); let mig_cred: Migration = serde_json::from_str(&trust_promo_cred).unwrap(); let tm_result = match migration::request( &lox_cred.lox_credential, &mig_cred, &pubkeys.lox_pub, &pubkeys.migration_pub, ) { Ok(tm_result) => tm_result, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); } }; let req_state = lox_utils::MigReqState { request: tm_result.0, state: tm_result.1, }; log(&format!( "Formatted Trust Migration request: {}", serde_json::to_string(&req_state).unwrap() )); Ok(serde_json::to_string(&req_state).unwrap()) } #[wasm_bindgen] pub fn handle_trust_migration( trust_migration_request: String, trust_migration_response: String, lox_pub: String, ) -> Result { let pubkeys: lox_utils::PubKeys = serde_json::from_str(&lox_pub).unwrap(); let req_state: lox_utils::MigReqState = serde_json::from_str(&trust_migration_request).unwrap(); let deserialized_state = req_state.state; let deserialized_response = serde_json::from_str(&trust_migration_response).unwrap(); let level_one_cred = match migration::handle_response( deserialized_state, deserialized_response, &pubkeys.lox_pub, ) { Ok(level_1_cred) => lox_utils::LoxCredential { lox_credential: level_1_cred, bridgeline: None, invitation: None, }, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); } }; log(&format!( "Got new Level 1 Credential: {}", serde_json::to_string(&level_one_cred).unwrap() )); Ok(serde_json::to_string(&level_one_cred).unwrap()) } #[wasm_bindgen] pub fn level_up( level_one_cred: String, encrypted_table: String, lox_pub: String, ) -> Result { let lox_cred: lox_utils::LoxCredential = serde_json::from_str(&level_one_cred).unwrap(); let pubkeys: lox_utils::PubKeys = serde_json::from_str(&lox_pub).unwrap(); let reach_cred = lox_utils::generate_reachability_cred(&lox_cred.lox_credential, encrypted_table); let lu_result = match level_up::request( &lox_cred.lox_credential, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, today(), ) { Ok(lu_result) => lu_result, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); } }; let req_state = lox_utils::LevelupReqState { request: lu_result.0, state: lu_result.1, }; log(&format!( "Formatted Level Up request: {}", serde_json::to_string(&req_state).unwrap() )); Ok(serde_json::to_string(&req_state).unwrap()) } #[wasm_bindgen] pub fn handle_level_up( levelup_request: String, levelup_response: String, lox_pub: String, ) -> Result { let pubkeys: lox_utils::PubKeys = serde_json::from_str(&lox_pub).unwrap(); let req_state: lox_utils::LevelupReqState = serde_json::from_str(&levelup_request).unwrap(); let deserialized_state = req_state.state; let deserialized_response = serde_json::from_str(&levelup_response).unwrap(); let level_up_cred = match level_up::handle_response( deserialized_state, deserialized_response, &pubkeys.lox_pub, ) { Ok(level_up_cred) => lox_utils::LoxCredential { lox_credential: level_up_cred, bridgeline: None, invitation: None, }, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); } }; log(&format!( "Got new Level Up Credential: {}", serde_json::to_string(&level_up_cred).unwrap() )); Ok(serde_json::to_string(&level_up_cred).unwrap()) } #[wasm_bindgen] pub fn issue_invite( trusted_cred: String, encrypted_table: String, lox_pub: String, ) -> Result { let lox_cred: lox_utils::LoxCredential = serde_json::from_str(&trusted_cred).unwrap(); let pubkeys: lox_utils::PubKeys = serde_json::from_str(&lox_pub).unwrap(); let reach_cred = lox_utils::generate_reachability_cred(&lox_cred.lox_credential, encrypted_table); let issue_result = match issue_invite::request( &lox_cred.lox_credential, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, today(), ) { Ok(issue_result) => issue_result, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); } }; let req_state = lox_utils::IssueInviteReqState { request: issue_result.0, state: issue_result.1, }; log(&format!( "Formatted Issue Invite request: {}", serde_json::to_string(&req_state).unwrap() )); Ok(serde_json::to_string(&req_state).unwrap()) } #[wasm_bindgen] pub fn handle_issue_invite( issue_invite_request: String, issue_invite_response: String, lox_pub: String, ) -> Result { let pubkeys: lox_utils::PubKeys = serde_json::from_str(&lox_pub).unwrap(); let req_state: lox_utils::IssueInviteReqState = serde_json::from_str(&issue_invite_request).unwrap(); let deserialized_state = req_state.state; let deserialized_response = serde_json::from_str(&issue_invite_response).unwrap(); let issue_invite_cred = match issue_invite::handle_response( deserialized_state, deserialized_response, &pubkeys.lox_pub, &pubkeys.invitation_pub, ) { Ok(issue_invite_cred) => issue_invite_cred, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); } }; let invitation_cred = lox_utils::LoxCredential { lox_credential: issue_invite_cred.0, bridgeline: None, invitation: Some(issue_invite_cred.1), }; log(&format!( "Got new Invitation Credential and Lox Credential: {}", serde_json::to_string(&invitation_cred).unwrap() )); Ok(serde_json::to_string(&invitation_cred).unwrap()) } // Separate Trusted Invite from credential prior to passing it to friend #[wasm_bindgen] pub fn prepare_invite(invitation_cred: String) -> String { let cred: lox_utils::LoxCredential = serde_json::from_str(&invitation_cred).unwrap(); log(&format!( "Prepared Invitation: {}", serde_json::to_string(&cred.invitation).unwrap() )); serde_json::to_string(&cred.invitation).unwrap() } // #[wasm_bindgen] pub fn redeem_invite(invitation: String, lox_pub: String) -> Result { let invitation_cred: Invitation = serde_json::from_str(&invitation).unwrap(); let pubkeys: lox_utils::PubKeys = serde_json::from_str(&lox_pub).unwrap(); let redeem_result = match redeem_invite::request(&invitation_cred, &pubkeys.invitation_pub, today()) { Ok(redeem_result) => redeem_result, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); } }; let req_state = lox_utils::RedeemReqState { request: redeem_result.0, state: redeem_result.1, }; log(&format!( "Formatted Redeem Invite request: {}", serde_json::to_string(&req_state).unwrap() )); Ok(serde_json::to_string(&req_state).unwrap()) } #[wasm_bindgen] pub fn handle_redeem_invite( redeem_invite_request: String, redeem_invite_response: String, lox_pub: String, ) -> Result { let pubkeys: lox_utils::PubKeys = serde_json::from_str(&lox_pub).unwrap(); let req_state: lox_utils::RedeemReqState = serde_json::from_str(&redeem_invite_request).unwrap(); let deserialized_state = req_state.state; let deserialized_response = serde_json::from_str(&redeem_invite_response).unwrap(); let redeem_invite_cred = match redeem_invite::handle_response( deserialized_state, deserialized_response, &pubkeys.lox_pub, ) { Ok(issue_invite_cred) => lox_utils::LoxCredential { lox_credential: issue_invite_cred, bridgeline: None, invitation: None, }, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); } }; log(&format!( "Got new Trusted Lox Credential from Invitation: {}", serde_json::to_string(&redeem_invite_cred).unwrap() )); Ok(serde_json::to_string(&redeem_invite_cred).unwrap()) } #[wasm_bindgen] pub fn check_blockage(lox_cred: String, lox_pub: String) -> Result { let lox: lox_utils::LoxCredential = serde_json::from_str(&lox_cred).unwrap(); let pubkeys: lox_utils::PubKeys = serde_json::from_str(&lox_pub).unwrap(); let cb_result = match check_blockage::request(&lox.lox_credential, &pubkeys.lox_pub) { Ok(cb_result) => cb_result, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); } }; let req_state = lox_utils::CheckBlockageReqState { request: cb_result.0, state: cb_result.1, }; log(&format!( "Formatted Check Blockage request: {}", serde_json::to_string(&req_state).unwrap() )); Ok(serde_json::to_string(&req_state).unwrap()) } #[wasm_bindgen] pub fn handle_check_blockage( check_blockage_request: String, check_blockage_response: String, ) -> Result { let req_state: lox_utils::CheckBlockageReqState = serde_json::from_str(&check_blockage_request).unwrap(); let deserialized_state = req_state.state; let deserialized_response = serde_json::from_str(&check_blockage_response).unwrap(); let migration_cred = match check_blockage::handle_response(deserialized_state, deserialized_response) { Ok(migration_cred) => migration_cred, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); } }; log(&format!( "Got new Blockage Migration Credential: {}", serde_json::to_string(&migration_cred).unwrap() )); Ok(serde_json::to_string(&migration_cred).unwrap()) } #[wasm_bindgen] pub fn blockage_migration( lox_cred: String, check_migration_cred: String, lox_pub: String, ) -> Result { let lox_cred: lox_utils::LoxCredential = serde_json::from_str(&lox_cred).unwrap(); let pubkeys: lox_utils::PubKeys = serde_json::from_str(&lox_pub).unwrap(); let mig_cred: Migration = serde_json::from_str(&check_migration_cred).unwrap(); let bm_result = match blockage_migration::request( &lox_cred.lox_credential, &mig_cred, &pubkeys.lox_pub, &pubkeys.migration_pub, ) { Ok(bm_result) => bm_result, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); } }; let req_state = lox_utils::BlockageMigReqState { request: bm_result.0, state: bm_result.1, }; log(&format!( "Formatted Blockage Migration request: {}", serde_json::to_string(&req_state).unwrap() )); Ok(serde_json::to_string(&req_state).unwrap()) } #[wasm_bindgen] pub fn handle_blockage_migration( blockage_migration_request: String, blockage_migration_response: String, lox_pub: String, ) -> Result { let pubkeys: lox_utils::PubKeys = serde_json::from_str(&lox_pub).unwrap(); let req_state: lox_utils::BlockageMigReqState = serde_json::from_str(&blockage_migration_request).unwrap(); let deserialized_state = req_state.state; let deserialized_response = serde_json::from_str(&blockage_migration_response).unwrap(); let lox_cred = match blockage_migration::handle_response( deserialized_state, deserialized_response, &pubkeys.lox_pub, ) { Ok(lox_cred) => lox_utils::LoxCredential { lox_credential: lox_cred, bridgeline: None, invitation: None, }, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); } }; log(&format!( "Got new Lox Credential after Migration: {}", serde_json::to_string(&lox_cred).unwrap() )); Ok(serde_json::to_string(&lox_cred).unwrap()) } #[wasm_bindgen] pub fn get_last_upgrade_time(lox_cred_str: String) -> String { let lox_cred: lox_utils::LoxCredential = serde_json::from_str(&lox_cred_str).unwrap(); let upgrade_date = scalar_u32(&lox_cred.lox_credential.level_since).unwrap(); let date_time = JulianDay::new(upgrade_date as i32).to_date(); log(&format!( "Time of last upgrade {}", serde_json::to_string(&date_time).unwrap() )); 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(); let trust_level = scalar_u32(&lox_cred.lox_credential.trust_level).unwrap(); log(&format!( "Trust level {}", serde_json::to_string(&trust_level).unwrap() )); serde_json::to_string(&trust_level).unwrap() } #[wasm_bindgen] pub fn get_invites_remaining(lox_cred_str: String) -> String { let lox_cred: lox_utils::LoxCredential = serde_json::from_str(&lox_cred_str).unwrap(); let invites = scalar_u32(&lox_cred.lox_credential.invites_remaining); log(&format!( "Invites remaining {}", serde_json::to_string(&invites).unwrap() )); serde_json::to_string(&invites).unwrap() } #[wasm_bindgen] 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(); 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() }