@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/index.d.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { type TArg, type TRet } from '@decryption/hashes/utils';
|
|
2
|
+
/**
|
|
3
|
+
* Generate x random words. Uses Cryptographically-Secure Random Number Generator.
|
|
4
|
+
* @param wordlist - Imported wordlist for a specific language.
|
|
5
|
+
* @param strength - Mnemonic strength, from 128 to 256 bits.
|
|
6
|
+
* @returns 12-24 word mnemonic phrase.
|
|
7
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
8
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
9
|
+
* @example
|
|
10
|
+
* Generate a new English mnemonic.
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { generateMnemonic } from '@decryption/bip39';
|
|
13
|
+
* import { wordlist } from '@decryption/bip39/wordlists/english.js';
|
|
14
|
+
* const mnemonic = generateMnemonic(wordlist, 128);
|
|
15
|
+
* // 'legal winner thank year wave sausage worth useful legal winner thank yellow'
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function generateMnemonic(wordlist: string[], strength?: number): string;
|
|
19
|
+
/**
|
|
20
|
+
* Reversible: Converts mnemonic string to raw entropy in form of byte array.
|
|
21
|
+
* @param mnemonic - 12-24 words.
|
|
22
|
+
* @param wordlist - Imported wordlist for a specific language.
|
|
23
|
+
* @returns Raw entropy bytes.
|
|
24
|
+
* @throws If the mnemonic shape or checksum is invalid. {@link Error}
|
|
25
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
26
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
27
|
+
* @example
|
|
28
|
+
* Decode a mnemonic back into its original entropy bytes.
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { mnemonicToEntropy } from '@decryption/bip39';
|
|
31
|
+
* import { wordlist } from '@decryption/bip39/wordlists/english.js';
|
|
32
|
+
* const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
|
|
33
|
+
* const entropy = mnemonicToEntropy(mnem, wordlist);
|
|
34
|
+
* // Produces the original 16-byte entropy payload.
|
|
35
|
+
* new Uint8Array([
|
|
36
|
+
* 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
|
|
37
|
+
* 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f
|
|
38
|
+
* ])
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export declare function mnemonicToEntropy(mnemonic: string, wordlist: string[]): TRet<Uint8Array>;
|
|
42
|
+
/**
|
|
43
|
+
* Reversible: Converts raw entropy in form of byte array to mnemonic string.
|
|
44
|
+
* @param entropy - Byte array.
|
|
45
|
+
* @param wordlist - Imported wordlist for a specific language.
|
|
46
|
+
* @returns 12-24 words.
|
|
47
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
48
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
49
|
+
* @example
|
|
50
|
+
* Convert raw entropy into an English mnemonic.
|
|
51
|
+
* ```ts
|
|
52
|
+
* import { entropyToMnemonic } from '@decryption/bip39';
|
|
53
|
+
* import { wordlist } from '@decryption/bip39/wordlists/english.js';
|
|
54
|
+
* const ent = new Uint8Array([
|
|
55
|
+
* 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
|
|
56
|
+
* 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f
|
|
57
|
+
* ]);
|
|
58
|
+
* const mnemonic = entropyToMnemonic(ent, wordlist);
|
|
59
|
+
* // 'legal winner thank year wave sausage worth useful legal winner thank yellow'
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export declare function entropyToMnemonic(entropy: TArg<Uint8Array>, wordlist: string[]): string;
|
|
63
|
+
/**
|
|
64
|
+
* Validates mnemonic for being 12-24 words contained in `wordlist`.
|
|
65
|
+
* @param mnemonic - 12-24 words.
|
|
66
|
+
* @param wordlist - Imported wordlist for a specific language.
|
|
67
|
+
* @returns `true` when mnemonic checksum and words are valid.
|
|
68
|
+
* @example
|
|
69
|
+
* Validate one English mnemonic.
|
|
70
|
+
* ```ts
|
|
71
|
+
* import { validateMnemonic } from '@decryption/bip39';
|
|
72
|
+
* import { wordlist } from '@decryption/bip39/wordlists/english.js';
|
|
73
|
+
* const ok = validateMnemonic(
|
|
74
|
+
* 'legal winner thank year wave sausage worth useful legal winner thank yellow',
|
|
75
|
+
* wordlist
|
|
76
|
+
* );
|
|
77
|
+
* // => true
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export declare function validateMnemonic(mnemonic: string, wordlist: string[]): boolean;
|
|
81
|
+
/**
|
|
82
|
+
* Irreversible: Uses KDF to derive 64 bytes of key data from mnemonic + optional password.
|
|
83
|
+
* @param mnemonic - 12-24 words.
|
|
84
|
+
* @param passphrase - String that will additionally protect the key.
|
|
85
|
+
* @returns 64 bytes of key data.
|
|
86
|
+
* @throws If the mnemonic shape is invalid. {@link Error}
|
|
87
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
88
|
+
* @example
|
|
89
|
+
* Derive a seed from a mnemonic with the async PBKDF2 helper.
|
|
90
|
+
* ```ts
|
|
91
|
+
* const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
|
|
92
|
+
* const seed = await mnemonicToSeed(mnem, 'password');
|
|
93
|
+
* // => new Uint8Array([...64 bytes])
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export declare function mnemonicToSeed(mnemonic: string, passphrase?: string): Promise<TRet<Uint8Array>>;
|
|
97
|
+
/**
|
|
98
|
+
* Irreversible: Uses KDF to derive 64 bytes of key data from mnemonic + optional password.
|
|
99
|
+
* @param mnemonic - 12-24 words.
|
|
100
|
+
* @param passphrase - String that will additionally protect the key.
|
|
101
|
+
* @returns 64 bytes of key data.
|
|
102
|
+
* @throws If the mnemonic shape is invalid. {@link Error}
|
|
103
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
104
|
+
* @example
|
|
105
|
+
* Derive a seed from a mnemonic with the sync PBKDF2 helper.
|
|
106
|
+
* ```ts
|
|
107
|
+
* const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
|
|
108
|
+
* const seed = mnemonicToSeedSync(mnem, 'password');
|
|
109
|
+
* // => new Uint8Array([...64 bytes])
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
export declare function mnemonicToSeedSync(mnemonic: string, passphrase?: string): TRet<Uint8Array>;
|
|
113
|
+
/**
|
|
114
|
+
* Uses native, built-in functionality, provided by globalThis.crypto.
|
|
115
|
+
* Irreversible: Uses KDF to derive 64 bytes of key data from mnemonic + optional password.
|
|
116
|
+
* @param mnemonic - 12-24 words.
|
|
117
|
+
* @param passphrase - String that will additionally protect the key.
|
|
118
|
+
* @returns 64 bytes of key data.
|
|
119
|
+
* @throws If the mnemonic shape is invalid. {@link Error}
|
|
120
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
121
|
+
* @example
|
|
122
|
+
* Derive a seed with the native WebCrypto PBKDF2 helper.
|
|
123
|
+
* ```ts
|
|
124
|
+
* const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
|
|
125
|
+
* const seed = await mnemonicToSeedWebcrypto(mnem, 'password');
|
|
126
|
+
* // => new Uint8Array([...64 bytes])
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
export declare function mnemonicToSeedWebcrypto(mnemonic: string, passphrase?: string): Promise<TRet<Uint8Array>>;
|
package/index.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateMnemonic = generateMnemonic;
|
|
4
|
+
exports.mnemonicToEntropy = mnemonicToEntropy;
|
|
5
|
+
exports.entropyToMnemonic = entropyToMnemonic;
|
|
6
|
+
exports.validateMnemonic = validateMnemonic;
|
|
7
|
+
exports.mnemonicToSeed = mnemonicToSeed;
|
|
8
|
+
exports.mnemonicToSeedSync = mnemonicToSeedSync;
|
|
9
|
+
exports.mnemonicToSeedWebcrypto = mnemonicToSeedWebcrypto;
|
|
10
|
+
/*! scure-bip39 - MIT License (c) 2022 Patricio Palladino, Paul Miller (paulmillr.com) */
|
|
11
|
+
const pbkdf2_1 = require("@decryption/hashes/pbkdf2");
|
|
12
|
+
const sha2_1 = require("@decryption/hashes/sha2");
|
|
13
|
+
const utils_1 = require("@decryption/hashes/utils");
|
|
14
|
+
const webcrypto_1 = require("@decryption/hashes/webcrypto");
|
|
15
|
+
const base_1 = require("@decryption/base");
|
|
16
|
+
// Japanese wordlist
|
|
17
|
+
// The canonical BIP-39 Japanese wordlist starts with あいこくしん.
|
|
18
|
+
// Use that sentinel so generated phrases use U+3000 ideographic spaces.
|
|
19
|
+
const isJapanese = (wordlist) => wordlist[0] === '\u3042\u3044\u3053\u304f\u3057\u3093';
|
|
20
|
+
// Normalization replaces equivalent sequences of characters
|
|
21
|
+
// so that any two texts that are equivalent will be reduced
|
|
22
|
+
// to the same sequence of code points, called the normal form of the original text.
|
|
23
|
+
// https://tonsky.me/blog/unicode/#why-is-a----
|
|
24
|
+
// BIP-39 requires UTF-8 NFKD for localized wordlists and mnemonic sentences.
|
|
25
|
+
// It also applies NFKD to the "mnemonic" + passphrase salt.
|
|
26
|
+
function nfkd(str) {
|
|
27
|
+
if (typeof str !== 'string')
|
|
28
|
+
throw new TypeError('invalid mnemonic type: ' + typeof str);
|
|
29
|
+
return str.normalize('NFKD');
|
|
30
|
+
}
|
|
31
|
+
// BIP-39 mnemonics are consumed in NFKD form.
|
|
32
|
+
// They must contain 12, 15, 18, 21, or 24 words before checksum validation.
|
|
33
|
+
function normalize(str) {
|
|
34
|
+
const norm = nfkd(str);
|
|
35
|
+
const words = norm.split(' ');
|
|
36
|
+
if (![12, 15, 18, 21, 24].includes(words.length))
|
|
37
|
+
throw new Error('Invalid mnemonic');
|
|
38
|
+
return { nfkd: norm, words };
|
|
39
|
+
}
|
|
40
|
+
// BIP-39 entropy payloads are 128-256 bits in 32-bit increments, i.e. 16/20/24/28/32 bytes.
|
|
41
|
+
function aentropy(ent) {
|
|
42
|
+
(0, utils_1.abytes)(ent);
|
|
43
|
+
if (![16, 20, 24, 28, 32].includes(ent.length))
|
|
44
|
+
throw new RangeError('invalid entropy length');
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Generate x random words. Uses Cryptographically-Secure Random Number Generator.
|
|
48
|
+
* @param wordlist - Imported wordlist for a specific language.
|
|
49
|
+
* @param strength - Mnemonic strength, from 128 to 256 bits.
|
|
50
|
+
* @returns 12-24 word mnemonic phrase.
|
|
51
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
52
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
53
|
+
* @example
|
|
54
|
+
* Generate a new English mnemonic.
|
|
55
|
+
* ```ts
|
|
56
|
+
* import { generateMnemonic } from '@decryption/bip39';
|
|
57
|
+
* import { wordlist } from '@decryption/bip39/wordlists/english.js';
|
|
58
|
+
* const mnemonic = generateMnemonic(wordlist, 128);
|
|
59
|
+
* // 'legal winner thank year wave sausage worth useful legal winner thank yellow'
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
function generateMnemonic(wordlist, strength = 128) {
|
|
63
|
+
(0, utils_1.anumber)(strength);
|
|
64
|
+
if (strength % 32 !== 0 || strength > 256)
|
|
65
|
+
throw new RangeError('Invalid entropy');
|
|
66
|
+
return entropyToMnemonic((0, utils_1.randomBytes)(strength / 8), wordlist);
|
|
67
|
+
}
|
|
68
|
+
const calcChecksum = (entropy) => {
|
|
69
|
+
// Checksum is ent.length/4 bits long
|
|
70
|
+
const bitsLeft = 8 - entropy.length / 4;
|
|
71
|
+
// Zero rightmost "bitsLeft" bits in byte
|
|
72
|
+
// For example: bitsLeft=4 val=10111101 -> 10110000
|
|
73
|
+
return new Uint8Array([((0, sha2_1.sha256)(entropy)[0] >> bitsLeft) << bitsLeft]);
|
|
74
|
+
};
|
|
75
|
+
function getCoder(wordlist) {
|
|
76
|
+
if (!Array.isArray(wordlist) || wordlist.length !== 2048 || typeof wordlist[0] !== 'string')
|
|
77
|
+
throw new TypeError('Wordlist: expected array of 2048 strings');
|
|
78
|
+
wordlist.forEach((i) => {
|
|
79
|
+
if (typeof i !== 'string')
|
|
80
|
+
throw new TypeError('wordlist: non-string element: ' + i);
|
|
81
|
+
});
|
|
82
|
+
// BIP-39 appends checksum bits to entropy.
|
|
83
|
+
// It then splits the bitstream into 11-bit indexes for a 2048-word list.
|
|
84
|
+
return base_1.utils.chain(base_1.utils.checksum(1, calcChecksum), base_1.utils.radix2(11, true), base_1.utils.alphabet(wordlist));
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Reversible: Converts mnemonic string to raw entropy in form of byte array.
|
|
88
|
+
* @param mnemonic - 12-24 words.
|
|
89
|
+
* @param wordlist - Imported wordlist for a specific language.
|
|
90
|
+
* @returns Raw entropy bytes.
|
|
91
|
+
* @throws If the mnemonic shape or checksum is invalid. {@link Error}
|
|
92
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
93
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
94
|
+
* @example
|
|
95
|
+
* Decode a mnemonic back into its original entropy bytes.
|
|
96
|
+
* ```ts
|
|
97
|
+
* import { mnemonicToEntropy } from '@decryption/bip39';
|
|
98
|
+
* import { wordlist } from '@decryption/bip39/wordlists/english.js';
|
|
99
|
+
* const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
|
|
100
|
+
* const entropy = mnemonicToEntropy(mnem, wordlist);
|
|
101
|
+
* // Produces the original 16-byte entropy payload.
|
|
102
|
+
* new Uint8Array([
|
|
103
|
+
* 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
|
|
104
|
+
* 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f
|
|
105
|
+
* ])
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
function mnemonicToEntropy(mnemonic, wordlist) {
|
|
109
|
+
const { words } = normalize(mnemonic);
|
|
110
|
+
const entropy = getCoder(wordlist).decode(words);
|
|
111
|
+
aentropy(entropy);
|
|
112
|
+
return entropy;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Reversible: Converts raw entropy in form of byte array to mnemonic string.
|
|
116
|
+
* @param entropy - Byte array.
|
|
117
|
+
* @param wordlist - Imported wordlist for a specific language.
|
|
118
|
+
* @returns 12-24 words.
|
|
119
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
120
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
121
|
+
* @example
|
|
122
|
+
* Convert raw entropy into an English mnemonic.
|
|
123
|
+
* ```ts
|
|
124
|
+
* import { entropyToMnemonic } from '@decryption/bip39';
|
|
125
|
+
* import { wordlist } from '@decryption/bip39/wordlists/english.js';
|
|
126
|
+
* const ent = new Uint8Array([
|
|
127
|
+
* 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
|
|
128
|
+
* 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f
|
|
129
|
+
* ]);
|
|
130
|
+
* const mnemonic = entropyToMnemonic(ent, wordlist);
|
|
131
|
+
* // 'legal winner thank year wave sausage worth useful legal winner thank yellow'
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
function entropyToMnemonic(entropy, wordlist) {
|
|
135
|
+
aentropy(entropy);
|
|
136
|
+
const words = getCoder(wordlist).encode(entropy);
|
|
137
|
+
return words.join(isJapanese(wordlist) ? '\u3000' : ' ');
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Validates mnemonic for being 12-24 words contained in `wordlist`.
|
|
141
|
+
* @param mnemonic - 12-24 words.
|
|
142
|
+
* @param wordlist - Imported wordlist for a specific language.
|
|
143
|
+
* @returns `true` when mnemonic checksum and words are valid.
|
|
144
|
+
* @example
|
|
145
|
+
* Validate one English mnemonic.
|
|
146
|
+
* ```ts
|
|
147
|
+
* import { validateMnemonic } from '@decryption/bip39';
|
|
148
|
+
* import { wordlist } from '@decryption/bip39/wordlists/english.js';
|
|
149
|
+
* const ok = validateMnemonic(
|
|
150
|
+
* 'legal winner thank year wave sausage worth useful legal winner thank yellow',
|
|
151
|
+
* wordlist
|
|
152
|
+
* );
|
|
153
|
+
* // => true
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
function validateMnemonic(mnemonic, wordlist) {
|
|
157
|
+
try {
|
|
158
|
+
mnemonicToEntropy(mnemonic, wordlist);
|
|
159
|
+
}
|
|
160
|
+
catch (e) {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
// BIP-39 salts PBKDF2 with the UTF-8 NFKD string "mnemonic" + passphrase.
|
|
166
|
+
const psalt = (passphrase) => nfkd('mnemonic' + passphrase);
|
|
167
|
+
/**
|
|
168
|
+
* Irreversible: Uses KDF to derive 64 bytes of key data from mnemonic + optional password.
|
|
169
|
+
* @param mnemonic - 12-24 words.
|
|
170
|
+
* @param passphrase - String that will additionally protect the key.
|
|
171
|
+
* @returns 64 bytes of key data.
|
|
172
|
+
* @throws If the mnemonic shape is invalid. {@link Error}
|
|
173
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
174
|
+
* @example
|
|
175
|
+
* Derive a seed from a mnemonic with the async PBKDF2 helper.
|
|
176
|
+
* ```ts
|
|
177
|
+
* const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
|
|
178
|
+
* const seed = await mnemonicToSeed(mnem, 'password');
|
|
179
|
+
* // => new Uint8Array([...64 bytes])
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
// BIP-39 seed derivation is independent from mnemonic generation.
|
|
183
|
+
// These helpers normalize the phrase but do not verify checksum or wordlist membership.
|
|
184
|
+
function mnemonicToSeed(mnemonic, passphrase = '') {
|
|
185
|
+
return (0, pbkdf2_1.pbkdf2Async)(sha2_1.sha512, normalize(mnemonic).nfkd, psalt(passphrase), {
|
|
186
|
+
c: 2048,
|
|
187
|
+
dkLen: 64,
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Irreversible: Uses KDF to derive 64 bytes of key data from mnemonic + optional password.
|
|
192
|
+
* @param mnemonic - 12-24 words.
|
|
193
|
+
* @param passphrase - String that will additionally protect the key.
|
|
194
|
+
* @returns 64 bytes of key data.
|
|
195
|
+
* @throws If the mnemonic shape is invalid. {@link Error}
|
|
196
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
197
|
+
* @example
|
|
198
|
+
* Derive a seed from a mnemonic with the sync PBKDF2 helper.
|
|
199
|
+
* ```ts
|
|
200
|
+
* const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
|
|
201
|
+
* const seed = mnemonicToSeedSync(mnem, 'password');
|
|
202
|
+
* // => new Uint8Array([...64 bytes])
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
205
|
+
function mnemonicToSeedSync(mnemonic, passphrase = '') {
|
|
206
|
+
return (0, pbkdf2_1.pbkdf2)(sha2_1.sha512, normalize(mnemonic).nfkd, psalt(passphrase), {
|
|
207
|
+
c: 2048,
|
|
208
|
+
dkLen: 64,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Uses native, built-in functionality, provided by globalThis.crypto.
|
|
213
|
+
* Irreversible: Uses KDF to derive 64 bytes of key data from mnemonic + optional password.
|
|
214
|
+
* @param mnemonic - 12-24 words.
|
|
215
|
+
* @param passphrase - String that will additionally protect the key.
|
|
216
|
+
* @returns 64 bytes of key data.
|
|
217
|
+
* @throws If the mnemonic shape is invalid. {@link Error}
|
|
218
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
219
|
+
* @example
|
|
220
|
+
* Derive a seed with the native WebCrypto PBKDF2 helper.
|
|
221
|
+
* ```ts
|
|
222
|
+
* const mnem = 'legal winner thank year wave sausage worth useful legal winner thank yellow';
|
|
223
|
+
* const seed = await mnemonicToSeedWebcrypto(mnem, 'password');
|
|
224
|
+
* // => new Uint8Array([...64 bytes])
|
|
225
|
+
* ```
|
|
226
|
+
*/
|
|
227
|
+
function mnemonicToSeedWebcrypto(mnemonic, passphrase = '') {
|
|
228
|
+
return (0, webcrypto_1.pbkdf2)(webcrypto_1.sha512, normalize(mnemonic).nfkd, psalt(passphrase), {
|
|
229
|
+
c: 2048,
|
|
230
|
+
dkLen: 64,
|
|
231
|
+
});
|
|
232
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@decryption/bip39",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Audited & minimal implementation of BIP39 mnemonic phrases. Dual CJS+ESM fork of @scure/bip39.",
|
|
5
|
+
"author": "Constructive <developers@constructive.io>",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"module": "esm/index.js",
|
|
8
|
+
"types": "index.d.ts",
|
|
9
|
+
"homepage": "https://github.com/constructive-io/decryption",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"directory": "dist"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/constructive-io/decryption"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/constructive-io/decryption/issues"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"copy": "makage assets",
|
|
24
|
+
"clean": "makage clean",
|
|
25
|
+
"prepublishOnly": "npm run build",
|
|
26
|
+
"build": "makage build && cp LICENSE dist/LICENSE",
|
|
27
|
+
"lint": "echo \"vendored fork — linted upstream\" && exit 0",
|
|
28
|
+
"test": "jest",
|
|
29
|
+
"test:watch": "jest --watch"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"bip39",
|
|
33
|
+
"mnemonic",
|
|
34
|
+
"wallet",
|
|
35
|
+
"seed",
|
|
36
|
+
"wordlist"
|
|
37
|
+
],
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@decryption/base": "0.2.0",
|
|
40
|
+
"@decryption/hashes": "0.2.0"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"makage": "0.3.0"
|
|
44
|
+
},
|
|
45
|
+
"gitHead": "8d85fbfdbb1d57da93e99904a03e6e8d8531d281"
|
|
46
|
+
}
|