168 lines
4.7 KiB
JavaScript
168 lines
4.7 KiB
JavaScript
import init, {
|
|
open_invite,
|
|
handle_new_lox_credential,
|
|
trust_promotion,
|
|
handle_trust_promotion,
|
|
trust_migration,
|
|
handle_trust_migration,
|
|
set_panic_hook } from "./pkg/lox_wasm.js";
|
|
let pubkeys = await request_pubkeys();
|
|
|
|
let requested_invite = await init().then(() => {
|
|
set_panic_hook();
|
|
let requested_invite = request_open_invite().then((token) => {
|
|
return open_invite(token);
|
|
});
|
|
return requested_invite;
|
|
});
|
|
console.log("Got request and state: "+requested_invite);
|
|
|
|
let open_lox_cred = await init().then(() => {
|
|
set_panic_hook();
|
|
let cred = requested_cred("/openreq", requested_invite).then((response) => {
|
|
return handle_new_lox_credential(requested_invite, response, pubkeys);
|
|
});
|
|
return cred;
|
|
});
|
|
|
|
let requested_trust_promo = trust_promotion(open_lox_cred, pubkeys);
|
|
|
|
let trust_promo_cred = await init().then(() => {
|
|
set_panic_hook();
|
|
let cred = requested_cred("/trustpromo", requested_trust_promo).then((response)=> {
|
|
return handle_trust_promotion(requested_trust_promo, response);
|
|
});
|
|
return cred;
|
|
});
|
|
|
|
let reachability_cred = await request_reachability();
|
|
|
|
let requested_trust_migration = trust_migration(open_lox_cred, trust_promo_cred, pubkeys);
|
|
|
|
let level_1_cred = await init().then(() => {
|
|
set_panic_hook();
|
|
let cred = requested_cred("/trustmig", requested_trust_migration).then((response)=> {
|
|
return handle_trust_migration(requested_trust_migration, response, pubkeys);
|
|
});
|
|
return cred;
|
|
});
|
|
|
|
function requested_cred(command, requested) {
|
|
return new Promise((fulfill, reject) => {
|
|
let req = JSON.parse(requested);
|
|
loxServerPostRequest(command, req.request).then((response) => {
|
|
console.log("Got new Trust Migration Lox Credential: " + JSON.stringify(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) => {
|
|
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();
|
|
});
|
|
});
|
|
}
|
|
|
|
function request_pubkeys() {
|
|
return new Promise((fulfill, reject) => {
|
|
loxServerGetRequest("/pubkeys").then((response) => {
|
|
console.log("Got pubkeys: " + JSON.stringify(response));
|
|
fulfill(JSON.stringify(response));
|
|
return;
|
|
}).catch(() => {
|
|
console.log("Error requesting open invite from Lox server");
|
|
reject();
|
|
});
|
|
});
|
|
}
|
|
|
|
function request_reachability() {
|
|
return new Promise((fulfill, reject) => {
|
|
loxServerGetRequest("/reachability").then((response) => {
|
|
console.log("Got reachability Cred: " + JSON.stringify(response));
|
|
fulfill(JSON.stringify(response));
|
|
return;
|
|
}).catch(() => {
|
|
console.log("Error requesting bucket reachability credential from Lox server");
|
|
reject();
|
|
});
|
|
});
|
|
}
|
|
|
|
function loxServerGetRequest(data) {
|
|
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('GET', "http://localhost:8001"+data);
|
|
} catch (err) {
|
|
console.log("Error connecting to lox bridge db");
|
|
reject();
|
|
return;
|
|
}
|
|
xhr.send();
|
|
});
|
|
}
|
|
|
|
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) => {
|
|
|
|
|
|
})
|
|
}
|