@decryption/bip39 0.2.0
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/LICENSE +21 -0
- package/README.md +75 -0
- package/esm/index.js +223 -0
- package/esm/wordlists/czech.js +2049 -0
- package/esm/wordlists/english.js +2049 -0
- package/esm/wordlists/french.js +2049 -0
- package/esm/wordlists/italian.js +2049 -0
- package/esm/wordlists/japanese.js +2049 -0
- package/esm/wordlists/korean.js +2049 -0
- package/esm/wordlists/portuguese.js +2049 -0
- package/esm/wordlists/simplified-chinese.js +2049 -0
- package/esm/wordlists/spanish.js +2049 -0
- package/esm/wordlists/traditional-chinese.js +2049 -0
- package/index.d.ts +129 -0
- package/index.js +232 -0
- package/package.json +46 -0
- package/wordlists/czech.d.ts +2 -0
- package/wordlists/czech.js +2052 -0
- package/wordlists/english.d.ts +2 -0
- package/wordlists/english.js +2052 -0
- package/wordlists/french.d.ts +2 -0
- package/wordlists/french.js +2052 -0
- package/wordlists/italian.d.ts +2 -0
- package/wordlists/italian.js +2052 -0
- package/wordlists/japanese.d.ts +2 -0
- package/wordlists/japanese.js +2052 -0
- package/wordlists/korean.d.ts +2 -0
- package/wordlists/korean.js +2052 -0
- package/wordlists/portuguese.d.ts +2 -0
- package/wordlists/portuguese.js +2052 -0
- package/wordlists/simplified-chinese.d.ts +2 -0
- package/wordlists/simplified-chinese.js +2052 -0
- package/wordlists/spanish.d.ts +2 -0
- package/wordlists/spanish.js +2052 -0
- package/wordlists/traditional-chinese.d.ts +2 -0
- package/wordlists/traditional-chinese.js +2052 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Patricio Palladino, Paul Miller (paulmillr.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the “Software”), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# @decryption/bip39
|
|
2
|
+
|
|
3
|
+
> **NOTE:** This is a fork of [`@scure/bip39`](https://www.npmjs.com/package/@scure/bip39) (v2.2.0).
|
|
4
|
+
> We dual-publish as both **CJS and ESM** because upstream is ESM-only (`"type": "module"`), which breaks
|
|
5
|
+
> `require()` in Jest, older bundlers and CJS-based Node.js tooling. The source is unchanged from upstream —
|
|
6
|
+
> only the build tooling differs, plus `@noble/*` / `@scure/*` import specifiers are rewritten to their
|
|
7
|
+
> `@decryption/*` forks.
|
|
8
|
+
|
|
9
|
+
Audited & minimal implementation of BIP39 mnemonic phrases. Dual CJS+ESM fork of @scure/bip39.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @decryption/bip39
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
Import submodules directly — `dist/` is the package root once published, so deep imports work in both CJS and ESM:
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { sha256 } from '@decryption/hashes/sha2';
|
|
23
|
+
import { xchacha20poly1305 } from '@decryption/ciphers/chacha';
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Refer to the [upstream documentation](https://github.com/paulmillr/bip39) for the full API.
|
|
27
|
+
|
|
28
|
+
## Syncing upstream
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
./scripts/vendor-fork.sh @scure/bip39 <version> bip39
|
|
32
|
+
node scripts/fix-imports.js packages/bip39 bip39
|
|
33
|
+
pnpm --filter @decryption/bip39 build && pnpm --filter @decryption/bip39 test
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## License
|
|
37
|
+
|
|
38
|
+
MIT — Copyright (c) 2022 Paul Miller (https://paulmillr.com). See [LICENSE](./LICENSE).
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## Development
|
|
43
|
+
|
|
44
|
+
### Setup
|
|
45
|
+
|
|
46
|
+
1. Clone the repository:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
git clone https://github.com/constructive-io/decryption.git
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
2. Install dependencies:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
cd decryption
|
|
56
|
+
pnpm install
|
|
57
|
+
pnpm build
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
3. Test the package of interest:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
cd packages/<packagename>
|
|
64
|
+
pnpm test:watch
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Credits
|
|
68
|
+
|
|
69
|
+
**🛠 Built by the [Constructive](https://constructive.io) team — creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on [GitHub](https://github.com/constructive-io).**
|
|
70
|
+
|
|
71
|
+
## Disclaimer
|
|
72
|
+
|
|
73
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
74
|
+
|
|
75
|
+
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/*! scure-bip39 - MIT License (c) 2022 Patricio Palladino, Paul Miller (paulmillr.com) */
|
|
2
|
+
import { pbkdf2, pbkdf2Async } from '@decryption/hashes/pbkdf2';
|
|
3
|
+
import { sha256, sha512 } from '@decryption/hashes/sha2';
|
|
4
|
+
import { abytes, anumber, randomBytes } from '@decryption/hashes/utils';
|
|
5
|
+
import { pbkdf2 as pbkdf2web, sha512 as sha512web } from '@decryption/hashes/webcrypto';
|
|
6
|
+
import { utils as baseUtils } from '@decryption/base';
|
|
7
|
+
// Japanese wordlist
|
|
8
|
+
// The canonical BIP-39 Japanese wordlist starts with あいこくしん.
|
|
9
|
+
// Use that sentinel so generated phrases use U+3000 ideographic spaces.
|
|
10
|
+
const isJapanese = (wordlist) => wordlist[0] === '\u3042\u3044\u3053\u304f\u3057\u3093';
|
|
11
|
+
// Normalization replaces equivalent sequences of characters
|
|
12
|
+
// so that any two texts that are equivalent will be reduced
|
|
13
|
+
// to the same sequence of code points, called the normal form of the original text.
|
|
14
|
+
// https://tonsky.me/blog/unicode/#why-is-a----
|
|
15
|
+
// BIP-39 requires UTF-8 NFKD for localized wordlists and mnemonic sentences.
|
|
16
|
+
// It also applies NFKD to the "mnemonic" + passphrase salt.
|
|
17
|
+
function nfkd(str) {
|
|
18
|
+
if (typeof str !== 'string')
|
|
19
|
+
throw new TypeError('invalid mnemonic type: ' + typeof str);
|
|
20
|
+
return str.normalize('NFKD');
|
|
21
|
+
}
|
|
22
|
+
// BIP-39 mnemonics are consumed in NFKD form.
|
|
23
|
+
// They must contain 12, 15, 18, 21, or 24 words before checksum validation.
|
|
24
|
+
function normalize(str) {
|
|
25
|
+
const norm = nfkd(str);
|
|
26
|
+
const words = norm.split(' ');
|
|
27
|
+
if (![12, 15, 18, 21, 24].includes(words.length))
|
|
28
|
+
throw new Error('Invalid mnemonic');
|
|
29
|
+
return { nfkd: norm, words };
|
|
30
|
+
}
|
|
31
|
+
// BIP-39 entropy payloads are 128-256 bits in 32-bit increments, i.e. 16/20/24/28/32 bytes.
|
|
32
|
+
function aentropy(ent) {
|
|
33
|
+
abytes(ent);
|
|
34
|
+
if (![16, 20, 24, 28, 32].includes(ent.length))
|
|
35
|
+
throw new RangeError('invalid entropy length');
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Generate x random words. Uses Cryptographically-Secure Random Number Generator.
|
|
39
|
+
* @param wordlist - Imported wordlist for a specific language.
|
|
40
|
+
* @param strength - Mnemonic strength, from 128 to 256 bits.
|
|
41
|
+
* @returns 12-24 word mnemonic phrase.
|
|
42
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
43
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
44
|
+
* @example
|
|
45
|
+
* Generate a new English mnemonic.
|
|
46
|
+
* ```ts
|
|
47
|
+
* import { generateMnemonic } from '@decryption/bip39';
|
|
48
|
+
* import { wordlist } from '@decryption/bip39/wordlists/english.js';
|
|
49
|
+
* const mnemonic = generateMnemonic(wordlist, 128);
|
|
50
|
+
* // 'legal winner thank year wave sausage worth useful legal winner thank yellow'
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export function generateMnemonic(wordlist, strength = 128) {
|
|
54
|
+
anumber(strength);
|
|
55
|
+
if (strength % 32 !== 0 || strength > 256)
|
|
56
|
+
throw new RangeError('Invalid entropy');
|
|
57
|
+
return entropyToMnemonic(randomBytes(strength / 8), wordlist);
|
|
58
|
+
}
|
|
59
|
+
const calcChecksum = (entropy) => {
|
|
60
|
+
// Checksum is ent.length/4 bits long
|
|
61
|
+
const bitsLeft = 8 - entropy.length / 4;
|
|
62
|
+
// Zero rightmost "bitsLeft" bits in byte
|
|
63
|
+
// For example: bitsLeft=4 val=10111101 -> 10110000
|
|
64
|
+
return new Uint8Array([(sha256(entropy)[0] >> bitsLeft) << bitsLeft]);
|
|
65
|
+
};
|
|
66
|
+
function getCoder(wordlist) {
|
|
67
|
+
if (!Array.isArray(wordlist) || wordlist.length !== 2048 || typeof wordlist[0] !== 'string')
|
|
68
|
+
throw new TypeError('Wordlist: expected array of 2048 strings');
|
|
69
|
+
wordlist.forEach((i) => {
|
|
70
|
+
if (typeof i !== 'string')
|
|
71
|
+
throw new TypeError('wordlist: non-string element: ' + i);
|
|
72
|
+
});
|
|
73
|
+
// BIP-39 appends checksum bits to entropy.
|
|
74
|
+
// It then splits the bitstream into 11-bit indexes for a 2048-word list.
|
|
75
|
+
return baseUtils.chain(baseUtils.checksum(1, calcChecksum), baseUtils.radix2(11, true), baseUtils.alphabet(wordlist));
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Reversible: Converts mnemonic string to raw entropy in form of byte array.
|
|
79
|
+
* @param mnemonic - 12-24 words.
|
|
80
|
+
* @param wordlist - Imported wordlist for a specific language.
|
|
81
|
+
* @returns Raw entropy bytes.
|
|
82
|
+
* @throws If the mnemonic shape or checksum is invalid. {@link Error}
|
|
83
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
84
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
85
|
+
* @example
|
|
86
|
+
* Decode a mnemonic back into its original entropy bytes.
|
|
87
|
+
* ```ts
|
|
88
|
+
* import { mnemonicToEntropy } from '@decryption/bip39';
|
|
89
|
+
* import { wordlist } from '@decryption/bip39/wordlists/english.js';
|
|
90
|
+
* const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
|
|
91
|
+
* const entropy = mnemonicToEntropy(mnem, wordlist);
|
|
92
|
+
* // Produces the original 16-byte entropy payload.
|
|
93
|
+
* new Uint8Array([
|
|
94
|
+
* 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
|
|
95
|
+
* 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f
|
|
96
|
+
* ])
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export function mnemonicToEntropy(mnemonic, wordlist) {
|
|
100
|
+
const { words } = normalize(mnemonic);
|
|
101
|
+
const entropy = getCoder(wordlist).decode(words);
|
|
102
|
+
aentropy(entropy);
|
|
103
|
+
return entropy;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Reversible: Converts raw entropy in form of byte array to mnemonic string.
|
|
107
|
+
* @param entropy - Byte array.
|
|
108
|
+
* @param wordlist - Imported wordlist for a specific language.
|
|
109
|
+
* @returns 12-24 words.
|
|
110
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
111
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
112
|
+
* @example
|
|
113
|
+
* Convert raw entropy into an English mnemonic.
|
|
114
|
+
* ```ts
|
|
115
|
+
* import { entropyToMnemonic } from '@decryption/bip39';
|
|
116
|
+
* import { wordlist } from '@decryption/bip39/wordlists/english.js';
|
|
117
|
+
* const ent = new Uint8Array([
|
|
118
|
+
* 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
|
|
119
|
+
* 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f
|
|
120
|
+
* ]);
|
|
121
|
+
* const mnemonic = entropyToMnemonic(ent, wordlist);
|
|
122
|
+
* // 'legal winner thank year wave sausage worth useful legal winner thank yellow'
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
export function entropyToMnemonic(entropy, wordlist) {
|
|
126
|
+
aentropy(entropy);
|
|
127
|
+
const words = getCoder(wordlist).encode(entropy);
|
|
128
|
+
return words.join(isJapanese(wordlist) ? '\u3000' : ' ');
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Validates mnemonic for being 12-24 words contained in `wordlist`.
|
|
132
|
+
* @param mnemonic - 12-24 words.
|
|
133
|
+
* @param wordlist - Imported wordlist for a specific language.
|
|
134
|
+
* @returns `true` when mnemonic checksum and words are valid.
|
|
135
|
+
* @example
|
|
136
|
+
* Validate one English mnemonic.
|
|
137
|
+
* ```ts
|
|
138
|
+
* import { validateMnemonic } from '@decryption/bip39';
|
|
139
|
+
* import { wordlist } from '@decryption/bip39/wordlists/english.js';
|
|
140
|
+
* const ok = validateMnemonic(
|
|
141
|
+
* 'legal winner thank year wave sausage worth useful legal winner thank yellow',
|
|
142
|
+
* wordlist
|
|
143
|
+
* );
|
|
144
|
+
* // => true
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
export function validateMnemonic(mnemonic, wordlist) {
|
|
148
|
+
try {
|
|
149
|
+
mnemonicToEntropy(mnemonic, wordlist);
|
|
150
|
+
}
|
|
151
|
+
catch (e) {
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
// BIP-39 salts PBKDF2 with the UTF-8 NFKD string "mnemonic" + passphrase.
|
|
157
|
+
const psalt = (passphrase) => nfkd('mnemonic' + passphrase);
|
|
158
|
+
/**
|
|
159
|
+
* Irreversible: Uses KDF to derive 64 bytes of key data from mnemonic + optional password.
|
|
160
|
+
* @param mnemonic - 12-24 words.
|
|
161
|
+
* @param passphrase - String that will additionally protect the key.
|
|
162
|
+
* @returns 64 bytes of key data.
|
|
163
|
+
* @throws If the mnemonic shape is invalid. {@link Error}
|
|
164
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
165
|
+
* @example
|
|
166
|
+
* Derive a seed from a mnemonic with the async PBKDF2 helper.
|
|
167
|
+
* ```ts
|
|
168
|
+
* const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
|
|
169
|
+
* const seed = await mnemonicToSeed(mnem, 'password');
|
|
170
|
+
* // => new Uint8Array([...64 bytes])
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
// BIP-39 seed derivation is independent from mnemonic generation.
|
|
174
|
+
// These helpers normalize the phrase but do not verify checksum or wordlist membership.
|
|
175
|
+
export function mnemonicToSeed(mnemonic, passphrase = '') {
|
|
176
|
+
return pbkdf2Async(sha512, normalize(mnemonic).nfkd, psalt(passphrase), {
|
|
177
|
+
c: 2048,
|
|
178
|
+
dkLen: 64,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Irreversible: Uses KDF to derive 64 bytes of key data from mnemonic + optional password.
|
|
183
|
+
* @param mnemonic - 12-24 words.
|
|
184
|
+
* @param passphrase - String that will additionally protect the key.
|
|
185
|
+
* @returns 64 bytes of key data.
|
|
186
|
+
* @throws If the mnemonic shape is invalid. {@link Error}
|
|
187
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
188
|
+
* @example
|
|
189
|
+
* Derive a seed from a mnemonic with the sync PBKDF2 helper.
|
|
190
|
+
* ```ts
|
|
191
|
+
* const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
|
|
192
|
+
* const seed = mnemonicToSeedSync(mnem, 'password');
|
|
193
|
+
* // => new Uint8Array([...64 bytes])
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
export function mnemonicToSeedSync(mnemonic, passphrase = '') {
|
|
197
|
+
return pbkdf2(sha512, normalize(mnemonic).nfkd, psalt(passphrase), {
|
|
198
|
+
c: 2048,
|
|
199
|
+
dkLen: 64,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Uses native, built-in functionality, provided by globalThis.crypto.
|
|
204
|
+
* Irreversible: Uses KDF to derive 64 bytes of key data from mnemonic + optional password.
|
|
205
|
+
* @param mnemonic - 12-24 words.
|
|
206
|
+
* @param passphrase - String that will additionally protect the key.
|
|
207
|
+
* @returns 64 bytes of key data.
|
|
208
|
+
* @throws If the mnemonic shape is invalid. {@link Error}
|
|
209
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
210
|
+
* @example
|
|
211
|
+
* Derive a seed with the native WebCrypto PBKDF2 helper.
|
|
212
|
+
* ```ts
|
|
213
|
+
* const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
|
|
214
|
+
* const seed = await mnemonicToSeedWebcrypto(mnem, 'password');
|
|
215
|
+
* // => new Uint8Array([...64 bytes])
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
export function mnemonicToSeedWebcrypto(mnemonic, passphrase = '') {
|
|
219
|
+
return pbkdf2web(sha512web, normalize(mnemonic).nfkd, psalt(passphrase), {
|
|
220
|
+
c: 2048,
|
|
221
|
+
dkLen: 64,
|
|
222
|
+
});
|
|
223
|
+
}
|