Add Trust migration protocol

This commit is contained in:
onyinyang 2023-02-13 23:58:07 -05:00
parent 56bc148db1
commit 944c3634ca
No known key found for this signature in database
GPG Key ID: 156A6435430C2036
2 changed files with 102 additions and 34 deletions

View File

@ -1,4 +1,11 @@
import init, { open_invite, handle_new_lox_credential, trust_promotion, handle_trust_promotion, set_panic_hook } from "./pkg/lox_wasm.js"; 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 pubkeys = await request_pubkeys();
let requested_invite = await init().then(() => { let requested_invite = await init().then(() => {
@ -12,7 +19,7 @@ console.log("Got request and state: "+requested_invite);
let open_lox_cred = await init().then(() => { let open_lox_cred = await init().then(() => {
set_panic_hook(); set_panic_hook();
let cred = request_new_lox_credential(requested_invite).then((response) => { let cred = requested_cred("/openreq", requested_invite).then((response) => {
return handle_new_lox_credential(requested_invite, response, pubkeys); return handle_new_lox_credential(requested_invite, response, pubkeys);
}); });
return cred; return cred;
@ -22,33 +29,29 @@ let requested_trust_promo = trust_promotion(open_lox_cred, pubkeys);
let trust_promo_cred = await init().then(() => { let trust_promo_cred = await init().then(() => {
set_panic_hook(); set_panic_hook();
let cred = request_trust_promo_cred(requested_trust_promo).then((response)=> { let cred = requested_cred("/trustpromo", requested_trust_promo).then((response)=> {
return handle_trust_promotion(requested_trust_promo, response); return handle_trust_promotion(requested_trust_promo, response);
}); });
return cred; return cred;
}); });
let reachability_cred = await request_reachability(); let reachability_cred = await request_reachability();
function request_new_lox_credential(requested_invite) { let requested_trust_migration = trust_migration(open_lox_cred, trust_promo_cred, pubkeys);
return new Promise((fulfill, reject) => {
let req = JSON.parse(requested_invite); let level_1_cred = await init().then(() => {
loxServerPostRequest("/openreq", req.request).then((response) => { set_panic_hook();
console.log("Got new Open Invite Lox Credential: " + JSON.stringify(response)); let cred = requested_cred("/trustmig", requested_trust_migration).then((response)=> {
fulfill(JSON.stringify(response)); return handle_trust_migration(requested_trust_migration, response, pubkeys);
return; });
}).catch(() => { return cred;
console.log("Error requesting new Lox credential from server");
reject();
});
}); });
}
function request_trust_promo_cred(requested) { function requested_cred(command, requested) {
return new Promise((fulfill, reject) => { return new Promise((fulfill, reject) => {
let req = JSON.parse(requested); let req = JSON.parse(requested);
loxServerPostRequest("/trustpromo", req.request).then((response) => { loxServerPostRequest(command, req.request).then((response) => {
console.log("Got new Trust Promotion Lox Credential: " + JSON.stringify(response)); console.log("Got new Trust Migration Lox Credential: " + JSON.stringify(response));
fulfill(JSON.stringify(response)); fulfill(JSON.stringify(response));
return; return;
}).catch(() => { }).catch(() => {

View File

@ -2,7 +2,8 @@ use chrono::{Duration, Utc};
use julianday::JulianDay; use julianday::JulianDay;
use lox::bridge_table::BridgeLine; use lox::bridge_table::BridgeLine;
use lox::cred::Lox; use lox::cred::Lox;
use lox::proto::{open_invite, trust_promotion}; use lox::proto::{open_invite, trust_promotion, migration, level_up,
issue_invite, redeem_invite, check_blockage, blockage_migration};
use lox::{IssuerPubKey, OPENINV_LENGTH}; use lox::{IssuerPubKey, OPENINV_LENGTH};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json; use serde_json;
@ -23,6 +24,13 @@ struct TrustReqState {
state: trust_promotion::State, state: trust_promotion::State,
} }
#[derive(Deserialize, Serialize)]
struct MigReqState {
request: migration::Request,
state: migration::State,
}
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
struct PubKeys { struct PubKeys {
lox_pub: IssuerPubKey, lox_pub: IssuerPubKey,
@ -38,14 +46,24 @@ struct Credential {
bridgeline: BridgeLine, bridgeline: BridgeLine,
} }
fn today() -> u32 {
let naive_now = Utc::now().date_naive();
JulianDay::from(naive_now).inner().try_into().unwrap()
}
// This should only be used for testing, use today in production
fn add_today(sum: i64) -> u32 {
let naive_now_plus = (Utc::now() + Duration::days(sum)).date_naive();
JulianDay::from(naive_now_plus).inner().try_into().unwrap()
}
#[wasm_bindgen] #[wasm_bindgen]
extern "C" { extern "C" {
#[wasm_bindgen(js_namespace = console)] #[wasm_bindgen(js_namespace = console)]
pub fn log(s: &str); pub fn log(s: &str);
} }
// Time has to be implemented with wasmbind feature as
// explained here: https://stackoverflow.com/questions/63210984/chrono-kills-my-rust-webassembly-function
#[wasm_bindgen] #[wasm_bindgen]
pub fn set_panic_hook() { pub fn set_panic_hook() {
@ -75,16 +93,6 @@ pub fn open_invite(invite: &[u8]) -> Result<String, JsValue> {
Ok(serde_json::to_string(&req_state).unwrap()) Ok(serde_json::to_string(&req_state).unwrap())
} }
fn today() -> u32 {
let naive_now = Utc::now().date_naive();
JulianDay::from(naive_now).inner().try_into().unwrap()
}
fn add_today(sum: i64) -> u32 {
let naive_now_plus = (Utc::now() + Duration::days(sum)).date_naive();
JulianDay::from(naive_now_plus).inner().try_into().unwrap()
}
#[wasm_bindgen] #[wasm_bindgen]
pub fn handle_new_lox_credential( pub fn handle_new_lox_credential(
open_lox_result: String, open_lox_result: String,
@ -148,7 +156,7 @@ pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> Result<String,
}; };
unsafe { unsafe {
log(&format!( log(&format!(
"Formatted open invite request: {}", "Formatted Trust Promotion request: {}",
serde_json::to_string(&req_state).unwrap() serde_json::to_string(&req_state).unwrap()
)); ));
} }
@ -180,6 +188,63 @@ pub fn handle_trust_promotion(
Ok(serde_json::to_string(&migration_cred).unwrap()) Ok(serde_json::to_string(&migration_cred).unwrap())
} }
#[wasm_bindgen]
pub fn trust_migration(open_lox_cred: String, trust_promo_cred: String, lox_pub: String) -> Result<String, JsValue> {
let lox_cred: Credential = serde_json::from_str(&open_lox_cred).unwrap();
let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap();
let mig_cred = serde_json::from_str(&trust_promo_cred).unwrap();
// To test creation of the credential we need to advance the day to 30
// in production this should just use the today() function
let tm_result =
//CHANGE add_today(31) to today() for production
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: tm_result.0,
state: tm_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_trust_migration(
trust_migration_request: String,
trust_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(&trust_migration_request).unwrap();
let deserialized_state = req_state.state;
let deserialized_response = serde_json::from_str(&trust_migration_response).unwrap();
let level_1_cred =
match migration::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) {
Ok(level_1_cred) => level_1_cred,
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(&level_1_cred).unwrap()
));
}
Ok(serde_json::to_string(&level_1_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()