11# ethereum-cryptography
22
3- [ ![ npm version] [ 1 ]] [ 2 ] [ ![ Travis CI ] [ 3 ]] [ 4 ] [ ![ license ] [ 5 ]] [ 6 ] [ ![ Types ] [ 7 ]] [ 8 ]
3+ [ ![ npm version] [ 1 ]] [ 2 ] [ ![ license ] [ 3 ]] [ 4 ]
44
5- All pure-js cryptographic primitives normally used when
6- developing Javascript / TypeScript applications and tools for Ethereum.
5+ Audited pure JS library containing all Ethereum-related cryptographic primitives.
6+
7+ Included algorithms:
8+
9+ * [ Hashes: SHA256, keccak-256, RIPEMD160, BLAKE2b] ( #hashes-sha256-keccak-256-ripemd160-blake2b )
10+ * [ KDFs: PBKDF2, Scrypt] ( #kdfs-pbkdf2-scrypt )
11+ * [ CSPRNG (Cryptographically Secure Pseudorandom Number Generator)] ( #csprng-cryptographically-strong-pseudorandom-number-generator )
12+ * [ secp256k1 elliptic curve] ( #secp256k1-curve )
13+ * [ BIP32 HD Keygen] ( #bip32-hd-keygen )
14+ * [ BIP39 Mnemonic phrases] ( #bip39-mnemonic-seed-phrase )
15+ * [ AES Encryption] ( #aes-encryption )
716
817** April 2023 update:** v2.0 is out, switching
918[ noble-secp256k1] ( https://github.com/paulmillr/noble-secp256k1 ) to
@@ -19,16 +28,6 @@ There have been no other changes. Check out [Upgrading](#upgrading).
1928- Check out the article about it: [ A safer, smaller, and faster Ethereum cryptography stack] ( https://medium.com/nomic-labs-blog/a-safer-smaller-and-faster-ethereum-cryptography-stack-5eeb47f62d79 )
2029- Take a glance at the [ Upgrading] ( #upgrading ) section for breaking changes: there are almost none
2130
22- The cryptographic primitives included are:
23-
24- * [ Hashes: SHA256, keccak-256, RIPEMD160, BLAKE2b] ( #hashes-sha256-keccak-256-ripemd160-blake2b )
25- * [ KDFs: PBKDF2, Scrypt] ( #kdfs-pbkdf2-scrypt )
26- * [ CSPRNG (Cryptographically strong pseudorandom number generator)] ( #csprng-cryptographically-strong-pseudorandom-number-generator )
27- * [ secp256k1 curve] ( #secp256k1-curve )
28- * [ BIP32 HD Keygen] ( #bip32-hd-keygen )
29- * [ BIP39 Mnemonic phrases] ( #bip39-mnemonic-seed-phrase )
30- * [ AES Encryption] ( #aes-encryption )
31-
3231## Usage
3332
3433Use NPM / Yarn in node.js / browser:
@@ -55,31 +54,31 @@ tree-shaking, but the possibility of it not working properly on one of
5554
5655``` js
5756// Hashes
58- const { sha256 } = require ( " ethereum-cryptography/sha256" ) ;
59- const { keccak256 } = require ( " ethereum-cryptography/keccak" ) ;
60- const { ripemd160 } = require ( " ethereum-cryptography/ripemd160" ) ;
61- const { blake2b } = require ( " ethereum-cryptography/blake2b" ) ;
57+ import { sha256 } from " ethereum-cryptography/sha256.js " ;
58+ import { keccak256 } from " ethereum-cryptography/keccak.js " ;
59+ import { ripemd160 } from " ethereum-cryptography/ripemd160.js " ;
60+ import { blake2b } from " ethereum-cryptography/blake2b.js " ;
6261
6362// KDFs
64- const { pbkdf2Sync } = require ( " ethereum-cryptography/pbkdf2" ) ;
65- const { scryptSync } = require ( " ethereum-cryptography/scrypt" ) ;
63+ import { pbkdf2Sync } from " ethereum-cryptography/pbkdf2.js " ;
64+ import { scryptSync } from " ethereum-cryptography/scrypt.js " ;
6665
6766// Random
68- const { getRandomBytesSync } = require ( " ethereum-cryptography/random" ) ;
67+ import { getRandomBytesSync } from " ethereum-cryptography/random.js " ;
6968
7069// AES encryption
71- const { encrypt } = require ( " ethereum-cryptography/aes" ) ;
70+ import { encrypt } from " ethereum-cryptography/aes.js " ;
7271
7372// secp256k1 elliptic curve operations
74- const { createPrivateKeySync , ecdsaSign } = require ( " ethereum-cryptography/secp256k1" ) ;
73+ import { secp256k1 } from " ethereum-cryptography/secp256k1.js " ;
7574
7675// BIP32 HD Keygen, BIP39 Mnemonic Phrases
77- const { HDKey } = require ( " ethereum-cryptography/hdkey" ) ;
78- const { generateMnemonic } = require ( " ethereum-cryptography/bip39" ) ;
79- const { wordlist } = require ( " ethereum-cryptography/bip39/wordlists/english" ) ;
76+ import { HDKey } from " ethereum-cryptography/hdkey.js " ;
77+ import { generateMnemonic } from " ethereum-cryptography/bip39/index.js " ;
78+ import { wordlist } from " ethereum-cryptography/bip39/wordlists/english.js " ;
8079
8180// utilities
82- const { hexToBytes , toHex , utf8ToBytes } = require ( " ethereum-cryptography/utils" ) ;
81+ import { hexToBytes , toHex , utf8ToBytes } from " ethereum-cryptography/utils.js " ;
8382```
8483
8584## Hashes: SHA256, keccak-256, RIPEMD160, BLAKE2b
@@ -100,20 +99,20 @@ and `keccak512`)
10099- BLAKE2b
101100
102101``` js
103- const { sha256 } = require ( " ethereum-cryptography/sha256" ) ;
104- const { sha512 } = require ( " ethereum-cryptography/sha512" ) ;
105- const { keccak256 , keccak224 , keccak384 , keccak512 } = require ( " ethereum-cryptography/keccak" ) ;
106- const { ripemd160 } = require ( " ethereum-cryptography/ripemd160" ) ;
107- const { blake2b } = require ( " ethereum-cryptography/blake2b" ) ;
102+ import { sha256 } from " ethereum-cryptography/sha256.js " ;
103+ import { sha512 } from " ethereum-cryptography/sha512.js " ;
104+ import { keccak256 , keccak224 , keccak384 , keccak512 } from " ethereum-cryptography/keccak.js " ;
105+ import { ripemd160 } from " ethereum-cryptography/ripemd160.js " ;
106+ import { blake2b } from " ethereum-cryptography/blake2b.js " ;
108107
109108sha256 (Uint8Array .from ([1 , 2 , 3 ]))
110109
111110// Can be used with strings
112- const { utf8ToBytes } = require ( " ethereum-cryptography/utils" ) ;
111+ import { utf8ToBytes } from " ethereum-cryptography/utils.js " ;
113112sha256 (utf8ToBytes (" abc" ))
114113
115114// If you need hex
116- const { bytesToHex as toHex } = require ( " ethereum-cryptography/utils" ) ;
115+ import { bytesToHex as toHex } from " ethereum-cryptography/utils.js " ;
117116toHex (sha256 (utf8ToBytes (" abc" )))
118117```
119118
@@ -141,15 +140,15 @@ Encoding passwords is a frequent source of errors. Please read
141140before using these submodules.
142141
143142``` js
144- const { pbkdf2 } = require ( " ethereum-cryptography/pbkdf2" ) ;
145- const { utf8ToBytes } = require ( " ethereum-cryptography/utils" ) ;
143+ import { pbkdf2 } from " ethereum-cryptography/pbkdf2.js " ;
144+ import { utf8ToBytes } from " ethereum-cryptography/utils.js " ;
146145// Pass Uint8Array, or convert strings to Uint8Array
147146console .log (await pbkdf2 (utf8ToBytes (" password" ), utf8ToBytes (" salt" ), 131072 , 32 , " sha256" ));
148147```
149148
150149``` js
151- const { scrypt } = require ( " ethereum-cryptography/scrypt" ) ;
152- const { utf8ToBytes } = require ( " ethereum-cryptography/utils" ) ;
150+ import { scrypt } from " ethereum-cryptography/scrypt.js " ;
151+ import { utf8ToBytes } from " ethereum-cryptography/utils.js " ;
153152console .log (await scrypt (utf8ToBytes (" password" ), utf8ToBytes (" salt" ), 262144 , 8 , 1 , 32 ));
154153```
155154
@@ -166,7 +165,7 @@ pseudo-random data in synchronous and asynchronous ways.
166165Backed by [ ` crypto.getRandomValues ` ] ( https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues ) in browser and by [ ` crypto.randomBytes ` ] ( https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback ) in node.js. If backends are somehow not available, the module would throw an error and won't work, as keeping them working would be insecure.
167166
168167``` js
169- const { getRandomBytesSync } = require ( " ethereum-cryptography/random" ) ;
168+ import { getRandomBytesSync } from " ethereum-cryptography/random.js " ;
170169console .log (getRandomBytesSync (32 ));
171170```
172171
@@ -188,7 +187,7 @@ certain characteristics. If this is not the case, the security of secp256k1 is
188187compromised. We strongly recommend using ` utils.randomPrivateKey() ` to generate them.
189188
190189``` js
191- const { secp256k1 } = require ( " ethereum-cryptography/secp256k1" ) ;
190+ import { secp256k1 } from " ethereum-cryptography/secp256k1.js " ;
192191(async () => {
193192 // You pass either a hex string, or Uint8Array
194193 const privateKey = " 6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e" ;
@@ -199,7 +198,9 @@ const {secp256k1} = require("ethereum-cryptography/secp256k1");
199198})();
200199```
201200
202- Note: if you've been using ethereum-cryptography v0.1, it had different API. We're providing a compatibility layer for users who want to upgrade without hassle. Check out [ the legacy documentation] ( #legacy-secp256k1-compatibility-layer ) .
201+ We're also providing a compatibility layer for users who want to upgrade
202+ from ` tiny-secp256k1 ` or ` secp256k1 ` modules without hassle.
203+ Check out [ secp256k1 compatibility layer] ( #legacy-secp256k1-compatibility-layer ) .
203204
204205## BIP32 HD Keygen
205206
@@ -209,7 +210,7 @@ Also available as standalone package [scure-bip32](https://github.com/paulmillr/
209210This module exports a single class ` HDKey ` , which should be used like this:
210211
211212``` ts
212- const { HDKey } = require ( " ethereum-cryptography/hdkey" ) ;
213+ import { HDKey } from " ethereum-cryptography/hdkey.js " ;
213214const hdkey1 = HDKey .fromMasterSeed (seed );
214215const hdkey2 = HDKey .fromExtendedKey (base58key );
215216const hdkey3 = HDKey .fromJSON ({ xpriv: string });
@@ -287,8 +288,8 @@ recovery phrases according to [BIP39](https://github.com/bitcoin/bips/blob/maste
287288Also available as standalone package [ scure-bip39] ( https://github.com/paulmillr/scure-bip39 ) .
288289
289290``` js
290- const { generateMnemonic } = require ( " ethereum-cryptography/bip39" ) ;
291- const { wordlist } = require ( " ethereum-cryptography/bip39/wordlists/english" ) ;
291+ import { generateMnemonic } from " ethereum-cryptography/bip39/index.js " ;
292+ import { wordlist } from " ethereum-cryptography/bip39/wordlists/english.js " ;
292293console .log (generateMnemonic (wordlist));
293294```
294295
@@ -385,8 +386,8 @@ exception.
385386### Example usage
386387
387388``` js
388- const { encrypt } = require ( " ethereum-cryptography/aes" ) ;
389- const { hexToBytes , utf8ToBytes } = require ( " ethereum-cryptography/utils" ) ;
389+ import { encrypt } from " ethereum-cryptography/aes.js " ;
390+ import { hexToBytes , utf8ToBytes } from " ethereum-cryptography/utils.js " ;
390391
391392console .log (
392393 encrypt (
@@ -420,15 +421,13 @@ These can be used by setting your `plugins` array like this:
420421
421422## Legacy secp256k1 compatibility layer
422423
423- ** Note:** consider using ` secp256k1 ` instead;
424- This module is only for users who upgraded
425- from ethereum-cryptography v0.1. It could be removed in the future,
426- but we're keeping it around for now, for backwards-compatibility.
424+ ** Warning:** use ` secp256k1 ` instead. This module is only for users who upgraded
425+ from ethereum-cryptography v0.1. It could be removed in the future.
427426
428427The API of ` secp256k1-compat ` is the same as [ secp256k1-node] ( https://github.com/cryptocoinjs/secp256k1-node ) :
429428
430429``` js
431- const { createPrivateKeySync , ecdsaSign } = require ( " ethereum-cryptography/secp256k1-compat" ) ;
430+ import { createPrivateKeySync , ecdsaSign } from " ethereum-cryptography/secp256k1-compat" ;
432431const msgHash = Uint8Array .from (
433432 " 82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28" ,
434433 " hex"
@@ -460,7 +459,7 @@ Upgrading from 1.0 to 2.0:
460459 to [ upgrading section from curves README] ( https://github.com/paulmillr/noble-curves#upgrading ) .
461460 Main changes to keep in mind: a) ` sign ` now returns ` Signature ` instance
462461 b) ` recoverPublicKey ` got moved onto a ` Signature ` instance
463- 2 . node.js 14 and older support was dropped. Upgrade to node.js 16 or later.
462+ 2 . node.js 14 and older support was dropped. Upgrade to node.js 16 or later.
464463
465464Upgrading from 0.1 to 1.0: ** Same functionality** , all old APIs remain the same except for the breaking changes:
466465
@@ -471,14 +470,14 @@ browsers and node.js.
471470which is Chrome 67+, Edge 79+, Firefox 68+, Safari 14+, node.js 10+. If you need to support older runtimes, use ` ethereum-cryptography@0.1 `
4724713 . If you've used ` secp256k1 ` , [ rename it to ` secp256k1-compat ` ] ( #legacy-secp256k1-compatibility-layer )
473472
474- ```
475- const { sha256 } = require( "ethereum-cryptography/sha256") ;
473+ ``` js
474+ import { sha256 } from " ethereum-cryptography/sha256.js " ;
476475
477476// Old usage
478477const hasho = sha256 (Buffer .from (" string" , " utf8" )).toString (" hex" );
479478
480479// New usage
481- const { toHex } = require( "ethereum-cryptography/utils") ;
480+ import { toHex } from " ethereum-cryptography/utils.js " ;
482481const hashn = toHex (sha256 (" string" ));
483482
484483// If you have `Buffer` module and want to preserve it:
@@ -505,9 +504,5 @@ Copyright (c) 2018 cryptocoinjs
505504
506505[ 1 ] : https://img.shields.io/npm/v/ethereum-cryptography.svg
507506[ 2 ] : https://www.npmjs.com/package/ethereum-cryptography
508- [ 3 ] : https://img.shields.io/travis/ethereum/js-ethereum-cryptography/master.svg?label=Travis%20CI
509- [ 4 ] : https://travis-ci.org/ethereum/js-ethereum-cryptography
510- [ 5 ] : https://img.shields.io/npm/l/ethereum-cryptography
511- [ 6 ] : https://github.com/ethereum/js-ethereum-cryptography/blob/master/packages/ethereum-cryptography/LICENSE
512- [ 7 ] : https://img.shields.io/npm/types/ethereum-cryptography.svg
513- [ 8 ] : https://www.npmjs.com/package/ethereum-cryptography
507+ [ 3 ] : https://img.shields.io/npm/l/ethereum-cryptography
508+ [ 4 ] : https://github.com/ethereum/js-ethereum-cryptography/blob/master/packages/ethereum-cryptography/LICENSE
0 commit comments