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 8, 2024
1 parent 82c6d72 commit f07fc96
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 170 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ jobs:

- run: cd example && pnpm install
- run: node example/index.js
- run: node example/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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This is the JavaScript/TypeScript version of [eciespy](https://github.com/ecies/
npm install eciesjs
```

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

## Quick Start

Expand All @@ -40,7 +40,7 @@ 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.
This library is browser-friendly, check the [`example/browser`](./example/browser) directory for details. Currently it's necessary 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).

Expand All @@ -50,7 +50,7 @@ If you want a WASM version to run directly in modern browsers or on some blockch

Parameters:

- **receiverRawPK** - Receiver's public key, hex string or buffer
- **receiverRawPK** - Receiver's public key, hex string or Uint8Array
- **msg** - Data to encrypt

Returns: **Buffer**
Expand All @@ -59,7 +59,7 @@ Returns: **Buffer**

Parameters:

- **receiverRawSK** - Receiver's private key, hex string or buffer
- **receiverRawSK** - Receiver's private key, hex string or Uint8Array
- **msg** - Data to decrypt

Returns: **Buffer**
Expand Down
4 changes: 3 additions & 1 deletion example/import.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { ECIES_CONFIG, utils } from "eciesjs";
import { ECIES_CONFIG, utils as _utils } from "eciesjs";
import config from "eciesjs/config";
import consts from "eciesjs/consts";
import utils from "eciesjs/utils";

console.log("ECIES_CONFIG:", ECIES_CONFIG)
console.log("config:", config)
console.log("consts:", consts)
console.log("utils:", utils)
console.log("index utils:", _utils)
6 changes: 3 additions & 3 deletions example/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { nodePolyfills } from "vite-plugin-node-polyfills";
export default defineConfig({
plugins: [
nodePolyfills({
include: ["crypto", "stream", "util"],
include: [],
globals: {
Buffer: true,
global: true,
process: true,
global: false,
process: false,
},
}),
],
Expand Down
24 changes: 21 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"type": "git",
"url": "git+https://github.com/ecies/js.git"
},
"version": "0.4.8",
"version": "0.4.9",
"engines": {
"node": ">=16.0.0"
},
Expand All @@ -30,6 +30,24 @@
"files": [
"dist"
],
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"./config": {
"types": "./dist/config.d.ts",
"import": "./dist/config.js"
},
"./consts": {
"types": "./dist/consts.d.ts",
"import": "./dist/consts.js"
},
"./utils": {
"types": "./dist/utils/index.d.ts",
"import": "./dist/utils/index.js"
}
},
"scripts": {
"build": "npx tsc",
"test": "vitest"
Expand All @@ -40,11 +58,11 @@
"@noble/hashes": "^1.5.0"
},
"devDependencies": {
"@types/node": "^22.7.4",
"@types/node": "^22.7.5",
"@vitest/coverage-v8": "2.1.2",
"typescript": "^5.6.2",
"undici": "^6.19.8",
"vitest": "^2.1.2"
},
"packageManager": "[email protected].0+sha512.4abf725084d7bcbafbd728bfc7bee61f2f791f977fd87542b3579dcb23504d170d46337945e4c66485cd12d588a0c0e570ed9c477e7ccdd8507cf05f3f92eaca"
"packageManager": "[email protected].1+sha512.e5a7e52a4183a02d5931057f7a0dbff9d5e9ce3161e33fa68ae392125b79282a8a8a470a51dfc8a0ed86221442eb2fb57019b0990ed24fab519bf0e1bc5ccfc4"
}
57 changes: 25 additions & 32 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export { ECIES_CONFIG } from "./config";
export { PrivateKey, PublicKey } from "./keys";

export const utils = {
// TODO: review these before 0.5.0
// TODO: remove after 0.5.0
aesEncrypt,
aesDecrypt,
symEncrypt,
Expand Down
62 changes: 0 additions & 62 deletions src/utils/compat.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/utils/elliptic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { secp256k1 } from "@noble/curves/secp256k1";

import { ellipticCurve } from "../config";
import { ETH_PUBLIC_KEY_SIZE, SECRET_KEY_LENGTH } from "../consts";
import { deriveKey } from "./hash";
import { decodeHex } from "./hex";
import { deriveKey } from "./symmetric";

export const isValidPrivateKey = (secret: Uint8Array): boolean =>
// on secp256k1: only key ∈ (0, group order) is valid
Expand Down
Loading

0 comments on commit f07fc96

Please sign in to comment.