Add blockage migration (untested)

This commit is contained in:
onyinyang 2023-02-27 16:57:23 -05:00
parent 7c770b1310
commit e12bff83e3
No known key found for this signature in database
GPG Key ID: 156A6435430C2036
2 changed files with 73 additions and 1 deletions

View File

@ -14,6 +14,8 @@ import init, {
handle_redeem_invite, handle_redeem_invite,
check_blockage, check_blockage,
handle_check_blockage, handle_check_blockage,
blockage_migration,
handle_blockage_migration,
set_panic_hook } from "./pkg/lox_wasm.js"; set_panic_hook } from "./pkg/lox_wasm.js";
let pubkeys = await simple_request("/pubkeys"); let pubkeys = await simple_request("/pubkeys");
console.log("Got pubkeys: " + pubkeys); console.log("Got pubkeys: " + pubkeys);
@ -140,7 +142,7 @@ let lox_cred_from_invite = await init().then(() => {
let requested_check_blockage = check_blockage(lox_cred, pubkeys); let requested_check_blockage = check_blockage(lox_cred, pubkeys);
// Check whether or not a bucket is blocked // Check whether or not a bucket is blocked
let check_cred = await init().then(() => { let check_migration_cred = await init().then(() => {
set_panic_hook(); set_panic_hook();
let cred = requested_cred("/checkblockage", requested_check_blockage).then((response)=> { let cred = requested_cred("/checkblockage", requested_check_blockage).then((response)=> {
console.log("Got check blockage Migration Credential: " + response); console.log("Got check blockage Migration Credential: " + response);
@ -149,6 +151,18 @@ let lox_cred_from_invite = await init().then(() => {
return cred; return cred;
}); });
let requested_blockage_migration = blockage_migration(lox_cred, check_migration_cred, pubkeys);
// Migrate to a new unblocked bridge
lox_cred = await init().then(() => {
set_panic_hook();
let cred = requested_cred("/blockagemigration", requested_blockage_migration).then((response)=> {
console.log("Got Lox Credential for new bucket: " + response);
return handle_blockage_migration(requested_check_blockage, response);
});
return cred;
});
function requested_cred(command, requested) { function requested_cred(command, requested) {
return new Promise((fulfill, reject) => { return new Promise((fulfill, reject) => {

View File

@ -562,6 +562,64 @@ pub fn handle_check_blockage(
Ok(serde_json::to_string(&migration_cred).unwrap()) Ok(serde_json::to_string(&migration_cred).unwrap())
} }
#[wasm_bindgen]
pub fn blockage_migration(lox_cred: String, check_migration_cred: String, lox_pub: String) -> Result<String, JsValue> {
let lox_cred: LoxCredential = serde_json::from_str(&lox_cred).unwrap();
let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap();
let mig_cred: Migration = serde_json::from_str(&check_migration_cred).unwrap();
let bm_result =
match migration::request(&lox_cred.lox_credential, &mig_cred, &pubkeys.lox_pub, &pubkeys.migration_pub) {
Ok(tm_result) => tm_result,
Err(e) => {
log(&format!("Error: {:?}", e.to_string()));
return Err(JsValue::from(e.to_string()));
}
};
let req_state = MigReqState {
request: bm_result.0,
state: bm_result.1,
};
unsafe {
log(&format!(
"Formatted Trust Migration request: {}",
serde_json::to_string(&req_state).unwrap()
));
}
Ok(serde_json::to_string(&req_state).unwrap())
}
#[wasm_bindgen]
pub fn handle_blockage_migration(
blockage_migration_request: String,
blockage_migration_response: String,
lox_pub: String
) -> Result<String, JsValue> {
let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap();
let req_state: MigReqState = serde_json::from_str(&blockage_migration_request).unwrap();
let deserialized_state = req_state.state;
let deserialized_response = serde_json::from_str(&blockage_migration_response).unwrap();
let lox_cred =
match migration::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) {
Ok(lox_cred) => LoxCredential {
lox_credential: lox_cred,
bridgeline: None,
invitation: None,
},
Err(e) => {
log(&format!("Error: {:?}", e.to_string()));
return Err(JsValue::from(e.to_string()));
}
};
unsafe {
log(&format!(
"Got new Level 1 Credential: {}",
serde_json::to_string(&lox_cred).unwrap()
));
}
Ok(serde_json::to_string(&lox_cred).unwrap())
}
// This should also check the pubkey // This should also check the pubkey
fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> { fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> {
invite.try_into() invite.try_into()