From dfa2ae4c6f926bdcbf1dc1db230d7715f5e06ae4 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Mon, 30 Jan 2023 18:13:17 -0500 Subject: [PATCH] Handle open invite credential --- crates/lox-wasm/index.js | 73 +++++++++++++++++++++++++++++++------- crates/lox-wasm/src/lib.rs | 2 +- 2 files changed, 61 insertions(+), 14 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 8aa6a2a..0110e33 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -1,4 +1,4 @@ -import init, { open_invite, set_panic_hook } from "./pkg/lox_wasm.js"; +import init, { open_invite, handle_new_lox_credential, set_panic_hook } from "./pkg/lox_wasm.js"; let pubkeys = await request_pubkeys(); console.log(pubkeys); @@ -9,16 +9,36 @@ let requested = await init().then(() => { return open_invite(token); }); return requested; -// unsigned_open_lox_credential = handle_new_lox_credential(requested, pubkeys); -// lox_credential = request_new_lox_credential(request_cred[0]).then((lox_cred) => { -// handle_new_lox_credential(request_cred[1], response, pubkey); -// }) }); console.log("Got request and state "+requested); +let open_lox_cred = await init().then(() => { + set_panic_hook(); + let cred = request_new_lox_credential(requested).then((response) => { + return handle_new_lox_credential(requested, response, pubkeys); + }); + return cred; +}); +console.log("Got request and state "+requested); + +function request_new_lox_credential(requested) { + let req = JSON.parse(requested); + console.log("Request? "+req.request); + return new Promise((fulfill, reject) => { + loxServerPostRequest("/openreq", req.request).then((response) => { + console.log("Got new Lox Credential: " + response); + fulfill(response); + return; + }).catch(() => { + console.log("Error requesting new Lox credential from server"); + reject(); + }); + }); +} + function request_open_invite() { return new Promise((fulfill, reject) => { - loxServerRequest("/invite").then((response) => { + loxServerGetRequest("/invite").then((response) => { console.log("Got invitation token: " + response.invite); fulfill(response.invite); return; @@ -31,8 +51,8 @@ function request_open_invite() { function request_pubkeys() { return new Promise((fulfill, reject) => { - loxServerRequest("/pubkeys").then((response) => { - console.log("Got pubkeys: " + JSON.parse(response)); + loxServerGetRequest("/pubkeys").then((response) => { + console.log("Got pubkeys: " + response); fulfill(JSON.parse(response)); return; }).catch(() => { @@ -42,9 +62,9 @@ function request_pubkeys() { }); } -function loxServerRequest(data) { +function loxServerGetRequest(data) { return new Promise((fulfill, reject) => { - const xhr = new XMLHttpRequest(data); + const xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.DONE !== xhr.readyState) { return; @@ -61,9 +81,6 @@ function loxServerRequest(data) { }; try { xhr.open('GET', "http://localhost:8001"+data); - //xhr.open("Post", "http://localhost:8001"+data) - //xhr.setRequestHeader("Content-Type", "application/json"); - //var data = JSON.stringify({}) } catch (err) { console.log("Error connecting to lox bridge db"); reject(); @@ -73,6 +90,36 @@ function loxServerRequest(data) { }); } +function loxServerPostRequest(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; + } + const response = JSON.parse(xhr.responseText); + console.log("Received: "+response); + fulfill(response); + 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) { diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 6233666..a3df463 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -58,7 +58,7 @@ pub fn open_invite(invite: &[u8]) -> Result { #[wasm_bindgen] pub fn handle_new_lox_credential(open_lox_result: String, open_lox_response: String, lox_pub: String) -> Result { unsafe { - log(&format!("Using server response: {:?}", open_lox_result)); + log(&format!("Using server response: {:?}", open_lox_result)); } let req_state: ReqState = serde_json::from_str(&open_lox_result).unwrap(); let deserialized_state = req_state.state;