Skip to content

Commit

Permalink
Replace node:crypto aes with pure JS aes
Browse files Browse the repository at this point in the history
  • Loading branch information
kigawas committed Oct 2, 2024
1 parent b82c085 commit df01b8b
Show file tree
Hide file tree
Showing 19 changed files with 2,101 additions and 382 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ jobs:
with:
token: ${{ secrets.CODECOV_TOKEN }}
- run: pnpm run build && npm publish --dry-run

- run: cd example && pnpm install
- run: node index.js
- run: node import.js
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@

# Changelog

## 0.4.9

- Use [noble-ciphers](https://github.com/paulmillr/noble-ciphers)'s pure JS aes to improve browser compatibility. Now there's no need to polyfill `node:crypto` in browsers
- Revamp dependencies
- Revamp exports so that almost all functions can be imported
- Update documentation

## 0.4.1 ~ 0.4.8

- Revamp util functions
Expand Down
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 (node>=16 is required).

## 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
$ 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 neccessary to polyfill `Buffer`, from version 0.5.0, `Buffer` will be completely removed.

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 df01b8b

Please sign in to comment.