@twin.org/crypto 0.0.1-next.4 → 0.0.1-next.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +239 -0
- package/dist/esm/index.mjs +239 -2
- package/dist/types/address/bip44.d.ts +15 -0
- package/dist/types/curves/ed25519.d.ts +6 -0
- package/dist/types/hashes/blake3.d.ts +44 -0
- package/dist/types/hashes/sha3.d.ts +61 -0
- package/dist/types/index.d.ts +2 -0
- package/docs/changelog.md +1 -1
- package/docs/reference/classes/Bech32.md +12 -4
- package/docs/reference/classes/Bip32Path.md +12 -4
- package/docs/reference/classes/Bip39.md +24 -8
- package/docs/reference/classes/Bip44.md +118 -18
- package/docs/reference/classes/Blake2b.md +27 -9
- package/docs/reference/classes/Blake3.md +137 -0
- package/docs/reference/classes/ChaCha20Poly1305.md +15 -5
- package/docs/reference/classes/Ed25519.md +40 -6
- package/docs/reference/classes/HmacSha1.md +12 -4
- package/docs/reference/classes/HmacSha256.md +21 -7
- package/docs/reference/classes/HmacSha512.md +33 -11
- package/docs/reference/classes/Hotp.md +6 -2
- package/docs/reference/classes/PasswordGenerator.md +3 -1
- package/docs/reference/classes/PasswordValidator.md +19 -7
- package/docs/reference/classes/Pbkdf2.md +24 -8
- package/docs/reference/classes/Secp256k1.md +18 -6
- package/docs/reference/classes/Sha1.md +6 -2
- package/docs/reference/classes/Sha256.md +12 -4
- package/docs/reference/classes/Sha3.md +179 -0
- package/docs/reference/classes/Sha512.md +18 -6
- package/docs/reference/classes/Slip0010.md +24 -8
- package/docs/reference/classes/Totp.md +39 -13
- package/docs/reference/classes/X25519.md +6 -2
- package/docs/reference/classes/Zip215.md +9 -3
- package/docs/reference/index.md +2 -0
- package/locales/en.json +3 -6
- package/package.json +12 -12
package/dist/cjs/index.cjs
CHANGED
|
@@ -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,27 @@ 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
|
|
208
|
+
const pkcs8Prefix = new Uint8Array([48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32]); // 0x302e020100300506032b657004220420
|
|
209
|
+
const fullKey = core.Uint8ArrayHelper.concat([pkcs8Prefix, privateKey]);
|
|
210
|
+
return crypto.subtle.importKey("pkcs8", fullKey, "Ed25519", false, ["sign"]);
|
|
211
|
+
}
|
|
189
212
|
}
|
|
190
213
|
|
|
191
214
|
// Copyright 2024 IOTA Stiftung.
|
|
@@ -590,6 +613,24 @@ class Bip44 {
|
|
|
590
613
|
static basePath(coinType) {
|
|
591
614
|
return `m/44'/${coinType}'`;
|
|
592
615
|
}
|
|
616
|
+
/**
|
|
617
|
+
* Generate an address from the seed and parts.
|
|
618
|
+
* @param seed The account seed.
|
|
619
|
+
* @param keyType The key type.
|
|
620
|
+
* @param coinType The coin type.
|
|
621
|
+
* @param accountIndex The account index.
|
|
622
|
+
* @param isInternal Is this an internal address.
|
|
623
|
+
* @param addressIndex The address index.
|
|
624
|
+
* @returns The generated path and the associated keypair.
|
|
625
|
+
*/
|
|
626
|
+
static address(seed, keyType, coinType, accountIndex, isInternal, addressIndex) {
|
|
627
|
+
const keyPair = Bip44.keyPair(seed, keyType, coinType, accountIndex, isInternal, addressIndex);
|
|
628
|
+
const addressData = Blake2b.sum256(keyPair.publicKey);
|
|
629
|
+
return {
|
|
630
|
+
address: core.Converter.bytesToHex(addressData, true),
|
|
631
|
+
...keyPair
|
|
632
|
+
};
|
|
633
|
+
}
|
|
593
634
|
/**
|
|
594
635
|
* Generate a bech32 address from the seed and parts.
|
|
595
636
|
* @param seed The account seed.
|
|
@@ -727,6 +768,81 @@ class Zip215 {
|
|
|
727
768
|
}
|
|
728
769
|
}
|
|
729
770
|
|
|
771
|
+
// Copyright 2024 IOTA Stiftung.
|
|
772
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
773
|
+
/**
|
|
774
|
+
* Class to help with Blake3 Signature scheme.
|
|
775
|
+
*/
|
|
776
|
+
class Blake3 {
|
|
777
|
+
/**
|
|
778
|
+
* Blake3 256.
|
|
779
|
+
*/
|
|
780
|
+
static SIZE_256 = 32;
|
|
781
|
+
/**
|
|
782
|
+
* Blake3 512.
|
|
783
|
+
*/
|
|
784
|
+
static SIZE_512 = 64;
|
|
785
|
+
/**
|
|
786
|
+
* Runtime name for the class.
|
|
787
|
+
* @internal
|
|
788
|
+
*/
|
|
789
|
+
static _CLASS_NAME = "Blake3";
|
|
790
|
+
/**
|
|
791
|
+
* The instance of the hash.
|
|
792
|
+
* @internal
|
|
793
|
+
*/
|
|
794
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
795
|
+
_instance;
|
|
796
|
+
/**
|
|
797
|
+
* Create a new instance of Blake3.
|
|
798
|
+
* @param outputLength The output length.
|
|
799
|
+
* @param key Optional key for the hash.
|
|
800
|
+
*/
|
|
801
|
+
constructor(outputLength, key) {
|
|
802
|
+
this._instance = blake3.blake3.create({
|
|
803
|
+
dkLen: outputLength,
|
|
804
|
+
key
|
|
805
|
+
});
|
|
806
|
+
}
|
|
807
|
+
/**
|
|
808
|
+
* Perform Sum 256 on the block.
|
|
809
|
+
* @param block The block to operate on.
|
|
810
|
+
* @param key Optional key for the hash.
|
|
811
|
+
* @returns The sum 256 of the block.
|
|
812
|
+
*/
|
|
813
|
+
static sum256(block, key) {
|
|
814
|
+
core.Guards.uint8Array(Blake3._CLASS_NAME, "block", block);
|
|
815
|
+
return new Blake3(Blake3.SIZE_256, key).update(block).digest();
|
|
816
|
+
}
|
|
817
|
+
/**
|
|
818
|
+
* Perform Sum 512 on the block.
|
|
819
|
+
* @param block The block to operate on.
|
|
820
|
+
* @param key Optional key for the hash.
|
|
821
|
+
* @returns The sum 512 of the block.
|
|
822
|
+
*/
|
|
823
|
+
static sum512(block, key) {
|
|
824
|
+
core.Guards.uint8Array(Blake3._CLASS_NAME, "block", block);
|
|
825
|
+
return new Blake3(Blake3.SIZE_512, key).update(block).digest();
|
|
826
|
+
}
|
|
827
|
+
/**
|
|
828
|
+
* Update the hash with the block.
|
|
829
|
+
* @param block The block to update the hash with.
|
|
830
|
+
* @returns The instance for chaining.
|
|
831
|
+
*/
|
|
832
|
+
update(block) {
|
|
833
|
+
core.Guards.uint8Array(Blake3._CLASS_NAME, "block", block);
|
|
834
|
+
this._instance.update(block);
|
|
835
|
+
return this;
|
|
836
|
+
}
|
|
837
|
+
/**
|
|
838
|
+
* Get the digest for the hash.
|
|
839
|
+
* @returns The instance for chaining.
|
|
840
|
+
*/
|
|
841
|
+
digest() {
|
|
842
|
+
return this._instance.digest();
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
|
|
730
846
|
// Copyright 2024 IOTA Stiftung.
|
|
731
847
|
// SPDX-License-Identifier: Apache-2.0.
|
|
732
848
|
/**
|
|
@@ -1160,6 +1276,127 @@ class Sha256 {
|
|
|
1160
1276
|
}
|
|
1161
1277
|
}
|
|
1162
1278
|
|
|
1279
|
+
// Copyright 2024 IOTA Stiftung.
|
|
1280
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
1281
|
+
// eslint-disable-next-line camelcase
|
|
1282
|
+
/**
|
|
1283
|
+
* Perform a SHA-3 hash on the block.
|
|
1284
|
+
*/
|
|
1285
|
+
class Sha3 {
|
|
1286
|
+
/**
|
|
1287
|
+
* Sha3 224.
|
|
1288
|
+
*/
|
|
1289
|
+
static SIZE_224 = 224;
|
|
1290
|
+
/**
|
|
1291
|
+
* Sha3 256.
|
|
1292
|
+
*/
|
|
1293
|
+
static SIZE_256 = 256;
|
|
1294
|
+
/**
|
|
1295
|
+
* Sha3 384.
|
|
1296
|
+
*/
|
|
1297
|
+
static SIZE_384 = 384;
|
|
1298
|
+
/**
|
|
1299
|
+
* Sha3 512.
|
|
1300
|
+
*/
|
|
1301
|
+
static SIZE_512 = 512;
|
|
1302
|
+
/**
|
|
1303
|
+
* Runtime name for the class.
|
|
1304
|
+
* @internal
|
|
1305
|
+
*/
|
|
1306
|
+
static _CLASS_NAME = "Sha3";
|
|
1307
|
+
/**
|
|
1308
|
+
* The instance of the hash.
|
|
1309
|
+
* @internal
|
|
1310
|
+
*/
|
|
1311
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1312
|
+
_instance;
|
|
1313
|
+
/**
|
|
1314
|
+
* Create a new instance of Sha3.
|
|
1315
|
+
* @param bits The number of bits.
|
|
1316
|
+
*/
|
|
1317
|
+
constructor(bits = Sha3.SIZE_256) {
|
|
1318
|
+
if (bits !== Sha3.SIZE_224 &&
|
|
1319
|
+
bits !== Sha3.SIZE_256 &&
|
|
1320
|
+
bits !== Sha3.SIZE_384 &&
|
|
1321
|
+
bits !== Sha3.SIZE_512) {
|
|
1322
|
+
throw new core.GeneralError(Sha3._CLASS_NAME, "bitSize", { bitSize: bits });
|
|
1323
|
+
}
|
|
1324
|
+
if (bits === Sha3.SIZE_224) {
|
|
1325
|
+
// eslint-disable-next-line camelcase
|
|
1326
|
+
this._instance = sha3.sha3_224.create();
|
|
1327
|
+
}
|
|
1328
|
+
else if (bits === Sha3.SIZE_256) {
|
|
1329
|
+
// eslint-disable-next-line camelcase
|
|
1330
|
+
this._instance = sha3.sha3_256.create();
|
|
1331
|
+
}
|
|
1332
|
+
else if (bits === Sha3.SIZE_384) {
|
|
1333
|
+
// eslint-disable-next-line camelcase
|
|
1334
|
+
this._instance = sha3.sha3_384.create();
|
|
1335
|
+
}
|
|
1336
|
+
else {
|
|
1337
|
+
// eslint-disable-next-line camelcase
|
|
1338
|
+
this._instance = sha3.sha3_512.create();
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
/**
|
|
1342
|
+
* Perform Sum 256 on the block.
|
|
1343
|
+
* @param block The block to operate on.
|
|
1344
|
+
* @returns The sum 256 of the block.
|
|
1345
|
+
*/
|
|
1346
|
+
static sum256(block) {
|
|
1347
|
+
const b2b = new Sha3(Sha3.SIZE_256);
|
|
1348
|
+
b2b.update(block);
|
|
1349
|
+
return b2b.digest();
|
|
1350
|
+
}
|
|
1351
|
+
/**
|
|
1352
|
+
* Perform Sum 224 on the block.
|
|
1353
|
+
* @param block The block to operate on.
|
|
1354
|
+
* @returns The sum 224 of the block.
|
|
1355
|
+
*/
|
|
1356
|
+
static sum224(block) {
|
|
1357
|
+
const b2b = new Sha3(Sha3.SIZE_224);
|
|
1358
|
+
b2b.update(block);
|
|
1359
|
+
return b2b.digest();
|
|
1360
|
+
}
|
|
1361
|
+
/**
|
|
1362
|
+
* Perform Sum 384 on the block.
|
|
1363
|
+
* @param block The block to operate on.
|
|
1364
|
+
* @returns The sum 384 of the block.
|
|
1365
|
+
*/
|
|
1366
|
+
static sum384(block) {
|
|
1367
|
+
const b2b = new Sha3(Sha3.SIZE_384);
|
|
1368
|
+
b2b.update(block);
|
|
1369
|
+
return b2b.digest();
|
|
1370
|
+
}
|
|
1371
|
+
/**
|
|
1372
|
+
* Perform Sum 512 on the block.
|
|
1373
|
+
* @param block The block to operate on.
|
|
1374
|
+
* @returns The sum 512 of the block.
|
|
1375
|
+
*/
|
|
1376
|
+
static sum512(block) {
|
|
1377
|
+
const b2b = new Sha3(Sha3.SIZE_512);
|
|
1378
|
+
b2b.update(block);
|
|
1379
|
+
return b2b.digest();
|
|
1380
|
+
}
|
|
1381
|
+
/**
|
|
1382
|
+
* Update the hash with the block.
|
|
1383
|
+
* @param block The block to update the hash with.
|
|
1384
|
+
* @returns The instance for chaining.
|
|
1385
|
+
*/
|
|
1386
|
+
update(block) {
|
|
1387
|
+
core.Guards.uint8Array(Sha3._CLASS_NAME, "block", block);
|
|
1388
|
+
this._instance.update(block);
|
|
1389
|
+
return this;
|
|
1390
|
+
}
|
|
1391
|
+
/**
|
|
1392
|
+
* Get the digest for the hash.
|
|
1393
|
+
* @returns The instance for chaining.
|
|
1394
|
+
*/
|
|
1395
|
+
digest() {
|
|
1396
|
+
return this._instance.digest();
|
|
1397
|
+
}
|
|
1398
|
+
}
|
|
1399
|
+
|
|
1163
1400
|
// Copyright 2024 IOTA Stiftung.
|
|
1164
1401
|
// SPDX-License-Identifier: Apache-2.0.
|
|
1165
1402
|
/* eslint-disable camelcase */
|
|
@@ -1552,6 +1789,7 @@ exports.Bip32Path = Bip32Path;
|
|
|
1552
1789
|
exports.Bip39 = Bip39;
|
|
1553
1790
|
exports.Bip44 = Bip44;
|
|
1554
1791
|
exports.Blake2b = Blake2b;
|
|
1792
|
+
exports.Blake3 = Blake3;
|
|
1555
1793
|
exports.ChaCha20Poly1305 = ChaCha20Poly1305;
|
|
1556
1794
|
exports.Ed25519 = Ed25519;
|
|
1557
1795
|
exports.HmacSha1 = HmacSha1;
|
|
@@ -1565,6 +1803,7 @@ exports.Pbkdf2 = Pbkdf2;
|
|
|
1565
1803
|
exports.Secp256k1 = Secp256k1;
|
|
1566
1804
|
exports.Sha1 = Sha1;
|
|
1567
1805
|
exports.Sha256 = Sha256;
|
|
1806
|
+
exports.Sha3 = Sha3;
|
|
1568
1807
|
exports.Sha512 = Sha512;
|
|
1569
1808
|
exports.Slip0010 = Slip0010;
|
|
1570
1809
|
exports.Totp = Totp;
|
package/dist/esm/index.mjs
CHANGED
|
@@ -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,27 @@ 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
|
|
186
|
+
const pkcs8Prefix = new Uint8Array([48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32]); // 0x302e020100300506032b657004220420
|
|
187
|
+
const fullKey = Uint8ArrayHelper.concat([pkcs8Prefix, privateKey]);
|
|
188
|
+
return crypto.subtle.importKey("pkcs8", fullKey, "Ed25519", false, ["sign"]);
|
|
189
|
+
}
|
|
167
190
|
}
|
|
168
191
|
|
|
169
192
|
// Copyright 2024 IOTA Stiftung.
|
|
@@ -568,6 +591,24 @@ class Bip44 {
|
|
|
568
591
|
static basePath(coinType) {
|
|
569
592
|
return `m/44'/${coinType}'`;
|
|
570
593
|
}
|
|
594
|
+
/**
|
|
595
|
+
* Generate an address from the seed and parts.
|
|
596
|
+
* @param seed The account seed.
|
|
597
|
+
* @param keyType The key type.
|
|
598
|
+
* @param coinType The coin type.
|
|
599
|
+
* @param accountIndex The account index.
|
|
600
|
+
* @param isInternal Is this an internal address.
|
|
601
|
+
* @param addressIndex The address index.
|
|
602
|
+
* @returns The generated path and the associated keypair.
|
|
603
|
+
*/
|
|
604
|
+
static address(seed, keyType, coinType, accountIndex, isInternal, addressIndex) {
|
|
605
|
+
const keyPair = Bip44.keyPair(seed, keyType, coinType, accountIndex, isInternal, addressIndex);
|
|
606
|
+
const addressData = Blake2b.sum256(keyPair.publicKey);
|
|
607
|
+
return {
|
|
608
|
+
address: Converter.bytesToHex(addressData, true),
|
|
609
|
+
...keyPair
|
|
610
|
+
};
|
|
611
|
+
}
|
|
571
612
|
/**
|
|
572
613
|
* Generate a bech32 address from the seed and parts.
|
|
573
614
|
* @param seed The account seed.
|
|
@@ -705,6 +746,81 @@ class Zip215 {
|
|
|
705
746
|
}
|
|
706
747
|
}
|
|
707
748
|
|
|
749
|
+
// Copyright 2024 IOTA Stiftung.
|
|
750
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
751
|
+
/**
|
|
752
|
+
* Class to help with Blake3 Signature scheme.
|
|
753
|
+
*/
|
|
754
|
+
class Blake3 {
|
|
755
|
+
/**
|
|
756
|
+
* Blake3 256.
|
|
757
|
+
*/
|
|
758
|
+
static SIZE_256 = 32;
|
|
759
|
+
/**
|
|
760
|
+
* Blake3 512.
|
|
761
|
+
*/
|
|
762
|
+
static SIZE_512 = 64;
|
|
763
|
+
/**
|
|
764
|
+
* Runtime name for the class.
|
|
765
|
+
* @internal
|
|
766
|
+
*/
|
|
767
|
+
static _CLASS_NAME = "Blake3";
|
|
768
|
+
/**
|
|
769
|
+
* The instance of the hash.
|
|
770
|
+
* @internal
|
|
771
|
+
*/
|
|
772
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
773
|
+
_instance;
|
|
774
|
+
/**
|
|
775
|
+
* Create a new instance of Blake3.
|
|
776
|
+
* @param outputLength The output length.
|
|
777
|
+
* @param key Optional key for the hash.
|
|
778
|
+
*/
|
|
779
|
+
constructor(outputLength, key) {
|
|
780
|
+
this._instance = blake3.create({
|
|
781
|
+
dkLen: outputLength,
|
|
782
|
+
key
|
|
783
|
+
});
|
|
784
|
+
}
|
|
785
|
+
/**
|
|
786
|
+
* Perform Sum 256 on the block.
|
|
787
|
+
* @param block The block to operate on.
|
|
788
|
+
* @param key Optional key for the hash.
|
|
789
|
+
* @returns The sum 256 of the block.
|
|
790
|
+
*/
|
|
791
|
+
static sum256(block, key) {
|
|
792
|
+
Guards.uint8Array(Blake3._CLASS_NAME, "block", block);
|
|
793
|
+
return new Blake3(Blake3.SIZE_256, key).update(block).digest();
|
|
794
|
+
}
|
|
795
|
+
/**
|
|
796
|
+
* Perform Sum 512 on the block.
|
|
797
|
+
* @param block The block to operate on.
|
|
798
|
+
* @param key Optional key for the hash.
|
|
799
|
+
* @returns The sum 512 of the block.
|
|
800
|
+
*/
|
|
801
|
+
static sum512(block, key) {
|
|
802
|
+
Guards.uint8Array(Blake3._CLASS_NAME, "block", block);
|
|
803
|
+
return new Blake3(Blake3.SIZE_512, key).update(block).digest();
|
|
804
|
+
}
|
|
805
|
+
/**
|
|
806
|
+
* Update the hash with the block.
|
|
807
|
+
* @param block The block to update the hash with.
|
|
808
|
+
* @returns The instance for chaining.
|
|
809
|
+
*/
|
|
810
|
+
update(block) {
|
|
811
|
+
Guards.uint8Array(Blake3._CLASS_NAME, "block", block);
|
|
812
|
+
this._instance.update(block);
|
|
813
|
+
return this;
|
|
814
|
+
}
|
|
815
|
+
/**
|
|
816
|
+
* Get the digest for the hash.
|
|
817
|
+
* @returns The instance for chaining.
|
|
818
|
+
*/
|
|
819
|
+
digest() {
|
|
820
|
+
return this._instance.digest();
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
|
|
708
824
|
// Copyright 2024 IOTA Stiftung.
|
|
709
825
|
// SPDX-License-Identifier: Apache-2.0.
|
|
710
826
|
/**
|
|
@@ -1138,6 +1254,127 @@ class Sha256 {
|
|
|
1138
1254
|
}
|
|
1139
1255
|
}
|
|
1140
1256
|
|
|
1257
|
+
// Copyright 2024 IOTA Stiftung.
|
|
1258
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
1259
|
+
// eslint-disable-next-line camelcase
|
|
1260
|
+
/**
|
|
1261
|
+
* Perform a SHA-3 hash on the block.
|
|
1262
|
+
*/
|
|
1263
|
+
class Sha3 {
|
|
1264
|
+
/**
|
|
1265
|
+
* Sha3 224.
|
|
1266
|
+
*/
|
|
1267
|
+
static SIZE_224 = 224;
|
|
1268
|
+
/**
|
|
1269
|
+
* Sha3 256.
|
|
1270
|
+
*/
|
|
1271
|
+
static SIZE_256 = 256;
|
|
1272
|
+
/**
|
|
1273
|
+
* Sha3 384.
|
|
1274
|
+
*/
|
|
1275
|
+
static SIZE_384 = 384;
|
|
1276
|
+
/**
|
|
1277
|
+
* Sha3 512.
|
|
1278
|
+
*/
|
|
1279
|
+
static SIZE_512 = 512;
|
|
1280
|
+
/**
|
|
1281
|
+
* Runtime name for the class.
|
|
1282
|
+
* @internal
|
|
1283
|
+
*/
|
|
1284
|
+
static _CLASS_NAME = "Sha3";
|
|
1285
|
+
/**
|
|
1286
|
+
* The instance of the hash.
|
|
1287
|
+
* @internal
|
|
1288
|
+
*/
|
|
1289
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1290
|
+
_instance;
|
|
1291
|
+
/**
|
|
1292
|
+
* Create a new instance of Sha3.
|
|
1293
|
+
* @param bits The number of bits.
|
|
1294
|
+
*/
|
|
1295
|
+
constructor(bits = Sha3.SIZE_256) {
|
|
1296
|
+
if (bits !== Sha3.SIZE_224 &&
|
|
1297
|
+
bits !== Sha3.SIZE_256 &&
|
|
1298
|
+
bits !== Sha3.SIZE_384 &&
|
|
1299
|
+
bits !== Sha3.SIZE_512) {
|
|
1300
|
+
throw new GeneralError(Sha3._CLASS_NAME, "bitSize", { bitSize: bits });
|
|
1301
|
+
}
|
|
1302
|
+
if (bits === Sha3.SIZE_224) {
|
|
1303
|
+
// eslint-disable-next-line camelcase
|
|
1304
|
+
this._instance = sha3_224.create();
|
|
1305
|
+
}
|
|
1306
|
+
else if (bits === Sha3.SIZE_256) {
|
|
1307
|
+
// eslint-disable-next-line camelcase
|
|
1308
|
+
this._instance = sha3_256.create();
|
|
1309
|
+
}
|
|
1310
|
+
else if (bits === Sha3.SIZE_384) {
|
|
1311
|
+
// eslint-disable-next-line camelcase
|
|
1312
|
+
this._instance = sha3_384.create();
|
|
1313
|
+
}
|
|
1314
|
+
else {
|
|
1315
|
+
// eslint-disable-next-line camelcase
|
|
1316
|
+
this._instance = sha3_512.create();
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
/**
|
|
1320
|
+
* Perform Sum 256 on the block.
|
|
1321
|
+
* @param block The block to operate on.
|
|
1322
|
+
* @returns The sum 256 of the block.
|
|
1323
|
+
*/
|
|
1324
|
+
static sum256(block) {
|
|
1325
|
+
const b2b = new Sha3(Sha3.SIZE_256);
|
|
1326
|
+
b2b.update(block);
|
|
1327
|
+
return b2b.digest();
|
|
1328
|
+
}
|
|
1329
|
+
/**
|
|
1330
|
+
* Perform Sum 224 on the block.
|
|
1331
|
+
* @param block The block to operate on.
|
|
1332
|
+
* @returns The sum 224 of the block.
|
|
1333
|
+
*/
|
|
1334
|
+
static sum224(block) {
|
|
1335
|
+
const b2b = new Sha3(Sha3.SIZE_224);
|
|
1336
|
+
b2b.update(block);
|
|
1337
|
+
return b2b.digest();
|
|
1338
|
+
}
|
|
1339
|
+
/**
|
|
1340
|
+
* Perform Sum 384 on the block.
|
|
1341
|
+
* @param block The block to operate on.
|
|
1342
|
+
* @returns The sum 384 of the block.
|
|
1343
|
+
*/
|
|
1344
|
+
static sum384(block) {
|
|
1345
|
+
const b2b = new Sha3(Sha3.SIZE_384);
|
|
1346
|
+
b2b.update(block);
|
|
1347
|
+
return b2b.digest();
|
|
1348
|
+
}
|
|
1349
|
+
/**
|
|
1350
|
+
* Perform Sum 512 on the block.
|
|
1351
|
+
* @param block The block to operate on.
|
|
1352
|
+
* @returns The sum 512 of the block.
|
|
1353
|
+
*/
|
|
1354
|
+
static sum512(block) {
|
|
1355
|
+
const b2b = new Sha3(Sha3.SIZE_512);
|
|
1356
|
+
b2b.update(block);
|
|
1357
|
+
return b2b.digest();
|
|
1358
|
+
}
|
|
1359
|
+
/**
|
|
1360
|
+
* Update the hash with the block.
|
|
1361
|
+
* @param block The block to update the hash with.
|
|
1362
|
+
* @returns The instance for chaining.
|
|
1363
|
+
*/
|
|
1364
|
+
update(block) {
|
|
1365
|
+
Guards.uint8Array(Sha3._CLASS_NAME, "block", block);
|
|
1366
|
+
this._instance.update(block);
|
|
1367
|
+
return this;
|
|
1368
|
+
}
|
|
1369
|
+
/**
|
|
1370
|
+
* Get the digest for the hash.
|
|
1371
|
+
* @returns The instance for chaining.
|
|
1372
|
+
*/
|
|
1373
|
+
digest() {
|
|
1374
|
+
return this._instance.digest();
|
|
1375
|
+
}
|
|
1376
|
+
}
|
|
1377
|
+
|
|
1141
1378
|
// Copyright 2024 IOTA Stiftung.
|
|
1142
1379
|
// SPDX-License-Identifier: Apache-2.0.
|
|
1143
1380
|
/* eslint-disable camelcase */
|
|
@@ -1525,4 +1762,4 @@ class PasswordValidator {
|
|
|
1525
1762
|
}
|
|
1526
1763
|
}
|
|
1527
1764
|
|
|
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 };
|
|
1765
|
+
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,10 @@ 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>;
|
|
37
43
|
}
|
|
@@ -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
|
+
}
|