@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
|
@@ -45,22 +45,62 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
|
|
|
45
45
|
};
|
|
46
46
|
|
|
47
47
|
/**
|
|
48
|
-
*
|
|
49
|
-
* @
|
|
48
|
+
* Checks if something is Uint8Array. Be careful: nodejs Buffer will return true.
|
|
49
|
+
* @param a - value to test
|
|
50
|
+
* @returns `true` when the value is a Uint8Array-compatible view.
|
|
51
|
+
* @example
|
|
52
|
+
* Check whether a value is a Uint8Array-compatible view.
|
|
53
|
+
* ```ts
|
|
54
|
+
* isBytes(new Uint8Array([1, 2, 3]));
|
|
55
|
+
* ```
|
|
50
56
|
*/
|
|
51
|
-
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
52
|
-
/** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */
|
|
53
57
|
function isBytes$1(a) {
|
|
54
|
-
|
|
58
|
+
// Plain `instanceof Uint8Array` is too strict for some Buffer / proxy / cross-realm cases.
|
|
59
|
+
// The fallback still requires a real ArrayBuffer view, so plain
|
|
60
|
+
// JSON-deserialized `{ constructor: ... }` spoofing is rejected, and
|
|
61
|
+
// `BYTES_PER_ELEMENT === 1` keeps the fallback on byte-oriented views.
|
|
62
|
+
return (a instanceof Uint8Array ||
|
|
63
|
+
(ArrayBuffer.isView(a) &&
|
|
64
|
+
a.constructor.name === 'Uint8Array' &&
|
|
65
|
+
'BYTES_PER_ELEMENT' in a &&
|
|
66
|
+
a.BYTES_PER_ELEMENT === 1));
|
|
55
67
|
}
|
|
56
|
-
/**
|
|
68
|
+
/**
|
|
69
|
+
* Asserts something is a non-negative integer.
|
|
70
|
+
* @param n - number to validate
|
|
71
|
+
* @param title - label included in thrown errors
|
|
72
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
73
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
74
|
+
* @example
|
|
75
|
+
* Validate a non-negative integer option.
|
|
76
|
+
* ```ts
|
|
77
|
+
* anumber(32, 'length');
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
57
80
|
function anumber$1(n, title = '') {
|
|
81
|
+
if (typeof n !== 'number') {
|
|
82
|
+
const prefix = title && `"${title}" `;
|
|
83
|
+
throw new TypeError(`${prefix}expected number, got ${typeof n}`);
|
|
84
|
+
}
|
|
58
85
|
if (!Number.isSafeInteger(n) || n < 0) {
|
|
59
86
|
const prefix = title && `"${title}" `;
|
|
60
|
-
throw new
|
|
87
|
+
throw new RangeError(`${prefix}expected integer >= 0, got ${n}`);
|
|
61
88
|
}
|
|
62
89
|
}
|
|
63
|
-
/**
|
|
90
|
+
/**
|
|
91
|
+
* Asserts something is Uint8Array.
|
|
92
|
+
* @param value - value to validate
|
|
93
|
+
* @param length - optional exact length constraint
|
|
94
|
+
* @param title - label included in thrown errors
|
|
95
|
+
* @returns The validated byte array.
|
|
96
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
97
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
98
|
+
* @example
|
|
99
|
+
* Validate that a value is a byte array.
|
|
100
|
+
* ```ts
|
|
101
|
+
* abytes(new Uint8Array([1, 2, 3]));
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
64
104
|
function abytes(value, length, title = '') {
|
|
65
105
|
const bytes = isBytes$1(value);
|
|
66
106
|
const len = value?.length;
|
|
@@ -69,111 +109,297 @@ function abytes(value, length, title = '') {
|
|
|
69
109
|
const prefix = title && `"${title}" `;
|
|
70
110
|
const ofLen = needsLen ? ` of length ${length}` : '';
|
|
71
111
|
const got = bytes ? `length=${len}` : `type=${typeof value}`;
|
|
72
|
-
|
|
112
|
+
const message = prefix + 'expected Uint8Array' + ofLen + ', got ' + got;
|
|
113
|
+
if (!bytes)
|
|
114
|
+
throw new TypeError(message);
|
|
115
|
+
throw new RangeError(message);
|
|
73
116
|
}
|
|
74
117
|
return value;
|
|
75
118
|
}
|
|
76
|
-
/**
|
|
119
|
+
/**
|
|
120
|
+
* Asserts something is a wrapped hash constructor.
|
|
121
|
+
* @param h - hash constructor to validate
|
|
122
|
+
* @throws On wrong argument types or invalid hash wrapper shape. {@link TypeError}
|
|
123
|
+
* @throws On invalid hash metadata ranges or values. {@link RangeError}
|
|
124
|
+
* @throws If the hash metadata allows empty outputs or block sizes. {@link Error}
|
|
125
|
+
* @example
|
|
126
|
+
* Validate a callable hash wrapper.
|
|
127
|
+
* ```ts
|
|
128
|
+
* import { ahash } from '@noble/hashes/utils.js';
|
|
129
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
130
|
+
* ahash(sha256);
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
77
133
|
function ahash(h) {
|
|
78
134
|
if (typeof h !== 'function' || typeof h.create !== 'function')
|
|
79
|
-
throw new
|
|
135
|
+
throw new TypeError('Hash must wrapped by utils.createHasher');
|
|
80
136
|
anumber$1(h.outputLen);
|
|
81
137
|
anumber$1(h.blockLen);
|
|
138
|
+
// HMAC and KDF callers treat these as real byte lengths; allowing zero lets fake wrappers pass
|
|
139
|
+
// validation and can produce empty outputs instead of failing fast.
|
|
140
|
+
if (h.outputLen < 1)
|
|
141
|
+
throw new Error('"outputLen" must be >= 1');
|
|
142
|
+
if (h.blockLen < 1)
|
|
143
|
+
throw new Error('"blockLen" must be >= 1');
|
|
82
144
|
}
|
|
83
|
-
/**
|
|
145
|
+
/**
|
|
146
|
+
* Asserts a hash instance has not been destroyed or finished.
|
|
147
|
+
* @param instance - hash instance to validate
|
|
148
|
+
* @param checkFinished - whether to reject finalized instances
|
|
149
|
+
* @throws If the hash instance has already been destroyed or finalized. {@link Error}
|
|
150
|
+
* @example
|
|
151
|
+
* Validate that a hash instance is still usable.
|
|
152
|
+
* ```ts
|
|
153
|
+
* import { aexists } from '@noble/hashes/utils.js';
|
|
154
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
155
|
+
* const hash = sha256.create();
|
|
156
|
+
* aexists(hash);
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
84
159
|
function aexists(instance, checkFinished = true) {
|
|
85
160
|
if (instance.destroyed)
|
|
86
161
|
throw new Error('Hash instance has been destroyed');
|
|
87
162
|
if (checkFinished && instance.finished)
|
|
88
163
|
throw new Error('Hash#digest() has already been called');
|
|
89
164
|
}
|
|
90
|
-
/**
|
|
165
|
+
/**
|
|
166
|
+
* Asserts output is a sufficiently-sized byte array.
|
|
167
|
+
* @param out - destination buffer
|
|
168
|
+
* @param instance - hash instance providing output length
|
|
169
|
+
* Oversized buffers are allowed; downstream code only promises to fill the first `outputLen` bytes.
|
|
170
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
171
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
172
|
+
* @example
|
|
173
|
+
* Validate a caller-provided digest buffer.
|
|
174
|
+
* ```ts
|
|
175
|
+
* import { aoutput } from '@noble/hashes/utils.js';
|
|
176
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
177
|
+
* const hash = sha256.create();
|
|
178
|
+
* aoutput(new Uint8Array(hash.outputLen), hash);
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
91
181
|
function aoutput(out, instance) {
|
|
92
182
|
abytes(out, undefined, 'digestInto() output');
|
|
93
183
|
const min = instance.outputLen;
|
|
94
184
|
if (out.length < min) {
|
|
95
|
-
throw new
|
|
185
|
+
throw new RangeError('"digestInto() output" expected to be of length >=' + min);
|
|
96
186
|
}
|
|
97
187
|
}
|
|
98
|
-
/**
|
|
188
|
+
/**
|
|
189
|
+
* Casts a typed array view to Uint32Array.
|
|
190
|
+
* `arr.byteOffset` must already be 4-byte aligned or the platform
|
|
191
|
+
* Uint32Array constructor will throw.
|
|
192
|
+
* @param arr - source typed array
|
|
193
|
+
* @returns Uint32Array view over the same buffer.
|
|
194
|
+
* @example
|
|
195
|
+
* Reinterpret a byte array as 32-bit words.
|
|
196
|
+
* ```ts
|
|
197
|
+
* u32(new Uint8Array(8));
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
99
200
|
function u32(arr) {
|
|
100
201
|
return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
|
|
101
202
|
}
|
|
102
|
-
/**
|
|
203
|
+
/**
|
|
204
|
+
* Zeroizes typed arrays in place. Warning: JS provides no guarantees.
|
|
205
|
+
* @param arrays - arrays to overwrite with zeros
|
|
206
|
+
* @example
|
|
207
|
+
* Zeroize sensitive buffers in place.
|
|
208
|
+
* ```ts
|
|
209
|
+
* clean(new Uint8Array([1, 2, 3]));
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
103
212
|
function clean(...arrays) {
|
|
104
213
|
for (let i = 0; i < arrays.length; i++) {
|
|
105
214
|
arrays[i].fill(0);
|
|
106
215
|
}
|
|
107
216
|
}
|
|
108
|
-
/**
|
|
217
|
+
/**
|
|
218
|
+
* Creates a DataView for byte-level manipulation.
|
|
219
|
+
* @param arr - source typed array
|
|
220
|
+
* @returns DataView over the same buffer region.
|
|
221
|
+
* @example
|
|
222
|
+
* Create a DataView over an existing buffer.
|
|
223
|
+
* ```ts
|
|
224
|
+
* createView(new Uint8Array(4));
|
|
225
|
+
* ```
|
|
226
|
+
*/
|
|
109
227
|
function createView(arr) {
|
|
110
228
|
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
111
229
|
}
|
|
112
|
-
/**
|
|
230
|
+
/**
|
|
231
|
+
* Rotate-right operation for uint32 values.
|
|
232
|
+
* @param word - source word
|
|
233
|
+
* @param shift - shift amount in bits
|
|
234
|
+
* @returns Rotated word.
|
|
235
|
+
* @example
|
|
236
|
+
* Rotate a 32-bit word to the right.
|
|
237
|
+
* ```ts
|
|
238
|
+
* rotr(0x12345678, 8);
|
|
239
|
+
* ```
|
|
240
|
+
*/
|
|
113
241
|
function rotr(word, shift) {
|
|
114
242
|
return (word << (32 - shift)) | (word >>> shift);
|
|
115
243
|
}
|
|
116
|
-
/**
|
|
244
|
+
/** Whether the current platform is little-endian. */
|
|
117
245
|
const isLE = /* @__PURE__ */ (() => new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44)();
|
|
118
|
-
/**
|
|
246
|
+
/**
|
|
247
|
+
* Byte-swap operation for uint32 values.
|
|
248
|
+
* @param word - source word
|
|
249
|
+
* @returns Word with reversed byte order.
|
|
250
|
+
* @example
|
|
251
|
+
* Reverse the byte order of a 32-bit word.
|
|
252
|
+
* ```ts
|
|
253
|
+
* byteSwap(0x11223344);
|
|
254
|
+
* ```
|
|
255
|
+
*/
|
|
119
256
|
function byteSwap(word) {
|
|
120
257
|
return (((word << 24) & 0xff000000) |
|
|
121
258
|
((word << 8) & 0xff0000) |
|
|
122
259
|
((word >>> 8) & 0xff00) |
|
|
123
260
|
((word >>> 24) & 0xff));
|
|
124
261
|
}
|
|
125
|
-
/**
|
|
262
|
+
/**
|
|
263
|
+
* Conditionally byte-swaps one 32-bit word on big-endian platforms.
|
|
264
|
+
* @param n - source word
|
|
265
|
+
* @returns Original or byte-swapped word depending on platform endianness.
|
|
266
|
+
* @example
|
|
267
|
+
* Normalize a 32-bit word for host endianness.
|
|
268
|
+
* ```ts
|
|
269
|
+
* swap8IfBE(0x11223344);
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
126
272
|
const swap8IfBE = isLE
|
|
127
273
|
? (n) => n
|
|
128
|
-
: (n) => byteSwap(n);
|
|
129
|
-
/**
|
|
274
|
+
: (n) => byteSwap(n) >>> 0;
|
|
275
|
+
/**
|
|
276
|
+
* Byte-swaps every word of a Uint32Array in place.
|
|
277
|
+
* @param arr - array to mutate
|
|
278
|
+
* @returns The same array after mutation; callers pass live state arrays here.
|
|
279
|
+
* @example
|
|
280
|
+
* Reverse the byte order of every word in place.
|
|
281
|
+
* ```ts
|
|
282
|
+
* byteSwap32(new Uint32Array([0x11223344]));
|
|
283
|
+
* ```
|
|
284
|
+
*/
|
|
130
285
|
function byteSwap32(arr) {
|
|
131
286
|
for (let i = 0; i < arr.length; i++) {
|
|
132
287
|
arr[i] = byteSwap(arr[i]);
|
|
133
288
|
}
|
|
134
289
|
return arr;
|
|
135
290
|
}
|
|
291
|
+
/**
|
|
292
|
+
* Conditionally byte-swaps a Uint32Array on big-endian platforms.
|
|
293
|
+
* @param u - array to normalize for host endianness
|
|
294
|
+
* @returns Original or byte-swapped array depending on platform endianness.
|
|
295
|
+
* On big-endian runtimes this mutates `u` in place via `byteSwap32(...)`.
|
|
296
|
+
* @example
|
|
297
|
+
* Normalize a word array for host endianness.
|
|
298
|
+
* ```ts
|
|
299
|
+
* swap32IfBE(new Uint32Array([0x11223344]));
|
|
300
|
+
* ```
|
|
301
|
+
*/
|
|
136
302
|
const swap32IfBE = isLE
|
|
137
303
|
? (u) => u
|
|
138
304
|
: byteSwap32;
|
|
139
305
|
/**
|
|
140
306
|
* Converts string to bytes using UTF8 encoding.
|
|
141
307
|
* Built-in doesn't validate input to be string: we do the check.
|
|
142
|
-
*
|
|
308
|
+
* Non-ASCII details are delegated to the platform `TextEncoder`.
|
|
309
|
+
* @param str - string to encode
|
|
310
|
+
* @returns UTF-8 encoded bytes.
|
|
311
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
312
|
+
* @example
|
|
313
|
+
* Encode a string as UTF-8 bytes.
|
|
314
|
+
* ```ts
|
|
315
|
+
* utf8ToBytes('abc'); // Uint8Array.from([97, 98, 99])
|
|
316
|
+
* ```
|
|
143
317
|
*/
|
|
144
318
|
function utf8ToBytes(str) {
|
|
145
319
|
if (typeof str !== 'string')
|
|
146
|
-
throw new
|
|
320
|
+
throw new TypeError('string expected');
|
|
147
321
|
return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
|
|
148
322
|
}
|
|
149
323
|
/**
|
|
150
|
-
* Helper for KDFs: consumes
|
|
151
|
-
*
|
|
324
|
+
* Helper for KDFs: consumes Uint8Array or string.
|
|
325
|
+
* String inputs are UTF-8 encoded; byte-array inputs stay aliased to the caller buffer.
|
|
326
|
+
* @param data - user-provided KDF input
|
|
327
|
+
* @param errorTitle - label included in thrown errors
|
|
328
|
+
* @returns Byte representation of the input.
|
|
329
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
330
|
+
* @example
|
|
331
|
+
* Normalize KDF input to bytes.
|
|
332
|
+
* ```ts
|
|
333
|
+
* kdfInputToBytes('password');
|
|
334
|
+
* ```
|
|
152
335
|
*/
|
|
153
336
|
function kdfInputToBytes(data, errorTitle = '') {
|
|
154
337
|
if (typeof data === 'string')
|
|
155
338
|
return utf8ToBytes(data);
|
|
156
339
|
return abytes(data, undefined, errorTitle);
|
|
157
340
|
}
|
|
158
|
-
/**
|
|
341
|
+
/**
|
|
342
|
+
* Merges default options and passed options.
|
|
343
|
+
* @param defaults - base option object
|
|
344
|
+
* @param opts - user overrides
|
|
345
|
+
* @returns Merged option object. The merge mutates `defaults` in place.
|
|
346
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
347
|
+
* @example
|
|
348
|
+
* Merge user overrides onto default options.
|
|
349
|
+
* ```ts
|
|
350
|
+
* checkOpts({ dkLen: 32 }, { asyncTick: 10 });
|
|
351
|
+
* ```
|
|
352
|
+
*/
|
|
159
353
|
function checkOpts(defaults, opts) {
|
|
160
354
|
if (opts !== undefined && {}.toString.call(opts) !== '[object Object]')
|
|
161
|
-
throw new
|
|
355
|
+
throw new TypeError('options must be object or undefined');
|
|
162
356
|
const merged = Object.assign(defaults, opts);
|
|
163
357
|
return merged;
|
|
164
358
|
}
|
|
165
|
-
/**
|
|
359
|
+
/**
|
|
360
|
+
* Creates a callable hash function from a stateful class constructor.
|
|
361
|
+
* @param hashCons - hash constructor or factory
|
|
362
|
+
* @param info - optional metadata such as DER OID
|
|
363
|
+
* @returns Frozen callable hash wrapper with `.create()`.
|
|
364
|
+
* Wrapper construction eagerly calls `hashCons(undefined)` once to read
|
|
365
|
+
* `outputLen` / `blockLen`, so constructor side effects happen at module
|
|
366
|
+
* init time.
|
|
367
|
+
* @example
|
|
368
|
+
* Wrap a stateful hash constructor into a callable helper.
|
|
369
|
+
* ```ts
|
|
370
|
+
* import { createHasher } from '@noble/hashes/utils.js';
|
|
371
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
372
|
+
* const wrapped = createHasher(sha256.create, { oid: sha256.oid });
|
|
373
|
+
* wrapped(new Uint8Array([1]));
|
|
374
|
+
* ```
|
|
375
|
+
*/
|
|
166
376
|
function createHasher(hashCons, info = {}) {
|
|
167
|
-
const hashC = (msg, opts) => hashCons(opts)
|
|
377
|
+
const hashC = (msg, opts) => hashCons(opts)
|
|
378
|
+
.update(msg)
|
|
379
|
+
.digest();
|
|
168
380
|
const tmp = hashCons(undefined);
|
|
169
381
|
hashC.outputLen = tmp.outputLen;
|
|
170
382
|
hashC.blockLen = tmp.blockLen;
|
|
383
|
+
hashC.canXOF = tmp.canXOF;
|
|
171
384
|
hashC.create = (opts) => hashCons(opts);
|
|
172
385
|
Object.assign(hashC, info);
|
|
173
386
|
return Object.freeze(hashC);
|
|
174
387
|
}
|
|
175
|
-
/**
|
|
388
|
+
/**
|
|
389
|
+
* Creates OID metadata for NIST hashes with prefix `06 09 60 86 48 01 65 03 04 02`.
|
|
390
|
+
* @param suffix - final OID byte for the selected hash.
|
|
391
|
+
* The helper accepts any byte even though only the documented NIST hash
|
|
392
|
+
* suffixes are meaningful downstream.
|
|
393
|
+
* @returns Object containing the DER-encoded OID.
|
|
394
|
+
* @example
|
|
395
|
+
* Build OID metadata for a NIST hash.
|
|
396
|
+
* ```ts
|
|
397
|
+
* oidNist(0x01);
|
|
398
|
+
* ```
|
|
399
|
+
*/
|
|
176
400
|
const oidNist = (suffix) => ({
|
|
401
|
+
// Current NIST hashAlgs suffixes used here fit in one DER subidentifier octet.
|
|
402
|
+
// Larger suffix values would need base-128 OID encoding and a different length byte.
|
|
177
403
|
oid: Uint8Array.from([0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, suffix]),
|
|
178
404
|
});
|
|
179
405
|
|
|
@@ -182,8 +408,10 @@ const oidNist = (suffix) => ({
|
|
|
182
408
|
* @module
|
|
183
409
|
*/
|
|
184
410
|
/**
|
|
185
|
-
* Internal blake
|
|
186
|
-
*
|
|
411
|
+
* Internal blake permutation table.
|
|
412
|
+
* Rows `0..9` serve BLAKE2s, rows `0..11` serve BLAKE2b with `10..11 = 0..1`, and Blake1 also
|
|
413
|
+
* reuses the later rows shown below. Blake1 expands rounds `10..15` as `SIGMA[i % 10]`, so rows
|
|
414
|
+
* `10..15` intentionally repeat rows `0..5` for the 14-round (256) and 16-round (512) variants.
|
|
187
415
|
*/
|
|
188
416
|
// prettier-ignore
|
|
189
417
|
const BSIGMA = /* @__PURE__ */ Uint8Array.from([
|
|
@@ -210,21 +438,62 @@ const BSIGMA = /* @__PURE__ */ Uint8Array.from([
|
|
|
210
438
|
* Internal Merkle-Damgard hash utils.
|
|
211
439
|
* @module
|
|
212
440
|
*/
|
|
213
|
-
/**
|
|
441
|
+
/**
|
|
442
|
+
* Shared 32-bit conditional boolean primitive reused by SHA-256, SHA-1, and MD5 `F`.
|
|
443
|
+
* Returns bits from `b` when `a` is set, otherwise from `c`.
|
|
444
|
+
* The XOR form is equivalent to MD5's `F(X,Y,Z) = XY v not(X)Z` because the masked terms never
|
|
445
|
+
* set the same bit.
|
|
446
|
+
* @param a - selector word
|
|
447
|
+
* @param b - word chosen when selector bit is set
|
|
448
|
+
* @param c - word chosen when selector bit is clear
|
|
449
|
+
* @returns Mixed 32-bit word.
|
|
450
|
+
* @example
|
|
451
|
+
* Combine three words with the shared 32-bit choice primitive.
|
|
452
|
+
* ```ts
|
|
453
|
+
* Chi(0xffffffff, 0x12345678, 0x87654321);
|
|
454
|
+
* ```
|
|
455
|
+
*/
|
|
214
456
|
function Chi(a, b, c) {
|
|
215
457
|
return (a & b) ^ (~a & c);
|
|
216
458
|
}
|
|
217
|
-
/**
|
|
459
|
+
/**
|
|
460
|
+
* Shared 32-bit majority primitive reused by SHA-256 and SHA-1.
|
|
461
|
+
* Returns bits shared by at least two inputs.
|
|
462
|
+
* @param a - first input word
|
|
463
|
+
* @param b - second input word
|
|
464
|
+
* @param c - third input word
|
|
465
|
+
* @returns Mixed 32-bit word.
|
|
466
|
+
* @example
|
|
467
|
+
* Combine three words with the shared 32-bit majority primitive.
|
|
468
|
+
* ```ts
|
|
469
|
+
* Maj(0xffffffff, 0x12345678, 0x87654321);
|
|
470
|
+
* ```
|
|
471
|
+
*/
|
|
218
472
|
function Maj(a, b, c) {
|
|
219
473
|
return (a & b) ^ (a & c) ^ (b & c);
|
|
220
474
|
}
|
|
221
475
|
/**
|
|
222
476
|
* Merkle-Damgard hash construction base class.
|
|
223
477
|
* Could be used to create MD5, RIPEMD, SHA1, SHA2.
|
|
478
|
+
* Accepts only byte-aligned `Uint8Array` input, even when the underlying spec describes bit
|
|
479
|
+
* strings with partial-byte tails.
|
|
480
|
+
* @param blockLen - internal block size in bytes
|
|
481
|
+
* @param outputLen - digest size in bytes
|
|
482
|
+
* @param padOffset - trailing length field size in bytes
|
|
483
|
+
* @param isLE - whether length and state words are encoded in little-endian
|
|
484
|
+
* @example
|
|
485
|
+
* Use a concrete subclass to get the shared Merkle-Damgard update/digest flow.
|
|
486
|
+
* ```ts
|
|
487
|
+
* import { _SHA1 } from '@noble/hashes/legacy.js';
|
|
488
|
+
* const hash = new _SHA1();
|
|
489
|
+
* hash.update(new Uint8Array([97, 98, 99]));
|
|
490
|
+
* hash.digest();
|
|
491
|
+
* ```
|
|
224
492
|
*/
|
|
225
493
|
class HashMD {
|
|
226
494
|
blockLen;
|
|
227
495
|
outputLen;
|
|
496
|
+
canXOF = false;
|
|
228
497
|
padOffset;
|
|
229
498
|
isLE;
|
|
230
499
|
// For partial updates less than block size
|
|
@@ -249,7 +518,8 @@ class HashMD {
|
|
|
249
518
|
const len = data.length;
|
|
250
519
|
for (let pos = 0; pos < len;) {
|
|
251
520
|
const take = Math.min(blockLen - this.pos, len - pos);
|
|
252
|
-
// Fast path
|
|
521
|
+
// Fast path only when there is no buffered partial block: `take === blockLen` implies
|
|
522
|
+
// `this.pos === 0`, so we can process full blocks directly from the input view.
|
|
253
523
|
if (take === blockLen) {
|
|
254
524
|
const dataView = createView(data);
|
|
255
525
|
for (; blockLen <= len - pos; pos += blockLen)
|
|
@@ -289,9 +559,9 @@ class HashMD {
|
|
|
289
559
|
// Pad until full block byte with zeros
|
|
290
560
|
for (let i = pos; i < blockLen; i++)
|
|
291
561
|
buffer[i] = 0;
|
|
292
|
-
//
|
|
293
|
-
//
|
|
294
|
-
// So we
|
|
562
|
+
// `padOffset` reserves the whole length field. For SHA-384/512 the high 64 bits stay zero from
|
|
563
|
+
// the padding fill above, and JS will overflow before user input can make that half non-zero.
|
|
564
|
+
// So we only need to write the low 64 bits here.
|
|
295
565
|
view.setBigUint64(blockLen - 8, BigInt(this.length * 8), isLE);
|
|
296
566
|
this.process(view, 0);
|
|
297
567
|
const oview = createView(out);
|
|
@@ -309,6 +579,8 @@ class HashMD {
|
|
|
309
579
|
digest() {
|
|
310
580
|
const { buffer, outputLen } = this;
|
|
311
581
|
this.digestInto(buffer);
|
|
582
|
+
// Copy before destroy(): subclasses wipe `buffer` during cleanup, but `digest()` must return
|
|
583
|
+
// fresh bytes to the caller.
|
|
312
584
|
const res = buffer.slice(0, outputLen);
|
|
313
585
|
this.destroy();
|
|
314
586
|
return res;
|
|
@@ -321,6 +593,8 @@ class HashMD {
|
|
|
321
593
|
to.finished = finished;
|
|
322
594
|
to.length = length;
|
|
323
595
|
to.pos = pos;
|
|
596
|
+
// Only partial-block bytes need copying: when `length % blockLen === 0`, `pos === 0` and
|
|
597
|
+
// later `update()` / `digestInto()` overwrite `to.buffer` from the start before reading it.
|
|
324
598
|
if (length % blockLen)
|
|
325
599
|
to.buffer.set(buffer);
|
|
326
600
|
return to;
|
|
@@ -333,28 +607,32 @@ class HashMD {
|
|
|
333
607
|
* Initial SHA-2 state: fractional parts of square roots of first 16 primes 2..53.
|
|
334
608
|
* Check out `test/misc/sha2-gen-iv.js` for recomputation guide.
|
|
335
609
|
*/
|
|
336
|
-
/** Initial SHA256 state.
|
|
610
|
+
/** Initial SHA256 state from RFC 6234 §6.1: the first 32 bits of the fractional parts of the
|
|
611
|
+
* square roots of the first eight prime numbers. Exported as a shared table; callers must treat
|
|
612
|
+
* it as read-only because constructors copy words from it by index. */
|
|
337
613
|
const SHA256_IV = /* @__PURE__ */ Uint32Array.from([
|
|
338
614
|
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
|
|
339
615
|
]);
|
|
340
|
-
/** Initial SHA512 state
|
|
616
|
+
/** Initial SHA512 state from RFC 6234 §6.3: eight RFC 64-bit `H(0)` words stored as sixteen
|
|
617
|
+
* big-endian 32-bit halves. Derived from the fractional parts of the square roots of the first
|
|
618
|
+
* eight prime numbers. Exported as a shared table; callers must treat it as read-only because
|
|
619
|
+
* constructors copy halves from it by index. */
|
|
341
620
|
const SHA512_IV = /* @__PURE__ */ Uint32Array.from([
|
|
342
621
|
0x6a09e667, 0xf3bcc908, 0xbb67ae85, 0x84caa73b, 0x3c6ef372, 0xfe94f82b, 0xa54ff53a, 0x5f1d36f1,
|
|
343
622
|
0x510e527f, 0xade682d1, 0x9b05688c, 0x2b3e6c1f, 0x1f83d9ab, 0xfb41bd6b, 0x5be0cd19, 0x137e2179,
|
|
344
623
|
]);
|
|
345
624
|
|
|
346
|
-
/**
|
|
347
|
-
* Internal helpers for u64. BigUint64Array is too slow as per 2025, so we implement it using Uint32Array.
|
|
348
|
-
* @todo re-check https://issues.chromium.org/issues/42212588
|
|
349
|
-
* @module
|
|
350
|
-
*/
|
|
351
625
|
const U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
|
|
352
626
|
const _32n = /* @__PURE__ */ BigInt(32);
|
|
627
|
+
// Split bigint into two 32-bit halves. With `le=true`, returned fields become `{ h: low, l: high
|
|
628
|
+
// }` to match little-endian word order rather than the property names.
|
|
353
629
|
function fromBig(n, le = false) {
|
|
354
630
|
if (le)
|
|
355
631
|
return { h: Number(n & U32_MASK64), l: Number((n >> _32n) & U32_MASK64) };
|
|
356
632
|
return { h: Number((n >> _32n) & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
|
|
357
633
|
}
|
|
634
|
+
// Split bigint list into `[highWords, lowWords]` when `le=false`; with `le=true`, the first array
|
|
635
|
+
// holds the low halves because `fromBig(...)` swaps the semantic meaning of `h` and `l`.
|
|
358
636
|
function split(lst, le = false) {
|
|
359
637
|
const len = lst.length;
|
|
360
638
|
let Ah = new Uint32Array(len);
|
|
@@ -365,30 +643,41 @@ function split(lst, le = false) {
|
|
|
365
643
|
}
|
|
366
644
|
return [Ah, Al];
|
|
367
645
|
}
|
|
368
|
-
// for
|
|
646
|
+
// High 32-bit half of a 64-bit logical right shift for `s` in `0..31`.
|
|
369
647
|
const shrSH = (h, _l, s) => h >>> s;
|
|
648
|
+
// Low 32-bit half of a 64-bit logical right shift, valid for `s` in `1..31`.
|
|
370
649
|
const shrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
|
|
371
|
-
//
|
|
650
|
+
// High 32-bit half of a 64-bit right rotate, valid for `s` in `1..31`.
|
|
372
651
|
const rotrSH = (h, l, s) => (h >>> s) | (l << (32 - s));
|
|
652
|
+
// Low 32-bit half of a 64-bit right rotate, valid for `s` in `1..31`.
|
|
373
653
|
const rotrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
|
|
374
|
-
//
|
|
654
|
+
// High 32-bit half of a 64-bit right rotate, valid for `s` in `33..63`; `32` uses `rotr32*`.
|
|
375
655
|
const rotrBH = (h, l, s) => (h << (64 - s)) | (l >>> (s - 32));
|
|
656
|
+
// Low 32-bit half of a 64-bit right rotate, valid for `s` in `33..63`; `32` uses `rotr32*`.
|
|
376
657
|
const rotrBL = (h, l, s) => (h >>> (s - 32)) | (l << (64 - s));
|
|
377
|
-
//
|
|
658
|
+
// High 32-bit half of a 64-bit right rotate for `s === 32`; this is just the swapped low half.
|
|
378
659
|
const rotr32H = (_h, l) => l;
|
|
660
|
+
// Low 32-bit half of a 64-bit right rotate for `s === 32`; this is just the swapped high half.
|
|
379
661
|
const rotr32L = (h, _l) => h;
|
|
380
|
-
//
|
|
381
|
-
//
|
|
662
|
+
// Add two split 64-bit words and return the split `{ h, l }` sum.
|
|
663
|
+
// JS uses 32-bit signed integers for bitwise operations, so we cannot simply shift the carry out
|
|
664
|
+
// of the low sum and instead use division.
|
|
382
665
|
function add(Ah, Al, Bh, Bl) {
|
|
383
666
|
const l = (Al >>> 0) + (Bl >>> 0);
|
|
384
667
|
return { h: (Ah + Bh + ((l / 2 ** 32) | 0)) | 0, l: l | 0 };
|
|
385
668
|
}
|
|
386
669
|
// Addition with more than 2 elements
|
|
670
|
+
// Unmasked low-word accumulator for 3-way addition; pass the raw result into `add3H(...)`.
|
|
387
671
|
const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
672
|
+
// High-word finalize step for 3-way addition; `low` must be the untruncated output of `add3L(...)`.
|
|
388
673
|
const add3H = (low, Ah, Bh, Ch) => (Ah + Bh + Ch + ((low / 2 ** 32) | 0)) | 0;
|
|
674
|
+
// Unmasked low-word accumulator for 4-way addition; pass the raw result into `add4H(...)`.
|
|
389
675
|
const add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
676
|
+
// High-word finalize step for 4-way addition; `low` must be the untruncated output of `add4L(...)`.
|
|
390
677
|
const add4H = (low, Ah, Bh, Ch, Dh) => (Ah + Bh + Ch + Dh + ((low / 2 ** 32) | 0)) | 0;
|
|
678
|
+
// Unmasked low-word accumulator for 5-way addition; pass the raw result into `add5H(...)`.
|
|
391
679
|
const add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
680
|
+
// High-word finalize step for 5-way addition; `low` must be the untruncated output of `add5L(...)`.
|
|
392
681
|
const add5H = (low, Ah, Bh, Ch, Dh, Eh) => (Ah + Bh + Ch + Dh + Eh + ((low / 2 ** 32) | 0)) | 0;
|
|
393
682
|
|
|
394
683
|
/**
|
|
@@ -396,14 +685,15 @@ const add5H = (low, Ah, Bh, Ch, Dh, Eh) => (Ah + Bh + Ch + Dh + Eh + ((low / 2 *
|
|
|
396
685
|
* b could have been faster, but there is no fast u64 in js, so s is 1.5x faster.
|
|
397
686
|
* @module
|
|
398
687
|
*/
|
|
399
|
-
// Same as SHA512_IV
|
|
688
|
+
// Same IV words as `SHA512_IV`, but endian-swapped into LE u32 low/high halves
|
|
689
|
+
// for the BLAKE2b u64 helpers below.
|
|
400
690
|
const B2B_IV = /* @__PURE__ */ Uint32Array.from([
|
|
401
691
|
0xf3bcc908, 0x6a09e667, 0x84caa73b, 0xbb67ae85, 0xfe94f82b, 0x3c6ef372, 0x5f1d36f1, 0xa54ff53a,
|
|
402
692
|
0xade682d1, 0x510e527f, 0x2b3e6c1f, 0x9b05688c, 0xfb41bd6b, 0x1f83d9ab, 0x137e2179, 0x5be0cd19,
|
|
403
693
|
]);
|
|
404
|
-
//
|
|
694
|
+
// Shared synchronous BLAKE2b work vector as LE u32 low/high halves.
|
|
405
695
|
const BBUF = /* @__PURE__ */ new Uint32Array(32);
|
|
406
|
-
//
|
|
696
|
+
// BLAKE2b G mix split into two half-rounds over LE u32 low/high limbs.
|
|
407
697
|
function G1b(a, b, c, d, msg, x) {
|
|
408
698
|
// NOTE: V is LE here
|
|
409
699
|
const Xl = msg[x], Xh = msg[x + 1]; // prettier-ignore
|
|
@@ -428,6 +718,7 @@ function G1b(a, b, c, d, msg, x) {
|
|
|
428
718
|
((BBUF[2 * c] = Cl), (BBUF[2 * c + 1] = Ch));
|
|
429
719
|
((BBUF[2 * d] = Dl), (BBUF[2 * d + 1] = Dh));
|
|
430
720
|
}
|
|
721
|
+
// Second half-round of the same LE-limb BLAKE2b G mix; `x` is the message word offset.
|
|
431
722
|
function G2b(a, b, c, d, msg, x) {
|
|
432
723
|
// NOTE: V is LE here
|
|
433
724
|
const Xl = msg[x], Xh = msg[x + 1]; // prettier-ignore
|
|
@@ -454,9 +745,11 @@ function G2b(a, b, c, d, msg, x) {
|
|
|
454
745
|
}
|
|
455
746
|
function checkBlake2Opts(outputLen, opts = {}, keyLen, saltLen, persLen) {
|
|
456
747
|
anumber$1(keyLen);
|
|
457
|
-
|
|
748
|
+
// RFC 7693 §2.1 requires digest length nn in 1..keyLen.
|
|
749
|
+
if (outputLen <= 0 || outputLen > keyLen)
|
|
458
750
|
throw new Error('outputLen bigger than keyLen');
|
|
459
751
|
const { key, salt, personalization } = opts;
|
|
752
|
+
// This API uses `undefined` for the RFC 7693 `kk = 0` case, so a provided key must be non-empty.
|
|
460
753
|
if (key !== undefined && (key.length < 1 || key.length > keyLen))
|
|
461
754
|
throw new Error('"key" expected to be undefined or of length=1..' + keyLen);
|
|
462
755
|
if (salt !== undefined)
|
|
@@ -474,6 +767,7 @@ class _BLAKE2 {
|
|
|
474
767
|
pos = 0;
|
|
475
768
|
blockLen;
|
|
476
769
|
outputLen;
|
|
770
|
+
canXOF = false;
|
|
477
771
|
constructor(blockLen, outputLen) {
|
|
478
772
|
anumber$1(blockLen);
|
|
479
773
|
anumber$1(outputLen);
|
|
@@ -503,7 +797,7 @@ class _BLAKE2 {
|
|
|
503
797
|
}
|
|
504
798
|
const take = Math.min(blockLen - this.pos, len - pos);
|
|
505
799
|
const dataOffset = offset + pos;
|
|
506
|
-
//
|
|
800
|
+
// Zero-copy only for full, 4-byte-aligned, non-final blocks.
|
|
507
801
|
if (take === blockLen && !(dataOffset % 4) && pos + take < len) {
|
|
508
802
|
const data32 = new Uint32Array(buf, dataOffset, Math.floor((len - pos) / 4));
|
|
509
803
|
swap32IfBE(data32);
|
|
@@ -531,18 +825,33 @@ class _BLAKE2 {
|
|
|
531
825
|
swap32IfBE(buffer32);
|
|
532
826
|
this.compress(buffer32, 0, true);
|
|
533
827
|
swap32IfBE(buffer32);
|
|
828
|
+
// Reject unaligned views explicitly instead of hiding them behind a full scratch copy.
|
|
829
|
+
if (out.byteOffset & 3)
|
|
830
|
+
throw new RangeError('"digestInto() output" expected 4-byte aligned byteOffset, got ' + out.byteOffset);
|
|
831
|
+
const state = this.get();
|
|
534
832
|
const out32 = u32(out);
|
|
535
|
-
|
|
833
|
+
const full = Math.floor(this.outputLen / 4);
|
|
834
|
+
for (let i = 0; i < full; i++)
|
|
835
|
+
out32[i] = swap8IfBE(state[i]);
|
|
836
|
+
const tail = this.outputLen % 4;
|
|
837
|
+
if (!tail)
|
|
838
|
+
return;
|
|
839
|
+
const off = full * 4;
|
|
840
|
+
const word = state[full];
|
|
841
|
+
for (let i = 0; i < tail; i++)
|
|
842
|
+
out[off + i] = word >>> (8 * i);
|
|
536
843
|
}
|
|
537
844
|
digest() {
|
|
538
845
|
const { buffer, outputLen } = this;
|
|
539
846
|
this.digestInto(buffer);
|
|
847
|
+
// Return a copy so callers do not alias the instance scratch buffer used during finalization.
|
|
540
848
|
const res = buffer.slice(0, outputLen);
|
|
541
849
|
this.destroy();
|
|
542
850
|
return res;
|
|
543
851
|
}
|
|
544
852
|
_cloneInto(to) {
|
|
545
853
|
const { buffer, length, finished, destroyed, outputLen, pos } = this;
|
|
854
|
+
// Recreate only `dkLen`; key/salt/personalization are already absorbed into the copied state.
|
|
546
855
|
to ||= new this.constructor({ dkLen: outputLen });
|
|
547
856
|
to.set(...this.get());
|
|
548
857
|
to.buffer.set(buffer);
|
|
@@ -558,9 +867,9 @@ class _BLAKE2 {
|
|
|
558
867
|
return this._cloneInto();
|
|
559
868
|
}
|
|
560
869
|
}
|
|
561
|
-
/** Internal blake2b hash class. */
|
|
870
|
+
/** Internal blake2b hash class with state stored as LE u32 low/high halves. */
|
|
562
871
|
class _BLAKE2b extends _BLAKE2 {
|
|
563
|
-
// Same as SHA-512,
|
|
872
|
+
// Same IV words as SHA-512 / BLAKE2b, encoded as LE u32 low/high halves.
|
|
564
873
|
v0l = B2B_IV[0] | 0;
|
|
565
874
|
v0h = B2B_IV[1] | 0;
|
|
566
875
|
v1l = B2B_IV[2] | 0;
|
|
@@ -587,6 +896,8 @@ class _BLAKE2b extends _BLAKE2 {
|
|
|
587
896
|
abytes(key, undefined, 'key');
|
|
588
897
|
keyLength = key.length;
|
|
589
898
|
}
|
|
899
|
+
// RFC 7693 §2.5: xor `p[0] = 0x0101kknn` into the low 32 bits of `h[0]`;
|
|
900
|
+
// the high 32 bits stay at `IV[0]`.
|
|
590
901
|
this.v0l ^= this.outputLen | (keyLength << 8) | (0x01 << 16) | (0x01 << 24);
|
|
591
902
|
if (salt !== undefined) {
|
|
592
903
|
abytes(salt, undefined, 'salt');
|
|
@@ -648,6 +959,8 @@ class _BLAKE2b extends _BLAKE2 {
|
|
|
648
959
|
}
|
|
649
960
|
let j = 0;
|
|
650
961
|
const s = BSIGMA;
|
|
962
|
+
// SIGMA selects 64-bit message words; multiply by 2 because `msg` stores
|
|
963
|
+
// each word as [low32, high32].
|
|
651
964
|
for (let i = 0; i < 12; i++) {
|
|
652
965
|
G1b(0, 4, 8, 12, msg, offset + 2 * s[j++]);
|
|
653
966
|
G2b(0, 4, 8, 12, msg, offset + 2 * s[j++]);
|
|
@@ -693,7 +1006,15 @@ class _BLAKE2b extends _BLAKE2 {
|
|
|
693
1006
|
/**
|
|
694
1007
|
* Blake2b hash function. 64-bit. 1.5x slower than blake2s in JS.
|
|
695
1008
|
* @param msg - message that would be hashed
|
|
696
|
-
* @param opts -
|
|
1009
|
+
* @param opts - Optional output, MAC, salt, and personalization settings.
|
|
1010
|
+
* `dkLen` must be 1..64 bytes; `salt` and `personalization`, if present,
|
|
1011
|
+
* must be 16 bytes each. See {@link Blake2Opts}.
|
|
1012
|
+
* @returns Digest bytes.
|
|
1013
|
+
* @example
|
|
1014
|
+
* Hash a message with Blake2b.
|
|
1015
|
+
* ```ts
|
|
1016
|
+
* blake2b(new Uint8Array([97, 98, 99]));
|
|
1017
|
+
* ```
|
|
697
1018
|
*/
|
|
698
1019
|
const blake2b = /* @__PURE__ */ createHasher((opts) => new _BLAKE2b(opts));
|
|
699
1020
|
|
|
@@ -1057,12 +1378,17 @@ _ECPublicKey_key = new WeakMap();
|
|
|
1057
1378
|
* HMAC: RFC2104 message authentication code.
|
|
1058
1379
|
* @module
|
|
1059
1380
|
*/
|
|
1060
|
-
/**
|
|
1381
|
+
/**
|
|
1382
|
+
* Internal class for HMAC.
|
|
1383
|
+
* Accepts any byte key, although RFC 2104 §3 recommends keys at least
|
|
1384
|
+
* `HashLen` bytes long.
|
|
1385
|
+
*/
|
|
1061
1386
|
class _HMAC {
|
|
1062
1387
|
oHash;
|
|
1063
1388
|
iHash;
|
|
1064
1389
|
blockLen;
|
|
1065
1390
|
outputLen;
|
|
1391
|
+
canXOF = false;
|
|
1066
1392
|
finished = false;
|
|
1067
1393
|
destroyed = false;
|
|
1068
1394
|
constructor(hash, key) {
|
|
@@ -1080,7 +1406,8 @@ class _HMAC {
|
|
|
1080
1406
|
for (let i = 0; i < pad.length; i++)
|
|
1081
1407
|
pad[i] ^= 0x36;
|
|
1082
1408
|
this.iHash.update(pad);
|
|
1083
|
-
// By doing update (processing of first block) of outer hash here
|
|
1409
|
+
// By doing update (processing of the first block) of the outer hash here,
|
|
1410
|
+
// we can re-use it between multiple calls via clone.
|
|
1084
1411
|
this.oHash = hash.create();
|
|
1085
1412
|
// Undo internal XOR && apply outer XOR
|
|
1086
1413
|
for (let i = 0; i < pad.length; i++)
|
|
@@ -1095,11 +1422,14 @@ class _HMAC {
|
|
|
1095
1422
|
}
|
|
1096
1423
|
digestInto(out) {
|
|
1097
1424
|
aexists(this);
|
|
1098
|
-
|
|
1425
|
+
aoutput(out, this);
|
|
1099
1426
|
this.finished = true;
|
|
1100
|
-
this.
|
|
1101
|
-
|
|
1102
|
-
|
|
1427
|
+
const buf = out.subarray(0, this.outputLen);
|
|
1428
|
+
// Reuse the first outputLen bytes for the inner digest; the outer hash consumes them before
|
|
1429
|
+
// overwriting that same prefix with the final tag, leaving any oversized tail untouched.
|
|
1430
|
+
this.iHash.digestInto(buf);
|
|
1431
|
+
this.oHash.update(buf);
|
|
1432
|
+
this.oHash.digestInto(buf);
|
|
1103
1433
|
this.destroy();
|
|
1104
1434
|
}
|
|
1105
1435
|
digest() {
|
|
@@ -1108,7 +1438,8 @@ class _HMAC {
|
|
|
1108
1438
|
return out;
|
|
1109
1439
|
}
|
|
1110
1440
|
_cloneInto(to) {
|
|
1111
|
-
// Create new instance without calling constructor since key
|
|
1441
|
+
// Create new instance without calling constructor since the key
|
|
1442
|
+
// is already in state and we don't know it.
|
|
1112
1443
|
to ||= Object.create(Object.getPrototypeOf(this), {});
|
|
1113
1444
|
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
|
1114
1445
|
to = to;
|
|
@@ -1129,18 +1460,11 @@ class _HMAC {
|
|
|
1129
1460
|
this.iHash.destroy();
|
|
1130
1461
|
}
|
|
1131
1462
|
}
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
* @example
|
|
1138
|
-
* import { hmac } from '@noble/hashes/hmac';
|
|
1139
|
-
* import { sha256 } from '@noble/hashes/sha2';
|
|
1140
|
-
* const mac1 = hmac(sha256, 'key', 'message');
|
|
1141
|
-
*/
|
|
1142
|
-
const hmac = (hash, key, message) => new _HMAC(hash, key).update(message).digest();
|
|
1143
|
-
hmac.create = (hash, key) => new _HMAC(hash, key);
|
|
1463
|
+
const hmac = /* @__PURE__ */ (() => {
|
|
1464
|
+
const hmac_ = ((hash, key, message) => new _HMAC(hash, key).update(message).digest());
|
|
1465
|
+
hmac_.create = (hash, key) => new _HMAC(hash, key);
|
|
1466
|
+
return hmac_;
|
|
1467
|
+
})();
|
|
1144
1468
|
|
|
1145
1469
|
/**
|
|
1146
1470
|
* PBKDF (RFC 2898). Can be used to create a key from password and salt.
|
|
@@ -1156,16 +1480,26 @@ function pbkdf2Init(hash, _password, _salt, _opts) {
|
|
|
1156
1480
|
anumber$1(asyncTick, 'asyncTick');
|
|
1157
1481
|
if (c < 1)
|
|
1158
1482
|
throw new Error('iterations (c) must be >= 1');
|
|
1483
|
+
// RFC 8018 §5.2 defines `dkLen` as "a positive integer".
|
|
1484
|
+
if (dkLen < 1)
|
|
1485
|
+
throw new Error('"dkLen" must be >= 1');
|
|
1486
|
+
// RFC 8018 §5.2 step 1 requires rejecting oversize `dkLen`
|
|
1487
|
+
// before allocating the destination buffer.
|
|
1488
|
+
if (dkLen > (2 ** 32 - 1) * hash.outputLen)
|
|
1489
|
+
throw new Error('derived key too long');
|
|
1159
1490
|
const password = kdfInputToBytes(_password, 'password');
|
|
1160
1491
|
const salt = kdfInputToBytes(_salt, 'salt');
|
|
1161
1492
|
// DK = PBKDF2(PRF, Password, Salt, c, dkLen);
|
|
1162
1493
|
const DK = new Uint8Array(dkLen);
|
|
1163
1494
|
// U1 = PRF(Password, Salt + INT_32_BE(i))
|
|
1164
1495
|
const PRF = hmac.create(hash, password);
|
|
1496
|
+
// Cache PRF(P, S || ...) prefix state so each block only appends INT_32_BE(i).
|
|
1165
1497
|
const PRFSalt = PRF._cloneInto().update(salt);
|
|
1166
1498
|
return { c, dkLen, asyncTick, DK, PRF, PRFSalt };
|
|
1167
1499
|
}
|
|
1168
1500
|
function pbkdf2Output(PRF, PRFSalt, DK, prfW, u) {
|
|
1501
|
+
// Shared sync/async cleanup point: wipe transient PRF state
|
|
1502
|
+
// while preserving the derived key buffer.
|
|
1169
1503
|
PRF.destroy();
|
|
1170
1504
|
PRFSalt.destroy();
|
|
1171
1505
|
if (prfW)
|
|
@@ -1174,13 +1508,22 @@ function pbkdf2Output(PRF, PRFSalt, DK, prfW, u) {
|
|
|
1174
1508
|
return DK;
|
|
1175
1509
|
}
|
|
1176
1510
|
/**
|
|
1177
|
-
* PBKDF2-HMAC: RFC
|
|
1511
|
+
* PBKDF2-HMAC: RFC 8018 key derivation function.
|
|
1178
1512
|
* @param hash - hash function that would be used e.g. sha256
|
|
1179
|
-
* @param password - password from which a derived key is generated
|
|
1180
|
-
*
|
|
1181
|
-
* @param
|
|
1513
|
+
* @param password - password from which a derived key is generated;
|
|
1514
|
+
* JS string inputs are UTF-8 encoded first
|
|
1515
|
+
* @param salt - cryptographic salt; JS string inputs are UTF-8 encoded first
|
|
1516
|
+
* @param opts - PBKDF2 work factor and output settings. `dkLen`, if provided,
|
|
1517
|
+
* must be `>= 1` per RFC 8018 §5.2. See {@link Pbkdf2Opt}.
|
|
1518
|
+
* @returns Derived key bytes.
|
|
1519
|
+
* @throws If the PBKDF2 iteration count or derived-key settings are invalid. {@link Error}
|
|
1182
1520
|
* @example
|
|
1521
|
+
* PBKDF2-HMAC: RFC 2898 key derivation function.
|
|
1522
|
+
* ```ts
|
|
1523
|
+
* import { pbkdf2 } from '@noble/hashes/pbkdf2.js';
|
|
1524
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
1183
1525
|
* const key = pbkdf2(sha256, 'password', 'salt', { dkLen: 32, c: Math.pow(2, 18) });
|
|
1526
|
+
* ```
|
|
1184
1527
|
*/
|
|
1185
1528
|
function pbkdf2(hash, password, salt, opts) {
|
|
1186
1529
|
const { c, dkLen, DK, PRF, PRFSalt } = pbkdf2Init(hash, password, salt, opts);
|
|
@@ -1191,6 +1534,8 @@ function pbkdf2(hash, password, salt, opts) {
|
|
|
1191
1534
|
// DK = T1 + T2 + ⋯ + Tdklen/hlen
|
|
1192
1535
|
for (let ti = 1, pos = 0; pos < dkLen; ti++, pos += PRF.outputLen) {
|
|
1193
1536
|
// Ti = F(Password, Salt, c, i)
|
|
1537
|
+
// The last Ti view can be shorter than hLen, which applies
|
|
1538
|
+
// RFC 8018 §5.2 step 4's T_l<0..r-1> truncation without extra copies.
|
|
1194
1539
|
const Ti = DK.subarray(pos, pos + PRF.outputLen);
|
|
1195
1540
|
view.setInt32(0, ti, false);
|
|
1196
1541
|
// F(Password, Salt, c, i) = U1 ^ U2 ^ ⋯ ^ Uc
|
|
@@ -1210,13 +1555,13 @@ function pbkdf2(hash, password, salt, opts) {
|
|
|
1210
1555
|
/**
|
|
1211
1556
|
* SHA2 hash function. A.k.a. sha256, sha384, sha512, sha512_224, sha512_256.
|
|
1212
1557
|
* SHA256 is the fastest hash implementable in JS, even faster than Blake3.
|
|
1213
|
-
* Check out
|
|
1214
|
-
*
|
|
1558
|
+
* Check out {@link https://www.rfc-editor.org/rfc/rfc4634 | RFC 4634} and
|
|
1559
|
+
* {@link https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf | FIPS 180-4}.
|
|
1215
1560
|
* @module
|
|
1216
1561
|
*/
|
|
1217
1562
|
/**
|
|
1218
|
-
*
|
|
1219
|
-
*
|
|
1563
|
+
* SHA-224 / SHA-256 round constants from RFC 6234 §5.1: the first 32 bits
|
|
1564
|
+
* of the cube roots of the first 64 primes (2..311).
|
|
1220
1565
|
*/
|
|
1221
1566
|
// prettier-ignore
|
|
1222
1567
|
const SHA256_K = /* @__PURE__ */ Uint32Array.from([
|
|
@@ -1229,9 +1574,9 @@ const SHA256_K = /* @__PURE__ */ Uint32Array.from([
|
|
|
1229
1574
|
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
1230
1575
|
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
1231
1576
|
]);
|
|
1232
|
-
/** Reusable
|
|
1577
|
+
/** Reusable SHA-224 / SHA-256 message schedule buffer `W_t` from RFC 6234 §6.2 step 1. */
|
|
1233
1578
|
const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
1234
|
-
/** Internal
|
|
1579
|
+
/** Internal SHA-224 / SHA-256 compression engine from RFC 6234 §6.2. */
|
|
1235
1580
|
class SHA2_32B extends HashMD {
|
|
1236
1581
|
constructor(outputLen) {
|
|
1237
1582
|
super(64, outputLen, 8, false);
|
|
@@ -1293,11 +1638,14 @@ class SHA2_32B extends HashMD {
|
|
|
1293
1638
|
clean(SHA256_W);
|
|
1294
1639
|
}
|
|
1295
1640
|
destroy() {
|
|
1641
|
+
// HashMD callers route post-destroy usability through `destroyed`; zeroizing alone still leaves
|
|
1642
|
+
// update()/digest() callable on reused instances.
|
|
1643
|
+
this.destroyed = true;
|
|
1296
1644
|
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
1297
1645
|
clean(this.buffer);
|
|
1298
1646
|
}
|
|
1299
1647
|
}
|
|
1300
|
-
/** Internal
|
|
1648
|
+
/** Internal SHA-256 hash class grounded in RFC 6234 §6.2. */
|
|
1301
1649
|
class _SHA256 extends SHA2_32B {
|
|
1302
1650
|
// We cannot use array here since array allows indexing by variable
|
|
1303
1651
|
// which means optimizer/compiler cannot use registers.
|
|
@@ -1314,8 +1662,8 @@ class _SHA256 extends SHA2_32B {
|
|
|
1314
1662
|
}
|
|
1315
1663
|
}
|
|
1316
1664
|
// SHA2-512 is slower than sha256 in js because u64 operations are slow.
|
|
1317
|
-
//
|
|
1318
|
-
//
|
|
1665
|
+
// SHA-384 / SHA-512 round constants from RFC 6234 §5.2:
|
|
1666
|
+
// 80 full 64-bit words split into high/low halves.
|
|
1319
1667
|
// prettier-ignore
|
|
1320
1668
|
const K512 = /* @__PURE__ */ (() => split([
|
|
1321
1669
|
'0x428a2f98d728ae22', '0x7137449123ef65cd', '0xb5c0fbcfec4d3b2f', '0xe9b5dba58189dbbc',
|
|
@@ -1341,10 +1689,11 @@ const K512 = /* @__PURE__ */ (() => split([
|
|
|
1341
1689
|
].map(n => BigInt(n))))();
|
|
1342
1690
|
const SHA512_Kh = /* @__PURE__ */ (() => K512[0])();
|
|
1343
1691
|
const SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
|
|
1344
|
-
// Reusable
|
|
1692
|
+
// Reusable high-half schedule buffer for the RFC 6234 §6.4 64-bit `W_t` words.
|
|
1345
1693
|
const SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
|
|
1694
|
+
// Reusable low-half schedule buffer for the RFC 6234 §6.4 64-bit `W_t` words.
|
|
1346
1695
|
const SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
|
|
1347
|
-
/** Internal
|
|
1696
|
+
/** Internal SHA-384 / SHA-512 compression engine from RFC 6234 §6.4. */
|
|
1348
1697
|
class SHA2_64B extends HashMD {
|
|
1349
1698
|
constructor(outputLen) {
|
|
1350
1699
|
super(128, outputLen, 16, false);
|
|
@@ -1390,7 +1739,7 @@ class SHA2_64B extends HashMD {
|
|
|
1390
1739
|
const W2l = SHA512_W_L[i - 2] | 0;
|
|
1391
1740
|
const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
|
|
1392
1741
|
const s1l = rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6);
|
|
1393
|
-
//
|
|
1742
|
+
// SHA512_W[i] = s0 + s1 + SHA512_W[i - 7] + SHA512_W[i - 16];
|
|
1394
1743
|
const SUMl = add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
|
|
1395
1744
|
const SUMh = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
|
|
1396
1745
|
SHA512_W_H[i] = SUMh | 0;
|
|
@@ -1447,11 +1796,14 @@ class SHA2_64B extends HashMD {
|
|
|
1447
1796
|
clean(SHA512_W_H, SHA512_W_L);
|
|
1448
1797
|
}
|
|
1449
1798
|
destroy() {
|
|
1799
|
+
// HashMD callers route post-destroy usability through `destroyed`; zeroizing alone still leaves
|
|
1800
|
+
// update()/digest() callable on reused instances.
|
|
1801
|
+
this.destroyed = true;
|
|
1450
1802
|
clean(this.buffer);
|
|
1451
1803
|
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
1452
1804
|
}
|
|
1453
1805
|
}
|
|
1454
|
-
/** Internal
|
|
1806
|
+
/** Internal SHA-512 hash class grounded in RFC 6234 §6.3 and §6.4. */
|
|
1455
1807
|
class _SHA512 extends SHA2_64B {
|
|
1456
1808
|
Ah = SHA512_IV[0] | 0;
|
|
1457
1809
|
Al = SHA512_IV[1] | 0;
|
|
@@ -1480,16 +1832,40 @@ class _SHA512 extends SHA2_64B {
|
|
|
1480
1832
|
* - BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
|
|
1481
1833
|
* - Each sha256 hash is executing 2^18 bit operations.
|
|
1482
1834
|
* - Good 2024 ASICs can do 200Th/sec with 3500 watts of power, corresponding to 2^36 hashes/joule.
|
|
1835
|
+
* @param msg - message bytes to hash
|
|
1836
|
+
* @returns Digest bytes.
|
|
1837
|
+
* @example
|
|
1838
|
+
* Hash a message with SHA2-256.
|
|
1839
|
+
* ```ts
|
|
1840
|
+
* sha256(new Uint8Array([97, 98, 99]));
|
|
1841
|
+
* ```
|
|
1483
1842
|
*/
|
|
1484
1843
|
const sha256 = /* @__PURE__ */ createHasher(() => new _SHA256(),
|
|
1485
1844
|
/* @__PURE__ */ oidNist(0x01));
|
|
1486
|
-
/**
|
|
1845
|
+
/**
|
|
1846
|
+
* SHA2-512 hash function from RFC 4634.
|
|
1847
|
+
* @param msg - message bytes to hash
|
|
1848
|
+
* @returns Digest bytes.
|
|
1849
|
+
* @example
|
|
1850
|
+
* Hash a message with SHA2-512.
|
|
1851
|
+
* ```ts
|
|
1852
|
+
* sha512(new Uint8Array([97, 98, 99]));
|
|
1853
|
+
* ```
|
|
1854
|
+
*/
|
|
1487
1855
|
const sha512 = /* @__PURE__ */ createHasher(() => new _SHA512(),
|
|
1488
1856
|
/* @__PURE__ */ oidNist(0x03));
|
|
1489
1857
|
|
|
1490
1858
|
/*! scure-base - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
1491
1859
|
function isBytes(a) {
|
|
1492
|
-
|
|
1860
|
+
// Plain `instanceof Uint8Array` is too strict for some Buffer / proxy / cross-realm cases. The
|
|
1861
|
+
// fallback still requires a real ArrayBuffer view, so plain JSON-deserialized
|
|
1862
|
+
// `{ constructor: ... }` spoofing is rejected. `BYTES_PER_ELEMENT === 1` keeps the
|
|
1863
|
+
// fallback on byte-oriented views.
|
|
1864
|
+
return (a instanceof Uint8Array ||
|
|
1865
|
+
(ArrayBuffer.isView(a) &&
|
|
1866
|
+
a.constructor.name === 'Uint8Array' &&
|
|
1867
|
+
'BYTES_PER_ELEMENT' in a &&
|
|
1868
|
+
a.BYTES_PER_ELEMENT === 1));
|
|
1493
1869
|
}
|
|
1494
1870
|
function isArrayOf(isString, arr) {
|
|
1495
1871
|
if (!Array.isArray(arr))
|
|
@@ -1505,29 +1881,31 @@ function isArrayOf(isString, arr) {
|
|
|
1505
1881
|
}
|
|
1506
1882
|
function afn(input) {
|
|
1507
1883
|
if (typeof input !== 'function')
|
|
1508
|
-
throw new
|
|
1884
|
+
throw new TypeError('function expected');
|
|
1509
1885
|
return true;
|
|
1510
1886
|
}
|
|
1511
1887
|
function astr(label, input) {
|
|
1512
1888
|
if (typeof input !== 'string')
|
|
1513
|
-
throw new
|
|
1889
|
+
throw new TypeError(`${label}: string expected`);
|
|
1514
1890
|
return true;
|
|
1515
1891
|
}
|
|
1516
1892
|
function anumber(n) {
|
|
1893
|
+
if (typeof n !== 'number')
|
|
1894
|
+
throw new TypeError(`number expected, got ${typeof n}`);
|
|
1517
1895
|
if (!Number.isSafeInteger(n))
|
|
1518
|
-
throw new
|
|
1896
|
+
throw new RangeError(`invalid integer: ${n}`);
|
|
1519
1897
|
}
|
|
1520
1898
|
function aArr(input) {
|
|
1521
1899
|
if (!Array.isArray(input))
|
|
1522
|
-
throw new
|
|
1900
|
+
throw new TypeError('array expected');
|
|
1523
1901
|
}
|
|
1524
1902
|
function astrArr(label, input) {
|
|
1525
1903
|
if (!isArrayOf(true, input))
|
|
1526
|
-
throw new
|
|
1904
|
+
throw new TypeError(`${label}: array of strings expected`);
|
|
1527
1905
|
}
|
|
1528
1906
|
function anumArr(label, input) {
|
|
1529
1907
|
if (!isArrayOf(false, input))
|
|
1530
|
-
throw new
|
|
1908
|
+
throw new TypeError(`${label}: array of numbers expected`);
|
|
1531
1909
|
}
|
|
1532
1910
|
/**
|
|
1533
1911
|
* @__NO_SIDE_EFFECTS__
|
|
@@ -1580,6 +1958,8 @@ function alphabet(letters) {
|
|
|
1580
1958
|
*/
|
|
1581
1959
|
function join(separator = '') {
|
|
1582
1960
|
astr('join', separator);
|
|
1961
|
+
// join('') is only lossless when each chunk is already unambiguous, such as single-symbol alphabets.
|
|
1962
|
+
// Multi-character tokens need a separator that cannot appear inside the chunks.
|
|
1583
1963
|
return {
|
|
1584
1964
|
encode: (from) => {
|
|
1585
1965
|
astrArr('join.decode', from);
|
|
@@ -1601,6 +1981,8 @@ function padding(bits, chr = '=') {
|
|
|
1601
1981
|
return {
|
|
1602
1982
|
encode(data) {
|
|
1603
1983
|
astrArr('padding.encode', data);
|
|
1984
|
+
// Mutates the intermediate token array in place while appending pad chars.
|
|
1985
|
+
// utils.padding callers that need to preserve their input should pass a copy.
|
|
1604
1986
|
while ((data.length * bits) % 8)
|
|
1605
1987
|
data.push(chr);
|
|
1606
1988
|
return data;
|
|
@@ -1626,9 +2008,9 @@ function padding(bits, chr = '=') {
|
|
|
1626
2008
|
function convertRadix(data, from, to) {
|
|
1627
2009
|
// base 1 is impossible
|
|
1628
2010
|
if (from < 2)
|
|
1629
|
-
throw new
|
|
2011
|
+
throw new RangeError(`convertRadix: invalid from=${from}, base cannot be less than 2`);
|
|
1630
2012
|
if (to < 2)
|
|
1631
|
-
throw new
|
|
2013
|
+
throw new RangeError(`convertRadix: invalid to=${to}, base cannot be less than 2`);
|
|
1632
2014
|
aArr(data);
|
|
1633
2015
|
if (!data.length)
|
|
1634
2016
|
return [];
|
|
@@ -1670,11 +2052,14 @@ function convertRadix(data, from, to) {
|
|
|
1670
2052
|
if (done)
|
|
1671
2053
|
break;
|
|
1672
2054
|
}
|
|
2055
|
+
// Preserve explicit leading zero digits so callers like base58 keep zero-prefix semantics.
|
|
1673
2056
|
for (let i = 0; i < data.length - 1 && data[i] === 0; i++)
|
|
1674
2057
|
res.push(0);
|
|
1675
2058
|
return res.reverse();
|
|
1676
2059
|
}
|
|
1677
2060
|
const gcd = (a, b) => (b === 0 ? a : gcd(b, a % b));
|
|
2061
|
+
// Maximum carry width before the `pos` cycle repeats.
|
|
2062
|
+
// Residues advance in gcd(from, to) steps, so the largest pre-drain width is from + (to - gcd).
|
|
1678
2063
|
const radix2carry = /* @__NO_SIDE_EFFECTS__ */ (from, to) => from + (to - gcd(from, to));
|
|
1679
2064
|
const powers = /* @__PURE__ */ (() => {
|
|
1680
2065
|
let res = [];
|
|
@@ -1688,9 +2073,9 @@ const powers = /* @__PURE__ */ (() => {
|
|
|
1688
2073
|
function convertRadix2(data, from, to, padding) {
|
|
1689
2074
|
aArr(data);
|
|
1690
2075
|
if (from <= 0 || from > 32)
|
|
1691
|
-
throw new
|
|
2076
|
+
throw new RangeError(`convertRadix2: wrong from=${from}`);
|
|
1692
2077
|
if (to <= 0 || to > 32)
|
|
1693
|
-
throw new
|
|
2078
|
+
throw new RangeError(`convertRadix2: wrong to=${to}`);
|
|
1694
2079
|
if (radix2carry(from, to) > 32) {
|
|
1695
2080
|
throw new Error(`convertRadix2: carry overflow from=${from} to=${to} carryBits=${radix2carry(from, to)}`);
|
|
1696
2081
|
}
|
|
@@ -1715,6 +2100,8 @@ function convertRadix2(data, from, to, padding) {
|
|
|
1715
2100
|
carry &= pow - 1; // clean carry, otherwise it will cause overflow
|
|
1716
2101
|
}
|
|
1717
2102
|
carry = (carry << (to - pos)) & mask;
|
|
2103
|
+
// Canonical decode paths reject leftover whole input words and non-zero pad bits.
|
|
2104
|
+
// For Bech32 5->8 regrouping, this is the "4 bits or less, all zeroes" tail rule.
|
|
1718
2105
|
if (!padding && pos >= from)
|
|
1719
2106
|
throw new Error('Excess padding');
|
|
1720
2107
|
if (!padding && carry > 0)
|
|
@@ -1729,10 +2116,11 @@ function convertRadix2(data, from, to, padding) {
|
|
|
1729
2116
|
function radix(num) {
|
|
1730
2117
|
anumber(num);
|
|
1731
2118
|
const _256 = 2 ** 8;
|
|
2119
|
+
// Base-range and carry-overflow checks live in convertRadix so encode/decode reject unsupported bases symmetrically.
|
|
1732
2120
|
return {
|
|
1733
2121
|
encode: (bytes) => {
|
|
1734
2122
|
if (!isBytes(bytes))
|
|
1735
|
-
throw new
|
|
2123
|
+
throw new TypeError('radix.encode input should be Uint8Array');
|
|
1736
2124
|
return convertRadix(Array.from(bytes), _256, num);
|
|
1737
2125
|
},
|
|
1738
2126
|
decode: (digits) => {
|
|
@@ -1749,13 +2137,15 @@ function radix(num) {
|
|
|
1749
2137
|
function radix2(bits, revPadding = false) {
|
|
1750
2138
|
anumber(bits);
|
|
1751
2139
|
if (bits <= 0 || bits > 32)
|
|
1752
|
-
throw new
|
|
2140
|
+
throw new RangeError('radix2: bits should be in (0..32]');
|
|
1753
2141
|
if (radix2carry(8, bits) > 32 || radix2carry(bits, 8) > 32)
|
|
1754
|
-
throw new
|
|
2142
|
+
throw new RangeError('radix2: carry overflow');
|
|
2143
|
+
// revPadding flips which direction allows a partial zero tail.
|
|
2144
|
+
// Default pads 8->bits and rejects extra bits on bits->8; `true` does the opposite.
|
|
1755
2145
|
return {
|
|
1756
2146
|
encode: (bytes) => {
|
|
1757
2147
|
if (!isBytes(bytes))
|
|
1758
|
-
throw new
|
|
2148
|
+
throw new TypeError('radix2.encode input should be Uint8Array');
|
|
1759
2149
|
return convertRadix2(Array.from(bytes), 8, bits, !revPadding);
|
|
1760
2150
|
},
|
|
1761
2151
|
decode: (digits) => {
|
|
@@ -1766,12 +2156,19 @@ function radix2(bits, revPadding = false) {
|
|
|
1766
2156
|
}
|
|
1767
2157
|
function checksum(len, fn) {
|
|
1768
2158
|
anumber(len);
|
|
2159
|
+
// Reject degenerate zero-byte checksums up front so callers don't accidentally
|
|
2160
|
+
// build a no-op checksum stage.
|
|
2161
|
+
if (len <= 0)
|
|
2162
|
+
throw new RangeError(`checksum length must be positive: ${len}`);
|
|
1769
2163
|
afn(fn);
|
|
2164
|
+
const _fn = fn;
|
|
2165
|
+
// Uses the first `len` bytes of fn(data) in both directions.
|
|
2166
|
+
// Current call sites rely on `len > 0` and checksum functions that return at least that many bytes.
|
|
1770
2167
|
return {
|
|
1771
2168
|
encode(data) {
|
|
1772
2169
|
if (!isBytes(data))
|
|
1773
|
-
throw new
|
|
1774
|
-
const sum =
|
|
2170
|
+
throw new TypeError('checksum.encode: input should be Uint8Array');
|
|
2171
|
+
const sum = _fn(data).slice(0, len);
|
|
1775
2172
|
const res = new Uint8Array(data.length + len);
|
|
1776
2173
|
res.set(data);
|
|
1777
2174
|
res.set(sum, data.length);
|
|
@@ -1779,10 +2176,10 @@ function checksum(len, fn) {
|
|
|
1779
2176
|
},
|
|
1780
2177
|
decode(data) {
|
|
1781
2178
|
if (!isBytes(data))
|
|
1782
|
-
throw new
|
|
2179
|
+
throw new TypeError('checksum.decode: input should be Uint8Array');
|
|
1783
2180
|
const payload = data.slice(0, -len);
|
|
1784
2181
|
const oldChecksum = data.slice(-len);
|
|
1785
|
-
const newChecksum =
|
|
2182
|
+
const newChecksum = _fn(payload).slice(0, len);
|
|
1786
2183
|
for (let i = 0; i < len; i++)
|
|
1787
2184
|
if (newChecksum[i] !== oldChecksum[i])
|
|
1788
2185
|
throw new Error('Invalid checksum');
|
|
@@ -1791,20 +2188,33 @@ function checksum(len, fn) {
|
|
|
1791
2188
|
};
|
|
1792
2189
|
}
|
|
1793
2190
|
// prettier-ignore
|
|
1794
|
-
|
|
2191
|
+
/**
|
|
2192
|
+
* Low-level building blocks used by the exported codecs.
|
|
2193
|
+
* @example
|
|
2194
|
+
* Build a radix-32 coder from the low-level helpers.
|
|
2195
|
+
* ```ts
|
|
2196
|
+
* import { utils } from '@scure/base';
|
|
2197
|
+
* utils.radix2(5).encode(Uint8Array.from([1, 2, 3]));
|
|
2198
|
+
* ```
|
|
2199
|
+
*/
|
|
2200
|
+
const utils = /* @__PURE__ */ Object.freeze({
|
|
1795
2201
|
alphabet, chain, checksum, convertRadix, convertRadix2, radix, radix2, join, padding,
|
|
1796
|
-
};
|
|
2202
|
+
});
|
|
1797
2203
|
|
|
1798
2204
|
/*! scure-bip39 - MIT License (c) 2022 Patricio Palladino, Paul Miller (paulmillr.com) */
|
|
1799
2205
|
// Normalization replaces equivalent sequences of characters
|
|
1800
2206
|
// so that any two texts that are equivalent will be reduced
|
|
1801
2207
|
// to the same sequence of code points, called the normal form of the original text.
|
|
1802
2208
|
// https://tonsky.me/blog/unicode/#why-is-a----
|
|
2209
|
+
// BIP-39 requires UTF-8 NFKD for localized wordlists and mnemonic sentences.
|
|
2210
|
+
// It also applies NFKD to the "mnemonic" + passphrase salt.
|
|
1803
2211
|
function nfkd(str) {
|
|
1804
2212
|
if (typeof str !== 'string')
|
|
1805
2213
|
throw new TypeError('invalid mnemonic type: ' + typeof str);
|
|
1806
2214
|
return str.normalize('NFKD');
|
|
1807
2215
|
}
|
|
2216
|
+
// BIP-39 mnemonics are consumed in NFKD form.
|
|
2217
|
+
// They must contain 12, 15, 18, 21, or 24 words before checksum validation.
|
|
1808
2218
|
function normalize(str) {
|
|
1809
2219
|
const norm = nfkd(str);
|
|
1810
2220
|
const words = norm.split(' ');
|
|
@@ -1812,10 +2222,11 @@ function normalize(str) {
|
|
|
1812
2222
|
throw new Error('Invalid mnemonic');
|
|
1813
2223
|
return { nfkd: norm, words };
|
|
1814
2224
|
}
|
|
2225
|
+
// BIP-39 entropy payloads are 128-256 bits in 32-bit increments, i.e. 16/20/24/28/32 bytes.
|
|
1815
2226
|
function aentropy(ent) {
|
|
1816
2227
|
abytes(ent);
|
|
1817
2228
|
if (![16, 20, 24, 28, 32].includes(ent.length))
|
|
1818
|
-
throw new
|
|
2229
|
+
throw new RangeError('invalid entropy length');
|
|
1819
2230
|
}
|
|
1820
2231
|
const calcChecksum = (entropy) => {
|
|
1821
2232
|
// Checksum is ent.length/4 bits long
|
|
@@ -1826,25 +2237,36 @@ const calcChecksum = (entropy) => {
|
|
|
1826
2237
|
};
|
|
1827
2238
|
function getCoder(wordlist) {
|
|
1828
2239
|
if (!Array.isArray(wordlist) || wordlist.length !== 2048 || typeof wordlist[0] !== 'string')
|
|
1829
|
-
throw new
|
|
2240
|
+
throw new TypeError('Wordlist: expected array of 2048 strings');
|
|
1830
2241
|
wordlist.forEach((i) => {
|
|
1831
2242
|
if (typeof i !== 'string')
|
|
1832
|
-
throw new
|
|
2243
|
+
throw new TypeError('wordlist: non-string element: ' + i);
|
|
1833
2244
|
});
|
|
2245
|
+
// BIP-39 appends checksum bits to entropy.
|
|
2246
|
+
// It then splits the bitstream into 11-bit indexes for a 2048-word list.
|
|
1834
2247
|
return utils.chain(utils.checksum(1, calcChecksum), utils.radix2(11, true), utils.alphabet(wordlist));
|
|
1835
2248
|
}
|
|
1836
2249
|
/**
|
|
1837
2250
|
* Reversible: Converts mnemonic string to raw entropy in form of byte array.
|
|
1838
|
-
* @param mnemonic 12-24 words
|
|
1839
|
-
* @param wordlist
|
|
2251
|
+
* @param mnemonic - 12-24 words.
|
|
2252
|
+
* @param wordlist - Imported wordlist for a specific language.
|
|
2253
|
+
* @returns Raw entropy bytes.
|
|
2254
|
+
* @throws If the mnemonic shape or checksum is invalid. {@link Error}
|
|
2255
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
2256
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
1840
2257
|
* @example
|
|
2258
|
+
* Decode a mnemonic back into its original entropy bytes.
|
|
2259
|
+
* ```ts
|
|
2260
|
+
* import { mnemonicToEntropy } from '@scure/bip39';
|
|
2261
|
+
* import { wordlist } from '@scure/bip39/wordlists/english.js';
|
|
1841
2262
|
* const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
|
|
1842
|
-
* mnemonicToEntropy(mnem, wordlist)
|
|
1843
|
-
* // Produces
|
|
2263
|
+
* const entropy = mnemonicToEntropy(mnem, wordlist);
|
|
2264
|
+
* // Produces the original 16-byte entropy payload.
|
|
1844
2265
|
* new Uint8Array([
|
|
1845
2266
|
* 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
|
|
1846
2267
|
* 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f
|
|
1847
2268
|
* ])
|
|
2269
|
+
* ```
|
|
1848
2270
|
*/
|
|
1849
2271
|
function mnemonicToEntropy(mnemonic, wordlist) {
|
|
1850
2272
|
const { words } = normalize(mnemonic);
|
|
@@ -1854,6 +2276,20 @@ function mnemonicToEntropy(mnemonic, wordlist) {
|
|
|
1854
2276
|
}
|
|
1855
2277
|
/**
|
|
1856
2278
|
* Validates mnemonic for being 12-24 words contained in `wordlist`.
|
|
2279
|
+
* @param mnemonic - 12-24 words.
|
|
2280
|
+
* @param wordlist - Imported wordlist for a specific language.
|
|
2281
|
+
* @returns `true` when mnemonic checksum and words are valid.
|
|
2282
|
+
* @example
|
|
2283
|
+
* Validate one English mnemonic.
|
|
2284
|
+
* ```ts
|
|
2285
|
+
* import { validateMnemonic } from '@scure/bip39';
|
|
2286
|
+
* import { wordlist } from '@scure/bip39/wordlists/english.js';
|
|
2287
|
+
* const ok = validateMnemonic(
|
|
2288
|
+
* 'legal winner thank year wave sausage worth useful legal winner thank yellow',
|
|
2289
|
+
* wordlist
|
|
2290
|
+
* );
|
|
2291
|
+
* // => true
|
|
2292
|
+
* ```
|
|
1857
2293
|
*/
|
|
1858
2294
|
function validateMnemonic(mnemonic, wordlist) {
|
|
1859
2295
|
try {
|
|
@@ -1864,22 +2300,32 @@ function validateMnemonic(mnemonic, wordlist) {
|
|
|
1864
2300
|
}
|
|
1865
2301
|
return true;
|
|
1866
2302
|
}
|
|
2303
|
+
// BIP-39 salts PBKDF2 with the UTF-8 NFKD string "mnemonic" + passphrase.
|
|
1867
2304
|
const psalt = (passphrase) => nfkd('mnemonic' + passphrase);
|
|
1868
2305
|
/**
|
|
1869
2306
|
* Irreversible: Uses KDF to derive 64 bytes of key data from mnemonic + optional password.
|
|
1870
|
-
* @param mnemonic 12-24 words
|
|
1871
|
-
* @param passphrase
|
|
1872
|
-
* @returns 64 bytes of key data
|
|
2307
|
+
* @param mnemonic - 12-24 words.
|
|
2308
|
+
* @param passphrase - String that will additionally protect the key.
|
|
2309
|
+
* @returns 64 bytes of key data.
|
|
2310
|
+
* @throws If the mnemonic shape is invalid. {@link Error}
|
|
2311
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
1873
2312
|
* @example
|
|
2313
|
+
* Derive a seed from a mnemonic with the sync PBKDF2 helper.
|
|
2314
|
+
* ```ts
|
|
1874
2315
|
* const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
|
|
1875
|
-
* mnemonicToSeedSync(mnem, 'password');
|
|
1876
|
-
* // new Uint8Array([...64 bytes])
|
|
2316
|
+
* const seed = mnemonicToSeedSync(mnem, 'password');
|
|
2317
|
+
* // => new Uint8Array([...64 bytes])
|
|
2318
|
+
* ```
|
|
1877
2319
|
*/
|
|
1878
2320
|
function mnemonicToSeedSync(mnemonic, passphrase = '') {
|
|
1879
|
-
return pbkdf2(sha512, normalize(mnemonic).nfkd, psalt(passphrase), {
|
|
2321
|
+
return pbkdf2(sha512, normalize(mnemonic).nfkd, psalt(passphrase), {
|
|
2322
|
+
c: 2048,
|
|
2323
|
+
dkLen: 64,
|
|
2324
|
+
});
|
|
1880
2325
|
}
|
|
1881
2326
|
|
|
1882
|
-
|
|
2327
|
+
/** English BIP39 wordlist. */
|
|
2328
|
+
const wordlist = /* @__PURE__ */ Object.freeze(`abandon
|
|
1883
2329
|
ability
|
|
1884
2330
|
able
|
|
1885
2331
|
about
|
|
@@ -3926,7 +4372,7 @@ youth
|
|
|
3926
4372
|
zebra
|
|
3927
4373
|
zero
|
|
3928
4374
|
zone
|
|
3929
|
-
zoo`.split('\n');
|
|
4375
|
+
zoo`.split('\n'));
|
|
3930
4376
|
|
|
3931
4377
|
const Hard = 0x80000000;
|
|
3932
4378
|
|
|
@@ -4504,8 +4950,8 @@ function publicKeyFromString(src) {
|
|
|
4504
4950
|
|
|
4505
4951
|
// IMPORTANT: THIS FILE IS AUTO GENERATED! DO NOT MANUALLY EDIT!
|
|
4506
4952
|
const VERSION = {
|
|
4507
|
-
"commitHash": "
|
|
4508
|
-
"version": "
|
|
4953
|
+
"commitHash": "9851c9b7e8387a82f8ff0aa6a34277a9108bb68c",
|
|
4954
|
+
"version": "25.0.0-beta.1"
|
|
4509
4955
|
};
|
|
4510
4956
|
|
|
4511
4957
|
export { ecdsa as ECDSA, ed25519 as Ed25519, Hard, InMemorySigner, InvalidPassphraseError, Path, VERSION, generateSecretKey, publicKeyFromString };
|