@@ -26,6 +26,7 @@ The module has been completely rewritten:
26
26
browsers and node.js. See [ Upgrading] ( #upgrading )
27
27
2 . We target runtimes with [ bigint] ( https://caniuse.com/bigint ) support,
28
28
which is Chrome 67+, Edge 79+, Firefox 68+, Safari 14+, node.js 10+. If you need to support older runtimes, use
` [email protected] `
29
+ 3 . If you've used ` secp256k1 ` , [ rename it to ` secp256k1-compat ` ] ( #legacy-secp256k1-compatibility-layer )
29
30
- The new module [ has not been audited yet] ( #security ) , but it's in the process of getting the audit. Use it at your own risk
30
31
31
32
## Usage
@@ -34,10 +35,10 @@ Use NPM / Yarn in node.js / browser:
34
35
35
36
``` bash
36
37
# NPM
37
- npm install ethereum-cryptography
38
+ npm install ethereum-cryptography@next
38
39
39
40
# Yarn
40
- yarn add ethereum-cryptography
41
+ yarn add ethereum-cryptography@next
41
42
```
42
43
43
44
See [ browser usage] ( #browser-usage ) for information on using the package with major Javascript bundlers. It is
@@ -76,6 +77,9 @@ const { createPrivateKeySync, ecdsaSign } = require("ethereum-cryptography/secp2
76
77
const { HDKey } = require (" ethereum-cryptography/hdkey" );
77
78
const { generateMnemonic } = require (" ethereum-cryptography/bip39" );
78
79
const { wordlist } = require (" ethereum-cryptography/bip39/wordlists/english" );
80
+
81
+ // utilities
82
+ const { hexToBytes , toHex , utf8ToBytes } = require (" ethereum-cryptography/utils" );
79
83
```
80
84
81
85
## Hashes: SHA256, keccak-256, RIPEMD160, BLAKE2b
@@ -101,6 +105,16 @@ const { sha512 } = require("ethereum-cryptography/sha512");
101
105
const { keccak256 , keccak224 , keccak384 , keccak512 } = require (" ethereum-cryptography/keccak" );
102
106
const { ripemd160 } = require (" ethereum-cryptography/ripemd160" );
103
107
const { blake2b } = require (" ethereum-cryptography/blake2b" );
108
+
109
+ sha256 (Uint8Array .from ([1 , 2 , 3 ]))
110
+
111
+ // Can be used with strings
112
+ const { utf8ToBytes } = require (" ethereum-cryptography/utils" );
113
+ sha256 (utf8ToBytes (" abc" ))
114
+
115
+ // If you need hex
116
+ const { toHex } = require (" ethereum-cryptography/utils" );
117
+ toHex (sha256 (utf8ToBytes (" abc" )))
104
118
```
105
119
106
120
## KDFs: PBKDF2, Scrypt
@@ -127,13 +141,16 @@ Encoding passwords is a frequent source of errors. Please read
127
141
before using these submodules.
128
142
129
143
``` js
130
- const { pbkdf2Sync } = require (" ethereum-cryptography/pbkdf2" );
131
- console .log (pbkdf2Sync (" password" , " salt" , 131072 , 32 , " sha256" ));
144
+ const { pbkdf2 } = require (" ethereum-cryptography/pbkdf2" );
145
+ const { utf8ToBytes } = require (" ethereum-cryptography/utils" );
146
+ // Pass Uint8Array, or convert strings to Uint8Array
147
+ console .log (await pbkdf2 (utf8ToBytes (" password" ), utf8ToBytes (" salt" ), 131072 , 32 , " sha256" ));
132
148
```
133
149
134
150
``` js
135
151
const { scryptSync } = require (" ethereum-cryptography/scrypt" );
136
- console .log (scryptSync (" password" , " salt" , 262144 , 8 , 1 , 32 ));
152
+ const { utf8ToBytes } = require (" ethereum-cryptography/utils" );
153
+ console .log (await scrypt (utf8ToBytes (" password" ), utf8ToBytes (" salt" ), 262144 , 8 , 1 , 32 ));
137
154
```
138
155
139
156
## CSPRNG (Cryptographically strong pseudorandom number generator)
@@ -165,15 +182,15 @@ function recoverPublicKey(msgHash: Uint8Array, signature: Uint8Array, recovery:
165
182
function utils.randomPrivateKey(): Uint8Array ;
166
183
```
167
184
168
- The ` curve- secp256k1` submodule provides a library for elliptic curve operations on
185
+ The ` secp256k1 ` submodule provides a library for elliptic curve operations on
169
186
the curve secp256k1. For detailed documentation, follow [ README of ` noble-secp256k1 ` ] ( https://github.com/paulmillr/noble-secp256k1 ) , which the module uses as a backend.
170
187
171
188
secp256k1 private keys need to be cryptographically secure random numbers with
172
189
certain caracteristics. If this is not the case, the security of secp256k1 is
173
190
compromised. We strongly recommend using ` utils.randomPrivateKey() ` to generate them.
174
191
175
192
``` js
176
- const secp = require (" ethereum-cryptography/curve- secp256k1" );
193
+ const secp = require (" ethereum-cryptography/secp256k1" );
177
194
(async () => {
178
195
// You pass either a hex string, or Uint8Array
179
196
const privateKey = " 6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e" ;
@@ -376,19 +393,13 @@ console.log(
376
393
Using this library with Rollup requires the following plugins :
377
394
378
395
* [` @rollup/plugin-commonjs ` ](https : // www.npmjs.com/package/@rollup/plugin-commonjs)
379
- * [` @rollup/plugin-json ` ](https : // www.npmjs.com/package/@rollup/plugin-json)
380
396
* [` @rollup/plugin-node-resolve ` ](https : // www.npmjs.com/package/@rollup/plugin-node-resolve)
381
- * [` rollup-plugin-node-builtins ` ](https : // www.npmjs.com/package/rollup-plugin-node-builtins)
382
- * [` rollup-plugin-node-globals ` ](https : // www.npmjs.com/package/rollup-plugin-node-globals)
383
397
384
398
These can be used by setting your ` plugins ` array like this :
385
399
386
400
` ` ` js
387
401
plugins: [
388
402
commonjs(),
389
- json(),
390
- nodeGlobals(),
391
- nodeBuiltins(),
392
403
resolve({
393
404
browser: true,
394
405
preferBuiltins: false,
@@ -398,14 +409,15 @@ These can be used by setting your `plugins` array like this:
398
409
399
410
## Legacy secp256k1 compatibility layer
400
411
401
- **Note : ** do not use this module ; it is only for users who upgrade
412
+ **Note : ** consider using ` secp256k1 ` instead ;
413
+ This module is only for users who upgraded
402
414
from ethereum -cryptography v0 .1. It could be removed in the future ,
403
415
but we ' re keeping it around for now, for backwards-compatibility.
404
416
405
- The API of ` secp256k1 ` is the same as [secp256k1 -node ](https : // github.com/cryptocoinjs/secp256k1-node):
417
+ The API of ` secp256k1-compat ` is the same as [secp256k1 -node ](https : // github.com/cryptocoinjs/secp256k1-node):
406
418
407
419
` ` ` js
408
- const { createPrivateKeySync, ecdsaSign } = require("ethereum-cryptography/secp256k1");
420
+ const { createPrivateKeySync, ecdsaSign } = require("ethereum-cryptography/secp256k1-compat ");
409
421
const msgHash = Uint8Array.from(
410
422
"82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28",
411
423
"hex"
0 commit comments