lox/crates/lox-wasm/index.js

68 lines
1.8 KiB
JavaScript
Raw Normal View History

2023-01-13 14:16:37 -05:00
import init, { open_invite, set_panic_hook } from "./pkg/lox_wasm.js";
init().then(() => {
set_panic_hook();
requested = request_open_invite().then((token) => {
2023-01-13 14:16:37 -05:00
open_invite(token);
});
// lox_credential = request_new_lox_credential(request_cred[0]).then((lox_cred) => {
// handle_new_lox_credential(request_cred[1], response, pubkey);
// })
2023-01-13 14:16:37 -05:00
});
function request_open_invite() {
return new Promise((fulfill, reject) => {
loxServerRequest("").then((response) => {
console.log("Got invitation token: " + response.invite);
fulfill(response.invite);
return;
}).catch(() => {
console.log("Error requesting open invite from Lox server");
reject();
});
});
}
function loxServerRequest(data) {
2023-01-13 14:16:37 -05:00
return new Promise((fulfill, reject) => {
const xhr = new XMLHttpRequest(data);
2023-01-13 14:16:37 -05:00
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);
fulfill(response);
2023-01-13 14:16:37 -05:00
return;
};
try {
if (data != ""){
2023-01-13 14:16:37 -05:00
xhr.open('GET', "http://localhost:8001");
}
else {
xhr.open("Post", "http://localhost:8001")
xhr.setRequestHeader("Content-Type", "application/json");
var data = JSON.stringify({})
}
2023-01-13 14:16:37 -05:00
} catch (err) {
console.log("Error connecting to lox bridge db");
reject();
return;
}
xhr.send(data);
2023-01-13 14:16:37 -05:00
});
}
// 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) {
return new Promise((fulfull, reject) => {
})
}