Skip to content

Commit 1f6c23a

Browse files
authored
Feat/esm (#1)
* esm-only migration
1 parent fd09c76 commit 1f6c23a

21 files changed

+914
-3368
lines changed

.github/actions/setup/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ runs:
77
- uses: actions/setup-node@v4
88
with:
99
cache: "npm"
10-
node-version: 18.12.x
10+
node-version: 20.19.x
1111
registry-url: "https://registry.npmjs.org"
1212
scope: '@zeroledger'
1313
always-auth: 'true'

.github/workflows/quality-gate.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,12 @@ jobs:
2424
- uses: ./.github/actions/setup
2525
- name: Run unit tests
2626
run: npm run test
27+
28+
test-build:
29+
needs: lint
30+
runs-on: ubuntu-latest
31+
steps:
32+
- uses: actions/checkout@v4
33+
- uses: ./.github/actions/setup
34+
- name: Run build tests
35+
run: npm run test:build

README.md

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
[![Quality gate](https://github.com/zeroledger/vycrypt/actions/workflows/quality-gate.yml/badge.svg)](https://github.com/zeroledger/vycrypt/actions/workflows/quality-gate.yml)
44

5-
6-
7-
TypeScript Development Kit for ZeroLedger Protocol - A comprehensive cryptographic library for stealth addresses and ECDH encryption.
5+
Crypto primitives for ZeroLedger Protocol - A comprehensive cryptographic library for stealth addresses and ECDH encryption. Pure ESM, optimized for modern JavaScript environments.
86

97
*Warning*: Software provided as is and has not passed any security checks and reviews.
108

@@ -29,30 +27,25 @@ TypeScript Development Kit for ZeroLedger Protocol - A comprehensive cryptograph
2927
npm install @zeroledger/vycrypt
3028
```
3129

32-
## Module Formats
33-
34-
This library supports both **CommonJS** and **ES Modules** formats for maximum compatibility:
30+
## Module Format
3531

36-
- **CommonJS**: For Node.js environments and bundlers that prefer CommonJS
37-
- **ES Modules**: For modern bundlers and environments that support ES modules
32+
This library is **pure ESM** (ES Modules) and requires **Node.js 20.19.0 or later**.
3833

39-
The library automatically provides the appropriate format based on your import method:
34+
**Import the library:**
4035

4136
```typescript
42-
// ES Modules (recommended for modern projects)
43-
import { encrypt, decrypt } from "@zeroledger/vycrypt";
44-
45-
// CommonJS (for legacy Node.js or specific bundler requirements)
46-
const { encrypt, decrypt } = require("@zeroledger/vycrypt");
37+
import { encrypt, decrypt } from "@zeroledger/vycrypt/crypt.js";
38+
import { createStealth, deriveStealthAccount } from "@zeroledger/vycrypt/stealth.js";
4739
```
4840

4941
### Build Output
5042

51-
The library builds to two directories:
52-
- `cjs/` - CommonJS format with `.js` files
53-
- `esm/` - ES Modules format with `.js` files and `package.json`
43+
The library builds directly to the root directory with ESM format:
44+
- `*.js` - ES Module JavaScript files
45+
- `*.d.ts` - TypeScript declaration files
46+
- `stealth/` - Stealth address modules
5447

55-
Both formats include TypeScript declaration files (`.d.ts`) for full type support.
48+
All files include source maps (`.js.map`, `.d.ts.map`) for debugging.
5649

5750
## API Reference
5851

@@ -119,7 +112,7 @@ Multiplies a private key by a scalar value.
119112
### Basic Encryption/Decryption
120113

121114
```typescript
122-
import { encrypt, decrypt } from "@zeroledger/vycrypt";
115+
import { encrypt, decrypt } from "@zeroledger/vycrypt/crypt.js";
123116
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
124117

125118
// Generate key pair
@@ -138,7 +131,7 @@ console.log(decryptedData); // "Hello, World!"
138131
### Stealth Address Creation
139132

140133
```typescript
141-
import { createStealth, deriveStealthAccount } from "@zeroledger/vycrypt";
134+
import { createStealth, deriveStealthAccount } from "@zeroledger/vycrypt/stealth.js";
142135
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
143136
import { toHex } from "viem";
144137

@@ -158,7 +151,7 @@ console.log("Derived Address:", account.address); // Same as stealthAddress
158151
### Elliptic Curve Operations
159152

160153
```typescript
161-
import { mulPublicKey, mulPrivateKey } from "@zeroledger/vycrypt";
154+
import { mulPublicKey, mulPrivateKey } from "@zeroledger/vycrypt/stealth.js";
162155
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
163156

164157
const privateKey = generatePrivateKey();
@@ -179,7 +172,7 @@ console.log(newAccount.publicKey === newPublicKey); // true
179172
### Advanced: Encrypting Large Data
180173

181174
```typescript
182-
import { encrypt, decrypt } from "@zeroledger/vycrypt";
175+
import { encrypt, decrypt } from "@zeroledger/vycrypt/crypt.js";
183176
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
184177

185178
const privKey = generatePrivateKey();
@@ -250,12 +243,25 @@ Run tests with:
250243
npm test
251244
```
252245

246+
Validate build output and ESM imports:
247+
```bash
248+
npm run test:build
249+
```
250+
251+
This command:
252+
1. Builds the library
253+
2. Validates all expected files are created
254+
3. Verifies built modules can be imported as ESM
255+
4. Confirms the API works as documented
256+
253257
## Dependencies
254258

255-
- **@noble/curves**: For secp256k1 elliptic curve operations
256-
- **@noble/ciphers**: For AES-256-GCM encryption
257-
- **@noble/hashes**: For SHA-256 hashing
258-
- **viem**: For Ethereum-compatible utilities and types
259+
- **@noble/ciphers** (v2.0.1): AES-256-GCM authenticated encryption
260+
- **viem** (v2.38.6): Ethereum-compatible utilities, types, and hashing (SHA-256)
261+
262+
**Note:** Viem internally uses and re-exports `@noble/curves` (secp256k1) and `@noble/hashes`, ensuring compatibility across the ecosystem.
263+
264+
All dependencies are ESM-compatible and actively maintained.
259265

260266
## Contributing
261267

@@ -278,16 +284,16 @@ npm test
278284

279285
### Building
280286

281-
To build both CommonJS and ES Module versions:
287+
To build the ESM output:
282288

283289
```bash
284290
npm run build
285291
```
286292

287293
This creates:
288-
- `cjs/` directory with CommonJS files
289-
- `esm/` directory with ES Module files
290-
- TypeScript declaration files (`.d.ts`) for both formats
294+
- `*.js` files in the root directory (ESM format)
295+
- `stealth/` directory with stealth modules
296+
- TypeScript declaration files (`.d.ts`) and source maps
291297

292298
### Type Checking
293299

esm/package.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

jest.config.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
import type { Config } from "jest";
22

33
const config: Config = {
4+
preset: "ts-jest/presets/default-esm",
5+
extensionsToTreatAsEsm: [".ts"],
46
verbose: true,
57
moduleFileExtensions: ["js", "json", "ts"],
6-
testRegex: ".*\\.spec\\.ts$",
8+
testMatch: ["**/test/**/*.spec.ts", "!**/test/build-output.spec.ts"],
79
transform: {
8-
"^.+\\.(t|j)s$": "@swc/jest",
10+
"^.+\\.ts$": [
11+
"ts-jest",
12+
{
13+
useESM: true,
14+
tsconfig: {
15+
module: "ES2022",
16+
target: "ES2022",
17+
},
18+
},
19+
],
920
},
10-
collectCoverageFrom: ["<rootDir>/src/**/*.ts", "!<rootDir>/**/*.module.ts"],
11-
coveragePathIgnorePatterns: [
12-
"/node_modules/",
13-
"<rootDir>/src/(.+)/dto/(.+)",
14-
"<rootDir>/src/(.+)/(.+).module.ts",
15-
"<rootDir>/src/(.+).module.ts",
16-
"<rootDir>/src/main.ts",
17-
"<rootDir>/src/run.ts",
18-
"<rootDir>/src/config/",
19-
"<rootDir>/src/common/db/migrations/",
20-
],
21+
moduleNameMapper: {
22+
"^(\\.{1,2}/.*)\\.js$": "$1",
23+
},
24+
collectCoverageFrom: ["<rootDir>/src/**/*.ts"],
2125
coverageDirectory: "<rootDir>/coverage",
2226
testEnvironment: "node",
23-
modulePaths: ["<rootDir>", "<rootDir>/src"],
2427
};
2528

2629
export default config;

0 commit comments

Comments
 (0)