From d8cdf3ccab4cfc44069d2ba0a9db2ef3061c0ad0 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Tue, 14 Feb 2023 17:08:09 -0500 Subject: [PATCH] Logic for level up, reachability credential unimplemented --- crates/lox-wasm/index.js | 17 +++++++- crates/lox-wasm/src/lib.rs | 79 ++++++++++++++++++++++++++++++++++---- 2 files changed, 86 insertions(+), 10 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 7a5e8a1..423271b 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -5,6 +5,8 @@ import init, { handle_trust_promotion, trust_migration, handle_trust_migration, + level_up, + handle_level_up, set_panic_hook } from "./pkg/lox_wasm.js"; let pubkeys = await simple_request("/pubkeys"); console.log("Got pubkeys: " + pubkeys); @@ -38,12 +40,11 @@ let trust_promo_cred = await init().then(() => { return cred; }); -let reachability_cred = await simple_request("/reachability"); console.log("Got reachability credential: " + reachability_cred); let requested_trust_migration = trust_migration(open_lox_cred, trust_promo_cred, pubkeys); -let level_1_cred = await init().then(() => { +let level_one_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/trustmig", requested_trust_migration).then((response)=> { console.log("Got new Level 1 Lox Credential: " + response); @@ -52,6 +53,18 @@ let level_1_cred = await init().then(() => { return cred; }); +let reachability_cred = await simple_request("/reachability"); +let requested_level_two = level_up(level_one_cred, reachability_cred, pubkeys); + +let level_two_cred = await init().then(() => { + set_panic_hook(); + let cred = requested_cred("/levelup", requested_level_two).then((response)=> { + console.log("Got new Level 2 Lox Credential: " + response); + return handle_level_up(requested_level_two, response, pubkeys); + }); + return cred; + }); + function requested_cred(command, requested) { return new Promise((fulfill, reject) => { let req = JSON.parse(requested); diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 7308a1f..be96107 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -1,7 +1,7 @@ use chrono::{Duration, Utc}; use julianday::JulianDay; use lox::bridge_table::BridgeLine; -use lox::cred::Lox; +use lox::cred::{BucketReachability, Lox, Migration}; use lox::proto::{open_invite, trust_promotion, migration, level_up, issue_invite, redeem_invite, check_blockage, blockage_migration}; use lox::{IssuerPubKey, OPENINV_LENGTH}; @@ -30,6 +30,11 @@ struct MigReqState { state: migration::State, } +#[derive(Deserialize, Serialize)] +struct LevelupReqState { + request: level_up::Request, + state: level_up::State, +} #[derive(Debug, Deserialize, Serialize)] struct PubKeys { @@ -192,11 +197,8 @@ pub fn handle_trust_promotion( pub fn trust_migration(open_lox_cred: String, trust_promo_cred: String, lox_pub: String) -> 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 mig_cred: Migration = serde_json::from_str(&trust_promo_cred).unwrap(); 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) => { @@ -227,7 +229,7 @@ pub fn handle_trust_migration( 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 = + let level_one_cred = match migration::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) { Ok(level_1_cred) => level_1_cred, Err(e) => { @@ -238,10 +240,71 @@ pub fn handle_trust_migration( unsafe { log(&format!( "Got new Level 1 Credential: {}", - serde_json::to_string(&level_1_cred).unwrap() + serde_json::to_string(&level_one_cred).unwrap() )); } - Ok(serde_json::to_string(&level_1_cred).unwrap()) + Ok(serde_json::to_string(&level_one_cred).unwrap()) +} + +#[wasm_bindgen] +pub fn level_up(level_one_cred: String, reachability_cred: String, lox_pub: String) -> Result { + let lox_cred: Lox = serde_json::from_str(&level_one_cred).unwrap(); + let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + let reach_cred: BucketReachability = serde_json::from_str(&reachability_cred).unwrap(); + // To test level up of the credential we need to advance the day to the correct interval + // In this case, the maximum of 85 can be used to test all level ups + // in production this should just use the today() function + log(&format!( + "TEST ONLY: Add 85 days to today's date: {}", + add_today(85) + )); + let lu_result = + //CHANGE add_today(31) to today() for production + match level_up::request(&lox_cred, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, add_today(85)) { + Ok(lu_result) => lu_result, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; + let req_state = LevelupReqState { + request: lu_result.0, + state: lu_result.1, + }; + unsafe { + 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: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + let req_state: 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) => level_up_cred, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; + unsafe { + log(&format!( + "Got new Level Up Credential: {}", + serde_json::to_string(&level_up_cred).unwrap() + )); + } + Ok(serde_json::to_string(&level_up_cred).unwrap()) }