This commit is contained in:
poma 2020-04-23 07:43:45 +03:00
commit 782d425e24
No known key found for this signature in database
GPG Key ID: BA20CB01FE165657
10 changed files with 5981 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
node_modules
/dist
/target
/pkg
/wasm-pack.log

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "phase2-bn254"]
path = phase2-bn254
url = https://github.com/kobigurk/phase2-bn254

34
README.md Normal file
View File

@ -0,0 +1,34 @@
# phase2-wasm
This demo generates contributions for phase 2 of trusted setup MPC in a browser using WebAssembly
## How to install
```sh
git clone --recursive https://github.com/tornadocash/phase2-wasm
npm install
```
## How to run in debug mode
```sh
# Builds the project and opens it in a new browser tab. Auto-reloads when the project changes.
npm start
```
## How to build in release mode
```sh
# Builds the project and places it into the `dist` folder.
npm run build
```
## Project structure
* `webpack.config.js` config that is used to build .wasm and other project files
* `phase2-bn254/phase2` trusted setup crate, we build .wasm module from it
* `js/index.js` main frontend script that calls .wasm to generate the contribution
* `static/index.html` empty index file that just includes .js
* `static/params.bin` example previous contribution
This example uses static previous contribution file and outputs new contribution to console. On prod this should be handled by the server.

16
js/index.js Normal file
View File

@ -0,0 +1,16 @@
import("../pkg/index.js").catch(console.error);
async function main() {
const pkg = await import("../pkg/index.js")
console.log('Downloading previous contribution from /params.bin')
let params = await fetch('params.bin')
params = new Uint8Array(await params.arrayBuffer())
const entropy = new Uint8Array(32)
window.crypto.getRandomValues(entropy)
console.log('Contributing with entropy', entropy) // shouldn't be logged on prod
const result = pkg.contribute(params, entropy)
console.log('Your contribution', result)
console.log('Contribution raw data:', '0x' + Buffer.from(result).toString('hex'))
}
main()

5862
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

18
package.json Normal file
View File

@ -0,0 +1,18 @@
{
"author": "You <you@example.com>",
"name": "rust-webpack-template",
"version": "0.1.0",
"scripts": {
"build": "rimraf dist pkg && webpack",
"start": "rimraf dist pkg && webpack-dev-server --open -d",
"test": "cargo test && wasm-pack test --headless"
},
"devDependencies": {
"@wasm-tool/wasm-pack-plugin": "^1.1.0",
"copy-webpack-plugin": "^5.0.3",
"webpack": "^4.42.0",
"webpack-cli": "^3.3.3",
"webpack-dev-server": "^3.7.1",
"rimraf": "^3.0.0"
}
}

1
phase2-bn254 Submodule

@ -0,0 +1 @@
Subproject commit 23338795bfbc598aec99de1f35fda1d0af559eee

11
static/index.html Normal file
View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Trusted setup demo</title>
</head>
<body>
<h1>Please check the console</h1>
<script src="index.js"></script>
</body>
</html>

BIN
static/params.bin Normal file

Binary file not shown.

31
webpack.config.js Normal file
View File

@ -0,0 +1,31 @@
const path = require("path");
const CopyPlugin = require("copy-webpack-plugin");
const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin");
const dist = path.resolve(__dirname, "dist");
module.exports = {
mode: "production",
entry: {
index: "./js/index.js"
},
output: {
path: dist,
filename: "[name].js"
},
devServer: {
contentBase: dist,
},
plugins: [
new CopyPlugin([
path.resolve(__dirname, "static")
]),
new WasmPackPlugin({
crateDirectory: path.resolve(__dirname, 'phase2-bn254', 'phase2'),
outDir: path.resolve(__dirname, "pkg"),
extraArgs: "--target browser --mode normal -- --no-default-features --features wasm",
forceMode: "production"
}),
]
};