Improve testing branch for Lox distributor/wasm interactions

Closes #62 (closed) and should provide the functionality required for #67 so that we can #77 (closed)

Adding the end points revealed a bug in the lox-library's blocking function (or possibly elsewhere) that I need to make an issue for an get to the bottom of.

Merge request reports

Loading
+45 −6
Changes for crates/lox-wasm/src/lib.rs: 45 added lines, 6 removed lines.
Original line number Diff line number Diff line
use chrono::{DateTime, NaiveDateTime, NaiveTime, Utc};
use julianday::JulianDay;
use lox_library::bridge_table::{BridgeLine, MAX_BRIDGES_PER_BUCKET};
use lox_library::cred::{Invitation, Migration};
use lox_library::proto::{
    blockage_migration, check_blockage, issue_invite, level_up, migration, open_invite,
@@ -104,7 +105,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 lox_cred: lox_utils::LoxCredential = match serde_json::from_str(&open_lox_cred) {
        Ok(lox_cred) => lox_cred,
        Err(e) => return Err(JsValue::from(e.to_string())),
@@ -113,8 +118,15 @@ 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(&lox_cred.lox_credential, &pubkeys.lox_pub, today()) {
        match trust_promotion::request(&lox_cred.lox_credential, &pubkeys.lox_pub, server_today) {
            Ok(tp_result) => tp_result,
            Err(e) => {
                log(&format!("Error: {:?}", e.to_string()));
@@ -258,6 +270,7 @@ pub fn level_up(
    level_one_cred: String,
    encrypted_table: String,
    lox_pub: String,
    server_today: u32,
) -> Result<String, JsValue> {
    let lox_cred: lox_utils::LoxCredential = match serde_json::from_str(&level_one_cred) {
        Ok(lox_cred) => lox_cred,
@@ -270,12 +283,19 @@ pub fn level_up(
    let reach_cred =
        lox_utils::generate_reachability_cred(&lox_cred.lox_credential, encrypted_table);

    // 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(
        &lox_cred.lox_credential,
        &reach_cred,
        &pubkeys.lox_pub,
        &pubkeys.reachability_pub,
        today(),
        server_today,
    ) {
        Ok(lu_result) => lu_result,
        Err(e) => {
@@ -346,6 +366,7 @@ pub fn issue_invite(
    trusted_cred: String,
    encrypted_table: String,
    lox_pub: String,
    server_today: u32,
) -> Result<String, JsValue> {
    let lox_cred: lox_utils::LoxCredential = match serde_json::from_str(&trusted_cred) {
        Ok(lox_cred) => lox_cred,
@@ -363,7 +384,7 @@ pub fn issue_invite(
        &reach_cred,
        &pubkeys.lox_pub,
        &pubkeys.reachability_pub,
        today(),
        server_today,
    ) {
        Ok(issue_result) => issue_result,
        Err(e) => {
@@ -452,7 +473,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 invitation_cred: Invitation = match serde_json::from_str(&invitation) {
        Ok(invitation_cred) => invitation_cred,
        Err(e) => return Err(JsValue::from(e.to_string())),
@@ -462,7 +487,7 @@ pub fn redeem_invite(invitation: String, lox_pub: String) -> Result<String, JsVa
        Err(e) => return Err(JsValue::from(e.to_string())),
    };
    let redeem_result =
        match redeem_invite::request(&invitation_cred, &pubkeys.invitation_pub, today()) {
        match redeem_invite::request(&invitation_cred, &pubkeys.invitation_pub, server_today) {
            Ok(redeem_result) => redeem_result,
            Err(e) => {
                log(&format!("Error: {:?}", e.to_string()));
@@ -941,6 +966,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) {
+88 −9
Changes for crates/lox-wasm/index.js: 88 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ import init, {
  handle_check_blockage,
  blockage_migration,
  handle_blockage_migration,
  get_bridge_fingerprint,
  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";
let pubkeys = await simple_request("/pubkeys");
console.log("Got pubkeys: " + pubkeys);
@@ -51,7 +52,15 @@ 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);
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 = 31;
    let req = requested_days("/advancedays", num.toString()).then((today)=> {
        console.log("Advanced days by "+num+" days to: "+ today.toString());
        return  trust_promotion(open_lox_cred, pubkeys, today);
    });
    return req;
});

// Get Migration credential for Trust Promotion from Trust Level 0 -> 1
let trust_promo_cred = await init().then(() => {
@@ -76,9 +85,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 = await level_up(lox_cred, encrypted_table, pubkeys, server_today);

// Level Up to Trust Level 2
lox_cred = await init().then(() => {
@@ -90,10 +105,16 @@ lox_cred = await init().then(() => {
  return cred;
  });

// Update reachability 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());

  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 = await level_up(lox_cred, encrypted_table, pubkeys, server_today);

// Level Up to Trust Level 3
  lox_cred = await init().then(() => {
@@ -108,11 +129,15 @@ lox_cred = await init().then(() => {
let info_three = get_invites_remaining(lox_cred);
console.log("Last upgrade time: "+info_three);

// Update reachability cred
  // 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());

  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 = await level_up(lox_cred, encrypted_table, pubkeys, server_today);
// Level Up to Trust Level 4
lox_cred = await init().then(() => {
  set_panic_hook();
@@ -126,7 +151,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,13 +165,13 @@ 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);
console.log("Last upgrade time: "+info_four);

let lox_cred_from_invite = await init().then(() => {
let _lox_cred_from_invite = await init().then(() => {
  set_panic_hook();
  let cred = requested_cred("/redeem", requested_invitation).then((response)=> {
    console.log("Got new Trusted Lox Credential Invite: " + response);
@@ -155,8 +180,19 @@ let lox_cred_from_invite = await init().then(() => {
  return cred;
  });

  let bridgelines = get_bridgelines_from_bucket(lox_cred, encrypted_table);
  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
  let check_migration_cred = await init().then(() => {
    set_panic_hook();
@@ -193,6 +229,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(JSON.stringify(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 +297,35 @@ 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) {
Loading