Skip to content

Commit ae00e6d

Browse files
paulmillralcuadrado
authored andcommitted
Re-export micro-bip32 and micro-bip39 modules.
1 parent b5cb6fe commit ae00e6d

13 files changed

+16
-18874
lines changed

‎package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
"*.d.ts.map"
2525
],
2626
"dependencies": {
27+
"@noble/hashes": "^0.5.9",
28+
"@noble/secp256k1": "^1.4.0",
2729
"micro-base": "^0.10.1",
28-
"@noble/hashes": "^0.5.7",
29-
"@noble/secp256k1": "^1.4.0"
30+
"micro-bip32": "^0.1.0",
31+
"micro-bip39": "^0.1.3"
3032
},
3133
"browser": {
3234
"crypto": false

‎src/bip39/index.ts

+1-111
Original file line numberDiff line numberDiff line change
@@ -1,111 +1 @@
1-
import { pbkdf2, pbkdf2Async } from "@noble/hashes/pbkdf2";
2-
import { sha256 } from "@noble/hashes/sha256";
3-
import { sha512 } from "@noble/hashes/sha512";
4-
import { assertBytes, assertNumber, randomBytes } from "@noble/hashes/utils";
5-
import { utils as baseUtils } from "micro-base";
6-
7-
const isJapanese = (wordlist: string[]) =>
8-
wordlist[0] === "\u3042\u3044\u3053\u304f\u3057\u3093"; // Japanese wordlist
9-
10-
// Normalization replaces equivalent sequences of characters
11-
// so that any two texts that are equivalent will be reduced
12-
// to the same sequence of code points, called the normal form of the original text.
13-
const nfkd = (str: string) => str.normalize("NFKD");
14-
15-
function assertMnemonic(mnemonic: string) {
16-
if (typeof mnemonic !== "string") {
17-
throw new TypeError(`Invalid mnemonic type: ${typeof mnemonic}`);
18-
}
19-
}
20-
21-
export function generateMnemonic(
22-
wordlist: string[],
23-
strength: number = 128
24-
): string {
25-
assertNumber(strength);
26-
if (strength % 32 !== 0) {
27-
throw new TypeError("Invalid entropy");
28-
}
29-
return entropyToMnemonic(randomBytes(strength / 8), wordlist);
30-
}
31-
32-
const checksum = (entropy: Uint8Array) => {
33-
// Checksum is ent.length/4 bits long
34-
const bitsLeft = 8 - entropy.length / 4;
35-
// Zero rightmost "bitsLeft" bits in byte
36-
// For example: bitsLeft=4 val=10111101 -> 10110000
37-
return new Uint8Array([(sha256(entropy)[0] >> bitsLeft) << bitsLeft]);
38-
};
39-
40-
function getCoder(wordlist: string[]) {
41-
if (
42-
!Array.isArray(wordlist) ||
43-
wordlist.length !== 2 ** 11 ||
44-
typeof wordlist[0] !== "string"
45-
) {
46-
throw new Error("Worlist: expected array of 2048 strings");
47-
}
48-
for (const i of wordlist) {
49-
if (typeof i !== "string") {
50-
throw new Error(`Wordlist: non-string element: ${i}`);
51-
}
52-
}
53-
return baseUtils.chain(
54-
baseUtils.checksum(1, checksum),
55-
baseUtils.radix2(11, true),
56-
baseUtils.alphabet(wordlist)
57-
);
58-
}
59-
60-
export function mnemonicToEntropy(
61-
mnemonic: string,
62-
wordlist: string[]
63-
): Uint8Array {
64-
assertMnemonic(mnemonic);
65-
const words = nfkd(mnemonic).split(" ");
66-
if (![12, 15, 18, 21, 24].includes(words.length)) {
67-
throw new Error("Invalid mnemonic");
68-
}
69-
const entropy = getCoder(wordlist).decode(words);
70-
assertBytes(entropy, 16, 20, 24, 28, 32);
71-
return entropy;
72-
}
73-
74-
export function entropyToMnemonic(
75-
entropy: Uint8Array,
76-
wordlist: string[]
77-
): string {
78-
assertBytes(entropy, 16, 20, 24, 28, 32);
79-
const words = getCoder(wordlist).encode(entropy);
80-
return words.join(isJapanese(wordlist) ? "\u3000" : " ");
81-
}
82-
83-
export function validateMnemonic(
84-
mnemonic: string,
85-
wordlist: string[]
86-
): boolean {
87-
try {
88-
mnemonicToEntropy(mnemonic, wordlist);
89-
} catch (e) {
90-
return false;
91-
}
92-
return true;
93-
}
94-
95-
const salt = (passphrase = "") => nfkd(`mnemonic${passphrase}`);
96-
97-
export function mnemonicToSeed(mnemonic: string, passphrase = "") {
98-
assertMnemonic(mnemonic);
99-
return pbkdf2Async(sha512, nfkd(mnemonic), salt(passphrase), {
100-
c: 2048,
101-
dkLen: 64
102-
});
103-
}
104-
105-
export function mnemonicToSeedSync(mnemonic: string, passphrase = "") {
106-
assertMnemonic(mnemonic);
107-
return pbkdf2(sha512, nfkd(mnemonic), salt(passphrase), {
108-
c: 2048,
109-
dkLen: 64
110-
});
111-
}
1+
export { generateMnemonic, mnemonicToEntropy, entropyToMnemonic, validateMnemonic, mnemonicToSeed, mnemonicToSeedSync } from 'micro-bip39';

0 commit comments

Comments
 (0)