@twin.org/crypto 0.0.2-next.9 → 0.0.3-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/dist/es/address/bech32.js +72 -0
  2. package/dist/es/address/bech32.js.map +1 -0
  3. package/dist/es/address/bip44.js +113 -0
  4. package/dist/es/address/bip44.js.map +1 -0
  5. package/dist/es/ciphers/chaCha20Poly1305.js +48 -0
  6. package/dist/es/ciphers/chaCha20Poly1305.js.map +1 -0
  7. package/dist/es/curves/ed25519.js +114 -0
  8. package/dist/es/curves/ed25519.js.map +1 -0
  9. package/dist/es/curves/secp256k1.js +82 -0
  10. package/dist/es/curves/secp256k1.js.map +1 -0
  11. package/dist/es/curves/x25519.js +34 -0
  12. package/dist/es/curves/x25519.js.map +1 -0
  13. package/dist/es/curves/zip215.js +33 -0
  14. package/dist/es/curves/zip215.js.map +1 -0
  15. package/dist/es/hashes/blake2b.js +90 -0
  16. package/dist/es/hashes/blake2b.js.map +1 -0
  17. package/dist/es/hashes/blake3.js +76 -0
  18. package/dist/es/hashes/blake3.js.map +1 -0
  19. package/dist/es/hashes/hmacSha1.js +56 -0
  20. package/dist/es/hashes/hmacSha1.js.map +1 -0
  21. package/dist/es/hashes/hmacSha256.js +83 -0
  22. package/dist/es/hashes/hmacSha256.js.map +1 -0
  23. package/dist/es/hashes/hmacSha512.js +132 -0
  24. package/dist/es/hashes/hmacSha512.js.map +1 -0
  25. package/dist/es/hashes/pbkdf2.js +45 -0
  26. package/dist/es/hashes/pbkdf2.js.map +1 -0
  27. package/dist/es/hashes/sha1.js +52 -0
  28. package/dist/es/hashes/sha1.js.map +1 -0
  29. package/dist/es/hashes/sha256.js +75 -0
  30. package/dist/es/hashes/sha256.js.map +1 -0
  31. package/dist/es/hashes/sha3.js +122 -0
  32. package/dist/es/hashes/sha3.js.map +1 -0
  33. package/dist/es/hashes/sha512.js +118 -0
  34. package/dist/es/hashes/sha512.js.map +1 -0
  35. package/dist/es/helpers/pemHelper.js +42 -0
  36. package/dist/es/helpers/pemHelper.js.map +1 -0
  37. package/dist/es/index.js +29 -0
  38. package/dist/es/index.js.map +1 -0
  39. package/dist/es/keys/bip32Path.js +72 -0
  40. package/dist/es/keys/bip32Path.js.map +1 -0
  41. package/dist/es/keys/bip39.js +83 -0
  42. package/dist/es/keys/bip39.js.map +1 -0
  43. package/dist/es/keys/slip0010.js +83 -0
  44. package/dist/es/keys/slip0010.js.map +1 -0
  45. package/dist/es/models/keyType.js +17 -0
  46. package/dist/es/models/keyType.js.map +1 -0
  47. package/dist/es/otp/hotp.js +27 -0
  48. package/dist/es/otp/hotp.js.map +1 -0
  49. package/dist/es/otp/totp.js +75 -0
  50. package/dist/es/otp/totp.js.map +1 -0
  51. package/dist/es/passwords/passwordGenerator.js +28 -0
  52. package/dist/es/passwords/passwordGenerator.js.map +1 -0
  53. package/dist/es/passwords/passwordValidator.js +81 -0
  54. package/dist/es/passwords/passwordValidator.js.map +1 -0
  55. package/dist/types/address/bech32.d.ts +4 -0
  56. package/dist/types/address/bip44.d.ts +6 -2
  57. package/dist/types/ciphers/chaCha20Poly1305.d.ts +4 -0
  58. package/dist/types/curves/ed25519.d.ts +4 -0
  59. package/dist/types/curves/secp256k1.d.ts +4 -0
  60. package/dist/types/curves/x25519.d.ts +4 -0
  61. package/dist/types/curves/zip215.d.ts +4 -0
  62. package/dist/types/hashes/blake2b.d.ts +4 -0
  63. package/dist/types/hashes/blake3.d.ts +4 -0
  64. package/dist/types/hashes/hmacSha1.d.ts +4 -0
  65. package/dist/types/hashes/hmacSha256.d.ts +4 -0
  66. package/dist/types/hashes/hmacSha512.d.ts +4 -0
  67. package/dist/types/hashes/pbkdf2.d.ts +4 -0
  68. package/dist/types/hashes/sha1.d.ts +4 -0
  69. package/dist/types/hashes/sha256.d.ts +4 -0
  70. package/dist/types/hashes/sha3.d.ts +4 -0
  71. package/dist/types/hashes/sha512.d.ts +4 -0
  72. package/dist/types/helpers/pemHelper.d.ts +4 -0
  73. package/dist/types/index.d.ts +26 -26
  74. package/dist/types/keys/bip39.d.ts +4 -0
  75. package/dist/types/keys/slip0010.d.ts +6 -2
  76. package/dist/types/otp/hotp.d.ts +4 -0
  77. package/docs/changelog.md +267 -0
  78. package/docs/reference/classes/Bech32.md +8 -0
  79. package/docs/reference/classes/Bip39.md +8 -0
  80. package/docs/reference/classes/Bip44.md +8 -0
  81. package/docs/reference/classes/Blake2b.md +8 -0
  82. package/docs/reference/classes/Blake3.md +8 -0
  83. package/docs/reference/classes/ChaCha20Poly1305.md +8 -0
  84. package/docs/reference/classes/Ed25519.md +8 -0
  85. package/docs/reference/classes/HmacSha1.md +8 -0
  86. package/docs/reference/classes/HmacSha256.md +8 -0
  87. package/docs/reference/classes/HmacSha512.md +8 -0
  88. package/docs/reference/classes/Hotp.md +8 -0
  89. package/docs/reference/classes/Pbkdf2.md +8 -0
  90. package/docs/reference/classes/PemHelper.md +8 -0
  91. package/docs/reference/classes/Secp256k1.md +8 -0
  92. package/docs/reference/classes/Sha1.md +8 -0
  93. package/docs/reference/classes/Sha256.md +8 -0
  94. package/docs/reference/classes/Sha3.md +8 -0
  95. package/docs/reference/classes/Sha512.md +8 -0
  96. package/docs/reference/classes/Slip0010.md +8 -0
  97. package/docs/reference/classes/Totp.md +2 -2
  98. package/docs/reference/classes/X25519.md +8 -0
  99. package/docs/reference/classes/Zip215.md +8 -0
  100. package/locales/en.json +9 -28
  101. package/package.json +29 -16
  102. package/dist/cjs/index.cjs +0 -1876
  103. package/dist/esm/index.mjs +0 -1829
@@ -1,1876 +0,0 @@
1
- 'use strict';
2
-
3
- var base = require('@scure/base');
4
- var core = require('@twin.org/core');
5
- var ed25519_js = require('@noble/curves/ed25519.js');
6
- var secp256k1_js = require('@noble/curves/secp256k1.js');
7
- var blake2_js = require('@noble/hashes/blake2.js');
8
- var bip32 = require('@scure/bip32');
9
- var slip10_js = require('micro-key-producer/slip10.js');
10
- var chacha_js = require('@noble/ciphers/chacha.js');
11
- var blake3_js = require('@noble/hashes/blake3.js');
12
- var hmac_js = require('@noble/hashes/hmac.js');
13
- var legacy_js = require('@noble/hashes/legacy.js');
14
- var sha2_js = require('@noble/hashes/sha2.js');
15
- var pbkdf2_js = require('@noble/hashes/pbkdf2.js');
16
- var sha3_js = require('@noble/hashes/sha3.js');
17
- var bip39 = require('@scure/bip39');
18
- var english_js = require('@scure/bip39/wordlists/english.js');
19
- var otp = require('micro-key-producer/otp.js');
20
-
21
- function _interopNamespaceDefault(e) {
22
- var n = Object.create(null);
23
- if (e) {
24
- Object.keys(e).forEach(function (k) {
25
- if (k !== 'default') {
26
- var d = Object.getOwnPropertyDescriptor(e, k);
27
- Object.defineProperty(n, k, d.get ? d : {
28
- enumerable: true,
29
- get: function () { return e[k]; }
30
- });
31
- }
32
- });
33
- }
34
- n.default = e;
35
- return Object.freeze(n);
36
- }
37
-
38
- var bip39__namespace = /*#__PURE__*/_interopNamespaceDefault(bip39);
39
- var otp__namespace = /*#__PURE__*/_interopNamespaceDefault(otp);
40
-
41
- // Copyright 2024 IOTA Stiftung.
42
- // SPDX-License-Identifier: Apache-2.0.
43
- /**
44
- * Bech32 encoding and decoding.
45
- */
46
- class Bech32 {
47
- /**
48
- * Runtime name for the class.
49
- * @internal
50
- */
51
- static _CLASS_NAME = "Bech32";
52
- /**
53
- * Encode the buffer.
54
- * @param humanReadablePart The header.
55
- * @param data The data to encode.
56
- * @returns The encoded data.
57
- */
58
- static encode(humanReadablePart, data) {
59
- core.Guards.stringValue(Bech32._CLASS_NAME, "humanReadablePart", humanReadablePart);
60
- core.Guards.uint8Array(Bech32._CLASS_NAME, "data", data);
61
- return base.bech32.encode(humanReadablePart, base.bech32.toWords(data));
62
- }
63
- /**
64
- * Decode a bech32 string.
65
- * @param bech The text to decode.
66
- * @returns The decoded data or undefined if it could not be decoded.
67
- * @throws An error if the decoding fails.
68
- */
69
- static decode(bech) {
70
- core.Guards.stringValue(Bech32._CLASS_NAME, "bech", bech);
71
- try {
72
- const result = base.bech32.decodeToBytes(bech);
73
- return {
74
- humanReadablePart: result.prefix,
75
- data: result.bytes
76
- };
77
- }
78
- catch (err) {
79
- if (core.BaseError.isErrorMessage(err, /checksum/)) {
80
- throw new core.GeneralError(Bech32._CLASS_NAME, "invalidChecksum", { bech: base.bech32 });
81
- }
82
- else if (core.BaseError.isErrorMessage(err, /between prefix and data only/i)) {
83
- throw new core.GeneralError(Bech32._CLASS_NAME, "separatorMisused", { bech: base.bech32 });
84
- }
85
- else if (core.BaseError.isErrorMessage(err, /lowercase or uppercase/i)) {
86
- throw new core.GeneralError(Bech32._CLASS_NAME, "lowerUpper", { bech: base.bech32 });
87
- }
88
- else if (core.BaseError.isErrorMessage(err, /must be at least/i) ||
89
- core.BaseError.isErrorMessage(err, /wrong string length/i)) {
90
- throw new core.GeneralError(Bech32._CLASS_NAME, "dataTooShort", { bech: base.bech32 });
91
- }
92
- throw new core.GeneralError(Bech32._CLASS_NAME, "decodeFailed", { bech: base.bech32 }, err);
93
- }
94
- }
95
- /**
96
- * Is the input a bech 32 address.
97
- * @param bech The value to test.
98
- * @returns True if this is potentially a match.
99
- */
100
- static isBech32(bech) {
101
- try {
102
- if (core.Is.stringValue(bech)) {
103
- const result = base.bech32.decodeToBytes(bech);
104
- return (core.Is.stringValue(result.prefix) && core.Is.uint8Array(result.bytes) && result.bytes.length > 0);
105
- }
106
- }
107
- catch { }
108
- return false;
109
- }
110
- }
111
-
112
- // Copyright 2024 IOTA Stiftung.
113
- // SPDX-License-Identifier: Apache-2.0.
114
- /**
115
- * Implementation of Ed25519.
116
- */
117
- class Ed25519 {
118
- /**
119
- * Private Key Size is the size, in bytes, of private keys as used in this package.
120
- */
121
- static PRIVATE_KEY_SIZE = 32;
122
- /**
123
- * Public Key Size is the size, in bytes, of public keys as used in this package.
124
- */
125
- static PUBLIC_KEY_SIZE = 32;
126
- /**
127
- * Runtime name for the class.
128
- * @internal
129
- */
130
- static _CLASS_NAME = "Ed25519";
131
- /**
132
- * Public returns the PublicKey corresponding to private.
133
- * @param privateKey The private key to get the corresponding public key.
134
- * @returns The public key.
135
- * @throws Error if the private key is not the correct length.
136
- */
137
- static publicKeyFromPrivateKey(privateKey) {
138
- core.Guards.uint8Array(Ed25519._CLASS_NAME, "privateKey", privateKey);
139
- if (privateKey.length !== Ed25519.PRIVATE_KEY_SIZE) {
140
- throw new core.GeneralError(Ed25519._CLASS_NAME, "privateKeyLength", {
141
- requiredSize: Ed25519.PRIVATE_KEY_SIZE,
142
- actualSize: privateKey.length
143
- });
144
- }
145
- return ed25519_js.ed25519.getPublicKey(privateKey);
146
- }
147
- /**
148
- * Sign the block with privateKey and returns a signature.
149
- * @param privateKey The private key.
150
- * @param block The block to sign.
151
- * @returns The signature.
152
- * @throws Error if the private key is not the correct length.
153
- */
154
- static sign(privateKey, block) {
155
- core.Guards.uint8Array(Ed25519._CLASS_NAME, "privateKey", privateKey);
156
- core.Guards.uint8Array(Ed25519._CLASS_NAME, "block", block);
157
- if (privateKey.length !== Ed25519.PRIVATE_KEY_SIZE) {
158
- throw new core.GeneralError(Ed25519._CLASS_NAME, "privateKeyLength", {
159
- requiredSize: Ed25519.PRIVATE_KEY_SIZE,
160
- actualSize: privateKey ? privateKey.length : 0
161
- });
162
- }
163
- return ed25519_js.ed25519.sign(block, privateKey);
164
- }
165
- /**
166
- * Verify reports whether sig is a valid signature of block by publicKey.
167
- * @param publicKey The public key to verify the signature.
168
- * @param block The block for the signature.
169
- * @param signature The signature.
170
- * @returns True if the signature matches.
171
- * @throws Error if the public key is not the correct length.
172
- */
173
- static verify(publicKey, block, signature) {
174
- core.Guards.uint8Array(Ed25519._CLASS_NAME, "publicKey", publicKey);
175
- core.Guards.uint8Array(Ed25519._CLASS_NAME, "block", block);
176
- core.Guards.uint8Array(Ed25519._CLASS_NAME, "signature", signature);
177
- if (publicKey.length !== Ed25519.PUBLIC_KEY_SIZE) {
178
- throw new core.GeneralError(Ed25519._CLASS_NAME, "publicKeyLength", {
179
- requiredSize: Ed25519.PUBLIC_KEY_SIZE,
180
- actualSize: publicKey ? publicKey.length : 0
181
- });
182
- }
183
- try {
184
- return ed25519_js.ed25519.verify(signature, block, publicKey);
185
- }
186
- catch {
187
- return false;
188
- }
189
- }
190
- /**
191
- * Convert a private key in PKCS8 format.
192
- * @param privateKey The private key to convert.
193
- * @returns The private key in PKCS8 format.
194
- */
195
- static async privateKeyToPkcs8(privateKey) {
196
- core.Guards.uint8Array(Ed25519._CLASS_NAME, "privateKey", privateKey);
197
- if (privateKey.length !== Ed25519.PRIVATE_KEY_SIZE) {
198
- throw new core.GeneralError(Ed25519._CLASS_NAME, "privateKeyLength", {
199
- requiredSize: Ed25519.PRIVATE_KEY_SIZE,
200
- actualSize: privateKey.length
201
- });
202
- }
203
- // crypto.subtle.importKey does not support Ed25519 keys in raw format.
204
- // We need to convert the key to PKCS8 format before importing.
205
- // The PKCS8 format is the raw key prefixed with the ASN.1 sequence for an Ed25519 private key.
206
- // The ASN.1 sequence is 48 46 02 01 00 30 05 06 03 2b 65 70 04 20 04 20 (0x302e020100300506032b657004220420)
207
- const pkcs8Prefix = new Uint8Array([48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32]);
208
- const fullKey = core.Uint8ArrayHelper.concat([pkcs8Prefix, privateKey]);
209
- return crypto.subtle.importKey("pkcs8", new Uint8Array(fullKey), "Ed25519", true, ["sign"]);
210
- }
211
- /**
212
- * Convert a crypto key to raw private key.
213
- * @param cryptoKey The crypto key to convert.
214
- * @returns The raw private key.
215
- */
216
- static async pkcs8ToPrivateKey(cryptoKey) {
217
- core.Guards.defined(Ed25519._CLASS_NAME, "cryptoKey", cryptoKey);
218
- // crypto.subtle.exportKey does not support Ed25519 keys in raw format.
219
- // so we export as PKCS8 and remove the ASN.1 sequence prefix.
220
- const pkcs8Bytes = await crypto.subtle.exportKey("pkcs8", cryptoKey);
221
- return new Uint8Array(pkcs8Bytes.slice(16));
222
- }
223
- }
224
-
225
- // Copyright 2024 IOTA Stiftung.
226
- // SPDX-License-Identifier: Apache-2.0.
227
- /**
228
- * Implementation of secp256k1.
229
- */
230
- class Secp256k1 {
231
- /**
232
- * Private Key Size is the size, in bytes, of private keys as used in this package.
233
- */
234
- static PRIVATE_KEY_SIZE = 32;
235
- /**
236
- * Public Key Size is the size, in bytes, of public keys as used in this package.
237
- */
238
- static PUBLIC_KEY_SIZE = 33;
239
- /**
240
- * Runtime name for the class.
241
- * @internal
242
- */
243
- static _CLASS_NAME = "Secp256k1";
244
- /**
245
- * Public returns the PublicKey corresponding to private.
246
- * @param privateKey The private key to get the corresponding public key.
247
- * @returns The public key.
248
- * @throws Error if the private key is not the correct length.
249
- */
250
- static publicKeyFromPrivateKey(privateKey) {
251
- core.Guards.uint8Array(Secp256k1._CLASS_NAME, "privateKey", privateKey);
252
- if (privateKey.length !== Secp256k1.PRIVATE_KEY_SIZE) {
253
- throw new core.GeneralError(Secp256k1._CLASS_NAME, "privateKeyLength", {
254
- requiredSize: Secp256k1.PRIVATE_KEY_SIZE,
255
- actualSize: privateKey.length
256
- });
257
- }
258
- return secp256k1_js.secp256k1.getPublicKey(privateKey);
259
- }
260
- /**
261
- * Sign the block with privateKey and returns a signature.
262
- * @param privateKey The private key.
263
- * @param block The block to sign.
264
- * @returns The signature.
265
- * @throws Error if the private key is not the correct length.
266
- */
267
- static sign(privateKey, block) {
268
- core.Guards.uint8Array(Secp256k1._CLASS_NAME, "privateKey", privateKey);
269
- core.Guards.uint8Array(Secp256k1._CLASS_NAME, "block", block);
270
- if (privateKey.length !== Secp256k1.PRIVATE_KEY_SIZE) {
271
- throw new core.GeneralError(Secp256k1._CLASS_NAME, "privateKeyLength", {
272
- requiredSize: Secp256k1.PRIVATE_KEY_SIZE,
273
- actualSize: privateKey.length
274
- });
275
- }
276
- const res = secp256k1_js.secp256k1.sign(block, privateKey, { prehash: false });
277
- return res;
278
- }
279
- /**
280
- * Verify reports whether sig is a valid signature of block by publicKey.
281
- * @param publicKey The public key to verify the signature.
282
- * @param block The block for the signature.
283
- * @param signature The signature.
284
- * @returns True if the signature matches.
285
- * @throws Error if the public key is not the correct length.
286
- */
287
- static verify(publicKey, block, signature) {
288
- core.Guards.uint8Array(Secp256k1._CLASS_NAME, "publicKey", publicKey);
289
- core.Guards.uint8Array(Secp256k1._CLASS_NAME, "block", block);
290
- core.Guards.uint8Array(Secp256k1._CLASS_NAME, "signature", signature);
291
- if (publicKey.length !== Secp256k1.PUBLIC_KEY_SIZE) {
292
- throw new core.GeneralError(Secp256k1._CLASS_NAME, "publicKeyLength", {
293
- requiredSize: Secp256k1.PUBLIC_KEY_SIZE,
294
- actualSize: publicKey ? publicKey.length : 0
295
- });
296
- }
297
- try {
298
- return secp256k1_js.secp256k1.verify(signature, block, publicKey, { prehash: false });
299
- }
300
- catch {
301
- return false;
302
- }
303
- }
304
- }
305
-
306
- // Copyright 2024 IOTA Stiftung.
307
- // SPDX-License-Identifier: Apache-2.0.
308
- /**
309
- * Class to help with Blake2B Signature scheme.
310
- */
311
- class Blake2b {
312
- /**
313
- * Blake2b 160.
314
- */
315
- static SIZE_160 = 20;
316
- /**
317
- * Blake2b 256.
318
- */
319
- static SIZE_256 = 32;
320
- /**
321
- * Blake2b 512.
322
- */
323
- static SIZE_512 = 64;
324
- /**
325
- * Runtime name for the class.
326
- * @internal
327
- */
328
- static _CLASS_NAME = "Blake2b";
329
- /**
330
- * The instance of the hash.
331
- * @internal
332
- */
333
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
334
- _instance;
335
- /**
336
- * Create a new instance of Blake2b.
337
- * @param outputLength The output length.
338
- * @param key Optional key for the hash.
339
- */
340
- constructor(outputLength, key) {
341
- this._instance = blake2_js.blake2b.create({
342
- dkLen: outputLength,
343
- key
344
- });
345
- }
346
- /**
347
- * Perform Sum 160 on the block.
348
- * @param block The block to operate on.
349
- * @param key Optional key for the hash.
350
- * @returns The sum 160 of the block.
351
- */
352
- static sum160(block, key) {
353
- core.Guards.uint8Array(Blake2b._CLASS_NAME, "block", block);
354
- return new Blake2b(Blake2b.SIZE_160, key).update(block).digest();
355
- }
356
- /**
357
- * Perform Sum 256 on the block.
358
- * @param block The block to operate on.
359
- * @param key Optional key for the hash.
360
- * @returns The sum 256 of the block.
361
- */
362
- static sum256(block, key) {
363
- core.Guards.uint8Array(Blake2b._CLASS_NAME, "block", block);
364
- return new Blake2b(Blake2b.SIZE_256, key).update(block).digest();
365
- }
366
- /**
367
- * Perform Sum 512 on the block.
368
- * @param block The block to operate on.
369
- * @param key Optional key for the hash.
370
- * @returns The sum 512 of the block.
371
- */
372
- static sum512(block, key) {
373
- core.Guards.uint8Array(Blake2b._CLASS_NAME, "block", block);
374
- return new Blake2b(Blake2b.SIZE_512, key).update(block).digest();
375
- }
376
- /**
377
- * Update the hash with the block.
378
- * @param block The block to update the hash with.
379
- * @returns The instance for chaining.
380
- */
381
- update(block) {
382
- core.Guards.uint8Array(Blake2b._CLASS_NAME, "block", block);
383
- this._instance.update(block);
384
- return this;
385
- }
386
- /**
387
- * Get the digest for the hash.
388
- * @returns The instance for chaining.
389
- */
390
- digest() {
391
- return this._instance.digest();
392
- }
393
- }
394
-
395
- // Copyright 2024 IOTA Stiftung.
396
- // SPDX-License-Identifier: Apache-2.0.
397
- /**
398
- * Class to help with bip32 paths.
399
- */
400
- class Bip32Path {
401
- /**
402
- * The path.
403
- * @internal
404
- */
405
- _path;
406
- /**
407
- * Create a new instance of Bip32Path.
408
- * @param initialPath Initial path to create.
409
- */
410
- constructor(initialPath) {
411
- if (initialPath) {
412
- this._path = initialPath.split("/");
413
- if (this._path[0] === "m") {
414
- this._path.shift();
415
- }
416
- }
417
- else {
418
- this._path = [];
419
- }
420
- }
421
- /**
422
- * Construct a new path by cloning an existing one.
423
- * @param bip32Path The path to clone.
424
- * @returns A new instance of Bip32Path.
425
- */
426
- static fromPath(bip32Path) {
427
- const p = new Bip32Path();
428
- p._path = bip32Path._path.slice();
429
- return p;
430
- }
431
- /**
432
- * Converts the path to a string.
433
- * @returns The path as a string.
434
- */
435
- toString() {
436
- return this._path.length > 0 ? `m/${this._path.join("/")}` : "m";
437
- }
438
- /**
439
- * Push a new index on to the path.
440
- * @param index The index to add to the path.
441
- */
442
- push(index) {
443
- this._path.push(`${index}`);
444
- }
445
- /**
446
- * Push a new hardened index on to the path.
447
- * @param index The index to add to the path.
448
- */
449
- pushHardened(index) {
450
- this._path.push(`${index}'`);
451
- }
452
- /**
453
- * Pop an index from the path.
454
- */
455
- pop() {
456
- this._path.pop();
457
- }
458
- /**
459
- * Get the segments.
460
- * @returns The segments as numbers.
461
- */
462
- numberSegments() {
463
- return this._path.map(p => Number.parseInt(p, 10));
464
- }
465
- }
466
-
467
- // Copyright 2024 IOTA Stiftung.
468
- // SPDX-License-Identifier: Apache-2.0.
469
- /**
470
- * The names of the key types.
471
- */
472
- // eslint-disable-next-line @typescript-eslint/naming-convention
473
- const KeyType = {
474
- /**
475
- * Ed25519.
476
- */
477
- Ed25519: 0,
478
- /**
479
- * Secp256k1.
480
- */
481
- Secp256k1: 1
482
- };
483
-
484
- // Copyright 2024 IOTA Stiftung.
485
- // SPDX-License-Identifier: Apache-2.0.
486
- /**
487
- * Class to help with slip0010 key derivation
488
- * https://github.com/satoshilabs/slips/blob/master/slip-0010.md.
489
- */
490
- class Slip0010 {
491
- /**
492
- * Runtime name for the class.
493
- * @internal
494
- */
495
- static _CLASS_NAME = "Slip0010";
496
- /**
497
- * Get the master key from the seed.
498
- * @param seed The seed to generate the master key from.
499
- * @param keyType The key type.
500
- * @returns The key and chain code.
501
- * @throws If the seed is invalid.
502
- */
503
- static getMasterKeyFromSeed(seed, keyType = KeyType.Ed25519) {
504
- try {
505
- const masterKey = keyType === KeyType.Ed25519
506
- ? slip10_js.HDKey.fromMasterSeed(seed)
507
- : bip32.HDKey.fromMasterSeed(seed);
508
- return {
509
- privateKey: masterKey.privateKey ?? new Uint8Array(),
510
- chainCode: masterKey.chainCode ?? new Uint8Array()
511
- };
512
- }
513
- catch (error) {
514
- throw new core.GeneralError(Slip0010._CLASS_NAME, "invalidSeed", { seed: core.Converter.bytesToUtf8(seed) }, error);
515
- }
516
- }
517
- /**
518
- * Derive a key from the path.
519
- * @param seed The seed.
520
- * @param path The path.
521
- * @param keyType The key type.
522
- * @returns The key and chain code.
523
- */
524
- static derivePath(seed, path, keyType = KeyType.Ed25519) {
525
- const keyOpts = Slip0010.getMasterKeyFromSeed(seed, keyType);
526
- if (keyType === KeyType.Ed25519) {
527
- const hdKey = new slip10_js.HDKey(keyOpts);
528
- const derivedKey = hdKey.derive(path.toString());
529
- return {
530
- privateKey: derivedKey.privateKey,
531
- chainCode: derivedKey.chainCode
532
- };
533
- }
534
- const hdKey = new bip32.HDKey(keyOpts);
535
- const derivedKey = hdKey.derive(path.toString());
536
- return {
537
- privateKey: derivedKey.privateKey ?? new Uint8Array(),
538
- chainCode: derivedKey.chainCode ?? new Uint8Array()
539
- };
540
- }
541
- /**
542
- * Get the public key from the private key.
543
- * @param privateKey The private key.
544
- * @param keyType The key type.
545
- * @param withZeroByte Include a zero bute prefix.
546
- * @returns The public key.
547
- */
548
- static getPublicKey(privateKey, keyType = KeyType.Ed25519, withZeroByte = true) {
549
- const signPk = keyType === KeyType.Ed25519
550
- ? Ed25519.publicKeyFromPrivateKey(privateKey)
551
- : Secp256k1.publicKeyFromPrivateKey(privateKey);
552
- if (withZeroByte) {
553
- const arr = new Uint8Array(1 + signPk.length);
554
- arr[0] = 0;
555
- arr.set(signPk, 1);
556
- return arr;
557
- }
558
- return signPk;
559
- }
560
- }
561
-
562
- // Copyright 2024 IOTA Stiftung.
563
- // SPDX-License-Identifier: Apache-2.0.
564
- /**
565
- * Implementation of Bip44 for address generation.
566
- */
567
- class Bip44 {
568
- /**
569
- * Runtime name for the class.
570
- * @internal
571
- */
572
- static _CLASS_NAME = "Bip44";
573
- /**
574
- * Generate a bip44 key pair from the seed and parts.
575
- * @param seed The account seed.
576
- * @param keyType The key type.
577
- * @param coinType The coin type.
578
- * @param accountIndex The account index.
579
- * @param isInternal Is this an internal address.
580
- * @param addressIndex The address index.
581
- * @returns The key pair.
582
- * @throws Error if the address type is not supported.
583
- */
584
- static keyPair(seed, keyType, coinType, accountIndex, isInternal, addressIndex) {
585
- const bip44Path = Bip44.path(coinType, accountIndex, isInternal, addressIndex);
586
- const keys = Slip0010.derivePath(seed, bip44Path);
587
- if (keyType === KeyType.Ed25519) {
588
- const publicKey = Ed25519.publicKeyFromPrivateKey(keys.privateKey);
589
- return {
590
- privateKey: keys.privateKey,
591
- publicKey
592
- };
593
- }
594
- else if (keyType === KeyType.Secp256k1) {
595
- const publicKey = Secp256k1.publicKeyFromPrivateKey(keys.privateKey);
596
- return {
597
- privateKey: keys.privateKey,
598
- publicKey
599
- };
600
- }
601
- throw new core.GeneralError(Bip44._CLASS_NAME, "unsupportedKeyType", { keyType });
602
- }
603
- /**
604
- * Generate a bip44 path based on all its parts.
605
- * @param coinType The coin type.
606
- * @param accountIndex The account index.
607
- * @param isInternal Is this an internal address.
608
- * @param addressIndex The address index.
609
- * @returns The generated path.
610
- */
611
- static path(coinType, accountIndex, isInternal, addressIndex) {
612
- const bip32Path = new Bip32Path(Bip44.basePath(coinType));
613
- bip32Path.pushHardened(accountIndex);
614
- bip32Path.pushHardened(isInternal ? 1 : 0);
615
- bip32Path.pushHardened(addressIndex);
616
- return bip32Path;
617
- }
618
- /**
619
- * Create a bip44 base path for the provided coin type.
620
- * @param coinType The coin type.
621
- * @returns The bip44 address base path.
622
- */
623
- static basePath(coinType) {
624
- return `m/44'/${coinType}'`;
625
- }
626
- /**
627
- * Generate an address from the seed and parts.
628
- * @param seed The account seed.
629
- * @param keyType The key type.
630
- * @param coinType The coin type.
631
- * @param accountIndex The account index.
632
- * @param isInternal Is this an internal address.
633
- * @param addressIndex The address index.
634
- * @returns The generated path and the associated keypair.
635
- */
636
- static address(seed, keyType, coinType, accountIndex, isInternal, addressIndex) {
637
- const keyPair = Bip44.keyPair(seed, keyType, coinType, accountIndex, isInternal, addressIndex);
638
- const addressData = Blake2b.sum256(keyPair.publicKey);
639
- return {
640
- address: core.Converter.bytesToHex(addressData, true),
641
- ...keyPair
642
- };
643
- }
644
- /**
645
- * Generate a bech32 address from the seed and parts.
646
- * @param seed The account seed.
647
- * @param keyType The key type.
648
- * @param hrp The human readable part of the address.
649
- * @param coinType The coin type.
650
- * @param accountIndex The account index.
651
- * @param isInternal Is this an internal address.
652
- * @param addressIndex The address index.
653
- * @returns The generated path and the associated keypair.
654
- */
655
- static addressBech32(seed, keyType, hrp, coinType, accountIndex, isInternal, addressIndex) {
656
- const keyPair = Bip44.keyPair(seed, keyType, coinType, accountIndex, isInternal, addressIndex);
657
- const addressData = Blake2b.sum256(keyPair.publicKey);
658
- const bech32Data = new Uint8Array(1 + addressData.length);
659
- bech32Data[0] = keyType;
660
- bech32Data.set(addressData, 1);
661
- return {
662
- address: Bech32.encode(hrp, bech32Data),
663
- ...keyPair
664
- };
665
- }
666
- }
667
-
668
- // Copyright 2024 IOTA Stiftung.
669
- // SPDX-License-Identifier: Apache-2.0.
670
- /**
671
- * Implementation of the ChaCha20Poly1305 cipher.
672
- */
673
- class ChaCha20Poly1305 {
674
- /**
675
- * Runtime name for the class.
676
- * @internal
677
- */
678
- static _CLASS_NAME = "ChaCha20Poly1305";
679
- /**
680
- * The cipher instance.
681
- * @internal
682
- */
683
- _instance;
684
- /**
685
- * Create a new instance of ChaCha20Poly1305.
686
- * @param key The key.
687
- * @param nonce The nonce.
688
- * @param aad The additional authenticated data.
689
- */
690
- constructor(key, nonce, aad) {
691
- core.Guards.uint8Array(ChaCha20Poly1305._CLASS_NAME, "key", key);
692
- core.Guards.uint8Array(ChaCha20Poly1305._CLASS_NAME, "nonce", nonce);
693
- this._instance = chacha_js.chacha20poly1305(key, nonce, aad);
694
- }
695
- /**
696
- * Encrypt the block.
697
- * @param block The block to encrypt.
698
- * @returns The block encrypted.
699
- */
700
- encrypt(block) {
701
- core.Guards.uint8Array(ChaCha20Poly1305._CLASS_NAME, "block", block);
702
- return this._instance.encrypt(block);
703
- }
704
- /**
705
- * Decrypt the block.
706
- * @param block The block to decrypt.
707
- * @returns The block decrypted.
708
- */
709
- decrypt(block) {
710
- core.Guards.uint8Array(ChaCha20Poly1305._CLASS_NAME, "block", block);
711
- return this._instance.decrypt(block);
712
- }
713
- }
714
-
715
- // Copyright 2024 IOTA Stiftung.
716
- // SPDX-License-Identifier: Apache-2.0.
717
- /**
718
- * Implementation of X25519.
719
- */
720
- class X25519 {
721
- /**
722
- * Runtime name for the class.
723
- * @internal
724
- */
725
- static _CLASS_NAME = "X25519";
726
- /**
727
- * Convert Ed25519 private key to X25519 private key.
728
- * @param ed25519PrivateKey The ed25519 private key to convert.
729
- * @returns The x25519 private key.
730
- */
731
- static convertPrivateKeyToX25519(ed25519PrivateKey) {
732
- core.Guards.uint8Array(X25519._CLASS_NAME, "ed25519PrivateKey", ed25519PrivateKey);
733
- return ed25519_js.ed25519.utils.toMontgomerySecret(ed25519PrivateKey.slice(0, Ed25519.PRIVATE_KEY_SIZE));
734
- }
735
- /**
736
- * Convert Ed25519 public key to X25519 public key.
737
- * @param ed25519PublicKey The ed25519 public key to convert.
738
- * @returns The x25519 public key.
739
- * @throws GeneralError On invalid public key.
740
- */
741
- static convertPublicKeyToX25519(ed25519PublicKey) {
742
- core.Guards.uint8Array(X25519._CLASS_NAME, "ed25519PublicKey", ed25519PublicKey);
743
- return ed25519_js.ed25519.utils.toMontgomery(ed25519PublicKey);
744
- }
745
- }
746
-
747
- // Copyright 2024 IOTA Stiftung.
748
- // SPDX-License-Identifier: Apache-2.0.
749
- /**
750
- * Implementation of Zip215.
751
- */
752
- class Zip215 {
753
- /**
754
- * Runtime name for the class.
755
- * @internal
756
- */
757
- static _CLASS_NAME = "Zip215";
758
- /**
759
- * Verify reports whether sig is a valid signature of block by
760
- * publicKey, using precisely-specified validation criteria (ZIP 215) suitable
761
- * for use in consensus-critical contexts.
762
- * @param publicKey The public key for the block.
763
- * @param block The block content to validate.
764
- * @param sig The signature to verify.
765
- * @returns True if the signature is valid.
766
- */
767
- static verify(publicKey, block, sig) {
768
- core.Guards.uint8Array(Zip215._CLASS_NAME, "publicKey", publicKey);
769
- core.Guards.uint8Array(Zip215._CLASS_NAME, "block", block);
770
- core.Guards.uint8Array(Zip215._CLASS_NAME, "sig", sig);
771
- if (publicKey.length !== Ed25519.PUBLIC_KEY_SIZE) {
772
- return false;
773
- }
774
- return ed25519_js.ed25519.verify(sig, block, publicKey, { zip215: true });
775
- }
776
- }
777
-
778
- // Copyright 2024 IOTA Stiftung.
779
- // SPDX-License-Identifier: Apache-2.0.
780
- /**
781
- * Class to help with Blake3 Signature scheme.
782
- */
783
- class Blake3 {
784
- /**
785
- * Blake3 256.
786
- */
787
- static SIZE_256 = 32;
788
- /**
789
- * Blake3 512.
790
- */
791
- static SIZE_512 = 64;
792
- /**
793
- * Runtime name for the class.
794
- * @internal
795
- */
796
- static _CLASS_NAME = "Blake3";
797
- /**
798
- * The instance of the hash.
799
- * @internal
800
- */
801
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
802
- _instance;
803
- /**
804
- * Create a new instance of Blake3.
805
- * @param outputLength The output length.
806
- * @param key Optional key for the hash.
807
- */
808
- constructor(outputLength, key) {
809
- this._instance = blake3_js.blake3.create({
810
- dkLen: outputLength,
811
- key
812
- });
813
- }
814
- /**
815
- * Perform Sum 256 on the block.
816
- * @param block The block to operate on.
817
- * @param key Optional key for the hash.
818
- * @returns The sum 256 of the block.
819
- */
820
- static sum256(block, key) {
821
- core.Guards.uint8Array(Blake3._CLASS_NAME, "block", block);
822
- return new Blake3(Blake3.SIZE_256, key).update(block).digest();
823
- }
824
- /**
825
- * Perform Sum 512 on the block.
826
- * @param block The block to operate on.
827
- * @param key Optional key for the hash.
828
- * @returns The sum 512 of the block.
829
- */
830
- static sum512(block, key) {
831
- core.Guards.uint8Array(Blake3._CLASS_NAME, "block", block);
832
- return new Blake3(Blake3.SIZE_512, key).update(block).digest();
833
- }
834
- /**
835
- * Update the hash with the block.
836
- * @param block The block to update the hash with.
837
- * @returns The instance for chaining.
838
- */
839
- update(block) {
840
- core.Guards.uint8Array(Blake3._CLASS_NAME, "block", block);
841
- this._instance.update(block);
842
- return this;
843
- }
844
- /**
845
- * Get the digest for the hash.
846
- * @returns The instance for chaining.
847
- */
848
- digest() {
849
- return this._instance.digest();
850
- }
851
- }
852
-
853
- // Copyright 2024 IOTA Stiftung.
854
- // SPDX-License-Identifier: Apache-2.0.
855
- /**
856
- * Class to help with HmacSha1 scheme.
857
- */
858
- class HmacSha1 {
859
- /**
860
- * Runtime name for the class.
861
- * @internal
862
- */
863
- static _CLASS_NAME = "HmacSha1";
864
- /**
865
- * The instance of the hash.
866
- * @internal
867
- */
868
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
869
- _instance;
870
- /**
871
- * Create a new instance of HmacSha1.
872
- * @param key The key for the hmac.
873
- */
874
- constructor(key) {
875
- this._instance = hmac_js.hmac.create(legacy_js.sha1, key);
876
- }
877
- /**
878
- * Perform Sum on the block.
879
- * @param key The key for the hmac.
880
- * @param block The block to operate on.
881
- * @returns The sum of the block.
882
- */
883
- static sum(key, block) {
884
- core.Guards.uint8Array(HmacSha1._CLASS_NAME, "key", key);
885
- core.Guards.uint8Array(HmacSha1._CLASS_NAME, "block", block);
886
- return new HmacSha1(key).update(block).digest();
887
- }
888
- /**
889
- * Update the hash with the block.
890
- * @param block The block to update the hash with.
891
- * @returns The instance for chaining.
892
- */
893
- update(block) {
894
- core.Guards.uint8Array(HmacSha1._CLASS_NAME, "block", block);
895
- this._instance.update(block);
896
- return this;
897
- }
898
- /**
899
- * Get the digest for the hash.
900
- * @returns The instance for chaining.
901
- */
902
- digest() {
903
- return this._instance.digest();
904
- }
905
- }
906
-
907
- // Copyright 2024 IOTA Stiftung.
908
- // SPDX-License-Identifier: Apache-2.0.
909
- /**
910
- * Class to help with HmacSha256 scheme.
911
- */
912
- class HmacSha256 {
913
- /**
914
- * Sha256 256.
915
- */
916
- static SIZE_256 = 256;
917
- /**
918
- * Sha256 224.
919
- */
920
- static SIZE_224 = 224;
921
- /**
922
- * Runtime name for the class.
923
- * @internal
924
- */
925
- static _CLASS_NAME = "HmacSha256";
926
- /**
927
- * The instance of the hash.
928
- * @internal
929
- */
930
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
931
- _instance;
932
- /**
933
- * Create a new instance of HmacSha256.
934
- * @param key The key for the hmac.
935
- * @param bits The number of bits.
936
- */
937
- constructor(key, bits = HmacSha256.SIZE_256) {
938
- if (bits !== HmacSha256.SIZE_224 && bits !== HmacSha256.SIZE_256) {
939
- throw new core.GeneralError(HmacSha256._CLASS_NAME, "bitSize", { bitSize: bits });
940
- }
941
- this._instance = hmac_js.hmac.create(bits === HmacSha256.SIZE_256 ? sha2_js.sha256 : sha2_js.sha224, key);
942
- }
943
- /**
944
- * Perform Sum 224 on the block.
945
- * @param key The key for the hmac.
946
- * @param block The block to operate on.
947
- * @returns The sum 224 of the block.
948
- */
949
- static sum224(key, block) {
950
- core.Guards.uint8Array(HmacSha256._CLASS_NAME, "key", key);
951
- core.Guards.uint8Array(HmacSha256._CLASS_NAME, "block", block);
952
- const instance = new HmacSha256(key, HmacSha256.SIZE_224);
953
- instance.update(block);
954
- return instance.digest();
955
- }
956
- /**
957
- * Perform Sum 256 on the block.
958
- * @param key The key for the hmac.
959
- * @param block The block to operate on.
960
- * @returns The sum 256 of the block.
961
- */
962
- static sum256(key, block) {
963
- core.Guards.uint8Array(HmacSha256._CLASS_NAME, "key", key);
964
- core.Guards.uint8Array(HmacSha256._CLASS_NAME, "block", block);
965
- const instance = new HmacSha256(key, HmacSha256.SIZE_256);
966
- instance.update(block);
967
- return instance.digest();
968
- }
969
- /**
970
- * Update the hash with the block.
971
- * @param block The block to update the hash with.
972
- * @returns The instance for chaining.
973
- */
974
- update(block) {
975
- core.Guards.uint8Array(HmacSha256._CLASS_NAME, "block", block);
976
- this._instance.update(block);
977
- return this;
978
- }
979
- /**
980
- * Get the digest for the hash.
981
- * @returns The instance for chaining.
982
- */
983
- digest() {
984
- return this._instance.digest();
985
- }
986
- }
987
-
988
- // Copyright 2024 IOTA Stiftung.
989
- // SPDX-License-Identifier: Apache-2.0.
990
- /* eslint-disable camelcase */
991
- /**
992
- * Class to help with HmacSha512 scheme.
993
- */
994
- class HmacSha512 {
995
- /**
996
- * Sha512 224.
997
- */
998
- static SIZE_224 = 224;
999
- /**
1000
- * Sha512 256.
1001
- */
1002
- static SIZE_256 = 256;
1003
- /**
1004
- * Sha512 384.
1005
- */
1006
- static SIZE_384 = 384;
1007
- /**
1008
- * Sha512 512.
1009
- */
1010
- static SIZE_512 = 512;
1011
- /**
1012
- * Runtime name for the class.
1013
- * @internal
1014
- */
1015
- static _CLASS_NAME = "HmacSha512";
1016
- /**
1017
- * The instance of the hash.
1018
- * @internal
1019
- */
1020
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1021
- _instance;
1022
- /**
1023
- * Create a new instance of HmacSha512.
1024
- * @param key The key for the hmac.
1025
- * @param bits The number of bits.
1026
- */
1027
- constructor(key, bits = HmacSha512.SIZE_512) {
1028
- if (bits !== HmacSha512.SIZE_224 &&
1029
- bits !== HmacSha512.SIZE_256 &&
1030
- bits !== HmacSha512.SIZE_384 &&
1031
- bits !== HmacSha512.SIZE_512) {
1032
- throw new core.GeneralError(HmacSha512._CLASS_NAME, "bitSize", { bitSize: bits });
1033
- }
1034
- if (bits === HmacSha512.SIZE_224) {
1035
- this._instance = hmac_js.hmac.create(sha2_js.sha512_224, key);
1036
- }
1037
- else if (bits === HmacSha512.SIZE_256) {
1038
- this._instance = hmac_js.hmac.create(sha2_js.sha512_256, key);
1039
- }
1040
- else if (bits === HmacSha512.SIZE_384) {
1041
- this._instance = hmac_js.hmac.create(sha2_js.sha384, key);
1042
- }
1043
- else {
1044
- this._instance = hmac_js.hmac.create(sha2_js.sha512, key);
1045
- }
1046
- }
1047
- /**
1048
- * Perform Sum 512 on the block.
1049
- * @param key The key for the hmac.
1050
- * @param block The block to operate on.
1051
- * @returns The sum 512 of the block.
1052
- */
1053
- static sum512(key, block) {
1054
- core.Guards.uint8Array(HmacSha512._CLASS_NAME, "key", key);
1055
- core.Guards.uint8Array(HmacSha512._CLASS_NAME, "block", block);
1056
- const instance = new HmacSha512(key, HmacSha512.SIZE_512);
1057
- instance.update(block);
1058
- return instance.digest();
1059
- }
1060
- /**
1061
- * Perform Sum 384 on the block.
1062
- * @param key The key for the hmac.
1063
- * @param block The block to operate on.
1064
- * @returns The sum 384 of the block.
1065
- */
1066
- static sum384(key, block) {
1067
- core.Guards.uint8Array(HmacSha512._CLASS_NAME, "key", key);
1068
- core.Guards.uint8Array(HmacSha512._CLASS_NAME, "block", block);
1069
- const instance = new HmacSha512(key, HmacSha512.SIZE_384);
1070
- instance.update(block);
1071
- return instance.digest();
1072
- }
1073
- /**
1074
- * Perform Sum 256 on the block.
1075
- * @param key The key for the hmac.
1076
- * @param block The block to operate on.
1077
- * @returns The sum 256 of the block.
1078
- */
1079
- static sum256(key, block) {
1080
- core.Guards.uint8Array(HmacSha512._CLASS_NAME, "key", key);
1081
- core.Guards.uint8Array(HmacSha512._CLASS_NAME, "block", block);
1082
- const instance = new HmacSha512(key, HmacSha512.SIZE_256);
1083
- instance.update(block);
1084
- return instance.digest();
1085
- }
1086
- /**
1087
- * Perform Sum 224 on the block.
1088
- * @param key The key for the hmac.
1089
- * @param block The block to operate on.
1090
- * @returns The sum 224 of the block.
1091
- */
1092
- static sum224(key, block) {
1093
- core.Guards.uint8Array(HmacSha512._CLASS_NAME, "key", key);
1094
- core.Guards.uint8Array(HmacSha512._CLASS_NAME, "block", block);
1095
- const instance = new HmacSha512(key, HmacSha512.SIZE_224);
1096
- instance.update(block);
1097
- return instance.digest();
1098
- }
1099
- /**
1100
- * Update the hash with the block.
1101
- * @param block The block to update the hash with.
1102
- * @returns The instance for chaining.
1103
- */
1104
- update(block) {
1105
- core.Guards.uint8Array(HmacSha512._CLASS_NAME, "block", block);
1106
- this._instance.update(block);
1107
- return this;
1108
- }
1109
- /**
1110
- * Get the digest for the hash.
1111
- * @returns The instance for chaining.
1112
- */
1113
- digest() {
1114
- return this._instance.digest();
1115
- }
1116
- }
1117
-
1118
- // Copyright 2024 IOTA Stiftung.
1119
- // SPDX-License-Identifier: Apache-2.0.
1120
- /**
1121
- * Implementation of the password based key derivation function 2.
1122
- */
1123
- class Pbkdf2 {
1124
- /**
1125
- * Runtime name for the class.
1126
- * @internal
1127
- */
1128
- static _CLASS_NAME = "Pbkdf2";
1129
- /**
1130
- * Derive a key from the parameters using Sha256.
1131
- * @param password The password to derive the key from.
1132
- * @param salt The salt for the derivation.
1133
- * @param iterations Number of iterations to perform.
1134
- * @param keyLength The length of the key to derive.
1135
- * @returns The derived key.
1136
- */
1137
- static sha256(password, salt, iterations, keyLength) {
1138
- core.Guards.uint8Array(Pbkdf2._CLASS_NAME, "password", password);
1139
- core.Guards.uint8Array(Pbkdf2._CLASS_NAME, "salt", salt);
1140
- core.Guards.number(Pbkdf2._CLASS_NAME, "iterations", iterations);
1141
- core.Guards.number(Pbkdf2._CLASS_NAME, "keyLength", keyLength);
1142
- return pbkdf2_js.pbkdf2(sha2_js.sha256, password, salt, { c: iterations, dkLen: keyLength });
1143
- }
1144
- /**
1145
- * Derive a key from the parameters using Sha512.
1146
- * @param password The password to derive the key from.
1147
- * @param salt The salt for the derivation.
1148
- * @param iterations Number of iterations to perform.
1149
- * @param keyLength The length of the key to derive.
1150
- * @returns The derived key.
1151
- */
1152
- static sha512(password, salt, iterations, keyLength) {
1153
- core.Guards.uint8Array(Pbkdf2._CLASS_NAME, "password", password);
1154
- core.Guards.uint8Array(Pbkdf2._CLASS_NAME, "salt", salt);
1155
- core.Guards.number(Pbkdf2._CLASS_NAME, "iterations", iterations);
1156
- core.Guards.number(Pbkdf2._CLASS_NAME, "keyLength", keyLength);
1157
- return pbkdf2_js.pbkdf2(sha2_js.sha512, password, salt, { c: iterations, dkLen: keyLength });
1158
- }
1159
- }
1160
-
1161
- // Copyright 2024 IOTA Stiftung.
1162
- // SPDX-License-Identifier: Apache-2.0.
1163
- /**
1164
- * Perform a SHA-1 hash on the block.
1165
- */
1166
- class Sha1 {
1167
- /**
1168
- * Runtime name for the class.
1169
- * @internal
1170
- */
1171
- static _CLASS_NAME = "Sha1";
1172
- /**
1173
- * The instance of the hash.
1174
- * @internal
1175
- */
1176
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1177
- _instance;
1178
- /**
1179
- * Create a new instance of Sha1.
1180
- */
1181
- constructor() {
1182
- this._instance = legacy_js.sha1.create();
1183
- }
1184
- /**
1185
- * Perform Sum on the block.
1186
- * @param block The block to operate on.
1187
- * @returns The sum of the block.
1188
- */
1189
- static sum(block) {
1190
- core.Guards.uint8Array(Sha1._CLASS_NAME, "block", block);
1191
- return new Sha1().update(block).digest();
1192
- }
1193
- /**
1194
- * Update the hash with the block.
1195
- * @param block The block to update the hash with.
1196
- * @returns The instance for chaining.
1197
- */
1198
- update(block) {
1199
- core.Guards.uint8Array(Sha1._CLASS_NAME, "block", block);
1200
- this._instance.update(block);
1201
- return this;
1202
- }
1203
- /**
1204
- * Get the digest for the hash.
1205
- * @returns The instance for chaining.
1206
- */
1207
- digest() {
1208
- return this._instance.digest();
1209
- }
1210
- }
1211
-
1212
- // Copyright 2024 IOTA Stiftung.
1213
- // SPDX-License-Identifier: Apache-2.0.
1214
- /**
1215
- * Perform a SHA-256 hash on the block.
1216
- */
1217
- class Sha256 {
1218
- /**
1219
- * Sha256 256.
1220
- */
1221
- static SIZE_256 = 256;
1222
- /**
1223
- * Sha256 224.
1224
- */
1225
- static SIZE_224 = 224;
1226
- /**
1227
- * Runtime name for the class.
1228
- * @internal
1229
- */
1230
- static _CLASS_NAME = "Sha256";
1231
- /**
1232
- * The instance of the hash.
1233
- * @internal
1234
- */
1235
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1236
- _instance;
1237
- /**
1238
- * Create a new instance of Sha256.
1239
- * @param bits The number of bits.
1240
- */
1241
- constructor(bits = Sha256.SIZE_256) {
1242
- if (bits !== Sha256.SIZE_224 && bits !== Sha256.SIZE_256) {
1243
- throw new core.GeneralError(Sha256._CLASS_NAME, "bitSize", { bitSize: bits });
1244
- }
1245
- this._instance = bits === Sha256.SIZE_256 ? sha2_js.sha256.create() : sha2_js.sha224.create();
1246
- }
1247
- /**
1248
- * Perform Sum 256 on the block.
1249
- * @param block The block to operate on.
1250
- * @returns The sum 256 of the block.
1251
- */
1252
- static sum256(block) {
1253
- const b2b = new Sha256(Sha256.SIZE_256);
1254
- b2b.update(block);
1255
- return b2b.digest();
1256
- }
1257
- /**
1258
- * Perform Sum 224 on the block.
1259
- * @param block The block to operate on.
1260
- * @returns The sum 224 of the block.
1261
- */
1262
- static sum224(block) {
1263
- const b2b = new Sha256(Sha256.SIZE_224);
1264
- b2b.update(block);
1265
- return b2b.digest();
1266
- }
1267
- /**
1268
- * Update the hash with the block.
1269
- * @param block The block to update the hash with.
1270
- * @returns The instance for chaining.
1271
- */
1272
- update(block) {
1273
- core.Guards.uint8Array(Sha256._CLASS_NAME, "block", block);
1274
- this._instance.update(block);
1275
- return this;
1276
- }
1277
- /**
1278
- * Get the digest for the hash.
1279
- * @returns The instance for chaining.
1280
- */
1281
- digest() {
1282
- return this._instance.digest();
1283
- }
1284
- }
1285
-
1286
- // Copyright 2024 IOTA Stiftung.
1287
- // SPDX-License-Identifier: Apache-2.0.
1288
- // eslint-disable-next-line camelcase
1289
- /**
1290
- * Perform a SHA-3 hash on the block.
1291
- */
1292
- class Sha3 {
1293
- /**
1294
- * Sha3 224.
1295
- */
1296
- static SIZE_224 = 224;
1297
- /**
1298
- * Sha3 256.
1299
- */
1300
- static SIZE_256 = 256;
1301
- /**
1302
- * Sha3 384.
1303
- */
1304
- static SIZE_384 = 384;
1305
- /**
1306
- * Sha3 512.
1307
- */
1308
- static SIZE_512 = 512;
1309
- /**
1310
- * Runtime name for the class.
1311
- * @internal
1312
- */
1313
- static _CLASS_NAME = "Sha3";
1314
- /**
1315
- * The instance of the hash.
1316
- * @internal
1317
- */
1318
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1319
- _instance;
1320
- /**
1321
- * Create a new instance of Sha3.
1322
- * @param bits The number of bits.
1323
- */
1324
- constructor(bits = Sha3.SIZE_256) {
1325
- if (bits !== Sha3.SIZE_224 &&
1326
- bits !== Sha3.SIZE_256 &&
1327
- bits !== Sha3.SIZE_384 &&
1328
- bits !== Sha3.SIZE_512) {
1329
- throw new core.GeneralError(Sha3._CLASS_NAME, "bitSize", { bitSize: bits });
1330
- }
1331
- if (bits === Sha3.SIZE_224) {
1332
- // eslint-disable-next-line camelcase
1333
- this._instance = sha3_js.sha3_224.create();
1334
- }
1335
- else if (bits === Sha3.SIZE_256) {
1336
- // eslint-disable-next-line camelcase
1337
- this._instance = sha3_js.sha3_256.create();
1338
- }
1339
- else if (bits === Sha3.SIZE_384) {
1340
- // eslint-disable-next-line camelcase
1341
- this._instance = sha3_js.sha3_384.create();
1342
- }
1343
- else {
1344
- // eslint-disable-next-line camelcase
1345
- this._instance = sha3_js.sha3_512.create();
1346
- }
1347
- }
1348
- /**
1349
- * Perform Sum 256 on the block.
1350
- * @param block The block to operate on.
1351
- * @returns The sum 256 of the block.
1352
- */
1353
- static sum256(block) {
1354
- const b2b = new Sha3(Sha3.SIZE_256);
1355
- b2b.update(block);
1356
- return b2b.digest();
1357
- }
1358
- /**
1359
- * Perform Sum 224 on the block.
1360
- * @param block The block to operate on.
1361
- * @returns The sum 224 of the block.
1362
- */
1363
- static sum224(block) {
1364
- const b2b = new Sha3(Sha3.SIZE_224);
1365
- b2b.update(block);
1366
- return b2b.digest();
1367
- }
1368
- /**
1369
- * Perform Sum 384 on the block.
1370
- * @param block The block to operate on.
1371
- * @returns The sum 384 of the block.
1372
- */
1373
- static sum384(block) {
1374
- const b2b = new Sha3(Sha3.SIZE_384);
1375
- b2b.update(block);
1376
- return b2b.digest();
1377
- }
1378
- /**
1379
- * Perform Sum 512 on the block.
1380
- * @param block The block to operate on.
1381
- * @returns The sum 512 of the block.
1382
- */
1383
- static sum512(block) {
1384
- const b2b = new Sha3(Sha3.SIZE_512);
1385
- b2b.update(block);
1386
- return b2b.digest();
1387
- }
1388
- /**
1389
- * Update the hash with the block.
1390
- * @param block The block to update the hash with.
1391
- * @returns The instance for chaining.
1392
- */
1393
- update(block) {
1394
- core.Guards.uint8Array(Sha3._CLASS_NAME, "block", block);
1395
- this._instance.update(block);
1396
- return this;
1397
- }
1398
- /**
1399
- * Get the digest for the hash.
1400
- * @returns The instance for chaining.
1401
- */
1402
- digest() {
1403
- return this._instance.digest();
1404
- }
1405
- }
1406
-
1407
- // Copyright 2024 IOTA Stiftung.
1408
- // SPDX-License-Identifier: Apache-2.0.
1409
- /* eslint-disable camelcase */
1410
- /**
1411
- * Perform a SHA-512 hash on the block.
1412
- */
1413
- class Sha512 {
1414
- /**
1415
- * Sha512 224.
1416
- */
1417
- static SIZE_224 = 224;
1418
- /**
1419
- * Sha512 256.
1420
- */
1421
- static SIZE_256 = 256;
1422
- /**
1423
- * Sha512 384.
1424
- */
1425
- static SIZE_384 = 384;
1426
- /**
1427
- * Sha512 512.
1428
- */
1429
- static SIZE_512 = 512;
1430
- /**
1431
- * Runtime name for the class.
1432
- * @internal
1433
- */
1434
- static _CLASS_NAME = "Sha512";
1435
- /**
1436
- * The instance of the hash.
1437
- * @internal
1438
- */
1439
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1440
- _instance;
1441
- /**
1442
- * Create a new instance of Sha512.
1443
- * @param bits The number of bits.
1444
- */
1445
- constructor(bits = Sha512.SIZE_512) {
1446
- if (bits !== Sha512.SIZE_224 &&
1447
- bits !== Sha512.SIZE_256 &&
1448
- bits !== Sha512.SIZE_384 &&
1449
- bits !== Sha512.SIZE_512) {
1450
- throw new core.GeneralError(Sha512._CLASS_NAME, "bitSize", { bitSize: bits });
1451
- }
1452
- if (bits === Sha512.SIZE_224) {
1453
- this._instance = sha2_js.sha512_224.create();
1454
- }
1455
- else if (bits === Sha512.SIZE_256) {
1456
- this._instance = sha2_js.sha512_256.create();
1457
- }
1458
- else if (bits === Sha512.SIZE_384) {
1459
- this._instance = sha2_js.sha384.create();
1460
- }
1461
- else {
1462
- this._instance = sha2_js.sha512.create();
1463
- }
1464
- }
1465
- /**
1466
- * Perform Sum 512 on the block.
1467
- * @param block The block to operate on.
1468
- * @returns The sum 512 of the block.
1469
- */
1470
- static sum512(block) {
1471
- const b2b = new Sha512(Sha512.SIZE_512);
1472
- b2b.update(block);
1473
- return b2b.digest();
1474
- }
1475
- /**
1476
- * Perform Sum 384 on the block.
1477
- * @param block The block to operate on.
1478
- * @returns The sum 384 of the block.
1479
- */
1480
- static sum384(block) {
1481
- const b2b = new Sha512(Sha512.SIZE_384);
1482
- b2b.update(block);
1483
- return b2b.digest();
1484
- }
1485
- /**
1486
- * Perform Sum 256 on the block.
1487
- * @param block The block to operate on.
1488
- * @returns The sum 256 of the block.
1489
- */
1490
- static sum256(block) {
1491
- const b2b = new Sha512(Sha512.SIZE_256);
1492
- b2b.update(block);
1493
- return b2b.digest();
1494
- }
1495
- /**
1496
- * Perform Sum 224 on the block.
1497
- * @param block The block to operate on.
1498
- * @returns The sum 224 of the block.
1499
- */
1500
- static sum224(block) {
1501
- const b2b = new Sha512(Sha512.SIZE_224);
1502
- b2b.update(block);
1503
- return b2b.digest();
1504
- }
1505
- /**
1506
- * Update the hash with the block.
1507
- * @param block The block to update the hash with.
1508
- * @returns The instance for chaining.
1509
- */
1510
- update(block) {
1511
- core.Guards.uint8Array(Sha512._CLASS_NAME, "block", block);
1512
- this._instance.update(block);
1513
- return this;
1514
- }
1515
- /**
1516
- * Get the digest for the hash.
1517
- * @returns The instance for chaining.
1518
- */
1519
- digest() {
1520
- return this._instance.digest();
1521
- }
1522
- }
1523
-
1524
- // Copyright 2024 IOTA Stiftung.
1525
- // SPDX-License-Identifier: Apache-2.0.
1526
- /**
1527
- * Helper class for working with PEM (Privacy-Enhanced Mail) formatted data.
1528
- */
1529
- class PemHelper {
1530
- /**
1531
- * Runtime name for the class.
1532
- * @internal
1533
- */
1534
- static _CLASS_NAME = "PemHelper";
1535
- /**
1536
- * Strip the PEM content of its headers, footers, and newlines.
1537
- * @param pemContent The PEM content to strip.
1538
- * @returns The stripped PEM content in bas64 format.
1539
- */
1540
- static stripPemMarkers(pemContent) {
1541
- core.Guards.string(PemHelper._CLASS_NAME, "pemContent", pemContent);
1542
- return pemContent
1543
- .replace(/-----BEGIN.*-----/, "")
1544
- .replace(/-----END.*-----/, "")
1545
- .replace(/\n/g, "")
1546
- .trim();
1547
- }
1548
- /**
1549
- * Format the PEM content to have a specific line length.
1550
- * @param marker The marker for the PEM content, e.g. RSA PRIVATE KEY
1551
- * @param base64Content The base64 content to format.
1552
- * @param lineLength The length of each line in the PEM content, default is 64 characters.
1553
- * @returns The formatted PEM content.
1554
- */
1555
- static formatPem(marker, base64Content, lineLength = 64) {
1556
- core.Guards.stringValue(PemHelper._CLASS_NAME, "marker", marker);
1557
- core.Guards.stringBase64(PemHelper._CLASS_NAME, "base64Content", base64Content);
1558
- const lines = [];
1559
- for (let i = 0; i < base64Content.length; i += lineLength) {
1560
- lines.push(base64Content.slice(i, i + lineLength));
1561
- }
1562
- return [`-----BEGIN ${marker}-----`, ...lines, `-----END ${marker}-----`].join("\n");
1563
- }
1564
- }
1565
-
1566
- // Copyright 2024 IOTA Stiftung.
1567
- // SPDX-License-Identifier: Apache-2.0.
1568
- /**
1569
- * Implementation of Bip39 for mnemonic generation.
1570
- */
1571
- class Bip39 {
1572
- /**
1573
- * Runtime name for the class.
1574
- * @internal
1575
- */
1576
- static _CLASS_NAME = "Bip39";
1577
- /**
1578
- * Generate a random mnemonic.
1579
- * @param strength The strength of the mnemonic to generate, defaults to 256.
1580
- * @param words The wordlist to use, defaults to the English wordlist.
1581
- * @returns The random mnemonic.
1582
- * @throws Error if the length is not a multiple of 32.
1583
- */
1584
- static randomMnemonic(strength = 256, words = english_js.wordlist) {
1585
- core.Guards.number(Bip39._CLASS_NAME, "strength", strength);
1586
- core.Guards.arrayValue(Bip39._CLASS_NAME, "words", words);
1587
- if (strength % 32 !== 0) {
1588
- throw new core.GuardError(Bip39._CLASS_NAME, "guard.length32Multiple", "strength", strength);
1589
- }
1590
- return bip39__namespace.generateMnemonic(words, strength);
1591
- }
1592
- /**
1593
- * Generate a mnemonic from the entropy.
1594
- * @param entropy The entropy to generate.
1595
- * @param words The wordlist to use, defaults to the English wordlist.
1596
- * @returns The mnemonic.
1597
- * @throws Error if the length of the entropy is not a multiple of 4, or is less than 16 or greater than 32.
1598
- */
1599
- static entropyToMnemonic(entropy, words = english_js.wordlist) {
1600
- core.Guards.uint8Array(Bip39._CLASS_NAME, "entropy", entropy);
1601
- core.Guards.arrayValue(Bip39._CLASS_NAME, "words", words);
1602
- if (entropy.length % 4 !== 0 || entropy.length < 16 || entropy.length > 32) {
1603
- throw new core.GuardError(Bip39._CLASS_NAME, "guard.lengthEntropy", "entropy", entropy.length);
1604
- }
1605
- return bip39__namespace.entropyToMnemonic(entropy, words);
1606
- }
1607
- /**
1608
- * Convert a mnemonic to a seed.
1609
- * @param mnemonic The mnemonic to convert.
1610
- * @param password The password to apply to the seed generation.
1611
- * @returns The seed.
1612
- */
1613
- static mnemonicToSeed(mnemonic, password) {
1614
- core.Guards.stringValue(Bip39._CLASS_NAME, "mnemonic", mnemonic);
1615
- return bip39__namespace.mnemonicToSeedSync(mnemonic, password);
1616
- }
1617
- /**
1618
- * Convert the mnemonic back to entropy.
1619
- * @param mnemonic The mnemonic to convert.
1620
- * @param words The wordlist to use, defaults to the English wordlist.
1621
- * @returns The entropy.
1622
- * @throws Error if the number of words is not a multiple of 3.
1623
- */
1624
- static mnemonicToEntropy(mnemonic, words = english_js.wordlist) {
1625
- core.Guards.stringValue(Bip39._CLASS_NAME, "mnemonic", mnemonic);
1626
- core.Guards.arrayValue(Bip39._CLASS_NAME, "words", words);
1627
- return bip39__namespace.mnemonicToEntropy(mnemonic, words);
1628
- }
1629
- /**
1630
- * Validate the mnemonic.
1631
- * @param mnemonic The mnemonic to validate.
1632
- * @param wordCount The expected number of words in the mnemonic, defaults to 24.
1633
- * @param words The wordlist to use, defaults to the English wordlist.
1634
- * @returns True if the mnemonic is valid.
1635
- */
1636
- static validateMnemonic(mnemonic, wordCount = 24, words = english_js.wordlist) {
1637
- core.Guards.string(Bip39._CLASS_NAME, "mnemonic", mnemonic);
1638
- core.Guards.integer(Bip39._CLASS_NAME, "wordCount", wordCount);
1639
- const mnemonicSplit = mnemonic.split(/\s+/);
1640
- if (mnemonicSplit.length !== wordCount) {
1641
- return false;
1642
- }
1643
- return bip39__namespace.validateMnemonic(mnemonic, words);
1644
- }
1645
- }
1646
-
1647
- // Copyright 2024 IOTA Stiftung.
1648
- // SPDX-License-Identifier: Apache-2.0.
1649
- /**
1650
- * Perform HOTP.
1651
- * Implementation of https://datatracker.ietf.org/doc/html/rfc4226 .
1652
- */
1653
- class Hotp {
1654
- /**
1655
- * Runtime name for the class.
1656
- * @internal
1657
- */
1658
- static _CLASS_NAME = "Hotp";
1659
- /**
1660
- * Generate a counter based One Time Password.
1661
- * @param key Key for the one time password.
1662
- * @param counter This should be stored by the application,
1663
- * must be user specific, and be incremented for each request.
1664
- * @returns The one time password.
1665
- */
1666
- static generate(key, counter) {
1667
- core.Guards.uint8Array(Hotp._CLASS_NAME, "key", key);
1668
- core.Guards.number(Hotp._CLASS_NAME, "counter", counter);
1669
- return otp__namespace.hotp({ secret: key, digits: 6, algorithm: "sha1", interval: 30 }, counter);
1670
- }
1671
- }
1672
-
1673
- // Copyright 2024 IOTA Stiftung.
1674
- // SPDX-License-Identifier: Apache-2.0.
1675
- /**
1676
- * Perform TOTP.
1677
- * Implementation of https://datatracker.ietf.org/doc/html/rfc4226 .
1678
- */
1679
- class Totp {
1680
- /**
1681
- * Generate a time based One Time Password.
1682
- * @param key Key for the one time password.
1683
- * @param interval The time step of the counter.
1684
- * @param timestamp The timestamp.
1685
- * @returns The one time password.
1686
- */
1687
- static generate(key, interval = 30, timestamp = Date.now()) {
1688
- return otp__namespace.totp({ secret: key, digits: 6, algorithm: "sha1", interval }, timestamp);
1689
- }
1690
- /**
1691
- * Check a One Time Password based on a timer.
1692
- * @param token Passcode to validate.
1693
- * @param key Key for the one time password. This should be unique and secret for
1694
- * every user as it is the seed used to calculate the HMAC.
1695
- * @param window The allowable margin for the counter.
1696
- * @param interval The time step of the counter.
1697
- * @param timestamp The timestamp now.
1698
- * @returns Undefined if failure, delta on success
1699
- */
1700
- static verify(token, key, window = 2, interval = 30, timestamp = Date.now()) {
1701
- for (let i = -window; i < window; i++) {
1702
- const intervalWindow = i * interval * 1000;
1703
- if (timestamp + intervalWindow > 0) {
1704
- const gen = this.generate(key, interval, timestamp + intervalWindow);
1705
- if (gen === token) {
1706
- // We have found a matching code
1707
- return i;
1708
- }
1709
- }
1710
- }
1711
- // If we get to here then no codes have matched, return undefined
1712
- return undefined;
1713
- }
1714
- /**
1715
- * Generate a secret.
1716
- * @param length The length of the secret to generate.
1717
- * @returns The secret encoded as base32.
1718
- */
1719
- static generateSecret(length) {
1720
- const encodedBase32 = core.Base32.encode(core.RandomHelper.generate(length));
1721
- // Strip the trailing = the authenticator apps don't need them
1722
- return encodedBase32.replace(/=/g, "");
1723
- }
1724
- /**
1725
- * Convert the secret back to bytes.
1726
- * @param secretBase32 The secret encoded as base32.
1727
- * @returns The bytes of the secret.
1728
- */
1729
- static secretToBytes(secretBase32) {
1730
- return core.Base32.decode(secretBase32);
1731
- }
1732
- /**
1733
- * Generate a url for use with authenticator apps.
1734
- * See https://github.com/google/google-authenticator/wiki/Key-Uri-Format .
1735
- * @param issuer The issuer of the totp.
1736
- * @param label The label that will show in auth apps.
1737
- * @param secretBase32 The secret as base 32.
1738
- * @returns The url.
1739
- */
1740
- static generateAuthUrl(issuer, label, secretBase32) {
1741
- const encodedIssuer = encodeURIComponent(issuer);
1742
- return `otpauth://totp/${encodedIssuer}%3A${encodeURIComponent(label)}?secret=${secretBase32}&issuer=${encodedIssuer}&digits=6&algorithm=SHA1&interval=30`;
1743
- }
1744
- }
1745
-
1746
- // Copyright 2024 IOTA Stiftung.
1747
- // SPDX-License-Identifier: Apache-2.0.
1748
- /**
1749
- * Generate random passwords.
1750
- */
1751
- class PasswordGenerator {
1752
- /**
1753
- * Generate a password of given length.
1754
- * @param length The length of the password to generate.
1755
- * @returns The random password.
1756
- */
1757
- static generate(length) {
1758
- const alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
1759
- const alphabet2 = `${alphabet}0123456789!#$£%^&*+=@~?}`;
1760
- const chars = [];
1761
- while (chars.length < length) {
1762
- const charSet = chars.length === 0 ? alphabet : alphabet2;
1763
- let b = 0;
1764
- do {
1765
- b = core.RandomHelper.generate(1)[0];
1766
- } while (b >= charSet.length);
1767
- chars.push(charSet[b]);
1768
- }
1769
- return chars.join("");
1770
- }
1771
- }
1772
-
1773
- // Copyright 2024 IOTA Stiftung.
1774
- // SPDX-License-Identifier: Apache-2.0.
1775
- /**
1776
- * Test password strength.
1777
- * Ref https://www.owasp.org/index.php/Authentication_Cheat_Sheet#Implement_Proper_Password_Strength_Controls .
1778
- */
1779
- class PasswordValidator {
1780
- /**
1781
- * Test the strength of the password.
1782
- * @param property The name of the property.
1783
- * @param password The password to test.
1784
- * @param failures The list of failures to add to.
1785
- * @param options Options to configure the testing.
1786
- * @param options.minLength The minimum length of the password, defaults to 8.
1787
- * @param options.maxLength The minimum length of the password, defaults to 128.
1788
- * @param options.minPhraseLength The minimum length of the password for it to be considered a pass phrase.
1789
- */
1790
- static validate(property, password, failures, options) {
1791
- const isString = core.Validation.stringValue(property, password, failures);
1792
- if (isString) {
1793
- const minLength = options?.minLength ?? 8;
1794
- if (password.length < minLength) {
1795
- failures.push({
1796
- property,
1797
- reason: "validation.minLengthRequired",
1798
- properties: {
1799
- minLength
1800
- }
1801
- });
1802
- }
1803
- const maxLength = options?.maxLength ?? 128;
1804
- if (password.length > maxLength) {
1805
- failures.push({
1806
- property,
1807
- reason: "validation.maxLengthRequired",
1808
- properties: {
1809
- maxLength
1810
- }
1811
- });
1812
- }
1813
- if (/(.)\1{2,}/.test(password)) {
1814
- failures.push({
1815
- property,
1816
- reason: "validation.repeatedCharacters"
1817
- });
1818
- }
1819
- // If this looks like a phrase then apply additional rules
1820
- const minPhraseLength = options?.minPhraseLength ?? 20;
1821
- if (password.length < minPhraseLength || !password.includes(" ")) {
1822
- if (!/[a-z]/.test(password)) {
1823
- failures.push({
1824
- property,
1825
- reason: "validation.atLeastOneLowerCase"
1826
- });
1827
- }
1828
- if (!/[A-Z]/.test(password)) {
1829
- failures.push({
1830
- property,
1831
- reason: "validation.atLeastOneUpperCase"
1832
- });
1833
- }
1834
- if (!/\d/.test(password)) {
1835
- failures.push({
1836
- property,
1837
- reason: "validation.atLeastOneNumber"
1838
- });
1839
- }
1840
- if (!/[^\dA-Za-z]/.test(password)) {
1841
- failures.push({
1842
- property,
1843
- reason: "validation.atLeastOneSpecialChar"
1844
- });
1845
- }
1846
- }
1847
- }
1848
- }
1849
- }
1850
-
1851
- exports.Bech32 = Bech32;
1852
- exports.Bip32Path = Bip32Path;
1853
- exports.Bip39 = Bip39;
1854
- exports.Bip44 = Bip44;
1855
- exports.Blake2b = Blake2b;
1856
- exports.Blake3 = Blake3;
1857
- exports.ChaCha20Poly1305 = ChaCha20Poly1305;
1858
- exports.Ed25519 = Ed25519;
1859
- exports.HmacSha1 = HmacSha1;
1860
- exports.HmacSha256 = HmacSha256;
1861
- exports.HmacSha512 = HmacSha512;
1862
- exports.Hotp = Hotp;
1863
- exports.KeyType = KeyType;
1864
- exports.PasswordGenerator = PasswordGenerator;
1865
- exports.PasswordValidator = PasswordValidator;
1866
- exports.Pbkdf2 = Pbkdf2;
1867
- exports.PemHelper = PemHelper;
1868
- exports.Secp256k1 = Secp256k1;
1869
- exports.Sha1 = Sha1;
1870
- exports.Sha256 = Sha256;
1871
- exports.Sha3 = Sha3;
1872
- exports.Sha512 = Sha512;
1873
- exports.Slip0010 = Slip0010;
1874
- exports.Totp = Totp;
1875
- exports.X25519 = X25519;
1876
- exports.Zip215 = Zip215;