2023-02-07 00:45:54 -05:00
|
|
|
use lox::cred::Lox;
|
|
|
|
use lox::bridge_table::BridgeLine;
|
|
|
|
use lox::proto::{open_invite, trust_promotion};
|
2023-01-25 15:02:23 -05:00
|
|
|
use lox::{IssuerPubKey, OPENINV_LENGTH};
|
2023-02-06 13:58:24 -05:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-11-09 14:40:53 -05:00
|
|
|
use serde_json;
|
2023-01-25 15:02:39 -05:00
|
|
|
//use serde_wasm_bindgen;
|
2022-11-15 21:57:49 -05:00
|
|
|
use std::array::TryFromSliceError;
|
|
|
|
use std::panic;
|
2023-01-25 15:02:23 -05:00
|
|
|
use wasm_bindgen::prelude::*;
|
2022-11-09 14:40:53 -05:00
|
|
|
|
2023-01-30 16:03:12 -05:00
|
|
|
#[derive(Deserialize, Serialize)]
|
2023-02-07 00:45:54 -05:00
|
|
|
struct OpenReqState {
|
|
|
|
request: open_invite::Request,
|
|
|
|
state: open_invite::State,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
struct TrustReqState {
|
|
|
|
request: trust_promotion::Request,
|
|
|
|
state: trust_promotion::State,
|
2023-01-30 16:03:12 -05:00
|
|
|
}
|
|
|
|
|
2023-02-06 13:58:24 -05:00
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
2023-02-01 00:42:49 -05:00
|
|
|
struct PubKeys {
|
|
|
|
lox_pub: IssuerPubKey,
|
|
|
|
migration_pub: IssuerPubKey,
|
|
|
|
migrationkey_pub: IssuerPubKey,
|
|
|
|
reachability_pub: IssuerPubKey,
|
|
|
|
invitation_pub: IssuerPubKey,
|
|
|
|
}
|
|
|
|
|
2023-02-06 13:58:24 -05:00
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
2023-01-30 16:03:12 -05:00
|
|
|
struct Credential {
|
2023-02-07 00:45:54 -05:00
|
|
|
lox_credential: Lox,
|
|
|
|
bridgeline: BridgeLine,
|
2023-01-26 14:16:41 -05:00
|
|
|
}
|
|
|
|
|
2022-11-09 14:40:53 -05:00
|
|
|
#[wasm_bindgen]
|
2023-01-25 15:02:23 -05:00
|
|
|
extern "C" {
|
2022-11-09 14:40:53 -05:00
|
|
|
#[wasm_bindgen(js_namespace = console)]
|
|
|
|
pub fn log(s: &str);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2022-11-15 21:57:49 -05:00
|
|
|
pub fn set_panic_hook() {
|
|
|
|
panic::set_hook(Box::new(console_error_panic_hook::hook));
|
2022-11-09 14:40:53 -05:00
|
|
|
}
|
|
|
|
|
2022-11-15 21:57:49 -05:00
|
|
|
#[wasm_bindgen]
|
2023-01-25 15:02:23 -05:00
|
|
|
pub fn open_invite(invite: &[u8]) -> Result<String, JsValue> {
|
|
|
|
unsafe {
|
|
|
|
log(&format!("Using invite: {:?}", invite));
|
2022-11-15 21:57:49 -05:00
|
|
|
}
|
2023-01-25 15:02:23 -05:00
|
|
|
let token = match validate(invite) {
|
|
|
|
Ok(token) => token,
|
|
|
|
Err(e) => return Err(JsValue::from(e.to_string())),
|
|
|
|
};
|
|
|
|
let (request, state) = open_invite::request(&token);
|
2023-02-07 00:45:54 -05:00
|
|
|
let req_state = OpenReqState {
|
2023-01-30 16:03:12 -05:00
|
|
|
request: request,
|
|
|
|
state: state,
|
2023-02-06 13:58:24 -05:00
|
|
|
};
|
2023-01-25 15:02:23 -05:00
|
|
|
unsafe {
|
|
|
|
log(&format!(
|
|
|
|
"Formatted open invite request: {}",
|
2023-01-26 14:16:41 -05:00
|
|
|
serde_json::to_string(&req_state).unwrap()
|
2023-01-25 15:02:23 -05:00
|
|
|
));
|
|
|
|
}
|
2023-01-26 14:16:41 -05:00
|
|
|
Ok(serde_json::to_string(&req_state).unwrap())
|
2022-11-15 21:57:49 -05:00
|
|
|
}
|
2022-11-09 14:40:53 -05:00
|
|
|
|
2023-01-25 15:02:23 -05:00
|
|
|
#[wasm_bindgen]
|
2023-02-06 13:58:24 -05:00
|
|
|
pub fn handle_new_lox_credential(
|
|
|
|
open_lox_result: String,
|
|
|
|
open_lox_response: String,
|
|
|
|
lox_pub: String,
|
|
|
|
) -> Result<String, JsValue> {
|
2023-02-07 00:45:54 -05:00
|
|
|
let req_state: OpenReqState = serde_json::from_str(&open_lox_result).unwrap();
|
2023-01-30 16:03:12 -05:00
|
|
|
let deserialized_state = req_state.state;
|
|
|
|
let deserialized_response = serde_json::from_str(&open_lox_response).unwrap();
|
2023-02-01 00:42:49 -05:00
|
|
|
let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap();
|
2023-02-06 13:58:24 -05:00
|
|
|
let lox_cred = match open_invite::handle_response(
|
|
|
|
deserialized_state,
|
|
|
|
deserialized_response,
|
|
|
|
&pubkeys.lox_pub,
|
|
|
|
) {
|
2023-01-25 15:02:23 -05:00
|
|
|
Ok(lox_cred) => lox_cred,
|
2023-02-01 00:42:49 -05:00
|
|
|
Err(e) => {
|
|
|
|
log(&format!("Error: {:?}", e.to_string()));
|
|
|
|
return Err(JsValue::from(e.to_string()));
|
|
|
|
}
|
2023-01-25 15:02:23 -05:00
|
|
|
};
|
2023-01-30 16:03:12 -05:00
|
|
|
let lox_cred = Credential {
|
2023-02-07 00:45:54 -05:00
|
|
|
lox_credential: lox_cred.0,
|
|
|
|
bridgeline: lox_cred.1,
|
2023-01-30 16:03:12 -05:00
|
|
|
};
|
2023-01-25 15:02:23 -05:00
|
|
|
unsafe {
|
2023-02-06 13:58:24 -05:00
|
|
|
log(&format!(
|
|
|
|
"Got new Lox Credential: {}",
|
2023-02-07 00:45:54 -05:00
|
|
|
serde_json::to_string(&lox_cred.lox_credential).unwrap()
|
2023-02-06 13:58:24 -05:00
|
|
|
));
|
2023-02-07 00:45:54 -05:00
|
|
|
log(&format!("Got new bridgeline: {}", serde_json::to_string(&lox_cred.bridgeline).unwrap()));
|
2023-01-25 15:02:23 -05:00
|
|
|
}
|
2023-01-30 16:03:12 -05:00
|
|
|
Ok(serde_json::to_string(&lox_cred).unwrap())
|
2023-01-25 15:02:39 -05:00
|
|
|
}
|
2023-01-25 15:02:23 -05:00
|
|
|
|
2023-02-07 00:45:54 -05:00
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> String {
|
|
|
|
let lox_cred: Credential = serde_json::from_str(&open_lox_cred).unwrap();
|
|
|
|
let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap();
|
|
|
|
let (request, state) = trust_promotion::request(&lox_cred.lox_credential, &pubkeys.lox_pub, today()).unwrap();
|
|
|
|
let req_state = TrustReqState {
|
|
|
|
request: request,
|
|
|
|
state: state,
|
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
log(&format!(
|
|
|
|
"Formatted open invite request: {}",
|
|
|
|
serde_json::to_string(&req_state).unwrap()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
serde_json::to_string(&req_state).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn handle_trust_promotion(
|
|
|
|
trust_promo_request: String,
|
|
|
|
trust_promo_response: String,
|
|
|
|
) -> Result<String, JsValue> {
|
|
|
|
let req_state: TrustReqState = serde_json::from_str(&trust_promo_request).unwrap();
|
|
|
|
let deserialized_state = req_state.state;
|
|
|
|
let deserialized_response = serde_json::from_str(&trust_promo_response).unwrap();
|
|
|
|
let migration_cred = match trust_promotion::handle_response(
|
|
|
|
deserialized_state,
|
|
|
|
deserialized_response,
|
|
|
|
) {
|
|
|
|
Ok(migration_cred) => migration_cred,
|
|
|
|
Err(e) => {
|
|
|
|
log(&format!("Error: {:?}", e.to_string()));
|
|
|
|
return Err(JsValue::from(e.to_string()));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
log(&format!(
|
|
|
|
"Got new Migration Credential: {}",
|
|
|
|
serde_json::to_string(&migration_cred).unwrap()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Ok(serde_json::to_string(&migration_cred).unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-01-25 15:02:23 -05:00
|
|
|
// This should also check the pubkey
|
2022-11-15 21:57:49 -05:00
|
|
|
fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> {
|
|
|
|
invite.try_into()
|
2022-11-09 14:40:53 -05:00
|
|
|
}
|
2023-02-07 00:45:54 -05:00
|
|
|
|
|
|
|
/// Get today's (real or simulated) date
|
|
|
|
fn today() -> u32 {
|
|
|
|
// We will not encounter negative Julian dates (~6700 years ago)
|
|
|
|
// or ones larger than 32 bits
|
|
|
|
(time::OffsetDateTime::now_utc().date())
|
|
|
|
.julian_day()
|
|
|
|
.try_into()
|
|
|
|
.unwrap()
|
|
|
|
}
|