@taquito/signer 24.3.0 → 25.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/version.js +2 -2
- package/dist/taquito-signer.es6.js +583 -137
- package/dist/taquito-signer.es6.js.map +1 -1
- package/dist/taquito-signer.umd.js +583 -137
- package/dist/taquito-signer.umd.js.map +1 -1
- package/dist/types/node_modules/@scure/base/index.d.ts +286 -24
- package/package.json +9 -9
|
@@ -40,22 +40,62 @@
|
|
|
40
40
|
};
|
|
41
41
|
|
|
42
42
|
/**
|
|
43
|
-
*
|
|
44
|
-
* @
|
|
43
|
+
* Checks if something is Uint8Array. Be careful: nodejs Buffer will return true.
|
|
44
|
+
* @param a - value to test
|
|
45
|
+
* @returns `true` when the value is a Uint8Array-compatible view.
|
|
46
|
+
* @example
|
|
47
|
+
* Check whether a value is a Uint8Array-compatible view.
|
|
48
|
+
* ```ts
|
|
49
|
+
* isBytes(new Uint8Array([1, 2, 3]));
|
|
50
|
+
* ```
|
|
45
51
|
*/
|
|
46
|
-
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
47
|
-
/** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */
|
|
48
52
|
function isBytes$1(a) {
|
|
49
|
-
|
|
53
|
+
// Plain `instanceof Uint8Array` is too strict for some Buffer / proxy / cross-realm cases.
|
|
54
|
+
// The fallback still requires a real ArrayBuffer view, so plain
|
|
55
|
+
// JSON-deserialized `{ constructor: ... }` spoofing is rejected, and
|
|
56
|
+
// `BYTES_PER_ELEMENT === 1` keeps the fallback on byte-oriented views.
|
|
57
|
+
return (a instanceof Uint8Array ||
|
|
58
|
+
(ArrayBuffer.isView(a) &&
|
|
59
|
+
a.constructor.name === 'Uint8Array' &&
|
|
60
|
+
'BYTES_PER_ELEMENT' in a &&
|
|
61
|
+
a.BYTES_PER_ELEMENT === 1));
|
|
50
62
|
}
|
|
51
|
-
/**
|
|
63
|
+
/**
|
|
64
|
+
* Asserts something is a non-negative integer.
|
|
65
|
+
* @param n - number to validate
|
|
66
|
+
* @param title - label included in thrown errors
|
|
67
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
68
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
69
|
+
* @example
|
|
70
|
+
* Validate a non-negative integer option.
|
|
71
|
+
* ```ts
|
|
72
|
+
* anumber(32, 'length');
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
52
75
|
function anumber$1(n, title = '') {
|
|
76
|
+
if (typeof n !== 'number') {
|
|
77
|
+
const prefix = title && `"${title}" `;
|
|
78
|
+
throw new TypeError(`${prefix}expected number, got ${typeof n}`);
|
|
79
|
+
}
|
|
53
80
|
if (!Number.isSafeInteger(n) || n < 0) {
|
|
54
81
|
const prefix = title && `"${title}" `;
|
|
55
|
-
throw new
|
|
82
|
+
throw new RangeError(`${prefix}expected integer >= 0, got ${n}`);
|
|
56
83
|
}
|
|
57
84
|
}
|
|
58
|
-
/**
|
|
85
|
+
/**
|
|
86
|
+
* Asserts something is Uint8Array.
|
|
87
|
+
* @param value - value to validate
|
|
88
|
+
* @param length - optional exact length constraint
|
|
89
|
+
* @param title - label included in thrown errors
|
|
90
|
+
* @returns The validated byte array.
|
|
91
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
92
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
93
|
+
* @example
|
|
94
|
+
* Validate that a value is a byte array.
|
|
95
|
+
* ```ts
|
|
96
|
+
* abytes(new Uint8Array([1, 2, 3]));
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
59
99
|
function abytes(value, length, title = '') {
|
|
60
100
|
const bytes = isBytes$1(value);
|
|
61
101
|
const len = value?.length;
|
|
@@ -64,111 +104,297 @@
|
|
|
64
104
|
const prefix = title && `"${title}" `;
|
|
65
105
|
const ofLen = needsLen ? ` of length ${length}` : '';
|
|
66
106
|
const got = bytes ? `length=${len}` : `type=${typeof value}`;
|
|
67
|
-
|
|
107
|
+
const message = prefix + 'expected Uint8Array' + ofLen + ', got ' + got;
|
|
108
|
+
if (!bytes)
|
|
109
|
+
throw new TypeError(message);
|
|
110
|
+
throw new RangeError(message);
|
|
68
111
|
}
|
|
69
112
|
return value;
|
|
70
113
|
}
|
|
71
|
-
/**
|
|
114
|
+
/**
|
|
115
|
+
* Asserts something is a wrapped hash constructor.
|
|
116
|
+
* @param h - hash constructor to validate
|
|
117
|
+
* @throws On wrong argument types or invalid hash wrapper shape. {@link TypeError}
|
|
118
|
+
* @throws On invalid hash metadata ranges or values. {@link RangeError}
|
|
119
|
+
* @throws If the hash metadata allows empty outputs or block sizes. {@link Error}
|
|
120
|
+
* @example
|
|
121
|
+
* Validate a callable hash wrapper.
|
|
122
|
+
* ```ts
|
|
123
|
+
* import { ahash } from '@noble/hashes/utils.js';
|
|
124
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
125
|
+
* ahash(sha256);
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
72
128
|
function ahash(h) {
|
|
73
129
|
if (typeof h !== 'function' || typeof h.create !== 'function')
|
|
74
|
-
throw new
|
|
130
|
+
throw new TypeError('Hash must wrapped by utils.createHasher');
|
|
75
131
|
anumber$1(h.outputLen);
|
|
76
132
|
anumber$1(h.blockLen);
|
|
133
|
+
// HMAC and KDF callers treat these as real byte lengths; allowing zero lets fake wrappers pass
|
|
134
|
+
// validation and can produce empty outputs instead of failing fast.
|
|
135
|
+
if (h.outputLen < 1)
|
|
136
|
+
throw new Error('"outputLen" must be >= 1');
|
|
137
|
+
if (h.blockLen < 1)
|
|
138
|
+
throw new Error('"blockLen" must be >= 1');
|
|
77
139
|
}
|
|
78
|
-
/**
|
|
140
|
+
/**
|
|
141
|
+
* Asserts a hash instance has not been destroyed or finished.
|
|
142
|
+
* @param instance - hash instance to validate
|
|
143
|
+
* @param checkFinished - whether to reject finalized instances
|
|
144
|
+
* @throws If the hash instance has already been destroyed or finalized. {@link Error}
|
|
145
|
+
* @example
|
|
146
|
+
* Validate that a hash instance is still usable.
|
|
147
|
+
* ```ts
|
|
148
|
+
* import { aexists } from '@noble/hashes/utils.js';
|
|
149
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
150
|
+
* const hash = sha256.create();
|
|
151
|
+
* aexists(hash);
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
79
154
|
function aexists(instance, checkFinished = true) {
|
|
80
155
|
if (instance.destroyed)
|
|
81
156
|
throw new Error('Hash instance has been destroyed');
|
|
82
157
|
if (checkFinished && instance.finished)
|
|
83
158
|
throw new Error('Hash#digest() has already been called');
|
|
84
159
|
}
|
|
85
|
-
/**
|
|
160
|
+
/**
|
|
161
|
+
* Asserts output is a sufficiently-sized byte array.
|
|
162
|
+
* @param out - destination buffer
|
|
163
|
+
* @param instance - hash instance providing output length
|
|
164
|
+
* Oversized buffers are allowed; downstream code only promises to fill the first `outputLen` bytes.
|
|
165
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
166
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
167
|
+
* @example
|
|
168
|
+
* Validate a caller-provided digest buffer.
|
|
169
|
+
* ```ts
|
|
170
|
+
* import { aoutput } from '@noble/hashes/utils.js';
|
|
171
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
172
|
+
* const hash = sha256.create();
|
|
173
|
+
* aoutput(new Uint8Array(hash.outputLen), hash);
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
86
176
|
function aoutput(out, instance) {
|
|
87
177
|
abytes(out, undefined, 'digestInto() output');
|
|
88
178
|
const min = instance.outputLen;
|
|
89
179
|
if (out.length < min) {
|
|
90
|
-
throw new
|
|
180
|
+
throw new RangeError('"digestInto() output" expected to be of length >=' + min);
|
|
91
181
|
}
|
|
92
182
|
}
|
|
93
|
-
/**
|
|
183
|
+
/**
|
|
184
|
+
* Casts a typed array view to Uint32Array.
|
|
185
|
+
* `arr.byteOffset` must already be 4-byte aligned or the platform
|
|
186
|
+
* Uint32Array constructor will throw.
|
|
187
|
+
* @param arr - source typed array
|
|
188
|
+
* @returns Uint32Array view over the same buffer.
|
|
189
|
+
* @example
|
|
190
|
+
* Reinterpret a byte array as 32-bit words.
|
|
191
|
+
* ```ts
|
|
192
|
+
* u32(new Uint8Array(8));
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
94
195
|
function u32(arr) {
|
|
95
196
|
return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
|
|
96
197
|
}
|
|
97
|
-
/**
|
|
198
|
+
/**
|
|
199
|
+
* Zeroizes typed arrays in place. Warning: JS provides no guarantees.
|
|
200
|
+
* @param arrays - arrays to overwrite with zeros
|
|
201
|
+
* @example
|
|
202
|
+
* Zeroize sensitive buffers in place.
|
|
203
|
+
* ```ts
|
|
204
|
+
* clean(new Uint8Array([1, 2, 3]));
|
|
205
|
+
* ```
|
|
206
|
+
*/
|
|
98
207
|
function clean(...arrays) {
|
|
99
208
|
for (let i = 0; i < arrays.length; i++) {
|
|
100
209
|
arrays[i].fill(0);
|
|
101
210
|
}
|
|
102
211
|
}
|
|
103
|
-
/**
|
|
212
|
+
/**
|
|
213
|
+
* Creates a DataView for byte-level manipulation.
|
|
214
|
+
* @param arr - source typed array
|
|
215
|
+
* @returns DataView over the same buffer region.
|
|
216
|
+
* @example
|
|
217
|
+
* Create a DataView over an existing buffer.
|
|
218
|
+
* ```ts
|
|
219
|
+
* createView(new Uint8Array(4));
|
|
220
|
+
* ```
|
|
221
|
+
*/
|
|
104
222
|
function createView(arr) {
|
|
105
223
|
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
106
224
|
}
|
|
107
|
-
/**
|
|
225
|
+
/**
|
|
226
|
+
* Rotate-right operation for uint32 values.
|
|
227
|
+
* @param word - source word
|
|
228
|
+
* @param shift - shift amount in bits
|
|
229
|
+
* @returns Rotated word.
|
|
230
|
+
* @example
|
|
231
|
+
* Rotate a 32-bit word to the right.
|
|
232
|
+
* ```ts
|
|
233
|
+
* rotr(0x12345678, 8);
|
|
234
|
+
* ```
|
|
235
|
+
*/
|
|
108
236
|
function rotr(word, shift) {
|
|
109
237
|
return (word << (32 - shift)) | (word >>> shift);
|
|
110
238
|
}
|
|
111
|
-
/**
|
|
239
|
+
/** Whether the current platform is little-endian. */
|
|
112
240
|
const isLE = /* @__PURE__ */ (() => new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44)();
|
|
113
|
-
/**
|
|
241
|
+
/**
|
|
242
|
+
* Byte-swap operation for uint32 values.
|
|
243
|
+
* @param word - source word
|
|
244
|
+
* @returns Word with reversed byte order.
|
|
245
|
+
* @example
|
|
246
|
+
* Reverse the byte order of a 32-bit word.
|
|
247
|
+
* ```ts
|
|
248
|
+
* byteSwap(0x11223344);
|
|
249
|
+
* ```
|
|
250
|
+
*/
|
|
114
251
|
function byteSwap(word) {
|
|
115
252
|
return (((word << 24) & 0xff000000) |
|
|
116
253
|
((word << 8) & 0xff0000) |
|
|
117
254
|
((word >>> 8) & 0xff00) |
|
|
118
255
|
((word >>> 24) & 0xff));
|
|
119
256
|
}
|
|
120
|
-
/**
|
|
257
|
+
/**
|
|
258
|
+
* Conditionally byte-swaps one 32-bit word on big-endian platforms.
|
|
259
|
+
* @param n - source word
|
|
260
|
+
* @returns Original or byte-swapped word depending on platform endianness.
|
|
261
|
+
* @example
|
|
262
|
+
* Normalize a 32-bit word for host endianness.
|
|
263
|
+
* ```ts
|
|
264
|
+
* swap8IfBE(0x11223344);
|
|
265
|
+
* ```
|
|
266
|
+
*/
|
|
121
267
|
const swap8IfBE = isLE
|
|
122
268
|
? (n) => n
|
|
123
|
-
: (n) => byteSwap(n);
|
|
124
|
-
/**
|
|
269
|
+
: (n) => byteSwap(n) >>> 0;
|
|
270
|
+
/**
|
|
271
|
+
* Byte-swaps every word of a Uint32Array in place.
|
|
272
|
+
* @param arr - array to mutate
|
|
273
|
+
* @returns The same array after mutation; callers pass live state arrays here.
|
|
274
|
+
* @example
|
|
275
|
+
* Reverse the byte order of every word in place.
|
|
276
|
+
* ```ts
|
|
277
|
+
* byteSwap32(new Uint32Array([0x11223344]));
|
|
278
|
+
* ```
|
|
279
|
+
*/
|
|
125
280
|
function byteSwap32(arr) {
|
|
126
281
|
for (let i = 0; i < arr.length; i++) {
|
|
127
282
|
arr[i] = byteSwap(arr[i]);
|
|
128
283
|
}
|
|
129
284
|
return arr;
|
|
130
285
|
}
|
|
286
|
+
/**
|
|
287
|
+
* Conditionally byte-swaps a Uint32Array on big-endian platforms.
|
|
288
|
+
* @param u - array to normalize for host endianness
|
|
289
|
+
* @returns Original or byte-swapped array depending on platform endianness.
|
|
290
|
+
* On big-endian runtimes this mutates `u` in place via `byteSwap32(...)`.
|
|
291
|
+
* @example
|
|
292
|
+
* Normalize a word array for host endianness.
|
|
293
|
+
* ```ts
|
|
294
|
+
* swap32IfBE(new Uint32Array([0x11223344]));
|
|
295
|
+
* ```
|
|
296
|
+
*/
|
|
131
297
|
const swap32IfBE = isLE
|
|
132
298
|
? (u) => u
|
|
133
299
|
: byteSwap32;
|
|
134
300
|
/**
|
|
135
301
|
* Converts string to bytes using UTF8 encoding.
|
|
136
302
|
* Built-in doesn't validate input to be string: we do the check.
|
|
137
|
-
*
|
|
303
|
+
* Non-ASCII details are delegated to the platform `TextEncoder`.
|
|
304
|
+
* @param str - string to encode
|
|
305
|
+
* @returns UTF-8 encoded bytes.
|
|
306
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
307
|
+
* @example
|
|
308
|
+
* Encode a string as UTF-8 bytes.
|
|
309
|
+
* ```ts
|
|
310
|
+
* utf8ToBytes('abc'); // Uint8Array.from([97, 98, 99])
|
|
311
|
+
* ```
|
|
138
312
|
*/
|
|
139
313
|
function utf8ToBytes(str) {
|
|
140
314
|
if (typeof str !== 'string')
|
|
141
|
-
throw new
|
|
315
|
+
throw new TypeError('string expected');
|
|
142
316
|
return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
|
|
143
317
|
}
|
|
144
318
|
/**
|
|
145
|
-
* Helper for KDFs: consumes
|
|
146
|
-
*
|
|
319
|
+
* Helper for KDFs: consumes Uint8Array or string.
|
|
320
|
+
* String inputs are UTF-8 encoded; byte-array inputs stay aliased to the caller buffer.
|
|
321
|
+
* @param data - user-provided KDF input
|
|
322
|
+
* @param errorTitle - label included in thrown errors
|
|
323
|
+
* @returns Byte representation of the input.
|
|
324
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
325
|
+
* @example
|
|
326
|
+
* Normalize KDF input to bytes.
|
|
327
|
+
* ```ts
|
|
328
|
+
* kdfInputToBytes('password');
|
|
329
|
+
* ```
|
|
147
330
|
*/
|
|
148
331
|
function kdfInputToBytes(data, errorTitle = '') {
|
|
149
332
|
if (typeof data === 'string')
|
|
150
333
|
return utf8ToBytes(data);
|
|
151
334
|
return abytes(data, undefined, errorTitle);
|
|
152
335
|
}
|
|
153
|
-
/**
|
|
336
|
+
/**
|
|
337
|
+
* Merges default options and passed options.
|
|
338
|
+
* @param defaults - base option object
|
|
339
|
+
* @param opts - user overrides
|
|
340
|
+
* @returns Merged option object. The merge mutates `defaults` in place.
|
|
341
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
342
|
+
* @example
|
|
343
|
+
* Merge user overrides onto default options.
|
|
344
|
+
* ```ts
|
|
345
|
+
* checkOpts({ dkLen: 32 }, { asyncTick: 10 });
|
|
346
|
+
* ```
|
|
347
|
+
*/
|
|
154
348
|
function checkOpts(defaults, opts) {
|
|
155
349
|
if (opts !== undefined && {}.toString.call(opts) !== '[object Object]')
|
|
156
|
-
throw new
|
|
350
|
+
throw new TypeError('options must be object or undefined');
|
|
157
351
|
const merged = Object.assign(defaults, opts);
|
|
158
352
|
return merged;
|
|
159
353
|
}
|
|
160
|
-
/**
|
|
354
|
+
/**
|
|
355
|
+
* Creates a callable hash function from a stateful class constructor.
|
|
356
|
+
* @param hashCons - hash constructor or factory
|
|
357
|
+
* @param info - optional metadata such as DER OID
|
|
358
|
+
* @returns Frozen callable hash wrapper with `.create()`.
|
|
359
|
+
* Wrapper construction eagerly calls `hashCons(undefined)` once to read
|
|
360
|
+
* `outputLen` / `blockLen`, so constructor side effects happen at module
|
|
361
|
+
* init time.
|
|
362
|
+
* @example
|
|
363
|
+
* Wrap a stateful hash constructor into a callable helper.
|
|
364
|
+
* ```ts
|
|
365
|
+
* import { createHasher } from '@noble/hashes/utils.js';
|
|
366
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
367
|
+
* const wrapped = createHasher(sha256.create, { oid: sha256.oid });
|
|
368
|
+
* wrapped(new Uint8Array([1]));
|
|
369
|
+
* ```
|
|
370
|
+
*/
|
|
161
371
|
function createHasher(hashCons, info = {}) {
|
|
162
|
-
const hashC = (msg, opts) => hashCons(opts)
|
|
372
|
+
const hashC = (msg, opts) => hashCons(opts)
|
|
373
|
+
.update(msg)
|
|
374
|
+
.digest();
|
|
163
375
|
const tmp = hashCons(undefined);
|
|
164
376
|
hashC.outputLen = tmp.outputLen;
|
|
165
377
|
hashC.blockLen = tmp.blockLen;
|
|
378
|
+
hashC.canXOF = tmp.canXOF;
|
|
166
379
|
hashC.create = (opts) => hashCons(opts);
|
|
167
380
|
Object.assign(hashC, info);
|
|
168
381
|
return Object.freeze(hashC);
|
|
169
382
|
}
|
|
170
|
-
/**
|
|
383
|
+
/**
|
|
384
|
+
* Creates OID metadata for NIST hashes with prefix `06 09 60 86 48 01 65 03 04 02`.
|
|
385
|
+
* @param suffix - final OID byte for the selected hash.
|
|
386
|
+
* The helper accepts any byte even though only the documented NIST hash
|
|
387
|
+
* suffixes are meaningful downstream.
|
|
388
|
+
* @returns Object containing the DER-encoded OID.
|
|
389
|
+
* @example
|
|
390
|
+
* Build OID metadata for a NIST hash.
|
|
391
|
+
* ```ts
|
|
392
|
+
* oidNist(0x01);
|
|
393
|
+
* ```
|
|
394
|
+
*/
|
|
171
395
|
const oidNist = (suffix) => ({
|
|
396
|
+
// Current NIST hashAlgs suffixes used here fit in one DER subidentifier octet.
|
|
397
|
+
// Larger suffix values would need base-128 OID encoding and a different length byte.
|
|
172
398
|
oid: Uint8Array.from([0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, suffix]),
|
|
173
399
|
});
|
|
174
400
|
|
|
@@ -177,8 +403,10 @@
|
|
|
177
403
|
* @module
|
|
178
404
|
*/
|
|
179
405
|
/**
|
|
180
|
-
* Internal blake
|
|
181
|
-
*
|
|
406
|
+
* Internal blake permutation table.
|
|
407
|
+
* Rows `0..9` serve BLAKE2s, rows `0..11` serve BLAKE2b with `10..11 = 0..1`, and Blake1 also
|
|
408
|
+
* reuses the later rows shown below. Blake1 expands rounds `10..15` as `SIGMA[i % 10]`, so rows
|
|
409
|
+
* `10..15` intentionally repeat rows `0..5` for the 14-round (256) and 16-round (512) variants.
|
|
182
410
|
*/
|
|
183
411
|
// prettier-ignore
|
|
184
412
|
const BSIGMA = /* @__PURE__ */ Uint8Array.from([
|
|
@@ -205,21 +433,62 @@
|
|
|
205
433
|
* Internal Merkle-Damgard hash utils.
|
|
206
434
|
* @module
|
|
207
435
|
*/
|
|
208
|
-
/**
|
|
436
|
+
/**
|
|
437
|
+
* Shared 32-bit conditional boolean primitive reused by SHA-256, SHA-1, and MD5 `F`.
|
|
438
|
+
* Returns bits from `b` when `a` is set, otherwise from `c`.
|
|
439
|
+
* The XOR form is equivalent to MD5's `F(X,Y,Z) = XY v not(X)Z` because the masked terms never
|
|
440
|
+
* set the same bit.
|
|
441
|
+
* @param a - selector word
|
|
442
|
+
* @param b - word chosen when selector bit is set
|
|
443
|
+
* @param c - word chosen when selector bit is clear
|
|
444
|
+
* @returns Mixed 32-bit word.
|
|
445
|
+
* @example
|
|
446
|
+
* Combine three words with the shared 32-bit choice primitive.
|
|
447
|
+
* ```ts
|
|
448
|
+
* Chi(0xffffffff, 0x12345678, 0x87654321);
|
|
449
|
+
* ```
|
|
450
|
+
*/
|
|
209
451
|
function Chi(a, b, c) {
|
|
210
452
|
return (a & b) ^ (~a & c);
|
|
211
453
|
}
|
|
212
|
-
/**
|
|
454
|
+
/**
|
|
455
|
+
* Shared 32-bit majority primitive reused by SHA-256 and SHA-1.
|
|
456
|
+
* Returns bits shared by at least two inputs.
|
|
457
|
+
* @param a - first input word
|
|
458
|
+
* @param b - second input word
|
|
459
|
+
* @param c - third input word
|
|
460
|
+
* @returns Mixed 32-bit word.
|
|
461
|
+
* @example
|
|
462
|
+
* Combine three words with the shared 32-bit majority primitive.
|
|
463
|
+
* ```ts
|
|
464
|
+
* Maj(0xffffffff, 0x12345678, 0x87654321);
|
|
465
|
+
* ```
|
|
466
|
+
*/
|
|
213
467
|
function Maj(a, b, c) {
|
|
214
468
|
return (a & b) ^ (a & c) ^ (b & c);
|
|
215
469
|
}
|
|
216
470
|
/**
|
|
217
471
|
* Merkle-Damgard hash construction base class.
|
|
218
472
|
* Could be used to create MD5, RIPEMD, SHA1, SHA2.
|
|
473
|
+
* Accepts only byte-aligned `Uint8Array` input, even when the underlying spec describes bit
|
|
474
|
+
* strings with partial-byte tails.
|
|
475
|
+
* @param blockLen - internal block size in bytes
|
|
476
|
+
* @param outputLen - digest size in bytes
|
|
477
|
+
* @param padOffset - trailing length field size in bytes
|
|
478
|
+
* @param isLE - whether length and state words are encoded in little-endian
|
|
479
|
+
* @example
|
|
480
|
+
* Use a concrete subclass to get the shared Merkle-Damgard update/digest flow.
|
|
481
|
+
* ```ts
|
|
482
|
+
* import { _SHA1 } from '@noble/hashes/legacy.js';
|
|
483
|
+
* const hash = new _SHA1();
|
|
484
|
+
* hash.update(new Uint8Array([97, 98, 99]));
|
|
485
|
+
* hash.digest();
|
|
486
|
+
* ```
|
|
219
487
|
*/
|
|
220
488
|
class HashMD {
|
|
221
489
|
blockLen;
|
|
222
490
|
outputLen;
|
|
491
|
+
canXOF = false;
|
|
223
492
|
padOffset;
|
|
224
493
|
isLE;
|
|
225
494
|
// For partial updates less than block size
|
|
@@ -244,7 +513,8 @@
|
|
|
244
513
|
const len = data.length;
|
|
245
514
|
for (let pos = 0; pos < len;) {
|
|
246
515
|
const take = Math.min(blockLen - this.pos, len - pos);
|
|
247
|
-
// Fast path
|
|
516
|
+
// Fast path only when there is no buffered partial block: `take === blockLen` implies
|
|
517
|
+
// `this.pos === 0`, so we can process full blocks directly from the input view.
|
|
248
518
|
if (take === blockLen) {
|
|
249
519
|
const dataView = createView(data);
|
|
250
520
|
for (; blockLen <= len - pos; pos += blockLen)
|
|
@@ -284,9 +554,9 @@
|
|
|
284
554
|
// Pad until full block byte with zeros
|
|
285
555
|
for (let i = pos; i < blockLen; i++)
|
|
286
556
|
buffer[i] = 0;
|
|
287
|
-
//
|
|
288
|
-
//
|
|
289
|
-
// So we
|
|
557
|
+
// `padOffset` reserves the whole length field. For SHA-384/512 the high 64 bits stay zero from
|
|
558
|
+
// the padding fill above, and JS will overflow before user input can make that half non-zero.
|
|
559
|
+
// So we only need to write the low 64 bits here.
|
|
290
560
|
view.setBigUint64(blockLen - 8, BigInt(this.length * 8), isLE);
|
|
291
561
|
this.process(view, 0);
|
|
292
562
|
const oview = createView(out);
|
|
@@ -304,6 +574,8 @@
|
|
|
304
574
|
digest() {
|
|
305
575
|
const { buffer, outputLen } = this;
|
|
306
576
|
this.digestInto(buffer);
|
|
577
|
+
// Copy before destroy(): subclasses wipe `buffer` during cleanup, but `digest()` must return
|
|
578
|
+
// fresh bytes to the caller.
|
|
307
579
|
const res = buffer.slice(0, outputLen);
|
|
308
580
|
this.destroy();
|
|
309
581
|
return res;
|
|
@@ -316,6 +588,8 @@
|
|
|
316
588
|
to.finished = finished;
|
|
317
589
|
to.length = length;
|
|
318
590
|
to.pos = pos;
|
|
591
|
+
// Only partial-block bytes need copying: when `length % blockLen === 0`, `pos === 0` and
|
|
592
|
+
// later `update()` / `digestInto()` overwrite `to.buffer` from the start before reading it.
|
|
319
593
|
if (length % blockLen)
|
|
320
594
|
to.buffer.set(buffer);
|
|
321
595
|
return to;
|
|
@@ -328,28 +602,32 @@
|
|
|
328
602
|
* Initial SHA-2 state: fractional parts of square roots of first 16 primes 2..53.
|
|
329
603
|
* Check out `test/misc/sha2-gen-iv.js` for recomputation guide.
|
|
330
604
|
*/
|
|
331
|
-
/** Initial SHA256 state.
|
|
605
|
+
/** Initial SHA256 state from RFC 6234 §6.1: the first 32 bits of the fractional parts of the
|
|
606
|
+
* square roots of the first eight prime numbers. Exported as a shared table; callers must treat
|
|
607
|
+
* it as read-only because constructors copy words from it by index. */
|
|
332
608
|
const SHA256_IV = /* @__PURE__ */ Uint32Array.from([
|
|
333
609
|
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
|
|
334
610
|
]);
|
|
335
|
-
/** Initial SHA512 state
|
|
611
|
+
/** Initial SHA512 state from RFC 6234 §6.3: eight RFC 64-bit `H(0)` words stored as sixteen
|
|
612
|
+
* big-endian 32-bit halves. Derived from the fractional parts of the square roots of the first
|
|
613
|
+
* eight prime numbers. Exported as a shared table; callers must treat it as read-only because
|
|
614
|
+
* constructors copy halves from it by index. */
|
|
336
615
|
const SHA512_IV = /* @__PURE__ */ Uint32Array.from([
|
|
337
616
|
0x6a09e667, 0xf3bcc908, 0xbb67ae85, 0x84caa73b, 0x3c6ef372, 0xfe94f82b, 0xa54ff53a, 0x5f1d36f1,
|
|
338
617
|
0x510e527f, 0xade682d1, 0x9b05688c, 0x2b3e6c1f, 0x1f83d9ab, 0xfb41bd6b, 0x5be0cd19, 0x137e2179,
|
|
339
618
|
]);
|
|
340
619
|
|
|
341
|
-
/**
|
|
342
|
-
* Internal helpers for u64. BigUint64Array is too slow as per 2025, so we implement it using Uint32Array.
|
|
343
|
-
* @todo re-check https://issues.chromium.org/issues/42212588
|
|
344
|
-
* @module
|
|
345
|
-
*/
|
|
346
620
|
const U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
|
|
347
621
|
const _32n = /* @__PURE__ */ BigInt(32);
|
|
622
|
+
// Split bigint into two 32-bit halves. With `le=true`, returned fields become `{ h: low, l: high
|
|
623
|
+
// }` to match little-endian word order rather than the property names.
|
|
348
624
|
function fromBig(n, le = false) {
|
|
349
625
|
if (le)
|
|
350
626
|
return { h: Number(n & U32_MASK64), l: Number((n >> _32n) & U32_MASK64) };
|
|
351
627
|
return { h: Number((n >> _32n) & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
|
|
352
628
|
}
|
|
629
|
+
// Split bigint list into `[highWords, lowWords]` when `le=false`; with `le=true`, the first array
|
|
630
|
+
// holds the low halves because `fromBig(...)` swaps the semantic meaning of `h` and `l`.
|
|
353
631
|
function split(lst, le = false) {
|
|
354
632
|
const len = lst.length;
|
|
355
633
|
let Ah = new Uint32Array(len);
|
|
@@ -360,30 +638,41 @@
|
|
|
360
638
|
}
|
|
361
639
|
return [Ah, Al];
|
|
362
640
|
}
|
|
363
|
-
// for
|
|
641
|
+
// High 32-bit half of a 64-bit logical right shift for `s` in `0..31`.
|
|
364
642
|
const shrSH = (h, _l, s) => h >>> s;
|
|
643
|
+
// Low 32-bit half of a 64-bit logical right shift, valid for `s` in `1..31`.
|
|
365
644
|
const shrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
|
|
366
|
-
//
|
|
645
|
+
// High 32-bit half of a 64-bit right rotate, valid for `s` in `1..31`.
|
|
367
646
|
const rotrSH = (h, l, s) => (h >>> s) | (l << (32 - s));
|
|
647
|
+
// Low 32-bit half of a 64-bit right rotate, valid for `s` in `1..31`.
|
|
368
648
|
const rotrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
|
|
369
|
-
//
|
|
649
|
+
// High 32-bit half of a 64-bit right rotate, valid for `s` in `33..63`; `32` uses `rotr32*`.
|
|
370
650
|
const rotrBH = (h, l, s) => (h << (64 - s)) | (l >>> (s - 32));
|
|
651
|
+
// Low 32-bit half of a 64-bit right rotate, valid for `s` in `33..63`; `32` uses `rotr32*`.
|
|
371
652
|
const rotrBL = (h, l, s) => (h >>> (s - 32)) | (l << (64 - s));
|
|
372
|
-
//
|
|
653
|
+
// High 32-bit half of a 64-bit right rotate for `s === 32`; this is just the swapped low half.
|
|
373
654
|
const rotr32H = (_h, l) => l;
|
|
655
|
+
// Low 32-bit half of a 64-bit right rotate for `s === 32`; this is just the swapped high half.
|
|
374
656
|
const rotr32L = (h, _l) => h;
|
|
375
|
-
//
|
|
376
|
-
//
|
|
657
|
+
// Add two split 64-bit words and return the split `{ h, l }` sum.
|
|
658
|
+
// JS uses 32-bit signed integers for bitwise operations, so we cannot simply shift the carry out
|
|
659
|
+
// of the low sum and instead use division.
|
|
377
660
|
function add(Ah, Al, Bh, Bl) {
|
|
378
661
|
const l = (Al >>> 0) + (Bl >>> 0);
|
|
379
662
|
return { h: (Ah + Bh + ((l / 2 ** 32) | 0)) | 0, l: l | 0 };
|
|
380
663
|
}
|
|
381
664
|
// Addition with more than 2 elements
|
|
665
|
+
// Unmasked low-word accumulator for 3-way addition; pass the raw result into `add3H(...)`.
|
|
382
666
|
const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
667
|
+
// High-word finalize step for 3-way addition; `low` must be the untruncated output of `add3L(...)`.
|
|
383
668
|
const add3H = (low, Ah, Bh, Ch) => (Ah + Bh + Ch + ((low / 2 ** 32) | 0)) | 0;
|
|
669
|
+
// Unmasked low-word accumulator for 4-way addition; pass the raw result into `add4H(...)`.
|
|
384
670
|
const add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
671
|
+
// High-word finalize step for 4-way addition; `low` must be the untruncated output of `add4L(...)`.
|
|
385
672
|
const add4H = (low, Ah, Bh, Ch, Dh) => (Ah + Bh + Ch + Dh + ((low / 2 ** 32) | 0)) | 0;
|
|
673
|
+
// Unmasked low-word accumulator for 5-way addition; pass the raw result into `add5H(...)`.
|
|
386
674
|
const add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
675
|
+
// High-word finalize step for 5-way addition; `low` must be the untruncated output of `add5L(...)`.
|
|
387
676
|
const add5H = (low, Ah, Bh, Ch, Dh, Eh) => (Ah + Bh + Ch + Dh + Eh + ((low / 2 ** 32) | 0)) | 0;
|
|
388
677
|
|
|
389
678
|
/**
|
|
@@ -391,14 +680,15 @@
|
|
|
391
680
|
* b could have been faster, but there is no fast u64 in js, so s is 1.5x faster.
|
|
392
681
|
* @module
|
|
393
682
|
*/
|
|
394
|
-
// Same as SHA512_IV
|
|
683
|
+
// Same IV words as `SHA512_IV`, but endian-swapped into LE u32 low/high halves
|
|
684
|
+
// for the BLAKE2b u64 helpers below.
|
|
395
685
|
const B2B_IV = /* @__PURE__ */ Uint32Array.from([
|
|
396
686
|
0xf3bcc908, 0x6a09e667, 0x84caa73b, 0xbb67ae85, 0xfe94f82b, 0x3c6ef372, 0x5f1d36f1, 0xa54ff53a,
|
|
397
687
|
0xade682d1, 0x510e527f, 0x2b3e6c1f, 0x9b05688c, 0xfb41bd6b, 0x1f83d9ab, 0x137e2179, 0x5be0cd19,
|
|
398
688
|
]);
|
|
399
|
-
//
|
|
689
|
+
// Shared synchronous BLAKE2b work vector as LE u32 low/high halves.
|
|
400
690
|
const BBUF = /* @__PURE__ */ new Uint32Array(32);
|
|
401
|
-
//
|
|
691
|
+
// BLAKE2b G mix split into two half-rounds over LE u32 low/high limbs.
|
|
402
692
|
function G1b(a, b, c, d, msg, x) {
|
|
403
693
|
// NOTE: V is LE here
|
|
404
694
|
const Xl = msg[x], Xh = msg[x + 1]; // prettier-ignore
|
|
@@ -423,6 +713,7 @@
|
|
|
423
713
|
((BBUF[2 * c] = Cl), (BBUF[2 * c + 1] = Ch));
|
|
424
714
|
((BBUF[2 * d] = Dl), (BBUF[2 * d + 1] = Dh));
|
|
425
715
|
}
|
|
716
|
+
// Second half-round of the same LE-limb BLAKE2b G mix; `x` is the message word offset.
|
|
426
717
|
function G2b(a, b, c, d, msg, x) {
|
|
427
718
|
// NOTE: V is LE here
|
|
428
719
|
const Xl = msg[x], Xh = msg[x + 1]; // prettier-ignore
|
|
@@ -449,9 +740,11 @@
|
|
|
449
740
|
}
|
|
450
741
|
function checkBlake2Opts(outputLen, opts = {}, keyLen, saltLen, persLen) {
|
|
451
742
|
anumber$1(keyLen);
|
|
452
|
-
|
|
743
|
+
// RFC 7693 §2.1 requires digest length nn in 1..keyLen.
|
|
744
|
+
if (outputLen <= 0 || outputLen > keyLen)
|
|
453
745
|
throw new Error('outputLen bigger than keyLen');
|
|
454
746
|
const { key, salt, personalization } = opts;
|
|
747
|
+
// This API uses `undefined` for the RFC 7693 `kk = 0` case, so a provided key must be non-empty.
|
|
455
748
|
if (key !== undefined && (key.length < 1 || key.length > keyLen))
|
|
456
749
|
throw new Error('"key" expected to be undefined or of length=1..' + keyLen);
|
|
457
750
|
if (salt !== undefined)
|
|
@@ -469,6 +762,7 @@
|
|
|
469
762
|
pos = 0;
|
|
470
763
|
blockLen;
|
|
471
764
|
outputLen;
|
|
765
|
+
canXOF = false;
|
|
472
766
|
constructor(blockLen, outputLen) {
|
|
473
767
|
anumber$1(blockLen);
|
|
474
768
|
anumber$1(outputLen);
|
|
@@ -498,7 +792,7 @@
|
|
|
498
792
|
}
|
|
499
793
|
const take = Math.min(blockLen - this.pos, len - pos);
|
|
500
794
|
const dataOffset = offset + pos;
|
|
501
|
-
//
|
|
795
|
+
// Zero-copy only for full, 4-byte-aligned, non-final blocks.
|
|
502
796
|
if (take === blockLen && !(dataOffset % 4) && pos + take < len) {
|
|
503
797
|
const data32 = new Uint32Array(buf, dataOffset, Math.floor((len - pos) / 4));
|
|
504
798
|
swap32IfBE(data32);
|
|
@@ -526,18 +820,33 @@
|
|
|
526
820
|
swap32IfBE(buffer32);
|
|
527
821
|
this.compress(buffer32, 0, true);
|
|
528
822
|
swap32IfBE(buffer32);
|
|
823
|
+
// Reject unaligned views explicitly instead of hiding them behind a full scratch copy.
|
|
824
|
+
if (out.byteOffset & 3)
|
|
825
|
+
throw new RangeError('"digestInto() output" expected 4-byte aligned byteOffset, got ' + out.byteOffset);
|
|
826
|
+
const state = this.get();
|
|
529
827
|
const out32 = u32(out);
|
|
530
|
-
|
|
828
|
+
const full = Math.floor(this.outputLen / 4);
|
|
829
|
+
for (let i = 0; i < full; i++)
|
|
830
|
+
out32[i] = swap8IfBE(state[i]);
|
|
831
|
+
const tail = this.outputLen % 4;
|
|
832
|
+
if (!tail)
|
|
833
|
+
return;
|
|
834
|
+
const off = full * 4;
|
|
835
|
+
const word = state[full];
|
|
836
|
+
for (let i = 0; i < tail; i++)
|
|
837
|
+
out[off + i] = word >>> (8 * i);
|
|
531
838
|
}
|
|
532
839
|
digest() {
|
|
533
840
|
const { buffer, outputLen } = this;
|
|
534
841
|
this.digestInto(buffer);
|
|
842
|
+
// Return a copy so callers do not alias the instance scratch buffer used during finalization.
|
|
535
843
|
const res = buffer.slice(0, outputLen);
|
|
536
844
|
this.destroy();
|
|
537
845
|
return res;
|
|
538
846
|
}
|
|
539
847
|
_cloneInto(to) {
|
|
540
848
|
const { buffer, length, finished, destroyed, outputLen, pos } = this;
|
|
849
|
+
// Recreate only `dkLen`; key/salt/personalization are already absorbed into the copied state.
|
|
541
850
|
to ||= new this.constructor({ dkLen: outputLen });
|
|
542
851
|
to.set(...this.get());
|
|
543
852
|
to.buffer.set(buffer);
|
|
@@ -553,9 +862,9 @@
|
|
|
553
862
|
return this._cloneInto();
|
|
554
863
|
}
|
|
555
864
|
}
|
|
556
|
-
/** Internal blake2b hash class. */
|
|
865
|
+
/** Internal blake2b hash class with state stored as LE u32 low/high halves. */
|
|
557
866
|
class _BLAKE2b extends _BLAKE2 {
|
|
558
|
-
// Same as SHA-512,
|
|
867
|
+
// Same IV words as SHA-512 / BLAKE2b, encoded as LE u32 low/high halves.
|
|
559
868
|
v0l = B2B_IV[0] | 0;
|
|
560
869
|
v0h = B2B_IV[1] | 0;
|
|
561
870
|
v1l = B2B_IV[2] | 0;
|
|
@@ -582,6 +891,8 @@
|
|
|
582
891
|
abytes(key, undefined, 'key');
|
|
583
892
|
keyLength = key.length;
|
|
584
893
|
}
|
|
894
|
+
// RFC 7693 §2.5: xor `p[0] = 0x0101kknn` into the low 32 bits of `h[0]`;
|
|
895
|
+
// the high 32 bits stay at `IV[0]`.
|
|
585
896
|
this.v0l ^= this.outputLen | (keyLength << 8) | (0x01 << 16) | (0x01 << 24);
|
|
586
897
|
if (salt !== undefined) {
|
|
587
898
|
abytes(salt, undefined, 'salt');
|
|
@@ -643,6 +954,8 @@
|
|
|
643
954
|
}
|
|
644
955
|
let j = 0;
|
|
645
956
|
const s = BSIGMA;
|
|
957
|
+
// SIGMA selects 64-bit message words; multiply by 2 because `msg` stores
|
|
958
|
+
// each word as [low32, high32].
|
|
646
959
|
for (let i = 0; i < 12; i++) {
|
|
647
960
|
G1b(0, 4, 8, 12, msg, offset + 2 * s[j++]);
|
|
648
961
|
G2b(0, 4, 8, 12, msg, offset + 2 * s[j++]);
|
|
@@ -688,7 +1001,15 @@
|
|
|
688
1001
|
/**
|
|
689
1002
|
* Blake2b hash function. 64-bit. 1.5x slower than blake2s in JS.
|
|
690
1003
|
* @param msg - message that would be hashed
|
|
691
|
-
* @param opts -
|
|
1004
|
+
* @param opts - Optional output, MAC, salt, and personalization settings.
|
|
1005
|
+
* `dkLen` must be 1..64 bytes; `salt` and `personalization`, if present,
|
|
1006
|
+
* must be 16 bytes each. See {@link Blake2Opts}.
|
|
1007
|
+
* @returns Digest bytes.
|
|
1008
|
+
* @example
|
|
1009
|
+
* Hash a message with Blake2b.
|
|
1010
|
+
* ```ts
|
|
1011
|
+
* blake2b(new Uint8Array([97, 98, 99]));
|
|
1012
|
+
* ```
|
|
692
1013
|
*/
|
|
693
1014
|
const blake2b = /* @__PURE__ */ createHasher((opts) => new _BLAKE2b(opts));
|
|
694
1015
|
|
|
@@ -1052,12 +1373,17 @@
|
|
|
1052
1373
|
* HMAC: RFC2104 message authentication code.
|
|
1053
1374
|
* @module
|
|
1054
1375
|
*/
|
|
1055
|
-
/**
|
|
1376
|
+
/**
|
|
1377
|
+
* Internal class for HMAC.
|
|
1378
|
+
* Accepts any byte key, although RFC 2104 §3 recommends keys at least
|
|
1379
|
+
* `HashLen` bytes long.
|
|
1380
|
+
*/
|
|
1056
1381
|
class _HMAC {
|
|
1057
1382
|
oHash;
|
|
1058
1383
|
iHash;
|
|
1059
1384
|
blockLen;
|
|
1060
1385
|
outputLen;
|
|
1386
|
+
canXOF = false;
|
|
1061
1387
|
finished = false;
|
|
1062
1388
|
destroyed = false;
|
|
1063
1389
|
constructor(hash, key) {
|
|
@@ -1075,7 +1401,8 @@
|
|
|
1075
1401
|
for (let i = 0; i < pad.length; i++)
|
|
1076
1402
|
pad[i] ^= 0x36;
|
|
1077
1403
|
this.iHash.update(pad);
|
|
1078
|
-
// By doing update (processing of first block) of outer hash here
|
|
1404
|
+
// By doing update (processing of the first block) of the outer hash here,
|
|
1405
|
+
// we can re-use it between multiple calls via clone.
|
|
1079
1406
|
this.oHash = hash.create();
|
|
1080
1407
|
// Undo internal XOR && apply outer XOR
|
|
1081
1408
|
for (let i = 0; i < pad.length; i++)
|
|
@@ -1090,11 +1417,14 @@
|
|
|
1090
1417
|
}
|
|
1091
1418
|
digestInto(out) {
|
|
1092
1419
|
aexists(this);
|
|
1093
|
-
|
|
1420
|
+
aoutput(out, this);
|
|
1094
1421
|
this.finished = true;
|
|
1095
|
-
this.
|
|
1096
|
-
|
|
1097
|
-
|
|
1422
|
+
const buf = out.subarray(0, this.outputLen);
|
|
1423
|
+
// Reuse the first outputLen bytes for the inner digest; the outer hash consumes them before
|
|
1424
|
+
// overwriting that same prefix with the final tag, leaving any oversized tail untouched.
|
|
1425
|
+
this.iHash.digestInto(buf);
|
|
1426
|
+
this.oHash.update(buf);
|
|
1427
|
+
this.oHash.digestInto(buf);
|
|
1098
1428
|
this.destroy();
|
|
1099
1429
|
}
|
|
1100
1430
|
digest() {
|
|
@@ -1103,7 +1433,8 @@
|
|
|
1103
1433
|
return out;
|
|
1104
1434
|
}
|
|
1105
1435
|
_cloneInto(to) {
|
|
1106
|
-
// Create new instance without calling constructor since key
|
|
1436
|
+
// Create new instance without calling constructor since the key
|
|
1437
|
+
// is already in state and we don't know it.
|
|
1107
1438
|
to ||= Object.create(Object.getPrototypeOf(this), {});
|
|
1108
1439
|
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
|
1109
1440
|
to = to;
|
|
@@ -1124,18 +1455,11 @@
|
|
|
1124
1455
|
this.iHash.destroy();
|
|
1125
1456
|
}
|
|
1126
1457
|
}
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
* @example
|
|
1133
|
-
* import { hmac } from '@noble/hashes/hmac';
|
|
1134
|
-
* import { sha256 } from '@noble/hashes/sha2';
|
|
1135
|
-
* const mac1 = hmac(sha256, 'key', 'message');
|
|
1136
|
-
*/
|
|
1137
|
-
const hmac = (hash, key, message) => new _HMAC(hash, key).update(message).digest();
|
|
1138
|
-
hmac.create = (hash, key) => new _HMAC(hash, key);
|
|
1458
|
+
const hmac = /* @__PURE__ */ (() => {
|
|
1459
|
+
const hmac_ = ((hash, key, message) => new _HMAC(hash, key).update(message).digest());
|
|
1460
|
+
hmac_.create = (hash, key) => new _HMAC(hash, key);
|
|
1461
|
+
return hmac_;
|
|
1462
|
+
})();
|
|
1139
1463
|
|
|
1140
1464
|
/**
|
|
1141
1465
|
* PBKDF (RFC 2898). Can be used to create a key from password and salt.
|
|
@@ -1151,16 +1475,26 @@
|
|
|
1151
1475
|
anumber$1(asyncTick, 'asyncTick');
|
|
1152
1476
|
if (c < 1)
|
|
1153
1477
|
throw new Error('iterations (c) must be >= 1');
|
|
1478
|
+
// RFC 8018 §5.2 defines `dkLen` as "a positive integer".
|
|
1479
|
+
if (dkLen < 1)
|
|
1480
|
+
throw new Error('"dkLen" must be >= 1');
|
|
1481
|
+
// RFC 8018 §5.2 step 1 requires rejecting oversize `dkLen`
|
|
1482
|
+
// before allocating the destination buffer.
|
|
1483
|
+
if (dkLen > (2 ** 32 - 1) * hash.outputLen)
|
|
1484
|
+
throw new Error('derived key too long');
|
|
1154
1485
|
const password = kdfInputToBytes(_password, 'password');
|
|
1155
1486
|
const salt = kdfInputToBytes(_salt, 'salt');
|
|
1156
1487
|
// DK = PBKDF2(PRF, Password, Salt, c, dkLen);
|
|
1157
1488
|
const DK = new Uint8Array(dkLen);
|
|
1158
1489
|
// U1 = PRF(Password, Salt + INT_32_BE(i))
|
|
1159
1490
|
const PRF = hmac.create(hash, password);
|
|
1491
|
+
// Cache PRF(P, S || ...) prefix state so each block only appends INT_32_BE(i).
|
|
1160
1492
|
const PRFSalt = PRF._cloneInto().update(salt);
|
|
1161
1493
|
return { c, dkLen, asyncTick, DK, PRF, PRFSalt };
|
|
1162
1494
|
}
|
|
1163
1495
|
function pbkdf2Output(PRF, PRFSalt, DK, prfW, u) {
|
|
1496
|
+
// Shared sync/async cleanup point: wipe transient PRF state
|
|
1497
|
+
// while preserving the derived key buffer.
|
|
1164
1498
|
PRF.destroy();
|
|
1165
1499
|
PRFSalt.destroy();
|
|
1166
1500
|
if (prfW)
|
|
@@ -1169,13 +1503,22 @@
|
|
|
1169
1503
|
return DK;
|
|
1170
1504
|
}
|
|
1171
1505
|
/**
|
|
1172
|
-
* PBKDF2-HMAC: RFC
|
|
1506
|
+
* PBKDF2-HMAC: RFC 8018 key derivation function.
|
|
1173
1507
|
* @param hash - hash function that would be used e.g. sha256
|
|
1174
|
-
* @param password - password from which a derived key is generated
|
|
1175
|
-
*
|
|
1176
|
-
* @param
|
|
1508
|
+
* @param password - password from which a derived key is generated;
|
|
1509
|
+
* JS string inputs are UTF-8 encoded first
|
|
1510
|
+
* @param salt - cryptographic salt; JS string inputs are UTF-8 encoded first
|
|
1511
|
+
* @param opts - PBKDF2 work factor and output settings. `dkLen`, if provided,
|
|
1512
|
+
* must be `>= 1` per RFC 8018 §5.2. See {@link Pbkdf2Opt}.
|
|
1513
|
+
* @returns Derived key bytes.
|
|
1514
|
+
* @throws If the PBKDF2 iteration count or derived-key settings are invalid. {@link Error}
|
|
1177
1515
|
* @example
|
|
1516
|
+
* PBKDF2-HMAC: RFC 2898 key derivation function.
|
|
1517
|
+
* ```ts
|
|
1518
|
+
* import { pbkdf2 } from '@noble/hashes/pbkdf2.js';
|
|
1519
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
1178
1520
|
* const key = pbkdf2(sha256, 'password', 'salt', { dkLen: 32, c: Math.pow(2, 18) });
|
|
1521
|
+
* ```
|
|
1179
1522
|
*/
|
|
1180
1523
|
function pbkdf2(hash, password, salt, opts) {
|
|
1181
1524
|
const { c, dkLen, DK, PRF, PRFSalt } = pbkdf2Init(hash, password, salt, opts);
|
|
@@ -1186,6 +1529,8 @@
|
|
|
1186
1529
|
// DK = T1 + T2 + ⋯ + Tdklen/hlen
|
|
1187
1530
|
for (let ti = 1, pos = 0; pos < dkLen; ti++, pos += PRF.outputLen) {
|
|
1188
1531
|
// Ti = F(Password, Salt, c, i)
|
|
1532
|
+
// The last Ti view can be shorter than hLen, which applies
|
|
1533
|
+
// RFC 8018 §5.2 step 4's T_l<0..r-1> truncation without extra copies.
|
|
1189
1534
|
const Ti = DK.subarray(pos, pos + PRF.outputLen);
|
|
1190
1535
|
view.setInt32(0, ti, false);
|
|
1191
1536
|
// F(Password, Salt, c, i) = U1 ^ U2 ^ ⋯ ^ Uc
|
|
@@ -1205,13 +1550,13 @@
|
|
|
1205
1550
|
/**
|
|
1206
1551
|
* SHA2 hash function. A.k.a. sha256, sha384, sha512, sha512_224, sha512_256.
|
|
1207
1552
|
* SHA256 is the fastest hash implementable in JS, even faster than Blake3.
|
|
1208
|
-
* Check out
|
|
1209
|
-
*
|
|
1553
|
+
* Check out {@link https://www.rfc-editor.org/rfc/rfc4634 | RFC 4634} and
|
|
1554
|
+
* {@link https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf | FIPS 180-4}.
|
|
1210
1555
|
* @module
|
|
1211
1556
|
*/
|
|
1212
1557
|
/**
|
|
1213
|
-
*
|
|
1214
|
-
*
|
|
1558
|
+
* SHA-224 / SHA-256 round constants from RFC 6234 §5.1: the first 32 bits
|
|
1559
|
+
* of the cube roots of the first 64 primes (2..311).
|
|
1215
1560
|
*/
|
|
1216
1561
|
// prettier-ignore
|
|
1217
1562
|
const SHA256_K = /* @__PURE__ */ Uint32Array.from([
|
|
@@ -1224,9 +1569,9 @@
|
|
|
1224
1569
|
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
1225
1570
|
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
1226
1571
|
]);
|
|
1227
|
-
/** Reusable
|
|
1572
|
+
/** Reusable SHA-224 / SHA-256 message schedule buffer `W_t` from RFC 6234 §6.2 step 1. */
|
|
1228
1573
|
const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
1229
|
-
/** Internal
|
|
1574
|
+
/** Internal SHA-224 / SHA-256 compression engine from RFC 6234 §6.2. */
|
|
1230
1575
|
class SHA2_32B extends HashMD {
|
|
1231
1576
|
constructor(outputLen) {
|
|
1232
1577
|
super(64, outputLen, 8, false);
|
|
@@ -1288,11 +1633,14 @@
|
|
|
1288
1633
|
clean(SHA256_W);
|
|
1289
1634
|
}
|
|
1290
1635
|
destroy() {
|
|
1636
|
+
// HashMD callers route post-destroy usability through `destroyed`; zeroizing alone still leaves
|
|
1637
|
+
// update()/digest() callable on reused instances.
|
|
1638
|
+
this.destroyed = true;
|
|
1291
1639
|
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
1292
1640
|
clean(this.buffer);
|
|
1293
1641
|
}
|
|
1294
1642
|
}
|
|
1295
|
-
/** Internal
|
|
1643
|
+
/** Internal SHA-256 hash class grounded in RFC 6234 §6.2. */
|
|
1296
1644
|
class _SHA256 extends SHA2_32B {
|
|
1297
1645
|
// We cannot use array here since array allows indexing by variable
|
|
1298
1646
|
// which means optimizer/compiler cannot use registers.
|
|
@@ -1309,8 +1657,8 @@
|
|
|
1309
1657
|
}
|
|
1310
1658
|
}
|
|
1311
1659
|
// SHA2-512 is slower than sha256 in js because u64 operations are slow.
|
|
1312
|
-
//
|
|
1313
|
-
//
|
|
1660
|
+
// SHA-384 / SHA-512 round constants from RFC 6234 §5.2:
|
|
1661
|
+
// 80 full 64-bit words split into high/low halves.
|
|
1314
1662
|
// prettier-ignore
|
|
1315
1663
|
const K512 = /* @__PURE__ */ (() => split([
|
|
1316
1664
|
'0x428a2f98d728ae22', '0x7137449123ef65cd', '0xb5c0fbcfec4d3b2f', '0xe9b5dba58189dbbc',
|
|
@@ -1336,10 +1684,11 @@
|
|
|
1336
1684
|
].map(n => BigInt(n))))();
|
|
1337
1685
|
const SHA512_Kh = /* @__PURE__ */ (() => K512[0])();
|
|
1338
1686
|
const SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
|
|
1339
|
-
// Reusable
|
|
1687
|
+
// Reusable high-half schedule buffer for the RFC 6234 §6.4 64-bit `W_t` words.
|
|
1340
1688
|
const SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
|
|
1689
|
+
// Reusable low-half schedule buffer for the RFC 6234 §6.4 64-bit `W_t` words.
|
|
1341
1690
|
const SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
|
|
1342
|
-
/** Internal
|
|
1691
|
+
/** Internal SHA-384 / SHA-512 compression engine from RFC 6234 §6.4. */
|
|
1343
1692
|
class SHA2_64B extends HashMD {
|
|
1344
1693
|
constructor(outputLen) {
|
|
1345
1694
|
super(128, outputLen, 16, false);
|
|
@@ -1385,7 +1734,7 @@
|
|
|
1385
1734
|
const W2l = SHA512_W_L[i - 2] | 0;
|
|
1386
1735
|
const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
|
|
1387
1736
|
const s1l = rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6);
|
|
1388
|
-
//
|
|
1737
|
+
// SHA512_W[i] = s0 + s1 + SHA512_W[i - 7] + SHA512_W[i - 16];
|
|
1389
1738
|
const SUMl = add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
|
|
1390
1739
|
const SUMh = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
|
|
1391
1740
|
SHA512_W_H[i] = SUMh | 0;
|
|
@@ -1442,11 +1791,14 @@
|
|
|
1442
1791
|
clean(SHA512_W_H, SHA512_W_L);
|
|
1443
1792
|
}
|
|
1444
1793
|
destroy() {
|
|
1794
|
+
// HashMD callers route post-destroy usability through `destroyed`; zeroizing alone still leaves
|
|
1795
|
+
// update()/digest() callable on reused instances.
|
|
1796
|
+
this.destroyed = true;
|
|
1445
1797
|
clean(this.buffer);
|
|
1446
1798
|
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
1447
1799
|
}
|
|
1448
1800
|
}
|
|
1449
|
-
/** Internal
|
|
1801
|
+
/** Internal SHA-512 hash class grounded in RFC 6234 §6.3 and §6.4. */
|
|
1450
1802
|
class _SHA512 extends SHA2_64B {
|
|
1451
1803
|
Ah = SHA512_IV[0] | 0;
|
|
1452
1804
|
Al = SHA512_IV[1] | 0;
|
|
@@ -1475,16 +1827,40 @@
|
|
|
1475
1827
|
* - BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
|
|
1476
1828
|
* - Each sha256 hash is executing 2^18 bit operations.
|
|
1477
1829
|
* - Good 2024 ASICs can do 200Th/sec with 3500 watts of power, corresponding to 2^36 hashes/joule.
|
|
1830
|
+
* @param msg - message bytes to hash
|
|
1831
|
+
* @returns Digest bytes.
|
|
1832
|
+
* @example
|
|
1833
|
+
* Hash a message with SHA2-256.
|
|
1834
|
+
* ```ts
|
|
1835
|
+
* sha256(new Uint8Array([97, 98, 99]));
|
|
1836
|
+
* ```
|
|
1478
1837
|
*/
|
|
1479
1838
|
const sha256 = /* @__PURE__ */ createHasher(() => new _SHA256(),
|
|
1480
1839
|
/* @__PURE__ */ oidNist(0x01));
|
|
1481
|
-
/**
|
|
1840
|
+
/**
|
|
1841
|
+
* SHA2-512 hash function from RFC 4634.
|
|
1842
|
+
* @param msg - message bytes to hash
|
|
1843
|
+
* @returns Digest bytes.
|
|
1844
|
+
* @example
|
|
1845
|
+
* Hash a message with SHA2-512.
|
|
1846
|
+
* ```ts
|
|
1847
|
+
* sha512(new Uint8Array([97, 98, 99]));
|
|
1848
|
+
* ```
|
|
1849
|
+
*/
|
|
1482
1850
|
const sha512 = /* @__PURE__ */ createHasher(() => new _SHA512(),
|
|
1483
1851
|
/* @__PURE__ */ oidNist(0x03));
|
|
1484
1852
|
|
|
1485
1853
|
/*! scure-base - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
1486
1854
|
function isBytes(a) {
|
|
1487
|
-
|
|
1855
|
+
// Plain `instanceof Uint8Array` is too strict for some Buffer / proxy / cross-realm cases. The
|
|
1856
|
+
// fallback still requires a real ArrayBuffer view, so plain JSON-deserialized
|
|
1857
|
+
// `{ constructor: ... }` spoofing is rejected. `BYTES_PER_ELEMENT === 1` keeps the
|
|
1858
|
+
// fallback on byte-oriented views.
|
|
1859
|
+
return (a instanceof Uint8Array ||
|
|
1860
|
+
(ArrayBuffer.isView(a) &&
|
|
1861
|
+
a.constructor.name === 'Uint8Array' &&
|
|
1862
|
+
'BYTES_PER_ELEMENT' in a &&
|
|
1863
|
+
a.BYTES_PER_ELEMENT === 1));
|
|
1488
1864
|
}
|
|
1489
1865
|
function isArrayOf(isString, arr) {
|
|
1490
1866
|
if (!Array.isArray(arr))
|
|
@@ -1500,29 +1876,31 @@
|
|
|
1500
1876
|
}
|
|
1501
1877
|
function afn(input) {
|
|
1502
1878
|
if (typeof input !== 'function')
|
|
1503
|
-
throw new
|
|
1879
|
+
throw new TypeError('function expected');
|
|
1504
1880
|
return true;
|
|
1505
1881
|
}
|
|
1506
1882
|
function astr(label, input) {
|
|
1507
1883
|
if (typeof input !== 'string')
|
|
1508
|
-
throw new
|
|
1884
|
+
throw new TypeError(`${label}: string expected`);
|
|
1509
1885
|
return true;
|
|
1510
1886
|
}
|
|
1511
1887
|
function anumber(n) {
|
|
1888
|
+
if (typeof n !== 'number')
|
|
1889
|
+
throw new TypeError(`number expected, got ${typeof n}`);
|
|
1512
1890
|
if (!Number.isSafeInteger(n))
|
|
1513
|
-
throw new
|
|
1891
|
+
throw new RangeError(`invalid integer: ${n}`);
|
|
1514
1892
|
}
|
|
1515
1893
|
function aArr(input) {
|
|
1516
1894
|
if (!Array.isArray(input))
|
|
1517
|
-
throw new
|
|
1895
|
+
throw new TypeError('array expected');
|
|
1518
1896
|
}
|
|
1519
1897
|
function astrArr(label, input) {
|
|
1520
1898
|
if (!isArrayOf(true, input))
|
|
1521
|
-
throw new
|
|
1899
|
+
throw new TypeError(`${label}: array of strings expected`);
|
|
1522
1900
|
}
|
|
1523
1901
|
function anumArr(label, input) {
|
|
1524
1902
|
if (!isArrayOf(false, input))
|
|
1525
|
-
throw new
|
|
1903
|
+
throw new TypeError(`${label}: array of numbers expected`);
|
|
1526
1904
|
}
|
|
1527
1905
|
/**
|
|
1528
1906
|
* @__NO_SIDE_EFFECTS__
|
|
@@ -1575,6 +1953,8 @@
|
|
|
1575
1953
|
*/
|
|
1576
1954
|
function join(separator = '') {
|
|
1577
1955
|
astr('join', separator);
|
|
1956
|
+
// join('') is only lossless when each chunk is already unambiguous, such as single-symbol alphabets.
|
|
1957
|
+
// Multi-character tokens need a separator that cannot appear inside the chunks.
|
|
1578
1958
|
return {
|
|
1579
1959
|
encode: (from) => {
|
|
1580
1960
|
astrArr('join.decode', from);
|
|
@@ -1596,6 +1976,8 @@
|
|
|
1596
1976
|
return {
|
|
1597
1977
|
encode(data) {
|
|
1598
1978
|
astrArr('padding.encode', data);
|
|
1979
|
+
// Mutates the intermediate token array in place while appending pad chars.
|
|
1980
|
+
// utils.padding callers that need to preserve their input should pass a copy.
|
|
1599
1981
|
while ((data.length * bits) % 8)
|
|
1600
1982
|
data.push(chr);
|
|
1601
1983
|
return data;
|
|
@@ -1621,9 +2003,9 @@
|
|
|
1621
2003
|
function convertRadix(data, from, to) {
|
|
1622
2004
|
// base 1 is impossible
|
|
1623
2005
|
if (from < 2)
|
|
1624
|
-
throw new
|
|
2006
|
+
throw new RangeError(`convertRadix: invalid from=${from}, base cannot be less than 2`);
|
|
1625
2007
|
if (to < 2)
|
|
1626
|
-
throw new
|
|
2008
|
+
throw new RangeError(`convertRadix: invalid to=${to}, base cannot be less than 2`);
|
|
1627
2009
|
aArr(data);
|
|
1628
2010
|
if (!data.length)
|
|
1629
2011
|
return [];
|
|
@@ -1665,11 +2047,14 @@
|
|
|
1665
2047
|
if (done)
|
|
1666
2048
|
break;
|
|
1667
2049
|
}
|
|
2050
|
+
// Preserve explicit leading zero digits so callers like base58 keep zero-prefix semantics.
|
|
1668
2051
|
for (let i = 0; i < data.length - 1 && data[i] === 0; i++)
|
|
1669
2052
|
res.push(0);
|
|
1670
2053
|
return res.reverse();
|
|
1671
2054
|
}
|
|
1672
2055
|
const gcd = (a, b) => (b === 0 ? a : gcd(b, a % b));
|
|
2056
|
+
// Maximum carry width before the `pos` cycle repeats.
|
|
2057
|
+
// Residues advance in gcd(from, to) steps, so the largest pre-drain width is from + (to - gcd).
|
|
1673
2058
|
const radix2carry = /* @__NO_SIDE_EFFECTS__ */ (from, to) => from + (to - gcd(from, to));
|
|
1674
2059
|
const powers = /* @__PURE__ */ (() => {
|
|
1675
2060
|
let res = [];
|
|
@@ -1683,9 +2068,9 @@
|
|
|
1683
2068
|
function convertRadix2(data, from, to, padding) {
|
|
1684
2069
|
aArr(data);
|
|
1685
2070
|
if (from <= 0 || from > 32)
|
|
1686
|
-
throw new
|
|
2071
|
+
throw new RangeError(`convertRadix2: wrong from=${from}`);
|
|
1687
2072
|
if (to <= 0 || to > 32)
|
|
1688
|
-
throw new
|
|
2073
|
+
throw new RangeError(`convertRadix2: wrong to=${to}`);
|
|
1689
2074
|
if (radix2carry(from, to) > 32) {
|
|
1690
2075
|
throw new Error(`convertRadix2: carry overflow from=${from} to=${to} carryBits=${radix2carry(from, to)}`);
|
|
1691
2076
|
}
|
|
@@ -1710,6 +2095,8 @@
|
|
|
1710
2095
|
carry &= pow - 1; // clean carry, otherwise it will cause overflow
|
|
1711
2096
|
}
|
|
1712
2097
|
carry = (carry << (to - pos)) & mask;
|
|
2098
|
+
// Canonical decode paths reject leftover whole input words and non-zero pad bits.
|
|
2099
|
+
// For Bech32 5->8 regrouping, this is the "4 bits or less, all zeroes" tail rule.
|
|
1713
2100
|
if (!padding && pos >= from)
|
|
1714
2101
|
throw new Error('Excess padding');
|
|
1715
2102
|
if (!padding && carry > 0)
|
|
@@ -1724,10 +2111,11 @@
|
|
|
1724
2111
|
function radix(num) {
|
|
1725
2112
|
anumber(num);
|
|
1726
2113
|
const _256 = 2 ** 8;
|
|
2114
|
+
// Base-range and carry-overflow checks live in convertRadix so encode/decode reject unsupported bases symmetrically.
|
|
1727
2115
|
return {
|
|
1728
2116
|
encode: (bytes) => {
|
|
1729
2117
|
if (!isBytes(bytes))
|
|
1730
|
-
throw new
|
|
2118
|
+
throw new TypeError('radix.encode input should be Uint8Array');
|
|
1731
2119
|
return convertRadix(Array.from(bytes), _256, num);
|
|
1732
2120
|
},
|
|
1733
2121
|
decode: (digits) => {
|
|
@@ -1744,13 +2132,15 @@
|
|
|
1744
2132
|
function radix2(bits, revPadding = false) {
|
|
1745
2133
|
anumber(bits);
|
|
1746
2134
|
if (bits <= 0 || bits > 32)
|
|
1747
|
-
throw new
|
|
2135
|
+
throw new RangeError('radix2: bits should be in (0..32]');
|
|
1748
2136
|
if (radix2carry(8, bits) > 32 || radix2carry(bits, 8) > 32)
|
|
1749
|
-
throw new
|
|
2137
|
+
throw new RangeError('radix2: carry overflow');
|
|
2138
|
+
// revPadding flips which direction allows a partial zero tail.
|
|
2139
|
+
// Default pads 8->bits and rejects extra bits on bits->8; `true` does the opposite.
|
|
1750
2140
|
return {
|
|
1751
2141
|
encode: (bytes) => {
|
|
1752
2142
|
if (!isBytes(bytes))
|
|
1753
|
-
throw new
|
|
2143
|
+
throw new TypeError('radix2.encode input should be Uint8Array');
|
|
1754
2144
|
return convertRadix2(Array.from(bytes), 8, bits, !revPadding);
|
|
1755
2145
|
},
|
|
1756
2146
|
decode: (digits) => {
|
|
@@ -1761,12 +2151,19 @@
|
|
|
1761
2151
|
}
|
|
1762
2152
|
function checksum(len, fn) {
|
|
1763
2153
|
anumber(len);
|
|
2154
|
+
// Reject degenerate zero-byte checksums up front so callers don't accidentally
|
|
2155
|
+
// build a no-op checksum stage.
|
|
2156
|
+
if (len <= 0)
|
|
2157
|
+
throw new RangeError(`checksum length must be positive: ${len}`);
|
|
1764
2158
|
afn(fn);
|
|
2159
|
+
const _fn = fn;
|
|
2160
|
+
// Uses the first `len` bytes of fn(data) in both directions.
|
|
2161
|
+
// Current call sites rely on `len > 0` and checksum functions that return at least that many bytes.
|
|
1765
2162
|
return {
|
|
1766
2163
|
encode(data) {
|
|
1767
2164
|
if (!isBytes(data))
|
|
1768
|
-
throw new
|
|
1769
|
-
const sum =
|
|
2165
|
+
throw new TypeError('checksum.encode: input should be Uint8Array');
|
|
2166
|
+
const sum = _fn(data).slice(0, len);
|
|
1770
2167
|
const res = new Uint8Array(data.length + len);
|
|
1771
2168
|
res.set(data);
|
|
1772
2169
|
res.set(sum, data.length);
|
|
@@ -1774,10 +2171,10 @@
|
|
|
1774
2171
|
},
|
|
1775
2172
|
decode(data) {
|
|
1776
2173
|
if (!isBytes(data))
|
|
1777
|
-
throw new
|
|
2174
|
+
throw new TypeError('checksum.decode: input should be Uint8Array');
|
|
1778
2175
|
const payload = data.slice(0, -len);
|
|
1779
2176
|
const oldChecksum = data.slice(-len);
|
|
1780
|
-
const newChecksum =
|
|
2177
|
+
const newChecksum = _fn(payload).slice(0, len);
|
|
1781
2178
|
for (let i = 0; i < len; i++)
|
|
1782
2179
|
if (newChecksum[i] !== oldChecksum[i])
|
|
1783
2180
|
throw new Error('Invalid checksum');
|
|
@@ -1786,20 +2183,33 @@
|
|
|
1786
2183
|
};
|
|
1787
2184
|
}
|
|
1788
2185
|
// prettier-ignore
|
|
1789
|
-
|
|
2186
|
+
/**
|
|
2187
|
+
* Low-level building blocks used by the exported codecs.
|
|
2188
|
+
* @example
|
|
2189
|
+
* Build a radix-32 coder from the low-level helpers.
|
|
2190
|
+
* ```ts
|
|
2191
|
+
* import { utils } from '@scure/base';
|
|
2192
|
+
* utils.radix2(5).encode(Uint8Array.from([1, 2, 3]));
|
|
2193
|
+
* ```
|
|
2194
|
+
*/
|
|
2195
|
+
const utils = /* @__PURE__ */ Object.freeze({
|
|
1790
2196
|
alphabet, chain, checksum, convertRadix, convertRadix2, radix, radix2, join, padding,
|
|
1791
|
-
};
|
|
2197
|
+
});
|
|
1792
2198
|
|
|
1793
2199
|
/*! scure-bip39 - MIT License (c) 2022 Patricio Palladino, Paul Miller (paulmillr.com) */
|
|
1794
2200
|
// Normalization replaces equivalent sequences of characters
|
|
1795
2201
|
// so that any two texts that are equivalent will be reduced
|
|
1796
2202
|
// to the same sequence of code points, called the normal form of the original text.
|
|
1797
2203
|
// https://tonsky.me/blog/unicode/#why-is-a----
|
|
2204
|
+
// BIP-39 requires UTF-8 NFKD for localized wordlists and mnemonic sentences.
|
|
2205
|
+
// It also applies NFKD to the "mnemonic" + passphrase salt.
|
|
1798
2206
|
function nfkd(str) {
|
|
1799
2207
|
if (typeof str !== 'string')
|
|
1800
2208
|
throw new TypeError('invalid mnemonic type: ' + typeof str);
|
|
1801
2209
|
return str.normalize('NFKD');
|
|
1802
2210
|
}
|
|
2211
|
+
// BIP-39 mnemonics are consumed in NFKD form.
|
|
2212
|
+
// They must contain 12, 15, 18, 21, or 24 words before checksum validation.
|
|
1803
2213
|
function normalize(str) {
|
|
1804
2214
|
const norm = nfkd(str);
|
|
1805
2215
|
const words = norm.split(' ');
|
|
@@ -1807,10 +2217,11 @@
|
|
|
1807
2217
|
throw new Error('Invalid mnemonic');
|
|
1808
2218
|
return { nfkd: norm, words };
|
|
1809
2219
|
}
|
|
2220
|
+
// BIP-39 entropy payloads are 128-256 bits in 32-bit increments, i.e. 16/20/24/28/32 bytes.
|
|
1810
2221
|
function aentropy(ent) {
|
|
1811
2222
|
abytes(ent);
|
|
1812
2223
|
if (![16, 20, 24, 28, 32].includes(ent.length))
|
|
1813
|
-
throw new
|
|
2224
|
+
throw new RangeError('invalid entropy length');
|
|
1814
2225
|
}
|
|
1815
2226
|
const calcChecksum = (entropy) => {
|
|
1816
2227
|
// Checksum is ent.length/4 bits long
|
|
@@ -1821,25 +2232,36 @@
|
|
|
1821
2232
|
};
|
|
1822
2233
|
function getCoder(wordlist) {
|
|
1823
2234
|
if (!Array.isArray(wordlist) || wordlist.length !== 2048 || typeof wordlist[0] !== 'string')
|
|
1824
|
-
throw new
|
|
2235
|
+
throw new TypeError('Wordlist: expected array of 2048 strings');
|
|
1825
2236
|
wordlist.forEach((i) => {
|
|
1826
2237
|
if (typeof i !== 'string')
|
|
1827
|
-
throw new
|
|
2238
|
+
throw new TypeError('wordlist: non-string element: ' + i);
|
|
1828
2239
|
});
|
|
2240
|
+
// BIP-39 appends checksum bits to entropy.
|
|
2241
|
+
// It then splits the bitstream into 11-bit indexes for a 2048-word list.
|
|
1829
2242
|
return utils.chain(utils.checksum(1, calcChecksum), utils.radix2(11, true), utils.alphabet(wordlist));
|
|
1830
2243
|
}
|
|
1831
2244
|
/**
|
|
1832
2245
|
* Reversible: Converts mnemonic string to raw entropy in form of byte array.
|
|
1833
|
-
* @param mnemonic 12-24 words
|
|
1834
|
-
* @param wordlist
|
|
2246
|
+
* @param mnemonic - 12-24 words.
|
|
2247
|
+
* @param wordlist - Imported wordlist for a specific language.
|
|
2248
|
+
* @returns Raw entropy bytes.
|
|
2249
|
+
* @throws If the mnemonic shape or checksum is invalid. {@link Error}
|
|
2250
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
2251
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
1835
2252
|
* @example
|
|
2253
|
+
* Decode a mnemonic back into its original entropy bytes.
|
|
2254
|
+
* ```ts
|
|
2255
|
+
* import { mnemonicToEntropy } from '@scure/bip39';
|
|
2256
|
+
* import { wordlist } from '@scure/bip39/wordlists/english.js';
|
|
1836
2257
|
* const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
|
|
1837
|
-
* mnemonicToEntropy(mnem, wordlist)
|
|
1838
|
-
* // Produces
|
|
2258
|
+
* const entropy = mnemonicToEntropy(mnem, wordlist);
|
|
2259
|
+
* // Produces the original 16-byte entropy payload.
|
|
1839
2260
|
* new Uint8Array([
|
|
1840
2261
|
* 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
|
|
1841
2262
|
* 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f
|
|
1842
2263
|
* ])
|
|
2264
|
+
* ```
|
|
1843
2265
|
*/
|
|
1844
2266
|
function mnemonicToEntropy(mnemonic, wordlist) {
|
|
1845
2267
|
const { words } = normalize(mnemonic);
|
|
@@ -1849,6 +2271,20 @@
|
|
|
1849
2271
|
}
|
|
1850
2272
|
/**
|
|
1851
2273
|
* Validates mnemonic for being 12-24 words contained in `wordlist`.
|
|
2274
|
+
* @param mnemonic - 12-24 words.
|
|
2275
|
+
* @param wordlist - Imported wordlist for a specific language.
|
|
2276
|
+
* @returns `true` when mnemonic checksum and words are valid.
|
|
2277
|
+
* @example
|
|
2278
|
+
* Validate one English mnemonic.
|
|
2279
|
+
* ```ts
|
|
2280
|
+
* import { validateMnemonic } from '@scure/bip39';
|
|
2281
|
+
* import { wordlist } from '@scure/bip39/wordlists/english.js';
|
|
2282
|
+
* const ok = validateMnemonic(
|
|
2283
|
+
* 'legal winner thank year wave sausage worth useful legal winner thank yellow',
|
|
2284
|
+
* wordlist
|
|
2285
|
+
* );
|
|
2286
|
+
* // => true
|
|
2287
|
+
* ```
|
|
1852
2288
|
*/
|
|
1853
2289
|
function validateMnemonic(mnemonic, wordlist) {
|
|
1854
2290
|
try {
|
|
@@ -1859,22 +2295,32 @@
|
|
|
1859
2295
|
}
|
|
1860
2296
|
return true;
|
|
1861
2297
|
}
|
|
2298
|
+
// BIP-39 salts PBKDF2 with the UTF-8 NFKD string "mnemonic" + passphrase.
|
|
1862
2299
|
const psalt = (passphrase) => nfkd('mnemonic' + passphrase);
|
|
1863
2300
|
/**
|
|
1864
2301
|
* Irreversible: Uses KDF to derive 64 bytes of key data from mnemonic + optional password.
|
|
1865
|
-
* @param mnemonic 12-24 words
|
|
1866
|
-
* @param passphrase
|
|
1867
|
-
* @returns 64 bytes of key data
|
|
2302
|
+
* @param mnemonic - 12-24 words.
|
|
2303
|
+
* @param passphrase - String that will additionally protect the key.
|
|
2304
|
+
* @returns 64 bytes of key data.
|
|
2305
|
+
* @throws If the mnemonic shape is invalid. {@link Error}
|
|
2306
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
1868
2307
|
* @example
|
|
2308
|
+
* Derive a seed from a mnemonic with the sync PBKDF2 helper.
|
|
2309
|
+
* ```ts
|
|
1869
2310
|
* const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
|
|
1870
|
-
* mnemonicToSeedSync(mnem, 'password');
|
|
1871
|
-
* // new Uint8Array([...64 bytes])
|
|
2311
|
+
* const seed = mnemonicToSeedSync(mnem, 'password');
|
|
2312
|
+
* // => new Uint8Array([...64 bytes])
|
|
2313
|
+
* ```
|
|
1872
2314
|
*/
|
|
1873
2315
|
function mnemonicToSeedSync(mnemonic, passphrase = '') {
|
|
1874
|
-
return pbkdf2(sha512, normalize(mnemonic).nfkd, psalt(passphrase), {
|
|
2316
|
+
return pbkdf2(sha512, normalize(mnemonic).nfkd, psalt(passphrase), {
|
|
2317
|
+
c: 2048,
|
|
2318
|
+
dkLen: 64,
|
|
2319
|
+
});
|
|
1875
2320
|
}
|
|
1876
2321
|
|
|
1877
|
-
|
|
2322
|
+
/** English BIP39 wordlist. */
|
|
2323
|
+
const wordlist = /* @__PURE__ */ Object.freeze(`abandon
|
|
1878
2324
|
ability
|
|
1879
2325
|
able
|
|
1880
2326
|
about
|
|
@@ -3921,7 +4367,7 @@ youth
|
|
|
3921
4367
|
zebra
|
|
3922
4368
|
zero
|
|
3923
4369
|
zone
|
|
3924
|
-
zoo`.split('\n');
|
|
4370
|
+
zoo`.split('\n'));
|
|
3925
4371
|
|
|
3926
4372
|
const Hard = 0x80000000;
|
|
3927
4373
|
|
|
@@ -4499,8 +4945,8 @@ zoo`.split('\n');
|
|
|
4499
4945
|
|
|
4500
4946
|
// IMPORTANT: THIS FILE IS AUTO GENERATED! DO NOT MANUALLY EDIT!
|
|
4501
4947
|
const VERSION = {
|
|
4502
|
-
"commitHash": "
|
|
4503
|
-
"version": "
|
|
4948
|
+
"commitHash": "9851c9b7e8387a82f8ff0aa6a34277a9108bb68c",
|
|
4949
|
+
"version": "25.0.0-beta.1"
|
|
4504
4950
|
};
|
|
4505
4951
|
|
|
4506
4952
|
exports.ECDSA = ecdsa;
|