Skip to content

Commit

Permalink
feat: add WebAssembly support (xJonathanLEI#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
xJonathanLEI authored Mar 9, 2022
1 parent 6f515af commit d7a9e8a
Show file tree
Hide file tree
Showing 21 changed files with 1,294 additions and 34 deletions.
45 changes: 33 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ members = [
"starknet-signers",
"starknet-accounts",
"starknet-ff",
"examples/starknet-wasm",
]

[dependencies]
Expand Down
3 changes: 3 additions & 0 deletions examples/starknet-wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/node_modules/
/pkg/
/dist/
25 changes: 25 additions & 0 deletions examples/starknet-wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "starknet-wasm"
version = "0.1.0"
authors = ["Jonathan LEI <[email protected]>"]
license = "MIT OR Apache-2.0"
edition = "2021"
readme = "README.md"
repository = "https://github.com/xJonathanLEI/starknet-rs"
homepage = "https://starknet.rs/"
description = """
Example usage of starknet-rs from WASM
"""
keywords = ["ethereum", "starknet", "web3"]

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

[features]
default = ["console_error_panic_hook"]

[dependencies]
starknet-ff = { version = "0.1.0", path = "../../starknet-ff" }
starknet-crypto = { version = "0.1.0", path = "../../starknet-crypto" }
console_error_panic_hook = { version = "0.1.7", optional = true }
wasm-bindgen = "0.2.79"
1 change: 1 addition & 0 deletions examples/starknet-wasm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Example usage of starknet-rs from WASM
10 changes: 10 additions & 0 deletions examples/starknet-wasm/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>starknet-rs WASM example</title>
</head>
<body>
<script src="index.js"></script>
<h1>starknet WASM</h1>
</body>
</html>
20 changes: 20 additions & 0 deletions examples/starknet-wasm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const starknet = import("./pkg");

starknet
.then((m) => {
const privateKey =
"0x03c1e9550e66958296d11b60f8e8e7a7ad990d07fa65d5f7652c4a6c87d4e3cc";
console.log("Private Key:", privateKey);

const publicKey = m.get_public_key(privateKey);

if (
publicKey !==
"0x077a3b314db07c45076d11f62b6f9e748a39790441823307743cf00d6597ea43"
) {
throw new Error("Unexpected public key");
} else {
console.log("Public Key:", publicKey);
}
})
.catch(console.error);
13 changes: 13 additions & 0 deletions examples/starknet-wasm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "starknet-wasm",
"license": "MIT OR Apache-2.0",
"scripts": {
"build": "webpack"
},
"devDependencies": {
"@wasm-tool/wasm-pack-plugin": "^1.6.0",
"html-webpack-plugin": "^5.5.0",
"webpack": "^5.70.0",
"webpack-cli": "^4.9.2"
}
}
15 changes: 15 additions & 0 deletions examples/starknet-wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#![allow(clippy::unused_unit)]

use starknet_crypto::FieldElement;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn get_public_key(private_key_hex: &str) -> String {
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();

let private_key = FieldElement::from_hex_be(private_key_hex).unwrap();
let public_key = starknet_crypto::get_public_key(&private_key);

format!("{:#064x}", public_key)
}
25 changes: 25 additions & 0 deletions examples/starknet-wasm/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const webpack = require("webpack");
const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin");

module.exports = {
entry: "./index.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "index.js",
},
plugins: [
new HtmlWebpackPlugin({
template: "./index.html",
inject: false,
}),
new WasmPackPlugin({
crateDirectory: path.resolve(__dirname, "."),
}),
],
mode: "development",
experiments: {
asyncWebAssembly: true,
},
};
Loading

0 comments on commit d7a9e8a

Please sign in to comment.