diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index bfde485..99ac848 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -7,10 +7,13 @@ import init, { handle_trust_migration, level_up, handle_level_up, + issue_invite, + handle_issue_invite, set_panic_hook } from "./pkg/lox_wasm.js"; let pubkeys = await simple_request("/pubkeys"); console.log("Got pubkeys: " + pubkeys); +// Get Lox Invitation let requested_invite = await init().then(() => { set_panic_hook(); let requested_invite = request_open_invite().then((token) => { @@ -20,6 +23,8 @@ let requested_invite = await init().then(() => { }); console.log("Got request and state: "+requested_invite); +// Redeem Lox Invitation for an Open Invitation Lox Credential +// Trust Level 0 let open_lox_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/openreq", requested_invite).then((response) => { @@ -31,6 +36,7 @@ let open_lox_cred = await init().then(() => { let requested_trust_promo = trust_promotion(open_lox_cred, pubkeys); +// Get Migration credential for Trust Promotion from Trust Level 0 -> 1 let trust_promo_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/trustpromo", requested_trust_promo).then((response)=> { @@ -43,6 +49,7 @@ let trust_promo_cred = await init().then(() => { let requested_trust_migration = trust_migration(open_lox_cred, trust_promo_cred, pubkeys); +// Trust Promotion from Trust Level 0 -> 1 let level_one_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/trustmig", requested_trust_migration).then((response)=> { @@ -56,6 +63,7 @@ let encrypted_table = await simple_request("/reachability"); console.log("Got Encrypted Table: " + encrypted_table); let requested_level_two = level_up(level_one_cred, encrypted_table, pubkeys); +// Level Up to Trust Level 2 let level_two_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/levelup", requested_level_two).then((response)=> { @@ -65,11 +73,12 @@ let level_two_cred = await init().then(() => { return cred; }); -//Update reachability cred +// Update reachability cred encrypted_table = await simple_request("/reachability"); console.log("Got Encrypted Table: " + encrypted_table); let requested_level_three = level_up(level_two_cred, encrypted_table, pubkeys); +// Level Up to Trust Level 3 let level_three_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/levelup", requested_level_three).then((response)=> { @@ -80,11 +89,12 @@ let level_two_cred = await init().then(() => { }); -//Update reachability cred +// Update reachability cred encrypted_table = await simple_request("/reachability"); console.log("Got Encrypted Table: " + encrypted_table); let requested_level_four = level_up(level_three_cred, encrypted_table, pubkeys); +// Level Up to Trust Level 4 let level_four_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/levelup", requested_level_four).then((response)=> { @@ -94,6 +104,22 @@ let level_four_cred = await init().then(() => { return cred; }); +// Update reachability cred +encrypted_table = await simple_request("/reachability"); +console.log("Got Encrypted Table: " + encrypted_table); +let requested_issue_invitation = issue_invite(level_four_cred, encrypted_table, pubkeys); + +// Issue an Invitation for a friend +let issue_invite_cred = await init().then(() => { + set_panic_hook(); + let cred = requested_cred("/issueinvite", requested_issue_invitation).then((response)=> { + console.log("Got new Invite and Lox Credential: " + response); + return handle_issue_invite(requested_issue_invitation, response, pubkeys); + }); + return cred; + }); + + function requested_cred(command, requested) { return new Promise((fulfill, reject) => { let req = JSON.parse(requested); diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 4701b70..4dfeeba 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -41,6 +41,12 @@ struct LevelupReqState { state: level_up::State, } +#[derive(Deserialize, Serialize)] +struct IssueInviteReqState { + request: issue_invite::Request, + state: issue_invite::State, +} + #[derive(Debug, Deserialize, Serialize)] struct PubKeys { lox_pub: IssuerPubKey, @@ -275,20 +281,24 @@ pub fn handle_trust_migration( Ok(serde_json::to_string(&level_one_cred).unwrap()) } +fn generate_reachability_cred(lox_cred: &Lox, encrypted_table: String) -> BucketReachability { + 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(); + return bucket.1.unwrap() +} + #[wasm_bindgen] pub fn level_up(level_one_cred: String, encrypted_table: String, lox_pub: String) -> Result { let lox_cred: Lox = serde_json::from_str(&level_one_cred).unwrap(); let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); - 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(); + let reach_cred = generate_reachability_cred(&lox_cred, encrypted_table); + // 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 // decrypt trust level and use to calculate the correct date for now // The trust level has to be at least 1 - let test_cumulative_days = match calc_test_days(&lox_cred) { Ok(v) => v, Err(e) => { @@ -350,6 +360,60 @@ pub fn handle_level_up( Ok(serde_json::to_string(&level_up_cred).unwrap()) } +#[wasm_bindgen] +pub fn issue_invite(trusted_cred: String, encrypted_table: String, lox_pub: String) -> Result { + let lox_cred: Lox = serde_json::from_str(&trusted_cred).unwrap(); + let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + let reach_cred = generate_reachability_cred(&lox_cred, encrypted_table); + + let issue_result = + match issue_invite::request(&lox_cred, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, test_today(371)) { + Ok(issue_result) => issue_result, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; + let req_state = IssueInviteReqState { + request: issue_result.0, + state: issue_result.1, + }; + unsafe { + log(&format!( + "Formatted Issue Invite request: {}", + serde_json::to_string(&req_state).unwrap() + )); + } + Ok(serde_json::to_string(&req_state).unwrap()) +} + + +#[wasm_bindgen] +pub fn handle_issue_invite( + issue_invite_request: String, + issue_invite_response: String, + lox_pub: String +) -> Result { + let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + let req_state: IssueInviteReqState = serde_json::from_str(&issue_invite_request).unwrap(); + let deserialized_state = req_state.state; + let deserialized_response = serde_json::from_str(&issue_invite_response).unwrap(); + let issue_invite_cred = + match issue_invite::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub, &pubkeys.invitation_pub) { + Ok(issue_invite_cred) => issue_invite_cred, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; + unsafe { + log(&format!( + "Got new Invitation Credential and Lox Credential: {}", + serde_json::to_string(&issue_invite_cred).unwrap() + )); + } + Ok(serde_json::to_string(&issue_invite_cred).unwrap()) +} // This should also check the pubkey fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> {