Skip to content

Commit abe8ca0

Browse files
authored
Revamp type for typescript (#22)
1 parent 0de0d2f commit abe8ca0

File tree

9 files changed

+61
-44
lines changed

9 files changed

+61
-44
lines changed

.cspell.jsonc

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,28 @@
44
// language - current active spelling language
55
"language": "en",
66
// words - list of words to be always considered correct
7-
"words": ["bindgen", "ecies", "eciesrs", "keypair"],
7+
"words": [
8+
"bindgen",
9+
"ecies",
10+
"eciesrs",
11+
"keypair",
12+
"libsecp256k1"
13+
],
814
// flagWords - list of words to be always considered incorrect
915
// This is useful for offensive words and common spelling errors.
1016
// For example "hte" should be "the"
11-
"flagWords": ["hte"],
17+
"flagWords": [
18+
"hte"
19+
],
1220
"ignorePaths": [
1321
".git",
1422
".github",
1523
".gitignore",
1624
".cspell.jsonc",
25+
"node_modules",
26+
"pkg",
1727
"LICENSE",
1828
"package.json",
19-
"yarn.lock"
29+
"pnpm-lock.yaml"
2030
]
2131
}

.github/dependabot.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ updates:
44
directory: "/"
55
schedule:
66
interval: monthly
7-
open-pull-requests-limit: 10
7+
open-pull-requests-limit: 3

.github/workflows/cd.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ env:
1010
jobs:
1111
publish:
1212
runs-on: ubuntu-latest
13+
permissions:
14+
contents: read
15+
id-token: write
1316
steps:
1417
- uses: actions/checkout@v4
15-
18+
- uses: pnpm/action-setup@v4
19+
with:
20+
version: 10
1621
- uses: actions/setup-node@v4
1722
with:
18-
node-version: 20
19-
registry-url: "https://registry.npmjs.org"
23+
node-version: 22
2024

2125
- uses: dtolnay/rust-toolchain@master
2226
with:
@@ -37,3 +41,4 @@ jobs:
3741
run: cd pkg && npm publish
3842
env:
3943
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
44+
NPM_CONFIG_PROVENANCE: true

.github/workflows/ci.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ jobs:
1515
runs-on: ubuntu-latest
1616
steps:
1717
- uses: actions/checkout@v4
18-
18+
- uses: pnpm/action-setup@v4
19+
with:
20+
version: 10
1921
- uses: actions/setup-node@v4
2022
with:
21-
node-version: 20
23+
node-version: 22
2224

2325
- uses: dtolnay/rust-toolchain@master
2426
with:

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 0.2.1
4+
5+
- Bump dependencies
6+
- Revamp type for typescript
7+
38
## 0.2.0
49

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

1116
## 0.1.2
17+
1218
- Bump dependencies
1319

1420
## 0.1.1

Cargo.toml

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,34 @@
11
[package]
22
name = "ecies-wasm"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
# docs
55
authors = ["Weiliang Li <[email protected]>"]
66
description = "A WASM binding for eciesrs"
77
edition = "2021"
8-
keywords = [
9-
"secp256k1",
10-
"crypto",
11-
"ecc",
12-
"ecies",
13-
"cryptocurrency",
14-
]
8+
keywords = ["secp256k1", "crypto", "ecc", "ecies", "cryptography"]
159
license = "MIT"
1610
readme = "README.md"
1711

1812
# links
1913
documentation = "https://www.npmjs.com/package/ecies-wasm/"
2014
homepage = "https://ecies.org/rs-wasm/"
21-
repository = "https://github.com/ecies/rs-wasm"
15+
repository = "git+https://github.com/ecies/rs-wasm.git"
2216

2317
[dependencies]
24-
ecies = {version = "0.2", default-features = false, features = ["pure"]}
25-
once_cell = {version = "1.18.0", default-features = false, features = ["std"]}
18+
ecies = { version = "0.2.9", default-features = false, features = ["aes-rust"] }
19+
once_cell = { version = "1.21.3", default-features = false, features = ["std"] }
2620

2721
# specify "lazy-static-context" to reduce wasm size
28-
libsecp256k1 = {version = "0.7.1", default-features = false, features = ["lazy-static-context"]}
22+
libsecp256k1 = { version = "0.7.2", default-features = false, features = [
23+
"lazy-static-context",
24+
] }
2925

3026
# wasm
31-
js-sys = "0.3.64"
32-
wasm-bindgen = "0.2.87"
27+
js-sys = "0.3.77"
28+
wasm-bindgen = "0.2.100"
3329

3430
[dev-dependencies]
35-
wasm-bindgen-test = "0.3.37"
31+
wasm-bindgen-test = "0.3.50"
3632

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

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020-2023 Weiliang Li
3+
Copyright (c) 2020-2025 Weiliang Li
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

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

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

3537
```ts
36-
export function generateKeypair(): Array<Uint8Array>; // [sk, pk]
38+
export function generateKeypair(): Uint8Array[]; // [sk, pk]
3739
export function encrypt(pk: Uint8Array, msg: Uint8Array): Uint8Array;
3840
export function decrypt(sk: Uint8Array, msg: Uint8Array): Uint8Array;
3941
```
@@ -42,6 +44,7 @@ export function decrypt(sk: Uint8Array, msg: Uint8Array): Uint8Array;
4244

4345
```bash
4446
wasm-pack build
47+
wasm-pack build --target web
4548
wasm-pack test --node
4649
```
4750

src/lib.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,32 @@
11
use ecies::{
22
decrypt as _decrypt, encrypt as _encrypt, utils::generate_keypair as _generate_keypair,
33
};
4-
use js_sys::{Array, Uint8Array};
4+
use js_sys::Uint8Array;
55
use wasm_bindgen::prelude::{wasm_bindgen, JsError};
66

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

15-
let ret = Array::new();
16-
ret.push(&sk);
17-
ret.push(&pk);
14+
let mut ret = Vec::with_capacity(2);
15+
ret.push(Uint8Array::from(sk));
16+
ret.push(Uint8Array::from(pk));
1817
ret
1918
}
2019

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

2926
/// Decrypt a message by a secret key
3027
#[wasm_bindgen]
31-
pub fn decrypt(sk: &[u8], msg: &[u8]) -> Result<Uint8Array, JsError> {
32-
_decrypt(sk, msg)
33-
.map(|v| Uint8Array::from(v.as_slice()))
34-
.map_err(|e| JsError::new(&e.to_string()))
28+
pub fn decrypt(sk: &[u8], msg: &[u8]) -> Result<Vec<u8>, JsError> {
29+
_decrypt(sk, msg).map_err(|e| JsError::new(&e.to_string()))
3530
}
3631

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

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

46-
const MSG: &str = "hello-world🌎";
41+
const MSG: &str = "hello ecies🔒";
4742

4843
fn __enc_dec(sk: &[u8], pk: &[u8], msg: &[u8]) {
4944
let encrypted = encrypt(pk, msg).ok().unwrap().to_vec();
@@ -63,8 +58,8 @@ mod tests {
6358
#[wasm_bindgen_test]
6459
fn test_wasm() {
6560
let pair = generateKeypair();
66-
let sk = Uint8Array::from(pair.get(0)).to_vec();
67-
let pk = Uint8Array::from(pair.get(1)).to_vec();
61+
let sk = pair[0].to_vec();
62+
let pk = pair[1].to_vec();
6863
let msg = Uint8Array::from(MSG.as_bytes()).to_vec();
6964

7065
__enc_dec(&sk, &pk, &msg);

0 commit comments

Comments
 (0)