@twin.org/crypto 0.0.2-next.20 → 0.0.2-next.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +124 -143
- package/dist/esm/index.mjs +124 -143
- package/dist/types/address/bech32.d.ts +4 -0
- package/dist/types/address/bip44.d.ts +4 -0
- package/dist/types/ciphers/chaCha20Poly1305.d.ts +4 -0
- package/dist/types/curves/ed25519.d.ts +4 -0
- package/dist/types/curves/secp256k1.d.ts +4 -0
- package/dist/types/curves/x25519.d.ts +4 -0
- package/dist/types/curves/zip215.d.ts +4 -0
- package/dist/types/hashes/blake2b.d.ts +4 -0
- package/dist/types/hashes/blake3.d.ts +4 -0
- package/dist/types/hashes/hmacSha1.d.ts +4 -0
- package/dist/types/hashes/hmacSha256.d.ts +4 -0
- package/dist/types/hashes/hmacSha512.d.ts +4 -0
- package/dist/types/hashes/pbkdf2.d.ts +4 -0
- package/dist/types/hashes/sha1.d.ts +4 -0
- package/dist/types/hashes/sha256.d.ts +4 -0
- package/dist/types/hashes/sha3.d.ts +4 -0
- package/dist/types/hashes/sha512.d.ts +4 -0
- package/dist/types/helpers/pemHelper.d.ts +4 -0
- package/dist/types/keys/bip39.d.ts +4 -0
- package/dist/types/keys/slip0010.d.ts +4 -0
- package/dist/types/otp/hotp.d.ts +4 -0
- package/docs/changelog.md +38 -0
- package/docs/reference/classes/Bech32.md +8 -0
- package/docs/reference/classes/Bip39.md +8 -0
- package/docs/reference/classes/Bip44.md +8 -0
- package/docs/reference/classes/Blake2b.md +8 -0
- package/docs/reference/classes/Blake3.md +8 -0
- package/docs/reference/classes/ChaCha20Poly1305.md +8 -0
- package/docs/reference/classes/Ed25519.md +8 -0
- package/docs/reference/classes/HmacSha1.md +8 -0
- package/docs/reference/classes/HmacSha256.md +8 -0
- package/docs/reference/classes/HmacSha512.md +8 -0
- package/docs/reference/classes/Hotp.md +8 -0
- package/docs/reference/classes/Pbkdf2.md +8 -0
- package/docs/reference/classes/PemHelper.md +8 -0
- package/docs/reference/classes/Secp256k1.md +8 -0
- package/docs/reference/classes/Sha1.md +8 -0
- package/docs/reference/classes/Sha256.md +8 -0
- package/docs/reference/classes/Sha3.md +8 -0
- package/docs/reference/classes/Sha512.md +8 -0
- package/docs/reference/classes/Slip0010.md +8 -0
- package/docs/reference/classes/X25519.md +8 -0
- package/docs/reference/classes/Zip215.md +8 -0
- package/locales/en.json +9 -28
- package/package.json +10 -6
package/dist/esm/index.mjs
CHANGED
|
@@ -24,9 +24,8 @@ import * as otp from 'micro-key-producer/otp.js';
|
|
|
24
24
|
class Bech32 {
|
|
25
25
|
/**
|
|
26
26
|
* Runtime name for the class.
|
|
27
|
-
* @internal
|
|
28
27
|
*/
|
|
29
|
-
static
|
|
28
|
+
static CLASS_NAME = "Bech32";
|
|
30
29
|
/**
|
|
31
30
|
* Encode the buffer.
|
|
32
31
|
* @param humanReadablePart The header.
|
|
@@ -34,8 +33,8 @@ class Bech32 {
|
|
|
34
33
|
* @returns The encoded data.
|
|
35
34
|
*/
|
|
36
35
|
static encode(humanReadablePart, data) {
|
|
37
|
-
Guards.stringValue(Bech32.
|
|
38
|
-
Guards.uint8Array(Bech32.
|
|
36
|
+
Guards.stringValue(Bech32.CLASS_NAME, "humanReadablePart", humanReadablePart);
|
|
37
|
+
Guards.uint8Array(Bech32.CLASS_NAME, "data", data);
|
|
39
38
|
return bech32.encode(humanReadablePart, bech32.toWords(data));
|
|
40
39
|
}
|
|
41
40
|
/**
|
|
@@ -45,7 +44,7 @@ class Bech32 {
|
|
|
45
44
|
* @throws An error if the decoding fails.
|
|
46
45
|
*/
|
|
47
46
|
static decode(bech) {
|
|
48
|
-
Guards.stringValue(Bech32.
|
|
47
|
+
Guards.stringValue(Bech32.CLASS_NAME, "bech", bech);
|
|
49
48
|
try {
|
|
50
49
|
const result = bech32.decodeToBytes(bech);
|
|
51
50
|
return {
|
|
@@ -55,19 +54,19 @@ class Bech32 {
|
|
|
55
54
|
}
|
|
56
55
|
catch (err) {
|
|
57
56
|
if (BaseError.isErrorMessage(err, /checksum/)) {
|
|
58
|
-
throw new GeneralError(Bech32.
|
|
57
|
+
throw new GeneralError(Bech32.CLASS_NAME, "invalidChecksum", { bech32 });
|
|
59
58
|
}
|
|
60
59
|
else if (BaseError.isErrorMessage(err, /between prefix and data only/i)) {
|
|
61
|
-
throw new GeneralError(Bech32.
|
|
60
|
+
throw new GeneralError(Bech32.CLASS_NAME, "separatorMisused", { bech32 });
|
|
62
61
|
}
|
|
63
62
|
else if (BaseError.isErrorMessage(err, /lowercase or uppercase/i)) {
|
|
64
|
-
throw new GeneralError(Bech32.
|
|
63
|
+
throw new GeneralError(Bech32.CLASS_NAME, "lowerUpper", { bech32 });
|
|
65
64
|
}
|
|
66
65
|
else if (BaseError.isErrorMessage(err, /must be at least/i) ||
|
|
67
66
|
BaseError.isErrorMessage(err, /wrong string length/i)) {
|
|
68
|
-
throw new GeneralError(Bech32.
|
|
67
|
+
throw new GeneralError(Bech32.CLASS_NAME, "dataTooShort", { bech32 });
|
|
69
68
|
}
|
|
70
|
-
throw new GeneralError(Bech32.
|
|
69
|
+
throw new GeneralError(Bech32.CLASS_NAME, "decodeFailed", { bech32 }, err);
|
|
71
70
|
}
|
|
72
71
|
}
|
|
73
72
|
/**
|
|
@@ -103,9 +102,8 @@ class Ed25519 {
|
|
|
103
102
|
static PUBLIC_KEY_SIZE = 32;
|
|
104
103
|
/**
|
|
105
104
|
* Runtime name for the class.
|
|
106
|
-
* @internal
|
|
107
105
|
*/
|
|
108
|
-
static
|
|
106
|
+
static CLASS_NAME = "Ed25519";
|
|
109
107
|
/**
|
|
110
108
|
* Public returns the PublicKey corresponding to private.
|
|
111
109
|
* @param privateKey The private key to get the corresponding public key.
|
|
@@ -113,9 +111,9 @@ class Ed25519 {
|
|
|
113
111
|
* @throws Error if the private key is not the correct length.
|
|
114
112
|
*/
|
|
115
113
|
static publicKeyFromPrivateKey(privateKey) {
|
|
116
|
-
Guards.uint8Array(Ed25519.
|
|
114
|
+
Guards.uint8Array(Ed25519.CLASS_NAME, "privateKey", privateKey);
|
|
117
115
|
if (privateKey.length !== Ed25519.PRIVATE_KEY_SIZE) {
|
|
118
|
-
throw new GeneralError(Ed25519.
|
|
116
|
+
throw new GeneralError(Ed25519.CLASS_NAME, "privateKeyLength", {
|
|
119
117
|
requiredSize: Ed25519.PRIVATE_KEY_SIZE,
|
|
120
118
|
actualSize: privateKey.length
|
|
121
119
|
});
|
|
@@ -130,10 +128,10 @@ class Ed25519 {
|
|
|
130
128
|
* @throws Error if the private key is not the correct length.
|
|
131
129
|
*/
|
|
132
130
|
static sign(privateKey, block) {
|
|
133
|
-
Guards.uint8Array(Ed25519.
|
|
134
|
-
Guards.uint8Array(Ed25519.
|
|
131
|
+
Guards.uint8Array(Ed25519.CLASS_NAME, "privateKey", privateKey);
|
|
132
|
+
Guards.uint8Array(Ed25519.CLASS_NAME, "block", block);
|
|
135
133
|
if (privateKey.length !== Ed25519.PRIVATE_KEY_SIZE) {
|
|
136
|
-
throw new GeneralError(Ed25519.
|
|
134
|
+
throw new GeneralError(Ed25519.CLASS_NAME, "privateKeyLength", {
|
|
137
135
|
requiredSize: Ed25519.PRIVATE_KEY_SIZE,
|
|
138
136
|
actualSize: privateKey ? privateKey.length : 0
|
|
139
137
|
});
|
|
@@ -149,11 +147,11 @@ class Ed25519 {
|
|
|
149
147
|
* @throws Error if the public key is not the correct length.
|
|
150
148
|
*/
|
|
151
149
|
static verify(publicKey, block, signature) {
|
|
152
|
-
Guards.uint8Array(Ed25519.
|
|
153
|
-
Guards.uint8Array(Ed25519.
|
|
154
|
-
Guards.uint8Array(Ed25519.
|
|
150
|
+
Guards.uint8Array(Ed25519.CLASS_NAME, "publicKey", publicKey);
|
|
151
|
+
Guards.uint8Array(Ed25519.CLASS_NAME, "block", block);
|
|
152
|
+
Guards.uint8Array(Ed25519.CLASS_NAME, "signature", signature);
|
|
155
153
|
if (publicKey.length !== Ed25519.PUBLIC_KEY_SIZE) {
|
|
156
|
-
throw new GeneralError(Ed25519.
|
|
154
|
+
throw new GeneralError(Ed25519.CLASS_NAME, "publicKeyLength", {
|
|
157
155
|
requiredSize: Ed25519.PUBLIC_KEY_SIZE,
|
|
158
156
|
actualSize: publicKey ? publicKey.length : 0
|
|
159
157
|
});
|
|
@@ -171,9 +169,9 @@ class Ed25519 {
|
|
|
171
169
|
* @returns The private key in PKCS8 format.
|
|
172
170
|
*/
|
|
173
171
|
static async privateKeyToPkcs8(privateKey) {
|
|
174
|
-
Guards.uint8Array(Ed25519.
|
|
172
|
+
Guards.uint8Array(Ed25519.CLASS_NAME, "privateKey", privateKey);
|
|
175
173
|
if (privateKey.length !== Ed25519.PRIVATE_KEY_SIZE) {
|
|
176
|
-
throw new GeneralError(Ed25519.
|
|
174
|
+
throw new GeneralError(Ed25519.CLASS_NAME, "privateKeyLength", {
|
|
177
175
|
requiredSize: Ed25519.PRIVATE_KEY_SIZE,
|
|
178
176
|
actualSize: privateKey.length
|
|
179
177
|
});
|
|
@@ -192,7 +190,7 @@ class Ed25519 {
|
|
|
192
190
|
* @returns The raw private key.
|
|
193
191
|
*/
|
|
194
192
|
static async pkcs8ToPrivateKey(cryptoKey) {
|
|
195
|
-
Guards.defined(Ed25519.
|
|
193
|
+
Guards.defined(Ed25519.CLASS_NAME, "cryptoKey", cryptoKey);
|
|
196
194
|
// crypto.subtle.exportKey does not support Ed25519 keys in raw format.
|
|
197
195
|
// so we export as PKCS8 and remove the ASN.1 sequence prefix.
|
|
198
196
|
const pkcs8Bytes = await crypto.subtle.exportKey("pkcs8", cryptoKey);
|
|
@@ -216,9 +214,8 @@ class Secp256k1 {
|
|
|
216
214
|
static PUBLIC_KEY_SIZE = 33;
|
|
217
215
|
/**
|
|
218
216
|
* Runtime name for the class.
|
|
219
|
-
* @internal
|
|
220
217
|
*/
|
|
221
|
-
static
|
|
218
|
+
static CLASS_NAME = "Secp256k1";
|
|
222
219
|
/**
|
|
223
220
|
* Public returns the PublicKey corresponding to private.
|
|
224
221
|
* @param privateKey The private key to get the corresponding public key.
|
|
@@ -226,9 +223,9 @@ class Secp256k1 {
|
|
|
226
223
|
* @throws Error if the private key is not the correct length.
|
|
227
224
|
*/
|
|
228
225
|
static publicKeyFromPrivateKey(privateKey) {
|
|
229
|
-
Guards.uint8Array(Secp256k1.
|
|
226
|
+
Guards.uint8Array(Secp256k1.CLASS_NAME, "privateKey", privateKey);
|
|
230
227
|
if (privateKey.length !== Secp256k1.PRIVATE_KEY_SIZE) {
|
|
231
|
-
throw new GeneralError(Secp256k1.
|
|
228
|
+
throw new GeneralError(Secp256k1.CLASS_NAME, "privateKeyLength", {
|
|
232
229
|
requiredSize: Secp256k1.PRIVATE_KEY_SIZE,
|
|
233
230
|
actualSize: privateKey.length
|
|
234
231
|
});
|
|
@@ -243,10 +240,10 @@ class Secp256k1 {
|
|
|
243
240
|
* @throws Error if the private key is not the correct length.
|
|
244
241
|
*/
|
|
245
242
|
static sign(privateKey, block) {
|
|
246
|
-
Guards.uint8Array(Secp256k1.
|
|
247
|
-
Guards.uint8Array(Secp256k1.
|
|
243
|
+
Guards.uint8Array(Secp256k1.CLASS_NAME, "privateKey", privateKey);
|
|
244
|
+
Guards.uint8Array(Secp256k1.CLASS_NAME, "block", block);
|
|
248
245
|
if (privateKey.length !== Secp256k1.PRIVATE_KEY_SIZE) {
|
|
249
|
-
throw new GeneralError(Secp256k1.
|
|
246
|
+
throw new GeneralError(Secp256k1.CLASS_NAME, "privateKeyLength", {
|
|
250
247
|
requiredSize: Secp256k1.PRIVATE_KEY_SIZE,
|
|
251
248
|
actualSize: privateKey.length
|
|
252
249
|
});
|
|
@@ -263,11 +260,11 @@ class Secp256k1 {
|
|
|
263
260
|
* @throws Error if the public key is not the correct length.
|
|
264
261
|
*/
|
|
265
262
|
static verify(publicKey, block, signature) {
|
|
266
|
-
Guards.uint8Array(Secp256k1.
|
|
267
|
-
Guards.uint8Array(Secp256k1.
|
|
268
|
-
Guards.uint8Array(Secp256k1.
|
|
263
|
+
Guards.uint8Array(Secp256k1.CLASS_NAME, "publicKey", publicKey);
|
|
264
|
+
Guards.uint8Array(Secp256k1.CLASS_NAME, "block", block);
|
|
265
|
+
Guards.uint8Array(Secp256k1.CLASS_NAME, "signature", signature);
|
|
269
266
|
if (publicKey.length !== Secp256k1.PUBLIC_KEY_SIZE) {
|
|
270
|
-
throw new GeneralError(Secp256k1.
|
|
267
|
+
throw new GeneralError(Secp256k1.CLASS_NAME, "publicKeyLength", {
|
|
271
268
|
requiredSize: Secp256k1.PUBLIC_KEY_SIZE,
|
|
272
269
|
actualSize: publicKey ? publicKey.length : 0
|
|
273
270
|
});
|
|
@@ -301,9 +298,8 @@ class Blake2b {
|
|
|
301
298
|
static SIZE_512 = 64;
|
|
302
299
|
/**
|
|
303
300
|
* Runtime name for the class.
|
|
304
|
-
* @internal
|
|
305
301
|
*/
|
|
306
|
-
static
|
|
302
|
+
static CLASS_NAME = "Blake2b";
|
|
307
303
|
/**
|
|
308
304
|
* The instance of the hash.
|
|
309
305
|
* @internal
|
|
@@ -328,7 +324,7 @@ class Blake2b {
|
|
|
328
324
|
* @returns The sum 160 of the block.
|
|
329
325
|
*/
|
|
330
326
|
static sum160(block, key) {
|
|
331
|
-
Guards.uint8Array(Blake2b.
|
|
327
|
+
Guards.uint8Array(Blake2b.CLASS_NAME, "block", block);
|
|
332
328
|
return new Blake2b(Blake2b.SIZE_160, key).update(block).digest();
|
|
333
329
|
}
|
|
334
330
|
/**
|
|
@@ -338,7 +334,7 @@ class Blake2b {
|
|
|
338
334
|
* @returns The sum 256 of the block.
|
|
339
335
|
*/
|
|
340
336
|
static sum256(block, key) {
|
|
341
|
-
Guards.uint8Array(Blake2b.
|
|
337
|
+
Guards.uint8Array(Blake2b.CLASS_NAME, "block", block);
|
|
342
338
|
return new Blake2b(Blake2b.SIZE_256, key).update(block).digest();
|
|
343
339
|
}
|
|
344
340
|
/**
|
|
@@ -348,7 +344,7 @@ class Blake2b {
|
|
|
348
344
|
* @returns The sum 512 of the block.
|
|
349
345
|
*/
|
|
350
346
|
static sum512(block, key) {
|
|
351
|
-
Guards.uint8Array(Blake2b.
|
|
347
|
+
Guards.uint8Array(Blake2b.CLASS_NAME, "block", block);
|
|
352
348
|
return new Blake2b(Blake2b.SIZE_512, key).update(block).digest();
|
|
353
349
|
}
|
|
354
350
|
/**
|
|
@@ -357,7 +353,7 @@ class Blake2b {
|
|
|
357
353
|
* @returns The instance for chaining.
|
|
358
354
|
*/
|
|
359
355
|
update(block) {
|
|
360
|
-
Guards.uint8Array(Blake2b.
|
|
356
|
+
Guards.uint8Array(Blake2b.CLASS_NAME, "block", block);
|
|
361
357
|
this._instance.update(block);
|
|
362
358
|
return this;
|
|
363
359
|
}
|
|
@@ -468,9 +464,8 @@ const KeyType = {
|
|
|
468
464
|
class Slip0010 {
|
|
469
465
|
/**
|
|
470
466
|
* Runtime name for the class.
|
|
471
|
-
* @internal
|
|
472
467
|
*/
|
|
473
|
-
static
|
|
468
|
+
static CLASS_NAME = "Slip0010";
|
|
474
469
|
/**
|
|
475
470
|
* Get the master key from the seed.
|
|
476
471
|
* @param seed The seed to generate the master key from.
|
|
@@ -489,7 +484,7 @@ class Slip0010 {
|
|
|
489
484
|
};
|
|
490
485
|
}
|
|
491
486
|
catch (error) {
|
|
492
|
-
throw new GeneralError(Slip0010.
|
|
487
|
+
throw new GeneralError(Slip0010.CLASS_NAME, "invalidSeed", { seed: Converter.bytesToUtf8(seed) }, error);
|
|
493
488
|
}
|
|
494
489
|
}
|
|
495
490
|
/**
|
|
@@ -545,9 +540,8 @@ class Slip0010 {
|
|
|
545
540
|
class Bip44 {
|
|
546
541
|
/**
|
|
547
542
|
* Runtime name for the class.
|
|
548
|
-
* @internal
|
|
549
543
|
*/
|
|
550
|
-
static
|
|
544
|
+
static CLASS_NAME = "Bip44";
|
|
551
545
|
/**
|
|
552
546
|
* Generate a bip44 key pair from the seed and parts.
|
|
553
547
|
* @param seed The account seed.
|
|
@@ -576,7 +570,7 @@ class Bip44 {
|
|
|
576
570
|
publicKey
|
|
577
571
|
};
|
|
578
572
|
}
|
|
579
|
-
throw new GeneralError(Bip44.
|
|
573
|
+
throw new GeneralError(Bip44.CLASS_NAME, "unsupportedKeyType", { keyType });
|
|
580
574
|
}
|
|
581
575
|
/**
|
|
582
576
|
* Generate a bip44 path based on all its parts.
|
|
@@ -651,9 +645,8 @@ class Bip44 {
|
|
|
651
645
|
class ChaCha20Poly1305 {
|
|
652
646
|
/**
|
|
653
647
|
* Runtime name for the class.
|
|
654
|
-
* @internal
|
|
655
648
|
*/
|
|
656
|
-
static
|
|
649
|
+
static CLASS_NAME = "ChaCha20Poly1305";
|
|
657
650
|
/**
|
|
658
651
|
* The cipher instance.
|
|
659
652
|
* @internal
|
|
@@ -666,8 +659,8 @@ class ChaCha20Poly1305 {
|
|
|
666
659
|
* @param aad The additional authenticated data.
|
|
667
660
|
*/
|
|
668
661
|
constructor(key, nonce, aad) {
|
|
669
|
-
Guards.uint8Array(ChaCha20Poly1305.
|
|
670
|
-
Guards.uint8Array(ChaCha20Poly1305.
|
|
662
|
+
Guards.uint8Array(ChaCha20Poly1305.CLASS_NAME, "key", key);
|
|
663
|
+
Guards.uint8Array(ChaCha20Poly1305.CLASS_NAME, "nonce", nonce);
|
|
671
664
|
this._instance = chacha20poly1305(key, nonce, aad);
|
|
672
665
|
}
|
|
673
666
|
/**
|
|
@@ -676,7 +669,7 @@ class ChaCha20Poly1305 {
|
|
|
676
669
|
* @returns The block encrypted.
|
|
677
670
|
*/
|
|
678
671
|
encrypt(block) {
|
|
679
|
-
Guards.uint8Array(ChaCha20Poly1305.
|
|
672
|
+
Guards.uint8Array(ChaCha20Poly1305.CLASS_NAME, "block", block);
|
|
680
673
|
return this._instance.encrypt(block);
|
|
681
674
|
}
|
|
682
675
|
/**
|
|
@@ -685,7 +678,7 @@ class ChaCha20Poly1305 {
|
|
|
685
678
|
* @returns The block decrypted.
|
|
686
679
|
*/
|
|
687
680
|
decrypt(block) {
|
|
688
|
-
Guards.uint8Array(ChaCha20Poly1305.
|
|
681
|
+
Guards.uint8Array(ChaCha20Poly1305.CLASS_NAME, "block", block);
|
|
689
682
|
return this._instance.decrypt(block);
|
|
690
683
|
}
|
|
691
684
|
}
|
|
@@ -698,16 +691,15 @@ class ChaCha20Poly1305 {
|
|
|
698
691
|
class X25519 {
|
|
699
692
|
/**
|
|
700
693
|
* Runtime name for the class.
|
|
701
|
-
* @internal
|
|
702
694
|
*/
|
|
703
|
-
static
|
|
695
|
+
static CLASS_NAME = "X25519";
|
|
704
696
|
/**
|
|
705
697
|
* Convert Ed25519 private key to X25519 private key.
|
|
706
698
|
* @param ed25519PrivateKey The ed25519 private key to convert.
|
|
707
699
|
* @returns The x25519 private key.
|
|
708
700
|
*/
|
|
709
701
|
static convertPrivateKeyToX25519(ed25519PrivateKey) {
|
|
710
|
-
Guards.uint8Array(X25519.
|
|
702
|
+
Guards.uint8Array(X25519.CLASS_NAME, "ed25519PrivateKey", ed25519PrivateKey);
|
|
711
703
|
return ed25519.utils.toMontgomerySecret(ed25519PrivateKey.slice(0, Ed25519.PRIVATE_KEY_SIZE));
|
|
712
704
|
}
|
|
713
705
|
/**
|
|
@@ -717,7 +709,7 @@ class X25519 {
|
|
|
717
709
|
* @throws GeneralError On invalid public key.
|
|
718
710
|
*/
|
|
719
711
|
static convertPublicKeyToX25519(ed25519PublicKey) {
|
|
720
|
-
Guards.uint8Array(X25519.
|
|
712
|
+
Guards.uint8Array(X25519.CLASS_NAME, "ed25519PublicKey", ed25519PublicKey);
|
|
721
713
|
return ed25519.utils.toMontgomery(ed25519PublicKey);
|
|
722
714
|
}
|
|
723
715
|
}
|
|
@@ -730,9 +722,8 @@ class X25519 {
|
|
|
730
722
|
class Zip215 {
|
|
731
723
|
/**
|
|
732
724
|
* Runtime name for the class.
|
|
733
|
-
* @internal
|
|
734
725
|
*/
|
|
735
|
-
static
|
|
726
|
+
static CLASS_NAME = "Zip215";
|
|
736
727
|
/**
|
|
737
728
|
* Verify reports whether sig is a valid signature of block by
|
|
738
729
|
* publicKey, using precisely-specified validation criteria (ZIP 215) suitable
|
|
@@ -743,9 +734,9 @@ class Zip215 {
|
|
|
743
734
|
* @returns True if the signature is valid.
|
|
744
735
|
*/
|
|
745
736
|
static verify(publicKey, block, sig) {
|
|
746
|
-
Guards.uint8Array(Zip215.
|
|
747
|
-
Guards.uint8Array(Zip215.
|
|
748
|
-
Guards.uint8Array(Zip215.
|
|
737
|
+
Guards.uint8Array(Zip215.CLASS_NAME, "publicKey", publicKey);
|
|
738
|
+
Guards.uint8Array(Zip215.CLASS_NAME, "block", block);
|
|
739
|
+
Guards.uint8Array(Zip215.CLASS_NAME, "sig", sig);
|
|
749
740
|
if (publicKey.length !== Ed25519.PUBLIC_KEY_SIZE) {
|
|
750
741
|
return false;
|
|
751
742
|
}
|
|
@@ -769,9 +760,8 @@ class Blake3 {
|
|
|
769
760
|
static SIZE_512 = 64;
|
|
770
761
|
/**
|
|
771
762
|
* Runtime name for the class.
|
|
772
|
-
* @internal
|
|
773
763
|
*/
|
|
774
|
-
static
|
|
764
|
+
static CLASS_NAME = "Blake3";
|
|
775
765
|
/**
|
|
776
766
|
* The instance of the hash.
|
|
777
767
|
* @internal
|
|
@@ -796,7 +786,7 @@ class Blake3 {
|
|
|
796
786
|
* @returns The sum 256 of the block.
|
|
797
787
|
*/
|
|
798
788
|
static sum256(block, key) {
|
|
799
|
-
Guards.uint8Array(Blake3.
|
|
789
|
+
Guards.uint8Array(Blake3.CLASS_NAME, "block", block);
|
|
800
790
|
return new Blake3(Blake3.SIZE_256, key).update(block).digest();
|
|
801
791
|
}
|
|
802
792
|
/**
|
|
@@ -806,7 +796,7 @@ class Blake3 {
|
|
|
806
796
|
* @returns The sum 512 of the block.
|
|
807
797
|
*/
|
|
808
798
|
static sum512(block, key) {
|
|
809
|
-
Guards.uint8Array(Blake3.
|
|
799
|
+
Guards.uint8Array(Blake3.CLASS_NAME, "block", block);
|
|
810
800
|
return new Blake3(Blake3.SIZE_512, key).update(block).digest();
|
|
811
801
|
}
|
|
812
802
|
/**
|
|
@@ -815,7 +805,7 @@ class Blake3 {
|
|
|
815
805
|
* @returns The instance for chaining.
|
|
816
806
|
*/
|
|
817
807
|
update(block) {
|
|
818
|
-
Guards.uint8Array(Blake3.
|
|
808
|
+
Guards.uint8Array(Blake3.CLASS_NAME, "block", block);
|
|
819
809
|
this._instance.update(block);
|
|
820
810
|
return this;
|
|
821
811
|
}
|
|
@@ -836,9 +826,8 @@ class Blake3 {
|
|
|
836
826
|
class HmacSha1 {
|
|
837
827
|
/**
|
|
838
828
|
* Runtime name for the class.
|
|
839
|
-
* @internal
|
|
840
829
|
*/
|
|
841
|
-
static
|
|
830
|
+
static CLASS_NAME = "HmacSha1";
|
|
842
831
|
/**
|
|
843
832
|
* The instance of the hash.
|
|
844
833
|
* @internal
|
|
@@ -859,8 +848,8 @@ class HmacSha1 {
|
|
|
859
848
|
* @returns The sum of the block.
|
|
860
849
|
*/
|
|
861
850
|
static sum(key, block) {
|
|
862
|
-
Guards.uint8Array(HmacSha1.
|
|
863
|
-
Guards.uint8Array(HmacSha1.
|
|
851
|
+
Guards.uint8Array(HmacSha1.CLASS_NAME, "key", key);
|
|
852
|
+
Guards.uint8Array(HmacSha1.CLASS_NAME, "block", block);
|
|
864
853
|
return new HmacSha1(key).update(block).digest();
|
|
865
854
|
}
|
|
866
855
|
/**
|
|
@@ -869,7 +858,7 @@ class HmacSha1 {
|
|
|
869
858
|
* @returns The instance for chaining.
|
|
870
859
|
*/
|
|
871
860
|
update(block) {
|
|
872
|
-
Guards.uint8Array(HmacSha1.
|
|
861
|
+
Guards.uint8Array(HmacSha1.CLASS_NAME, "block", block);
|
|
873
862
|
this._instance.update(block);
|
|
874
863
|
return this;
|
|
875
864
|
}
|
|
@@ -898,9 +887,8 @@ class HmacSha256 {
|
|
|
898
887
|
static SIZE_224 = 224;
|
|
899
888
|
/**
|
|
900
889
|
* Runtime name for the class.
|
|
901
|
-
* @internal
|
|
902
890
|
*/
|
|
903
|
-
static
|
|
891
|
+
static CLASS_NAME = "HmacSha256";
|
|
904
892
|
/**
|
|
905
893
|
* The instance of the hash.
|
|
906
894
|
* @internal
|
|
@@ -914,7 +902,7 @@ class HmacSha256 {
|
|
|
914
902
|
*/
|
|
915
903
|
constructor(key, bits = HmacSha256.SIZE_256) {
|
|
916
904
|
if (bits !== HmacSha256.SIZE_224 && bits !== HmacSha256.SIZE_256) {
|
|
917
|
-
throw new GeneralError(HmacSha256.
|
|
905
|
+
throw new GeneralError(HmacSha256.CLASS_NAME, "bitSize", { bitSize: bits });
|
|
918
906
|
}
|
|
919
907
|
this._instance = hmac.create(bits === HmacSha256.SIZE_256 ? sha256 : sha224, key);
|
|
920
908
|
}
|
|
@@ -925,8 +913,8 @@ class HmacSha256 {
|
|
|
925
913
|
* @returns The sum 224 of the block.
|
|
926
914
|
*/
|
|
927
915
|
static sum224(key, block) {
|
|
928
|
-
Guards.uint8Array(HmacSha256.
|
|
929
|
-
Guards.uint8Array(HmacSha256.
|
|
916
|
+
Guards.uint8Array(HmacSha256.CLASS_NAME, "key", key);
|
|
917
|
+
Guards.uint8Array(HmacSha256.CLASS_NAME, "block", block);
|
|
930
918
|
const instance = new HmacSha256(key, HmacSha256.SIZE_224);
|
|
931
919
|
instance.update(block);
|
|
932
920
|
return instance.digest();
|
|
@@ -938,8 +926,8 @@ class HmacSha256 {
|
|
|
938
926
|
* @returns The sum 256 of the block.
|
|
939
927
|
*/
|
|
940
928
|
static sum256(key, block) {
|
|
941
|
-
Guards.uint8Array(HmacSha256.
|
|
942
|
-
Guards.uint8Array(HmacSha256.
|
|
929
|
+
Guards.uint8Array(HmacSha256.CLASS_NAME, "key", key);
|
|
930
|
+
Guards.uint8Array(HmacSha256.CLASS_NAME, "block", block);
|
|
943
931
|
const instance = new HmacSha256(key, HmacSha256.SIZE_256);
|
|
944
932
|
instance.update(block);
|
|
945
933
|
return instance.digest();
|
|
@@ -950,7 +938,7 @@ class HmacSha256 {
|
|
|
950
938
|
* @returns The instance for chaining.
|
|
951
939
|
*/
|
|
952
940
|
update(block) {
|
|
953
|
-
Guards.uint8Array(HmacSha256.
|
|
941
|
+
Guards.uint8Array(HmacSha256.CLASS_NAME, "block", block);
|
|
954
942
|
this._instance.update(block);
|
|
955
943
|
return this;
|
|
956
944
|
}
|
|
@@ -988,9 +976,8 @@ class HmacSha512 {
|
|
|
988
976
|
static SIZE_512 = 512;
|
|
989
977
|
/**
|
|
990
978
|
* Runtime name for the class.
|
|
991
|
-
* @internal
|
|
992
979
|
*/
|
|
993
|
-
static
|
|
980
|
+
static CLASS_NAME = "HmacSha512";
|
|
994
981
|
/**
|
|
995
982
|
* The instance of the hash.
|
|
996
983
|
* @internal
|
|
@@ -1007,7 +994,7 @@ class HmacSha512 {
|
|
|
1007
994
|
bits !== HmacSha512.SIZE_256 &&
|
|
1008
995
|
bits !== HmacSha512.SIZE_384 &&
|
|
1009
996
|
bits !== HmacSha512.SIZE_512) {
|
|
1010
|
-
throw new GeneralError(HmacSha512.
|
|
997
|
+
throw new GeneralError(HmacSha512.CLASS_NAME, "bitSize", { bitSize: bits });
|
|
1011
998
|
}
|
|
1012
999
|
if (bits === HmacSha512.SIZE_224) {
|
|
1013
1000
|
this._instance = hmac.create(sha512_224, key);
|
|
@@ -1029,8 +1016,8 @@ class HmacSha512 {
|
|
|
1029
1016
|
* @returns The sum 512 of the block.
|
|
1030
1017
|
*/
|
|
1031
1018
|
static sum512(key, block) {
|
|
1032
|
-
Guards.uint8Array(HmacSha512.
|
|
1033
|
-
Guards.uint8Array(HmacSha512.
|
|
1019
|
+
Guards.uint8Array(HmacSha512.CLASS_NAME, "key", key);
|
|
1020
|
+
Guards.uint8Array(HmacSha512.CLASS_NAME, "block", block);
|
|
1034
1021
|
const instance = new HmacSha512(key, HmacSha512.SIZE_512);
|
|
1035
1022
|
instance.update(block);
|
|
1036
1023
|
return instance.digest();
|
|
@@ -1042,8 +1029,8 @@ class HmacSha512 {
|
|
|
1042
1029
|
* @returns The sum 384 of the block.
|
|
1043
1030
|
*/
|
|
1044
1031
|
static sum384(key, block) {
|
|
1045
|
-
Guards.uint8Array(HmacSha512.
|
|
1046
|
-
Guards.uint8Array(HmacSha512.
|
|
1032
|
+
Guards.uint8Array(HmacSha512.CLASS_NAME, "key", key);
|
|
1033
|
+
Guards.uint8Array(HmacSha512.CLASS_NAME, "block", block);
|
|
1047
1034
|
const instance = new HmacSha512(key, HmacSha512.SIZE_384);
|
|
1048
1035
|
instance.update(block);
|
|
1049
1036
|
return instance.digest();
|
|
@@ -1055,8 +1042,8 @@ class HmacSha512 {
|
|
|
1055
1042
|
* @returns The sum 256 of the block.
|
|
1056
1043
|
*/
|
|
1057
1044
|
static sum256(key, block) {
|
|
1058
|
-
Guards.uint8Array(HmacSha512.
|
|
1059
|
-
Guards.uint8Array(HmacSha512.
|
|
1045
|
+
Guards.uint8Array(HmacSha512.CLASS_NAME, "key", key);
|
|
1046
|
+
Guards.uint8Array(HmacSha512.CLASS_NAME, "block", block);
|
|
1060
1047
|
const instance = new HmacSha512(key, HmacSha512.SIZE_256);
|
|
1061
1048
|
instance.update(block);
|
|
1062
1049
|
return instance.digest();
|
|
@@ -1068,8 +1055,8 @@ class HmacSha512 {
|
|
|
1068
1055
|
* @returns The sum 224 of the block.
|
|
1069
1056
|
*/
|
|
1070
1057
|
static sum224(key, block) {
|
|
1071
|
-
Guards.uint8Array(HmacSha512.
|
|
1072
|
-
Guards.uint8Array(HmacSha512.
|
|
1058
|
+
Guards.uint8Array(HmacSha512.CLASS_NAME, "key", key);
|
|
1059
|
+
Guards.uint8Array(HmacSha512.CLASS_NAME, "block", block);
|
|
1073
1060
|
const instance = new HmacSha512(key, HmacSha512.SIZE_224);
|
|
1074
1061
|
instance.update(block);
|
|
1075
1062
|
return instance.digest();
|
|
@@ -1080,7 +1067,7 @@ class HmacSha512 {
|
|
|
1080
1067
|
* @returns The instance for chaining.
|
|
1081
1068
|
*/
|
|
1082
1069
|
update(block) {
|
|
1083
|
-
Guards.uint8Array(HmacSha512.
|
|
1070
|
+
Guards.uint8Array(HmacSha512.CLASS_NAME, "block", block);
|
|
1084
1071
|
this._instance.update(block);
|
|
1085
1072
|
return this;
|
|
1086
1073
|
}
|
|
@@ -1101,9 +1088,8 @@ class HmacSha512 {
|
|
|
1101
1088
|
class Pbkdf2 {
|
|
1102
1089
|
/**
|
|
1103
1090
|
* Runtime name for the class.
|
|
1104
|
-
* @internal
|
|
1105
1091
|
*/
|
|
1106
|
-
static
|
|
1092
|
+
static CLASS_NAME = "Pbkdf2";
|
|
1107
1093
|
/**
|
|
1108
1094
|
* Derive a key from the parameters using Sha256.
|
|
1109
1095
|
* @param password The password to derive the key from.
|
|
@@ -1113,10 +1099,10 @@ class Pbkdf2 {
|
|
|
1113
1099
|
* @returns The derived key.
|
|
1114
1100
|
*/
|
|
1115
1101
|
static sha256(password, salt, iterations, keyLength) {
|
|
1116
|
-
Guards.uint8Array(Pbkdf2.
|
|
1117
|
-
Guards.uint8Array(Pbkdf2.
|
|
1118
|
-
Guards.number(Pbkdf2.
|
|
1119
|
-
Guards.number(Pbkdf2.
|
|
1102
|
+
Guards.uint8Array(Pbkdf2.CLASS_NAME, "password", password);
|
|
1103
|
+
Guards.uint8Array(Pbkdf2.CLASS_NAME, "salt", salt);
|
|
1104
|
+
Guards.number(Pbkdf2.CLASS_NAME, "iterations", iterations);
|
|
1105
|
+
Guards.number(Pbkdf2.CLASS_NAME, "keyLength", keyLength);
|
|
1120
1106
|
return pbkdf2(sha256, password, salt, { c: iterations, dkLen: keyLength });
|
|
1121
1107
|
}
|
|
1122
1108
|
/**
|
|
@@ -1128,10 +1114,10 @@ class Pbkdf2 {
|
|
|
1128
1114
|
* @returns The derived key.
|
|
1129
1115
|
*/
|
|
1130
1116
|
static sha512(password, salt, iterations, keyLength) {
|
|
1131
|
-
Guards.uint8Array(Pbkdf2.
|
|
1132
|
-
Guards.uint8Array(Pbkdf2.
|
|
1133
|
-
Guards.number(Pbkdf2.
|
|
1134
|
-
Guards.number(Pbkdf2.
|
|
1117
|
+
Guards.uint8Array(Pbkdf2.CLASS_NAME, "password", password);
|
|
1118
|
+
Guards.uint8Array(Pbkdf2.CLASS_NAME, "salt", salt);
|
|
1119
|
+
Guards.number(Pbkdf2.CLASS_NAME, "iterations", iterations);
|
|
1120
|
+
Guards.number(Pbkdf2.CLASS_NAME, "keyLength", keyLength);
|
|
1135
1121
|
return pbkdf2(sha512, password, salt, { c: iterations, dkLen: keyLength });
|
|
1136
1122
|
}
|
|
1137
1123
|
}
|
|
@@ -1144,9 +1130,8 @@ class Pbkdf2 {
|
|
|
1144
1130
|
class Sha1 {
|
|
1145
1131
|
/**
|
|
1146
1132
|
* Runtime name for the class.
|
|
1147
|
-
* @internal
|
|
1148
1133
|
*/
|
|
1149
|
-
static
|
|
1134
|
+
static CLASS_NAME = "Sha1";
|
|
1150
1135
|
/**
|
|
1151
1136
|
* The instance of the hash.
|
|
1152
1137
|
* @internal
|
|
@@ -1165,7 +1150,7 @@ class Sha1 {
|
|
|
1165
1150
|
* @returns The sum of the block.
|
|
1166
1151
|
*/
|
|
1167
1152
|
static sum(block) {
|
|
1168
|
-
Guards.uint8Array(Sha1.
|
|
1153
|
+
Guards.uint8Array(Sha1.CLASS_NAME, "block", block);
|
|
1169
1154
|
return new Sha1().update(block).digest();
|
|
1170
1155
|
}
|
|
1171
1156
|
/**
|
|
@@ -1174,7 +1159,7 @@ class Sha1 {
|
|
|
1174
1159
|
* @returns The instance for chaining.
|
|
1175
1160
|
*/
|
|
1176
1161
|
update(block) {
|
|
1177
|
-
Guards.uint8Array(Sha1.
|
|
1162
|
+
Guards.uint8Array(Sha1.CLASS_NAME, "block", block);
|
|
1178
1163
|
this._instance.update(block);
|
|
1179
1164
|
return this;
|
|
1180
1165
|
}
|
|
@@ -1203,9 +1188,8 @@ class Sha256 {
|
|
|
1203
1188
|
static SIZE_224 = 224;
|
|
1204
1189
|
/**
|
|
1205
1190
|
* Runtime name for the class.
|
|
1206
|
-
* @internal
|
|
1207
1191
|
*/
|
|
1208
|
-
static
|
|
1192
|
+
static CLASS_NAME = "Sha256";
|
|
1209
1193
|
/**
|
|
1210
1194
|
* The instance of the hash.
|
|
1211
1195
|
* @internal
|
|
@@ -1218,7 +1202,7 @@ class Sha256 {
|
|
|
1218
1202
|
*/
|
|
1219
1203
|
constructor(bits = Sha256.SIZE_256) {
|
|
1220
1204
|
if (bits !== Sha256.SIZE_224 && bits !== Sha256.SIZE_256) {
|
|
1221
|
-
throw new GeneralError(Sha256.
|
|
1205
|
+
throw new GeneralError(Sha256.CLASS_NAME, "bitSize", { bitSize: bits });
|
|
1222
1206
|
}
|
|
1223
1207
|
this._instance = bits === Sha256.SIZE_256 ? sha256.create() : sha224.create();
|
|
1224
1208
|
}
|
|
@@ -1248,7 +1232,7 @@ class Sha256 {
|
|
|
1248
1232
|
* @returns The instance for chaining.
|
|
1249
1233
|
*/
|
|
1250
1234
|
update(block) {
|
|
1251
|
-
Guards.uint8Array(Sha256.
|
|
1235
|
+
Guards.uint8Array(Sha256.CLASS_NAME, "block", block);
|
|
1252
1236
|
this._instance.update(block);
|
|
1253
1237
|
return this;
|
|
1254
1238
|
}
|
|
@@ -1286,9 +1270,8 @@ class Sha3 {
|
|
|
1286
1270
|
static SIZE_512 = 512;
|
|
1287
1271
|
/**
|
|
1288
1272
|
* Runtime name for the class.
|
|
1289
|
-
* @internal
|
|
1290
1273
|
*/
|
|
1291
|
-
static
|
|
1274
|
+
static CLASS_NAME = "Sha3";
|
|
1292
1275
|
/**
|
|
1293
1276
|
* The instance of the hash.
|
|
1294
1277
|
* @internal
|
|
@@ -1304,7 +1287,7 @@ class Sha3 {
|
|
|
1304
1287
|
bits !== Sha3.SIZE_256 &&
|
|
1305
1288
|
bits !== Sha3.SIZE_384 &&
|
|
1306
1289
|
bits !== Sha3.SIZE_512) {
|
|
1307
|
-
throw new GeneralError(Sha3.
|
|
1290
|
+
throw new GeneralError(Sha3.CLASS_NAME, "bitSize", { bitSize: bits });
|
|
1308
1291
|
}
|
|
1309
1292
|
if (bits === Sha3.SIZE_224) {
|
|
1310
1293
|
// eslint-disable-next-line camelcase
|
|
@@ -1369,7 +1352,7 @@ class Sha3 {
|
|
|
1369
1352
|
* @returns The instance for chaining.
|
|
1370
1353
|
*/
|
|
1371
1354
|
update(block) {
|
|
1372
|
-
Guards.uint8Array(Sha3.
|
|
1355
|
+
Guards.uint8Array(Sha3.CLASS_NAME, "block", block);
|
|
1373
1356
|
this._instance.update(block);
|
|
1374
1357
|
return this;
|
|
1375
1358
|
}
|
|
@@ -1407,9 +1390,8 @@ class Sha512 {
|
|
|
1407
1390
|
static SIZE_512 = 512;
|
|
1408
1391
|
/**
|
|
1409
1392
|
* Runtime name for the class.
|
|
1410
|
-
* @internal
|
|
1411
1393
|
*/
|
|
1412
|
-
static
|
|
1394
|
+
static CLASS_NAME = "Sha512";
|
|
1413
1395
|
/**
|
|
1414
1396
|
* The instance of the hash.
|
|
1415
1397
|
* @internal
|
|
@@ -1425,7 +1407,7 @@ class Sha512 {
|
|
|
1425
1407
|
bits !== Sha512.SIZE_256 &&
|
|
1426
1408
|
bits !== Sha512.SIZE_384 &&
|
|
1427
1409
|
bits !== Sha512.SIZE_512) {
|
|
1428
|
-
throw new GeneralError(Sha512.
|
|
1410
|
+
throw new GeneralError(Sha512.CLASS_NAME, "bitSize", { bitSize: bits });
|
|
1429
1411
|
}
|
|
1430
1412
|
if (bits === Sha512.SIZE_224) {
|
|
1431
1413
|
this._instance = sha512_224.create();
|
|
@@ -1486,7 +1468,7 @@ class Sha512 {
|
|
|
1486
1468
|
* @returns The instance for chaining.
|
|
1487
1469
|
*/
|
|
1488
1470
|
update(block) {
|
|
1489
|
-
Guards.uint8Array(Sha512.
|
|
1471
|
+
Guards.uint8Array(Sha512.CLASS_NAME, "block", block);
|
|
1490
1472
|
this._instance.update(block);
|
|
1491
1473
|
return this;
|
|
1492
1474
|
}
|
|
@@ -1507,16 +1489,15 @@ class Sha512 {
|
|
|
1507
1489
|
class PemHelper {
|
|
1508
1490
|
/**
|
|
1509
1491
|
* Runtime name for the class.
|
|
1510
|
-
* @internal
|
|
1511
1492
|
*/
|
|
1512
|
-
static
|
|
1493
|
+
static CLASS_NAME = "PemHelper";
|
|
1513
1494
|
/**
|
|
1514
1495
|
* Strip the PEM content of its headers, footers, and newlines.
|
|
1515
1496
|
* @param pemContent The PEM content to strip.
|
|
1516
1497
|
* @returns The stripped PEM content in bas64 format.
|
|
1517
1498
|
*/
|
|
1518
1499
|
static stripPemMarkers(pemContent) {
|
|
1519
|
-
Guards.string(PemHelper.
|
|
1500
|
+
Guards.string(PemHelper.CLASS_NAME, "pemContent", pemContent);
|
|
1520
1501
|
return pemContent
|
|
1521
1502
|
.replace(/-----BEGIN.*-----/, "")
|
|
1522
1503
|
.replace(/-----END.*-----/, "")
|
|
@@ -1531,8 +1512,8 @@ class PemHelper {
|
|
|
1531
1512
|
* @returns The formatted PEM content.
|
|
1532
1513
|
*/
|
|
1533
1514
|
static formatPem(marker, base64Content, lineLength = 64) {
|
|
1534
|
-
Guards.stringValue(PemHelper.
|
|
1535
|
-
Guards.stringBase64(PemHelper.
|
|
1515
|
+
Guards.stringValue(PemHelper.CLASS_NAME, "marker", marker);
|
|
1516
|
+
Guards.stringBase64(PemHelper.CLASS_NAME, "base64Content", base64Content);
|
|
1536
1517
|
const lines = [];
|
|
1537
1518
|
for (let i = 0; i < base64Content.length; i += lineLength) {
|
|
1538
1519
|
lines.push(base64Content.slice(i, i + lineLength));
|
|
@@ -1549,9 +1530,8 @@ class PemHelper {
|
|
|
1549
1530
|
class Bip39 {
|
|
1550
1531
|
/**
|
|
1551
1532
|
* Runtime name for the class.
|
|
1552
|
-
* @internal
|
|
1553
1533
|
*/
|
|
1554
|
-
static
|
|
1534
|
+
static CLASS_NAME = "Bip39";
|
|
1555
1535
|
/**
|
|
1556
1536
|
* Generate a random mnemonic.
|
|
1557
1537
|
* @param strength The strength of the mnemonic to generate, defaults to 256.
|
|
@@ -1560,10 +1540,10 @@ class Bip39 {
|
|
|
1560
1540
|
* @throws Error if the length is not a multiple of 32.
|
|
1561
1541
|
*/
|
|
1562
1542
|
static randomMnemonic(strength = 256, words = wordlist) {
|
|
1563
|
-
Guards.number(Bip39.
|
|
1564
|
-
Guards.arrayValue(Bip39.
|
|
1543
|
+
Guards.number(Bip39.CLASS_NAME, "strength", strength);
|
|
1544
|
+
Guards.arrayValue(Bip39.CLASS_NAME, "words", words);
|
|
1565
1545
|
if (strength % 32 !== 0) {
|
|
1566
|
-
throw new GuardError(Bip39.
|
|
1546
|
+
throw new GuardError(Bip39.CLASS_NAME, "guard.length32Multiple", "strength", strength);
|
|
1567
1547
|
}
|
|
1568
1548
|
return bip39.generateMnemonic(words, strength);
|
|
1569
1549
|
}
|
|
@@ -1575,10 +1555,10 @@ class Bip39 {
|
|
|
1575
1555
|
* @throws Error if the length of the entropy is not a multiple of 4, or is less than 16 or greater than 32.
|
|
1576
1556
|
*/
|
|
1577
1557
|
static entropyToMnemonic(entropy, words = wordlist) {
|
|
1578
|
-
Guards.uint8Array(Bip39.
|
|
1579
|
-
Guards.arrayValue(Bip39.
|
|
1558
|
+
Guards.uint8Array(Bip39.CLASS_NAME, "entropy", entropy);
|
|
1559
|
+
Guards.arrayValue(Bip39.CLASS_NAME, "words", words);
|
|
1580
1560
|
if (entropy.length % 4 !== 0 || entropy.length < 16 || entropy.length > 32) {
|
|
1581
|
-
throw new GuardError(Bip39.
|
|
1561
|
+
throw new GuardError(Bip39.CLASS_NAME, "guard.lengthEntropy", "entropy", entropy.length);
|
|
1582
1562
|
}
|
|
1583
1563
|
return bip39.entropyToMnemonic(entropy, words);
|
|
1584
1564
|
}
|
|
@@ -1589,7 +1569,7 @@ class Bip39 {
|
|
|
1589
1569
|
* @returns The seed.
|
|
1590
1570
|
*/
|
|
1591
1571
|
static mnemonicToSeed(mnemonic, password) {
|
|
1592
|
-
Guards.stringValue(Bip39.
|
|
1572
|
+
Guards.stringValue(Bip39.CLASS_NAME, "mnemonic", mnemonic);
|
|
1593
1573
|
return bip39.mnemonicToSeedSync(mnemonic, password);
|
|
1594
1574
|
}
|
|
1595
1575
|
/**
|
|
@@ -1600,8 +1580,8 @@ class Bip39 {
|
|
|
1600
1580
|
* @throws Error if the number of words is not a multiple of 3.
|
|
1601
1581
|
*/
|
|
1602
1582
|
static mnemonicToEntropy(mnemonic, words = wordlist) {
|
|
1603
|
-
Guards.stringValue(Bip39.
|
|
1604
|
-
Guards.arrayValue(Bip39.
|
|
1583
|
+
Guards.stringValue(Bip39.CLASS_NAME, "mnemonic", mnemonic);
|
|
1584
|
+
Guards.arrayValue(Bip39.CLASS_NAME, "words", words);
|
|
1605
1585
|
return bip39.mnemonicToEntropy(mnemonic, words);
|
|
1606
1586
|
}
|
|
1607
1587
|
/**
|
|
@@ -1612,8 +1592,8 @@ class Bip39 {
|
|
|
1612
1592
|
* @returns True if the mnemonic is valid.
|
|
1613
1593
|
*/
|
|
1614
1594
|
static validateMnemonic(mnemonic, wordCount = 24, words = wordlist) {
|
|
1615
|
-
Guards.string(Bip39.
|
|
1616
|
-
Guards.integer(Bip39.
|
|
1595
|
+
Guards.string(Bip39.CLASS_NAME, "mnemonic", mnemonic);
|
|
1596
|
+
Guards.integer(Bip39.CLASS_NAME, "wordCount", wordCount);
|
|
1617
1597
|
const mnemonicSplit = mnemonic.split(/\s+/);
|
|
1618
1598
|
if (mnemonicSplit.length !== wordCount) {
|
|
1619
1599
|
return false;
|
|
@@ -1631,9 +1611,8 @@ class Bip39 {
|
|
|
1631
1611
|
class Hotp {
|
|
1632
1612
|
/**
|
|
1633
1613
|
* Runtime name for the class.
|
|
1634
|
-
* @internal
|
|
1635
1614
|
*/
|
|
1636
|
-
static
|
|
1615
|
+
static CLASS_NAME = "Hotp";
|
|
1637
1616
|
/**
|
|
1638
1617
|
* Generate a counter based One Time Password.
|
|
1639
1618
|
* @param key Key for the one time password.
|
|
@@ -1642,8 +1621,8 @@ class Hotp {
|
|
|
1642
1621
|
* @returns The one time password.
|
|
1643
1622
|
*/
|
|
1644
1623
|
static generate(key, counter) {
|
|
1645
|
-
Guards.uint8Array(Hotp.
|
|
1646
|
-
Guards.number(Hotp.
|
|
1624
|
+
Guards.uint8Array(Hotp.CLASS_NAME, "key", key);
|
|
1625
|
+
Guards.number(Hotp.CLASS_NAME, "counter", counter);
|
|
1647
1626
|
return otp.hotp({ secret: key, digits: 6, algorithm: "sha1", interval: 30 }, counter);
|
|
1648
1627
|
}
|
|
1649
1628
|
}
|
|
@@ -1679,7 +1658,7 @@ class Totp {
|
|
|
1679
1658
|
for (let i = -window; i < window; i++) {
|
|
1680
1659
|
const intervalWindow = i * interval * 1000;
|
|
1681
1660
|
if (timestamp + intervalWindow > 0) {
|
|
1682
|
-
const gen =
|
|
1661
|
+
const gen = Totp.generate(key, interval, timestamp + intervalWindow);
|
|
1683
1662
|
if (gen === token) {
|
|
1684
1663
|
// We have found a matching code
|
|
1685
1664
|
return i;
|
|
@@ -1774,7 +1753,8 @@ class PasswordValidator {
|
|
|
1774
1753
|
property,
|
|
1775
1754
|
reason: "validation.minLengthRequired",
|
|
1776
1755
|
properties: {
|
|
1777
|
-
minLength
|
|
1756
|
+
minLength,
|
|
1757
|
+
actualLength: password.length
|
|
1778
1758
|
}
|
|
1779
1759
|
});
|
|
1780
1760
|
}
|
|
@@ -1784,7 +1764,8 @@ class PasswordValidator {
|
|
|
1784
1764
|
property,
|
|
1785
1765
|
reason: "validation.maxLengthRequired",
|
|
1786
1766
|
properties: {
|
|
1787
|
-
maxLength
|
|
1767
|
+
maxLength,
|
|
1768
|
+
actualLength: password.length
|
|
1788
1769
|
}
|
|
1789
1770
|
});
|
|
1790
1771
|
}
|