2023-02-23 13:18:11 -05:00
|
|
|
use chrono::{Duration, Utc, NaiveDate};
|
|
|
|
use std::sync::atomic::{AtomicI64, Ordering};
|
2023-02-10 16:18:54 -05:00
|
|
|
use julianday::JulianDay;
|
2023-02-23 13:18:11 -05:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use lox::bridge_table::{BridgeLine,from_scalar,BridgeTable, ENC_BUCKET_BYTES};
|
2023-02-14 17:08:09 -05:00
|
|
|
use lox::cred::{BucketReachability, Lox, Migration};
|
2023-02-13 23:58:07 -05:00
|
|
|
use lox::proto::{open_invite, trust_promotion, migration, level_up,
|
|
|
|
issue_invite, redeem_invite, check_blockage, blockage_migration};
|
2023-02-23 22:16:45 -05:00
|
|
|
use lox::{IssuerPubKey, OPENINV_LENGTH, scalar_u32};
|
2023-02-06 13:58:24 -05:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-02-23 13:18:11 -05:00
|
|
|
use serde_with::{serde_as};
|
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;
|
2023-02-10 16:18:54 -05:00
|
|
|
use std::{panic};
|
2023-01-25 15:02:23 -05:00
|
|
|
use wasm_bindgen::prelude::*;
|
2023-02-23 22:16:45 -05:00
|
|
|
use zkp::ProofError;
|
2022-11-09 14:40:53 -05:00
|
|
|
|
2023-02-23 13:18:11 -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-13 23:58:07 -05:00
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
struct MigReqState {
|
|
|
|
request: migration::Request,
|
|
|
|
state: migration::State,
|
|
|
|
}
|
|
|
|
|
2023-02-14 17:08:09 -05:00
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
struct LevelupReqState {
|
|
|
|
request: level_up::Request,
|
|
|
|
state: level_up::State,
|
|
|
|
}
|
2023-02-13 23:58:07 -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-23 13:18:11 -05:00
|
|
|
#[serde_as]
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct EncBridgeTable {
|
|
|
|
#[serde_as(as = "Vec<[_; ENC_BUCKET_BYTES]>")]
|
|
|
|
pub etable: Vec<[u8; ENC_BUCKET_BYTES]>,
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-02-13 23:58:07 -05:00
|
|
|
|
|
|
|
fn today() -> u32 {
|
|
|
|
let naive_now = Utc::now().date_naive();
|
|
|
|
JulianDay::from(naive_now).inner().try_into().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should only be used for testing, use today in production
|
2023-02-23 13:18:11 -05:00
|
|
|
fn test_today(days: i64) -> u32 {
|
|
|
|
let naive_now_plus = (Utc::now() + Duration::days(days)).date_naive();
|
2023-02-13 23:58:07 -05:00
|
|
|
JulianDay::from(naive_now_plus).inner().try_into().unwrap()
|
|
|
|
}
|
|
|
|
|
2023-02-23 22:16:45 -05:00
|
|
|
|
|
|
|
//pub const MAX_LEVEL: usize = 4;
|
|
|
|
//pub const LEVEL_INTERVAL: [u32; MAX_LEVEL + 1] = [0, 14, 28, 56, 84];
|
|
|
|
fn calc_test_days(lox_cred: &Lox) -> Result<i64, ProofError> {
|
|
|
|
let trust_level: i64 = match scalar_u32(&lox_cred.trust_level) {
|
|
|
|
Some(v) => v as i64,
|
|
|
|
None => return Err(ProofError::VerificationFailure),
|
|
|
|
};
|
|
|
|
let mut total = 31;
|
|
|
|
// for level in 0..trust_level {
|
|
|
|
// let level_interval: u32 = LEVEL_INTERVAL[trust_level as usize];
|
|
|
|
// total += level_interval;
|
|
|
|
total += trust_level*85;
|
|
|
|
// }
|
|
|
|
Ok(total)
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-02-10 16:18:54 -05:00
|
|
|
|
2022-11-09 14:40:53 -05:00
|
|
|
#[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-10 16:18: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]
|
2023-02-10 16:18:54 -05:00
|
|
|
pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> Result<String, JsValue> {
|
2023-02-07 00:45:54 -05:00
|
|
|
let lox_cred: Credential = serde_json::from_str(&open_lox_cred).unwrap();
|
|
|
|
let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap();
|
2023-02-10 16:18:54 -05:00
|
|
|
// 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!(
|
2023-02-13 16:29:34 -05:00
|
|
|
"TEST ONLY: Add 31 days to today's date: {}",
|
2023-02-23 13:18:11 -05:00
|
|
|
test_today(31)
|
2023-02-10 16:18:54 -05:00
|
|
|
));
|
|
|
|
let tp_result =
|
2023-02-13 23:58:07 -05:00
|
|
|
//CHANGE add_today(31) to today() for production
|
2023-02-23 13:18:11 -05:00
|
|
|
match trust_promotion::request(&lox_cred.lox_credential, &pubkeys.lox_pub, test_today(31)) {
|
2023-02-10 16:18:54 -05:00
|
|
|
Ok(tp_result) => tp_result,
|
|
|
|
Err(e) => {
|
|
|
|
log(&format!("Error: {:?}", e.to_string()));
|
|
|
|
return Err(JsValue::from(e.to_string()));
|
|
|
|
}
|
|
|
|
};
|
2023-02-07 00:45:54 -05:00
|
|
|
let req_state = TrustReqState {
|
2023-02-10 16:18:54 -05:00
|
|
|
request: tp_result.0,
|
|
|
|
state: tp_result.1,
|
2023-02-07 00:45:54 -05:00
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
log(&format!(
|
2023-02-13 23:58:07 -05:00
|
|
|
"Formatted Trust Promotion request: {}",
|
2023-02-07 00:45:54 -05:00
|
|
|
serde_json::to_string(&req_state).unwrap()
|
|
|
|
));
|
|
|
|
}
|
2023-02-10 16:18:54 -05:00
|
|
|
Ok(serde_json::to_string(&req_state).unwrap())
|
2023-02-07 00:45:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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();
|
2023-02-10 16:18:54 -05:00
|
|
|
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()));
|
|
|
|
}
|
|
|
|
};
|
2023-02-07 00:45:54 -05:00
|
|
|
unsafe {
|
|
|
|
log(&format!(
|
|
|
|
"Got new Migration Credential: {}",
|
|
|
|
serde_json::to_string(&migration_cred).unwrap()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Ok(serde_json::to_string(&migration_cred).unwrap())
|
|
|
|
}
|
|
|
|
|
2023-02-13 23:58:07 -05:00
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn trust_migration(open_lox_cred: String, trust_promo_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();
|
2023-02-14 17:08:09 -05:00
|
|
|
let mig_cred: Migration = serde_json::from_str(&trust_promo_cred).unwrap();
|
2023-02-13 23:58:07 -05:00
|
|
|
let tm_result =
|
|
|
|
match migration::request(&lox_cred.lox_credential, &mig_cred, &pubkeys.lox_pub, &pubkeys.migration_pub) {
|
|
|
|
Ok(tm_result) => tm_result,
|
|
|
|
Err(e) => {
|
|
|
|
log(&format!("Error: {:?}", e.to_string()));
|
|
|
|
return Err(JsValue::from(e.to_string()));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let req_state = MigReqState {
|
|
|
|
request: tm_result.0,
|
|
|
|
state: tm_result.1,
|
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
log(&format!(
|
|
|
|
"Formatted Trust Migration request: {}",
|
|
|
|
serde_json::to_string(&req_state).unwrap()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Ok(serde_json::to_string(&req_state).unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn handle_trust_migration(
|
|
|
|
trust_migration_request: String,
|
|
|
|
trust_migration_response: String,
|
|
|
|
lox_pub: String
|
|
|
|
) -> Result<String, JsValue> {
|
|
|
|
let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap();
|
|
|
|
let req_state: MigReqState = serde_json::from_str(&trust_migration_request).unwrap();
|
|
|
|
let deserialized_state = req_state.state;
|
|
|
|
let deserialized_response = serde_json::from_str(&trust_migration_response).unwrap();
|
2023-02-14 17:08:09 -05:00
|
|
|
let level_one_cred =
|
2023-02-13 23:58:07 -05:00
|
|
|
match migration::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) {
|
|
|
|
Ok(level_1_cred) => level_1_cred,
|
|
|
|
Err(e) => {
|
|
|
|
log(&format!("Error: {:?}", e.to_string()));
|
|
|
|
return Err(JsValue::from(e.to_string()));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
log(&format!(
|
|
|
|
"Got new Level 1 Credential: {}",
|
2023-02-14 17:08:09 -05:00
|
|
|
serde_json::to_string(&level_one_cred).unwrap()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Ok(serde_json::to_string(&level_one_cred).unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2023-02-23 13:18:11 -05:00
|
|
|
pub fn level_up(level_one_cred: String, encrypted_table: String, lox_pub: String) -> Result<String, JsValue> {
|
2023-02-14 17:08:09 -05:00
|
|
|
let lox_cred: Lox = serde_json::from_str(&level_one_cred).unwrap();
|
|
|
|
let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap();
|
2023-02-23 13:18:11 -05:00
|
|
|
let (id, key) = from_scalar(lox_cred.bucket).unwrap();
|
|
|
|
let enc_buckets: EncBridgeTable = serde_json::from_str(&encrypted_table).unwrap();
|
|
|
|
let bucket = BridgeTable::decrypt_bucket(id, &key, &enc_buckets.etable[id as usize]).unwrap();
|
|
|
|
let reach_cred = bucket.1.unwrap();
|
2023-02-14 17:08:09 -05:00
|
|
|
// To test level up of the credential we need to advance the day to the correct interval
|
|
|
|
// In this case, the maximum of 85 can be used to test all level ups
|
|
|
|
// in production this should just use the today() function
|
2023-02-23 13:18:11 -05:00
|
|
|
// decrypt trust level and use to calculate the correct date for now
|
2023-02-23 22:16:45 -05:00
|
|
|
// The trust level has to be at least 1
|
|
|
|
|
|
|
|
let test_cumulative_days = match calc_test_days(&lox_cred) {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => {
|
|
|
|
log(&format!("Error: {:?}", e.to_string()));
|
|
|
|
return Err(JsValue::from(e.to_string()));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-02-14 17:08:09 -05:00
|
|
|
log(&format!(
|
2023-02-23 22:16:45 -05:00
|
|
|
"TEST ONLY: Add 31 (open invitation) + Trust Level*85 days to today's date: {}", test_today(test_cumulative_days)
|
2023-02-14 17:08:09 -05:00
|
|
|
));
|
|
|
|
let lu_result =
|
|
|
|
//CHANGE add_today(31) to today() for production
|
2023-02-23 22:16:45 -05:00
|
|
|
match level_up::request(&lox_cred, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, test_today(test_cumulative_days)) {
|
2023-02-14 17:08:09 -05:00
|
|
|
Ok(lu_result) => lu_result,
|
|
|
|
Err(e) => {
|
|
|
|
log(&format!("Error: {:?}", e.to_string()));
|
|
|
|
return Err(JsValue::from(e.to_string()));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let req_state = LevelupReqState {
|
|
|
|
request: lu_result.0,
|
|
|
|
state: lu_result.1,
|
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
log(&format!(
|
|
|
|
"Formatted Level Up request: {}",
|
|
|
|
serde_json::to_string(&req_state).unwrap()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Ok(serde_json::to_string(&req_state).unwrap())
|
|
|
|
}
|
|
|
|
|
2023-02-23 22:16:45 -05:00
|
|
|
|
2023-02-14 17:08:09 -05:00
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn handle_level_up(
|
|
|
|
levelup_request: String,
|
|
|
|
levelup_response: String,
|
|
|
|
lox_pub: String
|
|
|
|
) -> Result<String, JsValue> {
|
|
|
|
let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap();
|
|
|
|
let req_state: LevelupReqState = serde_json::from_str(&levelup_request).unwrap();
|
|
|
|
let deserialized_state = req_state.state;
|
|
|
|
let deserialized_response = serde_json::from_str(&levelup_response).unwrap();
|
|
|
|
let level_up_cred =
|
|
|
|
match level_up::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) {
|
|
|
|
Ok(level_up_cred) => level_up_cred,
|
|
|
|
Err(e) => {
|
|
|
|
log(&format!("Error: {:?}", e.to_string()));
|
|
|
|
return Err(JsValue::from(e.to_string()));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
log(&format!(
|
|
|
|
"Got new Level Up Credential: {}",
|
|
|
|
serde_json::to_string(&level_up_cred).unwrap()
|
2023-02-13 23:58:07 -05:00
|
|
|
));
|
|
|
|
}
|
2023-02-14 17:08:09 -05:00
|
|
|
Ok(serde_json::to_string(&level_up_cred).unwrap())
|
2023-02-13 23:58:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|