1
1
# ethereum-cryptography
2
2
3
- [ ![ npm version] [ 1 ]] [ 2 ] [ ![ Travis CI ] [ 3 ]] [ 4 ] [ ![ license ] [ 5 ]] [ 6 ] [ ![ Types ] [ 7 ]] [ 8 ]
3
+ [ ![ npm version] [ 1 ]] [ 2 ] [ ![ license ] [ 3 ]] [ 4 ]
4
4
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 )
7
16
8
17
** April 2023 update:** v2.0 is out, switching
9
18
[ noble-secp256k1] ( https://github.com/paulmillr/noble-secp256k1 ) to
@@ -19,16 +28,6 @@ There have been no other changes. Check out [Upgrading](#upgrading).
19
28
- 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 )
20
29
- Take a glance at the [ Upgrading] ( #upgrading ) section for breaking changes: there are almost none
21
30
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
-
32
31
## Usage
33
32
34
33
Use NPM / Yarn in node.js / browser:
@@ -55,31 +54,31 @@ tree-shaking, but the possibility of it not working properly on one of
55
54
56
55
``` js
57
56
// 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 " ;
62
61
63
62
// 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 " ;
66
65
67
66
// Random
68
- const { getRandomBytesSync } = require ( " ethereum-cryptography/random" ) ;
67
+ import { getRandomBytesSync } from " ethereum-cryptography/random.js " ;
69
68
70
69
// AES encryption
71
- const { encrypt } = require ( " ethereum-cryptography/aes" ) ;
70
+ import { encrypt } from " ethereum-cryptography/aes.js " ;
72
71
73
72
// secp256k1 elliptic curve operations
74
- const { createPrivateKeySync , ecdsaSign } = require ( " ethereum-cryptography/secp256k1" ) ;
73
+ import { secp256k1 } from " ethereum-cryptography/secp256k1.js " ;
75
74
76
75
// 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 " ;
80
79
81
80
// utilities
82
- const { hexToBytes , toHex , utf8ToBytes } = require ( " ethereum-cryptography/utils" ) ;
81
+ import { hexToBytes , toHex , utf8ToBytes } from " ethereum-cryptography/utils.js " ;
83
82
```
84
83
85
84
## Hashes: SHA256, keccak-256, RIPEMD160, BLAKE2b
@@ -100,20 +99,20 @@ and `keccak512`)
100
99
- BLAKE2b
101
100
102
101
``` 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 " ;
108
107
109
108
sha256 (Uint8Array .from ([1 , 2 , 3 ]))
110
109
111
110
// Can be used with strings
112
- const { utf8ToBytes } = require ( " ethereum-cryptography/utils" ) ;
111
+ import { utf8ToBytes } from " ethereum-cryptography/utils.js " ;
113
112
sha256 (utf8ToBytes (" abc" ))
114
113
115
114
// If you need hex
116
- const { bytesToHex as toHex } = require ( " ethereum-cryptography/utils" ) ;
115
+ import { bytesToHex as toHex } from " ethereum-cryptography/utils.js " ;
117
116
toHex (sha256 (utf8ToBytes (" abc" )))
118
117
```
119
118
@@ -141,15 +140,15 @@ Encoding passwords is a frequent source of errors. Please read
141
140
before using these submodules.
142
141
143
142
``` 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 " ;
146
145
// Pass Uint8Array, or convert strings to Uint8Array
147
146
console .log (await pbkdf2 (utf8ToBytes (" password" ), utf8ToBytes (" salt" ), 131072 , 32 , " sha256" ));
148
147
```
149
148
150
149
``` 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 " ;
153
152
console .log (await scrypt (utf8ToBytes (" password" ), utf8ToBytes (" salt" ), 262144 , 8 , 1 , 32 ));
154
153
```
155
154
@@ -166,7 +165,7 @@ pseudo-random data in synchronous and asynchronous ways.
166
165
Backed 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.
167
166
168
167
``` js
169
- const { getRandomBytesSync } = require ( " ethereum-cryptography/random" ) ;
168
+ import { getRandomBytesSync } from " ethereum-cryptography/random.js " ;
170
169
console .log (getRandomBytesSync (32 ));
171
170
```
172
171
@@ -188,7 +187,7 @@ certain characteristics. If this is not the case, the security of secp256k1 is
188
187
compromised. We strongly recommend using ` utils.randomPrivateKey() ` to generate them.
189
188
190
189
``` js
191
- const { secp256k1 } = require ( " ethereum-cryptography/secp256k1" ) ;
190
+ import { secp256k1 } from " ethereum-cryptography/secp256k1.js " ;
192
191
(async () => {
193
192
// You pass either a hex string, or Uint8Array
194
193
const privateKey = " 6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e" ;
@@ -199,7 +198,9 @@ const {secp256k1} = require("ethereum-cryptography/secp256k1");
199
198
})();
200
199
```
201
200
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 ) .
203
204
204
205
## BIP32 HD Keygen
205
206
@@ -209,7 +210,7 @@ Also available as standalone package [scure-bip32](https://github.com/paulmillr/
209
210
This module exports a single class ` HDKey ` , which should be used like this:
210
211
211
212
``` ts
212
- const { HDKey } = require ( " ethereum-cryptography/hdkey" ) ;
213
+ import { HDKey } from " ethereum-cryptography/hdkey.js " ;
213
214
const hdkey1 = HDKey .fromMasterSeed (seed );
214
215
const hdkey2 = HDKey .fromExtendedKey (base58key );
215
216
const hdkey3 = HDKey .fromJSON ({ xpriv: string });
@@ -287,8 +288,8 @@ recovery phrases according to [BIP39](https://github.com/bitcoin/bips/blob/maste
287
288
Also available as standalone package [ scure-bip39] ( https://github.com/paulmillr/scure-bip39 ) .
288
289
289
290
``` 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 " ;
292
293
console .log (generateMnemonic (wordlist));
293
294
```
294
295
@@ -385,8 +386,8 @@ exception.
385
386
### Example usage
386
387
387
388
``` 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 " ;
390
391
391
392
console .log (
392
393
encrypt (
@@ -420,15 +421,13 @@ These can be used by setting your `plugins` array like this:
420
421
421
422
## Legacy secp256k1 compatibility layer
422
423
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.
427
426
428
427
The API of ` secp256k1-compat ` is the same as [ secp256k1-node] ( https://github.com/cryptocoinjs/secp256k1-node ) :
429
428
430
429
``` js
431
- const { createPrivateKeySync , ecdsaSign } = require ( " ethereum-cryptography/secp256k1-compat" ) ;
430
+ import { createPrivateKeySync , ecdsaSign } from " ethereum-cryptography/secp256k1-compat" ;
432
431
const msgHash = Uint8Array .from (
433
432
" 82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28" ,
434
433
" hex"
@@ -460,7 +459,7 @@ Upgrading from 1.0 to 2.0:
460
459
to [ upgrading section from curves README] ( https://github.com/paulmillr/noble-curves#upgrading ) .
461
460
Main changes to keep in mind: a) ` sign ` now returns ` Signature ` instance
462
461
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.
464
463
465
464
Upgrading from 0.1 to 1.0: ** Same functionality** , all old APIs remain the same except for the breaking changes:
466
465
@@ -471,14 +470,14 @@ browsers and node.js.
471
470
which is Chrome 67+, Edge 79+, Firefox 68+, Safari 14+, node.js 10+. If you need to support older runtimes, use
` [email protected] `
472
471
3 . If you've used ` secp256k1 ` , [ rename it to ` secp256k1-compat ` ] ( #legacy-secp256k1-compatibility-layer )
473
472
474
- ```
475
- const { sha256 } = require( "ethereum-cryptography/sha256") ;
473
+ ``` js
474
+ import { sha256 } from " ethereum-cryptography/sha256.js " ;
476
475
477
476
// Old usage
478
477
const hasho = sha256 (Buffer .from (" string" , " utf8" )).toString (" hex" );
479
478
480
479
// New usage
481
- const { toHex } = require( "ethereum-cryptography/utils") ;
480
+ import { toHex } from " ethereum-cryptography/utils.js " ;
482
481
const hashn = toHex (sha256 (" string" ));
483
482
484
483
// If you have `Buffer` module and want to preserve it:
@@ -505,9 +504,5 @@ Copyright (c) 2018 cryptocoinjs
505
504
506
505
[ 1 ] : https://img.shields.io/npm/v/ethereum-cryptography.svg
507
506
[ 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