-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add configuration & refactor utils (#724)
- Loading branch information
Showing
19 changed files
with
787 additions
and
588 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
|
||
# Changelog | ||
|
||
## 0.4.1 | ||
|
||
- Add configuration for more compatibility | ||
- Bump dependencies | ||
|
||
## 0.4.0 | ||
|
||
- Change secp256k1 library to [noble-curves](https://github.com/paulmillr/noble-curves), which is [audited](https://github.com/paulmillr/noble-curves/tree/main/audit) | ||
- Change hash library to [noble-hashes](https://github.com/paulmillr/noble-hashes) | ||
- Change test library to [jest](https://jestjs.io/) | ||
- Bump dependencies | ||
- Drop Node 14 support | ||
|
||
## 0.3.1 ~ 0.3.17 | ||
|
||
- Support Node 18, 20 | ||
- Drop Node 10, 12 support | ||
- Bump dependencies | ||
- Update documentation | ||
- Extract constant variables and rename some parameters | ||
|
||
## 0.3.0 | ||
|
||
- API change: `encrypt/decrypt` now can take both hex `string` and `Buffer` | ||
|
||
## 0.2.0 | ||
|
||
- API change: use `HKDF-sha256` to derive shared keys instead of `sha256` | ||
- Bump dependencies | ||
- Update documentation | ||
|
||
## 0.1.1 ~ 0.1.5 | ||
|
||
- Bump dependencies | ||
- Update documentation | ||
|
||
## 0.1.0 | ||
|
||
- First beta version release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { COMPRESSED_PUBLIC_KEY_SIZE, UNCOMPRESSED_PUBLIC_KEY_SIZE } from "./consts"; | ||
|
||
export type SymmetricAlgorithm = "aes-256-gcm"; | ||
export type NonceLength = 12 | 16 | 24; // bytes | ||
|
||
class Config { | ||
isEphemeralKeyCompressed: boolean = false; | ||
isHkdfKeyCompressed: boolean = false; | ||
symmetricAlgorithm: SymmetricAlgorithm = "aes-256-gcm"; | ||
symmetricNonceLength: NonceLength = 16; | ||
} | ||
|
||
export const ECIES_CONFIG = new Config(); | ||
|
||
export const isEphemeralKeyCompressed = () => ECIES_CONFIG.isEphemeralKeyCompressed; | ||
export const isHkdfKeyCompressed = () => ECIES_CONFIG.isHkdfKeyCompressed; | ||
export const ephemeralKeySize = () => | ||
ECIES_CONFIG.isEphemeralKeyCompressed | ||
? COMPRESSED_PUBLIC_KEY_SIZE | ||
: UNCOMPRESSED_PUBLIC_KEY_SIZE; | ||
export const symmetricAlgorithm = () => ECIES_CONFIG.symmetricAlgorithm; | ||
export const symmetricNonceLength = () => ECIES_CONFIG.symmetricNonceLength; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
export const COMPRESSED_PUBLIC_KEY_SIZE = 33; | ||
export const UNCOMPRESSED_PUBLIC_KEY_SIZE = 65; | ||
export const AES_IV_LENGTH = 16; | ||
export const AES_TAG_LENGTH = 16; | ||
export const AES_IV_PLUS_TAG_LENGTH = AES_IV_LENGTH + AES_TAG_LENGTH; | ||
export const ETH_PUBLIC_KEY_SIZE = 64; | ||
export const SECRET_KEY_LENGTH = 32; | ||
export const ONE = BigInt(1); | ||
export const AEAD_TAG_LENGTH = 16; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { secp256k1 } from "@noble/curves/secp256k1"; | ||
import { randomBytes } from "crypto"; | ||
|
||
import { SECRET_KEY_LENGTH } from "../consts"; | ||
|
||
export function isValidPrivateKey(secret: Buffer) { | ||
return secp256k1.utils.isValidPrivateKey(secret); | ||
} | ||
|
||
export function getValidSecret(): Buffer { | ||
let key: Buffer; | ||
do { | ||
key = randomBytes(SECRET_KEY_LENGTH); | ||
} while (!isValidPrivateKey(key)); | ||
return key; | ||
} | ||
|
||
export function getPublicKey(secret: Buffer): Buffer { | ||
return Buffer.from(secp256k1.getPublicKey(secret)); | ||
} | ||
|
||
export function getSharedPoint( | ||
skRaw: Buffer | bigint, | ||
pkRaw: Buffer, | ||
compressed: boolean | ||
): Buffer { | ||
return Buffer.from(secp256k1.getSharedSecret(skRaw, pkRaw, compressed)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export function remove0x(hex: string): string { | ||
if (hex.startsWith("0x") || hex.startsWith("0X")) { | ||
return hex.slice(2); | ||
} | ||
return hex; | ||
} | ||
|
||
export function decodeHex(hex: string): Buffer { | ||
return Buffer.from(remove0x(hex), "hex"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from "./elliptic"; | ||
export * from "./hex"; | ||
export * from "./symmetric"; |
Oops, something went wrong.