@twin.org/crypto 0.0.2-next.9 → 0.0.3-next.2
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/es/address/bech32.js +72 -0
- package/dist/es/address/bech32.js.map +1 -0
- package/dist/es/address/bip44.js +113 -0
- package/dist/es/address/bip44.js.map +1 -0
- package/dist/es/ciphers/chaCha20Poly1305.js +48 -0
- package/dist/es/ciphers/chaCha20Poly1305.js.map +1 -0
- package/dist/es/curves/ed25519.js +114 -0
- package/dist/es/curves/ed25519.js.map +1 -0
- package/dist/es/curves/secp256k1.js +82 -0
- package/dist/es/curves/secp256k1.js.map +1 -0
- package/dist/es/curves/x25519.js +34 -0
- package/dist/es/curves/x25519.js.map +1 -0
- package/dist/es/curves/zip215.js +33 -0
- package/dist/es/curves/zip215.js.map +1 -0
- package/dist/es/hashes/blake2b.js +90 -0
- package/dist/es/hashes/blake2b.js.map +1 -0
- package/dist/es/hashes/blake3.js +76 -0
- package/dist/es/hashes/blake3.js.map +1 -0
- package/dist/es/hashes/hmacSha1.js +56 -0
- package/dist/es/hashes/hmacSha1.js.map +1 -0
- package/dist/es/hashes/hmacSha256.js +83 -0
- package/dist/es/hashes/hmacSha256.js.map +1 -0
- package/dist/es/hashes/hmacSha512.js +132 -0
- package/dist/es/hashes/hmacSha512.js.map +1 -0
- package/dist/es/hashes/pbkdf2.js +45 -0
- package/dist/es/hashes/pbkdf2.js.map +1 -0
- package/dist/es/hashes/sha1.js +52 -0
- package/dist/es/hashes/sha1.js.map +1 -0
- package/dist/es/hashes/sha256.js +75 -0
- package/dist/es/hashes/sha256.js.map +1 -0
- package/dist/es/hashes/sha3.js +122 -0
- package/dist/es/hashes/sha3.js.map +1 -0
- package/dist/es/hashes/sha512.js +118 -0
- package/dist/es/hashes/sha512.js.map +1 -0
- package/dist/es/helpers/pemHelper.js +42 -0
- package/dist/es/helpers/pemHelper.js.map +1 -0
- package/dist/es/index.js +29 -0
- package/dist/es/index.js.map +1 -0
- package/dist/es/keys/bip32Path.js +72 -0
- package/dist/es/keys/bip32Path.js.map +1 -0
- package/dist/es/keys/bip39.js +83 -0
- package/dist/es/keys/bip39.js.map +1 -0
- package/dist/es/keys/slip0010.js +83 -0
- package/dist/es/keys/slip0010.js.map +1 -0
- package/dist/es/models/keyType.js +17 -0
- package/dist/es/models/keyType.js.map +1 -0
- package/dist/es/otp/hotp.js +27 -0
- package/dist/es/otp/hotp.js.map +1 -0
- package/dist/es/otp/totp.js +75 -0
- package/dist/es/otp/totp.js.map +1 -0
- package/dist/es/passwords/passwordGenerator.js +28 -0
- package/dist/es/passwords/passwordGenerator.js.map +1 -0
- package/dist/es/passwords/passwordValidator.js +81 -0
- package/dist/es/passwords/passwordValidator.js.map +1 -0
- package/dist/types/address/bech32.d.ts +4 -0
- package/dist/types/address/bip44.d.ts +6 -2
- package/dist/types/ciphers/chaCha20Poly1305.d.ts +4 -0
- package/dist/types/curves/ed25519.d.ts +4 -0
- package/dist/types/curves/secp256k1.d.ts +4 -0
- package/dist/types/curves/x25519.d.ts +4 -0
- package/dist/types/curves/zip215.d.ts +4 -0
- package/dist/types/hashes/blake2b.d.ts +4 -0
- package/dist/types/hashes/blake3.d.ts +4 -0
- package/dist/types/hashes/hmacSha1.d.ts +4 -0
- package/dist/types/hashes/hmacSha256.d.ts +4 -0
- package/dist/types/hashes/hmacSha512.d.ts +4 -0
- package/dist/types/hashes/pbkdf2.d.ts +4 -0
- package/dist/types/hashes/sha1.d.ts +4 -0
- package/dist/types/hashes/sha256.d.ts +4 -0
- package/dist/types/hashes/sha3.d.ts +4 -0
- package/dist/types/hashes/sha512.d.ts +4 -0
- package/dist/types/helpers/pemHelper.d.ts +4 -0
- package/dist/types/index.d.ts +26 -26
- package/dist/types/keys/bip39.d.ts +4 -0
- package/dist/types/keys/slip0010.d.ts +6 -2
- package/dist/types/otp/hotp.d.ts +4 -0
- package/docs/changelog.md +286 -0
- package/docs/reference/classes/Bech32.md +8 -0
- package/docs/reference/classes/Bip39.md +8 -0
- package/docs/reference/classes/Bip44.md +8 -0
- package/docs/reference/classes/Blake2b.md +8 -0
- package/docs/reference/classes/Blake3.md +8 -0
- package/docs/reference/classes/ChaCha20Poly1305.md +8 -0
- package/docs/reference/classes/Ed25519.md +8 -0
- package/docs/reference/classes/HmacSha1.md +8 -0
- package/docs/reference/classes/HmacSha256.md +8 -0
- package/docs/reference/classes/HmacSha512.md +8 -0
- package/docs/reference/classes/Hotp.md +8 -0
- package/docs/reference/classes/Pbkdf2.md +8 -0
- package/docs/reference/classes/PemHelper.md +8 -0
- package/docs/reference/classes/Secp256k1.md +8 -0
- package/docs/reference/classes/Sha1.md +8 -0
- package/docs/reference/classes/Sha256.md +8 -0
- package/docs/reference/classes/Sha3.md +8 -0
- package/docs/reference/classes/Sha512.md +8 -0
- package/docs/reference/classes/Slip0010.md +8 -0
- package/docs/reference/classes/Totp.md +2 -2
- package/docs/reference/classes/X25519.md +8 -0
- package/docs/reference/classes/Zip215.md +8 -0
- package/locales/en.json +9 -28
- package/package.json +29 -16
- package/dist/cjs/index.cjs +0 -1876
- package/dist/esm/index.mjs +0 -1829
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
import { sha224, sha256 } from "@noble/hashes/sha2.js";
|
|
4
|
+
import { GeneralError, Guards } from "@twin.org/core";
|
|
5
|
+
/**
|
|
6
|
+
* Perform a SHA-256 hash on the block.
|
|
7
|
+
*/
|
|
8
|
+
export class Sha256 {
|
|
9
|
+
/**
|
|
10
|
+
* Sha256 256.
|
|
11
|
+
*/
|
|
12
|
+
static SIZE_256 = 256;
|
|
13
|
+
/**
|
|
14
|
+
* Sha256 224.
|
|
15
|
+
*/
|
|
16
|
+
static SIZE_224 = 224;
|
|
17
|
+
/**
|
|
18
|
+
* Runtime name for the class.
|
|
19
|
+
*/
|
|
20
|
+
static CLASS_NAME = "Sha256";
|
|
21
|
+
/**
|
|
22
|
+
* The instance of the hash.
|
|
23
|
+
* @internal
|
|
24
|
+
*/
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
26
|
+
_instance;
|
|
27
|
+
/**
|
|
28
|
+
* Create a new instance of Sha256.
|
|
29
|
+
* @param bits The number of bits.
|
|
30
|
+
*/
|
|
31
|
+
constructor(bits = Sha256.SIZE_256) {
|
|
32
|
+
if (bits !== Sha256.SIZE_224 && bits !== Sha256.SIZE_256) {
|
|
33
|
+
throw new GeneralError(Sha256.CLASS_NAME, "bitSize", { bitSize: bits });
|
|
34
|
+
}
|
|
35
|
+
this._instance = bits === Sha256.SIZE_256 ? sha256.create() : sha224.create();
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Perform Sum 256 on the block.
|
|
39
|
+
* @param block The block to operate on.
|
|
40
|
+
* @returns The sum 256 of the block.
|
|
41
|
+
*/
|
|
42
|
+
static sum256(block) {
|
|
43
|
+
const b2b = new Sha256(Sha256.SIZE_256);
|
|
44
|
+
b2b.update(block);
|
|
45
|
+
return b2b.digest();
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Perform Sum 224 on the block.
|
|
49
|
+
* @param block The block to operate on.
|
|
50
|
+
* @returns The sum 224 of the block.
|
|
51
|
+
*/
|
|
52
|
+
static sum224(block) {
|
|
53
|
+
const b2b = new Sha256(Sha256.SIZE_224);
|
|
54
|
+
b2b.update(block);
|
|
55
|
+
return b2b.digest();
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Update the hash with the block.
|
|
59
|
+
* @param block The block to update the hash with.
|
|
60
|
+
* @returns The instance for chaining.
|
|
61
|
+
*/
|
|
62
|
+
update(block) {
|
|
63
|
+
Guards.uint8Array(Sha256.CLASS_NAME, "block", block);
|
|
64
|
+
this._instance.update(block);
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Get the digest for the hash.
|
|
69
|
+
* @returns The instance for chaining.
|
|
70
|
+
*/
|
|
71
|
+
digest() {
|
|
72
|
+
return this._instance.digest();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=sha256.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sha256.js","sourceRoot":"","sources":["../../../src/hashes/sha256.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAEvD,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAGtD;;GAEG;AACH,MAAM,OAAO,MAAM;IAClB;;OAEG;IACI,MAAM,CAAU,QAAQ,GAAW,GAAG,CAAC;IAE9C;;OAEG;IACI,MAAM,CAAU,QAAQ,GAAW,GAAG,CAAC;IAE9C;;OAEG;IACI,MAAM,CAAU,UAAU,YAA4B;IAE7D;;;OAGG;IACH,8DAA8D;IAC7C,SAAS,CAAY;IAEtC;;;OAGG;IACH,YAAY,OAAe,MAAM,CAAC,QAAQ;QACzC,IAAI,IAAI,KAAK,MAAM,CAAC,QAAQ,IAAI,IAAI,KAAK,MAAM,CAAC,QAAQ,EAAE,CAAC;YAC1D,MAAM,IAAI,YAAY,CAAC,MAAM,CAAC,UAAU,EAAE,SAAS,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;QACzE,CAAC;QAED,IAAI,CAAC,SAAS,GAAG,IAAI,KAAK,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;IAC/E,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,MAAM,CAAC,KAAiB;QACrC,MAAM,GAAG,GAAG,IAAI,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QACxC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAClB,OAAO,GAAG,CAAC,MAAM,EAAE,CAAC;IACrB,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,MAAM,CAAC,KAAiB;QACrC,MAAM,GAAG,GAAG,IAAI,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QACxC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAClB,OAAO,GAAG,CAAC,MAAM,EAAE,CAAC;IACrB,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,KAAiB;QAC9B,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,UAAU,WAAiB,KAAK,CAAC,CAAC;QAC3D,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC7B,OAAO,IAAI,CAAC;IACb,CAAC;IAED;;;OAGG;IACI,MAAM;QACZ,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC;IAChC,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { sha224, sha256 } from \"@noble/hashes/sha2.js\";\nimport type { Hash } from \"@noble/hashes/utils.js\";\nimport { GeneralError, Guards } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\n\n/**\n * Perform a SHA-256 hash on the block.\n */\nexport class Sha256 {\n\t/**\n\t * Sha256 256.\n\t */\n\tpublic static readonly SIZE_256: number = 256;\n\n\t/**\n\t * Sha256 224.\n\t */\n\tpublic static readonly SIZE_224: number = 224;\n\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Sha256>();\n\n\t/**\n\t * The instance of the hash.\n\t * @internal\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tprivate readonly _instance: Hash<any>;\n\n\t/**\n\t * Create a new instance of Sha256.\n\t * @param bits The number of bits.\n\t */\n\tconstructor(bits: number = Sha256.SIZE_256) {\n\t\tif (bits !== Sha256.SIZE_224 && bits !== Sha256.SIZE_256) {\n\t\t\tthrow new GeneralError(Sha256.CLASS_NAME, \"bitSize\", { bitSize: bits });\n\t\t}\n\n\t\tthis._instance = bits === Sha256.SIZE_256 ? sha256.create() : sha224.create();\n\t}\n\n\t/**\n\t * Perform Sum 256 on the block.\n\t * @param block The block to operate on.\n\t * @returns The sum 256 of the block.\n\t */\n\tpublic static sum256(block: Uint8Array): Uint8Array {\n\t\tconst b2b = new Sha256(Sha256.SIZE_256);\n\t\tb2b.update(block);\n\t\treturn b2b.digest();\n\t}\n\n\t/**\n\t * Perform Sum 224 on the block.\n\t * @param block The block to operate on.\n\t * @returns The sum 224 of the block.\n\t */\n\tpublic static sum224(block: Uint8Array): Uint8Array {\n\t\tconst b2b = new Sha256(Sha256.SIZE_224);\n\t\tb2b.update(block);\n\t\treturn b2b.digest();\n\t}\n\n\t/**\n\t * Update the hash with the block.\n\t * @param block The block to update the hash with.\n\t * @returns The instance for chaining.\n\t */\n\tpublic update(block: Uint8Array): Sha256 {\n\t\tGuards.uint8Array(Sha256.CLASS_NAME, nameof(block), block);\n\t\tthis._instance.update(block);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Get the digest for the hash.\n\t * @returns The instance for chaining.\n\t */\n\tpublic digest(): Uint8Array {\n\t\treturn this._instance.digest();\n\t}\n}\n"]}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
// eslint-disable-next-line camelcase
|
|
4
|
+
import { sha3_224, sha3_256, sha3_384, sha3_512 } from "@noble/hashes/sha3.js";
|
|
5
|
+
import { GeneralError, Guards } from "@twin.org/core";
|
|
6
|
+
/**
|
|
7
|
+
* Perform a SHA-3 hash on the block.
|
|
8
|
+
*/
|
|
9
|
+
export class Sha3 {
|
|
10
|
+
/**
|
|
11
|
+
* Sha3 224.
|
|
12
|
+
*/
|
|
13
|
+
static SIZE_224 = 224;
|
|
14
|
+
/**
|
|
15
|
+
* Sha3 256.
|
|
16
|
+
*/
|
|
17
|
+
static SIZE_256 = 256;
|
|
18
|
+
/**
|
|
19
|
+
* Sha3 384.
|
|
20
|
+
*/
|
|
21
|
+
static SIZE_384 = 384;
|
|
22
|
+
/**
|
|
23
|
+
* Sha3 512.
|
|
24
|
+
*/
|
|
25
|
+
static SIZE_512 = 512;
|
|
26
|
+
/**
|
|
27
|
+
* Runtime name for the class.
|
|
28
|
+
*/
|
|
29
|
+
static CLASS_NAME = "Sha3";
|
|
30
|
+
/**
|
|
31
|
+
* The instance of the hash.
|
|
32
|
+
* @internal
|
|
33
|
+
*/
|
|
34
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
35
|
+
_instance;
|
|
36
|
+
/**
|
|
37
|
+
* Create a new instance of Sha3.
|
|
38
|
+
* @param bits The number of bits.
|
|
39
|
+
*/
|
|
40
|
+
constructor(bits = Sha3.SIZE_256) {
|
|
41
|
+
if (bits !== Sha3.SIZE_224 &&
|
|
42
|
+
bits !== Sha3.SIZE_256 &&
|
|
43
|
+
bits !== Sha3.SIZE_384 &&
|
|
44
|
+
bits !== Sha3.SIZE_512) {
|
|
45
|
+
throw new GeneralError(Sha3.CLASS_NAME, "bitSize", { bitSize: bits });
|
|
46
|
+
}
|
|
47
|
+
if (bits === Sha3.SIZE_224) {
|
|
48
|
+
// eslint-disable-next-line camelcase
|
|
49
|
+
this._instance = sha3_224.create();
|
|
50
|
+
}
|
|
51
|
+
else if (bits === Sha3.SIZE_256) {
|
|
52
|
+
// eslint-disable-next-line camelcase
|
|
53
|
+
this._instance = sha3_256.create();
|
|
54
|
+
}
|
|
55
|
+
else if (bits === Sha3.SIZE_384) {
|
|
56
|
+
// eslint-disable-next-line camelcase
|
|
57
|
+
this._instance = sha3_384.create();
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
// eslint-disable-next-line camelcase
|
|
61
|
+
this._instance = sha3_512.create();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Perform Sum 256 on the block.
|
|
66
|
+
* @param block The block to operate on.
|
|
67
|
+
* @returns The sum 256 of the block.
|
|
68
|
+
*/
|
|
69
|
+
static sum256(block) {
|
|
70
|
+
const b2b = new Sha3(Sha3.SIZE_256);
|
|
71
|
+
b2b.update(block);
|
|
72
|
+
return b2b.digest();
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Perform Sum 224 on the block.
|
|
76
|
+
* @param block The block to operate on.
|
|
77
|
+
* @returns The sum 224 of the block.
|
|
78
|
+
*/
|
|
79
|
+
static sum224(block) {
|
|
80
|
+
const b2b = new Sha3(Sha3.SIZE_224);
|
|
81
|
+
b2b.update(block);
|
|
82
|
+
return b2b.digest();
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Perform Sum 384 on the block.
|
|
86
|
+
* @param block The block to operate on.
|
|
87
|
+
* @returns The sum 384 of the block.
|
|
88
|
+
*/
|
|
89
|
+
static sum384(block) {
|
|
90
|
+
const b2b = new Sha3(Sha3.SIZE_384);
|
|
91
|
+
b2b.update(block);
|
|
92
|
+
return b2b.digest();
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Perform Sum 512 on the block.
|
|
96
|
+
* @param block The block to operate on.
|
|
97
|
+
* @returns The sum 512 of the block.
|
|
98
|
+
*/
|
|
99
|
+
static sum512(block) {
|
|
100
|
+
const b2b = new Sha3(Sha3.SIZE_512);
|
|
101
|
+
b2b.update(block);
|
|
102
|
+
return b2b.digest();
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Update the hash with the block.
|
|
106
|
+
* @param block The block to update the hash with.
|
|
107
|
+
* @returns The instance for chaining.
|
|
108
|
+
*/
|
|
109
|
+
update(block) {
|
|
110
|
+
Guards.uint8Array(Sha3.CLASS_NAME, "block", block);
|
|
111
|
+
this._instance.update(block);
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Get the digest for the hash.
|
|
116
|
+
* @returns The instance for chaining.
|
|
117
|
+
*/
|
|
118
|
+
digest() {
|
|
119
|
+
return this._instance.digest();
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=sha3.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sha3.js","sourceRoot":"","sources":["../../../src/hashes/sha3.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,qCAAqC;AACrC,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAE/E,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAGtD;;GAEG;AACH,MAAM,OAAO,IAAI;IAChB;;OAEG;IACI,MAAM,CAAU,QAAQ,GAAW,GAAG,CAAC;IAE9C;;OAEG;IACI,MAAM,CAAU,QAAQ,GAAW,GAAG,CAAC;IAE9C;;OAEG;IACI,MAAM,CAAU,QAAQ,GAAW,GAAG,CAAC;IAE9C;;OAEG;IACI,MAAM,CAAU,QAAQ,GAAW,GAAG,CAAC;IAE9C;;OAEG;IACI,MAAM,CAAU,UAAU,UAA0B;IAE3D;;;OAGG;IACH,8DAA8D;IAC7C,SAAS,CAAY;IAEtC;;;OAGG;IACH,YAAY,OAAe,IAAI,CAAC,QAAQ;QACvC,IACC,IAAI,KAAK,IAAI,CAAC,QAAQ;YACtB,IAAI,KAAK,IAAI,CAAC,QAAQ;YACtB,IAAI,KAAK,IAAI,CAAC,QAAQ;YACtB,IAAI,KAAK,IAAI,CAAC,QAAQ,EACrB,CAAC;YACF,MAAM,IAAI,YAAY,CAAC,IAAI,CAAC,UAAU,EAAE,SAAS,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;QACvE,CAAC;QAED,IAAI,IAAI,KAAK,IAAI,CAAC,QAAQ,EAAE,CAAC;YAC5B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IAAI,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QACpC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAClB,OAAO,GAAG,CAAC,MAAM,EAAE,CAAC;IACrB,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,KAAiB;QAC9B,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,UAAU,WAAiB,KAAK,CAAC,CAAC;QACzD,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC7B,OAAO,IAAI,CAAC;IACb,CAAC;IAED;;;OAGG;IACI,MAAM;QACZ,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC;IAChC,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n// eslint-disable-next-line camelcase\nimport { sha3_224, sha3_256, sha3_384, sha3_512 } from \"@noble/hashes/sha3.js\";\nimport type { Hash } from \"@noble/hashes/utils.js\";\nimport { GeneralError, Guards } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\n\n/**\n * Perform a SHA-3 hash on the block.\n */\nexport class Sha3 {\n\t/**\n\t * Sha3 224.\n\t */\n\tpublic static readonly SIZE_224: number = 224;\n\n\t/**\n\t * Sha3 256.\n\t */\n\tpublic static readonly SIZE_256: number = 256;\n\n\t/**\n\t * Sha3 384.\n\t */\n\tpublic static readonly SIZE_384: number = 384;\n\n\t/**\n\t * Sha3 512.\n\t */\n\tpublic static readonly SIZE_512: number = 512;\n\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Sha3>();\n\n\t/**\n\t * The instance of the hash.\n\t * @internal\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tprivate readonly _instance: Hash<any>;\n\n\t/**\n\t * Create a new instance of Sha3.\n\t * @param bits The number of bits.\n\t */\n\tconstructor(bits: number = Sha3.SIZE_256) {\n\t\tif (\n\t\t\tbits !== Sha3.SIZE_224 &&\n\t\t\tbits !== Sha3.SIZE_256 &&\n\t\t\tbits !== Sha3.SIZE_384 &&\n\t\t\tbits !== Sha3.SIZE_512\n\t\t) {\n\t\t\tthrow new GeneralError(Sha3.CLASS_NAME, \"bitSize\", { bitSize: bits });\n\t\t}\n\n\t\tif (bits === Sha3.SIZE_224) {\n\t\t\t// eslint-disable-next-line camelcase\n\t\t\tthis._instance = sha3_224.create();\n\t\t} else if (bits === Sha3.SIZE_256) {\n\t\t\t// eslint-disable-next-line camelcase\n\t\t\tthis._instance = sha3_256.create();\n\t\t} else if (bits === Sha3.SIZE_384) {\n\t\t\t// eslint-disable-next-line camelcase\n\t\t\tthis._instance = sha3_384.create();\n\t\t} else {\n\t\t\t// eslint-disable-next-line camelcase\n\t\t\tthis._instance = sha3_512.create();\n\t\t}\n\t}\n\n\t/**\n\t * Perform Sum 256 on the block.\n\t * @param block The block to operate on.\n\t * @returns The sum 256 of the block.\n\t */\n\tpublic static sum256(block: Uint8Array): Uint8Array {\n\t\tconst b2b = new Sha3(Sha3.SIZE_256);\n\t\tb2b.update(block);\n\t\treturn b2b.digest();\n\t}\n\n\t/**\n\t * Perform Sum 224 on the block.\n\t * @param block The block to operate on.\n\t * @returns The sum 224 of the block.\n\t */\n\tpublic static sum224(block: Uint8Array): Uint8Array {\n\t\tconst b2b = new Sha3(Sha3.SIZE_224);\n\t\tb2b.update(block);\n\t\treturn b2b.digest();\n\t}\n\n\t/**\n\t * Perform Sum 384 on the block.\n\t * @param block The block to operate on.\n\t * @returns The sum 384 of the block.\n\t */\n\tpublic static sum384(block: Uint8Array): Uint8Array {\n\t\tconst b2b = new Sha3(Sha3.SIZE_384);\n\t\tb2b.update(block);\n\t\treturn b2b.digest();\n\t}\n\n\t/**\n\t * Perform Sum 512 on the block.\n\t * @param block The block to operate on.\n\t * @returns The sum 512 of the block.\n\t */\n\tpublic static sum512(block: Uint8Array): Uint8Array {\n\t\tconst b2b = new Sha3(Sha3.SIZE_512);\n\t\tb2b.update(block);\n\t\treturn b2b.digest();\n\t}\n\n\t/**\n\t * Update the hash with the block.\n\t * @param block The block to update the hash with.\n\t * @returns The instance for chaining.\n\t */\n\tpublic update(block: Uint8Array): Sha3 {\n\t\tGuards.uint8Array(Sha3.CLASS_NAME, nameof(block), block);\n\t\tthis._instance.update(block);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Get the digest for the hash.\n\t * @returns The instance for chaining.\n\t */\n\tpublic digest(): Uint8Array {\n\t\treturn this._instance.digest();\n\t}\n}\n"]}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
/* eslint-disable camelcase */
|
|
4
|
+
import { sha384, sha512, sha512_224, sha512_256 } from "@noble/hashes/sha2.js";
|
|
5
|
+
import { GeneralError, Guards } from "@twin.org/core";
|
|
6
|
+
/**
|
|
7
|
+
* Perform a SHA-512 hash on the block.
|
|
8
|
+
*/
|
|
9
|
+
export class Sha512 {
|
|
10
|
+
/**
|
|
11
|
+
* Sha512 224.
|
|
12
|
+
*/
|
|
13
|
+
static SIZE_224 = 224;
|
|
14
|
+
/**
|
|
15
|
+
* Sha512 256.
|
|
16
|
+
*/
|
|
17
|
+
static SIZE_256 = 256;
|
|
18
|
+
/**
|
|
19
|
+
* Sha512 384.
|
|
20
|
+
*/
|
|
21
|
+
static SIZE_384 = 384;
|
|
22
|
+
/**
|
|
23
|
+
* Sha512 512.
|
|
24
|
+
*/
|
|
25
|
+
static SIZE_512 = 512;
|
|
26
|
+
/**
|
|
27
|
+
* Runtime name for the class.
|
|
28
|
+
*/
|
|
29
|
+
static CLASS_NAME = "Sha512";
|
|
30
|
+
/**
|
|
31
|
+
* The instance of the hash.
|
|
32
|
+
* @internal
|
|
33
|
+
*/
|
|
34
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
35
|
+
_instance;
|
|
36
|
+
/**
|
|
37
|
+
* Create a new instance of Sha512.
|
|
38
|
+
* @param bits The number of bits.
|
|
39
|
+
*/
|
|
40
|
+
constructor(bits = Sha512.SIZE_512) {
|
|
41
|
+
if (bits !== Sha512.SIZE_224 &&
|
|
42
|
+
bits !== Sha512.SIZE_256 &&
|
|
43
|
+
bits !== Sha512.SIZE_384 &&
|
|
44
|
+
bits !== Sha512.SIZE_512) {
|
|
45
|
+
throw new GeneralError(Sha512.CLASS_NAME, "bitSize", { bitSize: bits });
|
|
46
|
+
}
|
|
47
|
+
if (bits === Sha512.SIZE_224) {
|
|
48
|
+
this._instance = sha512_224.create();
|
|
49
|
+
}
|
|
50
|
+
else if (bits === Sha512.SIZE_256) {
|
|
51
|
+
this._instance = sha512_256.create();
|
|
52
|
+
}
|
|
53
|
+
else if (bits === Sha512.SIZE_384) {
|
|
54
|
+
this._instance = sha384.create();
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
this._instance = sha512.create();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Perform Sum 512 on the block.
|
|
62
|
+
* @param block The block to operate on.
|
|
63
|
+
* @returns The sum 512 of the block.
|
|
64
|
+
*/
|
|
65
|
+
static sum512(block) {
|
|
66
|
+
const b2b = new Sha512(Sha512.SIZE_512);
|
|
67
|
+
b2b.update(block);
|
|
68
|
+
return b2b.digest();
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Perform Sum 384 on the block.
|
|
72
|
+
* @param block The block to operate on.
|
|
73
|
+
* @returns The sum 384 of the block.
|
|
74
|
+
*/
|
|
75
|
+
static sum384(block) {
|
|
76
|
+
const b2b = new Sha512(Sha512.SIZE_384);
|
|
77
|
+
b2b.update(block);
|
|
78
|
+
return b2b.digest();
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Perform Sum 256 on the block.
|
|
82
|
+
* @param block The block to operate on.
|
|
83
|
+
* @returns The sum 256 of the block.
|
|
84
|
+
*/
|
|
85
|
+
static sum256(block) {
|
|
86
|
+
const b2b = new Sha512(Sha512.SIZE_256);
|
|
87
|
+
b2b.update(block);
|
|
88
|
+
return b2b.digest();
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Perform Sum 224 on the block.
|
|
92
|
+
* @param block The block to operate on.
|
|
93
|
+
* @returns The sum 224 of the block.
|
|
94
|
+
*/
|
|
95
|
+
static sum224(block) {
|
|
96
|
+
const b2b = new Sha512(Sha512.SIZE_224);
|
|
97
|
+
b2b.update(block);
|
|
98
|
+
return b2b.digest();
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Update the hash with the block.
|
|
102
|
+
* @param block The block to update the hash with.
|
|
103
|
+
* @returns The instance for chaining.
|
|
104
|
+
*/
|
|
105
|
+
update(block) {
|
|
106
|
+
Guards.uint8Array(Sha512.CLASS_NAME, "block", block);
|
|
107
|
+
this._instance.update(block);
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Get the digest for the hash.
|
|
112
|
+
* @returns The instance for chaining.
|
|
113
|
+
*/
|
|
114
|
+
digest() {
|
|
115
|
+
return this._instance.digest();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=sha512.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sha512.js","sourceRoot":"","sources":["../../../src/hashes/sha512.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,8BAA8B;AAC9B,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAE/E,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAGtD;;GAEG;AACH,MAAM,OAAO,MAAM;IAClB;;OAEG;IACI,MAAM,CAAC,QAAQ,GAAW,GAAG,CAAC;IAErC;;OAEG;IACI,MAAM,CAAC,QAAQ,GAAW,GAAG,C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C;IACrB,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,MAAM,CAAC,KAAiB;QACrC,MAAM,GAAG,GAAG,IAAI,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QACxC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAClB,OAAO,GAAG,CAAC,MAAM,EAAE,CAAC;IACrB,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,MAAM,CAAC,KAAiB;QACrC,MAAM,GAAG,GAAG,IAAI,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QACxC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAClB,OAAO,GAAG,CAAC,MAAM,EAAE,CAAC;IACrB,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,MAAM,CAAC,KAAiB;QACrC,MAAM,GAAG,GAAG,IAAI,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QACxC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAClB,OAAO,GAAG,CAAC,MAAM,EAAE,CAAC;IACrB,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,KAAiB;QAC9B,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,UAAU,WAAiB,KAAK,CAAC,CAAC;QAC3D,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC7B,OAAO,IAAI,CAAC;IACb,CAAC;IAED;;;OAGG;IACI,MAAM;QACZ,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC;IAChC,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n/* eslint-disable camelcase */\nimport { sha384, sha512, sha512_224, sha512_256 } from \"@noble/hashes/sha2.js\";\nimport type { Hash } from \"@noble/hashes/utils.js\";\nimport { GeneralError, Guards } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\n\n/**\n * Perform a SHA-512 hash on the block.\n */\nexport class Sha512 {\n\t/**\n\t * Sha512 224.\n\t */\n\tpublic static SIZE_224: number = 224;\n\n\t/**\n\t * Sha512 256.\n\t */\n\tpublic static SIZE_256: number = 256;\n\n\t/**\n\t * Sha512 384.\n\t */\n\tpublic static SIZE_384: number = 384;\n\n\t/**\n\t * Sha512 512.\n\t */\n\tpublic static SIZE_512: number = 512;\n\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Sha512>();\n\n\t/**\n\t * The instance of the hash.\n\t * @internal\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tprivate readonly _instance: Hash<any>;\n\n\t/**\n\t * Create a new instance of Sha512.\n\t * @param bits The number of bits.\n\t */\n\tconstructor(bits: number = Sha512.SIZE_512) {\n\t\tif (\n\t\t\tbits !== Sha512.SIZE_224 &&\n\t\t\tbits !== Sha512.SIZE_256 &&\n\t\t\tbits !== Sha512.SIZE_384 &&\n\t\t\tbits !== Sha512.SIZE_512\n\t\t) {\n\t\t\tthrow new GeneralError(Sha512.CLASS_NAME, \"bitSize\", { bitSize: bits });\n\t\t}\n\n\t\tif (bits === Sha512.SIZE_224) {\n\t\t\tthis._instance = sha512_224.create();\n\t\t} else if (bits === Sha512.SIZE_256) {\n\t\t\tthis._instance = sha512_256.create();\n\t\t} else if (bits === Sha512.SIZE_384) {\n\t\t\tthis._instance = sha384.create();\n\t\t} else {\n\t\t\tthis._instance = sha512.create();\n\t\t}\n\t}\n\n\t/**\n\t * Perform Sum 512 on the block.\n\t * @param block The block to operate on.\n\t * @returns The sum 512 of the block.\n\t */\n\tpublic static sum512(block: Uint8Array): Uint8Array {\n\t\tconst b2b = new Sha512(Sha512.SIZE_512);\n\t\tb2b.update(block);\n\t\treturn b2b.digest();\n\t}\n\n\t/**\n\t * Perform Sum 384 on the block.\n\t * @param block The block to operate on.\n\t * @returns The sum 384 of the block.\n\t */\n\tpublic static sum384(block: Uint8Array): Uint8Array {\n\t\tconst b2b = new Sha512(Sha512.SIZE_384);\n\t\tb2b.update(block);\n\t\treturn b2b.digest();\n\t}\n\n\t/**\n\t * Perform Sum 256 on the block.\n\t * @param block The block to operate on.\n\t * @returns The sum 256 of the block.\n\t */\n\tpublic static sum256(block: Uint8Array): Uint8Array {\n\t\tconst b2b = new Sha512(Sha512.SIZE_256);\n\t\tb2b.update(block);\n\t\treturn b2b.digest();\n\t}\n\n\t/**\n\t * Perform Sum 224 on the block.\n\t * @param block The block to operate on.\n\t * @returns The sum 224 of the block.\n\t */\n\tpublic static sum224(block: Uint8Array): Uint8Array {\n\t\tconst b2b = new Sha512(Sha512.SIZE_224);\n\t\tb2b.update(block);\n\t\treturn b2b.digest();\n\t}\n\n\t/**\n\t * Update the hash with the block.\n\t * @param block The block to update the hash with.\n\t * @returns The instance for chaining.\n\t */\n\tpublic update(block: Uint8Array): Sha512 {\n\t\tGuards.uint8Array(Sha512.CLASS_NAME, nameof(block), block);\n\t\tthis._instance.update(block);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Get the digest for the hash.\n\t * @returns The instance for chaining.\n\t */\n\tpublic digest(): Uint8Array {\n\t\treturn this._instance.digest();\n\t}\n}\n"]}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
import { Guards } from "@twin.org/core";
|
|
4
|
+
/**
|
|
5
|
+
* Helper class for working with PEM (Privacy-Enhanced Mail) formatted data.
|
|
6
|
+
*/
|
|
7
|
+
export class PemHelper {
|
|
8
|
+
/**
|
|
9
|
+
* Runtime name for the class.
|
|
10
|
+
*/
|
|
11
|
+
static CLASS_NAME = "PemHelper";
|
|
12
|
+
/**
|
|
13
|
+
* Strip the PEM content of its headers, footers, and newlines.
|
|
14
|
+
* @param pemContent The PEM content to strip.
|
|
15
|
+
* @returns The stripped PEM content in bas64 format.
|
|
16
|
+
*/
|
|
17
|
+
static stripPemMarkers(pemContent) {
|
|
18
|
+
Guards.string(PemHelper.CLASS_NAME, "pemContent", pemContent);
|
|
19
|
+
return pemContent
|
|
20
|
+
.replace(/-----BEGIN.*-----/, "")
|
|
21
|
+
.replace(/-----END.*-----/, "")
|
|
22
|
+
.replace(/\n/g, "")
|
|
23
|
+
.trim();
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Format the PEM content to have a specific line length.
|
|
27
|
+
* @param marker The marker for the PEM content, e.g. RSA PRIVATE KEY
|
|
28
|
+
* @param base64Content The base64 content to format.
|
|
29
|
+
* @param lineLength The length of each line in the PEM content, default is 64 characters.
|
|
30
|
+
* @returns The formatted PEM content.
|
|
31
|
+
*/
|
|
32
|
+
static formatPem(marker, base64Content, lineLength = 64) {
|
|
33
|
+
Guards.stringValue(PemHelper.CLASS_NAME, "marker", marker);
|
|
34
|
+
Guards.stringBase64(PemHelper.CLASS_NAME, "base64Content", base64Content);
|
|
35
|
+
const lines = [];
|
|
36
|
+
for (let i = 0; i < base64Content.length; i += lineLength) {
|
|
37
|
+
lines.push(base64Content.slice(i, i + lineLength));
|
|
38
|
+
}
|
|
39
|
+
return [`-----BEGIN ${marker}-----`, ...lines, `-----END ${marker}-----`].join("\n");
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=pemHelper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pemHelper.js","sourceRoot":"","sources":["../../../src/helpers/pemHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAGxC;;GAEG;AACH,MAAM,OAAO,SAAS;IACrB;;OAEG;IACI,MAAM,CAAU,UAAU,eAA+B;IAEhE;;;;OAIG;IACI,MAAM,CAAC,eAAe,CAAC,UAAkB;QAC/C,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,UAAU,gBAAsB,UAAU,CAAC,CAAC;QACpE,OAAO,UAAU;aACf,OAAO,CAAC,mBAAmB,EAAE,EAAE,CAAC;aAChC,OAAO,CAAC,iBAAiB,EAAE,EAAE,CAAC;aAC9B,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC;aAClB,IAAI,EAAE,CAAC;IACV,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,SAAS,CAAC,MAAc,EAAE,aAAqB,EAAE,aAAqB,EAAE;QACrF,MAAM,CAAC,WAAW,CAAC,SAAS,CAAC,UAAU,YAAkB,MAAM,CAAC,CAAC;QACjE,MAAM,CAAC,YAAY,CAAC,SAAS,CAAC,UAAU,mBAAyB,aAAa,CAAC,CAAC;QAChF,MAAM,KAAK,GAAa,EAAE,CAAC;QAC3B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,aAAa,CAAC,MAAM,EAAE,CAAC,IAAI,UAAU,EAAE,CAAC;YAC3D,KAAK,CAAC,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC;QACpD,CAAC;QACD,OAAO,CAAC,cAAc,MAAM,OAAO,EAAE,GAAG,KAAK,EAAE,YAAY,MAAM,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACtF,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Guards } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\n\n/**\n * Helper class for working with PEM (Privacy-Enhanced Mail) formatted data.\n */\nexport class PemHelper {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<PemHelper>();\n\n\t/**\n\t * Strip the PEM content of its headers, footers, and newlines.\n\t * @param pemContent The PEM content to strip.\n\t * @returns The stripped PEM content in bas64 format.\n\t */\n\tpublic static stripPemMarkers(pemContent: string): string {\n\t\tGuards.string(PemHelper.CLASS_NAME, nameof(pemContent), pemContent);\n\t\treturn pemContent\n\t\t\t.replace(/-----BEGIN.*-----/, \"\")\n\t\t\t.replace(/-----END.*-----/, \"\")\n\t\t\t.replace(/\\n/g, \"\")\n\t\t\t.trim();\n\t}\n\n\t/**\n\t * Format the PEM content to have a specific line length.\n\t * @param marker The marker for the PEM content, e.g. RSA PRIVATE KEY\n\t * @param base64Content The base64 content to format.\n\t * @param lineLength The length of each line in the PEM content, default is 64 characters.\n\t * @returns The formatted PEM content.\n\t */\n\tpublic static formatPem(marker: string, base64Content: string, lineLength: number = 64): string {\n\t\tGuards.stringValue(PemHelper.CLASS_NAME, nameof(marker), marker);\n\t\tGuards.stringBase64(PemHelper.CLASS_NAME, nameof(base64Content), base64Content);\n\t\tconst lines: string[] = [];\n\t\tfor (let i = 0; i < base64Content.length; i += lineLength) {\n\t\t\tlines.push(base64Content.slice(i, i + lineLength));\n\t\t}\n\t\treturn [`-----BEGIN ${marker}-----`, ...lines, `-----END ${marker}-----`].join(\"\\n\");\n\t}\n}\n"]}
|
package/dist/es/index.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
export * from "./address/bech32.js";
|
|
4
|
+
export * from "./address/bip44.js";
|
|
5
|
+
export * from "./ciphers/chaCha20Poly1305.js";
|
|
6
|
+
export * from "./curves/ed25519.js";
|
|
7
|
+
export * from "./curves/secp256k1.js";
|
|
8
|
+
export * from "./curves/x25519.js";
|
|
9
|
+
export * from "./curves/zip215.js";
|
|
10
|
+
export * from "./hashes/blake2b.js";
|
|
11
|
+
export * from "./hashes/blake3.js";
|
|
12
|
+
export * from "./hashes/hmacSha1.js";
|
|
13
|
+
export * from "./hashes/hmacSha256.js";
|
|
14
|
+
export * from "./hashes/hmacSha512.js";
|
|
15
|
+
export * from "./hashes/pbkdf2.js";
|
|
16
|
+
export * from "./hashes/sha1.js";
|
|
17
|
+
export * from "./hashes/sha256.js";
|
|
18
|
+
export * from "./hashes/sha3.js";
|
|
19
|
+
export * from "./hashes/sha512.js";
|
|
20
|
+
export * from "./helpers/pemHelper.js";
|
|
21
|
+
export * from "./keys/bip32Path.js";
|
|
22
|
+
export * from "./keys/bip39.js";
|
|
23
|
+
export * from "./keys/slip0010.js";
|
|
24
|
+
export * from "./models/keyType.js";
|
|
25
|
+
export * from "./otp/hotp.js";
|
|
26
|
+
export * from "./otp/totp.js";
|
|
27
|
+
export * from "./passwords/passwordGenerator.js";
|
|
28
|
+
export * from "./passwords/passwordValidator.js";
|
|
29
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,cAAc,sBAAsB,CAAC;AACrC,cAAc,wBAAwB,CAAC;AACvC,cAAc,wBAAwB,CAAC;AACvC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,wBAAwB,CAAC;AACvC,cAAc,qBAAqB,CAAC;AACpC,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AACpC,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,kCAAkC,CAAC;AACjD,cAAc,kCAAkC,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nexport * from \"./address/bech32.js\";\nexport * from \"./address/bip44.js\";\nexport * from \"./ciphers/chaCha20Poly1305.js\";\nexport * from \"./curves/ed25519.js\";\nexport * from \"./curves/secp256k1.js\";\nexport * from \"./curves/x25519.js\";\nexport * from \"./curves/zip215.js\";\nexport * from \"./hashes/blake2b.js\";\nexport * from \"./hashes/blake3.js\";\nexport * from \"./hashes/hmacSha1.js\";\nexport * from \"./hashes/hmacSha256.js\";\nexport * from \"./hashes/hmacSha512.js\";\nexport * from \"./hashes/pbkdf2.js\";\nexport * from \"./hashes/sha1.js\";\nexport * from \"./hashes/sha256.js\";\nexport * from \"./hashes/sha3.js\";\nexport * from \"./hashes/sha512.js\";\nexport * from \"./helpers/pemHelper.js\";\nexport * from \"./keys/bip32Path.js\";\nexport * from \"./keys/bip39.js\";\nexport * from \"./keys/slip0010.js\";\nexport * from \"./models/keyType.js\";\nexport * from \"./otp/hotp.js\";\nexport * from \"./otp/totp.js\";\nexport * from \"./passwords/passwordGenerator.js\";\nexport * from \"./passwords/passwordValidator.js\";\n"]}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
/**
|
|
4
|
+
* Class to help with bip32 paths.
|
|
5
|
+
*/
|
|
6
|
+
export class Bip32Path {
|
|
7
|
+
/**
|
|
8
|
+
* The path.
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
_path;
|
|
12
|
+
/**
|
|
13
|
+
* Create a new instance of Bip32Path.
|
|
14
|
+
* @param initialPath Initial path to create.
|
|
15
|
+
*/
|
|
16
|
+
constructor(initialPath) {
|
|
17
|
+
if (initialPath) {
|
|
18
|
+
this._path = initialPath.split("/");
|
|
19
|
+
if (this._path[0] === "m") {
|
|
20
|
+
this._path.shift();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
this._path = [];
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Construct a new path by cloning an existing one.
|
|
29
|
+
* @param bip32Path The path to clone.
|
|
30
|
+
* @returns A new instance of Bip32Path.
|
|
31
|
+
*/
|
|
32
|
+
static fromPath(bip32Path) {
|
|
33
|
+
const p = new Bip32Path();
|
|
34
|
+
p._path = bip32Path._path.slice();
|
|
35
|
+
return p;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Converts the path to a string.
|
|
39
|
+
* @returns The path as a string.
|
|
40
|
+
*/
|
|
41
|
+
toString() {
|
|
42
|
+
return this._path.length > 0 ? `m/${this._path.join("/")}` : "m";
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Push a new index on to the path.
|
|
46
|
+
* @param index The index to add to the path.
|
|
47
|
+
*/
|
|
48
|
+
push(index) {
|
|
49
|
+
this._path.push(`${index}`);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Push a new hardened index on to the path.
|
|
53
|
+
* @param index The index to add to the path.
|
|
54
|
+
*/
|
|
55
|
+
pushHardened(index) {
|
|
56
|
+
this._path.push(`${index}'`);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Pop an index from the path.
|
|
60
|
+
*/
|
|
61
|
+
pop() {
|
|
62
|
+
this._path.pop();
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Get the segments.
|
|
66
|
+
* @returns The segments as numbers.
|
|
67
|
+
*/
|
|
68
|
+
numberSegments() {
|
|
69
|
+
return this._path.map(p => Number.parseInt(p, 10));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=bip32Path.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bip32Path.js","sourceRoot":"","sources":["../../../src/keys/bip32Path.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AAEvC;;GAEG;AACH,MAAM,OAAO,SA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C,QAAQ,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;IACpD,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n\n/**\n * Class to help with bip32 paths.\n */\nexport class Bip32Path {\n\t/**\n\t * The path.\n\t * @internal\n\t */\n\tprivate _path: string[];\n\n\t/**\n\t * Create a new instance of Bip32Path.\n\t * @param initialPath Initial path to create.\n\t */\n\tconstructor(initialPath?: string) {\n\t\tif (initialPath) {\n\t\t\tthis._path = initialPath.split(\"/\");\n\n\t\t\tif (this._path[0] === \"m\") {\n\t\t\t\tthis._path.shift();\n\t\t\t}\n\t\t} else {\n\t\t\tthis._path = [];\n\t\t}\n\t}\n\n\t/**\n\t * Construct a new path by cloning an existing one.\n\t * @param bip32Path The path to clone.\n\t * @returns A new instance of Bip32Path.\n\t */\n\tpublic static fromPath(bip32Path: Bip32Path): Bip32Path {\n\t\tconst p = new Bip32Path();\n\t\tp._path = bip32Path._path.slice();\n\t\treturn p;\n\t}\n\n\t/**\n\t * Converts the path to a string.\n\t * @returns The path as a string.\n\t */\n\tpublic toString(): string {\n\t\treturn this._path.length > 0 ? `m/${this._path.join(\"/\")}` : \"m\";\n\t}\n\n\t/**\n\t * Push a new index on to the path.\n\t * @param index The index to add to the path.\n\t */\n\tpublic push(index: number): void {\n\t\tthis._path.push(`${index}`);\n\t}\n\n\t/**\n\t * Push a new hardened index on to the path.\n\t * @param index The index to add to the path.\n\t */\n\tpublic pushHardened(index: number): void {\n\t\tthis._path.push(`${index}'`);\n\t}\n\n\t/**\n\t * Pop an index from the path.\n\t */\n\tpublic pop(): void {\n\t\tthis._path.pop();\n\t}\n\n\t/**\n\t * Get the segments.\n\t * @returns The segments as numbers.\n\t */\n\tpublic numberSegments(): number[] {\n\t\treturn this._path.map(p => Number.parseInt(p, 10));\n\t}\n}\n"]}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
import * as bip39 from "@scure/bip39";
|
|
4
|
+
import { wordlist } from "@scure/bip39/wordlists/english.js";
|
|
5
|
+
import { GuardError, Guards } from "@twin.org/core";
|
|
6
|
+
/**
|
|
7
|
+
* Implementation of Bip39 for mnemonic generation.
|
|
8
|
+
*/
|
|
9
|
+
export class Bip39 {
|
|
10
|
+
/**
|
|
11
|
+
* Runtime name for the class.
|
|
12
|
+
*/
|
|
13
|
+
static CLASS_NAME = "Bip39";
|
|
14
|
+
/**
|
|
15
|
+
* Generate a random mnemonic.
|
|
16
|
+
* @param strength The strength of the mnemonic to generate, defaults to 256.
|
|
17
|
+
* @param words The wordlist to use, defaults to the English wordlist.
|
|
18
|
+
* @returns The random mnemonic.
|
|
19
|
+
* @throws Error if the length is not a multiple of 32.
|
|
20
|
+
*/
|
|
21
|
+
static randomMnemonic(strength = 256, words = wordlist) {
|
|
22
|
+
Guards.number(Bip39.CLASS_NAME, "strength", strength);
|
|
23
|
+
Guards.arrayValue(Bip39.CLASS_NAME, "words", words);
|
|
24
|
+
if (strength % 32 !== 0) {
|
|
25
|
+
throw new GuardError(Bip39.CLASS_NAME, "guard.length32Multiple", "strength", strength);
|
|
26
|
+
}
|
|
27
|
+
return bip39.generateMnemonic(words, strength);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Generate a mnemonic from the entropy.
|
|
31
|
+
* @param entropy The entropy to generate.
|
|
32
|
+
* @param words The wordlist to use, defaults to the English wordlist.
|
|
33
|
+
* @returns The mnemonic.
|
|
34
|
+
* @throws Error if the length of the entropy is not a multiple of 4, or is less than 16 or greater than 32.
|
|
35
|
+
*/
|
|
36
|
+
static entropyToMnemonic(entropy, words = wordlist) {
|
|
37
|
+
Guards.uint8Array(Bip39.CLASS_NAME, "entropy", entropy);
|
|
38
|
+
Guards.arrayValue(Bip39.CLASS_NAME, "words", words);
|
|
39
|
+
if (entropy.length % 4 !== 0 || entropy.length < 16 || entropy.length > 32) {
|
|
40
|
+
throw new GuardError(Bip39.CLASS_NAME, "guard.lengthEntropy", "entropy", entropy.length);
|
|
41
|
+
}
|
|
42
|
+
return bip39.entropyToMnemonic(entropy, words);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Convert a mnemonic to a seed.
|
|
46
|
+
* @param mnemonic The mnemonic to convert.
|
|
47
|
+
* @param password The password to apply to the seed generation.
|
|
48
|
+
* @returns The seed.
|
|
49
|
+
*/
|
|
50
|
+
static mnemonicToSeed(mnemonic, password) {
|
|
51
|
+
Guards.stringValue(Bip39.CLASS_NAME, "mnemonic", mnemonic);
|
|
52
|
+
return bip39.mnemonicToSeedSync(mnemonic, password);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Convert the mnemonic back to entropy.
|
|
56
|
+
* @param mnemonic The mnemonic to convert.
|
|
57
|
+
* @param words The wordlist to use, defaults to the English wordlist.
|
|
58
|
+
* @returns The entropy.
|
|
59
|
+
* @throws Error if the number of words is not a multiple of 3.
|
|
60
|
+
*/
|
|
61
|
+
static mnemonicToEntropy(mnemonic, words = wordlist) {
|
|
62
|
+
Guards.stringValue(Bip39.CLASS_NAME, "mnemonic", mnemonic);
|
|
63
|
+
Guards.arrayValue(Bip39.CLASS_NAME, "words", words);
|
|
64
|
+
return bip39.mnemonicToEntropy(mnemonic, words);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Validate the mnemonic.
|
|
68
|
+
* @param mnemonic The mnemonic to validate.
|
|
69
|
+
* @param wordCount The expected number of words in the mnemonic, defaults to 24.
|
|
70
|
+
* @param words The wordlist to use, defaults to the English wordlist.
|
|
71
|
+
* @returns True if the mnemonic is valid.
|
|
72
|
+
*/
|
|
73
|
+
static validateMnemonic(mnemonic, wordCount = 24, words = wordlist) {
|
|
74
|
+
Guards.string(Bip39.CLASS_NAME, "mnemonic", mnemonic);
|
|
75
|
+
Guards.integer(Bip39.CLASS_NAME, "wordCount", wordCount);
|
|
76
|
+
const mnemonicSplit = mnemonic.split(/\s+/);
|
|
77
|
+
if (mnemonicSplit.length !== wordCount) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
return bip39.validateMnemonic(mnemonic, words);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=bip39.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bip39.js","sourceRoot":"","sources":["../../../src/keys/bip39.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,QAAQ,EAAE,MAAM,mCAAmC,CAAC;AAC7D,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAGpD;;GAEG;AACH,MAAM,OAAO,KAAK;IACjB;;OAEG;IACI,MAAM,CAAU,UAAU,WAA2B;IAE5D;;;;;;OAMG;IACI,MAAM,CAAC,cAAc,CAAC,WAAmB,GAAG,EAAE,QAAkB,QAAQ;QAC9E,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,UAAU,cAAoB,QAAQ,CAAC,CAAC;QAC5D,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,UAAU,WAAiB,KAAK,CAAC,CAAC;QAE1D,IAAI,QAAQ,GAAG,EAAE,KAAK,CAAC,EAAE,CAAC;YACzB,MAAM,IAAI,UAAU,CAAC,KAAK,CAAC,UAAU,EAAE,wBAAwB,cAAoB,QAAQ,CAAC,CAAC;QAC9F,CAAC;QACD,OAAO,KAAK,CAAC,gBAAgB,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;IAChD,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,iBAAiB,CAAC,OAAmB,EAAE,QAAkB,QAAQ;QAC9E,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,UAAU,aAAmB,OAAO,CAAC,CAAC;QAC9D,MAAM,CAAC,UAA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5C,IAAI,aAAa,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;YACxC,OAAO,KAAK,CAAC;QACd,CAAC;QAED,OAAO,KAAK,CAAC,gBAAgB,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;IAChD,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport * as bip39 from \"@scure/bip39\";\nimport { wordlist } from \"@scure/bip39/wordlists/english.js\";\nimport { GuardError, Guards } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\n\n/**\n * Implementation of Bip39 for mnemonic generation.\n */\nexport class Bip39 {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Bip39>();\n\n\t/**\n\t * Generate a random mnemonic.\n\t * @param strength The strength of the mnemonic to generate, defaults to 256.\n\t * @param words The wordlist to use, defaults to the English wordlist.\n\t * @returns The random mnemonic.\n\t * @throws Error if the length is not a multiple of 32.\n\t */\n\tpublic static randomMnemonic(strength: number = 256, words: string[] = wordlist): string {\n\t\tGuards.number(Bip39.CLASS_NAME, nameof(strength), strength);\n\t\tGuards.arrayValue(Bip39.CLASS_NAME, nameof(words), words);\n\n\t\tif (strength % 32 !== 0) {\n\t\t\tthrow new GuardError(Bip39.CLASS_NAME, \"guard.length32Multiple\", nameof(strength), strength);\n\t\t}\n\t\treturn bip39.generateMnemonic(words, strength);\n\t}\n\n\t/**\n\t * Generate a mnemonic from the entropy.\n\t * @param entropy The entropy to generate.\n\t * @param words The wordlist to use, defaults to the English wordlist.\n\t * @returns The mnemonic.\n\t * @throws Error if the length of the entropy is not a multiple of 4, or is less than 16 or greater than 32.\n\t */\n\tpublic static entropyToMnemonic(entropy: Uint8Array, words: string[] = wordlist): string {\n\t\tGuards.uint8Array(Bip39.CLASS_NAME, nameof(entropy), entropy);\n\t\tGuards.arrayValue(Bip39.CLASS_NAME, nameof(words), words);\n\n\t\tif (entropy.length % 4 !== 0 || entropy.length < 16 || entropy.length > 32) {\n\t\t\tthrow new GuardError(\n\t\t\t\tBip39.CLASS_NAME,\n\t\t\t\t\"guard.lengthEntropy\",\n\t\t\t\tnameof(entropy),\n\t\t\t\tentropy.length\n\t\t\t);\n\t\t}\n\n\t\treturn bip39.entropyToMnemonic(entropy, words);\n\t}\n\n\t/**\n\t * Convert a mnemonic to a seed.\n\t * @param mnemonic The mnemonic to convert.\n\t * @param password The password to apply to the seed generation.\n\t * @returns The seed.\n\t */\n\tpublic static mnemonicToSeed(mnemonic: string, password?: string): Uint8Array {\n\t\tGuards.stringValue(Bip39.CLASS_NAME, nameof(mnemonic), mnemonic);\n\n\t\treturn bip39.mnemonicToSeedSync(mnemonic, password);\n\t}\n\n\t/**\n\t * Convert the mnemonic back to entropy.\n\t * @param mnemonic The mnemonic to convert.\n\t * @param words The wordlist to use, defaults to the English wordlist.\n\t * @returns The entropy.\n\t * @throws Error if the number of words is not a multiple of 3.\n\t */\n\tpublic static mnemonicToEntropy(mnemonic: string, words: string[] = wordlist): Uint8Array {\n\t\tGuards.stringValue(Bip39.CLASS_NAME, nameof(mnemonic), mnemonic);\n\t\tGuards.arrayValue(Bip39.CLASS_NAME, nameof(words), words);\n\n\t\treturn bip39.mnemonicToEntropy(mnemonic, words);\n\t}\n\n\t/**\n\t * Validate the mnemonic.\n\t * @param mnemonic The mnemonic to validate.\n\t * @param wordCount The expected number of words in the mnemonic, defaults to 24.\n\t * @param words The wordlist to use, defaults to the English wordlist.\n\t * @returns True if the mnemonic is valid.\n\t */\n\tpublic static validateMnemonic(\n\t\tmnemonic: string,\n\t\twordCount: number = 24,\n\t\twords: string[] = wordlist\n\t): boolean {\n\t\tGuards.string(Bip39.CLASS_NAME, nameof(mnemonic), mnemonic);\n\t\tGuards.integer(Bip39.CLASS_NAME, nameof(wordCount), wordCount);\n\n\t\tconst mnemonicSplit = mnemonic.split(/\\s+/);\n\t\tif (mnemonicSplit.length !== wordCount) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn bip39.validateMnemonic(mnemonic, words);\n\t}\n}\n"]}
|