in src/commands/TruffleCommands.ts [188:231]
export async function getPrivateKeyFromMnemonic(): Promise<void> {
Telemetry.sendEvent('TruffleCommands.getPrivateKeyFromMnemonic.commandStarted');
const mnemonicItems: IExtendedQuickPickItem[] = MnemonicRepository
.getExistedMnemonicPaths()
.map((mnemonicPath) => {
const savedMnemonic = MnemonicRepository.getMnemonic(mnemonicPath);
return {
detail: mnemonicPath,
extended: savedMnemonic,
label: MnemonicRepository.MaskMnemonic(savedMnemonic),
};
});
if (mnemonicItems.length === 0) {
Telemetry.sendEvent('TruffleCommands.getPrivateKeyFromMnemonic.thereAreNoMnemonics');
window.showErrorMessage(Constants.errorMessageStrings.ThereAreNoMnemonics);
return;
}
const mnemonicItem = await showQuickPick(
mnemonicItems,
{ placeHolder: Constants.placeholders.selectMnemonicExtractKey, ignoreFocusOut: true },
);
const mnemonic = mnemonicItem.extended;
if (!mnemonic) {
Telemetry.sendEvent('TruffleCommands.getPrivateKeyFromMnemonic.mnemonicFileHaveNoText');
window.showErrorMessage(Constants.errorMessageStrings.MnemonicFileHaveNoText);
return;
}
try {
const buffer = await bip39.mnemonicToSeed(mnemonic);
const key = hdkey.fromMasterSeed(buffer);
const childKey = key.derive('m/44\'/60\'/0\'/0/0');
const privateKey = childKey.privateKey.toString('hex');
await vscodeEnvironment.writeToClipboard(privateKey);
window.showInformationMessage(Constants.informationMessage.privateKeyWasCopiedToClipboard);
} catch (error) {
Telemetry.sendException(error);
window.showErrorMessage(Constants.errorMessageStrings.InvalidMnemonic);
}
Telemetry.sendEvent('TruffleCommands.getPrivateKeyFromMnemonic.commandFinished');
}