Skip to content
Merged
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
16 changes: 13 additions & 3 deletions .cspell.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,28 @@
// language - current active spelling language
"language": "en",
// words - list of words to be always considered correct
"words": ["bindgen", "ecies", "eciesrs", "keypair"],
"words": [
"bindgen",
"ecies",
"eciesrs",
"keypair",
"libsecp256k1"
],
// flagWords - list of words to be always considered incorrect
// This is useful for offensive words and common spelling errors.
// For example "hte" should be "the"
"flagWords": ["hte"],
"flagWords": [
"hte"
],
"ignorePaths": [
".git",
".github",
".gitignore",
".cspell.jsonc",
"node_modules",
"pkg",
"LICENSE",
"package.json",
"yarn.lock"
"pnpm-lock.yaml"
]
}
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ updates:
directory: "/"
schedule:
interval: monthly
open-pull-requests-limit: 10
open-pull-requests-limit: 3
11 changes: 8 additions & 3 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ env:
jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v4

- uses: pnpm/action-setup@v4
with:
version: 10
- uses: actions/setup-node@v4
with:
node-version: 20
registry-url: "https://registry.npmjs.org"
node-version: 22

- uses: dtolnay/rust-toolchain@master
with:
Expand All @@ -37,3 +41,4 @@ jobs:
run: cd pkg && npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
NPM_CONFIG_PROVENANCE: true
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: pnpm/action-setup@v4
with:
version: 10
- uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22

- uses: dtolnay/rust-toolchain@master
with:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.2.1

- Bump dependencies
- Revamp type for typescript

## 0.2.0

- Reduce wasm size from 1.2MB to ~100KB by revamping the build process
Expand All @@ -9,6 +14,7 @@
- Bump dependencies

## 0.1.2

- Bump dependencies

## 0.1.1
Expand Down
26 changes: 11 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
[package]
name = "ecies-wasm"
version = "0.2.0"
version = "0.2.1"
# docs
authors = ["Weiliang Li <to.be.impressive@gmail.com>"]
description = "A WASM binding for eciesrs"
edition = "2021"
keywords = [
"secp256k1",
"crypto",
"ecc",
"ecies",
"cryptocurrency",
]
keywords = ["secp256k1", "crypto", "ecc", "ecies", "cryptography"]
license = "MIT"
readme = "README.md"

# links
documentation = "https://www.npmjs.com/package/ecies-wasm/"
homepage = "https://ecies.org/rs-wasm/"
repository = "https://github.com/ecies/rs-wasm"
repository = "git+https://github.com/ecies/rs-wasm.git"

[dependencies]
ecies = {version = "0.2", default-features = false, features = ["pure"]}
once_cell = {version = "1.18.0", default-features = false, features = ["std"]}
ecies = { version = "0.2.9", default-features = false, features = ["aes-rust"] }
once_cell = { version = "1.21.3", default-features = false, features = ["std"] }

# specify "lazy-static-context" to reduce wasm size
libsecp256k1 = {version = "0.7.1", default-features = false, features = ["lazy-static-context"]}
libsecp256k1 = { version = "0.7.2", default-features = false, features = [
"lazy-static-context",
] }

# wasm
js-sys = "0.3.64"
wasm-bindgen = "0.2.87"
js-sys = "0.3.77"
wasm-bindgen = "0.2.100"

[dev-dependencies]
wasm-bindgen-test = "0.3.37"
wasm-bindgen-test = "0.3.50"

[lib]
crate-type = ["cdylib", "rlib"]
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020-2023 Weiliang Li
Copyright (c) 2020-2025 Weiliang Li

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

[![License](https://img.shields.io/github/license/ecies/rs-wasm.svg)](https://github.com/ecies/rs-wasm)
[![NPM Package](https://img.shields.io/npm/v/ecies-wasm.svg)](https://www.npmjs.com/package/ecies-wasm)
[![NPM Downloads](https://img.shields.io/npm/dm/ecies-wasm)](https://npm-stat.link/ecies-wasm)
[![Install size](https://packagephobia.com/badge?p=ecies-wasm)](https://packagephobia.com/result?p=ecies-wasm)
[![CI](https://img.shields.io/github/actions/workflow/status/ecies/rs-wasm/ci.yml)](https://github.com/ecies/rs-wasm/actions)

A WASM binding for [eciesrs](https://github.com/ecies/rs).
Expand Down Expand Up @@ -33,7 +35,7 @@ Check [the example](./example) for how to use it in browsers.
## API

```ts
export function generateKeypair(): Array<Uint8Array>; // [sk, pk]
export function generateKeypair(): Uint8Array[]; // [sk, pk]
export function encrypt(pk: Uint8Array, msg: Uint8Array): Uint8Array;
export function decrypt(sk: Uint8Array, msg: Uint8Array): Uint8Array;
```
Expand All @@ -42,6 +44,7 @@ export function decrypt(sk: Uint8Array, msg: Uint8Array): Uint8Array;

```bash
wasm-pack build
wasm-pack build --target web
wasm-pack test --node
```

Expand Down
31 changes: 13 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
use ecies::{
decrypt as _decrypt, encrypt as _encrypt, utils::generate_keypair as _generate_keypair,
};
use js_sys::{Array, Uint8Array};
use js_sys::Uint8Array;
use wasm_bindgen::prelude::{wasm_bindgen, JsError};

/// Generate a `(SecretKey, PublicKey)` pair
#[allow(non_snake_case)]
#[wasm_bindgen]
pub fn generateKeypair() -> Array {
pub fn generateKeypair() -> Vec<Uint8Array> {
let (sk, pk) = _generate_keypair();
let (sk, pk) = (&sk.serialize()[..], &pk.serialize_compressed()[..]);
let (sk, pk) = (Uint8Array::from(sk), Uint8Array::from(pk));
let (sk, pk): (&[u8], &[u8]) = (&sk.serialize(), &pk.serialize_compressed());

let ret = Array::new();
ret.push(&sk);
ret.push(&pk);
let mut ret = Vec::with_capacity(2);
ret.push(Uint8Array::from(sk));
ret.push(Uint8Array::from(pk));
ret
}

/// Encrypt a message by a public key
#[wasm_bindgen]
pub fn encrypt(pk: &[u8], msg: &[u8]) -> Result<Uint8Array, JsError> {
_encrypt(pk, msg)
.map(|v| Uint8Array::from(v.as_slice()))
.map_err(|e| JsError::new(&e.to_string()))
pub fn encrypt(pk: &[u8], msg: &[u8]) -> Result<Vec<u8>, JsError> {
_encrypt(pk, msg).map_err(|e| JsError::new(&e.to_string()))
}

/// Decrypt a message by a secret key
#[wasm_bindgen]
pub fn decrypt(sk: &[u8], msg: &[u8]) -> Result<Uint8Array, JsError> {
_decrypt(sk, msg)
.map(|v| Uint8Array::from(v.as_slice()))
.map_err(|e| JsError::new(&e.to_string()))
pub fn decrypt(sk: &[u8], msg: &[u8]) -> Result<Vec<u8>, JsError> {
_decrypt(sk, msg).map_err(|e| JsError::new(&e.to_string()))
}

#[cfg(all(test, target_arch = "wasm32"))]
Expand All @@ -43,7 +38,7 @@ mod tests {

use super::{_generate_keypair, decrypt, encrypt, generateKeypair};

const MSG: &str = "hello-world🌎";
const MSG: &str = "hello ecies🔒";

fn __enc_dec(sk: &[u8], pk: &[u8], msg: &[u8]) {
let encrypted = encrypt(pk, msg).ok().unwrap().to_vec();
Expand All @@ -63,8 +58,8 @@ mod tests {
#[wasm_bindgen_test]
fn test_wasm() {
let pair = generateKeypair();
let sk = Uint8Array::from(pair.get(0)).to_vec();
let pk = Uint8Array::from(pair.get(1)).to_vec();
let sk = pair[0].to_vec();
let pk = pair[1].to_vec();
let msg = Uint8Array::from(MSG.as_bytes()).to_vec();

__enc_dec(&sk, &pk, &msg);
Expand Down
Loading