Skip to content

Commit

Permalink
Revamp example and its doc (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
kigawas authored Aug 4, 2023
1 parent da169bb commit c200b23
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 153 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import init, * as ecies from "ecies-wasm";

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

const data = Uint8Array.from([1, 2, 3, 4]);
const encoder = new TextEncoder();
const data = encoder.encode("hello ecies🔒");

const [sk, pk] = ecies.generateKeypair();
const encrypted = ecies.encrypt(pk, data);
Expand Down
14 changes: 13 additions & 1 deletion example/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# wasm example
# ecies wasm example

This example shows how to use `ecies-wasm` with [vite](https://vitejs.dev/).

## Install

Expand All @@ -11,3 +13,13 @@
## Build and preview

`yarn build && yarn preview`

## Build local wasm package (optional)

Make sure you have [wasm-pack](https://rustwasm.github.io/wasm-pack/installer/) installed. Then `wasm-pack build --target web`.

After `pkg` folder is generated, update `import` in `index.js` as:

```ts
import init, * as ecies from "../pkg/ecies_wasm";
```
14 changes: 11 additions & 3 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
// need to build with `wasm-pack build --target web`
import init, * as ecies from "../pkg/ecies_wasm";
// import init, * as ecies from "../pkg/ecies_wasm";
// check vite.config.js as well
import init, * as ecies from "ecies-wasm";

init();

const data = Uint8Array.from([1, 2, 3, 4]);
const encoder = new TextEncoder();
const data = encoder.encode("hello ecies🔒");

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

const encrypted = ecies.encrypt(pk, data);
const decrypted = ecies.decrypt(sk, encrypted);
alert(`decrypted: ${decrypted}`);

const decoder = new TextDecoder();
alert(`decrypted: ${decoder.decode(decrypted)}`);

if (decrypted.toString("hex") === data.toString("hex")) {
alert("call wasm encrypt decrypt ok");
} else {
alert("call wasm encrypt decrypt failed");
}
}

Expand Down
8 changes: 5 additions & 3 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@
"name": "ecies-wasm-example",
"version": "0.1.0",
"author": "Weiliang Li <to.be.impressive@gmail.com>",
"description": "ecies-wasm in browser",
"description": "ecies-wasm example in browser",
"license": "MIT",
"main": "index.js",
"type": "module",
"repository": {
"url": "https://github.com/ecies/rs"
"url": "https://github.com/ecies/rs-wasm"
},
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {},
"dependencies": {
"ecies-wasm": "^0.2.0"
},
"devDependencies": {
"vite": "^4.4.4"
}
Expand Down
3 changes: 3 additions & 0 deletions example/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { defineConfig } from "vite";

export default defineConfig({
optimizeDeps: {
exclude: ["ecies-wasm"], // otherwise wasm file will not be copied into .vite/deps
},
server: {
fs: {
// Allow serving files from one level up to the project root
Expand Down
Loading

0 comments on commit c200b23

Please sign in to comment.