Solve time issue, add time for testing
This commit is contained in:
parent
080d5879b1
commit
0a29265c1f
|
@ -10,12 +10,18 @@ license = "MIT"
|
|||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
julianday = "1.2.0"
|
||||
lox = { git = "https://gitlab.torproject.org/onyinyang/lox.git", branch = "master" }
|
||||
wasm-bindgen = "0.2"
|
||||
time = "0.2"
|
||||
serde_json = "1.0.87"
|
||||
serde = "1"
|
||||
serde-wasm-bindgen = "0.4.5"
|
||||
|
||||
console_error_panic_hook = "0.1.7"
|
||||
time = "0.2"
|
||||
js-sys = "0.3.61"
|
||||
rand = { version = "0.7", features = ["wasm-bindgen"] }
|
||||
|
||||
[dependencies.chrono]
|
||||
version = "0.4.19"
|
||||
features = ["serde", "wasmbind"]
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
use lox::cred::Lox;
|
||||
use chrono::{Duration, Utc};
|
||||
use julianday::JulianDay;
|
||||
use lox::bridge_table::BridgeLine;
|
||||
use lox::cred::Lox;
|
||||
use lox::proto::{open_invite, trust_promotion};
|
||||
use lox::{IssuerPubKey, OPENINV_LENGTH};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json;
|
||||
//use serde_wasm_bindgen;
|
||||
use std::array::TryFromSliceError;
|
||||
use std::panic;
|
||||
use std::{panic};
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
|
@ -42,6 +44,9 @@ extern "C" {
|
|||
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]
|
||||
pub fn set_panic_hook() {
|
||||
panic::set_hook(Box::new(console_error_panic_hook::hook));
|
||||
|
@ -70,6 +75,16 @@ pub fn open_invite(invite: &[u8]) -> Result<String, JsValue> {
|
|||
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]
|
||||
pub fn handle_new_lox_credential(
|
||||
open_lox_result: String,
|
||||
|
@ -100,20 +115,36 @@ pub fn handle_new_lox_credential(
|
|||
"Got new Lox Credential: {}",
|
||||
serde_json::to_string(&lox_cred.lox_credential).unwrap()
|
||||
));
|
||||
log(&format!("Got new bridgeline: {}", serde_json::to_string(&lox_cred.bridgeline).unwrap()));
|
||||
log(&format!(
|
||||
"Got new bridgeline: {}",
|
||||
serde_json::to_string(&lox_cred.bridgeline).unwrap()
|
||||
));
|
||||
}
|
||||
Ok(serde_json::to_string(&lox_cred).unwrap())
|
||||
}
|
||||
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> String {
|
||||
pub fn trust_promotion(open_lox_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 (request, state) = trust_promotion::request(&lox_cred.lox_credential, &pubkeys.lox_pub, today()).unwrap();
|
||||
// To test creation of the credential we need to advance the day to 30
|
||||
// in production this should just use the today() function
|
||||
log(&format!(
|
||||
"TEST: Add 30 days to today's date: {}",
|
||||
add_today(30)
|
||||
));
|
||||
let tp_result =
|
||||
match trust_promotion::request(&lox_cred.lox_credential, &pubkeys.lox_pub, add_today(30)) {
|
||||
//CHANGE add_today() to today()
|
||||
Ok(tp_result) => tp_result,
|
||||
Err(e) => {
|
||||
log(&format!("Error: {:?}", e.to_string()));
|
||||
return Err(JsValue::from(e.to_string()));
|
||||
}
|
||||
};
|
||||
let req_state = TrustReqState {
|
||||
request: request,
|
||||
state: state,
|
||||
request: tp_result.0,
|
||||
state: tp_result.1,
|
||||
};
|
||||
unsafe {
|
||||
log(&format!(
|
||||
|
@ -121,10 +152,9 @@ pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> String {
|
|||
serde_json::to_string(&req_state).unwrap()
|
||||
));
|
||||
}
|
||||
serde_json::to_string(&req_state).unwrap()
|
||||
Ok(serde_json::to_string(&req_state).unwrap())
|
||||
}
|
||||
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn handle_trust_promotion(
|
||||
trust_promo_request: String,
|
||||
|
@ -133,16 +163,14 @@ pub fn handle_trust_promotion(
|
|||
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()));
|
||||
}
|
||||
};
|
||||
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: {}",
|
||||
|
@ -152,19 +180,7 @@ pub fn handle_trust_promotion(
|
|||
Ok(serde_json::to_string(&migration_cred).unwrap())
|
||||
}
|
||||
|
||||
|
||||
|
||||
// This should also check the pubkey
|
||||
fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> {
|
||||
invite.try_into()
|
||||
}
|
||||
|
||||
/// 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()
|
||||
}
|
Loading…
Reference in New Issue