ソースを参照

Update lox-wasm for lox-extensions

onyinyang 10 ヶ月 前
親
コミット
b354f3f85d
2 ファイル変更、178 行追加、29 行削除
  1. 110 14
      crates/lox-wasm/index.js
  2. 68 15
      crates/lox-wasm/src/lib.rs

+ 110 - 14
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_next_unlock, get_received_invite_expiry, get_bridgelines_from_bucket} from "./pkg/lox_wasm.js";
+  set_panic_hook, get_last_upgrade_time, get_trust_level, get_invites_remaining, get_next_unlock, get_received_invite_expiry, get_bridgelines_from_bucket, get_bridge_fingerprint} from "./pkg/lox_wasm.js";
 let pubkeys = await simple_request("/pubkeys");
 console.log("Got pubkeys: " + pubkeys);
 let constants = await simple_request("/constants");
@@ -26,7 +26,7 @@ console.log("Got constants: " + constants);
 let requested_invite = await init().then(() => {
   set_panic_hook();
   let requested_invite = request_open_invite().then((token) => {
-    return open_invite(token);
+    return open_invite(token, pubkeys);
   });
   return requested_invite;
 });
@@ -36,9 +36,9 @@ console.log("Got request and state: "+requested_invite);
 // Trust Level 0
 let open_lox_cred = await init().then(() => {
   set_panic_hook();
-  let cred = requested_cred("/openreq", requested_invite).then((response) => {
+  let cred = open_requested_cred("/openreq", requested_invite).then((response) => {
     console.log("Got new Level 0 Lox Credential: " + response);
-    return handle_new_lox_credential(requested_invite, response, pubkeys);
+    return handle_new_lox_credential(requested_invite, response);
   });
   return cred;
 });
@@ -49,22 +49,33 @@ get_trust_level(open_lox_cred);
 get_invites_remaining(open_lox_cred);
 
 let encrypted_table = await simple_request("/reachability");
-let info_five = get_bridgelines_from_bucket(open_lox_cred, encrypted_table);
+let info_five = get_bridgelines_from_bucket(open_lox_cred, encrypted_table, pubkeys);
 console.log("Bridgelines available: "+info_five);
-let requested_trust_promo = trust_promotion(open_lox_cred, pubkeys);
+let requested_trust_promo = await init().then(() => {
+    set_panic_hook();
+    let num = 35;
+    let req = requested_days("/advancedays", num.toString()).then((today)=> {
+        console.log("Advanced days by "+num+" days to: "+today);
+        
+    return trust_promotion(open_lox_cred, pubkeys, today);
+    });
+    return req;
+});
+
+console.log("Got request and state: "+requested_trust_promo);
 
 // Get Migration credential for Trust Promotion from Trust Level 0 -> 1
 let trust_promo_cred = await init().then(() => {
   set_panic_hook();
   let cred = requested_cred("/trustpromo", requested_trust_promo).then((response)=> {
     console.log("Got Migration Credential for Trust Promotion: " + response);
-    return handle_trust_promotion(requested_trust_promo, response);
+    return handle_trust_promotion(requested_trust_promo, response, pubkeys);
   });
   return cred;
   });
 
 
-let requested_trust_migration = trust_migration(open_lox_cred, trust_promo_cred, pubkeys);
+let requested_trust_migration = trust_migration(open_lox_cred, trust_promo_cred);
 
 // Trust Promotion from Trust Level 0 -> 1
 let lox_cred = await init().then(() => {
@@ -75,10 +86,15 @@ let lox_cred = await init().then(() => {
   });
   return cred;
   });
+// Level Up days + 15
+  let num = 15;
+  let server_today = await requested_days("/advancedays", num.toString());
+  console.log("Advanced days by "+num+" days to: "+ server_today.toString());
+
 
 encrypted_table = await simple_request("/reachability");
 console.log("Got Encrypted Table: " + encrypted_table);
-let requested_level_two = level_up(lox_cred, encrypted_table, pubkeys);
+let requested_level_two = level_up(lox_cred, encrypted_table, pubkeys, server_today);
 
 // Level Up to Trust Level 2
 lox_cred = await init().then(() => {
@@ -90,10 +106,16 @@ lox_cred = await init().then(() => {
   return cred;
   });
 
+ // Level Up days + 29
+  num = 29;
+  server_today = await requested_days("/advancedays", num.toString());
+  console.log("Advanced days by "+num+" days to: "+ server_today.toString());
+
+
 // Update reachability cred
   encrypted_table = await simple_request("/reachability");
   console.log("Got Encrypted Table: " + encrypted_table);
-  let requested_level_three = level_up(lox_cred, encrypted_table, pubkeys);
+  let requested_level_three = level_up(lox_cred, encrypted_table, pubkeys, server_today);
 
 // Level Up to Trust Level 3
   lox_cred = await init().then(() => {
@@ -108,10 +130,17 @@ lox_cred = await init().then(() => {
 let info_three = get_invites_remaining(lox_cred);
 console.log("Last upgrade time: "+info_three);
 
+
+  // Level Up days + 57
+  num = 57;
+  server_today = await requested_days("/advancedays", num.toString());
+  console.log("Advanced days by "+num+" days to: "+ server_today.toString());
+
+
 // Update reachability cred
 encrypted_table = await simple_request("/reachability");
 console.log("Got Encrypted Table: " + encrypted_table);
-let requested_level_four = level_up(lox_cred, encrypted_table, pubkeys);
+let requested_level_four = level_up(lox_cred, encrypted_table, pubkeys, server_today);
 
 // Level Up to Trust Level 4
 lox_cred = await init().then(() => {
@@ -126,7 +155,7 @@ lox_cred = await init().then(() => {
 // Update reachability cred
 encrypted_table = await simple_request("/reachability");
 console.log("Got Encrypted Table: " + encrypted_table);
-let requested_issue_invitation = issue_invite(lox_cred, encrypted_table, pubkeys);
+let requested_issue_invitation = issue_invite(lox_cred, encrypted_table, pubkeys, server_today);
 
 // Issue an Invitation cred
 lox_cred = await init().then(() => {
@@ -140,7 +169,7 @@ lox_cred = await init().then(() => {
 
 let prepared_invitation = prepare_invite(lox_cred);
 // Trusted Invitation Request
-let requested_invitation = redeem_invite(prepared_invitation, pubkeys);
+let requested_invitation = redeem_invite(prepared_invitation, pubkeys, server_today);
 // Redeem an Invitation cred
 
 let info_four = get_received_invite_expiry(lox_cred);
@@ -155,6 +184,18 @@ let lox_cred_from_invite = await init().then(() => {
   return cred;
   });
 
+
+  let bridgelines = get_bridgelines_from_bucket(lox_cred, encrypted_table, pubkeys);
+  console.log("Bridgelines available: "+bridgelines);
+  let single_bridgeline = get_bridge_fingerprint(bridgelines, 2);
+  console.log("Bridgelines available: "+single_bridgeline);
+
+  await loxServerBlockageRequest("/blockbridges", JSON.parse(single_bridgeline));
+  let second_bridgeline = get_bridge_fingerprint(bridgelines, 1);
+  console.log("Bridgelines available: "+second_bridgeline);
+
+  await loxServerBlockageRequest("/blockbridges", JSON.parse(second_bridgeline));
+
   let requested_check_blockage = check_blockage(lox_cred, pubkeys);
 
   // Check whether or not a bucket is blocked
@@ -162,7 +203,7 @@ let lox_cred_from_invite = await init().then(() => {
     set_panic_hook();
     let cred = requested_cred("/checkblockage", requested_check_blockage).then((response)=> {
       console.log("Got check blockage Migration Credential: " + response);
-      return handle_check_blockage(requested_check_blockage, response);
+      return handle_check_blockage(requested_check_blockage, response, pubkeys);
     });
     return cred;
     });
@@ -179,6 +220,19 @@ let requested_blockage_migration = blockage_migration(lox_cred, check_migration_
     return cred;
     });
 
+function open_requested_cred(command, requested) {
+  return new Promise((fulfill, reject) => {
+  let req = JSON.parse(requested);
+    loxServerPostRequest(command, req.open_inv_req).then((response) => {
+      fulfill(JSON.stringify(response));
+      return;
+    }).catch(() => {
+      console.log("Error requesting new Lox credential from server");
+      reject();
+    });
+  });
+}
+
 
 function requested_cred(command, requested) {
   return new Promise((fulfill, reject) => {
@@ -193,6 +247,20 @@ function requested_cred(command, requested) {
   });
 }
 
+function requested_days(command, days) {
+  return new Promise((fulfill, reject) => {
+    let req = JSON.parse(days)
+    loxServerPostRequest(command, req).then((response) => {
+      fulfill(response);
+      return;
+    }).catch(() => {
+      console.log("Error requesting new Lox credential from server");
+      reject();
+    });
+  });
+}
+
+
 function request_open_invite() {
   return new Promise((fulfill, reject) => {
     loxServerPostRequest("/invite", null).then((response) => {
@@ -247,6 +315,34 @@ function loxServerPostRequest(data, payload) {
   });
 }
 
+function loxServerBlockageRequest(data, payload) {
+  return new Promise((fulfill, reject) => {
+    const xhr = new XMLHttpRequest();
+    xhr.onreadystatechange = function() {
+      if (xhr.DONE !== xhr.readyState) {
+        return;
+      }
+      if (xhr.status !== 200) {
+        console.log("Error. Status code: "+xhr.status);
+        console.log(xhr);
+        reject();
+        return;
+      }
+      fulfill();
+      return;
+    };
+    try {
+      xhr.open('POST', "http://localhost:8001"+data, true)
+      xhr.setRequestHeader("Content-Type", "application/json");
+    } catch (err) {
+      console.log("Error connecting to lox bridge db");
+      reject();
+      return;
+    }
+    xhr.send(JSON.stringify(payload));
+  });
+}
+
 // The correct key should be matched against a public commit to the key to
 // verify that the key issuer is in fact the correct Bridge Authority
 function loxKeyRequest(key_type) {

+ 68 - 15
crates/lox-wasm/src/lib.rs

@@ -1,5 +1,6 @@
 use chrono::{DateTime, NaiveDateTime, NaiveTime, Utc};
 use julianday::JulianDay;
+use lox_extensions::bridge_table::{BridgeLine, MAX_BRIDGES_PER_BUCKET};
 use lox_extensions::lox_creds::{Invitation, Migration};
 use lox_extensions::proto::{
     blockage_migration, check_blockage, issue_invite, level_up, migration, open_invite,
@@ -8,6 +9,7 @@ use lox_extensions::proto::{
 use lox_extensions::scalar_u32;
 use std::panic;
 use wasm_bindgen::prelude::*;
+extern crate console_error_panic_hook;
 
 // Returns today's Julian date as a u32 value
 fn today() -> u32 {
@@ -29,7 +31,17 @@ pub fn set_panic_hook() {
 // Receives an invite and prepares an open_invite request, returning the
 // Request and State
 #[wasm_bindgen]
-pub fn open_invite(lox_pub: String) -> Result<String, JsValue> {
+pub fn open_invite(base64_invite: String, lox_pub: String) -> Result<String, JsValue> {
+    log(&format!("Using invite: {base64_invite}"));
+    let invite: lox_utils::Invite = match serde_json::from_str(&base64_invite) {
+        Ok(invite) => invite,
+        Err(e) => return Err(JsValue::from(e.to_string())),
+    };
+    let _token = match lox_utils::validate(&invite.invite) {
+        Ok(token) => token,
+        Err(e) => return Err(JsValue::from(e.to_string())),
+    };
+
     let pubkeys: lox_utils::PubKeys = match serde_json::from_str(&lox_pub) {
         Ok(pubkeys) => pubkeys,
         Err(e) => return Err(JsValue::from(e.to_string())),
@@ -39,7 +51,11 @@ pub fn open_invite(lox_pub: String) -> Result<String, JsValue> {
         Ok((request, state)) => (request, state),
         Err(e) => return Err(JsValue::from(e.to_string())),
     };
-    let req_state = lox_utils::OpenReqState { request, state };
+    let open_inv_req = lox_utils::OpenInvReq { request, invite };
+    let req_state = lox_utils::OpenReqState {
+        open_inv_req,
+        state,
+    };
     log(&format!(
         "Formatted open invite request: {}",
         serde_json::to_string(&req_state).unwrap()
@@ -96,7 +112,11 @@ pub fn handle_new_lox_credential(
 }
 
 #[wasm_bindgen]
-pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> Result<String, JsValue> {
+pub fn trust_promotion(
+    open_lox_cred: String,
+    lox_pub: String,
+    server_today: u32,
+) -> Result<String, JsValue> {
     let rng = &mut rand::thread_rng();
     let lox_cred: lox_utils::LoxCredential = match serde_json::from_str(&open_lox_cred) {
         Ok(lox_cred) => lox_cred,
@@ -106,11 +126,17 @@ pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> Result<String,
         Ok(pubkeys) => pubkeys,
         Err(e) => return Err(JsValue::from(e.to_string())),
     };
+    // To test creation of the credential we need to advance the day to 30
+    // in production this should just use the today() function
+    log(&format!(
+        "TEST ONLY: Add 31 days to today's date: {}",
+        server_today
+    ));
     let tp_result = match trust_promotion::request(
         rng,
         lox_cred.lox_credential,
         pubkeys.migrationkey_pub,
-        today(),
+        server_today,
     ) {
         Ok(tp_result) => tp_result,
         Err(e) => {
@@ -244,6 +270,7 @@ pub fn level_up(
     level_one_cred: String,
     encrypted_table: String,
     lox_pub: String,
+    server_today: u32,
 ) -> Result<String, JsValue> {
     let rng = &mut rand::thread_rng();
     let lox_cred: lox_utils::LoxCredential = match serde_json::from_str(&level_one_cred) {
@@ -259,8 +286,15 @@ pub fn level_up(
         encrypted_table,
         pubkeys.reachability_pub,
     );
+    // To test creation of the credential we need to advance the day to 30
+    // in production this should just use the today() function
+    log(&format!(
+        "TEST ONLY: Today's date on Server: {}",
+        server_today
+    ));
 
-    let lu_result = match level_up::request(rng, lox_cred.lox_credential, reach_cred, today()) {
+    let lu_result = match level_up::request(rng, lox_cred.lox_credential, reach_cred, server_today)
+    {
         Ok(lu_result) => lu_result,
         Err(e) => {
             log(&format!("Error: {:?}", e.to_string()));
@@ -321,6 +355,7 @@ pub fn issue_invite(
     trusted_cred: String,
     encrypted_table: String,
     lox_pub: String,
+    server_today: u32,
 ) -> Result<String, JsValue> {
     let rng = &mut rand::thread_rng();
     let lox_cred: lox_utils::LoxCredential = match serde_json::from_str(&trusted_cred) {
@@ -342,7 +377,7 @@ pub fn issue_invite(
         lox_cred.lox_credential,
         reach_cred,
         pubkeys.invitation_pub,
-        today(),
+        server_today,
     ) {
         Ok(issue_result) => issue_result,
         Err(e) => {
@@ -422,7 +457,11 @@ pub fn prepare_invite(invitation_cred: String) -> Result<String, JsValue> {
 
 //
 #[wasm_bindgen]
-pub fn redeem_invite(invitation: String, lox_pub: String) -> Result<String, JsValue> {
+pub fn redeem_invite(
+    invitation: String,
+    lox_pub: String,
+    server_today: u32,
+) -> Result<String, JsValue> {
     let rng = &mut rand::thread_rng();
     let pubkeys: lox_utils::PubKeys = match serde_json::from_str(&lox_pub) {
         Ok(pubkeys) => pubkeys,
@@ -432,14 +471,14 @@ pub fn redeem_invite(invitation: String, lox_pub: String) -> Result<String, JsVa
         Ok(invitation_cred) => invitation_cred,
         Err(e) => return Err(JsValue::from(e.to_string())),
     };
-    let redeem_result = match redeem_invite::request(rng, invitation_cred, pubkeys.lox_pub, today())
-    {
-        Ok(redeem_result) => redeem_result,
-        Err(e) => {
-            log(&format!("Error: {:?}", e.to_string()));
-            return Err(JsValue::from(e.to_string()));
-        }
-    };
+    let redeem_result =
+        match redeem_invite::request(rng, invitation_cred, pubkeys.lox_pub, server_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,
@@ -893,6 +932,20 @@ pub fn get_bridgelines_from_bucket(
     }
 }
 
+#[wasm_bindgen]
+pub fn get_bridge_fingerprint(bridgelines_str: String, index: u32) -> Result<String, JsValue> {
+    let bridgelines: [BridgeLine; MAX_BRIDGES_PER_BUCKET] =
+        match serde_json::from_str(&bridgelines_str) {
+            Ok(bridgelines) => bridgelines,
+            Err(e) => return Err(JsValue::from(e.to_string())),
+        };
+    let bridge = bridgelines.get(index as usize).unwrap();
+    match serde_json::to_string(&bridge) {
+        Ok(converted) => Ok(converted),
+        Err(e) => Err(JsValue::from(e.to_string())),
+    }
+}
+
 #[wasm_bindgen]
 pub fn invitation_is_trusted(unspecified_invitation_str: String) -> Result<bool, JsValue> {
     match serde_json::from_str::<Invitation>(&unspecified_invitation_str) {