Refactor js to remove duplicate code

This commit is contained in:
onyinyang 2023-02-14 14:45:52 -05:00
parent 944c3634ca
commit f44ac0ae9f
No known key found for this signature in database
GPG Key ID: 156A6435430C2036
1 changed files with 10 additions and 20 deletions

View File

@ -6,7 +6,8 @@ import init, {
trust_migration,
handle_trust_migration,
set_panic_hook } from "./pkg/lox_wasm.js";
let pubkeys = await request_pubkeys();
let pubkeys = await simple_request("/pubkeys");
console.log("Got pubkeys: " + pubkeys);
let requested_invite = await init().then(() => {
set_panic_hook();
@ -20,6 +21,7 @@ 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) => {
console.log("Got new Level 0 Lox Credential: " + response);
return handle_new_lox_credential(requested_invite, response, pubkeys);
});
return cred;
@ -30,18 +32,21 @@ 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)=> {
console.log("Got Migration Credential for Trust Promotion: " + response);
return handle_trust_promotion(requested_trust_promo, response);
});
return cred;
});
let reachability_cred = await request_reachability();
let reachability_cred = await simple_request("/reachability");
console.log("Got reachability credential: " + reachability_cred);
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)=> {
console.log("Got new Level 1 Lox Credential: " + response);
return handle_trust_migration(requested_trust_migration, response, pubkeys);
});
return cred;
@ -51,7 +56,6 @@ 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(() => {
@ -74,27 +78,13 @@ function request_open_invite() {
});
}
function request_pubkeys() {
function simple_request(requested) {
return new Promise((fulfill, reject) => {
loxServerGetRequest("/pubkeys").then((response) => {
console.log("Got pubkeys: " + JSON.stringify(response));
loxServerGetRequest(requested).then((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");
console.log("Error making simple request: " + requested);
reject();
});
});