lox/crates/lox-wasm/src/lib.rs

103 lines
3.0 KiB
Rust
Raw Normal View History

use lox::bridge_table::BridgeLine;
2022-11-10 11:04:20 -05:00
use lox::proto::open_invite;
use lox::{IssuerPubKey, OPENINV_LENGTH};
use serde_json;
use serde::{Deserialize,Serialize};
//use serde_wasm_bindgen;
use std::array::TryFromSliceError;
use std::panic;
use wasm_bindgen::prelude::*;
2023-01-30 16:03:12 -05:00
#[derive(Deserialize, Serialize)]
struct ReqState {
2023-01-30 16:03:12 -05:00
request: lox::proto::open_invite::Request,
state: lox::proto::open_invite::State,
}
#[derive(Debug, Deserialize,Serialize)]
struct PubKeys {
lox_pub: IssuerPubKey,
migration_pub: IssuerPubKey,
migrationkey_pub: IssuerPubKey,
reachability_pub: IssuerPubKey,
invitation_pub: IssuerPubKey,
}
2023-01-30 16:03:12 -05:00
#[derive(Debug,Deserialize, Serialize)]
struct Credential {
lox_credential: String,
bridgeline: String,
}
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
pub fn log(s: &str);
}
#[wasm_bindgen]
pub fn set_panic_hook() {
panic::set_hook(Box::new(console_error_panic_hook::hook));
}
#[wasm_bindgen]
pub fn open_invite(invite: &[u8]) -> Result<String, JsValue> {
unsafe {
log(&format!("Using invite: {:?}", invite));
}
let token = match validate(invite) {
Ok(token) => token,
Err(e) => return Err(JsValue::from(e.to_string())),
};
let (request, state) = open_invite::request(&token);
let req_state = ReqState {
2023-01-30 16:03:12 -05:00
request: request,
state: state,
};
unsafe {
log(&format!(
"Formatted open invite request: {}",
serde_json::to_string(&req_state).unwrap()
));
}
Ok(serde_json::to_string(&req_state).unwrap())
}
#[wasm_bindgen]
pub fn handle_new_lox_credential(open_lox_result: String, open_lox_response: String, lox_pub: String) -> Result<String, JsValue> {
unsafe {
2023-01-30 18:13:17 -05:00
log(&format!("Using server response: {:?}", open_lox_result));
}
2023-01-30 16:03:12 -05:00
let req_state: ReqState = serde_json::from_str(&open_lox_result).unwrap();
let deserialized_state = req_state.state;
let deserialized_response = serde_json::from_str(&open_lox_response).unwrap();
let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap();
unsafe {
log(&format!("pubkeys: {:?}", pubkeys.lox_pub));
}
let lox_cred = match open_invite::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) {
Ok(lox_cred) => lox_cred,
Err(e) => {
log(&format!("Error: {:?}", e.to_string()));
return Err(JsValue::from(e.to_string()));
}
};
log(&format!("Did this actually work?: {:?}", lox_cred));
2023-01-30 16:03:12 -05:00
let lox_cred = Credential {
lox_credential: serde_json::to_string(&lox_cred.0).unwrap(),
bridgeline: serde_json::to_string(&lox_cred.1).unwrap(),
};
unsafe {
2023-01-30 16:03:12 -05:00
log(&format!("Got new Lox Credential: {}", lox_cred.lox_credential));
log(&format!("Got new bridgeline: {}", lox_cred.bridgeline));
}
2023-01-30 16:03:12 -05:00
Ok(serde_json::to_string(&lox_cred).unwrap())
}
// This should also check the pubkey
fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> {
invite.try_into()
}