Skip to content

Commit

Permalink
Add wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid committed Mar 1, 2021
1 parent af8e662 commit 0d281c1
Show file tree
Hide file tree
Showing 6 changed files with 334 additions and 2 deletions.
191 changes: 190 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["secp256k1"]
members = ["secp256k1", "secp256k1-wasm"]

[profile.release]
lto = true
Expand Down
19 changes: 19 additions & 0 deletions secp256k1-wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "secp256k1-wasm"
version = "0.0.0"
authors = []
edition = "2018"
description = "A Rust library for building tiny-secp256k1 WASM"
license = "MIT"
publish = false

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

[dependencies]
secp256k1 = { path = "../secp256k1", features = ["wasm"] }
wasm-bindgen = "0.2.71"
wee_alloc = "0.4.5"

[target.'cfg(debug)'.dependencies]
console_error_panic_hook = "0.1.6"
98 changes: 98 additions & 0 deletions secp256k1-wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use wasm_bindgen::prelude::{wasm_bindgen, JsValue};

#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

#[cfg(debug)]
#[wasn_bindgen(js_name = setPanicHook)]
pub fn set_panic_hook() {
console_error_panic_hook::set_once();
}

type InvalidInputResult<T> = Result<T, JsValue>;

#[wasm_bindgen(js_name = isPoint)]
pub fn is_point(pubkey: &[u8]) -> bool {
secp256k1::is_point(pubkey)
}

#[wasm_bindgen(js_name = isPointCompressed)]
pub fn is_point_compressed(pubkey: &[u8]) -> bool {
secp256k1::is_point_compressed(pubkey)
}

#[wasm_bindgen(js_name = isPrivate)]
pub fn is_private(seckey: &[u8]) -> bool {
secp256k1::is_private(seckey)
}

#[wasm_bindgen(js_name = pointAdd)]
pub fn point_add(
pubkey1: &[u8],
pubkey2: &[u8],
compressed: Option<bool>,
) -> InvalidInputResult<Option<Vec<u8>>> {
secp256k1::point_add(pubkey1, pubkey2, compressed).map_err(Into::into)
}

#[wasm_bindgen(js_name = pointAddScalar)]
pub fn point_add_scalar(
pubkey: &[u8],
tweak: &[u8],
compressed: Option<bool>,
) -> InvalidInputResult<Option<Vec<u8>>> {
secp256k1::point_add_scalar(pubkey, tweak, compressed).map_err(Into::into)
}

#[wasm_bindgen(js_name = pointCompress)]
pub fn point_compress(pubkey: &[u8], compressed: bool) -> InvalidInputResult<Vec<u8>> {
secp256k1::point_compress(pubkey, compressed).map_err(Into::into)
}

#[wasm_bindgen(js_name = pointFromScalar)]
pub fn point_from_scalar(seckey: &[u8], compressed: Option<bool>) -> InvalidInputResult<Vec<u8>> {
secp256k1::point_from_scalar(seckey, compressed).map_err(Into::into)
}

#[wasm_bindgen(js_name = pointMultiply)]
pub fn point_multiply(
pubkey: &[u8],
tweak: &[u8],
compressed: Option<bool>,
) -> InvalidInputResult<Vec<u8>> {
secp256k1::point_multiply(pubkey, tweak, compressed).map_err(Into::into)
}

#[wasm_bindgen(js_name = privateAdd)]
pub fn private_add(seckey: &[u8], tweak: &[u8]) -> InvalidInputResult<Option<Vec<u8>>> {
secp256k1::private_add(seckey, tweak).map_err(Into::into)
}

#[wasm_bindgen(js_name = privateSub)]
pub fn pirvate_sub(seckey: &[u8], tweak: &[u8]) -> InvalidInputResult<Option<Vec<u8>>> {
secp256k1::pirvate_sub(seckey, tweak).map_err(Into::into)
}

#[wasm_bindgen]
pub fn sign(message: &[u8], seckey: &[u8]) -> InvalidInputResult<Vec<u8>> {
secp256k1::sign(message, seckey).map_err(Into::into)
}

#[wasm_bindgen(js_name = signWithEntropy)]
pub fn sign_with_entropy(
message: &[u8],
seckey: &[u8],
noncedata: &[u8],
) -> InvalidInputResult<Vec<u8>> {
secp256k1::sign_with_entropy(message, seckey, noncedata).map_err(Into::into)
}

#[wasm_bindgen]
pub fn verify(
message: &[u8],
pubkey: &[u8],
signature: &[u8],
strict: Option<bool>,
) -> InvalidInputResult<bool> {
secp256k1::verify(message, pubkey, signature, strict).map_err(Into::into)
}
5 changes: 5 additions & 0 deletions secp256k1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ publish = false

[dependencies]
getrandom = "0.2.2"
js-sys = { version = "0.3.48", optional = true }
secp256k1-sys = "0.4.0"
wasm-bindgen = { version = "0.2.71", optional = true }

[features]
wasm = ["getrandom/js", "js-sys", "wasm-bindgen"]
Loading

0 comments on commit 0d281c1

Please sign in to comment.