@@ -57,10 +57,11 @@ import * as secp from '@noble/secp256k1';
5757 const alice = secp .keygen ();
5858 const bob = secp .keygen ();
5959 const shared = secp .getSharedSecret (alice .secretKey , bob .publicKey );
60+ const msg = new TextEncoder ().encode (' hello noble' );
6061
6162 // recovery
6263 const sigr = await secp .signAsync (msg, alice .secretKey , { format: ' recovered' });
63- const publicKey2 = secp .recoverPublicKey (sigr, msg);
64+ const publicKey2 = await secp .recoverPublicKeyAsync (sigr, msg);
6465})();
6566
6667// Schnorr signatures from BIP340
@@ -78,7 +79,10 @@ import * as secp from '@noble/secp256k1';
7879Only async methods are available by default, to keep the library dependency-free.
7980To enable sync methods:
8081
82+ > ` npm install @noble/hashes `
83+
8184``` ts
85+ import * as secp from ' @noble/secp256k1' ;
8286import { hmac } from ' @noble/hashes/hmac.js' ;
8387import { sha256 } from ' @noble/hashes/sha2.js' ;
8488secp .hashes .hmacSha256 = (key , msg ) => hmac (sha256 , key , msg );
@@ -92,6 +96,7 @@ This can't be securely polyfilled from our end, so one will need a RN-specific c
9296
9397``` ts
9498import ' react-native-get-random-values' ;
99+ import * as secp from ' @noble/secp256k1' ;
95100import { hmac } from ' @noble/hashes/hmac.js' ;
96101import { sha256 } from ' @noble/hashes/sha2.js' ;
97102secp .hashes .hmacSha256 = (key , msg ) => hmac (sha256 , key , msg );
@@ -138,18 +143,21 @@ Generates 33-byte compressed (default) or 65-byte public key from 32-byte privat
138143
139144``` ts
140145import * as secp from ' @noble/secp256k1' ;
146+ import { hmac } from ' @noble/hashes/hmac.js' ;
147+ import { sha256 } from ' @noble/hashes/sha2.js' ;
148+ import { keccak_256 } from ' @noble/hashes/sha3.js' ;
149+ secp .hashes .hmacSha256 = (key , msg ) => hmac (sha256 , key , msg );
150+ secp .hashes .sha256 = sha256 ;
141151const { secretKey } = secp .keygen ();
142- const msg = ' hello noble' ;
152+ const msg = new TextEncoder (). encode ( ' hello noble' ) ;
143153const sig = secp .sign (msg , secretKey );
144154
145155// async
146156const sigB = await secp .signAsync (msg , secretKey );
147157
148158// recovered, allows `recoverPublicKey(sigR, msg)`
149159const sigR = secp .sign (msg , secretKey , { format: ' recovered' });
150- // custom hash
151- import { keccak256 } from ' @noble/hashes/sha3.js' ;
152- const sigH = secp .sign (keccak256 (msg ), secretKey , { prehash: false });
160+ const sigH = secp .sign (keccak_256 (msg ), secretKey , { prehash: false });
153161// hedged sig
154162const sigC = secp .sign (msg , secretKey , { extraEntropy: true });
155163const sigC2 = secp .sign (msg , secretKey , { extraEntropy: Uint8Array .from ([0xca , 0xfe ]) });
@@ -172,14 +180,17 @@ Even if their RNG is broken, they will fall back to determinism.
172180
173181``` ts
174182import * as secp from ' @noble/secp256k1' ;
183+ import { hmac } from ' @noble/hashes/hmac.js' ;
184+ import { sha256 } from ' @noble/hashes/sha2.js' ;
185+ import { keccak_256 } from ' @noble/hashes/sha3.js' ;
186+ secp .hashes .hmacSha256 = (key , msg ) => hmac (sha256 , key , msg );
187+ secp .hashes .sha256 = sha256 ;
175188const { secretKey, publicKey } = secp .keygen ();
176- const msg = ' hello noble' ;
189+ const msg = new TextEncoder (). encode ( ' hello noble' ) ;
177190const sig = secp .sign (msg , secretKey );
178191const isValid = secp .verify (sig , msg , publicKey );
179192
180- // custom hash
181- import { keccak256 } from ' @noble/hashes/sha3.js' ;
182- const sigH = secp .sign (keccak256 (msg ), secretKey , { prehash: false });
193+ const sigH = secp .sign (keccak_256 (msg ), secretKey , { prehash: false });
183194```
184195
185196Verifies ECDSA signature.
@@ -210,31 +221,46 @@ key A and different key B.
210221
211222``` ts
212223import * as secp from ' @noble/secp256k1' ;
224+ import { hmac } from ' @noble/hashes/hmac.js' ;
225+ import { sha256 } from ' @noble/hashes/sha2.js' ;
226+ import { keccak_256 } from ' @noble/hashes/sha3.js' ;
227+ secp .hashes .hmacSha256 = (key , msg ) => hmac (sha256 , key , msg );
228+ secp .hashes .sha256 = sha256 ;
213229
214230const { secretKey, publicKey } = secp .keygen ();
215- const msg = ' hello noble' ;
231+ const msg = new TextEncoder (). encode ( ' hello noble' ) ;
216232const sigR = secp .sign (msg , secretKey , { format: ' recovered' });
217233const publicKey2 = secp .recoverPublicKey (sigR , msg );
218234
219- // custom hash
220- import { keccak256 } from ' @noble/hashes/sha3.js' ;
221- const sigR = secp .sign (keccak256 (msg ), secretKey , { format: ' recovered' , prehash: false });
222- const publicKey2 = secp .recoverPublicKey (sigR , keccak256 (msg ), { prehash: false });
235+ const sigRH = secp .sign (keccak_256 (msg ), secretKey , { format: ' recovered' , prehash: false });
236+ const publicKeyH = secp .recoverPublicKey (sigRH , keccak_256 (msg ), { prehash: false });
223237```
224238
225239Recover public key from Signature instance with ` recovery ` bit set.
226240
227241### schnorr
228242
229243``` ts
244+ import * as secp from ' @noble/secp256k1' ;
230245import { schnorr } from ' @noble/secp256k1' ;
246+ import { hmac } from ' @noble/hashes/hmac.js' ;
247+ import { sha256 } from ' @noble/hashes/sha2.js' ;
248+ secp .hashes .hmacSha256 = (key , msg ) => hmac (sha256 , key , msg );
249+ secp .hashes .sha256 = sha256 ;
231250const { secretKey, publicKey } = schnorr .keygen ();
232251const msg = new TextEncoder ().encode (' hello noble' );
233252const sig = schnorr .sign (msg , secretKey );
234253const isValid = schnorr .verify (sig , msg , publicKey );
254+ ```
255+
256+ Async methods work without extra setup:
235257
236- const sig = await schnorr .signAsync (msg , secretKey );
237- const isValid = await schnorr .verifyAsync (sig , msg , publicKey );
258+ ``` ts
259+ import { schnorr } from ' @noble/secp256k1' ;
260+ const { secretKey, publicKey } = schnorr .keygen ();
261+ const msg = new TextEncoder ().encode (' hello noble' );
262+ const sigA = await schnorr .signAsync (msg , secretKey );
263+ const isValidA = await schnorr .verifyAsync (sigA , msg , publicKey );
238264```
239265
240266Schnorr
@@ -377,6 +403,9 @@ v3 brings the package closer to noble-curves v2.
377403- etc: hashes are now set in ` hashes ` object. Also sha256 needs to be set now for ` prehash: true ` :
378404
379405``` js
406+ import { etc , hashes } from ' @noble/secp256k1' ;
407+ import { hmac } from ' @noble/hashes/hmac.js' ;
408+ import { sha256 } from ' @noble/hashes/sha2.js' ;
380409// before
381410etc .hmacSha256Sync = (key , ... messages ) => hmac (sha256, key, etc .concatBytes (... messages));
382411etc .hmacSha256Async = (key , ... messages ) => Promise .resolve (etc .hmacSha256Sync (key, ... messages));
0 commit comments