Repository navigation
Expand file tree
/
Copy pathmnemonic.ts
More file actions
155 lines (142 loc) · 3.94 KB
/
Copy pathmnemonic.ts
File metadata and controls
155 lines (142 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { HARDENED_OFFSET, HDKey } from "@scure/bip32";
import { entropyToMnemonic, mnemonicToSeedWebcrypto } from "@scure/bip39";
import { wordlist } from "@scure/bip39/wordlists/english.js";
import { deriveBitcoinAddress, deriveEthereumAddress } from "./chain.ts";
export type MnemonicWordCount = 12 | 24;
export type MnemonicAddresses = Readonly<{
bitcoin: string;
ethereum: string;
}>;
export type MnemonicBatch = Readonly<{
phrases: string;
bitcoinAddresses: string;
ethereumAddresses: string;
}>;
type MnemonicBatchOptions = Readonly<{
deriveAddresses?: (mnemonic: string) => Promise<MnemonicAddresses>;
isActive?: () => boolean;
}>;
export const BITCOIN_PREVIEW_PATH = "m/84'/0'/0'/0/0";
export const ETHEREUM_PREVIEW_PATH = "m/44'/60'/0'/0/0";
const hardened = (index: number): number => HARDENED_OFFSET + index;
const BITCOIN_PREVIEW_INDEXES = [
hardened(84),
hardened(0),
hardened(0),
0,
0,
] as const;
const ETHEREUM_PREVIEW_INDEXES = [
hardened(44),
hardened(60),
hardened(0),
0,
0,
] as const;
export const mnemonicEntropyLength = (
wordCount: MnemonicWordCount,
): 16 | 32 => {
if (wordCount === 12) return 16;
if (wordCount === 24) return 32;
throw new RangeError("Supported BIP-39 phrases contain 12 or 24 words");
};
export const formatMnemonicBatch = (
entropy: Uint8Array,
wordCount: MnemonicWordCount,
): string => {
const entropyLength = mnemonicEntropyLength(wordCount);
if (entropy.length % entropyLength !== 0) {
throw new RangeError(
"Mnemonic entropy length must divide the batch exactly",
);
}
const phrases: string[] = [];
for (let offset = 0; offset < entropy.length; offset += entropyLength) {
phrases.push(
entropyToMnemonic(
entropy.subarray(offset, offset + entropyLength),
wordlist,
),
);
}
return phrases.join("\n");
};
const wipeHdKey = (key: HDKey) => {
key.wipePrivateData();
key.chainCode?.fill(0);
};
const deriveAddress = (
root: HDKey,
path: string,
indexes: readonly number[],
fromSecretKey: (secretKey: Uint8Array) => string,
): string => {
let child: HDKey | undefined;
try {
for (const index of indexes) {
const next = (child ?? root).deriveChild(index);
if (child) wipeHdKey(child);
child = next;
}
const secretKey = child?.privateKey;
if (!secretKey) throw new Error(`No private key at ${path}`);
return fromSecretKey(secretKey);
} finally {
if (child) wipeHdKey(child);
}
};
export const deriveMnemonicAddresses = async (
mnemonic: string,
): Promise<MnemonicAddresses> => {
const seed = await mnemonicToSeedWebcrypto(mnemonic, "");
let root: HDKey | undefined;
try {
root = HDKey.fromMasterSeed(seed);
return {
bitcoin: deriveAddress(
root,
BITCOIN_PREVIEW_PATH,
BITCOIN_PREVIEW_INDEXES,
deriveBitcoinAddress,
),
ethereum: deriveAddress(
root,
ETHEREUM_PREVIEW_PATH,
ETHEREUM_PREVIEW_INDEXES,
deriveEthereumAddress,
),
};
} finally {
if (root) wipeHdKey(root);
seed.fill(0);
}
};
export const deriveMnemonicBatch = async (
entropy: Uint8Array,
wordCount: MnemonicWordCount,
options: MnemonicBatchOptions = {},
): Promise<MnemonicBatch | undefined> => {
const { deriveAddresses = deriveMnemonicAddresses, isActive = () => true } =
options;
let phrases = "";
try {
if (!isActive()) return;
phrases = formatMnemonicBatch(entropy, wordCount);
} finally {
entropy.fill(0);
}
const bitcoinAddresses: string[] = [];
const ethereumAddresses: string[] = [];
for (const phrase of phrases.split("\n")) {
if (!isActive()) return;
const addresses = await deriveAddresses(phrase);
if (!isActive()) return;
bitcoinAddresses.push(addresses.bitcoin);
ethereumAddresses.push(addresses.ethereum);
}
return {
phrases,
bitcoinAddresses: bitcoinAddresses.join("\n"),
ethereumAddresses: ethereumAddresses.join("\n"),
};
};