@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,136 @@
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
+ // Japanese wordlist
11
+ const isJapanese = (wordlist) => wordlist[0] === '\u3042\u3044\u3053\u304f\u3057\u3093';
12
+ // Normalization replaces equivalent sequences of characters
13
+ // so that any two texts that are equivalent will be reduced
14
+ // to the same sequence of code points, called the normal form of the original text.
15
+ function nfkd(str) {
16
+ if (typeof str !== 'string')
17
+ throw new TypeError(`Invalid mnemonic type: ${typeof str}`);
18
+ return str.normalize('NFKD');
19
+ }
20
+ function normalize(str) {
21
+ const norm = nfkd(str);
22
+ const words = norm.split(' ');
23
+ if (![12, 15, 18, 21, 24].includes(words.length))
24
+ throw new Error('Invalid mnemonic');
25
+ return { nfkd: norm, words };
26
+ }
27
+ function assertEntropy(entropy) {
28
+ assert.bytes(entropy, 16, 20, 24, 28, 32);
29
+ }
30
+ /**
31
+ * Generate x random words. Uses Cryptographically-Secure Random Number Generator.
32
+ * @param wordlist imported wordlist for specific language
33
+ * @param strength mnemonic strength 128-256 bits
34
+ * @example
35
+ * generateMnemonic(wordlist, 128)
36
+ * // 'legal winner thank year wave sausage worth useful legal winner thank yellow'
37
+ */
38
+ export function generateMnemonic(wordlist, strength = 128) {
39
+ assert.number(strength);
40
+ if (strength % 32 !== 0 || strength > 256)
41
+ throw new TypeError('Invalid entropy');
42
+ return entropyToMnemonic(randomBytes(strength / 8), wordlist);
43
+ }
44
+ const calcChecksum = (entropy) => {
45
+ // Checksum is ent.length/4 bits long
46
+ const bitsLeft = 8 - entropy.length / 4;
47
+ // Zero rightmost "bitsLeft" bits in byte
48
+ // For example: bitsLeft=4 val=10111101 -> 10110000
49
+ return new Uint8Array([(sha256(entropy)[0] >> bitsLeft) << bitsLeft]);
50
+ };
51
+ function getCoder(wordlist) {
52
+ if (!Array.isArray(wordlist) || wordlist.length !== 2048 || typeof wordlist[0] !== 'string')
53
+ throw new Error('Worlist: expected array of 2048 strings');
54
+ wordlist.forEach((i) => {
55
+ if (typeof i !== 'string')
56
+ throw new Error(`Wordlist: non-string element: ${i}`);
57
+ });
58
+ return baseUtils.chain(baseUtils.checksum(1, calcChecksum), baseUtils.radix2(11, true), baseUtils.alphabet(wordlist));
59
+ }
60
+ /**
61
+ * Reversible: Converts mnemonic string to raw entropy in form of byte array.
62
+ * @param mnemonic 12-24 words
63
+ * @param wordlist imported wordlist for specific language
64
+ * @example
65
+ * const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
66
+ * mnemonicToEntropy(mnem, wordlist)
67
+ * // Produces
68
+ * new Uint8Array([
69
+ * 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
70
+ * 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f
71
+ * ])
72
+ */
73
+ export function mnemonicToEntropy(mnemonic, wordlist) {
74
+ const { words } = normalize(mnemonic);
75
+ const entropy = getCoder(wordlist).decode(words);
76
+ assertEntropy(entropy);
77
+ return entropy;
78
+ }
79
+ /**
80
+ * Reversible: Converts raw entropy in form of byte array to mnemonic string.
81
+ * @param entropy byte array
82
+ * @param wordlist imported wordlist for specific language
83
+ * @returns 12-24 words
84
+ * @example
85
+ * const ent = new Uint8Array([
86
+ * 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
87
+ * 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f
88
+ * ]);
89
+ * entropyToMnemonic(ent, wordlist);
90
+ * // 'legal winner thank year wave sausage worth useful legal winner thank yellow'
91
+ */
92
+ export function entropyToMnemonic(entropy, wordlist) {
93
+ assertEntropy(entropy);
94
+ const words = getCoder(wordlist).encode(entropy);
95
+ return words.join(isJapanese(wordlist) ? '\u3000' : ' ');
96
+ }
97
+ /**
98
+ * Validates mnemonic for being 12-24 words contained in `wordlist`.
99
+ */
100
+ export function validateMnemonic(mnemonic, wordlist) {
101
+ try {
102
+ mnemonicToEntropy(mnemonic, wordlist);
103
+ }
104
+ catch (e) {
105
+ return false;
106
+ }
107
+ return true;
108
+ }
109
+ const salt = (passphrase) => nfkd(`mnemonic${passphrase}`);
110
+ /**
111
+ * Irreversible: Uses KDF to derive 64 bytes of key data from mnemonic + optional password.
112
+ * @param mnemonic 12-24 words
113
+ * @param passphrase string that will additionally protect the key
114
+ * @returns 64 bytes of key data
115
+ * @example
116
+ * const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
117
+ * await mnemonicToSeed(mnem, 'password');
118
+ * // new Uint8Array([...64 bytes])
119
+ */
120
+ export function mnemonicToSeed(mnemonic, passphrase = '') {
121
+ return pbkdf2Async(sha512, normalize(mnemonic).nfkd, salt(passphrase), { c: 2048, dkLen: 64 });
122
+ }
123
+ /**
124
+ * Irreversible: Uses KDF to derive 64 bytes of key data from mnemonic + optional password.
125
+ * @param mnemonic 12-24 words
126
+ * @param passphrase string that will additionally protect the key
127
+ * @returns 64 bytes of key data
128
+ * @example
129
+ * const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
130
+ * mnemonicToSeedSync(mnem, 'password');
131
+ * // new Uint8Array([...64 bytes])
132
+ */
133
+ export function mnemonicToSeedSync(mnemonic, passphrase = '') {
134
+ return pbkdf2(sha512, normalize(mnemonic).nfkd, salt(passphrase), { c: 2048, dkLen: 64 });
135
+ }
136
+ //#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