Skip to content

Commit

Permalink
Add examples (#796)
Browse files Browse the repository at this point in the history
  • Loading branch information
kigawas authored Oct 6, 2024
1 parent 0d385f4 commit 9ef73bb
Show file tree
Hide file tree
Showing 10 changed files with 1,771 additions and 10 deletions.
30 changes: 20 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,40 @@ Elliptic Curve Integrated Encryption Scheme for secp256k1/curve25519 in TypeScri

This is the JavaScript/TypeScript version of [eciespy](https://github.com/ecies/py) with a built-in class-like secp256k1/curve25519 [API](#privatekey), you may go there for detailed documentation and learn the mechanism under the hood.

If you want a WASM version to run directly in modern browsers or on some blockchains, check [`ecies-wasm`](https://github.com/ecies/rs-wasm).

## Install

```bash
npm install eciesjs
```

We recommend using the latest Node runtime although it's still possible to install on old versions.
We recommend using the latest Node runtime although it's still possible to install on old versions (but at least node 16).

## Quick Start

Run the code below with `npx ts-node`.

```typescript
> import { encrypt, decrypt, PrivateKey } from 'eciesjs'
> const sk = new PrivateKey()
> const data = Buffer.from('hello world🌍')
> decrypt(sk.secret, encrypt(sk.publicKey.toHex(), data)).toString()
'hello world🌍'
import { PrivateKey, decrypt, encrypt } from "eciesjs";

const sk = new PrivateKey()
const data = Buffer.from('hello world🌍')
const decrypted = decrypt(sk.secret, encrypt(sk.publicKey.toHex(), data))
console.log(Buffer.from(decrypted).toString())
```

Run the example code above:

```bash
$ pnpm build && cd example && pnpm install && node index.js
hello world🌍
```

See [Configuration](#configuration) to control with more granularity.

## Browser Support

This library is browser-friendly, check the [`example/browser`](./example/browser) directory for details. Currently it's necessary to polyfill some node modules (like `node:crypto`, `Buffer`). From v0.5.0, it can run in browsers without polyfill.

If you want a WASM version to run directly in modern browsers or on some blockchains, check [`ecies-wasm`](https://github.com/ecies/rs-wasm).

## API

### `encrypt(receiverRawPK: string | Uint8Array, msg: Uint8Array): Buffer`
Expand Down
15 changes: 15 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# eciesjs-example

Run `pnpm install` first

## Basic usage

Run `node index.js`

## Check import

Run `node import.js`

## Browser

Run `pnpm dev`
51 changes: 51 additions & 0 deletions example/browser/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { bytesToHex } from "@noble/ciphers/utils";
import { PrivateKey, decrypt, encrypt } from "eciesjs";
import './style.css';


const sk = new PrivateKey();
const encoder = new TextEncoder();
const decoder = new TextDecoder();

export function setup(encryptedElement, textElement, decryptedElement) {
const text = "hello eciesjs🔒"
let encrypted;

encryptedElement.innerHTML = `click me to encrypt`
textElement.innerHTML = text
decryptedElement.innerHTML = `click me to decrypt`

const _encrypt = () => {
encrypted = encrypt(sk.publicKey.toHex(), encoder.encode(text))
encryptedElement.innerHTML = `encrypted:`
textElement.innerHTML = `${bytesToHex(encrypted)}`
decryptedElement.innerHTML = `click me to decrypt`
}
const _decrypt = () => {
encryptedElement.innerHTML = `click me to encrypt`
if (encrypted) {
textElement.innerHTML = `${decoder.decode(decrypt(sk.secret, encrypted))}`
decryptedElement.innerHTML = `decrypted:`
encrypted = undefined
} else {
textElement.innerHTML = "click encrypt button first"
}
}
encryptedElement.addEventListener('click', () => _encrypt())
decryptedElement.addEventListener('click', () => _decrypt())
}


document.querySelector('#app').innerHTML = `
<div>
<h1>Hello eciesjs!</h1>
<div class="card">
<button id="encrypted" type="button"></button>
<button id="decrypted" type="button"></button>
</div>
<p id="text"></p>
</div>
`

setup(document.querySelector('#encrypted'), document.querySelector('#text'),
document.querySelector('#decrypted'))
93 changes: 93 additions & 0 deletions example/browser/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
:root {
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
line-height: 1.5;
font-weight: 400;

color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;

font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}

a:hover {
color: #535bf2;
}

body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}

h1 {
font-size: 3.2em;
line-height: 1.1;
}

p {
padding: 2em;
word-break: break-all;
}

#app {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}

button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}

button:hover {
border-color: #646cff;
}

button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}

@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}

a:hover {
color: #747bff;
}

button {
background-color: #f9f9f9;
}
}
8 changes: 8 additions & 0 deletions example/import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ECIES_CONFIG, utils } from "eciesjs";
import config from "eciesjs/config";
import consts from "eciesjs/consts";

console.log("ECIES_CONFIG:", ECIES_CONFIG)
console.log("config:", config)
console.log("consts:", consts)
console.log("utils:", utils)
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 lang="en">

<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Eciesjs App</title>
</head>

<body>
<div id="app"></div>
<script type="module" src="/browser/script.js"></script>
</body>

</html>
6 changes: 6 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { PrivateKey, decrypt, encrypt } from "eciesjs";

const sk = new PrivateKey()
const data = Buffer.from('hello world🌍')
const decrypted = decrypt(sk.secret, encrypt(sk.publicKey.toHex(), data))
console.log(Buffer.from(decrypted).toString())
22 changes: 22 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "eciesjs-example",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"eciesjs": "file:.."
},
"devDependencies": {
"vite": "^5.4.8",
"vite-plugin-node-polyfills": "^0.22.0"
}
}
Loading

0 comments on commit 9ef73bb

Please sign in to comment.