Skip to content

Commit

Permalink
Add PublicKey.toBytes (#814)
Browse files Browse the repository at this point in the history
  • Loading branch information
kigawas authored Nov 12, 2024
1 parent a2eaacb commit 8bcce34
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 145 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@

# Changelog

## 0.4.12

- Add `PublicKey.toBytes` and deprecate `PublicKey.compressed` and `PublicKey.uncompressed`
- Save uncompressed public key data for secp256k1

## 0.4.11

- Revamp encapsulate/decapsulate
Expand Down
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { PrivateKey, decrypt, encrypt } from "eciesjs";

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

Expand All @@ -42,16 +42,18 @@ See [Configuration](#configuration) to control with more granularity.

### Browser

This library is browser-friendly, check the [`example/browser`](./example/browser) directory for details. Currently it's necessary to polyfill `Buffer` for backward compatibility. From v0.5.0, it can run in browsers as is.
This library is browser-friendly, check the [`example/browser`](./example/browser) directory for details. The online demo is hosted [here](https://js-demo.ecies.org/).

Currently it's necessary to polyfill `Buffer` for backward compatibility. From v0.5.0, it can run in browsers as is.

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

### Bun/Deno

For bun/deno, see [`example/runtime`](./example/runtime). There are some limitations currently:
For bun/deno, see [`example/runtime`](./example/runtime). There are some limitations currently, mentioned in [`@ecies/ciphers`](https://github.com/ecies/js-ciphers#known-limitations):

- `xchacha20` does not work on bun
- Only `aes-256-gcm` with 12 bytes nonce works on deno
- `node:crypto`'s `xchacha20` does not work on bun (pure JS implementation is used instead)
- `aes-256-gcm` only works with 12 bytes nonce on deno (deno is not handling package exports correctly)

### React Native

Expand Down Expand Up @@ -85,7 +87,7 @@ Returns: **Buffer**
static fromHex(hex: string): PrivateKey;
constructor(secret?: Uint8Array);
toHex(): string;
encapsulate(pk: PublicKey): Uint8Array;
encapsulate(pk: PublicKey, compressed?: boolean): Uint8Array;
multiply(pk: PublicKey, compressed?: boolean): Uint8Array;
equals(other: PrivateKey): boolean;
```
Expand All @@ -95,7 +97,6 @@ equals(other: PrivateKey): boolean;
```typescript
get secret(): Buffer;
readonly publicKey: PublicKey;
private readonly data;
```

### `PublicKey`
Expand All @@ -105,17 +106,19 @@ private readonly data;
```typescript
static fromHex(hex: string): PublicKey;
constructor(data: Uint8Array);
toBytes(compressed?: boolean): Uint8Array;
toHex(compressed?: boolean): string;
decapsulate(sk: PrivateKey): Uint8Array;
decapsulate(sk: PrivateKey, compressed?: boolean): Uint8Array;
equals(other: PublicKey): boolean;
```

- Properties

```typescript
/** @deprecated - use `PublicKey.toBytes(false)` instead. You may also need `Buffer.from`. */
get uncompressed(): Buffer;
/** @deprecated - use `PublicKey.toBytes()` instead. You may also need `Buffer.from`. */
get compressed(): Buffer;
private readonly data;
```

## Configuration
Expand Down
2 changes: 1 addition & 1 deletion example/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"eciesjs": "file:../.."
},
"devDependencies": {
"vite": "6.0.0-beta.5",
"vite": "6.0.0-beta.9",
"vite-bundle-visualizer": "^1.2.1"
}
}
2 changes: 1 addition & 1 deletion example/runtime/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ ECIES_CONFIG.symmetricNonceLength = 12;

const sk = new PrivateKey();
const data = Buffer.from("hello world🌍");
const decrypted = decrypt(sk.secret, encrypt(sk.publicKey.compressed, data));
const decrypted = decrypt(sk.secret, encrypt(sk.publicKey.toBytes(), data));
console.log(Buffer.from(decrypted).toString());
2 changes: 1 addition & 1 deletion 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.11",
"version": "0.4.12",
"engines": {
"node": ">=16",
"bun": ">=1",
Expand Down
Loading

0 comments on commit 8bcce34

Please sign in to comment.