diff --git a/crates/lox-wasm/Cargo.toml b/crates/lox-wasm/Cargo.toml index 93c7d3c..e5712e5 100644 --- a/crates/lox-wasm/Cargo.toml +++ b/crates/lox-wasm/Cargo.toml @@ -23,6 +23,7 @@ serde-wasm-bindgen = "0.4.5" console_error_panic_hook = "0.1.7" js-sys = "0.3.61" rand = { version = "0.7", features = ["wasm-bindgen"] } +zkp = "0.8.0" [dependencies.chrono] version = "0.4.19" diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 5755e3b..bfde485 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -65,6 +65,35 @@ let level_two_cred = await init().then(() => { return cred; }); +//Update reachability cred + encrypted_table = await simple_request("/reachability"); + console.log("Got Encrypted Table: " + encrypted_table); + let requested_level_three = level_up(level_two_cred, encrypted_table, pubkeys); + + let level_three_cred = await init().then(() => { + set_panic_hook(); + let cred = requested_cred("/levelup", requested_level_three).then((response)=> { + console.log("Got new Level 3 Lox Credential: " + response); + return handle_level_up(requested_level_three, response, pubkeys); + }); + return cred; + }); + + +//Update reachability cred +encrypted_table = await simple_request("/reachability"); +console.log("Got Encrypted Table: " + encrypted_table); +let requested_level_four = level_up(level_three_cred, encrypted_table, pubkeys); + +let level_four_cred = await init().then(() => { + set_panic_hook(); + let cred = requested_cred("/levelup", requested_level_four).then((response)=> { + console.log("Got new Level 4 Lox Credential: " + response); + return handle_level_up(requested_level_four, 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 0bd94a3..4701b70 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -6,7 +6,7 @@ use lox::bridge_table::{BridgeLine,from_scalar,BridgeTable, ENC_BUCKET_BYTES}; 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}; +use lox::{IssuerPubKey, OPENINV_LENGTH, scalar_u32}; use serde::{Deserialize, Serialize}; use serde_with::{serde_as}; use serde_json; @@ -14,6 +14,7 @@ use serde_json; use std::array::TryFromSliceError; use std::{panic}; use wasm_bindgen::prelude::*; +use zkp::ProofError; #[derive(Deserialize, Serialize)] @@ -74,6 +75,23 @@ fn test_today(days: i64) -> u32 { JulianDay::from(naive_now_plus).inner().try_into().unwrap() } + +//pub const MAX_LEVEL: usize = 4; +//pub const LEVEL_INTERVAL: [u32; MAX_LEVEL + 1] = [0, 14, 28, 56, 84]; +fn calc_test_days(lox_cred: &Lox) -> Result { + let trust_level: i64 = match scalar_u32(&lox_cred.trust_level) { + Some(v) => v as i64, + None => return Err(ProofError::VerificationFailure), + }; + let mut total = 31; + // for level in 0..trust_level { + // let level_interval: u32 = LEVEL_INTERVAL[trust_level as usize]; + // total += level_interval; + total += trust_level*85; + // } + Ok(total) + } + #[wasm_bindgen] extern "C" { #[wasm_bindgen(js_namespace = console)] @@ -269,13 +287,22 @@ pub fn level_up(level_one_cred: String, encrypted_table: String, lox_pub: String // In this case, the maximum of 85 can be used to test all level ups // in production this should just use the today() function // decrypt trust level and use to calculate the correct date for now + // The trust level has to be at least 1 + + let test_cumulative_days = match calc_test_days(&lox_cred) { + Ok(v) => v, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; + log(&format!( - "TEST ONLY: Add 31 (open invitation) + x*85 days to today's date: {}", - test_today(31+85) + "TEST ONLY: Add 31 (open invitation) + Trust Level*85 days to today's date: {}", test_today(test_cumulative_days) )); 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, test_today(31+85)) { + match level_up::request(&lox_cred, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, test_today(test_cumulative_days)) { Ok(lu_result) => lu_result, Err(e) => { log(&format!("Error: {:?}", e.to_string())); @@ -295,6 +322,7 @@ pub fn level_up(level_one_cred: String, encrypted_table: String, lox_pub: String Ok(serde_json::to_string(&req_state).unwrap()) } + #[wasm_bindgen] pub fn handle_level_up( levelup_request: String,