lox/crates/lox-wasm/index.js

129 lines
3.5 KiB
JavaScript
Raw Normal View History

2023-01-30 18:13:17 -05:00
import init, { open_invite, handle_new_lox_credential, set_panic_hook } from "./pkg/lox_wasm.js";
let pubkeys = await request_pubkeys();
console.log(pubkeys);
let requested = await init().then(() => {
2023-01-13 14:16:37 -05:00
set_panic_hook();
let requested = request_open_invite().then((token) => {
return open_invite(token);
2023-01-13 14:16:37 -05:00
});
return requested;
2023-01-13 14:16:37 -05:00
});
console.log("Got request and state "+requested);
2023-01-13 14:16:37 -05:00
2023-01-30 18:13:17 -05:00
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) {
return new Promise((fulfill, reject) => {
2023-01-30 18:13:17 -05:00
let req = JSON.parse(requested);
console.log("Request? "+req.request);
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();
});
});
}
2023-01-13 14:16:37 -05:00
function request_open_invite() {
return new Promise((fulfill, reject) => {
2023-01-30 18:13:17 -05:00
loxServerGetRequest("/invite").then((response) => {
console.log("Got invitation token: " + response.invite);
fulfill(response.invite);
return;
}).catch(() => {
console.log("Error requesting open invite from Lox server");
reject();
});
});
}
2023-01-25 15:02:53 -05:00
function request_pubkeys() {
return new Promise((fulfill, reject) => {
2023-01-30 18:13:17 -05:00
loxServerGetRequest("/pubkeys").then((response) => {
console.log("Got pubkeys: " + JSON.stringify(response));
fulfill(JSON.stringify(response));
2023-01-25 15:02:53 -05:00
return;
}).catch(() => {
console.log("Error requesting open invite from Lox server");
reject();
});
});
}
2023-01-30 18:13:17 -05:00
function loxServerGetRequest(data) {
2023-01-13 14:16:37 -05:00
return new Promise((fulfill, reject) => {
2023-01-30 18:13:17 -05:00
const xhr = new XMLHttpRequest();
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 {
2023-01-25 15:02:53 -05:00
xhr.open('GET', "http://localhost:8001"+data);
2023-01-13 14:16:37 -05:00
} catch (err) {
console.log("Error connecting to lox bridge db");
reject();
return;
}
2023-01-25 15:02:53 -05:00
xhr.send();
2023-01-13 14:16:37 -05:00
});
}
2023-01-30 18:13:17 -05:00
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);
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) {
return new Promise((fulfull, reject) => {
})
}