@twin.org/crypto 0.0.1-next.8 → 0.0.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 (36) hide show
  1. package/dist/cjs/index.cjs +251 -0
  2. package/dist/esm/index.mjs +251 -2
  3. package/dist/types/address/bip44.d.ts +15 -0
  4. package/dist/types/curves/ed25519.d.ts +12 -0
  5. package/dist/types/hashes/blake3.d.ts +44 -0
  6. package/dist/types/hashes/sha3.d.ts +61 -0
  7. package/dist/types/index.d.ts +2 -0
  8. package/docs/changelog.md +331 -1
  9. package/docs/reference/classes/Bech32.md +15 -7
  10. package/docs/reference/classes/Bip32Path.md +17 -9
  11. package/docs/reference/classes/Bip39.md +28 -12
  12. package/docs/reference/classes/Bip44.md +121 -21
  13. package/docs/reference/classes/Blake2b.md +35 -17
  14. package/docs/reference/classes/Blake3.md +137 -0
  15. package/docs/reference/classes/ChaCha20Poly1305.md +18 -8
  16. package/docs/reference/classes/Ed25519.md +65 -9
  17. package/docs/reference/classes/HmacSha1.md +17 -9
  18. package/docs/reference/classes/HmacSha256.md +26 -12
  19. package/docs/reference/classes/HmacSha512.md +38 -16
  20. package/docs/reference/classes/Hotp.md +9 -5
  21. package/docs/reference/classes/PasswordGenerator.md +6 -4
  22. package/docs/reference/classes/PasswordValidator.md +23 -11
  23. package/docs/reference/classes/Pbkdf2.md +27 -11
  24. package/docs/reference/classes/Secp256k1.md +21 -9
  25. package/docs/reference/classes/Sha1.md +11 -7
  26. package/docs/reference/classes/Sha256.md +17 -9
  27. package/docs/reference/classes/Sha3.md +179 -0
  28. package/docs/reference/classes/Sha512.md +23 -11
  29. package/docs/reference/classes/Slip0010.md +27 -11
  30. package/docs/reference/classes/Totp.md +42 -16
  31. package/docs/reference/classes/X25519.md +9 -5
  32. package/docs/reference/classes/Zip215.md +12 -6
  33. package/docs/reference/index.md +2 -0
  34. package/docs/reference/type-aliases/KeyType.md +1 -1
  35. package/locales/en.json +3 -6
  36. package/package.json +13 -13
@@ -8,11 +8,13 @@ var blake2b = require('@noble/hashes/blake2b');
8
8
  var bip32 = require('@scure/bip32');
9
9
  var slip10_js = require('micro-key-producer/slip10.js');
10
10
  var chacha = require('@noble/ciphers/chacha');
11
+ var blake3 = require('@noble/hashes/blake3');
11
12
  var hmac = require('@noble/hashes/hmac');
12
13
  var sha1 = require('@noble/hashes/sha1');
13
14
  var sha256 = require('@noble/hashes/sha256');
14
15
  var sha512 = require('@noble/hashes/sha512');
15
16
  var pbkdf2 = require('@noble/hashes/pbkdf2');
17
+ var sha3 = require('@noble/hashes/sha3');
16
18
  var bip39 = require('@scure/bip39');
17
19
  var english = require('@scure/bip39/wordlists/english');
18
20
  var otp = require('micro-key-producer/otp.js');
@@ -186,6 +188,39 @@ class Ed25519 {
186
188
  return false;
187
189
  }
188
190
  }
191
+ /**
192
+ * Convert a private key in PKCS8 format.
193
+ * @param privateKey The private key to convert.
194
+ * @returns The private key in PKCS8 format.
195
+ */
196
+ static async privateKeyToPkcs8(privateKey) {
197
+ core.Guards.uint8Array(Ed25519._CLASS_NAME, "privateKey", privateKey);
198
+ if (privateKey.length !== Ed25519.PRIVATE_KEY_SIZE) {
199
+ throw new core.GeneralError(Ed25519._CLASS_NAME, "privateKeyLength", {
200
+ requiredSize: Ed25519.PRIVATE_KEY_SIZE,
201
+ actualSize: privateKey.length
202
+ });
203
+ }
204
+ // crypto.subtle.importKey does not support Ed25519 keys in raw format.
205
+ // We need to convert the key to PKCS8 format before importing.
206
+ // The PKCS8 format is the raw key prefixed with the ASN.1 sequence for an Ed25519 private key.
207
+ // The ASN.1 sequence is 48 46 02 01 00 30 05 06 03 2b 65 70 04 20 04 20 (0x302e020100300506032b657004220420)
208
+ const pkcs8Prefix = new Uint8Array([48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32]);
209
+ const fullKey = core.Uint8ArrayHelper.concat([pkcs8Prefix, privateKey]);
210
+ return crypto.subtle.importKey("pkcs8", fullKey, "Ed25519", true, ["sign"]);
211
+ }
212
+ /**
213
+ * Convert a crypto key to raw private key.
214
+ * @param cryptoKey The crypto key to convert.
215
+ * @returns The raw private key.
216
+ */
217
+ static async pkcs8ToPrivateKey(cryptoKey) {
218
+ core.Guards.defined(Ed25519._CLASS_NAME, "cryptoKey", cryptoKey);
219
+ // crypto.subtle.exportKey does not support Ed25519 keys in raw format.
220
+ // so we export as PKCS8 and remove the ASN.1 sequence prefix.
221
+ const pkcs8Bytes = await crypto.subtle.exportKey("pkcs8", cryptoKey);
222
+ return new Uint8Array(pkcs8Bytes.slice(16));
223
+ }
189
224
  }
190
225
 
191
226
  // Copyright 2024 IOTA Stiftung.
@@ -590,6 +625,24 @@ class Bip44 {
590
625
  static basePath(coinType) {
591
626
  return `m/44'/${coinType}'`;
592
627
  }
628
+ /**
629
+ * Generate an address from the seed and parts.
630
+ * @param seed The account seed.
631
+ * @param keyType The key type.
632
+ * @param coinType The coin type.
633
+ * @param accountIndex The account index.
634
+ * @param isInternal Is this an internal address.
635
+ * @param addressIndex The address index.
636
+ * @returns The generated path and the associated keypair.
637
+ */
638
+ static address(seed, keyType, coinType, accountIndex, isInternal, addressIndex) {
639
+ const keyPair = Bip44.keyPair(seed, keyType, coinType, accountIndex, isInternal, addressIndex);
640
+ const addressData = Blake2b.sum256(keyPair.publicKey);
641
+ return {
642
+ address: core.Converter.bytesToHex(addressData, true),
643
+ ...keyPair
644
+ };
645
+ }
593
646
  /**
594
647
  * Generate a bech32 address from the seed and parts.
595
648
  * @param seed The account seed.
@@ -727,6 +780,81 @@ class Zip215 {
727
780
  }
728
781
  }
729
782
 
783
+ // Copyright 2024 IOTA Stiftung.
784
+ // SPDX-License-Identifier: Apache-2.0.
785
+ /**
786
+ * Class to help with Blake3 Signature scheme.
787
+ */
788
+ class Blake3 {
789
+ /**
790
+ * Blake3 256.
791
+ */
792
+ static SIZE_256 = 32;
793
+ /**
794
+ * Blake3 512.
795
+ */
796
+ static SIZE_512 = 64;
797
+ /**
798
+ * Runtime name for the class.
799
+ * @internal
800
+ */
801
+ static _CLASS_NAME = "Blake3";
802
+ /**
803
+ * The instance of the hash.
804
+ * @internal
805
+ */
806
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
807
+ _instance;
808
+ /**
809
+ * Create a new instance of Blake3.
810
+ * @param outputLength The output length.
811
+ * @param key Optional key for the hash.
812
+ */
813
+ constructor(outputLength, key) {
814
+ this._instance = blake3.blake3.create({
815
+ dkLen: outputLength,
816
+ key
817
+ });
818
+ }
819
+ /**
820
+ * Perform Sum 256 on the block.
821
+ * @param block The block to operate on.
822
+ * @param key Optional key for the hash.
823
+ * @returns The sum 256 of the block.
824
+ */
825
+ static sum256(block, key) {
826
+ core.Guards.uint8Array(Blake3._CLASS_NAME, "block", block);
827
+ return new Blake3(Blake3.SIZE_256, key).update(block).digest();
828
+ }
829
+ /**
830
+ * Perform Sum 512 on the block.
831
+ * @param block The block to operate on.
832
+ * @param key Optional key for the hash.
833
+ * @returns The sum 512 of the block.
834
+ */
835
+ static sum512(block, key) {
836
+ core.Guards.uint8Array(Blake3._CLASS_NAME, "block", block);
837
+ return new Blake3(Blake3.SIZE_512, key).update(block).digest();
838
+ }
839
+ /**
840
+ * Update the hash with the block.
841
+ * @param block The block to update the hash with.
842
+ * @returns The instance for chaining.
843
+ */
844
+ update(block) {
845
+ core.Guards.uint8Array(Blake3._CLASS_NAME, "block", block);
846
+ this._instance.update(block);
847
+ return this;
848
+ }
849
+ /**
850
+ * Get the digest for the hash.
851
+ * @returns The instance for chaining.
852
+ */
853
+ digest() {
854
+ return this._instance.digest();
855
+ }
856
+ }
857
+
730
858
  // Copyright 2024 IOTA Stiftung.
731
859
  // SPDX-License-Identifier: Apache-2.0.
732
860
  /**
@@ -1160,6 +1288,127 @@ class Sha256 {
1160
1288
  }
1161
1289
  }
1162
1290
 
1291
+ // Copyright 2024 IOTA Stiftung.
1292
+ // SPDX-License-Identifier: Apache-2.0.
1293
+ // eslint-disable-next-line camelcase
1294
+ /**
1295
+ * Perform a SHA-3 hash on the block.
1296
+ */
1297
+ class Sha3 {
1298
+ /**
1299
+ * Sha3 224.
1300
+ */
1301
+ static SIZE_224 = 224;
1302
+ /**
1303
+ * Sha3 256.
1304
+ */
1305
+ static SIZE_256 = 256;
1306
+ /**
1307
+ * Sha3 384.
1308
+ */
1309
+ static SIZE_384 = 384;
1310
+ /**
1311
+ * Sha3 512.
1312
+ */
1313
+ static SIZE_512 = 512;
1314
+ /**
1315
+ * Runtime name for the class.
1316
+ * @internal
1317
+ */
1318
+ static _CLASS_NAME = "Sha3";
1319
+ /**
1320
+ * The instance of the hash.
1321
+ * @internal
1322
+ */
1323
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1324
+ _instance;
1325
+ /**
1326
+ * Create a new instance of Sha3.
1327
+ * @param bits The number of bits.
1328
+ */
1329
+ constructor(bits = Sha3.SIZE_256) {
1330
+ if (bits !== Sha3.SIZE_224 &&
1331
+ bits !== Sha3.SIZE_256 &&
1332
+ bits !== Sha3.SIZE_384 &&
1333
+ bits !== Sha3.SIZE_512) {
1334
+ throw new core.GeneralError(Sha3._CLASS_NAME, "bitSize", { bitSize: bits });
1335
+ }
1336
+ if (bits === Sha3.SIZE_224) {
1337
+ // eslint-disable-next-line camelcase
1338
+ this._instance = sha3.sha3_224.create();
1339
+ }
1340
+ else if (bits === Sha3.SIZE_256) {
1341
+ // eslint-disable-next-line camelcase
1342
+ this._instance = sha3.sha3_256.create();
1343
+ }
1344
+ else if (bits === Sha3.SIZE_384) {
1345
+ // eslint-disable-next-line camelcase
1346
+ this._instance = sha3.sha3_384.create();
1347
+ }
1348
+ else {
1349
+ // eslint-disable-next-line camelcase
1350
+ this._instance = sha3.sha3_512.create();
1351
+ }
1352
+ }
1353
+ /**
1354
+ * Perform Sum 256 on the block.
1355
+ * @param block The block to operate on.
1356
+ * @returns The sum 256 of the block.
1357
+ */
1358
+ static sum256(block) {
1359
+ const b2b = new Sha3(Sha3.SIZE_256);
1360
+ b2b.update(block);
1361
+ return b2b.digest();
1362
+ }
1363
+ /**
1364
+ * Perform Sum 224 on the block.
1365
+ * @param block The block to operate on.
1366
+ * @returns The sum 224 of the block.
1367
+ */
1368
+ static sum224(block) {
1369
+ const b2b = new Sha3(Sha3.SIZE_224);
1370
+ b2b.update(block);
1371
+ return b2b.digest();
1372
+ }
1373
+ /**
1374
+ * Perform Sum 384 on the block.
1375
+ * @param block The block to operate on.
1376
+ * @returns The sum 384 of the block.
1377
+ */
1378
+ static sum384(block) {
1379
+ const b2b = new Sha3(Sha3.SIZE_384);
1380
+ b2b.update(block);
1381
+ return b2b.digest();
1382
+ }
1383
+ /**
1384
+ * Perform Sum 512 on the block.
1385
+ * @param block The block to operate on.
1386
+ * @returns The sum 512 of the block.
1387
+ */
1388
+ static sum512(block) {
1389
+ const b2b = new Sha3(Sha3.SIZE_512);
1390
+ b2b.update(block);
1391
+ return b2b.digest();
1392
+ }
1393
+ /**
1394
+ * Update the hash with the block.
1395
+ * @param block The block to update the hash with.
1396
+ * @returns The instance for chaining.
1397
+ */
1398
+ update(block) {
1399
+ core.Guards.uint8Array(Sha3._CLASS_NAME, "block", block);
1400
+ this._instance.update(block);
1401
+ return this;
1402
+ }
1403
+ /**
1404
+ * Get the digest for the hash.
1405
+ * @returns The instance for chaining.
1406
+ */
1407
+ digest() {
1408
+ return this._instance.digest();
1409
+ }
1410
+ }
1411
+
1163
1412
  // Copyright 2024 IOTA Stiftung.
1164
1413
  // SPDX-License-Identifier: Apache-2.0.
1165
1414
  /* eslint-disable camelcase */
@@ -1552,6 +1801,7 @@ exports.Bip32Path = Bip32Path;
1552
1801
  exports.Bip39 = Bip39;
1553
1802
  exports.Bip44 = Bip44;
1554
1803
  exports.Blake2b = Blake2b;
1804
+ exports.Blake3 = Blake3;
1555
1805
  exports.ChaCha20Poly1305 = ChaCha20Poly1305;
1556
1806
  exports.Ed25519 = Ed25519;
1557
1807
  exports.HmacSha1 = HmacSha1;
@@ -1565,6 +1815,7 @@ exports.Pbkdf2 = Pbkdf2;
1565
1815
  exports.Secp256k1 = Secp256k1;
1566
1816
  exports.Sha1 = Sha1;
1567
1817
  exports.Sha256 = Sha256;
1818
+ exports.Sha3 = Sha3;
1568
1819
  exports.Sha512 = Sha512;
1569
1820
  exports.Slip0010 = Slip0010;
1570
1821
  exports.Totp = Totp;
@@ -1,16 +1,18 @@
1
1
  import { bech32 } from '@scure/base';
2
- import { Guards, BaseError, GeneralError, Is, Converter, GuardError, Base32, RandomHelper, Validation } from '@twin.org/core';
2
+ import { Guards, BaseError, GeneralError, Is, Uint8ArrayHelper, Converter, GuardError, Base32, RandomHelper, Validation } from '@twin.org/core';
3
3
  import { ed25519, edwardsToMontgomeryPriv, edwardsToMontgomeryPub } from '@noble/curves/ed25519';
4
4
  import { secp256k1 } from '@noble/curves/secp256k1';
5
5
  import { blake2b } from '@noble/hashes/blake2b';
6
6
  import { HDKey as HDKey$1 } from '@scure/bip32';
7
7
  import { HDKey } from 'micro-key-producer/slip10.js';
8
8
  import { chacha20poly1305 } from '@noble/ciphers/chacha';
9
+ import { blake3 } from '@noble/hashes/blake3';
9
10
  import { hmac } from '@noble/hashes/hmac';
10
11
  import { sha1 } from '@noble/hashes/sha1';
11
12
  import { sha256, sha224 } from '@noble/hashes/sha256';
12
13
  import { sha512_224, sha512_256, sha384, sha512 } from '@noble/hashes/sha512';
13
14
  import { pbkdf2 } from '@noble/hashes/pbkdf2';
15
+ import { sha3_224, sha3_256, sha3_384, sha3_512 } from '@noble/hashes/sha3';
14
16
  import * as bip39 from '@scure/bip39';
15
17
  import { wordlist } from '@scure/bip39/wordlists/english';
16
18
  import * as otp from 'micro-key-producer/otp.js';
@@ -164,6 +166,39 @@ class Ed25519 {
164
166
  return false;
165
167
  }
166
168
  }
169
+ /**
170
+ * Convert a private key in PKCS8 format.
171
+ * @param privateKey The private key to convert.
172
+ * @returns The private key in PKCS8 format.
173
+ */
174
+ static async privateKeyToPkcs8(privateKey) {
175
+ Guards.uint8Array(Ed25519._CLASS_NAME, "privateKey", privateKey);
176
+ if (privateKey.length !== Ed25519.PRIVATE_KEY_SIZE) {
177
+ throw new GeneralError(Ed25519._CLASS_NAME, "privateKeyLength", {
178
+ requiredSize: Ed25519.PRIVATE_KEY_SIZE,
179
+ actualSize: privateKey.length
180
+ });
181
+ }
182
+ // crypto.subtle.importKey does not support Ed25519 keys in raw format.
183
+ // We need to convert the key to PKCS8 format before importing.
184
+ // The PKCS8 format is the raw key prefixed with the ASN.1 sequence for an Ed25519 private key.
185
+ // The ASN.1 sequence is 48 46 02 01 00 30 05 06 03 2b 65 70 04 20 04 20 (0x302e020100300506032b657004220420)
186
+ const pkcs8Prefix = new Uint8Array([48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32]);
187
+ const fullKey = Uint8ArrayHelper.concat([pkcs8Prefix, privateKey]);
188
+ return crypto.subtle.importKey("pkcs8", fullKey, "Ed25519", true, ["sign"]);
189
+ }
190
+ /**
191
+ * Convert a crypto key to raw private key.
192
+ * @param cryptoKey The crypto key to convert.
193
+ * @returns The raw private key.
194
+ */
195
+ static async pkcs8ToPrivateKey(cryptoKey) {
196
+ Guards.defined(Ed25519._CLASS_NAME, "cryptoKey", cryptoKey);
197
+ // crypto.subtle.exportKey does not support Ed25519 keys in raw format.
198
+ // so we export as PKCS8 and remove the ASN.1 sequence prefix.
199
+ const pkcs8Bytes = await crypto.subtle.exportKey("pkcs8", cryptoKey);
200
+ return new Uint8Array(pkcs8Bytes.slice(16));
201
+ }
167
202
  }
168
203
 
169
204
  // Copyright 2024 IOTA Stiftung.
@@ -568,6 +603,24 @@ class Bip44 {
568
603
  static basePath(coinType) {
569
604
  return `m/44'/${coinType}'`;
570
605
  }
606
+ /**
607
+ * Generate an address from the seed and parts.
608
+ * @param seed The account seed.
609
+ * @param keyType The key type.
610
+ * @param coinType The coin type.
611
+ * @param accountIndex The account index.
612
+ * @param isInternal Is this an internal address.
613
+ * @param addressIndex The address index.
614
+ * @returns The generated path and the associated keypair.
615
+ */
616
+ static address(seed, keyType, coinType, accountIndex, isInternal, addressIndex) {
617
+ const keyPair = Bip44.keyPair(seed, keyType, coinType, accountIndex, isInternal, addressIndex);
618
+ const addressData = Blake2b.sum256(keyPair.publicKey);
619
+ return {
620
+ address: Converter.bytesToHex(addressData, true),
621
+ ...keyPair
622
+ };
623
+ }
571
624
  /**
572
625
  * Generate a bech32 address from the seed and parts.
573
626
  * @param seed The account seed.
@@ -705,6 +758,81 @@ class Zip215 {
705
758
  }
706
759
  }
707
760
 
761
+ // Copyright 2024 IOTA Stiftung.
762
+ // SPDX-License-Identifier: Apache-2.0.
763
+ /**
764
+ * Class to help with Blake3 Signature scheme.
765
+ */
766
+ class Blake3 {
767
+ /**
768
+ * Blake3 256.
769
+ */
770
+ static SIZE_256 = 32;
771
+ /**
772
+ * Blake3 512.
773
+ */
774
+ static SIZE_512 = 64;
775
+ /**
776
+ * Runtime name for the class.
777
+ * @internal
778
+ */
779
+ static _CLASS_NAME = "Blake3";
780
+ /**
781
+ * The instance of the hash.
782
+ * @internal
783
+ */
784
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
785
+ _instance;
786
+ /**
787
+ * Create a new instance of Blake3.
788
+ * @param outputLength The output length.
789
+ * @param key Optional key for the hash.
790
+ */
791
+ constructor(outputLength, key) {
792
+ this._instance = blake3.create({
793
+ dkLen: outputLength,
794
+ key
795
+ });
796
+ }
797
+ /**
798
+ * Perform Sum 256 on the block.
799
+ * @param block The block to operate on.
800
+ * @param key Optional key for the hash.
801
+ * @returns The sum 256 of the block.
802
+ */
803
+ static sum256(block, key) {
804
+ Guards.uint8Array(Blake3._CLASS_NAME, "block", block);
805
+ return new Blake3(Blake3.SIZE_256, key).update(block).digest();
806
+ }
807
+ /**
808
+ * Perform Sum 512 on the block.
809
+ * @param block The block to operate on.
810
+ * @param key Optional key for the hash.
811
+ * @returns The sum 512 of the block.
812
+ */
813
+ static sum512(block, key) {
814
+ Guards.uint8Array(Blake3._CLASS_NAME, "block", block);
815
+ return new Blake3(Blake3.SIZE_512, key).update(block).digest();
816
+ }
817
+ /**
818
+ * Update the hash with the block.
819
+ * @param block The block to update the hash with.
820
+ * @returns The instance for chaining.
821
+ */
822
+ update(block) {
823
+ Guards.uint8Array(Blake3._CLASS_NAME, "block", block);
824
+ this._instance.update(block);
825
+ return this;
826
+ }
827
+ /**
828
+ * Get the digest for the hash.
829
+ * @returns The instance for chaining.
830
+ */
831
+ digest() {
832
+ return this._instance.digest();
833
+ }
834
+ }
835
+
708
836
  // Copyright 2024 IOTA Stiftung.
709
837
  // SPDX-License-Identifier: Apache-2.0.
710
838
  /**
@@ -1138,6 +1266,127 @@ class Sha256 {
1138
1266
  }
1139
1267
  }
1140
1268
 
1269
+ // Copyright 2024 IOTA Stiftung.
1270
+ // SPDX-License-Identifier: Apache-2.0.
1271
+ // eslint-disable-next-line camelcase
1272
+ /**
1273
+ * Perform a SHA-3 hash on the block.
1274
+ */
1275
+ class Sha3 {
1276
+ /**
1277
+ * Sha3 224.
1278
+ */
1279
+ static SIZE_224 = 224;
1280
+ /**
1281
+ * Sha3 256.
1282
+ */
1283
+ static SIZE_256 = 256;
1284
+ /**
1285
+ * Sha3 384.
1286
+ */
1287
+ static SIZE_384 = 384;
1288
+ /**
1289
+ * Sha3 512.
1290
+ */
1291
+ static SIZE_512 = 512;
1292
+ /**
1293
+ * Runtime name for the class.
1294
+ * @internal
1295
+ */
1296
+ static _CLASS_NAME = "Sha3";
1297
+ /**
1298
+ * The instance of the hash.
1299
+ * @internal
1300
+ */
1301
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1302
+ _instance;
1303
+ /**
1304
+ * Create a new instance of Sha3.
1305
+ * @param bits The number of bits.
1306
+ */
1307
+ constructor(bits = Sha3.SIZE_256) {
1308
+ if (bits !== Sha3.SIZE_224 &&
1309
+ bits !== Sha3.SIZE_256 &&
1310
+ bits !== Sha3.SIZE_384 &&
1311
+ bits !== Sha3.SIZE_512) {
1312
+ throw new GeneralError(Sha3._CLASS_NAME, "bitSize", { bitSize: bits });
1313
+ }
1314
+ if (bits === Sha3.SIZE_224) {
1315
+ // eslint-disable-next-line camelcase
1316
+ this._instance = sha3_224.create();
1317
+ }
1318
+ else if (bits === Sha3.SIZE_256) {
1319
+ // eslint-disable-next-line camelcase
1320
+ this._instance = sha3_256.create();
1321
+ }
1322
+ else if (bits === Sha3.SIZE_384) {
1323
+ // eslint-disable-next-line camelcase
1324
+ this._instance = sha3_384.create();
1325
+ }
1326
+ else {
1327
+ // eslint-disable-next-line camelcase
1328
+ this._instance = sha3_512.create();
1329
+ }
1330
+ }
1331
+ /**
1332
+ * Perform Sum 256 on the block.
1333
+ * @param block The block to operate on.
1334
+ * @returns The sum 256 of the block.
1335
+ */
1336
+ static sum256(block) {
1337
+ const b2b = new Sha3(Sha3.SIZE_256);
1338
+ b2b.update(block);
1339
+ return b2b.digest();
1340
+ }
1341
+ /**
1342
+ * Perform Sum 224 on the block.
1343
+ * @param block The block to operate on.
1344
+ * @returns The sum 224 of the block.
1345
+ */
1346
+ static sum224(block) {
1347
+ const b2b = new Sha3(Sha3.SIZE_224);
1348
+ b2b.update(block);
1349
+ return b2b.digest();
1350
+ }
1351
+ /**
1352
+ * Perform Sum 384 on the block.
1353
+ * @param block The block to operate on.
1354
+ * @returns The sum 384 of the block.
1355
+ */
1356
+ static sum384(block) {
1357
+ const b2b = new Sha3(Sha3.SIZE_384);
1358
+ b2b.update(block);
1359
+ return b2b.digest();
1360
+ }
1361
+ /**
1362
+ * Perform Sum 512 on the block.
1363
+ * @param block The block to operate on.
1364
+ * @returns The sum 512 of the block.
1365
+ */
1366
+ static sum512(block) {
1367
+ const b2b = new Sha3(Sha3.SIZE_512);
1368
+ b2b.update(block);
1369
+ return b2b.digest();
1370
+ }
1371
+ /**
1372
+ * Update the hash with the block.
1373
+ * @param block The block to update the hash with.
1374
+ * @returns The instance for chaining.
1375
+ */
1376
+ update(block) {
1377
+ Guards.uint8Array(Sha3._CLASS_NAME, "block", block);
1378
+ this._instance.update(block);
1379
+ return this;
1380
+ }
1381
+ /**
1382
+ * Get the digest for the hash.
1383
+ * @returns The instance for chaining.
1384
+ */
1385
+ digest() {
1386
+ return this._instance.digest();
1387
+ }
1388
+ }
1389
+
1141
1390
  // Copyright 2024 IOTA Stiftung.
1142
1391
  // SPDX-License-Identifier: Apache-2.0.
1143
1392
  /* eslint-disable camelcase */
@@ -1525,4 +1774,4 @@ class PasswordValidator {
1525
1774
  }
1526
1775
  }
1527
1776
 
1528
- export { Bech32, Bip32Path, Bip39, Bip44, Blake2b, ChaCha20Poly1305, Ed25519, HmacSha1, HmacSha256, HmacSha512, Hotp, KeyType, PasswordGenerator, PasswordValidator, Pbkdf2, Secp256k1, Sha1, Sha256, Sha512, Slip0010, Totp, X25519, Zip215 };
1777
+ export { Bech32, Bip32Path, Bip39, Bip44, Blake2b, Blake3, ChaCha20Poly1305, Ed25519, HmacSha1, HmacSha256, HmacSha512, Hotp, KeyType, PasswordGenerator, PasswordValidator, Pbkdf2, Secp256k1, Sha1, Sha256, Sha3, Sha512, Slip0010, Totp, X25519, Zip215 };
@@ -34,6 +34,21 @@ export declare class Bip44 {
34
34
  * @returns The bip44 address base path.
35
35
  */
36
36
  static basePath(coinType: number): string;
37
+ /**
38
+ * Generate an address from the seed and parts.
39
+ * @param seed The account seed.
40
+ * @param keyType The key type.
41
+ * @param coinType The coin type.
42
+ * @param accountIndex The account index.
43
+ * @param isInternal Is this an internal address.
44
+ * @param addressIndex The address index.
45
+ * @returns The generated path and the associated keypair.
46
+ */
47
+ static address(seed: Uint8Array, keyType: KeyType, coinType: number, accountIndex: number, isInternal: boolean, addressIndex: number): {
48
+ address: string;
49
+ privateKey: Uint8Array;
50
+ publicKey: Uint8Array;
51
+ };
37
52
  /**
38
53
  * Generate a bech32 address from the seed and parts.
39
54
  * @param seed The account seed.
@@ -34,4 +34,16 @@ export declare class Ed25519 {
34
34
  * @throws Error if the public key is not the correct length.
35
35
  */
36
36
  static verify(publicKey: Uint8Array, block: Uint8Array, signature: Uint8Array): boolean;
37
+ /**
38
+ * Convert a private key in PKCS8 format.
39
+ * @param privateKey The private key to convert.
40
+ * @returns The private key in PKCS8 format.
41
+ */
42
+ static privateKeyToPkcs8(privateKey: Uint8Array): Promise<CryptoKey>;
43
+ /**
44
+ * Convert a crypto key to raw private key.
45
+ * @param cryptoKey The crypto key to convert.
46
+ * @returns The raw private key.
47
+ */
48
+ static pkcs8ToPrivateKey(cryptoKey: CryptoKey): Promise<Uint8Array>;
37
49
  }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Class to help with Blake3 Signature scheme.
3
+ */
4
+ export declare class Blake3 {
5
+ /**
6
+ * Blake3 256.
7
+ */
8
+ static SIZE_256: number;
9
+ /**
10
+ * Blake3 512.
11
+ */
12
+ static SIZE_512: number;
13
+ /**
14
+ * Create a new instance of Blake3.
15
+ * @param outputLength The output length.
16
+ * @param key Optional key for the hash.
17
+ */
18
+ constructor(outputLength: number, key?: Uint8Array);
19
+ /**
20
+ * Perform Sum 256 on the block.
21
+ * @param block The block to operate on.
22
+ * @param key Optional key for the hash.
23
+ * @returns The sum 256 of the block.
24
+ */
25
+ static sum256(block: Uint8Array, key?: Uint8Array): Uint8Array;
26
+ /**
27
+ * Perform Sum 512 on the block.
28
+ * @param block The block to operate on.
29
+ * @param key Optional key for the hash.
30
+ * @returns The sum 512 of the block.
31
+ */
32
+ static sum512(block: Uint8Array, key?: Uint8Array): Uint8Array;
33
+ /**
34
+ * Update the hash with the block.
35
+ * @param block The block to update the hash with.
36
+ * @returns The instance for chaining.
37
+ */
38
+ update(block: Uint8Array): Blake3;
39
+ /**
40
+ * Get the digest for the hash.
41
+ * @returns The instance for chaining.
42
+ */
43
+ digest(): Uint8Array;
44
+ }