2023-01-25 15:02:23 -05:00
|
|
|
use lox::bridge_table::BridgeLine;
|
2022-11-10 11:04:20 -05:00
|
|
|
use lox::proto::open_invite;
|
2023-01-25 15:02:23 -05:00
|
|
|
use lox::{IssuerPubKey, OPENINV_LENGTH};
|
2022-11-09 14:40:53 -05:00
|
|
|
use serde_json;
|
2023-01-26 14:16:41 -05:00
|
|
|
use serde::{Deserialize,Serialize};
|
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-26 14:16:41 -05:00
|
|
|
#[derive(Debug,Deserialize, Serialize)]
|
|
|
|
struct ReqState {
|
|
|
|
request: String,
|
|
|
|
state: String,
|
|
|
|
}
|
|
|
|
|
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-01-26 14:16:41 -05:00
|
|
|
let req_state = ReqState {
|
|
|
|
request: serde_json::to_string(&request).unwrap(),
|
|
|
|
state: serde_json::to_string(&state).unwrap(),
|
|
|
|
};
|
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
|
|
|
fn concat_string(request: String, state: String) -> String {
|
|
|
|
let mut new_string: String = "Request:".to_owned();
|
|
|
|
new_string.push_str(&request);
|
|
|
|
new_string.push_str(", State:");
|
|
|
|
new_string.push_str(&state);
|
|
|
|
return new_string;
|
|
|
|
}
|
|
|
|
|
2023-01-25 15:02:53 -05:00
|
|
|
fn deconcat_string(concatString: String) -> (String, String) {
|
|
|
|
|
|
|
|
("hello".to_owned(), "world".to_owned())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-01-25 15:02:39 -05:00
|
|
|
|
2023-01-25 15:02:23 -05:00
|
|
|
#[wasm_bindgen]
|
2023-01-26 00:17:14 -05:00
|
|
|
pub fn handle_new_lox_credential(open_lox_result: String, open_lox_response: String, lox_pub: String) -> Result<String, JsValue> {
|
2023-01-25 15:02:23 -05:00
|
|
|
unsafe {
|
2023-01-25 15:02:53 -05:00
|
|
|
log(&format!("Using server response: {:?}", open_lox_result));
|
2023-01-25 15:02:23 -05:00
|
|
|
}
|
2023-01-25 15:02:53 -05:00
|
|
|
let (state, response) = deconcat_string(open_lox_result);
|
2023-01-25 15:02:23 -05:00
|
|
|
let deserialized_state = serde_json::from_str(&state).unwrap();
|
|
|
|
let deserialized_response = serde_json::from_str(&response).unwrap();
|
2023-01-25 15:02:39 -05:00
|
|
|
let deserialized_pubkey = serde_json::from_str(&lox_pub).unwrap();
|
|
|
|
let lox_cred = match open_invite::handle_response(deserialized_state, deserialized_response, &deserialized_pubkey) {
|
2023-01-25 15:02:23 -05:00
|
|
|
Ok(lox_cred) => lox_cred,
|
|
|
|
Err(e) => return Err(JsValue::from(e.to_string())),
|
|
|
|
};
|
|
|
|
let serialized_credential = serde_json::to_string(&lox_cred.0).unwrap();
|
|
|
|
let serialized_bridgeline= serde_json::to_string(&lox_cred.1).unwrap();
|
|
|
|
unsafe {
|
|
|
|
log(&format!("Got new Lox Credential: {}", serialized_credential));
|
|
|
|
log(&format!("Got new bridgeline: {}", serialized_bridgeline));
|
|
|
|
}
|
2023-01-25 15:02:39 -05:00
|
|
|
let open_lox_response = concat_string(serialized_credential, serialized_bridgeline);
|
|
|
|
Ok(open_lox_response)
|
|
|
|
}
|
2023-01-25 15:02:23 -05:00
|
|
|
|
|
|
|
/* Somehow get pubkeys and return to function
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub async fn get_pubkey(key_type: String) -> Result<JsValue, JsValue> {
|
|
|
|
let mut url = "http://localhost:8001/".to_owned() + &key_type;
|
|
|
|
let res = reqwest::Client::new()
|
|
|
|
.get(url)
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let text = res.text().await?;
|
|
|
|
let pub_key: IssuerPubKey = serde_json::from_str(&text).unwrap();
|
|
|
|
|
|
|
|
Ok(pub_key)
|
|
|
|
} */
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|