Fix serialization of request/state

This commit is contained in:
onyinyang 2023-01-30 16:03:12 -05:00
parent 03008449fa
commit 832a252318
No known key found for this signature in database
GPG Key ID: 156A6435430C2036
1 changed files with 22 additions and 44 deletions

View File

@ -8,10 +8,16 @@ use std::array::TryFromSliceError;
use std::panic; use std::panic;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
#[derive(Debug,Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
struct ReqState { struct ReqState {
request: String, request: lox::proto::open_invite::Request,
state: String, state: lox::proto::open_invite::State,
}
#[derive(Debug,Deserialize, Serialize)]
struct Credential {
lox_credential: String,
bridgeline: String,
} }
#[wasm_bindgen] #[wasm_bindgen]
@ -36,8 +42,8 @@ pub fn open_invite(invite: &[u8]) -> Result<String, JsValue> {
}; };
let (request, state) = open_invite::request(&token); let (request, state) = open_invite::request(&token);
let req_state = ReqState { let req_state = ReqState {
request: serde_json::to_string(&request).unwrap(), request: request,
state: serde_json::to_string(&state).unwrap(), state: state,
}; };
unsafe { unsafe {
log(&format!( log(&format!(
@ -48,59 +54,31 @@ 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 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;
}
fn deconcat_string(concatString: String) -> (String, String) {
("hello".to_owned(), "world".to_owned())
}
#[wasm_bindgen] #[wasm_bindgen]
pub fn handle_new_lox_credential(open_lox_result: String, open_lox_response: String, lox_pub: String) -> Result<String, JsValue> { pub fn handle_new_lox_credential(open_lox_result: String, open_lox_response: String, lox_pub: String) -> Result<String, JsValue> {
unsafe { unsafe {
log(&format!("Using server response: {:?}", open_lox_result)); log(&format!("Using server response: {:?}", open_lox_result));
} }
let (state, response) = deconcat_string(open_lox_result); let req_state: ReqState = serde_json::from_str(&open_lox_result).unwrap();
let deserialized_state = serde_json::from_str(&state).unwrap(); let deserialized_state = req_state.state;
let deserialized_response = serde_json::from_str(&response).unwrap(); let deserialized_response = serde_json::from_str(&open_lox_response).unwrap();
let deserialized_pubkey = serde_json::from_str(&lox_pub).unwrap(); let deserialized_pubkey = serde_json::from_str(&lox_pub).unwrap();
let lox_cred = match open_invite::handle_response(deserialized_state, deserialized_response, &deserialized_pubkey) { let lox_cred = match open_invite::handle_response(deserialized_state, deserialized_response, &deserialized_pubkey) {
Ok(lox_cred) => lox_cred, Ok(lox_cred) => lox_cred,
Err(e) => return Err(JsValue::from(e.to_string())), Err(e) => return Err(JsValue::from(e.to_string())),
}; };
let serialized_credential = serde_json::to_string(&lox_cred.0).unwrap(); let lox_cred = Credential {
let serialized_bridgeline= serde_json::to_string(&lox_cred.1).unwrap(); lox_credential: serde_json::to_string(&lox_cred.0).unwrap(),
bridgeline: serde_json::to_string(&lox_cred.1).unwrap(),
};
unsafe { unsafe {
log(&format!("Got new Lox Credential: {}", serialized_credential)); log(&format!("Got new Lox Credential: {}", lox_cred.lox_credential));
log(&format!("Got new bridgeline: {}", serialized_bridgeline)); log(&format!("Got new bridgeline: {}", lox_cred.bridgeline));
} }
let open_lox_response = concat_string(serialized_credential, serialized_bridgeline); Ok(serde_json::to_string(&lox_cred).unwrap())
Ok(open_lox_response)
} }
/* 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 // 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()