From dd34616d887acd78992736922c8b367729835271 Mon Sep 17 00:00:00 2001 From: Cecylia Bocovich Date: Wed, 9 Nov 2022 18:37:10 +0000 Subject: [PATCH 01/34] Initial commit --- crates/lox-wasm/README.md | 92 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 crates/lox-wasm/README.md diff --git a/crates/lox-wasm/README.md b/crates/lox-wasm/README.md new file mode 100644 index 0000000..feb02c5 --- /dev/null +++ b/crates/lox-wasm/README.md @@ -0,0 +1,92 @@ +# lox-wasm + + + +## Getting started + +To make it easy for you to get started with GitLab, here's a list of recommended next steps. + +Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! + +## Add your files + +- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files +- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command: + +``` +cd existing_repo +git remote add origin https://gitlab.torproject.org/cohosh/lox-wasm.git +git branch -M main +git push -uf origin main +``` + +## Integrate with your tools + +- [ ] [Set up project integrations](https://gitlab.torproject.org/cohosh/lox-wasm/-/settings/integrations) + +## Collaborate with your team + +- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/) +- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) +- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically) +- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/) +- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html) + +## Test and Deploy + +Use the built-in continuous integration in GitLab. + +- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html) +- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/) +- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html) +- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/) +- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html) + +*** + +# Editing this README + +When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template. + +## Suggestions for a good README +Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. + +## Name +Choose a self-explaining name for your project. + +## Description +Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors. + +## Badges +On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. + +## Visuals +Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method. + +## Installation +Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection. + +## Usage +Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README. + +## Support +Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc. + +## Roadmap +If you have ideas for releases in the future, it is a good idea to list them in the README. + +## Contributing +State if you are open to contributions and what your requirements are for accepting them. + +For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. + +You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. + +## Authors and acknowledgment +Show your appreciation to those who have contributed to the project. + +## License +For open source projects, say how it is licensed. + +## Project status +If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. From a0d6d8a2c4ab6bc58c7ccdf1f681a932413b4b92 Mon Sep 17 00:00:00 2001 From: Cecylia Bocovich Date: Wed, 9 Nov 2022 18:38:42 +0000 Subject: [PATCH 02/34] Add LICENSE --- crates/lox-wasm/LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 crates/lox-wasm/LICENSE diff --git a/crates/lox-wasm/LICENSE b/crates/lox-wasm/LICENSE new file mode 100644 index 0000000..ca549f7 --- /dev/null +++ b/crates/lox-wasm/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Cecylia Bocovich + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From 1b285cff425791a21b0dd971a1c4df0e788628e1 Mon Sep 17 00:00:00 2001 From: Cecylia Bocovich Date: Wed, 9 Nov 2022 13:39:24 -0500 Subject: [PATCH 03/34] Update README --- crates/lox-wasm/README.md | 91 +-------------------------------------- 1 file changed, 1 insertion(+), 90 deletions(-) diff --git a/crates/lox-wasm/README.md b/crates/lox-wasm/README.md index feb02c5..c9db215 100644 --- a/crates/lox-wasm/README.md +++ b/crates/lox-wasm/README.md @@ -1,92 +1,3 @@ # lox-wasm - - -## Getting started - -To make it easy for you to get started with GitLab, here's a list of recommended next steps. - -Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! - -## Add your files - -- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files -- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command: - -``` -cd existing_repo -git remote add origin https://gitlab.torproject.org/cohosh/lox-wasm.git -git branch -M main -git push -uf origin main -``` - -## Integrate with your tools - -- [ ] [Set up project integrations](https://gitlab.torproject.org/cohosh/lox-wasm/-/settings/integrations) - -## Collaborate with your team - -- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/) -- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) -- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically) -- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/) -- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html) - -## Test and Deploy - -Use the built-in continuous integration in GitLab. - -- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html) -- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/) -- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html) -- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/) -- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html) - -*** - -# Editing this README - -When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template. - -## Suggestions for a good README -Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. - -## Name -Choose a self-explaining name for your project. - -## Description -Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors. - -## Badges -On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. - -## Visuals -Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method. - -## Installation -Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection. - -## Usage -Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README. - -## Support -Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc. - -## Roadmap -If you have ideas for releases in the future, it is a good idea to list them in the README. - -## Contributing -State if you are open to contributions and what your requirements are for accepting them. - -For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. - -You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. - -## Authors and acknowledgment -Show your appreciation to those who have contributed to the project. - -## License -For open source projects, say how it is licensed. - -## Project status -If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. +wasm bindings for the lox crate: https://git-crysp.uwaterloo.ca/iang/lox From ff905db53a4c8d37f8d535b33c8314fecca5d9f2 Mon Sep 17 00:00:00 2001 From: Cecylia Bocovich Date: Wed, 9 Nov 2022 14:40:53 -0500 Subject: [PATCH 04/34] First attempt at binding the open invite function --- crates/lox-wasm/Cargo.toml | 14 ++++++++++++++ crates/lox-wasm/src/lib.rs | 23 +++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 crates/lox-wasm/Cargo.toml create mode 100644 crates/lox-wasm/src/lib.rs diff --git a/crates/lox-wasm/Cargo.toml b/crates/lox-wasm/Cargo.toml new file mode 100644 index 0000000..0ed661c --- /dev/null +++ b/crates/lox-wasm/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "lox-bindings" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +crate-type = ["cdylib"] + +[dependencies] +lox = { git = "https://git-crysp.uwaterloo.ca/iang/lox.git" } +wasm-bindgen = "0.2" +serde_json = "1.0.87" diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs new file mode 100644 index 0000000..e368d9d --- /dev/null +++ b/crates/lox-wasm/src/lib.rs @@ -0,0 +1,23 @@ +use wasm_bindgen::prelude::*; +use lox::OPENINV_LENGTH; +use lox::proto::*; +use serde_json; + +#[wasm_bindgen] +extern { + #[wasm_bindgen(js_namespace = console)] + pub fn log(s: &str); +} + +#[wasm_bindgen] +pub fn open_invite(invite: &[u8]) { + + let (request, _state) = open_invite::request(&validate_invite(invite)); + let serialized_request = serde_json::to_string(&request).unwrap(); + log(&format!("request: {}", serialized_request)); +} + + +fn validate_invite(invite: &[u8]) -> [u8; OPENINV_LENGTH] { + invite.try_into().expect("slice with incorrect length") +} From fcabba5e6e940037991ebc3bdc3df20d2f4575ec Mon Sep 17 00:00:00 2001 From: Cecylia Bocovich Date: Thu, 10 Nov 2022 11:00:28 -0500 Subject: [PATCH 05/34] Update Cargo.toml --- crates/lox-wasm/Cargo.toml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/lox-wasm/Cargo.toml b/crates/lox-wasm/Cargo.toml index 0ed661c..055a82e 100644 --- a/crates/lox-wasm/Cargo.toml +++ b/crates/lox-wasm/Cargo.toml @@ -1,9 +1,10 @@ [package] -name = "lox-bindings" +name = "lox-wasm" +authors = ["Cecylia Bocovich "] version = "0.1.0" edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +description = "WASM bindings for lox" +license = "MIT" [lib] crate-type = ["cdylib"] From 48b8a986efa833a05a86960338ee3d7a5c2b551d Mon Sep 17 00:00:00 2001 From: Cecylia Bocovich Date: Thu, 10 Nov 2022 11:04:20 -0500 Subject: [PATCH 06/34] Avoid use of * with use --- crates/lox-wasm/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index e368d9d..5f30efe 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -1,6 +1,6 @@ use wasm_bindgen::prelude::*; use lox::OPENINV_LENGTH; -use lox::proto::*; +use lox::proto::open_invite; use serde_json; #[wasm_bindgen] From 6e3525ac4c035cc5a7f6894fa5fdb63752355a9a Mon Sep 17 00:00:00 2001 From: Cecylia Bocovich Date: Thu, 10 Nov 2022 11:05:46 -0500 Subject: [PATCH 07/34] Add .gitignore --- crates/lox-wasm/.gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 crates/lox-wasm/.gitignore diff --git a/crates/lox-wasm/.gitignore b/crates/lox-wasm/.gitignore new file mode 100644 index 0000000..fbd7c91 --- /dev/null +++ b/crates/lox-wasm/.gitignore @@ -0,0 +1,2 @@ +target/* +Cargo.lock From ebae7fb6004ade4702a6727d0308e3b252c5777d Mon Sep 17 00:00:00 2001 From: Cecylia Bocovich Date: Tue, 15 Nov 2022 21:57:49 -0500 Subject: [PATCH 08/34] Improve error handling and add rand dependency This makes panics more visible in the console and fixes a weird bug where the rand package was throwing an error. --- crates/lox-wasm/Cargo.toml | 4 ++++ crates/lox-wasm/src/lib.rs | 27 ++++++++++++++++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/crates/lox-wasm/Cargo.toml b/crates/lox-wasm/Cargo.toml index 055a82e..58336b8 100644 --- a/crates/lox-wasm/Cargo.toml +++ b/crates/lox-wasm/Cargo.toml @@ -13,3 +13,7 @@ crate-type = ["cdylib"] lox = { git = "https://git-crysp.uwaterloo.ca/iang/lox.git" } wasm-bindgen = "0.2" serde_json = "1.0.87" + +console_error_panic_hook = "0.1.7" + +rand = { version = "0.7", features = ["wasm-bindgen"] } diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 5f30efe..6a50392 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -2,6 +2,8 @@ use wasm_bindgen::prelude::*; use lox::OPENINV_LENGTH; use lox::proto::open_invite; use serde_json; +use std::array::TryFromSliceError; +use std::panic; #[wasm_bindgen] extern { @@ -9,15 +11,26 @@ extern { pub fn log(s: &str); } +#[wasm_bindgen] +pub fn set_panic_hook() { + panic::set_hook(Box::new(console_error_panic_hook::hook)); +} + #[wasm_bindgen] pub fn open_invite(invite: &[u8]) { - - let (request, _state) = open_invite::request(&validate_invite(invite)); - let serialized_request = serde_json::to_string(&request).unwrap(); - log(&format!("request: {}", serialized_request)); + log(&format!("Using invite: {:?}", invite)); + match validate(invite) { + Ok(token) => { + let (request, _state) = open_invite::request(&token); + let serialized_request = serde_json::to_string(&request).unwrap(); + log(&format!("Formatted open invite request: {}", serialized_request)); + }, + Err(e) => { + log(&format!("{:?}", e)); + }, + } } - -fn validate_invite(invite: &[u8]) -> [u8; OPENINV_LENGTH] { - invite.try_into().expect("slice with incorrect length") +fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> { + invite.try_into() } From 9de53da203848c6421fcb5aa6bf23a81359fcc60 Mon Sep 17 00:00:00 2001 From: Cecylia Bocovich Date: Tue, 15 Nov 2022 22:03:30 -0500 Subject: [PATCH 09/34] Instructions and a simple test of the bindings --- crates/lox-wasm/README.md | 23 ++++++++++++++++++ crates/lox-wasm/index.html | 49 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 crates/lox-wasm/index.html diff --git a/crates/lox-wasm/README.md b/crates/lox-wasm/README.md index c9db215..3f0ffb7 100644 --- a/crates/lox-wasm/README.md +++ b/crates/lox-wasm/README.md @@ -1,3 +1,26 @@ # lox-wasm wasm bindings for the lox crate: https://git-crysp.uwaterloo.ca/iang/lox + +# Dependencies + +``` +cargo install wasm-pack +``` + +# Build + +``` +wasm-pack build --target web +``` + +# Testing + +The provided `index.html` file can be used for testing the lox bindings. First, follow the instructions to [run a lox server](https://gitlab.torproject.org/cohosh/lox-server). + +Then, spin up a simple local webserver in the current directory: +``` +python3 -m http.server 8000 +``` + +Next, open the dev console in your browser and navigate to `http://localhost:8000`. diff --git a/crates/lox-wasm/index.html b/crates/lox-wasm/index.html new file mode 100644 index 0000000..abbc18d --- /dev/null +++ b/crates/lox-wasm/index.html @@ -0,0 +1,49 @@ + + + + + Lox binding test + + + + + + From 21a29a09eb8495302e52a6084b35d4674f7a3e39 Mon Sep 17 00:00:00 2001 From: Cecylia Bocovich Date: Fri, 13 Jan 2023 14:16:37 -0500 Subject: [PATCH 10/34] Put lox test script into its own file --- crates/lox-wasm/index.html | 40 +------------------------------------- crates/lox-wasm/index.js | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 39 deletions(-) create mode 100644 crates/lox-wasm/index.js diff --git a/crates/lox-wasm/index.html b/crates/lox-wasm/index.html index abbc18d..4c7c7e2 100644 --- a/crates/lox-wasm/index.html +++ b/crates/lox-wasm/index.html @@ -5,45 +5,7 @@ Lox binding test - + diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js new file mode 100644 index 0000000..0d48133 --- /dev/null +++ b/crates/lox-wasm/index.js @@ -0,0 +1,36 @@ +import init, { open_invite, set_panic_hook } from "./pkg/lox_wasm.js"; +init().then(() => { + set_panic_hook(); + request_open_invite().then((token) => { + open_invite(token); + }); +}); + +function request_open_invite() { + return new Promise((fulfill, reject) => { + const xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function() { + if (xhr.DONE !== xhr.readyState) { + return; + } + if (xhr.status !== 200) { + console.log("Error. Status code: "+xhr.status); + console.log(xhr); + reject(); + return; + } + const response = JSON.parse(xhr.responseText); + console.log("Got invitation token: "+response.invite); + fulfill(response.invite); + return; + }; + try { + xhr.open('GET', "http://localhost:8001"); + } catch (err) { + console.log("Error connecting to lox bridge db"); + reject(); + return; + } + xhr.send(); + }); +} From e117dc554036a63bc96f38a3a5039c4f6e3bfc45 Mon Sep 17 00:00:00 2001 From: Cecylia Bocovich Date: Fri, 13 Jan 2023 14:23:35 -0500 Subject: [PATCH 11/34] Refactor the xhr so other requests can reuse the code --- crates/lox-wasm/index.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 0d48133..abf80e2 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -7,6 +7,19 @@ init().then(() => { }); function request_open_invite() { + return new Promise((fulfill, reject) => { + loxServerRequest("").then((response) => { + console.log("Got invitation token: " + response.invite); + fulfill(response.invite); + return; + }).catch(() => { + console.log("Error requesting open invite from Lox server"); + reject(); + }); + }); +} + +function loxServerRequest(data) { return new Promise((fulfill, reject) => { const xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { @@ -20,8 +33,7 @@ function request_open_invite() { return; } const response = JSON.parse(xhr.responseText); - console.log("Got invitation token: "+response.invite); - fulfill(response.invite); + fulfill(response); return; }; try { From 5d60807a42d6052ec3e5e9f5dd37539d73abac21 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Wed, 25 Jan 2023 15:02:23 -0500 Subject: [PATCH 12/34] Updates open_invite to return request and state --- crates/lox-wasm/Cargo.toml | 3 +- crates/lox-wasm/index.js | 25 ++++++++++-- crates/lox-wasm/src/lib.rs | 83 +++++++++++++++++++++++++++++++------- 3 files changed, 93 insertions(+), 18 deletions(-) diff --git a/crates/lox-wasm/Cargo.toml b/crates/lox-wasm/Cargo.toml index 58336b8..c54e2e7 100644 --- a/crates/lox-wasm/Cargo.toml +++ b/crates/lox-wasm/Cargo.toml @@ -10,9 +10,10 @@ license = "MIT" crate-type = ["cdylib"] [dependencies] -lox = { git = "https://git-crysp.uwaterloo.ca/iang/lox.git" } +lox = { git = "https://gitlab.torproject.org/onyinyang/lox.git" } wasm-bindgen = "0.2" serde_json = "1.0.87" +serde-wasm-bindgen = "0.4.5" console_error_panic_hook = "0.1.7" diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index abf80e2..269d1b4 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -1,9 +1,12 @@ import init, { open_invite, set_panic_hook } from "./pkg/lox_wasm.js"; init().then(() => { set_panic_hook(); - request_open_invite().then((token) => { +requested = request_open_invite().then((token) => { open_invite(token); }); +// lox_credential = request_new_lox_credential(request_cred[0]).then((lox_cred) => { +// handle_new_lox_credential(request_cred[1], response, pubkey); +// }) }); function request_open_invite() { @@ -21,7 +24,7 @@ function request_open_invite() { function loxServerRequest(data) { return new Promise((fulfill, reject) => { - const xhr = new XMLHttpRequest(); + const xhr = new XMLHttpRequest(data); xhr.onreadystatechange = function() { if (xhr.DONE !== xhr.readyState) { return; @@ -37,12 +40,28 @@ function loxServerRequest(data) { return; }; try { + if (data != ""){ xhr.open('GET', "http://localhost:8001"); + } + else { + xhr.open("Post", "http://localhost:8001") + xhr.setRequestHeader("Content-Type", "application/json"); + var data = JSON.stringify({}) + } } catch (err) { console.log("Error connecting to lox bridge db"); reject(); return; } - xhr.send(); + xhr.send(data); }); } + +// The correct key should be matched against a public commit to the key to +// verify that the key issuer is in fact the correct Bridge Authority +function loxKeyRequest(key_type) { + return new Promise((fulfull, reject) => { + + + }) +} diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 6a50392..6971ffd 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -1,12 +1,15 @@ -use wasm_bindgen::prelude::*; -use lox::OPENINV_LENGTH; +use lox::bridge_table::BridgeLine; +use lox::cred; use lox::proto::open_invite; +use lox::{IssuerPubKey, OPENINV_LENGTH}; use serde_json; +use serde_wasm_bindgen; use std::array::TryFromSliceError; use std::panic; +use wasm_bindgen::prelude::*; #[wasm_bindgen] -extern { +extern "C" { #[wasm_bindgen(js_namespace = console)] pub fn log(s: &str); } @@ -17,20 +20,72 @@ pub fn set_panic_hook() { } #[wasm_bindgen] -pub fn open_invite(invite: &[u8]) { - log(&format!("Using invite: {:?}", invite)); - match validate(invite) { - Ok(token) => { - let (request, _state) = open_invite::request(&token); - let serialized_request = serde_json::to_string(&request).unwrap(); - log(&format!("Formatted open invite request: {}", serialized_request)); - }, - Err(e) => { - log(&format!("{:?}", e)); - }, +pub fn open_invite(invite: &[u8]) -> Result { + unsafe { + log(&format!("Using invite: {:?}", invite)); } + let token = match validate(invite) { + Ok(token) => token, + Err(e) => return Err(JsValue::from(e.to_string())), + }; + let (request, state) = open_invite::request(&token); + let serialized_request = serde_json::to_string(&request).unwrap(); + let serialized_state = serde_json::to_string(&state).unwrap(); + unsafe { + log(&format!( + "Formatted open invite request: {}", + serialized_request + )); + } + let open_lox_result = concat_string(serialized_request, serialized_state); + Ok(open_lox_result) } +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; +} + +/* +#[wasm_bindgen] +pub fn handle_new_lox_credential(state: String, response: String) -> Result { + unsafe { + log(&format!("Using server response: {:?}", response)); + } + let deserialized_state = serde_json::from_str(&state).unwrap(); + let deserialized_response = serde_json::from_str(&response).unwrap(); + let lox_cred = match open_invite::handle_response(deserialized_state, deserialized_response, lox_pub) { + 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)); + } + Ok((serialized_credential, serialized_bridgeline)) +} */ + +/* Somehow get pubkeys and return to function +#[wasm_bindgen] +pub async fn get_pubkey(key_type: String) -> Result { + 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 fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> { invite.try_into() } From d473b537458bab59b99cd79ecd1a34519d25be19 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Wed, 25 Jan 2023 15:02:39 -0500 Subject: [PATCH 13/34] Add wasm_bindgen for handling new lox credential --- crates/lox-wasm/Cargo.toml | 2 +- crates/lox-wasm/src/lib.rs | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/crates/lox-wasm/Cargo.toml b/crates/lox-wasm/Cargo.toml index c54e2e7..1954a57 100644 --- a/crates/lox-wasm/Cargo.toml +++ b/crates/lox-wasm/Cargo.toml @@ -10,7 +10,7 @@ license = "MIT" crate-type = ["cdylib"] [dependencies] -lox = { git = "https://gitlab.torproject.org/onyinyang/lox.git" } +lox = { git = "https://gitlab.torproject.org/onyinyang/lox.git", branch = "master" } wasm-bindgen = "0.2" serde_json = "1.0.87" serde-wasm-bindgen = "0.4.5" diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 6971ffd..56d23e8 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -1,9 +1,8 @@ use lox::bridge_table::BridgeLine; -use lox::cred; use lox::proto::open_invite; use lox::{IssuerPubKey, OPENINV_LENGTH}; use serde_json; -use serde_wasm_bindgen; +//use serde_wasm_bindgen; use std::array::TryFromSliceError; use std::panic; use wasm_bindgen::prelude::*; @@ -49,15 +48,16 @@ fn concat_string(request: String, state: String) -> String { return new_string; } -/* + #[wasm_bindgen] -pub fn handle_new_lox_credential(state: String, response: String) -> Result { +pub fn handle_new_lox_credential(state: String, response: String, lox_pub: String) -> Result { unsafe { log(&format!("Using server response: {:?}", response)); } let deserialized_state = serde_json::from_str(&state).unwrap(); let deserialized_response = serde_json::from_str(&response).unwrap(); - let lox_cred = match open_invite::handle_response(deserialized_state, deserialized_response, lox_pub) { + let deserialized_pubkey = serde_json::from_str(&lox_pub).unwrap(); + let lox_cred = match open_invite::handle_response(deserialized_state, deserialized_response, &deserialized_pubkey) { Ok(lox_cred) => lox_cred, Err(e) => return Err(JsValue::from(e.to_string())), }; @@ -67,8 +67,9 @@ pub fn handle_new_lox_credential(state: String, response: String) -> Result Date: Wed, 25 Jan 2023 15:02:53 -0500 Subject: [PATCH 14/34] Change calls to match server --- crates/lox-wasm/index.js | 34 ++++++++++++++++++++++++---------- crates/lox-wasm/src/lib.rs | 11 +++++++++-- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 269d1b4..6b62307 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -1,9 +1,14 @@ import init, { open_invite, set_panic_hook } from "./pkg/lox_wasm.js"; +var pubkeys = request_pubkeys().then((pubkeys) => { + pubkeys +}); +console.log(pubkeys) init().then(() => { set_panic_hook(); requested = request_open_invite().then((token) => { open_invite(token); }); +// unsigned_open_lox_credential = handle_new_lox_credential(requested, pubkeys); // lox_credential = request_new_lox_credential(request_cred[0]).then((lox_cred) => { // handle_new_lox_credential(request_cred[1], response, pubkey); // }) @@ -11,7 +16,7 @@ requested = request_open_invite().then((token) => { function request_open_invite() { return new Promise((fulfill, reject) => { - loxServerRequest("").then((response) => { + loxServerRequest("/invite").then((response) => { console.log("Got invitation token: " + response.invite); fulfill(response.invite); return; @@ -22,6 +27,19 @@ function request_open_invite() { }); } +function request_pubkeys() { + return new Promise((fulfill, reject) => { + loxServerRequest("/pubkeys").then((response) => { + console.log("Got pubkeys: " + JSON.parse(response)); + fulfill(JSON.parse(response)); + return; + }).catch(() => { + console.log("Error requesting open invite from Lox server"); + reject(); + }); + }); +} + function loxServerRequest(data) { return new Promise((fulfill, reject) => { const xhr = new XMLHttpRequest(data); @@ -40,20 +58,16 @@ function loxServerRequest(data) { return; }; try { - if (data != ""){ - xhr.open('GET', "http://localhost:8001"); - } - else { - xhr.open("Post", "http://localhost:8001") - xhr.setRequestHeader("Content-Type", "application/json"); - var data = JSON.stringify({}) - } + xhr.open('GET', "http://localhost:8001"+data); + //xhr.open("Post", "http://localhost:8001"+data) + //xhr.setRequestHeader("Content-Type", "application/json"); + //var data = JSON.stringify({}) } catch (err) { console.log("Error connecting to lox bridge db"); reject(); return; } - xhr.send(data); + xhr.send(); }); } diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 56d23e8..b5fa0ba 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -48,12 +48,19 @@ fn concat_string(request: String, state: String) -> String { return new_string; } +fn deconcat_string(concatString: String) -> (String, String) { + + ("hello".to_owned(), "world".to_owned()) + +} + #[wasm_bindgen] -pub fn handle_new_lox_credential(state: String, response: String, lox_pub: String) -> Result { +pub fn handle_new_lox_credential(open_lox_result: String, lox_pub: String) -> Result { unsafe { - log(&format!("Using server response: {:?}", response)); + log(&format!("Using server response: {:?}", open_lox_result)); } + let (state, response) = deconcat_string(open_lox_result); let deserialized_state = serde_json::from_str(&state).unwrap(); let deserialized_response = serde_json::from_str(&response).unwrap(); let deserialized_pubkey = serde_json::from_str(&lox_pub).unwrap(); From dd1b97191426dac37c55c13889a70a16aa33f6f9 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Thu, 26 Jan 2023 00:17:14 -0500 Subject: [PATCH 15/34] Fix javascript to handle values properly --- crates/lox-wasm/index.js | 16 +++++++++------- crates/lox-wasm/src/lib.rs | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 6b62307..8aa6a2a 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -1,18 +1,20 @@ import init, { open_invite, set_panic_hook } from "./pkg/lox_wasm.js"; -var pubkeys = request_pubkeys().then((pubkeys) => { - pubkeys -}); -console.log(pubkeys) -init().then(() => { +let pubkeys = await request_pubkeys(); +console.log(pubkeys); + + +let requested = await init().then(() => { set_panic_hook(); -requested = request_open_invite().then((token) => { - open_invite(token); + let requested = request_open_invite().then((token) => { + return open_invite(token); }); + return requested; // unsigned_open_lox_credential = handle_new_lox_credential(requested, pubkeys); // lox_credential = request_new_lox_credential(request_cred[0]).then((lox_cred) => { // handle_new_lox_credential(request_cred[1], response, pubkey); // }) }); +console.log("Got request and state "+requested); function request_open_invite() { return new Promise((fulfill, reject) => { diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index b5fa0ba..10eb93d 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -56,7 +56,7 @@ fn deconcat_string(concatString: String) -> (String, String) { #[wasm_bindgen] -pub fn handle_new_lox_credential(open_lox_result: String, lox_pub: String) -> Result { +pub fn handle_new_lox_credential(open_lox_result: String, open_lox_response: String, lox_pub: String) -> Result { unsafe { log(&format!("Using server response: {:?}", open_lox_result)); } From 03008449faba4c355b14645edad13f56d1c1f5d8 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Thu, 26 Jan 2023 14:16:41 -0500 Subject: [PATCH 16/34] Handle request and state more reasonably --- crates/lox-wasm/Cargo.toml | 1 + crates/lox-wasm/src/lib.rs | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/crates/lox-wasm/Cargo.toml b/crates/lox-wasm/Cargo.toml index 1954a57..5af794d 100644 --- a/crates/lox-wasm/Cargo.toml +++ b/crates/lox-wasm/Cargo.toml @@ -13,6 +13,7 @@ crate-type = ["cdylib"] lox = { git = "https://gitlab.torproject.org/onyinyang/lox.git", branch = "master" } wasm-bindgen = "0.2" serde_json = "1.0.87" +serde = "1" serde-wasm-bindgen = "0.4.5" console_error_panic_hook = "0.1.7" diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 10eb93d..6d821ff 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -2,11 +2,18 @@ use lox::bridge_table::BridgeLine; use lox::proto::open_invite; use lox::{IssuerPubKey, OPENINV_LENGTH}; use serde_json; +use serde::{Deserialize,Serialize}; //use serde_wasm_bindgen; use std::array::TryFromSliceError; use std::panic; use wasm_bindgen::prelude::*; +#[derive(Debug,Deserialize, Serialize)] +struct ReqState { + request: String, + state: String, +} + #[wasm_bindgen] extern "C" { #[wasm_bindgen(js_namespace = console)] @@ -28,16 +35,17 @@ pub fn open_invite(invite: &[u8]) -> Result { Err(e) => return Err(JsValue::from(e.to_string())), }; let (request, state) = open_invite::request(&token); - let serialized_request = serde_json::to_string(&request).unwrap(); - let serialized_state = serde_json::to_string(&state).unwrap(); + let req_state = ReqState { + request: serde_json::to_string(&request).unwrap(), + state: serde_json::to_string(&state).unwrap(), + }; unsafe { log(&format!( "Formatted open invite request: {}", - serialized_request + serde_json::to_string(&req_state).unwrap() )); } - let open_lox_result = concat_string(serialized_request, serialized_state); - Ok(open_lox_result) + Ok(serde_json::to_string(&req_state).unwrap()) } fn concat_string(request: String, state: String) -> String { From 832a252318db4d764c51fabf3f430eb0eed96329 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Mon, 30 Jan 2023 16:03:12 -0500 Subject: [PATCH 17/34] Fix serialization of request/state --- crates/lox-wasm/src/lib.rs | 66 +++++++++++++------------------------- 1 file changed, 22 insertions(+), 44 deletions(-) diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 6d821ff..6233666 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -8,10 +8,16 @@ use std::array::TryFromSliceError; use std::panic; use wasm_bindgen::prelude::*; -#[derive(Debug,Deserialize, Serialize)] +#[derive(Deserialize, Serialize)] struct ReqState { - request: String, - state: String, + request: lox::proto::open_invite::Request, + state: lox::proto::open_invite::State, +} + +#[derive(Debug,Deserialize, Serialize)] +struct Credential { + lox_credential: String, + bridgeline: String, } #[wasm_bindgen] @@ -36,9 +42,9 @@ pub fn open_invite(invite: &[u8]) -> Result { }; let (request, state) = open_invite::request(&token); let req_state = ReqState { - request: serde_json::to_string(&request).unwrap(), - state: serde_json::to_string(&state).unwrap(), - }; + request: request, + state: state, + }; unsafe { log(&format!( "Formatted open invite request: {}", @@ -48,59 +54,31 @@ pub fn open_invite(invite: &[u8]) -> Result { 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] pub fn handle_new_lox_credential(open_lox_result: String, open_lox_response: String, lox_pub: String) -> Result { unsafe { log(&format!("Using server response: {:?}", open_lox_result)); } - let (state, response) = deconcat_string(open_lox_result); - let deserialized_state = serde_json::from_str(&state).unwrap(); - let deserialized_response = serde_json::from_str(&response).unwrap(); + let req_state: ReqState = serde_json::from_str(&open_lox_result).unwrap(); + let deserialized_state = req_state.state; + let deserialized_response = serde_json::from_str(&open_lox_response).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) { 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(); + let lox_cred = Credential { + lox_credential: serde_json::to_string(&lox_cred.0).unwrap(), + 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)); + log(&format!("Got new Lox Credential: {}", lox_cred.lox_credential)); + log(&format!("Got new bridgeline: {}", lox_cred.bridgeline)); } - let open_lox_response = concat_string(serialized_credential, serialized_bridgeline); - Ok(open_lox_response) + Ok(serde_json::to_string(&lox_cred).unwrap()) } -/* Somehow get pubkeys and return to function -#[wasm_bindgen] -pub async fn get_pubkey(key_type: String) -> Result { - 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 fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> { invite.try_into() From dfa2ae4c6f926bdcbf1dc1db230d7715f5e06ae4 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Mon, 30 Jan 2023 18:13:17 -0500 Subject: [PATCH 18/34] Handle open invite credential --- crates/lox-wasm/index.js | 73 +++++++++++++++++++++++++++++++------- crates/lox-wasm/src/lib.rs | 2 +- 2 files changed, 61 insertions(+), 14 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 8aa6a2a..0110e33 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -1,4 +1,4 @@ -import init, { open_invite, set_panic_hook } from "./pkg/lox_wasm.js"; +import init, { open_invite, handle_new_lox_credential, set_panic_hook } from "./pkg/lox_wasm.js"; let pubkeys = await request_pubkeys(); console.log(pubkeys); @@ -9,16 +9,36 @@ let requested = await init().then(() => { return open_invite(token); }); return requested; -// unsigned_open_lox_credential = handle_new_lox_credential(requested, pubkeys); -// lox_credential = request_new_lox_credential(request_cred[0]).then((lox_cred) => { -// handle_new_lox_credential(request_cred[1], response, pubkey); -// }) }); console.log("Got request and state "+requested); +let open_lox_cred = await init().then(() => { + set_panic_hook(); + let cred = request_new_lox_credential(requested).then((response) => { + return handle_new_lox_credential(requested, response, pubkeys); + }); + return cred; +}); +console.log("Got request and state "+requested); + +function request_new_lox_credential(requested) { + let req = JSON.parse(requested); + console.log("Request? "+req.request); + return new Promise((fulfill, reject) => { + loxServerPostRequest("/openreq", req.request).then((response) => { + console.log("Got new Lox Credential: " + response); + fulfill(response); + return; + }).catch(() => { + console.log("Error requesting new Lox credential from server"); + reject(); + }); + }); +} + function request_open_invite() { return new Promise((fulfill, reject) => { - loxServerRequest("/invite").then((response) => { + loxServerGetRequest("/invite").then((response) => { console.log("Got invitation token: " + response.invite); fulfill(response.invite); return; @@ -31,8 +51,8 @@ function request_open_invite() { function request_pubkeys() { return new Promise((fulfill, reject) => { - loxServerRequest("/pubkeys").then((response) => { - console.log("Got pubkeys: " + JSON.parse(response)); + loxServerGetRequest("/pubkeys").then((response) => { + console.log("Got pubkeys: " + response); fulfill(JSON.parse(response)); return; }).catch(() => { @@ -42,9 +62,9 @@ function request_pubkeys() { }); } -function loxServerRequest(data) { +function loxServerGetRequest(data) { return new Promise((fulfill, reject) => { - const xhr = new XMLHttpRequest(data); + const xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.DONE !== xhr.readyState) { return; @@ -61,9 +81,6 @@ function loxServerRequest(data) { }; try { xhr.open('GET', "http://localhost:8001"+data); - //xhr.open("Post", "http://localhost:8001"+data) - //xhr.setRequestHeader("Content-Type", "application/json"); - //var data = JSON.stringify({}) } catch (err) { console.log("Error connecting to lox bridge db"); reject(); @@ -73,6 +90,36 @@ function loxServerRequest(data) { }); } +function loxServerPostRequest(data, payload) { + return new Promise((fulfill, reject) => { + const xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function() { + if (xhr.DONE !== xhr.readyState) { + return; + } + if (xhr.status !== 200) { + console.log("Error. Status code: "+xhr.status); + console.log(xhr); + reject(); + return; + } + const response = JSON.parse(xhr.responseText); + console.log("Received: "+response); + fulfill(response); + return; + }; + try { + xhr.open('POST', "http://localhost:8001"+data, true) + xhr.setRequestHeader("Content-Type", "application/json"); + } catch (err) { + console.log("Error connecting to lox bridge db"); + reject(); + return; + } + xhr.send(JSON.stringify(payload)); + }); +} + // The correct key should be matched against a public commit to the key to // verify that the key issuer is in fact the correct Bridge Authority function loxKeyRequest(key_type) { diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 6233666..a3df463 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -58,7 +58,7 @@ pub fn open_invite(invite: &[u8]) -> Result { #[wasm_bindgen] pub fn handle_new_lox_credential(open_lox_result: String, open_lox_response: String, lox_pub: String) -> Result { unsafe { - log(&format!("Using server response: {:?}", open_lox_result)); + log(&format!("Using server response: {:?}", open_lox_result)); } let req_state: ReqState = serde_json::from_str(&open_lox_result).unwrap(); let deserialized_state = req_state.state; From 8cc9d3d5d36de6388178c6c9e4c51f7c60aeff02 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Wed, 1 Feb 2023 00:42:49 -0500 Subject: [PATCH 19/34] Change pubkey passing, handling credentials still broken --- crates/lox-wasm/index.js | 8 +++----- crates/lox-wasm/src/lib.rs | 23 ++++++++++++++++++++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 0110e33..bfcdb44 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -2,7 +2,6 @@ import init, { open_invite, handle_new_lox_credential, set_panic_hook } from "./ let pubkeys = await request_pubkeys(); console.log(pubkeys); - let requested = await init().then(() => { set_panic_hook(); let requested = request_open_invite().then((token) => { @@ -22,9 +21,9 @@ let open_lox_cred = await init().then(() => { console.log("Got request and state "+requested); function request_new_lox_credential(requested) { + return new Promise((fulfill, reject) => { let req = JSON.parse(requested); console.log("Request? "+req.request); - return new Promise((fulfill, reject) => { loxServerPostRequest("/openreq", req.request).then((response) => { console.log("Got new Lox Credential: " + response); fulfill(response); @@ -52,8 +51,8 @@ function request_open_invite() { function request_pubkeys() { return new Promise((fulfill, reject) => { loxServerGetRequest("/pubkeys").then((response) => { - console.log("Got pubkeys: " + response); - fulfill(JSON.parse(response)); + console.log("Got pubkeys: " + JSON.stringify(response)); + fulfill(JSON.stringify(response)); return; }).catch(() => { console.log("Error requesting open invite from Lox server"); @@ -104,7 +103,6 @@ function loxServerPostRequest(data, payload) { return; } const response = JSON.parse(xhr.responseText); - console.log("Received: "+response); fulfill(response); return; }; diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index a3df463..57916d0 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -14,6 +14,15 @@ struct ReqState { state: lox::proto::open_invite::State, } +#[derive(Debug, Deserialize,Serialize)] +struct PubKeys { + lox_pub: IssuerPubKey, + migration_pub: IssuerPubKey, + migrationkey_pub: IssuerPubKey, + reachability_pub: IssuerPubKey, + invitation_pub: IssuerPubKey, +} + #[derive(Debug,Deserialize, Serialize)] struct Credential { lox_credential: String, @@ -63,11 +72,18 @@ pub fn handle_new_lox_credential(open_lox_result: String, open_lox_response: Str let req_state: ReqState = serde_json::from_str(&open_lox_result).unwrap(); let deserialized_state = req_state.state; let deserialized_response = serde_json::from_str(&open_lox_response).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 pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + unsafe { + log(&format!("pubkeys: {:?}", pubkeys.lox_pub)); + } + let lox_cred = match open_invite::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) { Ok(lox_cred) => lox_cred, - Err(e) => return Err(JsValue::from(e.to_string())), + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } }; + log(&format!("Did this actually work?: {:?}", lox_cred)); let lox_cred = Credential { lox_credential: serde_json::to_string(&lox_cred.0).unwrap(), bridgeline: serde_json::to_string(&lox_cred.1).unwrap(), @@ -79,6 +95,7 @@ pub fn handle_new_lox_credential(open_lox_result: String, open_lox_response: Str Ok(serde_json::to_string(&lox_cred).unwrap()) } + // This should also check the pubkey fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> { invite.try_into() From 745b6673cb1f29cf586345a6b80002cb064dfd41 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Mon, 6 Feb 2023 13:58:24 -0500 Subject: [PATCH 20/34] Clean up messages, full first credential handling --- crates/lox-wasm/index.js | 8 ++++---- crates/lox-wasm/src/lib.rs | 35 ++++++++++++++++++----------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index bfcdb44..f04ad5b 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -18,15 +18,15 @@ let open_lox_cred = await init().then(() => { }); return cred; }); -console.log("Got request and state "+requested); +console.log("Got Lox Credential and BridgeLine "+open_lox_cred); function request_new_lox_credential(requested) { return new Promise((fulfill, reject) => { let req = JSON.parse(requested); - console.log("Request? "+req.request); + console.log("Request? "+JSON.stringify(req.request)); loxServerPostRequest("/openreq", req.request).then((response) => { - console.log("Got new Lox Credential: " + response); - fulfill(response); + console.log("Got new Lox Credential: " + JSON.stringify(response)); + fulfill(JSON.stringify(response)); return; }).catch(() => { console.log("Error requesting new Lox credential from server"); diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 57916d0..6f7f55a 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -1,8 +1,7 @@ -use lox::bridge_table::BridgeLine; use lox::proto::open_invite; use lox::{IssuerPubKey, OPENINV_LENGTH}; +use serde::{Deserialize, Serialize}; use serde_json; -use serde::{Deserialize,Serialize}; //use serde_wasm_bindgen; use std::array::TryFromSliceError; use std::panic; @@ -14,7 +13,7 @@ struct ReqState { state: lox::proto::open_invite::State, } -#[derive(Debug, Deserialize,Serialize)] +#[derive(Debug, Deserialize, Serialize)] struct PubKeys { lox_pub: IssuerPubKey, migration_pub: IssuerPubKey, @@ -23,7 +22,7 @@ struct PubKeys { invitation_pub: IssuerPubKey, } -#[derive(Debug,Deserialize, Serialize)] +#[derive(Debug, Deserialize, Serialize)] struct Credential { lox_credential: String, bridgeline: String, @@ -53,7 +52,7 @@ pub fn open_invite(invite: &[u8]) -> Result { let req_state = ReqState { request: request, state: state, - }; + }; unsafe { log(&format!( "Formatted open invite request: {}", @@ -63,39 +62,41 @@ pub fn open_invite(invite: &[u8]) -> Result { Ok(serde_json::to_string(&req_state).unwrap()) } - #[wasm_bindgen] -pub fn handle_new_lox_credential(open_lox_result: String, open_lox_response: String, lox_pub: String) -> Result { - unsafe { - log(&format!("Using server response: {:?}", open_lox_result)); - } +pub fn handle_new_lox_credential( + open_lox_result: String, + open_lox_response: String, + lox_pub: String, +) -> Result { let req_state: ReqState = serde_json::from_str(&open_lox_result).unwrap(); let deserialized_state = req_state.state; let deserialized_response = serde_json::from_str(&open_lox_response).unwrap(); let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); - unsafe { - log(&format!("pubkeys: {:?}", pubkeys.lox_pub)); - } - let lox_cred = match open_invite::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) { + let lox_cred = match open_invite::handle_response( + deserialized_state, + deserialized_response, + &pubkeys.lox_pub, + ) { Ok(lox_cred) => lox_cred, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); } }; - log(&format!("Did this actually work?: {:?}", lox_cred)); let lox_cred = Credential { lox_credential: serde_json::to_string(&lox_cred.0).unwrap(), bridgeline: serde_json::to_string(&lox_cred.1).unwrap(), }; unsafe { - log(&format!("Got new Lox Credential: {}", lox_cred.lox_credential)); + log(&format!( + "Got new Lox Credential: {}", + lox_cred.lox_credential + )); log(&format!("Got new bridgeline: {}", lox_cred.bridgeline)); } Ok(serde_json::to_string(&lox_cred).unwrap()) } - // This should also check the pubkey fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> { invite.try_into() From 080d5879b15cab0b29549541137e19514951c505 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Tue, 7 Feb 2023 00:45:54 -0500 Subject: [PATCH 21/34] Adds trust promo logic, time issue --- crates/lox-wasm/Cargo.toml | 2 +- crates/lox-wasm/index.js | 62 ++++++++++++++++++++------ crates/lox-wasm/src/lib.rs | 91 +++++++++++++++++++++++++++++++++----- 3 files changed, 129 insertions(+), 26 deletions(-) diff --git a/crates/lox-wasm/Cargo.toml b/crates/lox-wasm/Cargo.toml index 5af794d..7cf14be 100644 --- a/crates/lox-wasm/Cargo.toml +++ b/crates/lox-wasm/Cargo.toml @@ -17,5 +17,5 @@ serde = "1" serde-wasm-bindgen = "0.4.5" console_error_panic_hook = "0.1.7" - +time = "0.2" rand = { version = "0.7", features = ["wasm-bindgen"] } diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index f04ad5b..3c70961 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -1,31 +1,54 @@ -import init, { open_invite, handle_new_lox_credential, set_panic_hook } from "./pkg/lox_wasm.js"; +import init, { open_invite, handle_new_lox_credential, trust_promotion, handle_trust_promotion, set_panic_hook } from "./pkg/lox_wasm.js"; let pubkeys = await request_pubkeys(); -console.log(pubkeys); -let requested = await init().then(() => { +let requested_invite = await init().then(() => { set_panic_hook(); - let requested = request_open_invite().then((token) => { + let requested_invite = request_open_invite().then((token) => { return open_invite(token); }); - return requested; + return requested_invite; }); -console.log("Got request and state "+requested); +console.log("Got request and state: "+requested_invite); let open_lox_cred = await init().then(() => { set_panic_hook(); - let cred = request_new_lox_credential(requested).then((response) => { - return handle_new_lox_credential(requested, response, pubkeys); + let cred = request_new_lox_credential(requested_invite).then((response) => { + return handle_new_lox_credential(requested_invite, response, pubkeys); }); return cred; }); -console.log("Got Lox Credential and BridgeLine "+open_lox_cred); -function request_new_lox_credential(requested) { +let requested_trust_promo = trust_promotion(open_lox_cred, pubkeys); + +let trust_promo_cred = await init().then(() => { + set_panic_hook(); + let cred = request_trust_promo_cred(requested_trust_promo).then((response)=> { + return handle_trust_promotion(requested_trust_promo, response); + }); + return cred; + }); + + let reachability_cred = await request_reachability(); + +function request_new_lox_credential(requested_invite) { + return new Promise((fulfill, reject) => { + let req = JSON.parse(requested_invite); + loxServerPostRequest("/openreq", req.request).then((response) => { + console.log("Got new Open Invite Lox Credential: " + JSON.stringify(response)); + fulfill(JSON.stringify(response)); + return; + }).catch(() => { + console.log("Error requesting new Lox credential from server"); + reject(); + }); + }); +} + +function request_trust_promo_cred(requested) { return new Promise((fulfill, reject) => { let req = JSON.parse(requested); - console.log("Request? "+JSON.stringify(req.request)); - loxServerPostRequest("/openreq", req.request).then((response) => { - console.log("Got new Lox Credential: " + JSON.stringify(response)); + loxServerPostRequest("/trustpromo", req.request).then((response) => { + console.log("Got new Trust Promotion Lox Credential: " + JSON.stringify(response)); fulfill(JSON.stringify(response)); return; }).catch(() => { @@ -61,6 +84,19 @@ function request_pubkeys() { }); } +function request_reachability() { + return new Promise((fulfill, reject) => { + loxServerGetRequest("/reachability").then((response) => { + console.log("Got reachability Cred: " + JSON.stringify(response)); + fulfill(JSON.stringify(response)); + return; + }).catch(() => { + console.log("Error requesting bucket reachability credential from Lox server"); + reject(); + }); + }); +} + function loxServerGetRequest(data) { return new Promise((fulfill, reject) => { const xhr = new XMLHttpRequest(); diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 6f7f55a..41f0dbe 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -1,4 +1,6 @@ -use lox::proto::open_invite; +use lox::cred::Lox; +use lox::bridge_table::BridgeLine; +use lox::proto::{open_invite, trust_promotion}; use lox::{IssuerPubKey, OPENINV_LENGTH}; use serde::{Deserialize, Serialize}; use serde_json; @@ -8,9 +10,15 @@ use std::panic; use wasm_bindgen::prelude::*; #[derive(Deserialize, Serialize)] -struct ReqState { - request: lox::proto::open_invite::Request, - state: lox::proto::open_invite::State, +struct OpenReqState { + request: open_invite::Request, + state: open_invite::State, +} + +#[derive(Deserialize, Serialize)] +struct TrustReqState { + request: trust_promotion::Request, + state: trust_promotion::State, } #[derive(Debug, Deserialize, Serialize)] @@ -24,8 +32,8 @@ struct PubKeys { #[derive(Debug, Deserialize, Serialize)] struct Credential { - lox_credential: String, - bridgeline: String, + lox_credential: Lox, + bridgeline: BridgeLine, } #[wasm_bindgen] @@ -49,7 +57,7 @@ pub fn open_invite(invite: &[u8]) -> Result { Err(e) => return Err(JsValue::from(e.to_string())), }; let (request, state) = open_invite::request(&token); - let req_state = ReqState { + let req_state = OpenReqState { request: request, state: state, }; @@ -68,7 +76,7 @@ pub fn handle_new_lox_credential( open_lox_response: String, lox_pub: String, ) -> Result { - let req_state: ReqState = serde_json::from_str(&open_lox_result).unwrap(); + let req_state: OpenReqState = serde_json::from_str(&open_lox_result).unwrap(); let deserialized_state = req_state.state; let deserialized_response = serde_json::from_str(&open_lox_response).unwrap(); let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); @@ -84,20 +92,79 @@ pub fn handle_new_lox_credential( } }; let lox_cred = Credential { - lox_credential: serde_json::to_string(&lox_cred.0).unwrap(), - bridgeline: serde_json::to_string(&lox_cred.1).unwrap(), + lox_credential: lox_cred.0, + bridgeline: lox_cred.1, }; unsafe { log(&format!( "Got new Lox Credential: {}", - lox_cred.lox_credential + serde_json::to_string(&lox_cred.lox_credential).unwrap() )); - log(&format!("Got new bridgeline: {}", lox_cred.bridgeline)); + 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 { + 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(); + let req_state = TrustReqState { + request: request, + state: state, + }; + unsafe { + log(&format!( + "Formatted open invite request: {}", + serde_json::to_string(&req_state).unwrap() + )); + } + serde_json::to_string(&req_state).unwrap() +} + + +#[wasm_bindgen] +pub fn handle_trust_promotion( + trust_promo_request: String, + trust_promo_response: String, +) -> Result { + 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())); + } + }; + unsafe { + log(&format!( + "Got new Migration Credential: {}", + serde_json::to_string(&migration_cred).unwrap() + )); + } + 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() + } \ No newline at end of file From 0a29265c1ff885bd82e40902ce11c7484f079d2b Mon Sep 17 00:00:00 2001 From: onyinyang Date: Fri, 10 Feb 2023 16:18:54 -0500 Subject: [PATCH 22/34] Solve time issue, add time for testing --- crates/lox-wasm/Cargo.toml | 8 +++- crates/lox-wasm/src/lib.rs | 80 +++++++++++++++++++++++--------------- 2 files changed, 55 insertions(+), 33 deletions(-) diff --git a/crates/lox-wasm/Cargo.toml b/crates/lox-wasm/Cargo.toml index 7cf14be..c1d24a0 100644 --- a/crates/lox-wasm/Cargo.toml +++ b/crates/lox-wasm/Cargo.toml @@ -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"] diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 41f0dbe..e7be319 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -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 { 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 { 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() - } \ No newline at end of file From 56bc148db15aabc52080d21aaab6a6a4147ada58 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Mon, 13 Feb 2023 16:29:34 -0500 Subject: [PATCH 23/34] Update date to 1 past threshold for upgrade --- crates/lox-wasm/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index e7be319..2d78f70 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -130,12 +130,12 @@ pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> Result tp_result, Err(e) => { log(&format!("Error: {:?}", e.to_string())); From 944c3634ca516b0c6f3da88dc823f7c7831cb907 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Mon, 13 Feb 2023 23:58:07 -0500 Subject: [PATCH 24/34] Add Trust migration protocol --- crates/lox-wasm/index.js | 41 ++++++++-------- crates/lox-wasm/src/lib.rs | 95 ++++++++++++++++++++++++++++++++------ 2 files changed, 102 insertions(+), 34 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 3c70961..fa003d9 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -1,4 +1,11 @@ -import init, { open_invite, handle_new_lox_credential, trust_promotion, handle_trust_promotion, set_panic_hook } from "./pkg/lox_wasm.js"; +import init, { + open_invite, + handle_new_lox_credential, + trust_promotion, + handle_trust_promotion, + trust_migration, + handle_trust_migration, + set_panic_hook } from "./pkg/lox_wasm.js"; let pubkeys = await request_pubkeys(); let requested_invite = await init().then(() => { @@ -12,7 +19,7 @@ console.log("Got request and state: "+requested_invite); let open_lox_cred = await init().then(() => { set_panic_hook(); - let cred = request_new_lox_credential(requested_invite).then((response) => { + let cred = requested_cred("/openreq", requested_invite).then((response) => { return handle_new_lox_credential(requested_invite, response, pubkeys); }); return cred; @@ -22,33 +29,29 @@ let requested_trust_promo = trust_promotion(open_lox_cred, pubkeys); let trust_promo_cred = await init().then(() => { set_panic_hook(); - let cred = request_trust_promo_cred(requested_trust_promo).then((response)=> { + let cred = requested_cred("/trustpromo", requested_trust_promo).then((response)=> { return handle_trust_promotion(requested_trust_promo, response); }); return cred; }); - let reachability_cred = await request_reachability(); +let reachability_cred = await request_reachability(); -function request_new_lox_credential(requested_invite) { - return new Promise((fulfill, reject) => { - let req = JSON.parse(requested_invite); - loxServerPostRequest("/openreq", req.request).then((response) => { - console.log("Got new Open Invite Lox Credential: " + JSON.stringify(response)); - fulfill(JSON.stringify(response)); - return; - }).catch(() => { - console.log("Error requesting new Lox credential from server"); - reject(); - }); +let requested_trust_migration = trust_migration(open_lox_cred, trust_promo_cred, pubkeys); + +let level_1_cred = await init().then(() => { + set_panic_hook(); + let cred = requested_cred("/trustmig", requested_trust_migration).then((response)=> { + return handle_trust_migration(requested_trust_migration, response, pubkeys); + }); + return cred; }); -} -function request_trust_promo_cred(requested) { +function requested_cred(command, requested) { return new Promise((fulfill, reject) => { let req = JSON.parse(requested); - loxServerPostRequest("/trustpromo", req.request).then((response) => { - console.log("Got new Trust Promotion Lox Credential: " + JSON.stringify(response)); + loxServerPostRequest(command, req.request).then((response) => { + console.log("Got new Trust Migration Lox Credential: " + JSON.stringify(response)); fulfill(JSON.stringify(response)); return; }).catch(() => { diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 2d78f70..7308a1f 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -2,7 +2,8 @@ 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::proto::{open_invite, trust_promotion, migration, level_up, + issue_invite, redeem_invite, check_blockage, blockage_migration}; use lox::{IssuerPubKey, OPENINV_LENGTH}; use serde::{Deserialize, Serialize}; use serde_json; @@ -23,6 +24,13 @@ struct TrustReqState { state: trust_promotion::State, } +#[derive(Deserialize, Serialize)] +struct MigReqState { + request: migration::Request, + state: migration::State, +} + + #[derive(Debug, Deserialize, Serialize)] struct PubKeys { lox_pub: IssuerPubKey, @@ -38,14 +46,24 @@ struct Credential { bridgeline: BridgeLine, } + +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 +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] extern "C" { #[wasm_bindgen(js_namespace = console)] 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() { @@ -75,16 +93,6 @@ pub fn open_invite(invite: &[u8]) -> Result { 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, @@ -134,7 +142,7 @@ pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> Result tp_result, Err(e) => { @@ -148,7 +156,7 @@ pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> Result Result { + let lox_cred: Credential = serde_json::from_str(&open_lox_cred).unwrap(); + let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + let mig_cred = serde_json::from_str(&trust_promo_cred).unwrap(); + // To test creation of the credential we need to advance the day to 30 + // in production this should just use the today() function + let tm_result = + //CHANGE add_today(31) to today() for production + 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 { + 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(); + let level_1_cred = + 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: {}", + serde_json::to_string(&level_1_cred).unwrap() + )); + } + Ok(serde_json::to_string(&level_1_cred).unwrap()) +} + + // This should also check the pubkey fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> { invite.try_into() From f44ac0ae9f4219f99ad3d55d4b43ec9656e362be Mon Sep 17 00:00:00 2001 From: onyinyang Date: Tue, 14 Feb 2023 14:45:52 -0500 Subject: [PATCH 25/34] Refactor js to remove duplicate code --- crates/lox-wasm/index.js | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index fa003d9..7a5e8a1 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -6,7 +6,8 @@ import init, { trust_migration, handle_trust_migration, set_panic_hook } from "./pkg/lox_wasm.js"; -let pubkeys = await request_pubkeys(); +let pubkeys = await simple_request("/pubkeys"); +console.log("Got pubkeys: " + pubkeys); let requested_invite = await init().then(() => { set_panic_hook(); @@ -20,6 +21,7 @@ console.log("Got request and state: "+requested_invite); let open_lox_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/openreq", requested_invite).then((response) => { + console.log("Got new Level 0 Lox Credential: " + response); return handle_new_lox_credential(requested_invite, response, pubkeys); }); return cred; @@ -30,18 +32,21 @@ let requested_trust_promo = trust_promotion(open_lox_cred, pubkeys); let trust_promo_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/trustpromo", requested_trust_promo).then((response)=> { + console.log("Got Migration Credential for Trust Promotion: " + response); return handle_trust_promotion(requested_trust_promo, response); }); return cred; }); -let reachability_cred = await request_reachability(); +let reachability_cred = await simple_request("/reachability"); +console.log("Got reachability credential: " + reachability_cred); let requested_trust_migration = trust_migration(open_lox_cred, trust_promo_cred, pubkeys); let level_1_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/trustmig", requested_trust_migration).then((response)=> { + console.log("Got new Level 1 Lox Credential: " + response); return handle_trust_migration(requested_trust_migration, response, pubkeys); }); return cred; @@ -51,7 +56,6 @@ function requested_cred(command, requested) { return new Promise((fulfill, reject) => { let req = JSON.parse(requested); loxServerPostRequest(command, req.request).then((response) => { - console.log("Got new Trust Migration Lox Credential: " + JSON.stringify(response)); fulfill(JSON.stringify(response)); return; }).catch(() => { @@ -74,27 +78,13 @@ function request_open_invite() { }); } -function request_pubkeys() { +function simple_request(requested) { return new Promise((fulfill, reject) => { - loxServerGetRequest("/pubkeys").then((response) => { - console.log("Got pubkeys: " + JSON.stringify(response)); + loxServerGetRequest(requested).then((response) => { fulfill(JSON.stringify(response)); return; }).catch(() => { - console.log("Error requesting open invite from Lox server"); - reject(); - }); - }); -} - -function request_reachability() { - return new Promise((fulfill, reject) => { - loxServerGetRequest("/reachability").then((response) => { - console.log("Got reachability Cred: " + JSON.stringify(response)); - fulfill(JSON.stringify(response)); - return; - }).catch(() => { - console.log("Error requesting bucket reachability credential from Lox server"); + console.log("Error making simple request: " + requested); reject(); }); }); From d8cdf3ccab4cfc44069d2ba0a9db2ef3061c0ad0 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Tue, 14 Feb 2023 17:08:09 -0500 Subject: [PATCH 26/34] Logic for level up, reachability credential unimplemented --- crates/lox-wasm/index.js | 17 +++++++- crates/lox-wasm/src/lib.rs | 79 ++++++++++++++++++++++++++++++++++---- 2 files changed, 86 insertions(+), 10 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 7a5e8a1..423271b 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -5,6 +5,8 @@ import init, { handle_trust_promotion, trust_migration, handle_trust_migration, + level_up, + handle_level_up, set_panic_hook } from "./pkg/lox_wasm.js"; let pubkeys = await simple_request("/pubkeys"); console.log("Got pubkeys: " + pubkeys); @@ -38,12 +40,11 @@ let trust_promo_cred = await init().then(() => { return cred; }); -let reachability_cred = await simple_request("/reachability"); console.log("Got reachability credential: " + reachability_cred); let requested_trust_migration = trust_migration(open_lox_cred, trust_promo_cred, pubkeys); -let level_1_cred = await init().then(() => { +let level_one_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/trustmig", requested_trust_migration).then((response)=> { console.log("Got new Level 1 Lox Credential: " + response); @@ -52,6 +53,18 @@ let level_1_cred = await init().then(() => { return cred; }); +let reachability_cred = await simple_request("/reachability"); +let requested_level_two = level_up(level_one_cred, reachability_cred, pubkeys); + +let level_two_cred = await init().then(() => { + set_panic_hook(); + let cred = requested_cred("/levelup", requested_level_two).then((response)=> { + console.log("Got new Level 2 Lox Credential: " + response); + return handle_level_up(requested_level_two, 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 7308a1f..be96107 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -1,7 +1,7 @@ use chrono::{Duration, Utc}; use julianday::JulianDay; use lox::bridge_table::BridgeLine; -use lox::cred::Lox; +use lox::cred::{BucketReachability, Lox, Migration}; use lox::proto::{open_invite, trust_promotion, migration, level_up, issue_invite, redeem_invite, check_blockage, blockage_migration}; use lox::{IssuerPubKey, OPENINV_LENGTH}; @@ -30,6 +30,11 @@ struct MigReqState { state: migration::State, } +#[derive(Deserialize, Serialize)] +struct LevelupReqState { + request: level_up::Request, + state: level_up::State, +} #[derive(Debug, Deserialize, Serialize)] struct PubKeys { @@ -192,11 +197,8 @@ pub fn handle_trust_promotion( pub fn trust_migration(open_lox_cred: String, trust_promo_cred: String, lox_pub: String) -> Result { let lox_cred: Credential = serde_json::from_str(&open_lox_cred).unwrap(); let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); - let mig_cred = serde_json::from_str(&trust_promo_cred).unwrap(); - // To test creation of the credential we need to advance the day to 30 - // in production this should just use the today() function + let mig_cred: Migration = serde_json::from_str(&trust_promo_cred).unwrap(); let tm_result = - //CHANGE add_today(31) to today() for production match migration::request(&lox_cred.lox_credential, &mig_cred, &pubkeys.lox_pub, &pubkeys.migration_pub) { Ok(tm_result) => tm_result, Err(e) => { @@ -227,7 +229,7 @@ pub fn handle_trust_migration( 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(); - let level_1_cred = + let level_one_cred = match migration::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) { Ok(level_1_cred) => level_1_cred, Err(e) => { @@ -238,10 +240,71 @@ pub fn handle_trust_migration( unsafe { log(&format!( "Got new Level 1 Credential: {}", - serde_json::to_string(&level_1_cred).unwrap() + serde_json::to_string(&level_one_cred).unwrap() )); } - Ok(serde_json::to_string(&level_1_cred).unwrap()) + Ok(serde_json::to_string(&level_one_cred).unwrap()) +} + +#[wasm_bindgen] +pub fn level_up(level_one_cred: String, reachability_cred: 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 reach_cred: BucketReachability = serde_json::from_str(&reachability_cred).unwrap(); + // 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 + log(&format!( + "TEST ONLY: Add 85 days to today's date: {}", + add_today(85) + )); + let lu_result = + //CHANGE add_today(31) to today() for production + match level_up::request(&lox_cred, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, add_today(85)) { + 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()) +} + +#[wasm_bindgen] +pub fn handle_level_up( + levelup_request: String, + levelup_response: String, + lox_pub: String +) -> Result { + 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() + )); + } + Ok(serde_json::to_string(&level_up_cred).unwrap()) } From 01a6cb37e1693cf3a7bf2e6aa39463964e9b6c00 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Thu, 23 Feb 2023 13:18:11 -0500 Subject: [PATCH 27/34] Add working reachability cred, test credential aging needs work --- crates/lox-wasm/Cargo.toml | 2 ++ crates/lox-wasm/index.js | 6 +++--- crates/lox-wasm/src/lib.rs | 37 ++++++++++++++++++++++++++----------- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/crates/lox-wasm/Cargo.toml b/crates/lox-wasm/Cargo.toml index c1d24a0..93c7d3c 100644 --- a/crates/lox-wasm/Cargo.toml +++ b/crates/lox-wasm/Cargo.toml @@ -11,11 +11,13 @@ crate-type = ["cdylib"] [dependencies] julianday = "1.2.0" +lazy_static = "1.4.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_with = "1.9.1" serde-wasm-bindgen = "0.4.5" console_error_panic_hook = "0.1.7" diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 423271b..5755e3b 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -40,7 +40,6 @@ let trust_promo_cred = await init().then(() => { return cred; }); -console.log("Got reachability credential: " + reachability_cred); let requested_trust_migration = trust_migration(open_lox_cred, trust_promo_cred, pubkeys); @@ -53,8 +52,9 @@ let level_one_cred = await init().then(() => { return cred; }); -let reachability_cred = await simple_request("/reachability"); -let requested_level_two = level_up(level_one_cred, reachability_cred, pubkeys); +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); let level_two_cred = await init().then(() => { set_panic_hook(); diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index be96107..0bd94a3 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -1,17 +1,21 @@ -use chrono::{Duration, Utc}; +use chrono::{Duration, Utc, NaiveDate}; +use std::sync::atomic::{AtomicI64, Ordering}; use julianday::JulianDay; -use lox::bridge_table::BridgeLine; +use lazy_static::lazy_static; +use lox::bridge_table::{BridgeLine,from_scalar,BridgeTable, ENC_BUCKET_BYTES}; use lox::cred::{BucketReachability, Lox, Migration}; use lox::proto::{open_invite, trust_promotion, migration, level_up, issue_invite, redeem_invite, check_blockage, blockage_migration}; use lox::{IssuerPubKey, OPENINV_LENGTH}; use serde::{Deserialize, Serialize}; +use serde_with::{serde_as}; use serde_json; //use serde_wasm_bindgen; use std::array::TryFromSliceError; use std::{panic}; use wasm_bindgen::prelude::*; + #[derive(Deserialize, Serialize)] struct OpenReqState { request: open_invite::Request, @@ -45,6 +49,13 @@ struct PubKeys { invitation_pub: IssuerPubKey, } +#[serde_as] +#[derive(Serialize, Deserialize)] +pub struct EncBridgeTable { + #[serde_as(as = "Vec<[_; ENC_BUCKET_BYTES]>")] + pub etable: Vec<[u8; ENC_BUCKET_BYTES]>, +} + #[derive(Debug, Deserialize, Serialize)] struct Credential { lox_credential: Lox, @@ -58,8 +69,8 @@ fn today() -> u32 { } // This should only be used for testing, use today in production -fn add_today(sum: i64) -> u32 { - let naive_now_plus = (Utc::now() + Duration::days(sum)).date_naive(); +fn test_today(days: i64) -> u32 { + let naive_now_plus = (Utc::now() + Duration::days(days)).date_naive(); JulianDay::from(naive_now_plus).inner().try_into().unwrap() } @@ -144,11 +155,11 @@ pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> Result tp_result, Err(e) => { log(&format!("Error: {:?}", e.to_string())); @@ -247,20 +258,24 @@ pub fn handle_trust_migration( } #[wasm_bindgen] -pub fn level_up(level_one_cred: String, reachability_cred: String, lox_pub: String) -> Result { +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 reach_cred: BucketReachability = serde_json::from_str(&reachability_cred).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(); // 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 log(&format!( - "TEST ONLY: Add 85 days to today's date: {}", - add_today(85) + "TEST ONLY: Add 31 (open invitation) + x*85 days to today's date: {}", + test_today(31+85) )); let lu_result = //CHANGE add_today(31) to today() for production - match level_up::request(&lox_cred, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, add_today(85)) { + match level_up::request(&lox_cred, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, test_today(31+85)) { Ok(lu_result) => lu_result, Err(e) => { log(&format!("Error: {:?}", e.to_string())); From 66f7ae4c0dc914595953e8879ce81e9ba4cb20c9 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Thu, 23 Feb 2023 22:16:45 -0500 Subject: [PATCH 28/34] Add up to level up 4 --- crates/lox-wasm/Cargo.toml | 1 + crates/lox-wasm/index.js | 29 +++++++++++++++++++++++++++++ crates/lox-wasm/src/lib.rs | 36 ++++++++++++++++++++++++++++++++---- 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/crates/lox-wasm/Cargo.toml b/crates/lox-wasm/Cargo.toml index 93c7d3c..e5712e5 100644 --- a/crates/lox-wasm/Cargo.toml +++ b/crates/lox-wasm/Cargo.toml @@ -23,6 +23,7 @@ serde-wasm-bindgen = "0.4.5" console_error_panic_hook = "0.1.7" js-sys = "0.3.61" rand = { version = "0.7", features = ["wasm-bindgen"] } +zkp = "0.8.0" [dependencies.chrono] version = "0.4.19" diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 5755e3b..bfde485 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -65,6 +65,35 @@ let level_two_cred = await init().then(() => { return 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); + + let level_three_cred = await init().then(() => { + set_panic_hook(); + let cred = requested_cred("/levelup", requested_level_three).then((response)=> { + console.log("Got new Level 3 Lox Credential: " + response); + return handle_level_up(requested_level_three, response, pubkeys); + }); + return 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); + +let level_four_cred = await init().then(() => { + set_panic_hook(); + let cred = requested_cred("/levelup", requested_level_four).then((response)=> { + console.log("Got new Level 4 Lox Credential: " + response); + return handle_level_up(requested_level_four, 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 0bd94a3..4701b70 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -6,7 +6,7 @@ use lox::bridge_table::{BridgeLine,from_scalar,BridgeTable, ENC_BUCKET_BYTES}; use lox::cred::{BucketReachability, Lox, Migration}; use lox::proto::{open_invite, trust_promotion, migration, level_up, issue_invite, redeem_invite, check_blockage, blockage_migration}; -use lox::{IssuerPubKey, OPENINV_LENGTH}; +use lox::{IssuerPubKey, OPENINV_LENGTH, scalar_u32}; use serde::{Deserialize, Serialize}; use serde_with::{serde_as}; use serde_json; @@ -14,6 +14,7 @@ use serde_json; use std::array::TryFromSliceError; use std::{panic}; use wasm_bindgen::prelude::*; +use zkp::ProofError; #[derive(Deserialize, Serialize)] @@ -74,6 +75,23 @@ fn test_today(days: i64) -> u32 { JulianDay::from(naive_now_plus).inner().try_into().unwrap() } + +//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 { + 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) + } + #[wasm_bindgen] extern "C" { #[wasm_bindgen(js_namespace = console)] @@ -269,13 +287,22 @@ pub fn level_up(level_one_cred: String, encrypted_table: String, lox_pub: String // 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) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; + log(&format!( - "TEST ONLY: Add 31 (open invitation) + x*85 days to today's date: {}", - test_today(31+85) + "TEST ONLY: Add 31 (open invitation) + Trust Level*85 days to today's date: {}", test_today(test_cumulative_days) )); let lu_result = //CHANGE add_today(31) to today() for production - match level_up::request(&lox_cred, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, test_today(31+85)) { + match level_up::request(&lox_cred, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, test_today(test_cumulative_days)) { Ok(lu_result) => lu_result, Err(e) => { log(&format!("Error: {:?}", e.to_string())); @@ -295,6 +322,7 @@ pub fn level_up(level_one_cred: String, encrypted_table: String, lox_pub: String Ok(serde_json::to_string(&req_state).unwrap()) } + #[wasm_bindgen] pub fn handle_level_up( levelup_request: String, From 944cbb77e44d5358b86a0f2f752cd52d0fe2c0b5 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Fri, 24 Feb 2023 19:11:44 -0500 Subject: [PATCH 29/34] Add issue invitation --- crates/lox-wasm/index.js | 30 ++++++++++++++-- crates/lox-wasm/src/lib.rs | 74 +++++++++++++++++++++++++++++++++++--- 2 files changed, 97 insertions(+), 7 deletions(-) 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> { From 8faf4fc2736e2c39aa6d28aaeb25e9f8bcfd8485 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Mon, 27 Feb 2023 14:17:26 -0500 Subject: [PATCH 30/34] Add redeem invite --- crates/lox-wasm/index.js | 18 +++++++- crates/lox-wasm/src/lib.rs | 95 +++++++++++++++++++++++++++++++++++--- 2 files changed, 105 insertions(+), 8 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 99ac848..e380140 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -9,6 +9,9 @@ import init, { handle_level_up, issue_invite, handle_issue_invite, + prepare_invite, + redeem_invite, + handle_redeem_invite, set_panic_hook } from "./pkg/lox_wasm.js"; let pubkeys = await simple_request("/pubkeys"); console.log("Got pubkeys: " + pubkeys); @@ -109,7 +112,7 @@ 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 +// Issue an Invitation cred let issue_invite_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/issueinvite", requested_issue_invitation).then((response)=> { @@ -119,6 +122,19 @@ let issue_invite_cred = await init().then(() => { return cred; }); +let prepare_invitation = prepare_invite(issue_invite_cred); +// Trusted Invitation Request +let requested_invitation = redeem_invite(prepare_invitation, pubkeys); +// Issue an Invitation cred +let lox_cred_from_invite = await init().then(() => { + set_panic_hook(); + let cred = requested_cred("/redeem", requested_invitation).then((response)=> { + console.log("Got new Invite and Lox Credential: " + response); + return handle_redeem_invite(requested_invitation, response, pubkeys); + }); + return cred; + }); + function requested_cred(command, requested) { return new Promise((fulfill, reject) => { diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 4dfeeba..e838538 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -3,7 +3,7 @@ use std::sync::atomic::{AtomicI64, Ordering}; use julianday::JulianDay; use lazy_static::lazy_static; use lox::bridge_table::{BridgeLine,from_scalar,BridgeTable, ENC_BUCKET_BYTES}; -use lox::cred::{BucketReachability, Lox, Migration}; +use lox::cred::{BucketReachability, Invitation, Lox, Migration}; use lox::proto::{open_invite, trust_promotion, migration, level_up, issue_invite, redeem_invite, check_blockage, blockage_migration}; use lox::{IssuerPubKey, OPENINV_LENGTH, scalar_u32}; @@ -47,6 +47,12 @@ struct IssueInviteReqState { state: issue_invite::State, } +#[derive(Deserialize, Serialize)] +struct RedeemReqState { + request: redeem_invite::Request, + state: redeem_invite::State, +} + #[derive(Debug, Deserialize, Serialize)] struct PubKeys { lox_pub: IssuerPubKey, @@ -64,11 +70,16 @@ pub struct EncBridgeTable { } #[derive(Debug, Deserialize, Serialize)] -struct Credential { +struct OpenCredential { lox_credential: Lox, bridgeline: BridgeLine, } +#[derive(Debug, Deserialize, Serialize)] +struct InviteCredential { + lox_credential: Lox, + invitation: Invitation, +} fn today() -> u32 { let naive_now = Utc::now().date_naive(); @@ -154,7 +165,7 @@ pub fn handle_new_lox_credential( return Err(JsValue::from(e.to_string())); } }; - let lox_cred = Credential { + let lox_cred = OpenCredential { lox_credential: lox_cred.0, bridgeline: lox_cred.1, }; @@ -173,7 +184,7 @@ pub fn handle_new_lox_credential( #[wasm_bindgen] pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> Result { - let lox_cred: Credential = serde_json::from_str(&open_lox_cred).unwrap(); + let lox_cred: OpenCredential = serde_json::from_str(&open_lox_cred).unwrap(); let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); // To test creation of the credential we need to advance the day to 30 // in production this should just use the today() function @@ -230,7 +241,7 @@ pub fn handle_trust_promotion( #[wasm_bindgen] pub fn trust_migration(open_lox_cred: String, trust_promo_cred: String, lox_pub: String) -> Result { - let lox_cred: Credential = serde_json::from_str(&open_lox_cred).unwrap(); + let lox_cred: OpenCredential = serde_json::from_str(&open_lox_cred).unwrap(); let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); let mig_cred: Migration = serde_json::from_str(&trust_promo_cred).unwrap(); let tm_result = @@ -406,15 +417,85 @@ pub fn handle_issue_invite( return Err(JsValue::from(e.to_string())); } }; + let invitation_cred = InviteCredential { + lox_credential: issue_invite_cred.0, + invitation: issue_invite_cred.1, + }; + unsafe { log(&format!( "Got new Invitation Credential and Lox Credential: {}", - serde_json::to_string(&issue_invite_cred).unwrap() + serde_json::to_string(&invitation_cred).unwrap() )); } - Ok(serde_json::to_string(&issue_invite_cred).unwrap()) + Ok(serde_json::to_string(&invitation_cred).unwrap()) } +// Separate Trusted Invite from credential prior to passing it to friend +#[wasm_bindgen] +pub fn prepare_invite(invitation_cred: String) -> String { + let cred: InviteCredential = serde_json::from_str(&invitation_cred).unwrap(); + log(&format!( + "Prepared Invitation: {}", + serde_json::to_string(&cred.invitation).unwrap() + )); + serde_json::to_string(&cred.invitation).unwrap() +} + +// +#[wasm_bindgen] +pub fn redeem_invite(invitation: String, lox_pub: String) -> Result { + let invitation_cred: Invitation = serde_json::from_str(&invitation).unwrap(); + let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + let redeem_result = + match redeem_invite::request(&invitation_cred, &pubkeys.invitation_pub, test_today(371)) { + Ok(redeem_result) => redeem_result, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; + let req_state = RedeemReqState { + request: redeem_result.0, + state: redeem_result.1, + }; + unsafe { + log(&format!( + "Formatted Redeem Invite request: {}", + serde_json::to_string(&req_state).unwrap() + )); + } + Ok(serde_json::to_string(&req_state).unwrap()) +} + +#[wasm_bindgen] +pub fn handle_redeem_invite( + redeem_invite_request: String, + redeem_invite_response: String, + lox_pub: String +) -> Result { + let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + let req_state: RedeemReqState = serde_json::from_str(&redeem_invite_request).unwrap(); + let deserialized_state = req_state.state; + let deserialized_response = serde_json::from_str(&redeem_invite_response).unwrap(); + let redeem_invite_cred = + match redeem_invite::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_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 Trusted Lox Credential from Invitation: {}", + serde_json::to_string(&redeem_invite_cred).unwrap() + )); + } + Ok(serde_json::to_string(&redeem_invite_cred).unwrap()) +} + + // This should also check the pubkey fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> { invite.try_into() From 1bac11f1d0c86f6a1fda4db0a4c7afd4f441bd1f Mon Sep 17 00:00:00 2001 From: onyinyang Date: Mon, 27 Feb 2023 16:46:01 -0500 Subject: [PATCH 31/34] Refactor Lox Credential, Add check blockage --- crates/lox-wasm/index.js | 38 ++++++++---- crates/lox-wasm/src/lib.rs | 116 +++++++++++++++++++++++++++++-------- 2 files changed, 117 insertions(+), 37 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index e380140..73f9f4a 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -12,6 +12,8 @@ import init, { prepare_invite, redeem_invite, handle_redeem_invite, + check_blockage, + handle_check_blockage, set_panic_hook } from "./pkg/lox_wasm.js"; let pubkeys = await simple_request("/pubkeys"); console.log("Got pubkeys: " + pubkeys); @@ -53,7 +55,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(() => { +let lox_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/trustmig", requested_trust_migration).then((response)=> { console.log("Got new Level 1 Lox Credential: " + response); @@ -64,10 +66,10 @@ let level_one_cred = await init().then(() => { 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); +let requested_level_two = level_up(lox_cred, encrypted_table, pubkeys); // Level Up to Trust Level 2 -let level_two_cred = await init().then(() => { +lox_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/levelup", requested_level_two).then((response)=> { console.log("Got new Level 2 Lox Credential: " + response); @@ -79,10 +81,10 @@ let level_two_cred = await init().then(() => { // 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); + let requested_level_three = level_up(lox_cred, encrypted_table, pubkeys); // Level Up to Trust Level 3 - let level_three_cred = await init().then(() => { + lox_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/levelup", requested_level_three).then((response)=> { console.log("Got new Level 3 Lox Credential: " + response); @@ -95,10 +97,10 @@ let level_two_cred = await init().then(() => { // 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); +let requested_level_four = level_up(lox_cred, encrypted_table, pubkeys); // Level Up to Trust Level 4 -let level_four_cred = await init().then(() => { +lox_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/levelup", requested_level_four).then((response)=> { console.log("Got new Level 4 Lox Credential: " + response); @@ -110,10 +112,10 @@ let level_four_cred = await init().then(() => { // 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); +let requested_issue_invitation = issue_invite(lox_cred, encrypted_table, pubkeys); // Issue an Invitation cred -let issue_invite_cred = await init().then(() => { +lox_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); @@ -122,19 +124,31 @@ let issue_invite_cred = await init().then(() => { return cred; }); -let prepare_invitation = prepare_invite(issue_invite_cred); +let prepared_invitation = prepare_invite(lox_cred); // Trusted Invitation Request -let requested_invitation = redeem_invite(prepare_invitation, pubkeys); +let requested_invitation = redeem_invite(prepared_invitation, pubkeys); // Issue an Invitation cred let lox_cred_from_invite = await init().then(() => { set_panic_hook(); let cred = requested_cred("/redeem", requested_invitation).then((response)=> { - console.log("Got new Invite and Lox Credential: " + response); + console.log("Got new Trusted Lox Credential Invite: " + response); return handle_redeem_invite(requested_invitation, response, pubkeys); }); return cred; }); + let requested_check_blockage = check_blockage(lox_cred, pubkeys); + + // Check whether or not a bucket is blocked + let check_cred = await init().then(() => { + set_panic_hook(); + let cred = requested_cred("/checkblockage", requested_check_blockage).then((response)=> { + console.log("Got check blockage Migration Credential: " + response); + return handle_check_blockage(requested_check_blockage, response); + }); + return cred; + }); + function requested_cred(command, requested) { return new Promise((fulfill, reject) => { diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index e838538..a75b428 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -53,6 +53,13 @@ struct RedeemReqState { state: redeem_invite::State, } +#[derive(Deserialize, Serialize)] +struct CheckBlockageReqState { + request: check_blockage::Request, + state: check_blockage::State, +} + + #[derive(Debug, Deserialize, Serialize)] struct PubKeys { lox_pub: IssuerPubKey, @@ -70,15 +77,10 @@ pub struct EncBridgeTable { } #[derive(Debug, Deserialize, Serialize)] -struct OpenCredential { +struct LoxCredential { lox_credential: Lox, - bridgeline: BridgeLine, -} - -#[derive(Debug, Deserialize, Serialize)] -struct InviteCredential { - lox_credential: Lox, - invitation: Invitation, + bridgeline: Option, + invitation: Option, } fn today() -> u32 { @@ -165,9 +167,10 @@ pub fn handle_new_lox_credential( return Err(JsValue::from(e.to_string())); } }; - let lox_cred = OpenCredential { + let lox_cred = LoxCredential { lox_credential: lox_cred.0, - bridgeline: lox_cred.1, + bridgeline: Some(lox_cred.1), + invitation: None, }; unsafe { log(&format!( @@ -184,7 +187,7 @@ pub fn handle_new_lox_credential( #[wasm_bindgen] pub fn trust_promotion(open_lox_cred: String, lox_pub: String) -> Result { - let lox_cred: OpenCredential = serde_json::from_str(&open_lox_cred).unwrap(); + let lox_cred: LoxCredential = serde_json::from_str(&open_lox_cred).unwrap(); let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); // To test creation of the credential we need to advance the day to 30 // in production this should just use the today() function @@ -241,7 +244,7 @@ pub fn handle_trust_promotion( #[wasm_bindgen] pub fn trust_migration(open_lox_cred: String, trust_promo_cred: String, lox_pub: String) -> Result { - let lox_cred: OpenCredential = serde_json::from_str(&open_lox_cred).unwrap(); + let lox_cred: LoxCredential = serde_json::from_str(&open_lox_cred).unwrap(); let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); let mig_cred: Migration = serde_json::from_str(&trust_promo_cred).unwrap(); let tm_result = @@ -277,7 +280,11 @@ pub fn handle_trust_migration( let deserialized_response = serde_json::from_str(&trust_migration_response).unwrap(); let level_one_cred = match migration::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) { - Ok(level_1_cred) => level_1_cred, + Ok(level_1_cred) => LoxCredential { + lox_credential: level_1_cred, + bridgeline: None, + invitation: None, + }, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); @@ -301,16 +308,16 @@ fn generate_reachability_cred(lox_cred: &Lox, encrypted_table: String) -> Bucket #[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 lox_cred: LoxCredential = serde_json::from_str(&level_one_cred).unwrap(); let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); - let reach_cred = generate_reachability_cred(&lox_cred, encrypted_table); + let reach_cred = generate_reachability_cred(&lox_cred.lox_credential, 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) { + let test_cumulative_days = match calc_test_days(&lox_cred.lox_credential) { Ok(v) => v, Err(e) => { log(&format!("Error: {:?}", e.to_string())); @@ -323,7 +330,7 @@ pub fn level_up(level_one_cred: String, encrypted_table: String, lox_pub: String )); let lu_result = //CHANGE add_today(31) to today() for production - match level_up::request(&lox_cred, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, test_today(test_cumulative_days)) { + match level_up::request(&lox_cred.lox_credential, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, test_today(test_cumulative_days)) { Ok(lu_result) => lu_result, Err(e) => { log(&format!("Error: {:?}", e.to_string())); @@ -356,7 +363,11 @@ pub fn handle_level_up( 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, + Ok(level_up_cred) => LoxCredential { + lox_credential: level_up_cred, + bridgeline: None, + invitation: None, + }, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); @@ -373,12 +384,12 @@ pub fn handle_level_up( #[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 lox_cred: LoxCredential = 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 reach_cred = generate_reachability_cred(&lox_cred.lox_credential, encrypted_table); let issue_result = - match issue_invite::request(&lox_cred, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, test_today(371)) { + match issue_invite::request(&lox_cred.lox_credential, &reach_cred, &pubkeys.lox_pub, &pubkeys.reachability_pub, test_today(371)) { Ok(issue_result) => issue_result, Err(e) => { log(&format!("Error: {:?}", e.to_string())); @@ -417,9 +428,10 @@ pub fn handle_issue_invite( return Err(JsValue::from(e.to_string())); } }; - let invitation_cred = InviteCredential { + let invitation_cred = LoxCredential { lox_credential: issue_invite_cred.0, - invitation: issue_invite_cred.1, + bridgeline: None, + invitation: Some(issue_invite_cred.1), }; unsafe { @@ -434,7 +446,7 @@ pub fn handle_issue_invite( // Separate Trusted Invite from credential prior to passing it to friend #[wasm_bindgen] pub fn prepare_invite(invitation_cred: String) -> String { - let cred: InviteCredential = serde_json::from_str(&invitation_cred).unwrap(); + let cred: LoxCredential = serde_json::from_str(&invitation_cred).unwrap(); log(&format!( "Prepared Invitation: {}", serde_json::to_string(&cred.invitation).unwrap() @@ -480,7 +492,11 @@ pub fn handle_redeem_invite( let deserialized_response = serde_json::from_str(&redeem_invite_response).unwrap(); let redeem_invite_cred = match redeem_invite::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) { - Ok(issue_invite_cred) => issue_invite_cred, + Ok(issue_invite_cred) => LoxCredential { + lox_credential: issue_invite_cred, + bridgeline: None, + invitation: None, + }, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); @@ -496,6 +512,56 @@ pub fn handle_redeem_invite( } +#[wasm_bindgen] +pub fn check_blockage(lox_cred: String, lox_pub: String) -> Result { + let lox: LoxCredential = serde_json::from_str(&lox_cred).unwrap(); + let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + let cb_result = + match check_blockage::request(&lox.lox_credential, &pubkeys.lox_pub) { + Ok(cb_result) => cb_result, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; + let req_state = CheckBlockageReqState { + request: cb_result.0, + state: cb_result.1, + }; + unsafe { + log(&format!( + "Formatted Trust Promotion request: {}", + serde_json::to_string(&req_state).unwrap() + )); + } + Ok(serde_json::to_string(&req_state).unwrap()) +} + +#[wasm_bindgen] +pub fn handle_check_blockage( + check_blockage_request: String, + check_blockage_response: String, +) -> Result { + let req_state: CheckBlockageReqState = serde_json::from_str(&check_blockage_request).unwrap(); + let deserialized_state = req_state.state; + let deserialized_response = serde_json::from_str(&check_blockage_response).unwrap(); + let migration_cred = + match check_blockage::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: {}", + serde_json::to_string(&migration_cred).unwrap() + )); + } + 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() From dd00f99958d80f12b05cf18474d2c54aa264d557 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Mon, 27 Feb 2023 16:57:23 -0500 Subject: [PATCH 32/34] Add blockage migration (untested) --- crates/lox-wasm/index.js | 16 ++++++++++- crates/lox-wasm/src/lib.rs | 58 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 73f9f4a..ab3045f 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -14,6 +14,8 @@ import init, { handle_redeem_invite, check_blockage, handle_check_blockage, + blockage_migration, + handle_blockage_migration, set_panic_hook } from "./pkg/lox_wasm.js"; let pubkeys = await simple_request("/pubkeys"); console.log("Got pubkeys: " + pubkeys); @@ -140,7 +142,7 @@ let lox_cred_from_invite = await init().then(() => { let requested_check_blockage = check_blockage(lox_cred, pubkeys); // Check whether or not a bucket is blocked - let check_cred = await init().then(() => { + let check_migration_cred = await init().then(() => { set_panic_hook(); let cred = requested_cred("/checkblockage", requested_check_blockage).then((response)=> { console.log("Got check blockage Migration Credential: " + response); @@ -149,6 +151,18 @@ let lox_cred_from_invite = await init().then(() => { return cred; }); +let requested_blockage_migration = blockage_migration(lox_cred, check_migration_cred, pubkeys); + + // Migrate to a new unblocked bridge + lox_cred = await init().then(() => { + set_panic_hook(); + let cred = requested_cred("/blockagemigration", requested_blockage_migration).then((response)=> { + console.log("Got Lox Credential for new bucket: " + response); + return handle_blockage_migration(requested_check_blockage, response); + }); + return cred; + }); + function requested_cred(command, requested) { return new Promise((fulfill, reject) => { diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index a75b428..e7547d8 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -562,6 +562,64 @@ pub fn handle_check_blockage( Ok(serde_json::to_string(&migration_cred).unwrap()) } + +#[wasm_bindgen] +pub fn blockage_migration(lox_cred: String, check_migration_cred: String, lox_pub: String) -> Result { + let lox_cred: LoxCredential = serde_json::from_str(&lox_cred).unwrap(); + let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + let mig_cred: Migration = serde_json::from_str(&check_migration_cred).unwrap(); + let bm_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: bm_result.0, + state: bm_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_blockage_migration( + blockage_migration_request: String, + blockage_migration_response: String, + lox_pub: String +) -> Result { + let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); + let req_state: MigReqState = serde_json::from_str(&blockage_migration_request).unwrap(); + let deserialized_state = req_state.state; + let deserialized_response = serde_json::from_str(&blockage_migration_response).unwrap(); + let lox_cred = + match migration::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) { + Ok(lox_cred) => LoxCredential { + lox_credential: lox_cred, + bridgeline: None, + invitation: None, + }, + Err(e) => { + log(&format!("Error: {:?}", e.to_string())); + return Err(JsValue::from(e.to_string())); + } + }; + unsafe { + log(&format!( + "Got new Level 1 Credential: {}", + serde_json::to_string(&lox_cred).unwrap() + )); + } + Ok(serde_json::to_string(&lox_cred).unwrap()) +} + // This should also check the pubkey fn validate(invite: &[u8]) -> Result<[u8; OPENINV_LENGTH], TryFromSliceError> { invite.try_into() From a9de52a266d8c6b3cea085bd8030d9e5b2b80fbe Mon Sep 17 00:00:00 2001 From: onyinyang Date: Tue, 28 Feb 2023 12:57:57 -0500 Subject: [PATCH 33/34] Blockage Migration tested and fixed up --- crates/lox-wasm/index.js | 4 ++-- crates/lox-wasm/src/lib.rs | 25 ++++++++++++++++--------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index ab3045f..89e83ef 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -129,7 +129,7 @@ lox_cred = await init().then(() => { let prepared_invitation = prepare_invite(lox_cred); // Trusted Invitation Request let requested_invitation = redeem_invite(prepared_invitation, pubkeys); -// Issue an Invitation cred +// Redeem an Invitation cred let lox_cred_from_invite = await init().then(() => { set_panic_hook(); let cred = requested_cred("/redeem", requested_invitation).then((response)=> { @@ -158,7 +158,7 @@ let requested_blockage_migration = blockage_migration(lox_cred, check_migration_ set_panic_hook(); let cred = requested_cred("/blockagemigration", requested_blockage_migration).then((response)=> { console.log("Got Lox Credential for new bucket: " + response); - return handle_blockage_migration(requested_check_blockage, response); + return handle_blockage_migration(requested_blockage_migration, response, pubkeys); }); return cred; }); diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index e7547d8..74bd687 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -59,6 +59,13 @@ struct CheckBlockageReqState { state: check_blockage::State, } +#[derive(Deserialize, Serialize)] +struct BlockageMigReqState { + request: blockage_migration::Request, + state: blockage_migration::State, +} + + #[derive(Debug, Deserialize, Serialize)] struct PubKeys { @@ -530,7 +537,7 @@ pub fn check_blockage(lox_cred: String, lox_pub: String) -> Result tm_result, + match blockage_migration::request(&lox_cred.lox_credential, &mig_cred, &pubkeys.lox_pub, &pubkeys.migration_pub) { + Ok(bm_result) => bm_result, Err(e) => { log(&format!("Error: {:?}", e.to_string())); return Err(JsValue::from(e.to_string())); } }; - let req_state = MigReqState { + let req_state = BlockageMigReqState { request: bm_result.0, state: bm_result.1, }; unsafe { log(&format!( - "Formatted Trust Migration request: {}", + "Formatted Blockage Migration request: {}", serde_json::to_string(&req_state).unwrap() )); } @@ -596,11 +603,11 @@ pub fn handle_blockage_migration( lox_pub: String ) -> Result { let pubkeys: PubKeys = serde_json::from_str(&lox_pub).unwrap(); - let req_state: MigReqState = serde_json::from_str(&blockage_migration_request).unwrap(); + let req_state: BlockageMigReqState = serde_json::from_str(&blockage_migration_request).unwrap(); let deserialized_state = req_state.state; let deserialized_response = serde_json::from_str(&blockage_migration_response).unwrap(); let lox_cred = - match migration::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) { + match blockage_migration::handle_response(deserialized_state, deserialized_response, &pubkeys.lox_pub) { Ok(lox_cred) => LoxCredential { lox_credential: lox_cred, bridgeline: None, @@ -613,7 +620,7 @@ pub fn handle_blockage_migration( }; unsafe { log(&format!( - "Got new Level 1 Credential: {}", + "Got new Lox Credential after Migration: {}", serde_json::to_string(&lox_cred).unwrap() )); } From b0038720e0be12aba7ac0de2bc23a594bed1a6ea Mon Sep 17 00:00:00 2001 From: onyinyang Date: Fri, 24 Mar 2023 13:02:42 -0400 Subject: [PATCH 34/34] Make all requests POST requests to match server and browser handling --- crates/lox-wasm/index.js | 32 ++------------------------------ crates/lox-wasm/src/lib.rs | 12 ++++-------- 2 files changed, 6 insertions(+), 38 deletions(-) diff --git a/crates/lox-wasm/index.js b/crates/lox-wasm/index.js index 89e83ef..ad303ca 100644 --- a/crates/lox-wasm/index.js +++ b/crates/lox-wasm/index.js @@ -179,7 +179,7 @@ function requested_cred(command, requested) { function request_open_invite() { return new Promise((fulfill, reject) => { - loxServerGetRequest("/invite").then((response) => { + loxServerPostRequest("/invite", null).then((response) => { console.log("Got invitation token: " + response.invite); fulfill(response.invite); return; @@ -192,7 +192,7 @@ function request_open_invite() { function simple_request(requested) { return new Promise((fulfill, reject) => { - loxServerGetRequest(requested).then((response) => { + loxServerPostRequest(requested, null).then((response) => { fulfill(JSON.stringify(response)); return; }).catch(() => { @@ -202,34 +202,6 @@ function simple_request(requested) { }); } -function loxServerGetRequest(data) { - return new Promise((fulfill, reject) => { - const xhr = new XMLHttpRequest(); - xhr.onreadystatechange = function() { - if (xhr.DONE !== xhr.readyState) { - return; - } - if (xhr.status !== 200) { - console.log("Error. Status code: "+xhr.status); - console.log(xhr); - reject(); - return; - } - const response = JSON.parse(xhr.responseText); - fulfill(response); - return; - }; - try { - xhr.open('GET', "http://localhost:8001"+data); - } catch (err) { - console.log("Error connecting to lox bridge db"); - reject(); - return; - } - xhr.send(); - }); -} - function loxServerPostRequest(data, payload) { return new Promise((fulfill, reject) => { const xhr = new XMLHttpRequest(); diff --git a/crates/lox-wasm/src/lib.rs b/crates/lox-wasm/src/lib.rs index 74bd687..7db3092 100644 --- a/crates/lox-wasm/src/lib.rs +++ b/crates/lox-wasm/src/lib.rs @@ -1,7 +1,5 @@ -use chrono::{Duration, Utc, NaiveDate}; -use std::sync::atomic::{AtomicI64, Ordering}; +use chrono::{Duration, Utc}; use julianday::JulianDay; -use lazy_static::lazy_static; use lox::bridge_table::{BridgeLine,from_scalar,BridgeTable, ENC_BUCKET_BYTES}; use lox::cred::{BucketReachability, Invitation, Lox, Migration}; use lox::proto::{open_invite, trust_promotion, migration, level_up, @@ -9,8 +7,6 @@ use lox::proto::{open_invite, trust_promotion, migration, level_up, use lox::{IssuerPubKey, OPENINV_LENGTH, scalar_u32}; use serde::{Deserialize, Serialize}; use serde_with::{serde_as}; -use serde_json; -//use serde_wasm_bindgen; use std::array::TryFromSliceError; use std::{panic}; use wasm_bindgen::prelude::*; @@ -141,8 +137,8 @@ pub fn open_invite(invite: &[u8]) -> Result { }; let (request, state) = open_invite::request(&token); let req_state = OpenReqState { - request: request, - state: state, + request, + state, }; unsafe { log(&format!( @@ -310,7 +306,7 @@ fn generate_reachability_cred(lox_cred: &Lox, encrypted_table: String) -> Bucket 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() + bucket.1.unwrap() } #[wasm_bindgen]