diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 3c70961..fa003d9 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -1,4 +1,11 @@ -import init, { open_invite, handle_new_lox_credential, trust_promotion, handle_trust_promotion, set_panic_hook } from "./pkg/lox_wasm.js"; +import init, { + open_invite, + handle_new_lox_credential, + trust_promotion, + handle_trust_promotion, + trust_migration, + handle_trust_migration, + set_panic_hook } from "./pkg/lox_wasm.js"; let pubkeys = await request_pubkeys(); let requested_invite = await init().then(() => { @@ -12,7 +19,7 @@ console.log("Got request and state: "+requested_invite); let open_lox_cred = await init().then(() => { set_panic_hook(); - let cred = request_new_lox_credential(requested_invite).then((response) => { + let cred = requested_cred("/openreq", requested_invite).then((response) => { return handle_new_lox_credential(requested_invite, response, pubkeys); }); return cred; @@ -22,33 +29,29 @@ let requested_trust_promo = trust_promotion(open_lox_cred, pubkeys); let trust_promo_cred = await init().then(() => { set_panic_hook(); - let cred = request_trust_promo_cred(requested_trust_promo).then((response)=> { + let cred = requested_cred("/trustpromo", requested_trust_promo).then((response)=> { return handle_trust_promotion(requested_trust_promo, response); }); return cred; }); - let reachability_cred = await request_reachability(); +let reachability_cred = await request_reachability(); -function request_new_lox_credential(requested_invite) { - return new Promise((fulfill, reject) => { - let req = JSON.parse(requested_invite); - loxServerPostRequest("/openreq", req.request).then((response) => { - console.log("Got new Open Invite Lox Credential: " + JSON.stringify(response)); - fulfill(JSON.stringify(response)); - return; - }).catch(() => { - console.log("Error requesting new Lox credential from server"); - reject(); - }); +let requested_trust_migration = trust_migration(open_lox_cred, trust_promo_cred, pubkeys); + +let level_1_cred = await init().then(() => { + set_panic_hook(); + let cred = requested_cred("/trustmig", requested_trust_migration).then((response)=> { + return handle_trust_migration(requested_trust_migration, response, pubkeys); + }); + return cred; }); -} -function request_trust_promo_cred(requested) { +function requested_cred(command, requested) { return new Promise((fulfill, reject) => { let req = JSON.parse(requested); - loxServerPostRequest("/trustpromo", req.request).then((response) => { - console.log("Got new Trust Promotion Lox Credential: " + JSON.stringify(response)); + loxServerPostRequest(command, req.request).then((response) => { + console.log("Got new Trust Migration Lox Credential: " + JSON.stringify(response)); fulfill(JSON.stringify(response)); return; }).catch(() => { diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 2d78f70..7308a1f 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -2,7 +2,8 @@ use chrono::{Duration, Utc}; use julianday::JulianDay; use lox::bridge_table::BridgeLine; use lox::cred::Lox; -use lox::proto::{open_invite, trust_promotion}; +use lox::proto::{open_invite, trust_promotion, migration, level_up, + issue_invite, redeem_invite, check_blockage, blockage_migration}; use lox::{IssuerPubKey, OPENINV_LENGTH}; use serde::{Deserialize, Serialize}; use serde_json; @@ -23,6 +24,13 @@ struct TrustReqState { state: trust_promotion::State, } +#[derive(Deserialize, Serialize)] +struct MigReqState { + request: migration::Request, + state: migration::State, +} + + #[derive(Debug, Deserialize, Serialize)] struct PubKeys { lox_pub: IssuerPubKey, @@ -38,14 +46,24 @@ struct Credential { bridgeline: BridgeLine, } + +fn today() -> u32 { + let naive_now = Utc::now().date_naive(); + JulianDay::from(naive_now).inner().try_into().unwrap() +} + +// This should only be used for testing, use today in production +fn add_today(sum: i64) -> u32 { + let naive_now_plus = (Utc::now() + Duration::days(sum)).date_naive(); + JulianDay::from(naive_now_plus).inner().try_into().unwrap() +} + #[wasm_bindgen] extern "C" { #[wasm_bindgen(js_namespace = console)] pub fn log(s: &str); } -// Time has to be implemented with wasmbind feature as -// explained here: https://stackoverflow.com/questions/63210984/chrono-kills-my-rust-webassembly-function #[wasm_bindgen] pub fn set_panic_hook() { @@ -75,16 +93,6 @@ pub fn open_invite(invite: &[u8]) -> Result { Ok(serde_json::to_string(&req_state).unwrap()) } -fn today() -> u32 { - let naive_now = Utc::now().date_naive(); - JulianDay::from(naive_now).inner().try_into().unwrap() -} - -fn add_today(sum: i64) -> u32 { - let naive_now_plus = (Utc::now() + Duration::days(sum)).date_naive(); - JulianDay::from(naive_now_plus).inner().try_into().unwrap() -} - #[wasm_bindgen] pub fn handle_new_lox_credential( open_lox_result: String, @@ -134,7 +142,7 @@ pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> Result tp_result, Err(e) => { @@ -148,7 +156,7 @@ pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> Result Result { + let lox_cred: Credential = serde_json::from_str(&open_lox_cred).unwrap(); + let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + let mig_cred = serde_json::from_str(&trust_promo_cred).unwrap(); + // To test creation of the credential we need to advance the day to 30 + // in production this should just use the today() function + let tm_result = + //CHANGE add_today(31) to today() for production + 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 = MigReqState { + request: tm_result.0, + state: tm_result.1, + }; + unsafe { + 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: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + let req_state: 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_1_cred = + match migration::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) { + Ok(level_1_cred) => level_1_cred, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; + unsafe { + log(&format!( + "Got new Level 1 Credential: {}", + serde_json::to_string(&level_1_cred).unwrap() + )); + } + Ok(serde_json::to_string(&level_1_cred).unwrap()) +} + + // This should also check the pubkey fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> { invite.try_into()