@taquito/signer 25.1.0 → 25.2.0-beta0
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/README.md +42 -0
- package/dist/lib/bls-key.js +17 -4
- package/dist/lib/derivation-tools/ecdsa.js +12 -9
- package/dist/lib/derivation-tools/ed25519.js +2 -2
- package/dist/lib/derivation-tools/index.js +24 -2
- package/dist/lib/ec-key.js +10 -12
- package/dist/lib/ed-key.js +3 -3
- package/dist/lib/in-memory-signer.js +40 -13
- package/dist/lib/mnemonic.js +112 -0
- package/dist/lib/taquito-signer.js +10 -1
- package/dist/lib/version.js +2 -2
- package/dist/taquito-signer.es6.js +702 -611
- package/dist/taquito-signer.es6.js.map +1 -1
- package/dist/taquito-signer.umd.js +833 -736
- package/dist/taquito-signer.umd.js.map +1 -1
- package/dist/types/bls-key.d.ts +1 -1
- package/dist/types/in-memory-signer.d.ts +4 -9
- package/dist/types/key-interface.d.ts +2 -2
- package/dist/types/mnemonic.d.ts +39 -0
- package/dist/types/taquito-signer.d.ts +5 -1
- package/package.json +13 -17
- package/dist/types/node_modules/@scure/base/index.d.ts +0 -555
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@stablelib/nacl'), require('@taquito/utils'), require('@noble/curves/ed25519'), require('@noble/curves/secp256k1'), require('@noble/curves/nist.js'), require('@taquito/core'), require('
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports', '@stablelib/nacl', '@taquito/utils', '@noble/curves/ed25519', '@noble/curves/secp256k1', '@noble/curves/nist.js', '@taquito/core', '
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.taquitoSigner = {}, global.nacl, global.utils, global.ed25519, global.secp256k1, global.nist, global.core, global.
|
|
5
|
-
})(this, (function (exports, nacl, utils
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@stablelib/nacl'), require('@taquito/utils'), require('@noble/curves/ed25519.js'), require('@noble/curves/secp256k1.js'), require('@noble/curves/nist.js'), require('@taquito/core'), require('bn.js'), require('@noble/curves/bls12-381.js')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', '@stablelib/nacl', '@taquito/utils', '@noble/curves/ed25519.js', '@noble/curves/secp256k1.js', '@noble/curves/nist.js', '@taquito/core', 'bn.js', '@noble/curves/bls12-381.js'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.taquitoSigner = {}, global.nacl, global.utils, global.ed25519, global.secp256k1, global.nist, global.core, global.BN, global.bls12381));
|
|
5
|
+
})(this, (function (exports, nacl, utils, ed25519_js, secp256k1_js, nist_js, core, BN, bls12381_js) { 'use strict';
|
|
6
6
|
|
|
7
7
|
/******************************************************************************
|
|
8
8
|
Copyright (c) Microsoft Corporation.
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
* isBytes(new Uint8Array([1, 2, 3]));
|
|
50
50
|
* ```
|
|
51
51
|
*/
|
|
52
|
-
function isBytes$
|
|
52
|
+
function isBytes$1(a) {
|
|
53
53
|
// Plain `instanceof Uint8Array` is too strict for some Buffer / proxy / cross-realm cases.
|
|
54
54
|
// The fallback still requires a real ArrayBuffer view, so plain
|
|
55
55
|
// JSON-deserialized `{ constructor: ... }` spoofing is rejected, and
|
|
@@ -60,10 +60,14 @@
|
|
|
60
60
|
'BYTES_PER_ELEMENT' in a &&
|
|
61
61
|
a.BYTES_PER_ELEMENT === 1));
|
|
62
62
|
}
|
|
63
|
+
// Shared error-message prefix builder. Only called on throw paths, so assert
|
|
64
|
+
// success paths never pay for the string concatenation.
|
|
65
|
+
const atitle$2 = (title) => (title ? `"${title}" ` : '');
|
|
63
66
|
/**
|
|
64
67
|
* Asserts something is a non-negative integer.
|
|
65
68
|
* @param n - number to validate
|
|
66
69
|
* @param title - label included in thrown errors
|
|
70
|
+
* @returns The validated number.
|
|
67
71
|
* @throws On wrong argument types. {@link TypeError}
|
|
68
72
|
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
69
73
|
* @example
|
|
@@ -72,15 +76,12 @@
|
|
|
72
76
|
* anumber(32, 'length');
|
|
73
77
|
* ```
|
|
74
78
|
*/
|
|
75
|
-
function anumber$
|
|
76
|
-
if (typeof n !== 'number')
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
const prefix = title && `"${title}" `;
|
|
82
|
-
throw new RangeError(`${prefix}expected integer >= 0, got ${n}`);
|
|
83
|
-
}
|
|
79
|
+
function anumber$1(n, title = '') {
|
|
80
|
+
if (typeof n !== 'number')
|
|
81
|
+
throw new TypeError(atitle$2(title) + 'expected number, got ' + typeof n);
|
|
82
|
+
if (!Number.isSafeInteger(n) || n < 0)
|
|
83
|
+
throw new RangeError(atitle$2(title) + 'expected integer >= 0, got ' + n);
|
|
84
|
+
return n;
|
|
84
85
|
}
|
|
85
86
|
/**
|
|
86
87
|
* Asserts something is Uint8Array.
|
|
@@ -97,19 +98,37 @@
|
|
|
97
98
|
* ```
|
|
98
99
|
*/
|
|
99
100
|
function abytes$1(value, length, title = '') {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
101
|
+
// Success path first: this runs at the start of every update() / digestInto(), and the
|
|
102
|
+
// common `abytes(data)` form must not pay for length handling it does not use.
|
|
103
|
+
if (isBytes$1(value) && (length === undefined || value.length === length))
|
|
104
|
+
return value;
|
|
105
|
+
// Error path: recompute freely to build the exact message.
|
|
106
|
+
if (length !== undefined)
|
|
107
|
+
anumber$1(length, 'length');
|
|
108
|
+
const bytes = isBytes$1(value);
|
|
109
|
+
const ofLen = length !== undefined ? ` of length ${length}` : '';
|
|
110
|
+
const got = bytes ? `length=${value.length}` : `type=${typeof value}`;
|
|
111
|
+
const message = atitle$2(title) + 'expected Uint8Array' + ofLen + ', got ' + got;
|
|
112
|
+
if (!bytes)
|
|
113
|
+
throw new TypeError(message);
|
|
114
|
+
throw new RangeError(message);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Copies bytes into a fresh Uint8Array.
|
|
118
|
+
* Buffer-style slices can alias the same backing store, so callers that need ownership should copy.
|
|
119
|
+
* @param bytes - source bytes to clone
|
|
120
|
+
* @returns Freshly allocated copy of `bytes`.
|
|
121
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
122
|
+
* @example
|
|
123
|
+
* Clone a byte array before mutating it.
|
|
124
|
+
* ```ts
|
|
125
|
+
* const copy = copyBytes(new Uint8Array([1, 2, 3]));
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
function copyBytes(bytes) {
|
|
129
|
+
// `Uint8Array.from(...)` would also accept arrays / other typed arrays. Keep this helper strict
|
|
130
|
+
// because callers use it at byte-validation boundaries before mutating the detached copy.
|
|
131
|
+
return Uint8Array.from(abytes$1(bytes));
|
|
113
132
|
}
|
|
114
133
|
/**
|
|
115
134
|
* Asserts something is a wrapped hash constructor.
|
|
@@ -127,16 +146,28 @@
|
|
|
127
146
|
*/
|
|
128
147
|
function ahash$1(h) {
|
|
129
148
|
if (typeof h !== 'function' || typeof h.create !== 'function')
|
|
130
|
-
throw new TypeError('
|
|
131
|
-
anumber$
|
|
132
|
-
anumber$
|
|
149
|
+
throw new TypeError('expected hash wrapped by utils.createHasher');
|
|
150
|
+
anumber$1(h.outputLen);
|
|
151
|
+
anumber$1(h.blockLen);
|
|
133
152
|
// HMAC and KDF callers treat these as real byte lengths; allowing zero lets fake wrappers pass
|
|
134
153
|
// validation and can produce empty outputs instead of failing fast.
|
|
135
|
-
if (h.outputLen < 1)
|
|
136
|
-
throw new Error('
|
|
137
|
-
if (h.blockLen < 1)
|
|
138
|
-
throw new Error('"blockLen" must be >= 1');
|
|
154
|
+
if (h.outputLen < 1 || h.blockLen < 1)
|
|
155
|
+
throw new Error('hash blockLen / outputLen must be >= 1');
|
|
139
156
|
}
|
|
157
|
+
const aobject$3 = (value, label) => {
|
|
158
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value))
|
|
159
|
+
throw new TypeError((label === 'object' ? '' : `"${label}" `) + 'expected object, got type=' + typeof value);
|
|
160
|
+
};
|
|
161
|
+
const aopts$1 = (value, label) => {
|
|
162
|
+
aobject$3(value, label);
|
|
163
|
+
const proto = Object.getPrototypeOf(value);
|
|
164
|
+
if (proto !== Object.prototype && proto !== null)
|
|
165
|
+
throw new TypeError(`"${label}" expected plain object`);
|
|
166
|
+
// Object.assign() treats an own "__proto__" source key as a write to the target's legacy
|
|
167
|
+
// prototype setter. Reject it before merging so inherited option values cannot be injected.
|
|
168
|
+
if (Object.hasOwn(value, '__proto__'))
|
|
169
|
+
throw new TypeError(`"${label}.__proto__" is not allowed`);
|
|
170
|
+
};
|
|
140
171
|
/**
|
|
141
172
|
* Asserts a hash instance has not been destroyed or finished.
|
|
142
173
|
* @param instance - hash instance to validate
|
|
@@ -152,10 +183,12 @@
|
|
|
152
183
|
* ```
|
|
153
184
|
*/
|
|
154
185
|
function aexists$1(instance, checkFinished = true) {
|
|
186
|
+
// Runs on every update()/digestInto(); the flags are library-owned booleans, so only their
|
|
187
|
+
// truthiness is checked - re-validating their type per call was pure hot-path overhead.
|
|
155
188
|
if (instance.destroyed)
|
|
156
|
-
throw new Error('
|
|
189
|
+
throw new Error('hash was destroyed');
|
|
157
190
|
if (checkFinished && instance.finished)
|
|
158
|
-
throw new Error('
|
|
191
|
+
throw new Error('digest() was already called');
|
|
159
192
|
}
|
|
160
193
|
/**
|
|
161
194
|
* Asserts output is a sufficiently-sized byte array.
|
|
@@ -174,10 +207,12 @@
|
|
|
174
207
|
* ```
|
|
175
208
|
*/
|
|
176
209
|
function aoutput$1(out, instance) {
|
|
177
|
-
abytes$1(out, undefined, '
|
|
210
|
+
abytes$1(out, undefined, 'output');
|
|
211
|
+
// `outputLen` is a library-owned readonly number; the negated comparison keeps failing fast
|
|
212
|
+
// when it is missing/NaN (comparisons with undefined/NaN are false) without an anumber() call.
|
|
178
213
|
const min = instance.outputLen;
|
|
179
|
-
if (out.length
|
|
180
|
-
throw new RangeError('"
|
|
214
|
+
if (!(out.length >= min)) {
|
|
215
|
+
throw new RangeError('"output" expected length >= ' + min);
|
|
181
216
|
}
|
|
182
217
|
}
|
|
183
218
|
/**
|
|
@@ -313,7 +348,15 @@
|
|
|
313
348
|
function utf8ToBytes(str) {
|
|
314
349
|
if (typeof str !== 'string')
|
|
315
350
|
throw new TypeError('string expected');
|
|
316
|
-
|
|
351
|
+
const encoded = new TextEncoder().encode(str);
|
|
352
|
+
try {
|
|
353
|
+
// Copy into the current realm for Firefox extension contexts. Callers that own the returned
|
|
354
|
+
// buffer can then wipe it independently of TextEncoder's temporary result.
|
|
355
|
+
return new Uint8Array(encoded); // https://bugzil.la/1681809
|
|
356
|
+
}
|
|
357
|
+
finally {
|
|
358
|
+
clean$1(encoded);
|
|
359
|
+
}
|
|
317
360
|
}
|
|
318
361
|
/**
|
|
319
362
|
* Helper for KDFs: consumes Uint8Array or string.
|
|
@@ -337,7 +380,8 @@
|
|
|
337
380
|
* Merges default options and passed options.
|
|
338
381
|
* @param defaults - base option object
|
|
339
382
|
* @param opts - user overrides
|
|
340
|
-
* @
|
|
383
|
+
* @param title - label included in thrown override errors
|
|
384
|
+
* @returns Fresh merged option object with a null prototype.
|
|
341
385
|
* @throws On wrong argument types. {@link TypeError}
|
|
342
386
|
* @example
|
|
343
387
|
* Merge user overrides onto default options.
|
|
@@ -345,10 +389,13 @@
|
|
|
345
389
|
* checkOpts({ dkLen: 32 }, { asyncTick: 10 });
|
|
346
390
|
* ```
|
|
347
391
|
*/
|
|
348
|
-
function checkOpts$1(defaults, opts) {
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
392
|
+
function checkOpts$1(defaults, opts, title = 'opts') {
|
|
393
|
+
aopts$1(defaults, 'defaults');
|
|
394
|
+
if (opts !== undefined)
|
|
395
|
+
aopts$1(opts, title);
|
|
396
|
+
// Callers read optional fields directly, so omitted values must not fall through to ambient
|
|
397
|
+
// Object.prototype pollution (for example a forged `dkLen` changing SHAKE's default output).
|
|
398
|
+
const merged = Object.assign(Object.create(null), defaults, opts);
|
|
352
399
|
return merged;
|
|
353
400
|
}
|
|
354
401
|
/**
|
|
@@ -359,6 +406,7 @@
|
|
|
359
406
|
* Wrapper construction eagerly calls `hashCons(undefined)` once to read
|
|
360
407
|
* `outputLen` / `blockLen`, so constructor side effects happen at module
|
|
361
408
|
* init time.
|
|
409
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
362
410
|
* @example
|
|
363
411
|
* Wrap a stateful hash constructor into a callable helper.
|
|
364
412
|
* ```ts
|
|
@@ -369,6 +417,9 @@
|
|
|
369
417
|
* ```
|
|
370
418
|
*/
|
|
371
419
|
function createHasher$1(hashCons, info = {}) {
|
|
420
|
+
if (typeof hashCons !== 'function')
|
|
421
|
+
throw new TypeError('"hashCons" expected function, got type=' + typeof hashCons);
|
|
422
|
+
info = checkOpts$1({}, info, 'info');
|
|
372
423
|
const hashC = (msg, opts) => hashCons(opts)
|
|
373
424
|
.update(msg)
|
|
374
425
|
.digest();
|
|
@@ -380,6 +431,37 @@
|
|
|
380
431
|
Object.assign(hashC, info);
|
|
381
432
|
return Object.freeze(hashC);
|
|
382
433
|
}
|
|
434
|
+
/**
|
|
435
|
+
* Cryptographically secure PRNG backed by `crypto.getRandomValues`.
|
|
436
|
+
* @param bytesLength - number of random bytes to generate
|
|
437
|
+
* @returns Random bytes.
|
|
438
|
+
* The platform `getRandomValues()` implementation still defines any
|
|
439
|
+
* single-call length cap, and this helper rejects oversize requests
|
|
440
|
+
* with a stable library `RangeError` instead of host-specific errors.
|
|
441
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
442
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
443
|
+
* @throws If the current runtime does not provide `crypto.getRandomValues`. {@link Error}
|
|
444
|
+
* @example
|
|
445
|
+
* Generate a fresh random key or nonce.
|
|
446
|
+
* ```ts
|
|
447
|
+
* const key = randomBytes(16);
|
|
448
|
+
* ```
|
|
449
|
+
*/
|
|
450
|
+
function randomBytes$2(bytesLength = 32) {
|
|
451
|
+
// Match the repo's other length-taking helpers instead of relying on Uint8Array coercion.
|
|
452
|
+
anumber$1(bytesLength, 'bytesLength');
|
|
453
|
+
const cr = typeof globalThis === 'object' ? globalThis.crypto : null;
|
|
454
|
+
if (typeof cr?.getRandomValues !== 'function')
|
|
455
|
+
throw new Error('crypto.getRandomValues must be defined');
|
|
456
|
+
// Web Cryptography API Level 2 §10.1.1:
|
|
457
|
+
// if `byteLength > 65536`, throw `QuotaExceededError`.
|
|
458
|
+
// Keep the guard explicit so callers can see the quota in code
|
|
459
|
+
// instead of discovering it by reading the spec or host errors.
|
|
460
|
+
// This wrapper surfaces the same quota as a stable library RangeError.
|
|
461
|
+
if (bytesLength > 65536)
|
|
462
|
+
throw new RangeError(`"bytesLength" expected <= 65536, got ${bytesLength}`);
|
|
463
|
+
return cr.getRandomValues(new Uint8Array(bytesLength));
|
|
464
|
+
}
|
|
383
465
|
/**
|
|
384
466
|
* Creates OID metadata for NIST hashes with prefix `06 09 60 86 48 01 65 03 04 02`.
|
|
385
467
|
* @param suffix - final OID byte for the selected hash.
|
|
@@ -402,6 +484,9 @@
|
|
|
402
484
|
* Internal helpers for blake hash.
|
|
403
485
|
* @module
|
|
404
486
|
*/
|
|
487
|
+
// Unrealized speed-up: a file-local copy of rotr measured ~1-2% faster blake2s/blake256/blake3
|
|
488
|
+
// on Node 24 (V8 does not inline it into G1s/G2s across the module boundary). Reused from
|
|
489
|
+
// utils for deduplication.
|
|
405
490
|
/**
|
|
406
491
|
* Internal blake permutation table.
|
|
407
492
|
* Rows `0..9` serve BLAKE2s, rows `0..11` serve BLAKE2b with `10..11 = 0..1`, and Blake1 also
|
|
@@ -429,6 +514,79 @@
|
|
|
429
514
|
2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9,
|
|
430
515
|
]);
|
|
431
516
|
|
|
517
|
+
const U32_MASK64$1 = /* @__PURE__ */ (() => BigInt(2 ** 32 - 1))();
|
|
518
|
+
const _32n$1 = /* @__PURE__ */ BigInt(32);
|
|
519
|
+
// Split bigint into two 32-bit halves. With `le=true`, returned fields become `{ h: low, l: high
|
|
520
|
+
// }` to match little-endian word order rather than the property names.
|
|
521
|
+
function fromBig$1(n, le = false) {
|
|
522
|
+
if (le)
|
|
523
|
+
return { h: Number(n & U32_MASK64$1), l: Number((n >> _32n$1) & U32_MASK64$1) };
|
|
524
|
+
return { h: Number((n >> _32n$1) & U32_MASK64$1) | 0, l: Number(n & U32_MASK64$1) | 0 };
|
|
525
|
+
}
|
|
526
|
+
// Split bigint list into `[highWords, lowWords]` when `le=false`; with `le=true`, the first array
|
|
527
|
+
// holds the low halves because `fromBig(...)` swaps the semantic meaning of `h` and `l`.
|
|
528
|
+
function split$1(lst, le = false) {
|
|
529
|
+
const len = lst.length;
|
|
530
|
+
let Ah = new Uint32Array(len);
|
|
531
|
+
let Al = new Uint32Array(len);
|
|
532
|
+
for (let i = 0; i < len; i++) {
|
|
533
|
+
const { h, l } = fromBig$1(lst[i], le);
|
|
534
|
+
[Ah[i], Al[i]] = [h, l];
|
|
535
|
+
}
|
|
536
|
+
return [Ah, Al];
|
|
537
|
+
}
|
|
538
|
+
// Split a JS number into u32 halves without a BigInt allocation. Exact only for integers
|
|
539
|
+
// `0 <= n < 2**53`; callers use it on byte / bit counters, which JS length math caps far below
|
|
540
|
+
// that (an ArrayBuffer cannot exceed 2**53 - 1 bytes).
|
|
541
|
+
const fromNumH = (n) => (n / 2 ** 32) | 0;
|
|
542
|
+
const fromNumL = (n) => n >>> 0;
|
|
543
|
+
// Drop-in replacement for `view.setBigUint64(byteOffset, BigInt(n), isLE)` without the per-call
|
|
544
|
+
// BigInt allocation. Same `n < 2**53` precondition as `fromNumH`/`fromNumL`.
|
|
545
|
+
function setU64FromNum(view, byteOffset, n, isLE) {
|
|
546
|
+
const h = fromNumH(n);
|
|
547
|
+
const l = fromNumL(n);
|
|
548
|
+
view.setUint32(byteOffset, isLE ? l : h, isLE);
|
|
549
|
+
view.setUint32(byteOffset + 4, isLE ? h : l, isLE);
|
|
550
|
+
}
|
|
551
|
+
// High 32-bit half of a 64-bit logical right shift for `s` in `0..31`.
|
|
552
|
+
const shrSH = (h, _l, s) => h >>> s;
|
|
553
|
+
// Low 32-bit half of a 64-bit logical right shift, valid for `s` in `1..31`.
|
|
554
|
+
const shrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
|
|
555
|
+
// High 32-bit half of a 64-bit right rotate, valid for `s` in `1..31`.
|
|
556
|
+
const rotrSH = (h, l, s) => (h >>> s) | (l << (32 - s));
|
|
557
|
+
// Low 32-bit half of a 64-bit right rotate, valid for `s` in `1..31`.
|
|
558
|
+
const rotrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
|
|
559
|
+
// High 32-bit half of a 64-bit right rotate, valid for `s` in `33..63`; `32` uses `rotr32*`.
|
|
560
|
+
const rotrBH = (h, l, s) => (h << (64 - s)) | (l >>> (s - 32));
|
|
561
|
+
// Low 32-bit half of a 64-bit right rotate, valid for `s` in `33..63`; `32` uses `rotr32*`.
|
|
562
|
+
const rotrBL = (h, l, s) => (h >>> (s - 32)) | (l << (64 - s));
|
|
563
|
+
// High 32-bit half of a 64-bit right rotate for `s === 32`; this is just the swapped low half.
|
|
564
|
+
const rotr32H = (_h, l) => l;
|
|
565
|
+
// Low 32-bit half of a 64-bit right rotate for `s === 32`; this is just the swapped high half.
|
|
566
|
+
const rotr32L = (h, _l) => h;
|
|
567
|
+
// 64-bit left rotates (rotl*) are not defined here: sha3.ts, their only consumer, keeps
|
|
568
|
+
// local copies so V8 inlines them into keccakP.
|
|
569
|
+
// Add two split 64-bit words and return the split `{ h, l }` sum.
|
|
570
|
+
// JS uses 32-bit signed integers for bitwise operations, so we cannot simply shift the carry out
|
|
571
|
+
// of the low sum and instead use division.
|
|
572
|
+
function add(Ah, Al, Bh, Bl) {
|
|
573
|
+
const l = (Al >>> 0) + (Bl >>> 0);
|
|
574
|
+
return { h: (Ah + Bh + ((l / 2 ** 32) | 0)) | 0, l: l | 0 };
|
|
575
|
+
}
|
|
576
|
+
// Addition with more than 2 elements
|
|
577
|
+
// Unmasked low-word accumulator for 3-way addition; pass the raw result into `add3H(...)`.
|
|
578
|
+
const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
579
|
+
// High-word finalize step for 3-way addition; `low` must be the untruncated output of `add3L(...)`.
|
|
580
|
+
const add3H = (low, Ah, Bh, Ch) => (Ah + Bh + Ch + ((low / 2 ** 32) | 0)) | 0;
|
|
581
|
+
// Unmasked low-word accumulator for 4-way addition; pass the raw result into `add4H(...)`.
|
|
582
|
+
const add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
583
|
+
// High-word finalize step for 4-way addition; `low` must be the untruncated output of `add4L(...)`.
|
|
584
|
+
const add4H = (low, Ah, Bh, Ch, Dh) => (Ah + Bh + Ch + Dh + ((low / 2 ** 32) | 0)) | 0;
|
|
585
|
+
// Unmasked low-word accumulator for 5-way addition; pass the raw result into `add5H(...)`.
|
|
586
|
+
const add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
587
|
+
// High-word finalize step for 5-way addition; `low` must be the untruncated output of `add5L(...)`.
|
|
588
|
+
const add5H = (low, Ah, Bh, Ch, Dh, Eh) => (Ah + Bh + Ch + Dh + Eh + ((low / 2 ** 32) | 0)) | 0;
|
|
589
|
+
|
|
432
590
|
/**
|
|
433
591
|
* Internal Merkle-Damgard hash utils.
|
|
434
592
|
* @module
|
|
@@ -511,6 +669,7 @@
|
|
|
511
669
|
abytes$1(data);
|
|
512
670
|
const { view, buffer, blockLen } = this;
|
|
513
671
|
const len = data.length;
|
|
672
|
+
let processed = false;
|
|
514
673
|
for (let pos = 0; pos < len;) {
|
|
515
674
|
const take = Math.min(blockLen - this.pos, len - pos);
|
|
516
675
|
// Fast path only when there is no buffered partial block: `take === blockLen` implies
|
|
@@ -519,18 +678,25 @@
|
|
|
519
678
|
const dataView = createView(data);
|
|
520
679
|
for (; blockLen <= len - pos; pos += blockLen)
|
|
521
680
|
this.process(dataView, pos);
|
|
681
|
+
processed = true;
|
|
522
682
|
continue;
|
|
523
683
|
}
|
|
524
|
-
|
|
684
|
+
// When the whole input is buffered in one go (common for short messages), passing `data`
|
|
685
|
+
// directly avoids allocating a subarray view.
|
|
686
|
+
buffer.set(pos === 0 && take === len ? data : data.subarray(pos, pos + take), this.pos);
|
|
525
687
|
this.pos += take;
|
|
526
688
|
pos += take;
|
|
527
689
|
if (this.pos === blockLen) {
|
|
528
690
|
this.process(view, 0);
|
|
529
691
|
this.pos = 0;
|
|
692
|
+
processed = true;
|
|
530
693
|
}
|
|
531
694
|
}
|
|
532
695
|
this.length += data.length;
|
|
533
|
-
|
|
696
|
+
// Shared schedule buffers only pick up input-derived words inside process(); if everything
|
|
697
|
+
// was buffered without processing, there is nothing to zero.
|
|
698
|
+
if (processed)
|
|
699
|
+
this.roundClean();
|
|
534
700
|
return this;
|
|
535
701
|
}
|
|
536
702
|
digestInto(out) {
|
|
@@ -542,32 +708,33 @@
|
|
|
542
708
|
// was previously not allocated here. But it won't change performance.
|
|
543
709
|
const { buffer, view, blockLen, isLE } = this;
|
|
544
710
|
let { pos } = this;
|
|
545
|
-
// append the bit '1' to the message
|
|
711
|
+
// append the bit '1' to the message, then zero-pad the rest of the block
|
|
546
712
|
buffer[pos++] = 0b10000000;
|
|
547
|
-
|
|
713
|
+
buffer.fill(0, pos);
|
|
548
714
|
// we have less than padOffset left in buffer, so we cannot put length in
|
|
549
715
|
// current block, need process it and pad again
|
|
550
716
|
if (this.padOffset > blockLen - pos) {
|
|
551
717
|
this.process(view, 0);
|
|
552
|
-
|
|
718
|
+
buffer.fill(0);
|
|
553
719
|
}
|
|
554
|
-
// Pad until full block byte with zeros
|
|
555
|
-
for (let i = pos; i < blockLen; i++)
|
|
556
|
-
buffer[i] = 0;
|
|
557
720
|
// `padOffset` reserves the whole length field. For SHA-384/512 the high 64 bits stay zero from
|
|
558
721
|
// 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
|
|
560
|
-
|
|
722
|
+
// So we only need to write the low 64 bits here (`length * 8` only scales the exponent of an
|
|
723
|
+
// integer below 2**53, so the split inside the helper stays exact).
|
|
724
|
+
setU64FromNum(view, blockLen - 8, this.length * 8, isLE);
|
|
561
725
|
this.process(view, 0);
|
|
562
|
-
|
|
726
|
+
// The final block above is processed outside update(), so the shared message-schedule
|
|
727
|
+
// buffers (e.g. SHA256_W) would otherwise retain input-derived words after digest().
|
|
728
|
+
this.roundClean();
|
|
729
|
+
// digest() passes our own `buffer` as `out`; reuse its cached view instead of allocating one.
|
|
730
|
+
const oview = out === buffer ? view : createView(out);
|
|
563
731
|
const len = this.outputLen;
|
|
564
732
|
// NOTE: we do division by 4 later, which must be fused in single op with modulo by JIT
|
|
565
|
-
if (len % 4)
|
|
566
|
-
throw new Error('_sha2: outputLen must be aligned to 32bit');
|
|
567
733
|
const outLen = len / 4;
|
|
568
734
|
const state = this.get();
|
|
569
|
-
|
|
570
|
-
|
|
735
|
+
// Subclass-misconfiguration invariant: outputLen must be 32-bit aligned and fit the state.
|
|
736
|
+
if (len % 4 || outLen > state.length)
|
|
737
|
+
throw new Error('invalid outputLen');
|
|
571
738
|
for (let i = 0; i < outLen; i++)
|
|
572
739
|
oview.setUint32(4 * i, state[i], isLE);
|
|
573
740
|
}
|
|
@@ -580,18 +747,16 @@
|
|
|
580
747
|
this.destroy();
|
|
581
748
|
return res;
|
|
582
749
|
}
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
to.set(...this.get());
|
|
586
|
-
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
750
|
+
_cloneIntoMeta(to) {
|
|
751
|
+
const { buffer, length, finished, destroyed, pos } = this;
|
|
587
752
|
to.destroyed = destroyed;
|
|
588
753
|
to.finished = finished;
|
|
589
754
|
to.length = length;
|
|
590
755
|
to.pos = pos;
|
|
591
756
|
// Only partial-block bytes need copying: when `length % blockLen === 0`, `pos === 0` and
|
|
592
757
|
// later `update()` / `digestInto()` overwrite `to.buffer` from the start before reading it.
|
|
593
|
-
if (
|
|
594
|
-
to.buffer.set(buffer);
|
|
758
|
+
if (pos)
|
|
759
|
+
to.buffer.set(buffer); // Avoid a hot modulo guard.
|
|
595
760
|
return to;
|
|
596
761
|
}
|
|
597
762
|
clone() {
|
|
@@ -617,64 +782,6 @@
|
|
|
617
782
|
0x510e527f, 0xade682d1, 0x9b05688c, 0x2b3e6c1f, 0x1f83d9ab, 0xfb41bd6b, 0x5be0cd19, 0x137e2179,
|
|
618
783
|
]);
|
|
619
784
|
|
|
620
|
-
const U32_MASK64$1 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
|
|
621
|
-
const _32n$1 = /* @__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.
|
|
624
|
-
function fromBig$1(n, le = false) {
|
|
625
|
-
if (le)
|
|
626
|
-
return { h: Number(n & U32_MASK64$1), l: Number((n >> _32n$1) & U32_MASK64$1) };
|
|
627
|
-
return { h: Number((n >> _32n$1) & U32_MASK64$1) | 0, l: Number(n & U32_MASK64$1) | 0 };
|
|
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`.
|
|
631
|
-
function split$1(lst, le = false) {
|
|
632
|
-
const len = lst.length;
|
|
633
|
-
let Ah = new Uint32Array(len);
|
|
634
|
-
let Al = new Uint32Array(len);
|
|
635
|
-
for (let i = 0; i < len; i++) {
|
|
636
|
-
const { h, l } = fromBig$1(lst[i], le);
|
|
637
|
-
[Ah[i], Al[i]] = [h, l];
|
|
638
|
-
}
|
|
639
|
-
return [Ah, Al];
|
|
640
|
-
}
|
|
641
|
-
// High 32-bit half of a 64-bit logical right shift for `s` in `0..31`.
|
|
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`.
|
|
644
|
-
const shrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
|
|
645
|
-
// High 32-bit half of a 64-bit right rotate, valid for `s` in `1..31`.
|
|
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`.
|
|
648
|
-
const rotrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
|
|
649
|
-
// High 32-bit half of a 64-bit right rotate, valid for `s` in `33..63`; `32` uses `rotr32*`.
|
|
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*`.
|
|
652
|
-
const rotrBL = (h, l, s) => (h >>> (s - 32)) | (l << (64 - s));
|
|
653
|
-
// High 32-bit half of a 64-bit right rotate for `s === 32`; this is just the swapped low half.
|
|
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.
|
|
656
|
-
const rotr32L = (h, _l) => h;
|
|
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.
|
|
660
|
-
function add(Ah, Al, Bh, Bl) {
|
|
661
|
-
const l = (Al >>> 0) + (Bl >>> 0);
|
|
662
|
-
return { h: (Ah + Bh + ((l / 2 ** 32) | 0)) | 0, l: l | 0 };
|
|
663
|
-
}
|
|
664
|
-
// Addition with more than 2 elements
|
|
665
|
-
// Unmasked low-word accumulator for 3-way addition; pass the raw result into `add3H(...)`.
|
|
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(...)`.
|
|
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(...)`.
|
|
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(...)`.
|
|
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(...)`.
|
|
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(...)`.
|
|
676
|
-
const add5H = (low, Ah, Bh, Ch, Dh, Eh) => (Ah + Bh + Ch + Dh + Eh + ((low / 2 ** 32) | 0)) | 0;
|
|
677
|
-
|
|
678
785
|
/**
|
|
679
786
|
* blake2b (64-bit) & blake2s (8 to 32-bit) hash functions.
|
|
680
787
|
* b could have been faster, but there is no fast u64 in js, so s is 1.5x faster.
|
|
@@ -697,21 +804,28 @@
|
|
|
697
804
|
let Cl = BBUF[2 * c], Ch = BBUF[2 * c + 1]; // prettier-ignore
|
|
698
805
|
let Dl = BBUF[2 * d], Dh = BBUF[2 * d + 1]; // prettier-ignore
|
|
699
806
|
// v[a] = (v[a] + v[b] + x) | 0;
|
|
700
|
-
|
|
807
|
+
const ll = add3L(Al, Bl, Xl);
|
|
701
808
|
Ah = add3H(ll, Ah, Bh, Xh);
|
|
702
809
|
Al = ll | 0;
|
|
703
810
|
// v[d] = rotr(v[d] ^ v[a], 32)
|
|
704
|
-
|
|
705
|
-
|
|
811
|
+
let xh = Dh ^ Ah, xl = Dl ^ Al; // prettier-ignore
|
|
812
|
+
Dh = rotr32H(xh, xl);
|
|
813
|
+
Dl = rotr32L(xh);
|
|
706
814
|
// v[c] = (v[c] + v[d]) | 0;
|
|
707
815
|
({ h: Ch, l: Cl } = add(Ch, Cl, Dh, Dl));
|
|
708
816
|
// v[b] = rotr(v[b] ^ v[c], 24)
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
817
|
+
xh = Bh ^ Ch;
|
|
818
|
+
xl = Bl ^ Cl;
|
|
819
|
+
Bh = rotrSH(xh, xl, 24);
|
|
820
|
+
Bl = rotrSL(xh, xl, 24);
|
|
821
|
+
BBUF[2 * a] = Al;
|
|
822
|
+
BBUF[2 * a + 1] = Ah;
|
|
823
|
+
BBUF[2 * b] = Bl;
|
|
824
|
+
BBUF[2 * b + 1] = Bh;
|
|
825
|
+
BBUF[2 * c] = Cl;
|
|
826
|
+
BBUF[2 * c + 1] = Ch;
|
|
827
|
+
BBUF[2 * d] = Dl;
|
|
828
|
+
BBUF[2 * d + 1] = Dh;
|
|
715
829
|
}
|
|
716
830
|
// Second half-round of the same LE-limb BLAKE2b G mix; `x` is the message word offset.
|
|
717
831
|
function G2b(a, b, c, d, msg, x) {
|
|
@@ -722,27 +836,35 @@
|
|
|
722
836
|
let Cl = BBUF[2 * c], Ch = BBUF[2 * c + 1]; // prettier-ignore
|
|
723
837
|
let Dl = BBUF[2 * d], Dh = BBUF[2 * d + 1]; // prettier-ignore
|
|
724
838
|
// v[a] = (v[a] + v[b] + x) | 0;
|
|
725
|
-
|
|
839
|
+
const ll = add3L(Al, Bl, Xl);
|
|
726
840
|
Ah = add3H(ll, Ah, Bh, Xh);
|
|
727
841
|
Al = ll | 0;
|
|
728
842
|
// v[d] = rotr(v[d] ^ v[a], 16)
|
|
729
|
-
|
|
730
|
-
|
|
843
|
+
let xh = Dh ^ Ah, xl = Dl ^ Al; // prettier-ignore
|
|
844
|
+
Dh = rotrSH(xh, xl, 16);
|
|
845
|
+
Dl = rotrSL(xh, xl, 16);
|
|
731
846
|
// v[c] = (v[c] + v[d]) | 0;
|
|
732
847
|
({ h: Ch, l: Cl } = add(Ch, Cl, Dh, Dl));
|
|
733
848
|
// v[b] = rotr(v[b] ^ v[c], 63)
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
849
|
+
xh = Bh ^ Ch;
|
|
850
|
+
xl = Bl ^ Cl;
|
|
851
|
+
Bh = rotrBH(xh, xl, 63);
|
|
852
|
+
Bl = rotrBL(xh, xl, 63);
|
|
853
|
+
BBUF[2 * a] = Al;
|
|
854
|
+
BBUF[2 * a + 1] = Ah;
|
|
855
|
+
BBUF[2 * b] = Bl;
|
|
856
|
+
BBUF[2 * b + 1] = Bh;
|
|
857
|
+
BBUF[2 * c] = Cl;
|
|
858
|
+
BBUF[2 * c + 1] = Ch;
|
|
859
|
+
BBUF[2 * d] = Dl;
|
|
860
|
+
BBUF[2 * d + 1] = Dh;
|
|
740
861
|
}
|
|
741
862
|
function checkBlake2Opts(outputLen, opts = {}, keyLen, saltLen, persLen) {
|
|
742
|
-
anumber$
|
|
743
|
-
// RFC 7693 §2.1 requires digest length nn in 1..keyLen
|
|
863
|
+
anumber$1(keyLen);
|
|
864
|
+
// RFC 7693 §2.1 requires digest length nn in 1..keyLen (keyLen doubles as
|
|
865
|
+
// the per-variant max for both key and digest lengths: 64 for b, 32 for s).
|
|
744
866
|
if (outputLen <= 0 || outputLen > keyLen)
|
|
745
|
-
throw new Error('
|
|
867
|
+
throw new Error('"dkLen" must be 1..' + keyLen + ', got ' + outputLen);
|
|
746
868
|
const { key, salt, personalization } = opts;
|
|
747
869
|
// This API uses `undefined` for the RFC 7693 `kk = 0` case, so a provided key must be non-empty.
|
|
748
870
|
if (key !== undefined && (key.length < 1 || key.length > keyLen))
|
|
@@ -764,8 +886,8 @@
|
|
|
764
886
|
outputLen;
|
|
765
887
|
canXOF = false;
|
|
766
888
|
constructor(blockLen, outputLen) {
|
|
767
|
-
anumber$
|
|
768
|
-
anumber$
|
|
889
|
+
anumber$1(blockLen);
|
|
890
|
+
anumber$1(outputLen);
|
|
769
891
|
this.blockLen = blockLen;
|
|
770
892
|
this.outputLen = outputLen;
|
|
771
893
|
this.buffer = new Uint8Array(blockLen);
|
|
@@ -803,7 +925,9 @@
|
|
|
803
925
|
swap32IfBE$1(data32);
|
|
804
926
|
continue;
|
|
805
927
|
}
|
|
806
|
-
|
|
928
|
+
// When the whole input is buffered in one go (common for short messages), passing `data`
|
|
929
|
+
// directly avoids allocating a subarray view.
|
|
930
|
+
buffer.set(pos === 0 && take === len ? data : data.subarray(pos, pos + take), this.pos);
|
|
807
931
|
this.pos += take;
|
|
808
932
|
this.length += take;
|
|
809
933
|
pos += take;
|
|
@@ -813,18 +937,19 @@
|
|
|
813
937
|
digestInto(out) {
|
|
814
938
|
aexists$1(this);
|
|
815
939
|
aoutput$1(out, this);
|
|
940
|
+
// Reject unaligned views explicitly instead of hiding them behind a full scratch copy.
|
|
941
|
+
if (out.byteOffset & 3)
|
|
942
|
+
throw new RangeError('"output" expected 4-byte aligned byteOffset, got ' + out.byteOffset);
|
|
816
943
|
const { pos, buffer32 } = this;
|
|
817
944
|
this.finished = true;
|
|
818
945
|
// Padding
|
|
819
|
-
|
|
946
|
+
this.buffer.fill(0, pos);
|
|
820
947
|
swap32IfBE$1(buffer32);
|
|
821
948
|
this.compress(buffer32, 0, true);
|
|
822
949
|
swap32IfBE$1(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
950
|
const state = this.get();
|
|
827
|
-
|
|
951
|
+
// digest() passes our own `buffer` as `out`; reuse its cached u32 view instead of allocating.
|
|
952
|
+
const out32 = out === this.buffer ? buffer32 : u32$1(out);
|
|
828
953
|
const full = Math.floor(this.outputLen / 4);
|
|
829
954
|
for (let i = 0; i < full; i++)
|
|
830
955
|
out32[i] = swap8IfBE(state[i]);
|
|
@@ -849,6 +974,7 @@
|
|
|
849
974
|
// Recreate only `dkLen`; key/salt/personalization are already absorbed into the copied state.
|
|
850
975
|
to ||= new this.constructor({ dkLen: outputLen });
|
|
851
976
|
to.set(...this.get());
|
|
977
|
+
// Last-block-aware lazy compression keeps the pending block live even when full.
|
|
852
978
|
to.buffer.set(buffer);
|
|
853
979
|
to.destroyed = destroyed;
|
|
854
980
|
to.finished = finished;
|
|
@@ -882,6 +1008,7 @@
|
|
|
882
1008
|
v7l = B2B_IV[14] | 0;
|
|
883
1009
|
v7h = B2B_IV[15] | 0;
|
|
884
1010
|
constructor(opts = {}) {
|
|
1011
|
+
opts = checkOpts$1({}, opts);
|
|
885
1012
|
const olen = opts.dkLen === undefined ? 64 : opts.dkLen;
|
|
886
1013
|
super(128, olen);
|
|
887
1014
|
checkBlake2Opts(olen, opts, 64, 16, 16);
|
|
@@ -896,7 +1023,8 @@
|
|
|
896
1023
|
this.v0l ^= this.outputLen | (keyLength << 8) | (0x01 << 16) | (0x01 << 24);
|
|
897
1024
|
if (salt !== undefined) {
|
|
898
1025
|
abytes$1(salt, undefined, 'salt');
|
|
899
|
-
|
|
1026
|
+
// Copy: u32() would throw on views with byteOffset not divisible by 4.
|
|
1027
|
+
const slt = u32$1(copyBytes(salt));
|
|
900
1028
|
this.v4l ^= swap8IfBE(slt[0]);
|
|
901
1029
|
this.v4h ^= swap8IfBE(slt[1]);
|
|
902
1030
|
this.v5l ^= swap8IfBE(slt[2]);
|
|
@@ -904,7 +1032,8 @@
|
|
|
904
1032
|
}
|
|
905
1033
|
if (personalization !== undefined) {
|
|
906
1034
|
abytes$1(personalization, undefined, 'personalization');
|
|
907
|
-
|
|
1035
|
+
// Copy: u32() would throw on views with byteOffset not divisible by 4.
|
|
1036
|
+
const pers = u32$1(copyBytes(personalization));
|
|
908
1037
|
this.v6l ^= swap8IfBE(pers[0]);
|
|
909
1038
|
this.v6h ^= swap8IfBE(pers[1]);
|
|
910
1039
|
this.v7l ^= swap8IfBE(pers[2]);
|
|
@@ -915,6 +1044,8 @@
|
|
|
915
1044
|
const tmp = new Uint8Array(this.blockLen);
|
|
916
1045
|
tmp.set(key);
|
|
917
1046
|
this.update(tmp);
|
|
1047
|
+
// The padded copy holds key material; buffer/state keep what they need.
|
|
1048
|
+
clean$1(tmp);
|
|
918
1049
|
}
|
|
919
1050
|
}
|
|
920
1051
|
// prettier-ignore
|
|
@@ -942,9 +1073,32 @@
|
|
|
942
1073
|
this.v7h = v7h | 0;
|
|
943
1074
|
}
|
|
944
1075
|
compress(msg, offset, isLast) {
|
|
945
|
-
|
|
1076
|
+
// First half from state. Direct writes: get() would allocate an array +
|
|
1077
|
+
// closure per block.
|
|
1078
|
+
// prettier-ignore
|
|
1079
|
+
const { v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h } = this;
|
|
1080
|
+
// prettier-ignore
|
|
1081
|
+
{
|
|
1082
|
+
BBUF[0] = v0l;
|
|
1083
|
+
BBUF[1] = v0h;
|
|
1084
|
+
BBUF[2] = v1l;
|
|
1085
|
+
BBUF[3] = v1h;
|
|
1086
|
+
BBUF[4] = v2l;
|
|
1087
|
+
BBUF[5] = v2h;
|
|
1088
|
+
BBUF[6] = v3l;
|
|
1089
|
+
BBUF[7] = v3h;
|
|
1090
|
+
BBUF[8] = v4l;
|
|
1091
|
+
BBUF[9] = v4h;
|
|
1092
|
+
BBUF[10] = v5l;
|
|
1093
|
+
BBUF[11] = v5h;
|
|
1094
|
+
BBUF[12] = v6l;
|
|
1095
|
+
BBUF[13] = v6h;
|
|
1096
|
+
BBUF[14] = v7l;
|
|
1097
|
+
BBUF[15] = v7h;
|
|
1098
|
+
}
|
|
946
1099
|
BBUF.set(B2B_IV, 16); // Second half from IV.
|
|
947
|
-
|
|
1100
|
+
const l = fromNumL(this.length);
|
|
1101
|
+
const h = fromNumH(this.length);
|
|
948
1102
|
BBUF[24] = B2B_IV[8] ^ l; // Low word of the offset.
|
|
949
1103
|
BBUF[25] = B2B_IV[9] ^ h; // High word.
|
|
950
1104
|
// Invert all bits for last block
|
|
@@ -1010,12 +1164,22 @@
|
|
|
1010
1164
|
* ```ts
|
|
1011
1165
|
* blake2b(new Uint8Array([97, 98, 99]));
|
|
1012
1166
|
* ```
|
|
1167
|
+
* @example
|
|
1168
|
+
* Hash a message with Blake2b while selecting output, MAC, salt, and personalization settings.
|
|
1169
|
+
* ```ts
|
|
1170
|
+
* blake2b(new Uint8Array([97, 98, 99]), {
|
|
1171
|
+
* dkLen: 32,
|
|
1172
|
+
* key: new Uint8Array(32),
|
|
1173
|
+
* salt: new Uint8Array(16),
|
|
1174
|
+
* personalization: new Uint8Array(16),
|
|
1175
|
+
* });
|
|
1176
|
+
* ```
|
|
1013
1177
|
*/
|
|
1014
1178
|
const blake2b = /* @__PURE__ */ createHasher$1((opts) => new _BLAKE2b(opts));
|
|
1015
1179
|
|
|
1016
1180
|
var _EdKey_keyPair, _EdPublicKey_key;
|
|
1017
1181
|
function generateKeyPairFromSeed$1(seed) {
|
|
1018
|
-
const publicKey =
|
|
1182
|
+
const publicKey = ed25519_js.ed25519.getPublicKey(seed);
|
|
1019
1183
|
const secretKey = new Uint8Array(64);
|
|
1020
1184
|
secretKey.set(seed);
|
|
1021
1185
|
secretKey.set(publicKey, 32);
|
|
@@ -1034,21 +1198,21 @@
|
|
|
1034
1198
|
*/
|
|
1035
1199
|
constructor(key, decrypt) {
|
|
1036
1200
|
_EdKey_keyPair.set(this, void 0);
|
|
1037
|
-
const tmp = utils
|
|
1038
|
-
utils
|
|
1039
|
-
utils
|
|
1040
|
-
utils
|
|
1201
|
+
const tmp = utils.b58DecodeAndCheckPrefix(key, [
|
|
1202
|
+
utils.PrefixV2.Ed25519SecretKey,
|
|
1203
|
+
utils.PrefixV2.Ed25519EncryptedSeed,
|
|
1204
|
+
utils.PrefixV2.Ed25519Seed,
|
|
1041
1205
|
]);
|
|
1042
1206
|
let [keyData] = tmp;
|
|
1043
1207
|
const [, prefix] = tmp;
|
|
1044
|
-
if (prefix === utils
|
|
1208
|
+
if (prefix === utils.PrefixV2.Ed25519SecretKey) {
|
|
1045
1209
|
__classPrivateFieldSet(this, _EdKey_keyPair, {
|
|
1046
1210
|
secretKey: keyData,
|
|
1047
1211
|
publicKey: keyData.slice(32),
|
|
1048
1212
|
}, "f");
|
|
1049
1213
|
}
|
|
1050
1214
|
else {
|
|
1051
|
-
if (prefix === utils
|
|
1215
|
+
if (prefix === utils.PrefixV2.Ed25519EncryptedSeed) {
|
|
1052
1216
|
if (decrypt !== undefined) {
|
|
1053
1217
|
keyData = decrypt(keyData);
|
|
1054
1218
|
}
|
|
@@ -1067,11 +1231,11 @@
|
|
|
1067
1231
|
sign(bytes) {
|
|
1068
1232
|
const hash = blake2b(bytes, { dkLen: 32 });
|
|
1069
1233
|
const seed = __classPrivateFieldGet(this, _EdKey_keyPair, "f").secretKey.subarray(0, 32);
|
|
1070
|
-
const signature =
|
|
1234
|
+
const signature = ed25519_js.ed25519.sign(hash, seed);
|
|
1071
1235
|
return {
|
|
1072
1236
|
rawSignature: signature,
|
|
1073
|
-
sig: utils
|
|
1074
|
-
prefixSig: utils
|
|
1237
|
+
sig: utils.b58Encode(signature, utils.PrefixV2.GenericSignature),
|
|
1238
|
+
prefixSig: utils.b58Encode(signature, utils.PrefixV2.Ed25519Signature),
|
|
1075
1239
|
};
|
|
1076
1240
|
}
|
|
1077
1241
|
/**
|
|
@@ -1084,7 +1248,7 @@
|
|
|
1084
1248
|
* @returns Encoded private key
|
|
1085
1249
|
*/
|
|
1086
1250
|
secretKey() {
|
|
1087
|
-
return utils
|
|
1251
|
+
return utils.b58Encode(__classPrivateFieldGet(this, _EdKey_keyPair, "f").secretKey, utils.PrefixV2.Ed25519SecretKey);
|
|
1088
1252
|
}
|
|
1089
1253
|
}
|
|
1090
1254
|
_EdKey_keyPair = new WeakMap();
|
|
@@ -1092,7 +1256,7 @@
|
|
|
1092
1256
|
constructor(src) {
|
|
1093
1257
|
_EdPublicKey_key.set(this, void 0);
|
|
1094
1258
|
if (typeof src === 'string') {
|
|
1095
|
-
const [key, _] = utils
|
|
1259
|
+
const [key, _] = utils.b58DecodeAndCheckPrefix(src, [utils.PrefixV2.Ed25519PublicKey]);
|
|
1096
1260
|
__classPrivateFieldSet(this, _EdPublicKey_key, key, "f");
|
|
1097
1261
|
}
|
|
1098
1262
|
else {
|
|
@@ -1101,14 +1265,14 @@
|
|
|
1101
1265
|
}
|
|
1102
1266
|
compare(other) {
|
|
1103
1267
|
if (other instanceof EdPublicKey) {
|
|
1104
|
-
return utils
|
|
1268
|
+
return utils.compareArrays(this.bytes(), other.bytes());
|
|
1105
1269
|
}
|
|
1106
1270
|
else {
|
|
1107
|
-
throw new utils
|
|
1271
|
+
throw new utils.InvalidPublicKeyError('EdDSA key expected');
|
|
1108
1272
|
}
|
|
1109
1273
|
}
|
|
1110
1274
|
hash() {
|
|
1111
|
-
return utils
|
|
1275
|
+
return utils.b58Encode(blake2b(__classPrivateFieldGet(this, _EdPublicKey_key, "f"), { dkLen: 20 }), utils.PrefixV2.Ed25519PublicKeyHash);
|
|
1112
1276
|
}
|
|
1113
1277
|
bytes() {
|
|
1114
1278
|
return __classPrivateFieldGet(this, _EdPublicKey_key, "f");
|
|
@@ -1120,7 +1284,7 @@
|
|
|
1120
1284
|
return res;
|
|
1121
1285
|
}
|
|
1122
1286
|
toString() {
|
|
1123
|
-
return utils
|
|
1287
|
+
return utils.b58Encode(__classPrivateFieldGet(this, _EdPublicKey_key, "f"), utils.PrefixV2.Ed25519PublicKey);
|
|
1124
1288
|
}
|
|
1125
1289
|
}
|
|
1126
1290
|
_EdPublicKey_key = new WeakMap();
|
|
@@ -1196,17 +1360,17 @@
|
|
|
1196
1360
|
var _ECKey_keyPair, _ECPublicKey_key;
|
|
1197
1361
|
const pref = {
|
|
1198
1362
|
p256: {
|
|
1199
|
-
pk: utils
|
|
1200
|
-
sk: utils
|
|
1201
|
-
pkh: utils
|
|
1202
|
-
sig: utils
|
|
1363
|
+
pk: utils.PrefixV2.P256PublicKey,
|
|
1364
|
+
sk: utils.PrefixV2.P256SecretKey,
|
|
1365
|
+
pkh: utils.PrefixV2.P256PublicKeyHash,
|
|
1366
|
+
sig: utils.PrefixV2.P256Signature,
|
|
1203
1367
|
tag: 2,
|
|
1204
1368
|
},
|
|
1205
1369
|
secp256k1: {
|
|
1206
|
-
pk: utils
|
|
1207
|
-
sk: utils
|
|
1208
|
-
pkh: utils
|
|
1209
|
-
sig: utils
|
|
1370
|
+
pk: utils.PrefixV2.Secp256k1PublicKey,
|
|
1371
|
+
sk: utils.PrefixV2.Secp256k1SecretKey,
|
|
1372
|
+
pkh: utils.PrefixV2.Secp256k1PublicKeyHash,
|
|
1373
|
+
sig: utils.PrefixV2.Secp256k1Signature,
|
|
1210
1374
|
tag: 1,
|
|
1211
1375
|
},
|
|
1212
1376
|
};
|
|
@@ -1222,26 +1386,26 @@
|
|
|
1222
1386
|
*/
|
|
1223
1387
|
constructor(key, decrypt) {
|
|
1224
1388
|
_ECKey_keyPair.set(this, void 0);
|
|
1225
|
-
const [keyData, prefix] = utils
|
|
1226
|
-
utils
|
|
1227
|
-
utils
|
|
1228
|
-
utils
|
|
1229
|
-
utils
|
|
1389
|
+
const [keyData, prefix] = utils.b58DecodeAndCheckPrefix(key, [
|
|
1390
|
+
utils.PrefixV2.Secp256k1EncryptedSecretKey,
|
|
1391
|
+
utils.PrefixV2.P256EncryptedSecretKey,
|
|
1392
|
+
utils.PrefixV2.Secp256k1SecretKey,
|
|
1393
|
+
utils.PrefixV2.P256SecretKey,
|
|
1230
1394
|
]);
|
|
1231
1395
|
const [decKey, curve] = (() => {
|
|
1232
1396
|
switch (prefix) {
|
|
1233
|
-
case utils
|
|
1234
|
-
case utils
|
|
1397
|
+
case utils.PrefixV2.Secp256k1EncryptedSecretKey:
|
|
1398
|
+
case utils.PrefixV2.P256EncryptedSecretKey:
|
|
1235
1399
|
if (decrypt === undefined) {
|
|
1236
1400
|
throw new Error('decryption function is not provided');
|
|
1237
1401
|
}
|
|
1238
1402
|
else {
|
|
1239
1403
|
return [
|
|
1240
1404
|
decrypt(keyData),
|
|
1241
|
-
prefix === utils
|
|
1405
|
+
prefix === utils.PrefixV2.Secp256k1EncryptedSecretKey ? 'secp256k1' : 'p256',
|
|
1242
1406
|
];
|
|
1243
1407
|
}
|
|
1244
|
-
case utils
|
|
1408
|
+
case utils.PrefixV2.Secp256k1SecretKey:
|
|
1245
1409
|
return [keyData, 'secp256k1'];
|
|
1246
1410
|
default:
|
|
1247
1411
|
return [keyData, 'p256'];
|
|
@@ -1251,7 +1415,7 @@
|
|
|
1251
1415
|
curve,
|
|
1252
1416
|
secretKey: decKey,
|
|
1253
1417
|
publicKey: curve === 'secp256k1'
|
|
1254
|
-
?
|
|
1418
|
+
? secp256k1_js.secp256k1.getPublicKey(decKey, true)
|
|
1255
1419
|
: nist_js.p256.getPublicKey(decKey, true),
|
|
1256
1420
|
}, "f");
|
|
1257
1421
|
}
|
|
@@ -1264,23 +1428,21 @@
|
|
|
1264
1428
|
const hash = blake2b(bytes, { dkLen: 32 });
|
|
1265
1429
|
let signature;
|
|
1266
1430
|
if (__classPrivateFieldGet(this, _ECKey_keyPair, "f").curve === 'secp256k1') {
|
|
1267
|
-
signature = secp256k1.
|
|
1268
|
-
.sign(hash, __classPrivateFieldGet(this, _ECKey_keyPair, "f").secretKey, {
|
|
1431
|
+
signature = secp256k1_js.secp256k1.sign(hash, __classPrivateFieldGet(this, _ECKey_keyPair, "f").secretKey, {
|
|
1269
1432
|
lowS: true, // Use canonical signatures (prevents malleability)
|
|
1270
|
-
|
|
1271
|
-
|
|
1433
|
+
prehash: false,
|
|
1434
|
+
});
|
|
1272
1435
|
}
|
|
1273
1436
|
else {
|
|
1274
|
-
signature = nist_js.p256
|
|
1275
|
-
.sign(hash, __classPrivateFieldGet(this, _ECKey_keyPair, "f").secretKey, {
|
|
1437
|
+
signature = nist_js.p256.sign(hash, __classPrivateFieldGet(this, _ECKey_keyPair, "f").secretKey, {
|
|
1276
1438
|
lowS: true, // Use canonical signatures (prevents malleability)
|
|
1277
|
-
|
|
1278
|
-
|
|
1439
|
+
prehash: false,
|
|
1440
|
+
});
|
|
1279
1441
|
}
|
|
1280
1442
|
return {
|
|
1281
1443
|
rawSignature: signature,
|
|
1282
|
-
sig: utils
|
|
1283
|
-
prefixSig: utils
|
|
1444
|
+
sig: utils.b58Encode(signature, utils.PrefixV2.GenericSignature),
|
|
1445
|
+
prefixSig: utils.b58Encode(signature, pref[__classPrivateFieldGet(this, _ECKey_keyPair, "f").curve].sig),
|
|
1284
1446
|
};
|
|
1285
1447
|
}
|
|
1286
1448
|
/**
|
|
@@ -1293,7 +1455,7 @@
|
|
|
1293
1455
|
* @returns Encoded private key
|
|
1294
1456
|
*/
|
|
1295
1457
|
secretKey() {
|
|
1296
|
-
return utils
|
|
1458
|
+
return utils.b58Encode(__classPrivateFieldGet(this, _ECKey_keyPair, "f").secretKey, pref[__classPrivateFieldGet(this, _ECKey_keyPair, "f").curve].sk);
|
|
1297
1459
|
}
|
|
1298
1460
|
}
|
|
1299
1461
|
_ECKey_keyPair = new WeakMap();
|
|
@@ -1302,11 +1464,11 @@
|
|
|
1302
1464
|
_ECPublicKey_key.set(this, void 0);
|
|
1303
1465
|
const [key, crv] = (() => {
|
|
1304
1466
|
if (typeof src === 'string') {
|
|
1305
|
-
const [key, pre] = utils
|
|
1306
|
-
utils
|
|
1307
|
-
utils
|
|
1467
|
+
const [key, pre] = utils.b58DecodeAndCheckPrefix(src, [
|
|
1468
|
+
utils.PrefixV2.Secp256k1PublicKey,
|
|
1469
|
+
utils.PrefixV2.P256PublicKey,
|
|
1308
1470
|
]);
|
|
1309
|
-
return [key, pre === utils
|
|
1471
|
+
return [key, pre === utils.PrefixV2.Secp256k1PublicKey ? 'secp256k1' : 'p256'];
|
|
1310
1472
|
}
|
|
1311
1473
|
else if (curve !== undefined) {
|
|
1312
1474
|
return [src, curve];
|
|
@@ -1324,7 +1486,7 @@
|
|
|
1324
1486
|
if (other instanceof ECPublicKey) {
|
|
1325
1487
|
if (this.curve === other.curve) {
|
|
1326
1488
|
const compress = this.curve === 'secp256k1';
|
|
1327
|
-
return utils
|
|
1489
|
+
return utils.compareArrays(this.bytes(compress), other.bytes(compress));
|
|
1328
1490
|
}
|
|
1329
1491
|
else if (this.curve === 'secp256k1') {
|
|
1330
1492
|
return -1;
|
|
@@ -1334,24 +1496,24 @@
|
|
|
1334
1496
|
}
|
|
1335
1497
|
}
|
|
1336
1498
|
else {
|
|
1337
|
-
throw new utils
|
|
1499
|
+
throw new utils.InvalidPublicKeyError('ECDSA key expected');
|
|
1338
1500
|
}
|
|
1339
1501
|
}
|
|
1340
1502
|
hash() {
|
|
1341
1503
|
const key = this.bytes();
|
|
1342
|
-
return utils
|
|
1504
|
+
return utils.b58Encode(blake2b(key, { dkLen: 20 }), pref[this.curve].pkh);
|
|
1343
1505
|
}
|
|
1344
1506
|
bytes(compress = true) {
|
|
1345
1507
|
// @noble/curves supports both compressed and uncompressed formats
|
|
1346
1508
|
// We need to convert the stored public key to the requested format
|
|
1347
1509
|
if (this.curve === 'secp256k1') {
|
|
1348
1510
|
// For secp256k1, we need to get the public key from the stored bytes and convert format
|
|
1349
|
-
const point =
|
|
1511
|
+
const point = secp256k1_js.secp256k1.Point.fromBytes(__classPrivateFieldGet(this, _ECPublicKey_key, "f"));
|
|
1350
1512
|
return point.toBytes(compress);
|
|
1351
1513
|
}
|
|
1352
1514
|
else {
|
|
1353
1515
|
// For p256, we need to get the public key from the stored bytes and convert format
|
|
1354
|
-
const point = nist_js.p256.Point.
|
|
1516
|
+
const point = nist_js.p256.Point.fromBytes(__classPrivateFieldGet(this, _ECPublicKey_key, "f"));
|
|
1355
1517
|
return point.toBytes(compress);
|
|
1356
1518
|
}
|
|
1357
1519
|
}
|
|
@@ -1364,7 +1526,7 @@
|
|
|
1364
1526
|
}
|
|
1365
1527
|
toString() {
|
|
1366
1528
|
const key = this.bytes();
|
|
1367
|
-
return utils
|
|
1529
|
+
return utils.b58Encode(key, pref[this.curve].pk);
|
|
1368
1530
|
}
|
|
1369
1531
|
}
|
|
1370
1532
|
_ECPublicKey_key = new WeakMap();
|
|
@@ -1391,7 +1553,7 @@
|
|
|
1391
1553
|
abytes$1(key, undefined, 'key');
|
|
1392
1554
|
this.iHash = hash.create();
|
|
1393
1555
|
if (typeof this.iHash.update !== 'function')
|
|
1394
|
-
throw new Error('
|
|
1556
|
+
throw new Error('expected Hash instance');
|
|
1395
1557
|
this.blockLen = this.iHash.blockLen;
|
|
1396
1558
|
this.outputLen = this.iHash.outputLen;
|
|
1397
1559
|
const blockLen = this.blockLen;
|
|
@@ -1436,12 +1598,13 @@
|
|
|
1436
1598
|
// Create new instance without calling constructor since the key
|
|
1437
1599
|
// is already in state and we don't know it.
|
|
1438
1600
|
to ||= Object.create(Object.getPrototypeOf(this), {});
|
|
1439
|
-
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
|
1601
|
+
const { oHash, iHash, finished, destroyed, blockLen, outputLen, canXOF } = this;
|
|
1440
1602
|
to = to;
|
|
1441
1603
|
to.finished = finished;
|
|
1442
1604
|
to.destroyed = destroyed;
|
|
1443
1605
|
to.blockLen = blockLen;
|
|
1444
1606
|
to.outputLen = outputLen;
|
|
1607
|
+
to.canXOF = canXOF;
|
|
1445
1608
|
to.oHash = oHash._cloneInto(to.oHash);
|
|
1446
1609
|
to.iHash = iHash._cloneInto(to.iHash);
|
|
1447
1610
|
return to;
|
|
@@ -1465,16 +1628,16 @@
|
|
|
1465
1628
|
* PBKDF (RFC 2898). Can be used to create a key from password and salt.
|
|
1466
1629
|
* @module
|
|
1467
1630
|
*/
|
|
1468
|
-
// Common
|
|
1631
|
+
// Common validation and per-call state setup for sync/async functions.
|
|
1469
1632
|
function pbkdf2Init(hash, _password, _salt, _opts) {
|
|
1470
1633
|
ahash$1(hash);
|
|
1471
1634
|
const opts = checkOpts$1({ dkLen: 32, asyncTick: 10 }, _opts);
|
|
1472
1635
|
const { c, dkLen, asyncTick } = opts;
|
|
1473
|
-
anumber$
|
|
1474
|
-
anumber$
|
|
1475
|
-
anumber$
|
|
1636
|
+
anumber$1(c, 'c');
|
|
1637
|
+
anumber$1(dkLen, 'dkLen');
|
|
1638
|
+
anumber$1(asyncTick, 'asyncTick');
|
|
1476
1639
|
if (c < 1)
|
|
1477
|
-
throw new Error('
|
|
1640
|
+
throw new Error('"c" (iterations) must be >= 1');
|
|
1478
1641
|
// RFC 8018 §5.2 defines `dkLen` as "a positive integer".
|
|
1479
1642
|
if (dkLen < 1)
|
|
1480
1643
|
throw new Error('"dkLen" must be >= 1');
|
|
@@ -1482,25 +1645,69 @@
|
|
|
1482
1645
|
// before allocating the destination buffer.
|
|
1483
1646
|
if (dkLen > (2 ** 32 - 1) * hash.outputLen)
|
|
1484
1647
|
throw new Error('derived key too long');
|
|
1485
|
-
const
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1648
|
+
const p = kdfInputToBytes(_password, 'password');
|
|
1649
|
+
try {
|
|
1650
|
+
const s = kdfInputToBytes(_salt, 'salt');
|
|
1651
|
+
try {
|
|
1652
|
+
// DK = PBKDF2(PRF, Password, Salt, c, dkLen);
|
|
1653
|
+
const DK = new Uint8Array(dkLen);
|
|
1654
|
+
const { iHash, oHash, outputLen } = hmac.create(hash, p);
|
|
1655
|
+
// Drive keyed hashes directly; the wrapper is only needed to initialize their HMAC midstates.
|
|
1656
|
+
const u = new Uint8Array(outputLen);
|
|
1657
|
+
const eng = pbkdf2Engine(iHash, oHash, s, u);
|
|
1658
|
+
return { c, dkLen, asyncTick, DK, outputLen, eng };
|
|
1659
|
+
}
|
|
1660
|
+
finally {
|
|
1661
|
+
// Uint8Array inputs belong to the caller; only wipe our UTF-8 conversion.
|
|
1662
|
+
if (typeof _salt === 'string')
|
|
1663
|
+
clean$1(s);
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
finally {
|
|
1667
|
+
if (typeof _password === 'string')
|
|
1668
|
+
clean$1(p);
|
|
1669
|
+
}
|
|
1494
1670
|
}
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1671
|
+
// Per-call PRF driver writes U1 into both `u` and `Ti`, then later digests into `u`;
|
|
1672
|
+
// shared by the sync and async variants.
|
|
1673
|
+
function pbkdf2Engine(iHash, oHash, salt, u) {
|
|
1674
|
+
const counter = new Uint8Array(4);
|
|
1675
|
+
const view = createView(counter);
|
|
1676
|
+
// Full clones retain tree/config state; absorb salt before async yields without cloning input.
|
|
1677
|
+
const salted = iHash._cloneInto().update(salt);
|
|
1678
|
+
// u1() overwrites the worker before reading it. Seed from the outer midstate so a long salt
|
|
1679
|
+
// cannot pre-populate a tree stack that the first reset would abandon without wiping.
|
|
1680
|
+
const work = oHash._cloneInto();
|
|
1681
|
+
const iClone = iHash._cloneInto; // Capture before mixed feedback can materialize state tuples.
|
|
1682
|
+
const oClone = oHash._cloneInto;
|
|
1683
|
+
return {
|
|
1684
|
+
u1: (ti, Ti) => {
|
|
1685
|
+
view.setInt32(0, ti, false);
|
|
1686
|
+
salted._cloneInto(work).update(counter).digestInto(u);
|
|
1687
|
+
oHash._cloneInto(work).update(u).digestInto(u);
|
|
1688
|
+
Ti.set(u.subarray(0, Ti.length));
|
|
1689
|
+
},
|
|
1690
|
+
// Whole `F` inner loop for the sync variant: one optimized function owns the hot loop.
|
|
1691
|
+
rounds: (c, Ti) => {
|
|
1692
|
+
for (let ui = 1; ui < c; ui++) {
|
|
1693
|
+
iClone.call(iHash, work).update(u).digestInto(u);
|
|
1694
|
+
oClone.call(oHash, work).update(u).digestInto(u);
|
|
1695
|
+
for (let i = 0; i < Ti.length; i++)
|
|
1696
|
+
Ti[i] ^= u[i];
|
|
1697
|
+
}
|
|
1698
|
+
},
|
|
1699
|
+
output: (DK) => {
|
|
1700
|
+
// Keyed templates and derived worker states are secret material.
|
|
1701
|
+
iHash.destroy();
|
|
1702
|
+
oHash.destroy();
|
|
1703
|
+
salted.destroy();
|
|
1704
|
+
work.destroy();
|
|
1705
|
+
// Shared sync/async cleanup point: wipe transient PRF state
|
|
1706
|
+
// while preserving the derived key buffer.
|
|
1707
|
+
clean$1(u);
|
|
1708
|
+
return DK;
|
|
1709
|
+
},
|
|
1710
|
+
};
|
|
1504
1711
|
}
|
|
1505
1712
|
/**
|
|
1506
1713
|
* PBKDF2-HMAC: RFC 8018 key derivation function.
|
|
@@ -1521,30 +1728,20 @@
|
|
|
1521
1728
|
* ```
|
|
1522
1729
|
*/
|
|
1523
1730
|
function pbkdf2(hash, password, salt, opts) {
|
|
1524
|
-
const { c, dkLen, DK,
|
|
1525
|
-
let prfW; // Working copy
|
|
1526
|
-
const arr = new Uint8Array(4);
|
|
1527
|
-
const view = createView(arr);
|
|
1528
|
-
const u = new Uint8Array(PRF.outputLen);
|
|
1731
|
+
const { c, dkLen, DK, outputLen, eng } = pbkdf2Init(hash, password, salt, opts);
|
|
1529
1732
|
// DK = T1 + T2 + ⋯ + Tdklen/hlen
|
|
1530
|
-
for (let ti = 1, pos = 0; pos < dkLen; ti++, pos +=
|
|
1733
|
+
for (let ti = 1, pos = 0; pos < dkLen; ti++, pos += outputLen) {
|
|
1531
1734
|
// Ti = F(Password, Salt, c, i)
|
|
1532
1735
|
// The last Ti view can be shorter than hLen, which applies
|
|
1533
1736
|
// RFC 8018 §5.2 step 4's T_l<0..r-1> truncation without extra copies.
|
|
1534
|
-
const Ti = DK.subarray(pos, pos +
|
|
1535
|
-
view.setInt32(0, ti, false);
|
|
1737
|
+
const Ti = DK.subarray(pos, pos + outputLen);
|
|
1536
1738
|
// F(Password, Salt, c, i) = U1 ^ U2 ^ ⋯ ^ Uc
|
|
1537
1739
|
// U1 = PRF(Password, Salt + INT_32_BE(i))
|
|
1538
|
-
(
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
// Uc = PRF(Password, Uc−1)
|
|
1542
|
-
PRF._cloneInto(prfW).update(u).digestInto(u);
|
|
1543
|
-
for (let i = 0; i < Ti.length; i++)
|
|
1544
|
-
Ti[i] ^= u[i];
|
|
1545
|
-
}
|
|
1740
|
+
eng.u1(ti, Ti);
|
|
1741
|
+
// Uc = PRF(Password, Uc−1); Ti ^= Uc
|
|
1742
|
+
eng.rounds(c, Ti);
|
|
1546
1743
|
}
|
|
1547
|
-
return
|
|
1744
|
+
return eng.output(DK);
|
|
1548
1745
|
}
|
|
1549
1746
|
|
|
1550
1747
|
/**
|
|
@@ -1573,8 +1770,28 @@
|
|
|
1573
1770
|
const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
1574
1771
|
/** Internal SHA-224 / SHA-256 compression engine from RFC 6234 §6.2. */
|
|
1575
1772
|
class SHA2_32B extends HashMD {
|
|
1576
|
-
|
|
1773
|
+
// We cannot use array here since array allows indexing by variable
|
|
1774
|
+
// which means optimizer/compiler cannot use registers.
|
|
1775
|
+
// Numeric initializers matter: starting the fields as `undefined` changes
|
|
1776
|
+
// V8's field representation and makes sha256 3x slower (measured).
|
|
1777
|
+
A = 0;
|
|
1778
|
+
B = 0;
|
|
1779
|
+
C = 0;
|
|
1780
|
+
D = 0;
|
|
1781
|
+
E = 0;
|
|
1782
|
+
F = 0;
|
|
1783
|
+
G = 0;
|
|
1784
|
+
H = 0;
|
|
1785
|
+
constructor(outputLen, IV) {
|
|
1577
1786
|
super(64, outputLen, 8, false);
|
|
1787
|
+
this.A = IV[0] | 0;
|
|
1788
|
+
this.B = IV[1] | 0;
|
|
1789
|
+
this.C = IV[2] | 0;
|
|
1790
|
+
this.D = IV[3] | 0;
|
|
1791
|
+
this.E = IV[4] | 0;
|
|
1792
|
+
this.F = IV[5] | 0;
|
|
1793
|
+
this.G = IV[6] | 0;
|
|
1794
|
+
this.H = IV[7] | 0;
|
|
1578
1795
|
}
|
|
1579
1796
|
get() {
|
|
1580
1797
|
const { A, B, C, D, E, F, G, H } = this;
|
|
@@ -1591,6 +1808,10 @@
|
|
|
1591
1808
|
this.G = G | 0;
|
|
1592
1809
|
this.H = H | 0;
|
|
1593
1810
|
}
|
|
1811
|
+
_cloneInto(to) {
|
|
1812
|
+
(to ||= new this.constructor()).set(...this.get());
|
|
1813
|
+
return this._cloneIntoMeta(to);
|
|
1814
|
+
}
|
|
1594
1815
|
process(view, offset) {
|
|
1595
1816
|
// Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
|
|
1596
1817
|
for (let i = 0; i < 16; i++, offset += 4)
|
|
@@ -1642,18 +1863,8 @@
|
|
|
1642
1863
|
}
|
|
1643
1864
|
/** Internal SHA-256 hash class grounded in RFC 6234 §6.2. */
|
|
1644
1865
|
class _SHA256 extends SHA2_32B {
|
|
1645
|
-
// We cannot use array here since array allows indexing by variable
|
|
1646
|
-
// which means optimizer/compiler cannot use registers.
|
|
1647
|
-
A = SHA256_IV[0] | 0;
|
|
1648
|
-
B = SHA256_IV[1] | 0;
|
|
1649
|
-
C = SHA256_IV[2] | 0;
|
|
1650
|
-
D = SHA256_IV[3] | 0;
|
|
1651
|
-
E = SHA256_IV[4] | 0;
|
|
1652
|
-
F = SHA256_IV[5] | 0;
|
|
1653
|
-
G = SHA256_IV[6] | 0;
|
|
1654
|
-
H = SHA256_IV[7] | 0;
|
|
1655
1866
|
constructor() {
|
|
1656
|
-
super(32);
|
|
1867
|
+
super(32, SHA256_IV);
|
|
1657
1868
|
}
|
|
1658
1869
|
}
|
|
1659
1870
|
// SHA2-512 is slower than sha256 in js because u64 operations are slow.
|
|
@@ -1690,8 +1901,45 @@
|
|
|
1690
1901
|
const SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
|
|
1691
1902
|
/** Internal SHA-384 / SHA-512 compression engine from RFC 6234 §6.4. */
|
|
1692
1903
|
class SHA2_64B extends HashMD {
|
|
1693
|
-
|
|
1904
|
+
// We cannot use array here since array allows indexing by variable
|
|
1905
|
+
// which means optimizer/compiler cannot use registers.
|
|
1906
|
+
// h -- high 32 bits, l -- low 32 bits
|
|
1907
|
+
// Numeric initializers matter: starting the fields as `undefined` changes
|
|
1908
|
+
// V8's field representation and slows hashing down (measured on sha256).
|
|
1909
|
+
Ah = 0;
|
|
1910
|
+
Al = 0;
|
|
1911
|
+
Bh = 0;
|
|
1912
|
+
Bl = 0;
|
|
1913
|
+
Ch = 0;
|
|
1914
|
+
Cl = 0;
|
|
1915
|
+
Dh = 0;
|
|
1916
|
+
Dl = 0;
|
|
1917
|
+
Eh = 0;
|
|
1918
|
+
El = 0;
|
|
1919
|
+
Fh = 0;
|
|
1920
|
+
Fl = 0;
|
|
1921
|
+
Gh = 0;
|
|
1922
|
+
Gl = 0;
|
|
1923
|
+
Hh = 0;
|
|
1924
|
+
Hl = 0;
|
|
1925
|
+
constructor(outputLen, IV) {
|
|
1694
1926
|
super(128, outputLen, 16, false);
|
|
1927
|
+
this.Ah = IV[0] | 0;
|
|
1928
|
+
this.Al = IV[1] | 0;
|
|
1929
|
+
this.Bh = IV[2] | 0;
|
|
1930
|
+
this.Bl = IV[3] | 0;
|
|
1931
|
+
this.Ch = IV[4] | 0;
|
|
1932
|
+
this.Cl = IV[5] | 0;
|
|
1933
|
+
this.Dh = IV[6] | 0;
|
|
1934
|
+
this.Dl = IV[7] | 0;
|
|
1935
|
+
this.Eh = IV[8] | 0;
|
|
1936
|
+
this.El = IV[9] | 0;
|
|
1937
|
+
this.Fh = IV[10] | 0;
|
|
1938
|
+
this.Fl = IV[11] | 0;
|
|
1939
|
+
this.Gh = IV[12] | 0;
|
|
1940
|
+
this.Gl = IV[13] | 0;
|
|
1941
|
+
this.Hh = IV[14] | 0;
|
|
1942
|
+
this.Hl = IV[15] | 0;
|
|
1695
1943
|
}
|
|
1696
1944
|
// prettier-ignore
|
|
1697
1945
|
get() {
|
|
@@ -1717,6 +1965,10 @@
|
|
|
1717
1965
|
this.Hh = Hh | 0;
|
|
1718
1966
|
this.Hl = Hl | 0;
|
|
1719
1967
|
}
|
|
1968
|
+
_cloneInto(to) {
|
|
1969
|
+
(to ||= new this.constructor()).set(...this.get());
|
|
1970
|
+
return this._cloneIntoMeta(to);
|
|
1971
|
+
}
|
|
1720
1972
|
process(view, offset) {
|
|
1721
1973
|
// Extend the first 16 words into the remaining 64 words w[16..79] of the message schedule array
|
|
1722
1974
|
for (let i = 0; i < 16; i++, offset += 4) {
|
|
@@ -1800,24 +2052,8 @@
|
|
|
1800
2052
|
}
|
|
1801
2053
|
/** Internal SHA-512 hash class grounded in RFC 6234 §6.3 and §6.4. */
|
|
1802
2054
|
class _SHA512 extends SHA2_64B {
|
|
1803
|
-
Ah = SHA512_IV[0] | 0;
|
|
1804
|
-
Al = SHA512_IV[1] | 0;
|
|
1805
|
-
Bh = SHA512_IV[2] | 0;
|
|
1806
|
-
Bl = SHA512_IV[3] | 0;
|
|
1807
|
-
Ch = SHA512_IV[4] | 0;
|
|
1808
|
-
Cl = SHA512_IV[5] | 0;
|
|
1809
|
-
Dh = SHA512_IV[6] | 0;
|
|
1810
|
-
Dl = SHA512_IV[7] | 0;
|
|
1811
|
-
Eh = SHA512_IV[8] | 0;
|
|
1812
|
-
El = SHA512_IV[9] | 0;
|
|
1813
|
-
Fh = SHA512_IV[10] | 0;
|
|
1814
|
-
Fl = SHA512_IV[11] | 0;
|
|
1815
|
-
Gh = SHA512_IV[12] | 0;
|
|
1816
|
-
Gl = SHA512_IV[13] | 0;
|
|
1817
|
-
Hh = SHA512_IV[14] | 0;
|
|
1818
|
-
Hl = SHA512_IV[15] | 0;
|
|
1819
2055
|
constructor() {
|
|
1820
|
-
super(64);
|
|
2056
|
+
super(64, SHA512_IV);
|
|
1821
2057
|
}
|
|
1822
2058
|
}
|
|
1823
2059
|
/**
|
|
@@ -1828,6 +2064,7 @@
|
|
|
1828
2064
|
* - Each sha256 hash is executing 2^18 bit operations.
|
|
1829
2065
|
* - Good 2024 ASICs can do 200Th/sec with 3500 watts of power, corresponding to 2^36 hashes/joule.
|
|
1830
2066
|
* @param msg - message bytes to hash
|
|
2067
|
+
* @param opts - Reserved hash options.
|
|
1831
2068
|
* @returns Digest bytes.
|
|
1832
2069
|
* @example
|
|
1833
2070
|
* Hash a message with SHA2-256.
|
|
@@ -1840,6 +2077,7 @@
|
|
|
1840
2077
|
/**
|
|
1841
2078
|
* SHA2-512 hash function from RFC 4634.
|
|
1842
2079
|
* @param msg - message bytes to hash
|
|
2080
|
+
* @param opts - Reserved hash options.
|
|
1843
2081
|
* @returns Digest bytes.
|
|
1844
2082
|
* @example
|
|
1845
2083
|
* Hash a message with SHA2-512.
|
|
@@ -1850,353 +2088,28 @@
|
|
|
1850
2088
|
const sha512 = /* @__PURE__ */ createHasher$1(() => new _SHA512(),
|
|
1851
2089
|
/* @__PURE__ */ oidNist$1(0x03));
|
|
1852
2090
|
|
|
1853
|
-
/*! scure-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
}
|
|
1873
|
-
else {
|
|
1874
|
-
return arr.every((item) => Number.isSafeInteger(item));
|
|
2091
|
+
/*! scure-bip39 - MIT License (c) 2022 Patricio Palladino, Paul Miller (paulmillr.com) */
|
|
2092
|
+
// Japanese wordlist
|
|
2093
|
+
// The canonical BIP-39 Japanese wordlist starts with あいこくしん.
|
|
2094
|
+
// Use that sentinel so generated phrases use U+3000 ideographic spaces.
|
|
2095
|
+
const isJapanese = (wordlist) => wordlist[0] === '\u3042\u3044\u3053\u304f\u3057\u3093';
|
|
2096
|
+
// JavaScript strings can contain unpaired UTF-16 surrogates, which cannot be encoded as UTF-8.
|
|
2097
|
+
function isWellFormedUnicode(value) {
|
|
2098
|
+
for (let i = 0; i < value.length; i++) {
|
|
2099
|
+
const current = value.charCodeAt(i);
|
|
2100
|
+
if (current >= 0xd800 && current <= 0xdbff) {
|
|
2101
|
+
if (i + 1 >= value.length)
|
|
2102
|
+
return false;
|
|
2103
|
+
const next = value.charCodeAt(++i);
|
|
2104
|
+
if (next < 0xdc00 || next > 0xdfff)
|
|
2105
|
+
return false;
|
|
2106
|
+
}
|
|
2107
|
+
else if (current >= 0xdc00 && current <= 0xdfff) {
|
|
2108
|
+
return false;
|
|
2109
|
+
}
|
|
1875
2110
|
}
|
|
1876
|
-
}
|
|
1877
|
-
function afn(input) {
|
|
1878
|
-
if (typeof input !== 'function')
|
|
1879
|
-
throw new TypeError('function expected');
|
|
1880
2111
|
return true;
|
|
1881
2112
|
}
|
|
1882
|
-
function astr(label, input) {
|
|
1883
|
-
if (typeof input !== 'string')
|
|
1884
|
-
throw new TypeError(`${label}: string expected`);
|
|
1885
|
-
return true;
|
|
1886
|
-
}
|
|
1887
|
-
function anumber$1(n) {
|
|
1888
|
-
if (typeof n !== 'number')
|
|
1889
|
-
throw new TypeError(`number expected, got ${typeof n}`);
|
|
1890
|
-
if (!Number.isSafeInteger(n))
|
|
1891
|
-
throw new RangeError(`invalid integer: ${n}`);
|
|
1892
|
-
}
|
|
1893
|
-
function aArr(input) {
|
|
1894
|
-
if (!Array.isArray(input))
|
|
1895
|
-
throw new TypeError('array expected');
|
|
1896
|
-
}
|
|
1897
|
-
function astrArr(label, input) {
|
|
1898
|
-
if (!isArrayOf(true, input))
|
|
1899
|
-
throw new TypeError(`${label}: array of strings expected`);
|
|
1900
|
-
}
|
|
1901
|
-
function anumArr(label, input) {
|
|
1902
|
-
if (!isArrayOf(false, input))
|
|
1903
|
-
throw new TypeError(`${label}: array of numbers expected`);
|
|
1904
|
-
}
|
|
1905
|
-
/**
|
|
1906
|
-
* @__NO_SIDE_EFFECTS__
|
|
1907
|
-
*/
|
|
1908
|
-
function chain(...args) {
|
|
1909
|
-
const id = (a) => a;
|
|
1910
|
-
// Wrap call in closure so JIT can inline calls
|
|
1911
|
-
const wrap = (a, b) => (c) => a(b(c));
|
|
1912
|
-
// Construct chain of args[-1].encode(args[-2].encode([...]))
|
|
1913
|
-
const encode = args.map((x) => x.encode).reduceRight(wrap, id);
|
|
1914
|
-
// Construct chain of args[0].decode(args[1].decode(...))
|
|
1915
|
-
const decode = args.map((x) => x.decode).reduce(wrap, id);
|
|
1916
|
-
return { encode, decode };
|
|
1917
|
-
}
|
|
1918
|
-
/**
|
|
1919
|
-
* Encodes integer radix representation to array of strings using alphabet and back.
|
|
1920
|
-
* Could also be array of strings.
|
|
1921
|
-
* @__NO_SIDE_EFFECTS__
|
|
1922
|
-
*/
|
|
1923
|
-
function alphabet(letters) {
|
|
1924
|
-
// mapping 1 to "b"
|
|
1925
|
-
const lettersA = typeof letters === 'string' ? letters.split('') : letters;
|
|
1926
|
-
const len = lettersA.length;
|
|
1927
|
-
astrArr('alphabet', lettersA);
|
|
1928
|
-
// mapping "b" to 1
|
|
1929
|
-
const indexes = new Map(lettersA.map((l, i) => [l, i]));
|
|
1930
|
-
return {
|
|
1931
|
-
encode: (digits) => {
|
|
1932
|
-
aArr(digits);
|
|
1933
|
-
return digits.map((i) => {
|
|
1934
|
-
if (!Number.isSafeInteger(i) || i < 0 || i >= len)
|
|
1935
|
-
throw new Error(`alphabet.encode: digit index outside alphabet "${i}". Allowed: ${letters}`);
|
|
1936
|
-
return lettersA[i];
|
|
1937
|
-
});
|
|
1938
|
-
},
|
|
1939
|
-
decode: (input) => {
|
|
1940
|
-
aArr(input);
|
|
1941
|
-
return input.map((letter) => {
|
|
1942
|
-
astr('alphabet.decode', letter);
|
|
1943
|
-
const i = indexes.get(letter);
|
|
1944
|
-
if (i === undefined)
|
|
1945
|
-
throw new Error(`Unknown letter: "${letter}". Allowed: ${letters}`);
|
|
1946
|
-
return i;
|
|
1947
|
-
});
|
|
1948
|
-
},
|
|
1949
|
-
};
|
|
1950
|
-
}
|
|
1951
|
-
/**
|
|
1952
|
-
* @__NO_SIDE_EFFECTS__
|
|
1953
|
-
*/
|
|
1954
|
-
function join(separator = '') {
|
|
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.
|
|
1958
|
-
return {
|
|
1959
|
-
encode: (from) => {
|
|
1960
|
-
astrArr('join.decode', from);
|
|
1961
|
-
return from.join(separator);
|
|
1962
|
-
},
|
|
1963
|
-
decode: (to) => {
|
|
1964
|
-
astr('join.decode', to);
|
|
1965
|
-
return to.split(separator);
|
|
1966
|
-
},
|
|
1967
|
-
};
|
|
1968
|
-
}
|
|
1969
|
-
/**
|
|
1970
|
-
* Pad strings array so it has integer number of bits
|
|
1971
|
-
* @__NO_SIDE_EFFECTS__
|
|
1972
|
-
*/
|
|
1973
|
-
function padding(bits, chr = '=') {
|
|
1974
|
-
anumber$1(bits);
|
|
1975
|
-
astr('padding', chr);
|
|
1976
|
-
return {
|
|
1977
|
-
encode(data) {
|
|
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.
|
|
1981
|
-
while ((data.length * bits) % 8)
|
|
1982
|
-
data.push(chr);
|
|
1983
|
-
return data;
|
|
1984
|
-
},
|
|
1985
|
-
decode(input) {
|
|
1986
|
-
astrArr('padding.decode', input);
|
|
1987
|
-
let end = input.length;
|
|
1988
|
-
if ((end * bits) % 8)
|
|
1989
|
-
throw new Error('padding: invalid, string should have whole number of bytes');
|
|
1990
|
-
for (; end > 0 && input[end - 1] === chr; end--) {
|
|
1991
|
-
const last = end - 1;
|
|
1992
|
-
const byte = last * bits;
|
|
1993
|
-
if (byte % 8 === 0)
|
|
1994
|
-
throw new Error('padding: invalid, string has too much padding');
|
|
1995
|
-
}
|
|
1996
|
-
return input.slice(0, end);
|
|
1997
|
-
},
|
|
1998
|
-
};
|
|
1999
|
-
}
|
|
2000
|
-
/**
|
|
2001
|
-
* Slow: O(n^2) time complexity
|
|
2002
|
-
*/
|
|
2003
|
-
function convertRadix(data, from, to) {
|
|
2004
|
-
// base 1 is impossible
|
|
2005
|
-
if (from < 2)
|
|
2006
|
-
throw new RangeError(`convertRadix: invalid from=${from}, base cannot be less than 2`);
|
|
2007
|
-
if (to < 2)
|
|
2008
|
-
throw new RangeError(`convertRadix: invalid to=${to}, base cannot be less than 2`);
|
|
2009
|
-
aArr(data);
|
|
2010
|
-
if (!data.length)
|
|
2011
|
-
return [];
|
|
2012
|
-
let pos = 0;
|
|
2013
|
-
const res = [];
|
|
2014
|
-
const digits = Array.from(data, (d) => {
|
|
2015
|
-
anumber$1(d);
|
|
2016
|
-
if (d < 0 || d >= from)
|
|
2017
|
-
throw new Error(`invalid integer: ${d}`);
|
|
2018
|
-
return d;
|
|
2019
|
-
});
|
|
2020
|
-
const dlen = digits.length;
|
|
2021
|
-
while (true) {
|
|
2022
|
-
let carry = 0;
|
|
2023
|
-
let done = true;
|
|
2024
|
-
for (let i = pos; i < dlen; i++) {
|
|
2025
|
-
const digit = digits[i];
|
|
2026
|
-
const fromCarry = from * carry;
|
|
2027
|
-
const digitBase = fromCarry + digit;
|
|
2028
|
-
if (!Number.isSafeInteger(digitBase) ||
|
|
2029
|
-
fromCarry / from !== carry ||
|
|
2030
|
-
digitBase - digit !== fromCarry) {
|
|
2031
|
-
throw new Error('convertRadix: carry overflow');
|
|
2032
|
-
}
|
|
2033
|
-
const div = digitBase / to;
|
|
2034
|
-
carry = digitBase % to;
|
|
2035
|
-
const rounded = Math.floor(div);
|
|
2036
|
-
digits[i] = rounded;
|
|
2037
|
-
if (!Number.isSafeInteger(rounded) || rounded * to + carry !== digitBase)
|
|
2038
|
-
throw new Error('convertRadix: carry overflow');
|
|
2039
|
-
if (!done)
|
|
2040
|
-
continue;
|
|
2041
|
-
else if (!rounded)
|
|
2042
|
-
pos = i;
|
|
2043
|
-
else
|
|
2044
|
-
done = false;
|
|
2045
|
-
}
|
|
2046
|
-
res.push(carry);
|
|
2047
|
-
if (done)
|
|
2048
|
-
break;
|
|
2049
|
-
}
|
|
2050
|
-
// Preserve explicit leading zero digits so callers like base58 keep zero-prefix semantics.
|
|
2051
|
-
for (let i = 0; i < data.length - 1 && data[i] === 0; i++)
|
|
2052
|
-
res.push(0);
|
|
2053
|
-
return res.reverse();
|
|
2054
|
-
}
|
|
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).
|
|
2058
|
-
const radix2carry = /* @__NO_SIDE_EFFECTS__ */ (from, to) => from + (to - gcd(from, to));
|
|
2059
|
-
const powers = /* @__PURE__ */ (() => {
|
|
2060
|
-
let res = [];
|
|
2061
|
-
for (let i = 0; i < 40; i++)
|
|
2062
|
-
res.push(2 ** i);
|
|
2063
|
-
return res;
|
|
2064
|
-
})();
|
|
2065
|
-
/**
|
|
2066
|
-
* Implemented with numbers, because BigInt is 5x slower
|
|
2067
|
-
*/
|
|
2068
|
-
function convertRadix2(data, from, to, padding) {
|
|
2069
|
-
aArr(data);
|
|
2070
|
-
if (from <= 0 || from > 32)
|
|
2071
|
-
throw new RangeError(`convertRadix2: wrong from=${from}`);
|
|
2072
|
-
if (to <= 0 || to > 32)
|
|
2073
|
-
throw new RangeError(`convertRadix2: wrong to=${to}`);
|
|
2074
|
-
if (radix2carry(from, to) > 32) {
|
|
2075
|
-
throw new Error(`convertRadix2: carry overflow from=${from} to=${to} carryBits=${radix2carry(from, to)}`);
|
|
2076
|
-
}
|
|
2077
|
-
let carry = 0;
|
|
2078
|
-
let pos = 0; // bitwise position in current element
|
|
2079
|
-
const max = powers[from];
|
|
2080
|
-
const mask = powers[to] - 1;
|
|
2081
|
-
const res = [];
|
|
2082
|
-
for (const n of data) {
|
|
2083
|
-
anumber$1(n);
|
|
2084
|
-
if (n >= max)
|
|
2085
|
-
throw new Error(`convertRadix2: invalid data word=${n} from=${from}`);
|
|
2086
|
-
carry = (carry << from) | n;
|
|
2087
|
-
if (pos + from > 32)
|
|
2088
|
-
throw new Error(`convertRadix2: carry overflow pos=${pos} from=${from}`);
|
|
2089
|
-
pos += from;
|
|
2090
|
-
for (; pos >= to; pos -= to)
|
|
2091
|
-
res.push(((carry >> (pos - to)) & mask) >>> 0);
|
|
2092
|
-
const pow = powers[pos];
|
|
2093
|
-
if (pow === undefined)
|
|
2094
|
-
throw new Error('invalid carry');
|
|
2095
|
-
carry &= pow - 1; // clean carry, otherwise it will cause overflow
|
|
2096
|
-
}
|
|
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.
|
|
2100
|
-
if (!padding && pos >= from)
|
|
2101
|
-
throw new Error('Excess padding');
|
|
2102
|
-
if (!padding && carry > 0)
|
|
2103
|
-
throw new Error(`Non-zero padding: ${carry}`);
|
|
2104
|
-
if (padding && pos > 0)
|
|
2105
|
-
res.push(carry >>> 0);
|
|
2106
|
-
return res;
|
|
2107
|
-
}
|
|
2108
|
-
/**
|
|
2109
|
-
* @__NO_SIDE_EFFECTS__
|
|
2110
|
-
*/
|
|
2111
|
-
function radix(num) {
|
|
2112
|
-
anumber$1(num);
|
|
2113
|
-
const _256 = 2 ** 8;
|
|
2114
|
-
// Base-range and carry-overflow checks live in convertRadix so encode/decode reject unsupported bases symmetrically.
|
|
2115
|
-
return {
|
|
2116
|
-
encode: (bytes) => {
|
|
2117
|
-
if (!isBytes$1(bytes))
|
|
2118
|
-
throw new TypeError('radix.encode input should be Uint8Array');
|
|
2119
|
-
return convertRadix(Array.from(bytes), _256, num);
|
|
2120
|
-
},
|
|
2121
|
-
decode: (digits) => {
|
|
2122
|
-
anumArr('radix.decode', digits);
|
|
2123
|
-
return Uint8Array.from(convertRadix(digits, num, _256));
|
|
2124
|
-
},
|
|
2125
|
-
};
|
|
2126
|
-
}
|
|
2127
|
-
/**
|
|
2128
|
-
* If both bases are power of same number (like `2**8 <-> 2**64`),
|
|
2129
|
-
* there is a linear algorithm. For now we have implementation for power-of-two bases only.
|
|
2130
|
-
* @__NO_SIDE_EFFECTS__
|
|
2131
|
-
*/
|
|
2132
|
-
function radix2(bits, revPadding = false) {
|
|
2133
|
-
anumber$1(bits);
|
|
2134
|
-
if (bits <= 0 || bits > 32)
|
|
2135
|
-
throw new RangeError('radix2: bits should be in (0..32]');
|
|
2136
|
-
if (radix2carry(8, bits) > 32 || radix2carry(bits, 8) > 32)
|
|
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.
|
|
2140
|
-
return {
|
|
2141
|
-
encode: (bytes) => {
|
|
2142
|
-
if (!isBytes$1(bytes))
|
|
2143
|
-
throw new TypeError('radix2.encode input should be Uint8Array');
|
|
2144
|
-
return convertRadix2(Array.from(bytes), 8, bits, !revPadding);
|
|
2145
|
-
},
|
|
2146
|
-
decode: (digits) => {
|
|
2147
|
-
anumArr('radix2.decode', digits);
|
|
2148
|
-
return Uint8Array.from(convertRadix2(digits, bits, 8, revPadding));
|
|
2149
|
-
},
|
|
2150
|
-
};
|
|
2151
|
-
}
|
|
2152
|
-
function checksum(len, fn) {
|
|
2153
|
-
anumber$1(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}`);
|
|
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.
|
|
2162
|
-
return {
|
|
2163
|
-
encode(data) {
|
|
2164
|
-
if (!isBytes$1(data))
|
|
2165
|
-
throw new TypeError('checksum.encode: input should be Uint8Array');
|
|
2166
|
-
const sum = _fn(data).slice(0, len);
|
|
2167
|
-
const res = new Uint8Array(data.length + len);
|
|
2168
|
-
res.set(data);
|
|
2169
|
-
res.set(sum, data.length);
|
|
2170
|
-
return res;
|
|
2171
|
-
},
|
|
2172
|
-
decode(data) {
|
|
2173
|
-
if (!isBytes$1(data))
|
|
2174
|
-
throw new TypeError('checksum.decode: input should be Uint8Array');
|
|
2175
|
-
const payload = data.slice(0, -len);
|
|
2176
|
-
const oldChecksum = data.slice(-len);
|
|
2177
|
-
const newChecksum = _fn(payload).slice(0, len);
|
|
2178
|
-
for (let i = 0; i < len; i++)
|
|
2179
|
-
if (newChecksum[i] !== oldChecksum[i])
|
|
2180
|
-
throw new Error('Invalid checksum');
|
|
2181
|
-
return payload;
|
|
2182
|
-
},
|
|
2183
|
-
};
|
|
2184
|
-
}
|
|
2185
|
-
// prettier-ignore
|
|
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({
|
|
2196
|
-
alphabet, chain, checksum, convertRadix, convertRadix2, radix, radix2, join, padding,
|
|
2197
|
-
});
|
|
2198
|
-
|
|
2199
|
-
/*! scure-bip39 - MIT License (c) 2022 Patricio Palladino, Paul Miller (paulmillr.com) */
|
|
2200
2113
|
// Normalization replaces equivalent sequences of characters
|
|
2201
2114
|
// so that any two texts that are equivalent will be reduced
|
|
2202
2115
|
// to the same sequence of code points, called the normal form of the original text.
|
|
@@ -2206,6 +2119,8 @@
|
|
|
2206
2119
|
function nfkd(str) {
|
|
2207
2120
|
if (typeof str !== 'string')
|
|
2208
2121
|
throw new TypeError('invalid mnemonic type: ' + typeof str);
|
|
2122
|
+
if (!isWellFormedUnicode(str))
|
|
2123
|
+
throw new TypeError('expected well-formed Unicode string');
|
|
2209
2124
|
return str.normalize('NFKD');
|
|
2210
2125
|
}
|
|
2211
2126
|
// BIP-39 mnemonics are consumed in NFKD form.
|
|
@@ -2223,23 +2138,97 @@
|
|
|
2223
2138
|
if (![16, 20, 24, 28, 32].includes(ent.length))
|
|
2224
2139
|
throw new RangeError('invalid entropy length');
|
|
2225
2140
|
}
|
|
2141
|
+
/**
|
|
2142
|
+
* Generate x random words. Uses Cryptographically-Secure Random Number Generator.
|
|
2143
|
+
* @param wordlist - Imported wordlist for a specific language.
|
|
2144
|
+
* @param strength - Mnemonic strength, from 128 to 256 bits.
|
|
2145
|
+
* @returns 12-24 word mnemonic phrase.
|
|
2146
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
2147
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
2148
|
+
* @example
|
|
2149
|
+
* Generate a new English mnemonic.
|
|
2150
|
+
* ```ts
|
|
2151
|
+
* import { generateMnemonic } from '@scure/bip39';
|
|
2152
|
+
* import { wordlist } from '@scure/bip39/wordlists/english.js';
|
|
2153
|
+
* const mnemonic = generateMnemonic(wordlist, 128);
|
|
2154
|
+
* // 'legal winner thank year wave sausage worth useful legal winner thank yellow'
|
|
2155
|
+
* ```
|
|
2156
|
+
*/
|
|
2157
|
+
function generateMnemonic$1(wordlist, strength = 128) {
|
|
2158
|
+
anumber$1(strength);
|
|
2159
|
+
if (strength % 32 !== 0 || strength > 256)
|
|
2160
|
+
throw new RangeError('Invalid entropy');
|
|
2161
|
+
return entropyToMnemonic$1(randomBytes$2(strength / 8), wordlist);
|
|
2162
|
+
}
|
|
2163
|
+
// BIP-39 checksum is the first ENT/32 bits of SHA-256(entropy).
|
|
2164
|
+
// Returned as a byte with checksum bits on the left and zeroes on the right.
|
|
2226
2165
|
const calcChecksum = (entropy) => {
|
|
2227
2166
|
// Checksum is ent.length/4 bits long
|
|
2228
2167
|
const bitsLeft = 8 - entropy.length / 4;
|
|
2229
2168
|
// Zero rightmost "bitsLeft" bits in byte
|
|
2230
2169
|
// For example: bitsLeft=4 val=10111101 -> 10110000
|
|
2231
|
-
return
|
|
2170
|
+
return (sha256(entropy)[0] >> bitsLeft) << bitsLeft;
|
|
2232
2171
|
};
|
|
2233
|
-
function
|
|
2172
|
+
function awordlist(wordlist) {
|
|
2234
2173
|
if (!Array.isArray(wordlist) || wordlist.length !== 2048 || typeof wordlist[0] !== 'string')
|
|
2235
2174
|
throw new TypeError('Wordlist: expected array of 2048 strings');
|
|
2236
2175
|
wordlist.forEach((i) => {
|
|
2237
2176
|
if (typeof i !== 'string')
|
|
2238
2177
|
throw new TypeError('wordlist: non-string element: ' + i);
|
|
2178
|
+
// Wordlists become mnemonic output, so apply the same lossless UTF-8 boundary as input phrases.
|
|
2179
|
+
if (!isWellFormedUnicode(i))
|
|
2180
|
+
throw new TypeError('wordlist: expected well-formed Unicode string');
|
|
2239
2181
|
});
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2182
|
+
}
|
|
2183
|
+
// BIP-39 appends checksum bits to entropy,
|
|
2184
|
+
// then splits the bitstream into 11-bit indexes for a 2048-word list.
|
|
2185
|
+
function encodeWords(entropy, wordlist) {
|
|
2186
|
+
awordlist(wordlist);
|
|
2187
|
+
const bytes = new Uint8Array(entropy.length + 1); // entropy || checksum byte
|
|
2188
|
+
bytes.set(entropy);
|
|
2189
|
+
bytes[entropy.length] = calcChecksum(entropy);
|
|
2190
|
+
const words = [];
|
|
2191
|
+
let carry = 0; // bit accumulator, holds < 19 bits
|
|
2192
|
+
let bits = 0;
|
|
2193
|
+
for (const byte of bytes) {
|
|
2194
|
+
carry = (carry << 8) | byte;
|
|
2195
|
+
bits += 8;
|
|
2196
|
+
if (bits >= 11) {
|
|
2197
|
+
bits -= 11;
|
|
2198
|
+
words.push(wordlist[(carry >>> bits) & 0x7ff]);
|
|
2199
|
+
carry &= (1 << bits) - 1;
|
|
2200
|
+
}
|
|
2201
|
+
}
|
|
2202
|
+
// Bits still in carry are the zero right bits of the checksum byte; drop them.
|
|
2203
|
+
return words;
|
|
2204
|
+
}
|
|
2205
|
+
// Reverse of encodeWords: repacks 11-bit indexes into bytes and verifies checksum.
|
|
2206
|
+
function decodeWords(words, wordlist) {
|
|
2207
|
+
awordlist(wordlist);
|
|
2208
|
+
const entLen = (words.length / 3) * 4; // every 3 words hold 32 entropy bits + 1 checksum bit
|
|
2209
|
+
const bytes = new Uint8Array(entLen + 1); // entropy || checksum byte
|
|
2210
|
+
let carry = 0; // bit accumulator, holds < 19 bits
|
|
2211
|
+
let bits = 0;
|
|
2212
|
+
let pos = 0;
|
|
2213
|
+
for (const word of words) {
|
|
2214
|
+
const index = wordlist.indexOf(word);
|
|
2215
|
+
if (index === -1)
|
|
2216
|
+
throw new Error('Unknown word: ' + word);
|
|
2217
|
+
carry = (carry << 11) | index;
|
|
2218
|
+
bits += 11;
|
|
2219
|
+
while (bits >= 8) {
|
|
2220
|
+
bits -= 8;
|
|
2221
|
+
bytes[pos++] = (carry >>> bits) & 0xff;
|
|
2222
|
+
}
|
|
2223
|
+
carry &= (1 << bits) - 1;
|
|
2224
|
+
}
|
|
2225
|
+
// Left-align leftover checksum bits, matching calcChecksum output.
|
|
2226
|
+
if (bits > 0)
|
|
2227
|
+
bytes[pos] = carry << (8 - bits);
|
|
2228
|
+
const entropy = bytes.subarray(0, entLen);
|
|
2229
|
+
if (bytes[entLen] !== calcChecksum(entropy))
|
|
2230
|
+
throw new Error('Invalid checksum');
|
|
2231
|
+
return Uint8Array.from(entropy);
|
|
2243
2232
|
}
|
|
2244
2233
|
/**
|
|
2245
2234
|
* Reversible: Converts mnemonic string to raw entropy in form of byte array.
|
|
@@ -2263,12 +2252,37 @@
|
|
|
2263
2252
|
* ])
|
|
2264
2253
|
* ```
|
|
2265
2254
|
*/
|
|
2266
|
-
function mnemonicToEntropy(mnemonic, wordlist) {
|
|
2255
|
+
function mnemonicToEntropy$1(mnemonic, wordlist) {
|
|
2267
2256
|
const { words } = normalize(mnemonic);
|
|
2268
|
-
const entropy =
|
|
2257
|
+
const entropy = decodeWords(words, wordlist);
|
|
2269
2258
|
aentropy(entropy);
|
|
2270
2259
|
return entropy;
|
|
2271
2260
|
}
|
|
2261
|
+
/**
|
|
2262
|
+
* Reversible: Converts raw entropy in form of byte array to mnemonic string.
|
|
2263
|
+
* @param entropy - Byte array.
|
|
2264
|
+
* @param wordlist - Imported wordlist for a specific language.
|
|
2265
|
+
* @returns 12-24 words.
|
|
2266
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
2267
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
2268
|
+
* @example
|
|
2269
|
+
* Convert raw entropy into an English mnemonic.
|
|
2270
|
+
* ```ts
|
|
2271
|
+
* import { entropyToMnemonic } from '@scure/bip39';
|
|
2272
|
+
* import { wordlist } from '@scure/bip39/wordlists/english.js';
|
|
2273
|
+
* const ent = new Uint8Array([
|
|
2274
|
+
* 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
|
|
2275
|
+
* 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f
|
|
2276
|
+
* ]);
|
|
2277
|
+
* const mnemonic = entropyToMnemonic(ent, wordlist);
|
|
2278
|
+
* // 'legal winner thank year wave sausage worth useful legal winner thank yellow'
|
|
2279
|
+
* ```
|
|
2280
|
+
*/
|
|
2281
|
+
function entropyToMnemonic$1(entropy, wordlist) {
|
|
2282
|
+
aentropy(entropy);
|
|
2283
|
+
const words = encodeWords(entropy, wordlist);
|
|
2284
|
+
return words.join(isJapanese(wordlist) ? '\u3000' : ' ');
|
|
2285
|
+
}
|
|
2272
2286
|
/**
|
|
2273
2287
|
* Validates mnemonic for being 12-24 words contained in `wordlist`.
|
|
2274
2288
|
* @param mnemonic - 12-24 words.
|
|
@@ -2288,7 +2302,7 @@
|
|
|
2288
2302
|
*/
|
|
2289
2303
|
function validateMnemonic(mnemonic, wordlist) {
|
|
2290
2304
|
try {
|
|
2291
|
-
mnemonicToEntropy(mnemonic, wordlist);
|
|
2305
|
+
mnemonicToEntropy$1(mnemonic, wordlist);
|
|
2292
2306
|
}
|
|
2293
2307
|
catch (e) {
|
|
2294
2308
|
return false;
|
|
@@ -2296,7 +2310,11 @@
|
|
|
2296
2310
|
return true;
|
|
2297
2311
|
}
|
|
2298
2312
|
// BIP-39 salts PBKDF2 with the UTF-8 NFKD string "mnemonic" + passphrase.
|
|
2299
|
-
const psalt = (passphrase) =>
|
|
2313
|
+
const psalt = (passphrase) => {
|
|
2314
|
+
if (typeof passphrase !== 'string')
|
|
2315
|
+
throw new TypeError('invalid passphrase type: ' + typeof passphrase);
|
|
2316
|
+
return nfkd('mnemonic' + passphrase);
|
|
2317
|
+
};
|
|
2300
2318
|
/**
|
|
2301
2319
|
* Irreversible: Uses KDF to derive 64 bytes of key data from mnemonic + optional password.
|
|
2302
2320
|
* @param mnemonic - 12-24 words.
|
|
@@ -4386,7 +4404,7 @@ zoo`.split('\n'));
|
|
|
4386
4404
|
|
|
4387
4405
|
/* eslint-disable @typescript-eslint/no-this-alias */
|
|
4388
4406
|
function generateKeyPairFromSeed(seed) {
|
|
4389
|
-
const publicKey =
|
|
4407
|
+
const publicKey = ed25519_js.ed25519.getPublicKey(seed);
|
|
4390
4408
|
const secretKey = new Uint8Array(64);
|
|
4391
4409
|
secretKey.set(seed);
|
|
4392
4410
|
secretKey.set(publicKey, 32);
|
|
@@ -4482,8 +4500,8 @@ zoo`.split('\n'));
|
|
|
4482
4500
|
const privateKeyBytes = priv.secretKey.toArray('be', 32);
|
|
4483
4501
|
const privateKeyUint8 = new Uint8Array(privateKeyBytes);
|
|
4484
4502
|
const publicKey = priv.curve === 'secp256k1'
|
|
4485
|
-
?
|
|
4486
|
-
:
|
|
4503
|
+
? secp256k1_js.secp256k1.getPublicKey(privateKeyUint8, true) // compressed public key
|
|
4504
|
+
: nist_js.p256.getPublicKey(privateKeyUint8, true); // compressed public key
|
|
4487
4505
|
this.keyPair = {
|
|
4488
4506
|
curve: priv.curve,
|
|
4489
4507
|
secretKey: priv.secretKey,
|
|
@@ -4507,10 +4525,10 @@ zoo`.split('\n'));
|
|
|
4507
4525
|
// Get curve order for validation
|
|
4508
4526
|
let curveOrder;
|
|
4509
4527
|
if (curve === 'secp256k1') {
|
|
4510
|
-
curveOrder = new BN(
|
|
4528
|
+
curveOrder = new BN(secp256k1_js.secp256k1.Point.CURVE().n.toString());
|
|
4511
4529
|
}
|
|
4512
4530
|
else {
|
|
4513
|
-
curveOrder = new BN(
|
|
4531
|
+
curveOrder = new BN(nist_js.p256.Point.CURVE().n.toString());
|
|
4514
4532
|
}
|
|
4515
4533
|
if (curveOrder.bitLength() !== 256) {
|
|
4516
4534
|
throw new InvalidBitSize(`Invalid curve "${curve}" with bit size "${curveOrder.bitLength()}" expecting bit size "256"`);
|
|
@@ -4557,10 +4575,10 @@ zoo`.split('\n'));
|
|
|
4557
4575
|
// Get curve order for comparison
|
|
4558
4576
|
let curveOrder;
|
|
4559
4577
|
if (this.keyPair.curve === 'secp256k1') {
|
|
4560
|
-
curveOrder = new BN(
|
|
4578
|
+
curveOrder = new BN(secp256k1_js.secp256k1.Point.CURVE().n.toString());
|
|
4561
4579
|
}
|
|
4562
4580
|
else {
|
|
4563
|
-
curveOrder = new BN(
|
|
4581
|
+
curveOrder = new BN(nist_js.p256.Point.CURVE().n.toString());
|
|
4564
4582
|
}
|
|
4565
4583
|
if (d.cmp(curveOrder) < 0) {
|
|
4566
4584
|
d = d.add(this.keyPair.secretKey).mod(curveOrder);
|
|
@@ -4657,16 +4675,16 @@ zoo`.split('\n'));
|
|
|
4657
4675
|
switch (curve) {
|
|
4658
4676
|
case 'ed25519': {
|
|
4659
4677
|
node = PrivateKey$1.fromSeed(seed).derivePath(path);
|
|
4660
|
-
const sk = utils
|
|
4678
|
+
const sk = utils.b58Encode(node.seed().slice(0, 32), utils.PrefixV2.Ed25519Seed);
|
|
4661
4679
|
return sk;
|
|
4662
4680
|
}
|
|
4663
4681
|
case 'secp256k1':
|
|
4664
4682
|
case 'p256': {
|
|
4665
|
-
const prefixType = curve === 'secp256k1' ? utils
|
|
4683
|
+
const prefixType = curve === 'secp256k1' ? utils.PrefixV2.Secp256k1SecretKey : utils.PrefixV2.P256SecretKey;
|
|
4666
4684
|
let privKey = PrivateKey.fromSeed(seed, curve);
|
|
4667
4685
|
privKey = privKey.derivePath(path);
|
|
4668
4686
|
const uint8arr = new Uint8Array(privKey.keyPair.secretKey.toArray());
|
|
4669
|
-
const sk = utils
|
|
4687
|
+
const sk = utils.b58Encode(uint8arr, prefixType);
|
|
4670
4688
|
return sk;
|
|
4671
4689
|
}
|
|
4672
4690
|
case 'bip25519': {
|
|
@@ -4678,23 +4696,90 @@ zoo`.split('\n'));
|
|
|
4678
4696
|
}
|
|
4679
4697
|
};
|
|
4680
4698
|
|
|
4699
|
+
/**
|
|
4700
|
+
* Converts an English BIP39 mnemonic to entropy bytes.
|
|
4701
|
+
* @throws InvalidMnemonicError for an invalid phrase, without including its text.
|
|
4702
|
+
*/
|
|
4703
|
+
function mnemonicToEntropy(mnemonic) {
|
|
4704
|
+
try {
|
|
4705
|
+
return mnemonicToEntropy$1(mnemonic, wordlist);
|
|
4706
|
+
}
|
|
4707
|
+
catch {
|
|
4708
|
+
throw new InvalidMnemonicError();
|
|
4709
|
+
}
|
|
4710
|
+
}
|
|
4711
|
+
/**
|
|
4712
|
+
* Converts 16, 20, 24, 28 or 32 entropy bytes to an English BIP39 mnemonic.
|
|
4713
|
+
* Strings must contain unprefixed, even-length hexadecimal. Leading zeros matter.
|
|
4714
|
+
*/
|
|
4715
|
+
function entropyToMnemonic(entropy) {
|
|
4716
|
+
let bytes;
|
|
4717
|
+
if (typeof entropy === 'string') {
|
|
4718
|
+
if (!/^(?:[0-9a-fA-F]{2})+$/.test(entropy)) {
|
|
4719
|
+
throw new Error('Entropy must be unprefixed, even-length hexadecimal');
|
|
4720
|
+
}
|
|
4721
|
+
bytes = Uint8Array.from({ length: entropy.length / 2 }, (_, index) => parseInt(entropy.slice(index * 2, index * 2 + 2), 16));
|
|
4722
|
+
}
|
|
4723
|
+
else {
|
|
4724
|
+
if (!(entropy instanceof Uint8Array) &&
|
|
4725
|
+
!(ArrayBuffer.isView(entropy) &&
|
|
4726
|
+
Object.prototype.toString.call(entropy) === '[object Uint8Array]')) {
|
|
4727
|
+
throw new TypeError('Entropy must be a Uint8Array');
|
|
4728
|
+
}
|
|
4729
|
+
bytes = new Uint8Array(entropy);
|
|
4730
|
+
}
|
|
4731
|
+
return entropyToMnemonic$1(bytes, wordlist);
|
|
4732
|
+
}
|
|
4733
|
+
/**
|
|
4734
|
+
* Generates an English BIP39 mnemonic with the dependency's secure random source.
|
|
4735
|
+
* @param strength Entropy bits, 128, 160, 192, 224 or 256. Defaults to 128.
|
|
4736
|
+
*/
|
|
4737
|
+
function generateMnemonic(strength = 128) {
|
|
4738
|
+
return generateMnemonic$1(wordlist, strength);
|
|
4739
|
+
}
|
|
4740
|
+
// Shared with the signer, but not exported from the package entrypoint.
|
|
4741
|
+
function deriveMnemonicSecretKey({ mnemonic, password = '', derivationPath = "44'/1729'/0'/0'", curve = 'ed25519', }) {
|
|
4742
|
+
if (!validateMnemonic(mnemonic, wordlist)) {
|
|
4743
|
+
throw new InvalidMnemonicError();
|
|
4744
|
+
}
|
|
4745
|
+
const seed = mnemonicToSeedSync(mnemonic, password);
|
|
4746
|
+
return { secretKey: generateSecretKey(seed, derivationPath, curve), curve };
|
|
4747
|
+
}
|
|
4748
|
+
/**
|
|
4749
|
+
* Derives key material without constructing an InMemorySigner.
|
|
4750
|
+
* Uses the same defaults, English BIP39 phrases, password and paths as fromMnemonic.
|
|
4751
|
+
* Supports Ed25519, secp256k1 and P-256 mnemonic derivation.
|
|
4752
|
+
* The returned secretKey is unencrypted and matches InMemorySigner.secretKey(),
|
|
4753
|
+
* including the expanded Ed25519 form. Avoid logging or sharing the result.
|
|
4754
|
+
*/
|
|
4755
|
+
function deriveKeyFromMnemonic(params) {
|
|
4756
|
+
const { secretKey, curve } = deriveMnemonicSecretKey(params);
|
|
4757
|
+
const key = curve === 'ed25519' ? new EdKey(secretKey) : new ECKey(secretKey);
|
|
4758
|
+
const publicKey = key.publicKey();
|
|
4759
|
+
return {
|
|
4760
|
+
publicKey: String(publicKey),
|
|
4761
|
+
publicKeyHash: publicKey.hash(),
|
|
4762
|
+
secretKey: key.secretKey(),
|
|
4763
|
+
};
|
|
4764
|
+
}
|
|
4765
|
+
|
|
4681
4766
|
function isPOP(k) {
|
|
4682
4767
|
return 'provePossession' in k;
|
|
4683
4768
|
}
|
|
4684
4769
|
|
|
4685
4770
|
var _BLSKey_key, _BLSKey_publicKey, _BLSPublicKey_key;
|
|
4686
|
-
const bls =
|
|
4771
|
+
const bls = bls12381_js.bls12_381.longSignatures; // AKA MinPK
|
|
4687
4772
|
class BLSKey {
|
|
4688
4773
|
constructor(key, decrypt) {
|
|
4689
4774
|
_BLSKey_key.set(this, void 0);
|
|
4690
4775
|
_BLSKey_publicKey.set(this, void 0);
|
|
4691
|
-
const tmp = utils
|
|
4692
|
-
utils
|
|
4693
|
-
utils
|
|
4776
|
+
const tmp = utils.b58DecodeAndCheckPrefix(key, [
|
|
4777
|
+
utils.PrefixV2.BLS12_381EncryptedSecretKey,
|
|
4778
|
+
utils.PrefixV2.BLS12_381SecretKey,
|
|
4694
4779
|
]);
|
|
4695
4780
|
let [keyData] = tmp;
|
|
4696
4781
|
const [, prefix] = tmp;
|
|
4697
|
-
if (prefix === utils
|
|
4782
|
+
if (prefix === utils.PrefixV2.BLS12_381EncryptedSecretKey) {
|
|
4698
4783
|
if (decrypt !== undefined) {
|
|
4699
4784
|
keyData = decrypt(keyData);
|
|
4700
4785
|
}
|
|
@@ -4713,21 +4798,34 @@ zoo`.split('\n'));
|
|
|
4713
4798
|
const sig = bls.sign(point, this.sk()).toBytes();
|
|
4714
4799
|
return {
|
|
4715
4800
|
rawSignature: sig,
|
|
4716
|
-
sig: utils
|
|
4717
|
-
prefixSig: utils
|
|
4801
|
+
sig: utils.b58Encode(sig, utils.PrefixV2.GenericSignature),
|
|
4802
|
+
prefixSig: utils.b58Encode(sig, utils.PrefixV2.BLS12_381Signature),
|
|
4718
4803
|
};
|
|
4719
4804
|
}
|
|
4720
4805
|
sign(message) {
|
|
4721
|
-
return this.signDst(message, utils
|
|
4806
|
+
return this.signDst(message, utils.BLS12_381_DST);
|
|
4722
4807
|
}
|
|
4723
|
-
provePossession() {
|
|
4724
|
-
|
|
4808
|
+
provePossession(overridePublicKey) {
|
|
4809
|
+
let message = __classPrivateFieldGet(this, _BLSKey_publicKey, "f");
|
|
4810
|
+
if (overridePublicKey !== undefined) {
|
|
4811
|
+
try {
|
|
4812
|
+
if (typeof overridePublicKey !== 'string')
|
|
4813
|
+
throw new Error('Expected a public key');
|
|
4814
|
+
message = utils.b58DecodeAndCheckPrefix(overridePublicKey, [utils.PrefixV2.BLS12_381PublicKey], true);
|
|
4815
|
+
// Match Octez's compressed G1 parser, including subgroup checks and its accepted infinity encoding.
|
|
4816
|
+
bls12381_js.bls12_381.G1.Point.fromBytes(message).assertValidity();
|
|
4817
|
+
}
|
|
4818
|
+
catch {
|
|
4819
|
+
throw new utils.InvalidPublicKeyError('BLS override public key', 'Expecting a valid BLS public key');
|
|
4820
|
+
}
|
|
4821
|
+
}
|
|
4822
|
+
return this.signDst(message, utils.POP_DST);
|
|
4725
4823
|
}
|
|
4726
4824
|
publicKey() {
|
|
4727
4825
|
return new BLSPublicKey(__classPrivateFieldGet(this, _BLSKey_publicKey, "f"));
|
|
4728
4826
|
}
|
|
4729
4827
|
secretKey() {
|
|
4730
|
-
return utils
|
|
4828
|
+
return utils.b58Encode(__classPrivateFieldGet(this, _BLSKey_key, "f"), utils.PrefixV2.BLS12_381SecretKey);
|
|
4731
4829
|
}
|
|
4732
4830
|
}
|
|
4733
4831
|
_BLSKey_key = new WeakMap(), _BLSKey_publicKey = new WeakMap();
|
|
@@ -4735,7 +4833,7 @@ zoo`.split('\n'));
|
|
|
4735
4833
|
constructor(src) {
|
|
4736
4834
|
_BLSPublicKey_key.set(this, void 0);
|
|
4737
4835
|
if (typeof src === 'string') {
|
|
4738
|
-
const [key, _] = utils
|
|
4836
|
+
const [key, _] = utils.b58DecodeAndCheckPrefix(src, [utils.PrefixV2.BLS12_381PublicKey]);
|
|
4739
4837
|
__classPrivateFieldSet(this, _BLSPublicKey_key, key, "f");
|
|
4740
4838
|
}
|
|
4741
4839
|
else {
|
|
@@ -4744,14 +4842,14 @@ zoo`.split('\n'));
|
|
|
4744
4842
|
}
|
|
4745
4843
|
compare(other) {
|
|
4746
4844
|
if (other instanceof BLSPublicKey) {
|
|
4747
|
-
return utils
|
|
4845
|
+
return utils.compareArrays(this.bytes(), other.bytes());
|
|
4748
4846
|
}
|
|
4749
4847
|
else {
|
|
4750
|
-
throw new utils
|
|
4848
|
+
throw new utils.InvalidPublicKeyError('BLS key expected');
|
|
4751
4849
|
}
|
|
4752
4850
|
}
|
|
4753
4851
|
hash() {
|
|
4754
|
-
return utils
|
|
4852
|
+
return utils.b58Encode(blake2b(__classPrivateFieldGet(this, _BLSPublicKey_key, "f"), { dkLen: 20 }), utils.PrefixV2.BLS12_381PublicKeyHash);
|
|
4755
4853
|
}
|
|
4756
4854
|
bytes() {
|
|
4757
4855
|
return __classPrivateFieldGet(this, _BLSPublicKey_key, "f");
|
|
@@ -4763,7 +4861,7 @@ zoo`.split('\n'));
|
|
|
4763
4861
|
return res;
|
|
4764
4862
|
}
|
|
4765
4863
|
toString() {
|
|
4766
|
-
return utils
|
|
4864
|
+
return utils.b58Encode(__classPrivateFieldGet(this, _BLSPublicKey_key, "f"), utils.PrefixV2.BLS12_381PublicKey);
|
|
4767
4865
|
}
|
|
4768
4866
|
}
|
|
4769
4867
|
_BLSPublicKey_key = new WeakMap();
|
|
@@ -7222,17 +7320,17 @@ zoo`.split('\n'));
|
|
|
7222
7320
|
class MLDSAKey {
|
|
7223
7321
|
constructor(key, decrypt) {
|
|
7224
7322
|
_MLDSAKey_key.set(this, void 0);
|
|
7225
|
-
const [encoded, prefix] = utils
|
|
7226
|
-
utils
|
|
7227
|
-
utils
|
|
7323
|
+
const [encoded, prefix] = utils.b58DecodeAndCheckPrefix(key, [
|
|
7324
|
+
utils.PrefixV2.MLDSA44SecretKey,
|
|
7325
|
+
utils.PrefixV2.MLDSA44EncryptedSecretKey,
|
|
7228
7326
|
]);
|
|
7229
7327
|
let data = encoded;
|
|
7230
|
-
if (prefix === utils
|
|
7328
|
+
if (prefix === utils.PrefixV2.MLDSA44EncryptedSecretKey) {
|
|
7231
7329
|
if (!decrypt)
|
|
7232
7330
|
throw new Error('decryption function is not provided');
|
|
7233
7331
|
data = decrypt(data);
|
|
7234
7332
|
}
|
|
7235
|
-
if (data.length !== utils
|
|
7333
|
+
if (data.length !== utils.payloadLength[utils.PrefixV2.MLDSA44SecretKey]) {
|
|
7236
7334
|
throw new core.InvalidKeyError('Invalid ML-DSA-44 secret key length');
|
|
7237
7335
|
}
|
|
7238
7336
|
__classPrivateFieldSet(this, _MLDSAKey_key, Uint8Array.from(data), "f");
|
|
@@ -7241,43 +7339,43 @@ zoo`.split('\n'));
|
|
|
7241
7339
|
// Octez uses pure ML-DSA with an empty context over Blake2b-256(message),
|
|
7242
7340
|
// not HashML-DSA. InMemorySigner has already prepended any watermark.
|
|
7243
7341
|
const rawSignature = ml_dsa44.sign(blake2b(message, { dkLen: 32 }), __classPrivateFieldGet(this, _MLDSAKey_key, "f").subarray(0, SECRET_KEY_SIZE));
|
|
7244
|
-
const encoded = utils
|
|
7342
|
+
const encoded = utils.b58Encode(rawSignature, utils.PrefixV2.MLDSA44Signature);
|
|
7245
7343
|
return { rawSignature, sig: encoded, prefixSig: encoded };
|
|
7246
7344
|
}
|
|
7247
7345
|
publicKey() {
|
|
7248
7346
|
return new MLDSAPublicKey(__classPrivateFieldGet(this, _MLDSAKey_key, "f").subarray(SECRET_KEY_SIZE));
|
|
7249
7347
|
}
|
|
7250
7348
|
secretKey() {
|
|
7251
|
-
return utils
|
|
7349
|
+
return utils.b58Encode(__classPrivateFieldGet(this, _MLDSAKey_key, "f"), utils.PrefixV2.MLDSA44SecretKey);
|
|
7252
7350
|
}
|
|
7253
7351
|
}
|
|
7254
7352
|
_MLDSAKey_key = new WeakMap();
|
|
7255
7353
|
class MLDSAPublicKey {
|
|
7256
7354
|
constructor(src) {
|
|
7257
7355
|
_MLDSAPublicKey_key.set(this, void 0);
|
|
7258
|
-
const key = typeof src === 'string' ? utils
|
|
7259
|
-
if (key.length !== utils
|
|
7260
|
-
throw new utils
|
|
7356
|
+
const key = typeof src === 'string' ? utils.b58DecodeAndCheckPrefix(src, [utils.PrefixV2.MLDSA44PublicKey])[0] : src;
|
|
7357
|
+
if (key.length !== utils.payloadLength[utils.PrefixV2.MLDSA44PublicKey]) {
|
|
7358
|
+
throw new utils.InvalidPublicKeyError('Invalid ML-DSA-44 public key length');
|
|
7261
7359
|
}
|
|
7262
7360
|
__classPrivateFieldSet(this, _MLDSAPublicKey_key, Uint8Array.from(key), "f");
|
|
7263
7361
|
}
|
|
7264
7362
|
compare(other) {
|
|
7265
7363
|
if (!(other instanceof MLDSAPublicKey)) {
|
|
7266
|
-
throw new utils
|
|
7364
|
+
throw new utils.InvalidPublicKeyError('ML-DSA-44 key expected');
|
|
7267
7365
|
}
|
|
7268
|
-
return utils
|
|
7366
|
+
return utils.compareArrays(__classPrivateFieldGet(this, _MLDSAPublicKey_key, "f"), __classPrivateFieldGet(other, _MLDSAPublicKey_key, "f"));
|
|
7269
7367
|
}
|
|
7270
7368
|
hash() {
|
|
7271
|
-
return utils
|
|
7369
|
+
return utils.b58Encode(blake2b(__classPrivateFieldGet(this, _MLDSAPublicKey_key, "f"), { dkLen: 20 }), utils.PrefixV2.MLDSA44PublicKeyHash);
|
|
7272
7370
|
}
|
|
7273
7371
|
bytes() {
|
|
7274
7372
|
return __classPrivateFieldGet(this, _MLDSAPublicKey_key, "f").slice();
|
|
7275
7373
|
}
|
|
7276
7374
|
toProtocol() {
|
|
7277
|
-
return utils
|
|
7375
|
+
return utils.mergebuf(new Uint8Array([4]), __classPrivateFieldGet(this, _MLDSAPublicKey_key, "f"));
|
|
7278
7376
|
}
|
|
7279
7377
|
toString() {
|
|
7280
|
-
return utils
|
|
7378
|
+
return utils.b58Encode(__classPrivateFieldGet(this, _MLDSAPublicKey_key, "f"), utils.PrefixV2.MLDSA44PublicKey);
|
|
7281
7379
|
}
|
|
7282
7380
|
}
|
|
7283
7381
|
_MLDSAPublicKey_key = new WeakMap();
|
|
@@ -7295,7 +7393,7 @@ zoo`.split('\n'));
|
|
|
7295
7393
|
throw new InvalidMnemonicError();
|
|
7296
7394
|
}
|
|
7297
7395
|
const seed = mnemonicToSeedSync(mnemonic, `${email}${password}`);
|
|
7298
|
-
const key = utils
|
|
7396
|
+
const key = utils.b58Encode(seed.subarray(0, 32), utils.PrefixV2.Ed25519Seed);
|
|
7299
7397
|
return new InMemorySigner(key);
|
|
7300
7398
|
}
|
|
7301
7399
|
static async fromSecretKey(key, passphrase) {
|
|
@@ -7307,15 +7405,9 @@ zoo`.split('\n'));
|
|
|
7307
7405
|
* @returns InMemorySigner
|
|
7308
7406
|
* @throws InvalidMnemonicError
|
|
7309
7407
|
*/
|
|
7310
|
-
static fromMnemonic(
|
|
7311
|
-
|
|
7312
|
-
|
|
7313
|
-
// avoiding exposing mnemonic again in case of mistake making invalid
|
|
7314
|
-
throw new InvalidMnemonicError();
|
|
7315
|
-
}
|
|
7316
|
-
const seed = mnemonicToSeedSync(mnemonic, password);
|
|
7317
|
-
const sk = generateSecretKey(seed, derivationPath, curve);
|
|
7318
|
-
return new InMemorySigner(sk);
|
|
7408
|
+
static fromMnemonic(params) {
|
|
7409
|
+
const { secretKey } = deriveMnemonicSecretKey(params);
|
|
7410
|
+
return new InMemorySigner(secretKey);
|
|
7319
7411
|
}
|
|
7320
7412
|
/**
|
|
7321
7413
|
*
|
|
@@ -7327,32 +7419,32 @@ zoo`.split('\n'));
|
|
|
7327
7419
|
constructor(key, passphrase) {
|
|
7328
7420
|
_InMemorySigner_key.set(this, void 0);
|
|
7329
7421
|
const keyPrefixes = [
|
|
7330
|
-
utils
|
|
7331
|
-
utils
|
|
7332
|
-
utils
|
|
7333
|
-
utils
|
|
7334
|
-
utils
|
|
7335
|
-
utils
|
|
7336
|
-
utils
|
|
7337
|
-
utils
|
|
7338
|
-
utils
|
|
7339
|
-
utils
|
|
7340
|
-
utils
|
|
7422
|
+
utils.PrefixV2.Ed25519EncryptedSeed,
|
|
7423
|
+
utils.PrefixV2.Ed25519Seed,
|
|
7424
|
+
utils.PrefixV2.Ed25519SecretKey,
|
|
7425
|
+
utils.PrefixV2.Secp256k1EncryptedSecretKey,
|
|
7426
|
+
utils.PrefixV2.Secp256k1SecretKey,
|
|
7427
|
+
utils.PrefixV2.P256EncryptedSecretKey,
|
|
7428
|
+
utils.PrefixV2.P256SecretKey,
|
|
7429
|
+
utils.PrefixV2.BLS12_381EncryptedSecretKey,
|
|
7430
|
+
utils.PrefixV2.BLS12_381SecretKey,
|
|
7431
|
+
utils.PrefixV2.MLDSA44EncryptedSecretKey,
|
|
7432
|
+
utils.PrefixV2.MLDSA44SecretKey,
|
|
7341
7433
|
];
|
|
7342
7434
|
const pre = (() => {
|
|
7343
7435
|
try {
|
|
7344
|
-
const [, pre] = utils
|
|
7436
|
+
const [, pre] = utils.b58DecodeAndCheckPrefix(key, keyPrefixes);
|
|
7345
7437
|
return pre;
|
|
7346
7438
|
}
|
|
7347
7439
|
catch {
|
|
7348
7440
|
throw new core.InvalidKeyError(`Invalid private key, expecting one of the following prefixes '${keyPrefixes}'.`);
|
|
7349
7441
|
}
|
|
7350
7442
|
})();
|
|
7351
|
-
const encrypted = pre === utils
|
|
7352
|
-
pre === utils
|
|
7353
|
-
pre === utils
|
|
7354
|
-
pre === utils
|
|
7355
|
-
pre === utils
|
|
7443
|
+
const encrypted = pre === utils.PrefixV2.Ed25519EncryptedSeed ||
|
|
7444
|
+
pre === utils.PrefixV2.Secp256k1EncryptedSecretKey ||
|
|
7445
|
+
pre === utils.PrefixV2.P256EncryptedSecretKey ||
|
|
7446
|
+
pre === utils.PrefixV2.BLS12_381EncryptedSecretKey ||
|
|
7447
|
+
pre === utils.PrefixV2.MLDSA44EncryptedSecretKey;
|
|
7356
7448
|
let decrypt;
|
|
7357
7449
|
if (encrypted) {
|
|
7358
7450
|
if (!passphrase) {
|
|
@@ -7376,23 +7468,23 @@ zoo`.split('\n'));
|
|
|
7376
7468
|
};
|
|
7377
7469
|
}
|
|
7378
7470
|
switch (pre) {
|
|
7379
|
-
case utils
|
|
7380
|
-
case utils
|
|
7381
|
-
case utils
|
|
7471
|
+
case utils.PrefixV2.Ed25519EncryptedSeed:
|
|
7472
|
+
case utils.PrefixV2.Ed25519Seed:
|
|
7473
|
+
case utils.PrefixV2.Ed25519SecretKey:
|
|
7382
7474
|
__classPrivateFieldSet(this, _InMemorySigner_key, new EdKey(key, decrypt), "f");
|
|
7383
7475
|
break;
|
|
7384
|
-
case utils
|
|
7385
|
-
case utils
|
|
7386
|
-
case utils
|
|
7387
|
-
case utils
|
|
7476
|
+
case utils.PrefixV2.Secp256k1EncryptedSecretKey:
|
|
7477
|
+
case utils.PrefixV2.Secp256k1SecretKey:
|
|
7478
|
+
case utils.PrefixV2.P256EncryptedSecretKey:
|
|
7479
|
+
case utils.PrefixV2.P256SecretKey:
|
|
7388
7480
|
__classPrivateFieldSet(this, _InMemorySigner_key, new ECKey(key, decrypt), "f");
|
|
7389
7481
|
break;
|
|
7390
|
-
case utils
|
|
7391
|
-
case utils
|
|
7482
|
+
case utils.PrefixV2.BLS12_381EncryptedSecretKey:
|
|
7483
|
+
case utils.PrefixV2.BLS12_381SecretKey:
|
|
7392
7484
|
__classPrivateFieldSet(this, _InMemorySigner_key, new BLSKey(key, decrypt), "f");
|
|
7393
7485
|
break;
|
|
7394
|
-
case utils
|
|
7395
|
-
case utils
|
|
7486
|
+
case utils.PrefixV2.MLDSA44EncryptedSecretKey:
|
|
7487
|
+
case utils.PrefixV2.MLDSA44SecretKey:
|
|
7396
7488
|
__classPrivateFieldSet(this, _InMemorySigner_key, new MLDSAKey(key, decrypt), "f");
|
|
7397
7489
|
break;
|
|
7398
7490
|
}
|
|
@@ -7403,25 +7495,25 @@ zoo`.split('\n'));
|
|
|
7403
7495
|
* @param watermark Watermark to append to the bytes
|
|
7404
7496
|
*/
|
|
7405
7497
|
async sign(message, watermark) {
|
|
7406
|
-
const msg = typeof message == 'string' ? utils
|
|
7407
|
-
const watermarkMsg = watermark !== undefined ? utils
|
|
7498
|
+
const msg = typeof message == 'string' ? utils.hex2buf(message) : message;
|
|
7499
|
+
const watermarkMsg = watermark !== undefined ? utils.mergebuf(watermark, msg) : msg;
|
|
7408
7500
|
const { rawSignature, sig: signature, prefixSig: prefixedSignature, } = await __classPrivateFieldGet(this, _InMemorySigner_key, "f").sign(watermarkMsg);
|
|
7409
7501
|
// Variable-length operation signatures carry the signature-prefix marker
|
|
7410
7502
|
// and scheme tag before the complete signature payload (Octez signature_v3).
|
|
7411
7503
|
const signatureTag = __classPrivateFieldGet(this, _InMemorySigner_key, "f") instanceof MLDSAKey ? 4 : __classPrivateFieldGet(this, _InMemorySigner_key, "f") instanceof BLSKey ? 3 : undefined;
|
|
7412
7504
|
const signatureBytes = signatureTag === undefined
|
|
7413
7505
|
? rawSignature
|
|
7414
|
-
: utils
|
|
7506
|
+
: utils.mergebuf(new Uint8Array([255, signatureTag]), rawSignature);
|
|
7415
7507
|
return {
|
|
7416
|
-
bytes: utils
|
|
7508
|
+
bytes: utils.buf2hex(msg),
|
|
7417
7509
|
sig: signature,
|
|
7418
7510
|
prefixSig: prefixedSignature,
|
|
7419
|
-
sbytes: utils
|
|
7511
|
+
sbytes: utils.buf2hex(utils.mergebuf(msg, signatureBytes)),
|
|
7420
7512
|
};
|
|
7421
7513
|
}
|
|
7422
|
-
async provePossession() {
|
|
7514
|
+
async provePossession(overridePublicKey) {
|
|
7423
7515
|
if (isPOP(__classPrivateFieldGet(this, _InMemorySigner_key, "f"))) {
|
|
7424
|
-
return __classPrivateFieldGet(this, _InMemorySigner_key, "f").provePossession();
|
|
7516
|
+
return __classPrivateFieldGet(this, _InMemorySigner_key, "f").provePossession(overridePublicKey);
|
|
7425
7517
|
}
|
|
7426
7518
|
else {
|
|
7427
7519
|
throw new core.ProhibitedActionError('Only BLS keys can prove possession');
|
|
@@ -7451,41 +7543,46 @@ zoo`.split('\n'));
|
|
|
7451
7543
|
}
|
|
7452
7544
|
_InMemorySigner_key = new WeakMap();
|
|
7453
7545
|
function publicKeyFromString(src) {
|
|
7454
|
-
const [keyData, pre] = utils
|
|
7455
|
-
utils
|
|
7456
|
-
utils
|
|
7457
|
-
utils
|
|
7458
|
-
utils
|
|
7459
|
-
utils
|
|
7546
|
+
const [keyData, pre] = utils.b58DecodeAndCheckPrefix(src, [
|
|
7547
|
+
utils.PrefixV2.Ed25519PublicKey,
|
|
7548
|
+
utils.PrefixV2.Secp256k1PublicKey,
|
|
7549
|
+
utils.PrefixV2.P256PublicKey,
|
|
7550
|
+
utils.PrefixV2.BLS12_381PublicKey,
|
|
7551
|
+
utils.PrefixV2.MLDSA44PublicKey,
|
|
7460
7552
|
]);
|
|
7461
7553
|
switch (pre) {
|
|
7462
|
-
case utils
|
|
7554
|
+
case utils.PrefixV2.Ed25519PublicKey:
|
|
7463
7555
|
return new EdPublicKey(keyData);
|
|
7464
|
-
case utils
|
|
7556
|
+
case utils.PrefixV2.Secp256k1PublicKey:
|
|
7465
7557
|
return new ECPublicKey(keyData, 'secp256k1');
|
|
7466
|
-
case utils
|
|
7558
|
+
case utils.PrefixV2.P256PublicKey:
|
|
7467
7559
|
return new ECPublicKey(keyData, 'p256');
|
|
7468
|
-
case utils
|
|
7560
|
+
case utils.PrefixV2.BLS12_381PublicKey:
|
|
7469
7561
|
return new BLSPublicKey(keyData);
|
|
7470
|
-
case utils
|
|
7562
|
+
case utils.PrefixV2.MLDSA44PublicKey:
|
|
7471
7563
|
return new MLDSAPublicKey(keyData);
|
|
7472
7564
|
}
|
|
7473
7565
|
}
|
|
7474
7566
|
|
|
7475
7567
|
// IMPORTANT: THIS FILE IS AUTO GENERATED! DO NOT MANUALLY EDIT!
|
|
7476
7568
|
const VERSION = {
|
|
7477
|
-
"commitHash": "
|
|
7478
|
-
"version": "25.
|
|
7569
|
+
"commitHash": "78ca0b9cb430f230c3230cd28b7e89a310e84c15",
|
|
7570
|
+
"version": "25.2.0-beta0"
|
|
7479
7571
|
};
|
|
7480
7572
|
|
|
7481
7573
|
exports.ECDSA = ecdsa;
|
|
7482
7574
|
exports.Ed25519 = ed25519;
|
|
7483
7575
|
exports.Hard = Hard;
|
|
7484
7576
|
exports.InMemorySigner = InMemorySigner;
|
|
7577
|
+
exports.InvalidMnemonicError = InvalidMnemonicError;
|
|
7485
7578
|
exports.InvalidPassphraseError = InvalidPassphraseError;
|
|
7486
7579
|
exports.Path = Path;
|
|
7487
7580
|
exports.VERSION = VERSION;
|
|
7581
|
+
exports.deriveKeyFromMnemonic = deriveKeyFromMnemonic;
|
|
7582
|
+
exports.entropyToMnemonic = entropyToMnemonic;
|
|
7583
|
+
exports.generateMnemonic = generateMnemonic;
|
|
7488
7584
|
exports.generateSecretKey = generateSecretKey;
|
|
7585
|
+
exports.mnemonicToEntropy = mnemonicToEntropy;
|
|
7489
7586
|
exports.publicKeyFromString = publicKeyFromString;
|
|
7490
7587
|
|
|
7491
7588
|
}));
|