Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revamp wasm build & example #12

Merged
merged 1 commit into from
Jul 30, 2023
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
File renamed without changes.
21 changes: 21 additions & 0 deletions .cspell.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
// Version of the setting file. Always 0.2
"version": "0.2",
// language - current active spelling language
"language": "en",
// words - list of words to be always considered correct
"words": ["bindgen", "ecies", "eciesrs", "keypair"],
// 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"],
"ignorePaths": [
".git",
".github",
".gitignore",
".cspell.jsonc",
"LICENSE",
"package.json",
"yarn.lock"
]
}
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ updates:
- package-ecosystem: cargo
directory: "/"
schedule:
interval: daily
interval: monthly
open-pull-requests-limit: 10
12 changes: 6 additions & 6 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
node-version: 20
registry-url: "https://registry.npmjs.org"
- uses: actions-rs/toolchain@v1
with:
Expand All @@ -24,7 +24,7 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: generate-lockfile
- uses: actions/cache@v2
- uses: actions/cache@v3
with:
path: |
~/.cargo/registry
Expand All @@ -34,10 +34,10 @@ jobs:
key: ${{ runner.os }}-${{ hashFiles('**/Cargo.lock') }}

- name: Install wasm pack
run: cargo install --git https://github.com/rustwasm/wasm-pack.git || true
run: cargo install wasm-pack || true

- name: Build
run: wasm-pack build
run: wasm-pack build --target web

- name: Publish
run: cd pkg && npm publish
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
node-version: 20
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
Expand All @@ -26,7 +26,7 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: generate-lockfile
- uses: actions/cache@v2
- uses: actions/cache@v3
with:
path: |
~/.cargo/registry
Expand All @@ -35,11 +35,11 @@ jobs:
target
key: ${{ runner.os }}-${{ hashFiles('**/Cargo.lock') }}

- name: Install wasm pack # To fix bug: https://github.com/rustwasm/wasm-pack/issues/837
run: cargo install --git https://github.com/rustwasm/wasm-pack.git || true
- name: Install wasm pack
run: cargo install wasm-pack || true

- name: Build
run: wasm-pack build
run: wasm-pack build --target web

- name: Run tests
run: wasm-pack test --node
Expand Down
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk


.DS_Store

# js stuff
node_modules/
dist/
10 changes: 1 addition & 9 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
{
"spellright.language": ["en"],
"spellright.documentTypes": ["markdown", "latex", "plaintext", "toml"],
"rust-analyzer.cargo.target": "wasm32-unknown-unknown",
"spellright.ignoreFiles": [
"~/.cargo/",
"~/.rustup/",
"**/.gitignore",
"**/.spellignore"
]
"rust-analyzer.cargo.target": "wasm32-unknown-unknown"
}
12 changes: 0 additions & 12 deletions .vscode/spellright.dict

This file was deleted.

21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Changelog

## 0.2.0

- Reduce wasm size from 1.2MB to ~100KB by revamping the build process
- Rename `generate_keypair` to `generateKeypair` to follow JavaScript convention
- Revamp wasm example to use vite instead of webpack
- Revamp wasm build package to build web target
- Bump dependencies

## 0.1.2
- Bump dependencies

## 0.1.1

- Expose `generate_keypair`
- Revamp doc

## 0.1.0

- First beta release
15 changes: 10 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ecies-wasm"
version = "0.1.2"
version = "0.2.0"
# docs
authors = ["Weiliang Li <to.be.impressive@gmail.com>"]
description = "A WASM binding for eciesrs"
Expand All @@ -21,17 +21,22 @@ homepage = "https://ecies.org/rs-wasm/"
repository = "https://github.com/ecies/rs-wasm"

[dependencies]
ecies = {version = "0.2.2", default-features = false, features = ["pure"]}
ecies = {version = "0.2", default-features = false, features = ["pure"]}
once_cell = {version = "1.18.0", 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"]}

# wasm
js-sys = "0.3.55"
wasm-bindgen = "0.2.78"
js-sys = "0.3.64"
wasm-bindgen = "0.2.87"

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

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

[profile.release]
lto = true
opt-level = "z"
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-2021 Weiliang Li
Copyright (c) 2020-2023 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
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![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)
[![CI](https://img.shields.io/github/workflow/status/ecies/rs-wasm/Build%20wasm)](https://github.com/ecies/rs-wasm/actions)
[![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 All @@ -15,23 +15,26 @@ npm install ecies-wasm
## Usage

```js
import * as ecies from "ecies-wasm";
import init, * as ecies from "ecies-wasm";

init(); // if built with vite without plugin

const [sk, pk] = ecies.generate_keypair();
const data = Uint8Array.from([1, 2, 3, 4]);

const [sk, pk] = ecies.generateKeypair();
const encrypted = ecies.encrypt(pk, data);
const decrypted = ecies.decrypt(sk, encrypted);
alert("decrypted: " + decrypted);
```

Check [this example](https://github.com/ecies/wasm-example) for how to use it in browsers.
Check [the example](./example) for how to use it in browsers.

## API

```ts
function encrypt(receiver_pub: Uint8Array, msg: Uint8Array): Uint8Array | undefined;
function decrypt(receiver_sec: Uint8Array, msg: Uint8Array): Uint8Array | undefined;
export function generateKeypair(): Array<Uint8Array>; // [sk, pk]
export function encrypt(pk: Uint8Array, msg: Uint8Array): Uint8Array;
export function decrypt(sk: Uint8Array, msg: Uint8Array): Uint8Array;
```

## Build and test
Expand All @@ -40,3 +43,7 @@ function decrypt(receiver_sec: Uint8Array, msg: Uint8Array): Uint8Array | undefi
wasm-pack build
wasm-pack test --node
```

## Changelog

See [CHANGELOG.md](./CHANGELOG.md).
13 changes: 13 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# wasm example

## Install

`yarn install`

## Run dev server

`yarn dev`

## Build and preview

`yarn build && yarn preview`
15 changes: 15 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>Hello ecies</title>
</head>

<body>
<main id="app"></main>
<noscript>This page contains webassembly and javascript content, please enable javascript in your browser.</noscript>
<script type="module" src="./index.js"></script>
</body>

</html>
44 changes: 44 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// need to build with `wasm-pack build --target web`
import init, * as ecies from "../pkg/ecies_wasm";

init();

const data = Uint8Array.from([1, 2, 3, 4]);

function checkOk() {
const [sk, pk] = ecies.generateKeypair();

const encrypted = ecies.encrypt(pk, data);
const decrypted = ecies.decrypt(sk, encrypted);
alert(`decrypted: ${decrypted}`);
if (decrypted.toString("hex") === data.toString("hex")) {
alert("call wasm encrypt decrypt ok");
}
}

function checkError() {
const pk = Uint8Array.from([0]);
try {
ecies.encrypt(pk, data);
} catch (e) {
alert(e);
}
}

document.querySelector("#app").innerHTML = `
<h1>WASM Test</h1>
<button id="ok">Check ok</button>
<button id="error">Check error</button>
`;

document.getElementById("ok").addEventListener("click", () => {
checkOk();
});
document.getElementById("error").addEventListener("click", () => {
checkError();
});

window.addEventListener("error", (event) => {
// catch all other errors
console.error(event);
});
21 changes: 21 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "ecies-wasm-example",
"version": "0.1.0",
"author": "Weiliang Li <to.be.impressive@gmail.com>",
"description": "ecies-wasm in browser",
"license": "MIT",
"main": "index.js",
"type": "module",
"repository": {
"url": "https://github.com/ecies/rs"
},
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {},
"devDependencies": {
"vite": "^4.4.4"
}
}
10 changes: 10 additions & 0 deletions example/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from "vite";

export default defineConfig({
server: {
fs: {
// Allow serving files from one level up to the project root
allow: [".."],
},
},
});
Loading
Loading