Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DO NOT MERGE] Error when loading wasm #1

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/target
**/*.rs.bk
Cargo.lock
bin/
pkg/
wasm-pack.log
**/node_modules
76 changes: 76 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
[package]
name = "tlsn-extension-rs"
version = "0.1.0"
authors = ["The tlsn-extension Developers"]
edition = "2018"
rust-version = "1.56"

[lib]
crate-type = ["cdylib"]

[dependencies]
# rayon = "1.5"
# wasm-bindgen-rayon = "1.0"
wasm-bindgen = "0.2.74"
js-sys = "0.3"
tracing = "0.1"
tlsn-prover = { path = "../tlsn/tlsn/tlsn-prover", features = ["tracing"] }
getrandom = { version = "0.2", features = ["js"] }
ws_stream_wasm = "0.7.4"
wasm-bindgen-futures = "0.4.37"
tokio-util = "0.7"
futures = "0.3"
serde_json = "1.0"
futures-util = "0.3.28"

hyper = { version = "0.14", features = ["client", "http1"] }
console_error_panic_hook = "0.1.7"
tracing-web = "0.1.2"
tracing-subscriber = { version = "0.3", features = ["time"] }

# time crate: https://crates.io/crates/time
# NOTE: It is required, otherwise "time not implemented on this platform" error happens right after "!@# 2".
# Probably due to tokio's time feature is used in tlsn-prover?
time = { version = "0.3", features = ["wasm-bindgen"] }
# Used to calculate elapsed time.
web-time = "0.2"

[dependencies.web-sys]
version = "0.3.22"
features = [
"BinaryType",
"Blob",
"ErrorEvent",
"FileReader",
"MessageEvent",
"ProgressEvent",
"WebSocket",
"console",
'Document',
'HtmlElement',
'HtmlInputElement',
'Window',
'Worker',
]

# Replace ring with the forked version `ring-xous` implemented in pure rust
# to make it compiled to wasm.
# Refs:
# - Rationale for `ring-xous`: https://www.bunniestudios.com/blog/?p=6521
# - Issue for wasm comptability: https://github.com/briansmith/ring/issues/918
[patch.crates-io.ring]
git="https://github.com/betrusted-io/ring-xous"
branch="0.16.20-cleanup"


# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.7", optional = true }

[dev-dependencies]
wasm-bindgen-test = "0.3.34"

[profile.release]
# Tell `rustc` to optimize for small code size.
64 changes: 63 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,63 @@
# tlsn-extension
# tlsn-extension 🚧🚧🚧

## Bind a rust websocket client
We used the example from [wasm-bindgen](https://rustwasm.github.io/docs/wasm-bindgen/examples/websockets.html) with the following changes:
1. Use webpack to bundle the javascript
2. Add a websocket server to listen to the messages from the browser


### Install `wasm-pack`
Do it with `yarn`, or you can install it in [other ways](https://rustwasm.github.io/wasm-pack/installer/).

```bash
yarn global add wasm-pack
```

### Compile rust to wasm and create a JS package binding

```bash
yarn build-rs
```

You can see the resulting JS package in `pkg/`.

```bash
tree pkg
pkg
├── README.md
├── package.json
├── tlsn_extension_rs.d.ts
├── tlsn_extension_rs.js
├── tlsn_extension_rs_bg.wasm
└── tlsn_extension_rs_bg.wasm.d.ts
```

### Install dependencies

```bash
yarn install
```

### Run the websocket server in another terminal
Open a new terminal and run the websocket server
```bash
yarn ws-server
```

### Run the web server
```bash
yarn start
```

### Open the browser
```bash
open http://localhost:8080
```

And you should see outputs from the browser console:
```
socket opened
tlsn_extension_rs.js:278 message successfully sent
tlsn_extension_rs.js:278 binary message successfully sent
tlsn_extension_rs.js:278 message event, received Text: "something"
```
5 changes: 5 additions & 0 deletions bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// A dependency graph that contains any wasm must all be imported
// asynchronously. This `bootstrap.js` file does the single async import, so
// that no one else needs to worry about it again.
import("./index.js")
.catch(e => console.error("Error importing `index.js`:", e));
10 changes: 10 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebSockets example</title>
<script src="bootstrap.js"></script>
</head>
<body>
</body>
</html>
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// import init from './pkg/tlsn_extension_rs.js';
import * as Comlink from "comlink";


window.addEventListener('load', async () => {
console.log("!@# load: addEventListener");
const Wasm = Comlink.wrap(new Worker(new URL("./worker.js", import.meta.url)));
const instance = await new Wasm();
console.log("!@# instance = ", instance);

// // Regular wasm-bindgen initialization.
// // await init('./pkg/tlsn_extension_rs_bg.wasm');
// await init();
});
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "tlsn-extension",
"version": "0.0.1",
"description": "",
"main": "index.js",
"scripts": {
"build-rs": "wasm-pack build --target web",
"build": "webpack --config webpack.config.js",
"start": "webpack-dev-server",
"build-and-start": "yarn build-rs && yarn install && yarn start",
"clean": "rm -rf pkg/ node_modules/ dist/ yarn.lock"
},
"author": "",
"license": "ISC",
"dependencies": {
"comlink": "^4.4.1",
"tlsn-extension-rs": "file:./pkg"
},
"devDependencies": {
"copy-webpack-plugin": "^11.0.0",
"webpack": "^5.88.1",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^4.15.1"
}
}
Loading