@xyo-network/bip39 2.63.7

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 (75) hide show
  1. package/LICENSE +165 -0
  2. package/README.md +13 -0
  3. package/dist/cjs/index.js +146 -0
  4. package/dist/cjs/index.js.map +1 -0
  5. package/dist/cjs/wordlists/czech.js +2053 -0
  6. package/dist/cjs/wordlists/czech.js.map +1 -0
  7. package/dist/cjs/wordlists/english.js +2053 -0
  8. package/dist/cjs/wordlists/english.js.map +1 -0
  9. package/dist/cjs/wordlists/french.js +2053 -0
  10. package/dist/cjs/wordlists/french.js.map +1 -0
  11. package/dist/cjs/wordlists/italian.js +2053 -0
  12. package/dist/cjs/wordlists/italian.js.map +1 -0
  13. package/dist/cjs/wordlists/japanese.js +2053 -0
  14. package/dist/cjs/wordlists/japanese.js.map +1 -0
  15. package/dist/cjs/wordlists/korean.js +2053 -0
  16. package/dist/cjs/wordlists/korean.js.map +1 -0
  17. package/dist/cjs/wordlists/simplified-chinese.js +2053 -0
  18. package/dist/cjs/wordlists/simplified-chinese.js.map +1 -0
  19. package/dist/cjs/wordlists/spanish.js +2053 -0
  20. package/dist/cjs/wordlists/spanish.js.map +1 -0
  21. package/dist/cjs/wordlists/traditional-chinese.js +2053 -0
  22. package/dist/cjs/wordlists/traditional-chinese.js.map +1 -0
  23. package/dist/docs.json +755 -0
  24. package/dist/esm/index.js +136 -0
  25. package/dist/esm/index.js.map +1 -0
  26. package/dist/esm/wordlists/czech.js +2050 -0
  27. package/dist/esm/wordlists/czech.js.map +1 -0
  28. package/dist/esm/wordlists/english.js +2050 -0
  29. package/dist/esm/wordlists/english.js.map +1 -0
  30. package/dist/esm/wordlists/french.js +2050 -0
  31. package/dist/esm/wordlists/french.js.map +1 -0
  32. package/dist/esm/wordlists/italian.js +2050 -0
  33. package/dist/esm/wordlists/italian.js.map +1 -0
  34. package/dist/esm/wordlists/japanese.js +2050 -0
  35. package/dist/esm/wordlists/japanese.js.map +1 -0
  36. package/dist/esm/wordlists/korean.js +2050 -0
  37. package/dist/esm/wordlists/korean.js.map +1 -0
  38. package/dist/esm/wordlists/simplified-chinese.js +2050 -0
  39. package/dist/esm/wordlists/simplified-chinese.js.map +1 -0
  40. package/dist/esm/wordlists/spanish.js +2050 -0
  41. package/dist/esm/wordlists/spanish.js.map +1 -0
  42. package/dist/esm/wordlists/traditional-chinese.js +2050 -0
  43. package/dist/esm/wordlists/traditional-chinese.js.map +1 -0
  44. package/dist/types/index.d.ts +64 -0
  45. package/dist/types/index.d.ts.map +1 -0
  46. package/dist/types/wordlists/czech.d.ts +2 -0
  47. package/dist/types/wordlists/czech.d.ts.map +1 -0
  48. package/dist/types/wordlists/english.d.ts +2 -0
  49. package/dist/types/wordlists/english.d.ts.map +1 -0
  50. package/dist/types/wordlists/french.d.ts +2 -0
  51. package/dist/types/wordlists/french.d.ts.map +1 -0
  52. package/dist/types/wordlists/italian.d.ts +2 -0
  53. package/dist/types/wordlists/italian.d.ts.map +1 -0
  54. package/dist/types/wordlists/japanese.d.ts +2 -0
  55. package/dist/types/wordlists/japanese.d.ts.map +1 -0
  56. package/dist/types/wordlists/korean.d.ts +2 -0
  57. package/dist/types/wordlists/korean.d.ts.map +1 -0
  58. package/dist/types/wordlists/simplified-chinese.d.ts +2 -0
  59. package/dist/types/wordlists/simplified-chinese.d.ts.map +1 -0
  60. package/dist/types/wordlists/spanish.d.ts +2 -0
  61. package/dist/types/wordlists/spanish.d.ts.map +1 -0
  62. package/dist/types/wordlists/traditional-chinese.d.ts +2 -0
  63. package/dist/types/wordlists/traditional-chinese.d.ts.map +1 -0
  64. package/package.json +55 -0
  65. package/src/index.ts +143 -0
  66. package/src/wordlists/czech.ts +2049 -0
  67. package/src/wordlists/english.ts +2049 -0
  68. package/src/wordlists/french.ts +2049 -0
  69. package/src/wordlists/italian.ts +2049 -0
  70. package/src/wordlists/japanese.ts +2049 -0
  71. package/src/wordlists/korean.ts +2049 -0
  72. package/src/wordlists/simplified-chinese.ts +2049 -0
  73. package/src/wordlists/spanish.ts +2049 -0
  74. package/src/wordlists/traditional-chinese.ts +2049 -0
  75. package/typedoc.json +5 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"traditional-chinese.js","sourceRoot":"","sources":["../../../src/wordlists/traditional-chinese.ts"],"names":[],"mappings":"AAAA,8BAA8B;AAC9B,MAAM,CAAC,MAAM,QAAQ,GAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA+/DhC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA"}
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Generate x random words. Uses Cryptographically-Secure Random Number Generator.
3
+ * @param wordlist imported wordlist for specific language
4
+ * @param strength mnemonic strength 128-256 bits
5
+ * @example
6
+ * generateMnemonic(wordlist, 128)
7
+ * // 'legal winner thank year wave sausage worth useful legal winner thank yellow'
8
+ */
9
+ export declare function generateMnemonic(wordlist: string[], strength?: number): string;
10
+ /**
11
+ * Reversible: Converts mnemonic string to raw entropy in form of byte array.
12
+ * @param mnemonic 12-24 words
13
+ * @param wordlist imported wordlist for specific language
14
+ * @example
15
+ * const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
16
+ * mnemonicToEntropy(mnem, wordlist)
17
+ * // Produces
18
+ * new Uint8Array([
19
+ * 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
20
+ * 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f
21
+ * ])
22
+ */
23
+ export declare function mnemonicToEntropy(mnemonic: string, wordlist: string[]): Uint8Array;
24
+ /**
25
+ * Reversible: Converts raw entropy in form of byte array to mnemonic string.
26
+ * @param entropy byte array
27
+ * @param wordlist imported wordlist for specific language
28
+ * @returns 12-24 words
29
+ * @example
30
+ * const ent = new Uint8Array([
31
+ * 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
32
+ * 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f
33
+ * ]);
34
+ * entropyToMnemonic(ent, wordlist);
35
+ * // 'legal winner thank year wave sausage worth useful legal winner thank yellow'
36
+ */
37
+ export declare function entropyToMnemonic(entropy: Uint8Array, wordlist: string[]): string;
38
+ /**
39
+ * Validates mnemonic for being 12-24 words contained in `wordlist`.
40
+ */
41
+ export declare function validateMnemonic(mnemonic: string, wordlist: string[]): boolean;
42
+ /**
43
+ * Irreversible: Uses KDF to derive 64 bytes of key data from mnemonic + optional password.
44
+ * @param mnemonic 12-24 words
45
+ * @param passphrase string that will additionally protect the key
46
+ * @returns 64 bytes of key data
47
+ * @example
48
+ * const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
49
+ * await mnemonicToSeed(mnem, 'password');
50
+ * // new Uint8Array([...64 bytes])
51
+ */
52
+ export declare function mnemonicToSeed(mnemonic: string, passphrase?: string): Promise<Uint8Array>;
53
+ /**
54
+ * Irreversible: Uses KDF to derive 64 bytes of key data from mnemonic + optional password.
55
+ * @param mnemonic 12-24 words
56
+ * @param passphrase string that will additionally protect the key
57
+ * @returns 64 bytes of key data
58
+ * @example
59
+ * const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
60
+ * mnemonicToSeedSync(mnem, 'password');
61
+ * // new Uint8Array([...64 bytes])
62
+ */
63
+ export declare function mnemonicToSeedSync(mnemonic: string, passphrase?: string): Uint8Array;
64
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAgCA;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,EAAE,EAAE,QAAQ,SAAM,GAAG,MAAM,CAI3E;AAmBD;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,UAAU,CAKlF;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,MAAM,CAIjF;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,OAAO,CAO9E;AAID;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,SAAK,uBAE/D;AAED;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,SAAK,cAEnE"}
@@ -0,0 +1,2 @@
1
+ export declare const wordlist: string[];
2
+ //# sourceMappingURL=czech.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"czech.d.ts","sourceRoot":"","sources":["../../../src/wordlists/czech.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,QAAQ,EAAE,MAAM,EA+/DZ,CAAA"}
@@ -0,0 +1,2 @@
1
+ export declare const wordlist: string[];
2
+ //# sourceMappingURL=english.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"english.d.ts","sourceRoot":"","sources":["../../../src/wordlists/english.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,QAAQ,EAAE,MAAM,EA+/Db,CAAA"}
@@ -0,0 +1,2 @@
1
+ export declare const wordlist: string[];
2
+ //# sourceMappingURL=french.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"french.d.ts","sourceRoot":"","sources":["../../../src/wordlists/french.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,QAAQ,EAAE,MAAM,EA+/DR,CAAA"}
@@ -0,0 +1,2 @@
1
+ export declare const wordlist: string[];
2
+ //# sourceMappingURL=italian.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"italian.d.ts","sourceRoot":"","sources":["../../../src/wordlists/italian.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,QAAQ,EAAE,MAAM,EA+/DX,CAAA"}
@@ -0,0 +1,2 @@
1
+ export declare const wordlist: string[];
2
+ //# sourceMappingURL=japanese.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"japanese.d.ts","sourceRoot":"","sources":["../../../src/wordlists/japanese.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,QAAQ,EAAE,MAAM,EA+/Db,CAAA"}
@@ -0,0 +1,2 @@
1
+ export declare const wordlist: string[];
2
+ //# sourceMappingURL=korean.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"korean.d.ts","sourceRoot":"","sources":["../../../src/wordlists/korean.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,QAAQ,EAAE,MAAM,EA+/DV,CAAA"}
@@ -0,0 +1,2 @@
1
+ export declare const wordlist: string[];
2
+ //# sourceMappingURL=simplified-chinese.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"simplified-chinese.d.ts","sourceRoot":"","sources":["../../../src/wordlists/simplified-chinese.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,QAAQ,EAAE,MAAM,EA+/Df,CAAA"}
@@ -0,0 +1,2 @@
1
+ export declare const wordlist: string[];
2
+ //# sourceMappingURL=spanish.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"spanish.d.ts","sourceRoot":"","sources":["../../../src/wordlists/spanish.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,QAAQ,EAAE,MAAM,EA+/DX,CAAA"}
@@ -0,0 +1,2 @@
1
+ export declare const wordlist: string[];
2
+ //# sourceMappingURL=traditional-chinese.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"traditional-chinese.d.ts","sourceRoot":"","sources":["../../../src/wordlists/traditional-chinese.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,QAAQ,EAAE,MAAM,EA+/Df,CAAA"}
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@xyo-network/bip39",
3
+ "author": {
4
+ "email": "support@xyo.network",
5
+ "name": "XYO Development Team",
6
+ "url": "https://xyo.network"
7
+ },
8
+ "bugs": {
9
+ "email": "support@xyo.network",
10
+ "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js/issues"
11
+ },
12
+ "dependencies": {
13
+ "@noble/hashes": "~1.3.0",
14
+ "@scure/base": "~1.1.0"
15
+ },
16
+ "description": "Primary SDK for using XYO Protocol 2.0",
17
+ "devDependencies": {
18
+ "@xylabs/ts-scripts-yarn3": "^2.17.17",
19
+ "@xylabs/tsconfig": "^2.17.17",
20
+ "typescript": "^5.1.3"
21
+ },
22
+ "browser": "dist/esm/index.js",
23
+ "main": "dist/cjs/index.js",
24
+ "module": "dist/esm/index.js",
25
+ "docs": "dist/docs.json",
26
+ "exports": {
27
+ ".": {
28
+ "node": {
29
+ "import": "./dist/esm/index.js",
30
+ "require": "./dist/cjs/index.js"
31
+ },
32
+ "browser": {
33
+ "import": "./dist/esm/index.js",
34
+ "require": "./dist/cjs/index.js"
35
+ },
36
+ "default": "./dist/esm/index.js"
37
+ },
38
+ "./dist/docs.json": {
39
+ "default": "./dist/docs.json"
40
+ },
41
+ "./package.json": "./package.json"
42
+ },
43
+ "homepage": "https://xyo.network",
44
+ "license": "LGPL-3.0",
45
+ "publishConfig": {
46
+ "access": "public"
47
+ },
48
+ "repository": {
49
+ "type": "git",
50
+ "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js.git"
51
+ },
52
+ "sideEffects": false,
53
+ "types": "dist/types/index.d.ts",
54
+ "version": "2.63.7"
55
+ }
package/src/index.ts ADDED
@@ -0,0 +1,143 @@
1
+ /* eslint-disable import/no-named-as-default-member */
2
+ /* eslint-disable import/no-internal-modules */
3
+ /*! scure-bip39 - MIT License (c) 2022 Patricio Palladino, Paul Miller (paulmillr.com) */
4
+ import assert from '@noble/hashes/_assert'
5
+ import { pbkdf2, pbkdf2Async } from '@noble/hashes/pbkdf2'
6
+ import { sha256 } from '@noble/hashes/sha256'
7
+ import { sha512 } from '@noble/hashes/sha512'
8
+ import { randomBytes } from '@noble/hashes/utils'
9
+ import { utils as baseUtils } from '@scure/base'
10
+
11
+ // Japanese wordlist
12
+ const isJapanese = (wordlist: string[]) => wordlist[0] === '\u3042\u3044\u3053\u304f\u3057\u3093'
13
+
14
+ // Normalization replaces equivalent sequences of characters
15
+ // so that any two texts that are equivalent will be reduced
16
+ // to the same sequence of code points, called the normal form of the original text.
17
+ function nfkd(str: string) {
18
+ if (typeof str !== 'string') throw new TypeError(`Invalid mnemonic type: ${typeof str}`)
19
+ return str.normalize('NFKD')
20
+ }
21
+
22
+ function normalize(str: string) {
23
+ const norm = nfkd(str)
24
+ const words = norm.split(' ')
25
+ if (![12, 15, 18, 21, 24].includes(words.length)) throw new Error('Invalid mnemonic')
26
+ return { nfkd: norm, words }
27
+ }
28
+
29
+ function assertEntropy(entropy: Uint8Array) {
30
+ assert.bytes(entropy, 16, 20, 24, 28, 32)
31
+ }
32
+
33
+ /**
34
+ * Generate x random words. Uses Cryptographically-Secure Random Number Generator.
35
+ * @param wordlist imported wordlist for specific language
36
+ * @param strength mnemonic strength 128-256 bits
37
+ * @example
38
+ * generateMnemonic(wordlist, 128)
39
+ * // 'legal winner thank year wave sausage worth useful legal winner thank yellow'
40
+ */
41
+ export function generateMnemonic(wordlist: string[], strength = 128): string {
42
+ assert.number(strength)
43
+ if (strength % 32 !== 0 || strength > 256) throw new TypeError('Invalid entropy')
44
+ return entropyToMnemonic(randomBytes(strength / 8), wordlist)
45
+ }
46
+
47
+ const calcChecksum = (entropy: Uint8Array) => {
48
+ // Checksum is ent.length/4 bits long
49
+ const bitsLeft = 8 - entropy.length / 4
50
+ // Zero rightmost "bitsLeft" bits in byte
51
+ // For example: bitsLeft=4 val=10111101 -> 10110000
52
+ return new Uint8Array([(sha256(entropy)[0] >> bitsLeft) << bitsLeft])
53
+ }
54
+
55
+ function getCoder(wordlist: string[]) {
56
+ if (!Array.isArray(wordlist) || wordlist.length !== 2048 || typeof wordlist[0] !== 'string')
57
+ throw new Error('Worlist: expected array of 2048 strings')
58
+ wordlist.forEach((i) => {
59
+ if (typeof i !== 'string') throw new Error(`Wordlist: non-string element: ${i}`)
60
+ })
61
+ return baseUtils.chain(baseUtils.checksum(1, calcChecksum), baseUtils.radix2(11, true), baseUtils.alphabet(wordlist))
62
+ }
63
+
64
+ /**
65
+ * Reversible: Converts mnemonic string to raw entropy in form of byte array.
66
+ * @param mnemonic 12-24 words
67
+ * @param wordlist imported wordlist for specific language
68
+ * @example
69
+ * const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
70
+ * mnemonicToEntropy(mnem, wordlist)
71
+ * // Produces
72
+ * new Uint8Array([
73
+ * 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
74
+ * 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f
75
+ * ])
76
+ */
77
+ export function mnemonicToEntropy(mnemonic: string, wordlist: string[]): Uint8Array {
78
+ const { words } = normalize(mnemonic)
79
+ const entropy = getCoder(wordlist).decode(words)
80
+ assertEntropy(entropy)
81
+ return entropy
82
+ }
83
+
84
+ /**
85
+ * Reversible: Converts raw entropy in form of byte array to mnemonic string.
86
+ * @param entropy byte array
87
+ * @param wordlist imported wordlist for specific language
88
+ * @returns 12-24 words
89
+ * @example
90
+ * const ent = new Uint8Array([
91
+ * 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
92
+ * 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f
93
+ * ]);
94
+ * entropyToMnemonic(ent, wordlist);
95
+ * // 'legal winner thank year wave sausage worth useful legal winner thank yellow'
96
+ */
97
+ export function entropyToMnemonic(entropy: Uint8Array, wordlist: string[]): string {
98
+ assertEntropy(entropy)
99
+ const words = getCoder(wordlist).encode(entropy)
100
+ return words.join(isJapanese(wordlist) ? '\u3000' : ' ')
101
+ }
102
+
103
+ /**
104
+ * Validates mnemonic for being 12-24 words contained in `wordlist`.
105
+ */
106
+ export function validateMnemonic(mnemonic: string, wordlist: string[]): boolean {
107
+ try {
108
+ mnemonicToEntropy(mnemonic, wordlist)
109
+ } catch (e) {
110
+ return false
111
+ }
112
+ return true
113
+ }
114
+
115
+ const salt = (passphrase: string) => nfkd(`mnemonic${passphrase}`)
116
+
117
+ /**
118
+ * Irreversible: Uses KDF to derive 64 bytes of key data from mnemonic + optional password.
119
+ * @param mnemonic 12-24 words
120
+ * @param passphrase string that will additionally protect the key
121
+ * @returns 64 bytes of key data
122
+ * @example
123
+ * const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
124
+ * await mnemonicToSeed(mnem, 'password');
125
+ * // new Uint8Array([...64 bytes])
126
+ */
127
+ export function mnemonicToSeed(mnemonic: string, passphrase = '') {
128
+ return pbkdf2Async(sha512, normalize(mnemonic).nfkd, salt(passphrase), { c: 2048, dkLen: 64 })
129
+ }
130
+
131
+ /**
132
+ * Irreversible: Uses KDF to derive 64 bytes of key data from mnemonic + optional password.
133
+ * @param mnemonic 12-24 words
134
+ * @param passphrase string that will additionally protect the key
135
+ * @returns 64 bytes of key data
136
+ * @example
137
+ * const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
138
+ * mnemonicToSeedSync(mnem, 'password');
139
+ * // new Uint8Array([...64 bytes])
140
+ */
141
+ export function mnemonicToSeedSync(mnemonic: string, passphrase = '') {
142
+ return pbkdf2(sha512, normalize(mnemonic).nfkd, salt(passphrase), { c: 2048, dkLen: 64 })
143
+ }