forked from xJonathanLEI/starknet-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add WebAssembly support (xJonathanLEI#81)
- Loading branch information
1 parent
6f515af
commit d7a9e8a
Showing
21 changed files
with
1,294 additions
and
34 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/node_modules/ | ||
/pkg/ | ||
/dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Example usage of starknet-rs from WASM |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}; |
Oops, something went wrong.