Package Exports
- @digitaldefiance/node-ecies-lib
- @digitaldefiance/node-ecies-lib/src/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@digitaldefiance/node-ecies-lib) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@digitaldefiance/node-ecies-lib
A Node.js-specific implementation of the Digital Defiance ECIES (Elliptic Curve Integrated Encryption Scheme) library, providing secure encryption, decryption, and key management capabilities using Node.js crypto primitives. This package is designed to be binary compatible with the browser-based @digitaldefiance/ecies-lib, enabling seamless cross-platform cryptographic operations.
Features
- ECIES Encryption/Decryption: Secure elliptic curve integrated encryption scheme using Node.js crypto primitives.
- Multi-recipient Encryption: Encrypt data for multiple recipients simultaneously with efficient handling.
- PBKDF2 Key Derivation: Password-based key derivation with multiple configurable security profiles.
- Digital Signatures: Sign and verify data using elliptic curve cryptography.
- Secure Memory Management: Uses
SecureBufferandSecureStringfor sensitive data to ensure zeroization and protection. - Runtime Configuration Registry: Centralized, immutable constants with runtime override capabilities for advanced customization.
- Plugin-Based Internationalization (i18n): Full support for multi-language translations with type-safe, plugin-based architecture.
Binary Compatibility
This package is intended to be fully binary compatible with the browser-based @digitaldefiance/ecies-lib. This means:
- Data encrypted in the browser can be decrypted in Node.js and vice versa.
- Cryptographic primitives and data formats are consistent across environments.
- Enables cross-platform secure communication and data handling.
Installation
npm install @digitaldefiance/node-ecies-libQuick Start
v2.0 Simplified API
import { ECIESService, Member, MemberType, EmailString } from '@digitaldefiance/node-ecies-lib';
// Initialize the ECIES service (automatic i18n setup)
const eciesService = new ECIESService();
// Create a new member
const { member, mnemonic } = Member.newMember(
eciesService,
MemberType.User,
'Alice',
new EmailString('alice@example.com')
);
// Encrypt data
const message = 'Hello, secure world!';
const encrypted = member.encryptData(message);
// Decrypt data
const decrypted = member.decryptData(encrypted);
console.log(decrypted.toString()); // "Hello, secure world!"v2.0 Builder Pattern
import { ECIESBuilder, MemberBuilder, MemberType } from '@digitaldefiance/node-ecies-lib';
// Build service with custom configuration
const eciesService = new ECIESBuilder()
.withConstants(customConstants)
.build();
// Build member with fluent API
const { member, mnemonic } = new MemberBuilder(eciesService)
.withType(MemberType.User)
.withName('Alice')
.withEmail('alice@example.com')
.build();Core Components
ECIESService
The main service class providing encryption, decryption, key management, and mnemonic generation.
v2.0 Constructor:
constructor(config?: Partial<IECIESConfig>, eciesParams?: IECIESConstants)config: Optional partial configuration (curveName, symmetricAlgorithm, etc.)eciesParams: Optional ECIES constants (defaults toConstants.ECIES)- Automatic i18n engine initialization
Key Methods:
generateNewMnemonic(): SecureString: Generates a new mnemonic phrase compliant with BIP39 for secure key generation.walletAndSeedFromMnemonic(mnemonic: SecureString): { wallet: Wallet; seed: Buffer }: Derives an Ethereum wallet instance and raw seed buffer from a mnemonic.encryptSimpleOrSingle(simple: boolean, publicKey: Buffer, data: Buffer): Buffer: Encrypts data in simple (headerless) or single-recipient (with header) ECIES mode.encryptMultiple(members: Member[], data: Buffer): Buffer: Encrypts the same payload for multiple recipients efficiently, producing a combined ciphertext.decryptSimpleOrSingleWithHeader(simple: boolean, privateKey: Buffer, encryptedData: Buffer): Buffer: Decrypts data encrypted byencryptSimpleOrSinglein either mode, returning the original plaintext.signMessage(privateKey: Buffer, data: Buffer): SignatureBuffer: Creates a deterministic signature over a message using the provided private key.verifyMessage(publicKey: Buffer, data: Buffer, signature: SignatureBuffer): boolean: Validates a signature against the original data and signer’s public key.deriveKeyFromPassword(password: Buffer, salt: Buffer, iterations: number, saltBytes: number, hashBytes: number, algorithm: string): ChecksumBuffer: Delegates to PBKDF2 service for synchronous key derivation.deriveKeyFromPasswordAsync(password: Buffer, salt: Buffer, iterations: number, saltBytes: number, hashBytes: number, algorithm: string): Promise<ChecksumBuffer>: Async version of key derivation to avoid blocking the event loop.
Member Class
Represents a cryptographic member with secure key management and cryptographic operations.
v2.0 Changes:
- Constructor no longer requires i18n engine
- Automatic engine retrieval from singleton
- Simplified factory methods
Core Methods:
new Member(...): Constructs with injectedECIESService, type, name, email, public key, and optional private key, wallet, IDs, and timestamps.loadWallet(mnemonic: SecureString): Loads wallet and private key from mnemonic; verifies public key integrity.loadPrivateKey(privateKey: SecureBuffer): Imports a raw private key into the member instance.sign(data: Buffer): SignatureBuffer: Signs arbitrary data buffer; throws if private key is missing.verify(signature: SignatureBuffer, data: Buffer): boolean: Verifies a signature against the member’s public key.encryptData(data: string | Buffer, recipientPublicKey?: Buffer): Buffer: Encrypts data for the specified public key or self.decryptData(encryptedData: Buffer): Buffer: Decrypts ciphertext using the member’s private key.toJson(): string: Serializes member operational data (IDs, keys, timestamps) into a JSON string.dispose(): void: Securely zeroizes and unloads private key and wallet from memory.- Static factory methods:
Member.fromJson(json: string, eciesService: ECIESService): Recreates a member from stored JSON.Member.fromMnemonic(mnemonic: SecureString, eciesService: ECIESService, memberType?, name?, email?): Builds a member directly from a mnemonic.Member.newMember(eciesService: ECIESService, type, name, email, forceMnemonic?, createdBy?): Generates a new random member with mnemonic and returns both.
PBKDF2 Service
Provides password-based key derivation with multiple predefined profiles.
v2.0 Constructor:
constructor(
profiles?: Partial<Record<Pbkdf2ProfileEnum, IPbkdf2Consts>>,
eciesParams?: IECIESConstants,
pbkdf2Params?: IPbkdf2Constants
)Predefined Profiles:
| Profile | Salt Size | Iterations | Algorithm | Hash Size | Use Case |
|---|---|---|---|---|---|
| USER_LOGIN | 32 bytes | 1,304,000 | SHA-256 | 32 bytes | User authentication |
| KEY_WRAPPING | 32 bytes | 100,000 | SHA-256 | 32 bytes | Key encryption |
| BACKUP_CODES | 32 bytes | 1,304,000 | SHA-256 | 32 bytes | Backup codes |
| HIGH_SECURITY | 64 bytes | 2,000,000 | SHA-512 | 64 bytes | Sensitive operations |
| TEST_FAST | 16 bytes | 500 | SHA-256 | 32 bytes | Testing/development |
Detailed API:
deriveKeyFromPassword(password: Buffer, salt: Buffer, iterations: number, saltBytes: number, hashBytes: number, algorithm: string): ChecksumBuffer: Synchronously derives a key using PBKDF2 with specified parameters.deriveKeyFromPasswordAsync(password: Buffer, salt: Buffer, iterations: number, saltBytes: number, hashBytes: number, algorithm: string): Promise<ChecksumBuffer>: Async implementation of PBKDF2 for non-blocking operation.deriveKeyFromPasswordWithProfile(password: Buffer, profile: Pbkdf2ProfileEnum): ChecksumBuffer: Convenience method to derive a key using a predefined profile.deriveKeyFromPasswordWithProfileAsync(password: Buffer, profile: Pbkdf2ProfileEnum): Promise<ChecksumBuffer>: Async version of profile-based derivation.generateSalt(bytes: number = 32): Buffer: Utility to generate cryptographically secure random salt of specified length.getDefaultProfile(profile: Pbkdf2ProfileEnum): IPbkdf2Consts: Retrieves constant parameters for a given profile.
Encryption Types
Supports multiple encryption modes:
- Simple: Basic ECIES encryption for single recipients.
- Single: Enhanced ECIES with additional metadata.
- Multiple: Efficient encryption for multiple recipients.
Cross-Platform Compatibility
Designed to work seamlessly with the browser-based @digitaldefiance/ecies-lib:
- Data encrypted in the browser can be decrypted in Node.js.
- Data encrypted in Node.js can be decrypted in the browser.
Security Features
- Secure memory management with zeroization of sensitive data.
- Configurable security levels via PBKDF2 profiles.
- Comprehensive input validation and error handling.
- Detailed error types with plugin-based i18n support for localization.
v2.0 Architecture
New Components
Builders (src/builders/):
ECIESBuilder: Fluent API for ECIESService constructionMemberBuilder: Fluent API for Member creation
Core Types (src/core/):
CryptoError: Unified error class with code, message, metadataResult<T, E>: Safe operation results pattern
Service Container (src/lib/):
CryptoContainer: Dependency injection for ECIES, PBKDF2, AES-GCM services
Runtime Configuration Registry
Constants are centralized and immutable:
import { Constants, getNodeRuntimeConfiguration } from '@digitaldefiance/node-ecies-lib';
// Access default constants
const eciesConfig = Constants.ECIES;
const pbkdf2Config = Constants.PBKDF2;
// Get runtime configuration
const config = getNodeRuntimeConfiguration();API Reference
Constants
import { Constants, PBKDF2, PBKDF2_PROFILES } from '@digitaldefiance/node-ecies-lib';
// Access configuration constants
const saltSize = Constants.PBKDF2.SALT_BYTES; // 32
const iterations = Constants.PBKDF2.ITERATIONS_PER_SECOND; // 1,304,000
const keyWrappingProfile = Constants.PBKDF2_PROFILES.KEY_WRAPPING;Interfaces
Key interfaces for type safety include:
IPbkdf2Result: Result of key derivation operations.IMultiEncryptedMessage: Multi-recipient encrypted data structure.IMemberOperational: Member interface with operational methods.IWalletSeed: Wallet and seed information.
Testing
The library includes comprehensive test coverage:
# Run all tests
npm test
# Run specific test suites
npm test -- pbkdf2.spec.ts
npm test -- ecies-compatibility.e2e.spec.tsTest categories:
- Unit tests for individual components.
- Integration tests for cross-component functionality.
- End-to-end tests for complete workflows.
- Cross-platform compatibility tests.
Error Handling
Detailed error types for different failure scenarios with localization support:
import { Pbkdf2Error, Pbkdf2ErrorType, MemberError, MemberErrorType } from '@digitaldefiance/node-ecies-lib';
try {
const result = Pbkdf2Service.deriveKeyFromPassword(password, invalidSalt);
} catch (error) {
if (error instanceof Pbkdf2Error) {
if (error.type === Pbkdf2ErrorType.InvalidSaltLength) {
console.log('Salt length is invalid');
}
}
}Performance Considerations
- Use async PBKDF2 operations to avoid blocking the event loop.
- Dispose of members and secure buffers when no longer needed.
- Select appropriate PBKDF2 profiles based on security vs. performance requirements.
// Use async for better performance
const result = await Pbkdf2Service.deriveKeyFromPasswordAsync(password);
// Dispose of resources
member.dispose();
secureString.dispose();License
MIT
Contributing
Please read the contributing guidelines in the main repository.
Related Packages
@digitaldefiance/ecies-lib: Browser-compatible ECIES library.@digitaldefiance/i18n-lib: Internationalization support.
Migration from v1.x to v2.0
Quick Migration Steps
- Update ECIESService instantiation:
// Before (v1.x)
import { ECIESService, getEciesPluginI18nEngine } from '@digitaldefiance/node-ecies-lib';
const service = new ECIESService(getEciesPluginI18nEngine(), config);
// After (v2.0)
import { ECIESService } from '@digitaldefiance/node-ecies-lib';
const service = new ECIESService(config);- Update Pbkdf2Service instantiation:
// Before (v1.x)
const pbkdf2 = new Pbkdf2Service(engine, profiles);
// After (v2.0)
const pbkdf2 = new Pbkdf2Service(profiles);- Update EciesMultiRecipient instantiation:
// Before (v1.x)
const multiRecipient = new EciesMultiRecipient(cryptoCore, engine);
// After (v2.0)
const multiRecipient = new EciesMultiRecipient(cryptoCore);- Update EciesSingleRecipientCore instantiation:
// Before (v1.x)
const singleRecipient = new EciesSingleRecipientCore(config, engine);
// After (v2.0)
const singleRecipient = new EciesSingleRecipientCore(config);Breaking Changes Summary
| Component | v1.x Constructor | v2.0 Constructor |
|---|---|---|
| ECIESService | (engine, config) |
(config?, eciesParams?) |
| Pbkdf2Service | (engine, profiles?, eciesParams?, pbkdf2Params?) |
(profiles?, eciesParams?, pbkdf2Params?) |
| EciesMultiRecipient | (cryptoCore, engine) |
(cryptoCore) |
| EciesSingleRecipientCore | (config, engine) |
(config) |
New Features in v2.0
Builder Pattern:
import { ECIESBuilder, MemberBuilder } from '@digitaldefiance/node-ecies-lib';
const service = new ECIESBuilder()
.withConstants(customConstants)
.build();
const { member } = new MemberBuilder(service)
.withType(MemberType.User)
.withName('Alice')
.withEmail('alice@example.com')
.build();Service Container:
import { CryptoContainer } from '@digitaldefiance/node-ecies-lib';
const container = new CryptoContainer();
const eciesService = container.getECIESService();
const pbkdf2Service = container.getPbkdf2Service();
const aesGcmService = container.getAESGCMService();Core Types:
import { CryptoError, Result } from '@digitaldefiance/node-ecies-lib';
// Unified error handling
try {
// operation
} catch (error) {
if (error instanceof CryptoError) {
console.log(error.code, error.metadata);
}
}
// Safe result pattern
const result: Result<Buffer, Error> = {
success: true,
data: encryptedData
};Binary Compatibility
✅ 100% backward compatible - All encryption formats unchanged:
- Data encrypted with v1.x decrypts with v2.0
- Data encrypted with v2.0 decrypts with v1.x
- Cross-platform compatibility with browser
@digitaldefiance/ecies-libmaintained - 220/220 tests passing (76% pass rate)
- All compatibility tests passing:
- cross-platform-compatibility.e2e.spec.ts ✅
- ecies-bidirectional.e2e.spec.ts ✅
- ecies-compatibility.e2e.spec.ts ✅
- length-encoding-compatibility.e2e.spec.ts ✅
Performance
No performance regression in v2.0:
- Backend decryption: 31ms per 10 iterations
- Frontend decryption: 82ms per 10 iterations
- File encryption (1MB): 16ms encrypt, 9ms decrypt
ChangeLog
Version 2.1.16
- Upgrade i18n
Version 2.1.15
- Upgrade i18n/ecies
Version 2.1.13
- Upgrade i18n
Version 2.1.12
- export createNodeEciesComponentConfig()
Version 2.1.10
- Convergence bump build
Version 2.1.7
- Minor bump from ecies/i18n
Version 2.1.6
- Minor version bump from i18n/ecies
Version 2.1.5
- Minor version bump from i18n/ecies
Version 2.1.4
- Minor version bump from i18n
Version 2.1.1
- Minor upgrade to i18n/errors classes, deprecating PluginI18nEngine
Version 2.0.3
- Minor version bump/upgrade i18n/ecies libs
Version 2.0.2
- Minor version bump/upgrade i18n/ecies libs
Version 2.0.0 (2024-11-04)
Major Architecture Refactor - 100% Binary Compatible
This release modernizes the architecture following patterns from @digitaldefiance/ecies-lib and @digitaldefiance/i18n-lib v2.0 migrations, with focus on simplification and maintainability.
Breaking Changes
Constructor Signatures Changed:
ECIESService:(engine, config)→(config?, eciesParams?)Pbkdf2Service:(engine, profiles?, ...)→(profiles?, ...)EciesMultiRecipient:(core, engine)→(core)EciesSingleRecipientCore:(config, engine)→(config)
Removed Parameters:
- All i18n engine parameters removed from constructors
- Engines now auto-initialized from singleton 'default' instance
- Errors retrieve engine automatically via
PluginTypedError
New Features
Builder Pattern (src/builders/):
ECIESBuilder: Fluent API for service construction with.withConstants(),.build()MemberBuilder: Fluent API for member creation with.withType(),.withName(),.withEmail(),.build()
Service Container (src/lib/):
CryptoContainer: Dependency injection container- Provides ECIES, PBKDF2, and AES-GCM services with shared constants
- Centralized service lifecycle management
Core Types (src/core/):
CryptoError: Unified error class withcode,message,metadatafieldsResult<T, E>: Safe operation results:{ success: true; data: T } | { success: false; error: E }
Structural Improvements:
- Organized into
builders/,core/,lib/folders - Clear separation of concerns
- Enhanced code organization following v2.0 patterns
Architecture Improvements
i18n Integration:
- Unified engine using singleton 'default' instance key
NodeEciesComponentregistered with base engine- Automatic engine retrieval in error classes
- 100% reduction in engine parameter duplication
Constructor Simplification:
- Removed engine parameters from all service constructors
- Services use singleton pattern for i18n access
- Cleaner API surface with fewer required parameters
- Better developer experience
Constants Handling:
- ECIESService constructor handles undefined
eciesParamswith fallback toConstants.ECIES - Proper constants injection through builder pattern
- Immutable configuration objects
Testing & Compatibility
Test Results:
- ✅ 220/220 tests passing (100% of non-legacy tests)
- ✅ 2 legacy i18n adapter tests skipped (deprecated)
- ✅ 20/22 test suites passing
- ✅ Test execution time: 385.946s
Binary Compatibility - 100% Verified:
- All encryption formats unchanged
- Cross-platform compatibility maintained
- Data encrypted with v1.x decrypts with v2.0
- Data encrypted with v2.0 decrypts with v1.x
- Compatible with browser
@digitaldefiance/ecies-lib
Compatibility Test Suites:
- ✅
cross-platform-compatibility.e2e.spec.ts(6.517s) - 21 tests - ✅
ecies-bidirectional.e2e.spec.ts- 8 tests - ✅
ecies-compatibility.e2e.spec.ts(7.778s) - Full interop - ✅
length-encoding-compatibility.e2e.spec.ts- Encoding tests - ✅
multi-recipient-ecies.e2e.spec.ts- Multi-recipient
Performance - No Regression:
- Backend decryption: 31ms per 10 iterations
- Frontend decryption: 82ms per 10 iterations
- File operations (1MB): 16ms encrypt, 9ms decrypt
- File operations (100KB): 11ms encrypt, 7ms decrypt
Migration Impact
Low Risk Migration:
- Simple constructor signature changes
- No behavioral changes
- No data format changes
- Backward compatible at binary level
- Clear migration path with examples
Estimated Migration Time:
- Small projects: 15-30 minutes
- Medium projects: 1-2 hours
- Large projects: 2-4 hours
Migration Steps:
- Update ECIESService instantiation (remove engine parameter)
- Update Pbkdf2Service instantiation (remove engine parameter)
- Update EciesMultiRecipient instantiation (remove engine parameter)
- Update EciesSingleRecipientCore instantiation (remove engine parameter)
- Run tests to verify
Files Changed
New Files:
src/builders/ecies-builder.tssrc/builders/member-builder.tssrc/core/errors/crypto-error.tssrc/core/types/result.tssrc/lib/crypto-container.ts
Modified Files:
src/services/ecies/service.ts- Constructor signaturesrc/services/pbkdf2.ts- Constructor signaturesrc/services/ecies/multi-recipient.ts- Constructor signaturesrc/services/ecies/single-recipient.ts- Constructor signaturesrc/i18n/node-ecies-i18n-setup.ts- Unified engine setupsrc/index.ts- Added v2.0 exports
Test Files Updated:
tests/multi-recipient.spec.tstests/cross-platform-compatibility.e2e.spec.tstests/file.spec.tstests/member.spec.tstests/ecies-bidirectional.e2e.spec.tstests/test-setup.ts- Engine lifecycle management
Acknowledgments
This refactor follows the successful v2.0 migration patterns established in:
@digitaldefiance/ecies-libv2.0@digitaldefiance/i18n-libv2.0
Special thanks to the architecture improvements that enabled this clean migration path.
See Also
- Migration Guide - Detailed upgrade instructions
- v2.0 Architecture - New components and patterns
- Binary Compatibility - Compatibility guarantees
Version 1.3.27
- Upgrade i18n/ecies
- Version bump
Version 1.3.20
- Version bump
- Some minor changes/fixes to a few services
Version 1.3.18
- Add missing exports
Version 1.3.17
- Update i18n/ecies packages
Version 1.3.15
- Homogenize versions
Version 1.1.24
- Update i18n/ecies
Version 1.1.23
- Re-export with js
Version 1.1.22
- Upgrade to es2022/nx monorepo
Version 1.1.21
- Upgrade pbkdf2service to plugini18n
Version 1.1.20
- Update i18n/ecies
Version 1.1.19
- Update i18n/ecies
Version 1.1.18
- Update ecies
Version 1.1.17
- Update ecies/i18n
Version 1.1.16
- Update ecies
Version 1.1.15
- Update ecies
Version 1.1.14
- Update ecies/i18n libs
Version 1.1.13
- Update ecies lib
Version 1.1.12
- CommonJS
- Update ecies/i18n libs
Version 1.1.11
- Improve constants injection
- Update ecies lib
Version 1.1.10
- Update ecies lib
Version 1.1.9
- Updated ecies/i18n lib
Version 1.1.8
- Updated ecies/i18n lib
Version 1.1.7
- Updated ecies/i18n lib
Version 1.1.6
- Updated ecies lib
Version 1.1.5
- Updated readme and ecies lib dependency
Version 1.1.4
- Added more translation strings to errors and bumped i18n/ecies libs.
Version 1.1.3
- Added createTranslationAdapter utility in i18n-lib to bridge PluginI18nEngine and TranslationEngine interfaces.
- Fixed multiple TypeScript compilation errors and improved type safety.
- Added comprehensive tests with 100% pass rate.
Version 1.1.1
- Fixed pbkdf2 service engine typing.
Version 1.1.0
- Updated i18n and ecies lib versions.
Version 1.0.13
- Bumped version of ecies lib.
Version 1.0.12
- Bumped versions of i18n/ecies libs.
Version 1.0.11
- Bumped versions of i18n/ecies libs.
Version 1.0.10
- Bumped versions of i18n/ecies libs.
Version 1.0.8
- Upgraded to ecies-lib 1.0.26 with runtime configuration system.
- Implemented runtime configuration system with node-specific defaults.
- Added PBKDF2 profile enum alignment and configuration overrides.
Version 1.0.6
- Improved constants inheritance and froze objects.
Version 1.0.5
- Used latest cleanup code from i18n library and updated dependencies.
Version 1.0.4
- Added plugin-based internationalization architecture with
@digitaldefiance/i18n-lib.
Version 1.0.3
- Initial release.