Skip to content

Commit

Permalink
Added mnemonicPath option to cli.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed May 14, 2021
1 parent b6370f1 commit 6e08809
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions packages/cli/src.ts/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ async function loadAccount(arg: string, plugin: Plugin, preventFile?: boolean):

// Secure entry; use prompt with mask
if (arg === "-") {
const content = await getPassword("Private Key / Mnemonic:");
const content = await getPassword("Private Key / Mnemonic: ");
return loadAccount(content, plugin, true);
}

Expand All @@ -413,7 +413,7 @@ async function loadAccount(arg: string, plugin: Plugin, preventFile?: boolean):
let signerPromise: Promise<ethers.Wallet> = null;
if (plugin.mnemonicPassword) {
signerPromise = getPassword("Password (mnemonic): ").then((password) => {
let node = ethers.utils.HDNode.fromMnemonic(mnemonic, password).derivePath(ethers.utils.defaultPath);
let node = ethers.utils.HDNode.fromMnemonic(mnemonic, password).derivePath(plugin.mnemonicPath);
return new ethers.Wallet(node.privateKey, plugin.provider);
});

Expand All @@ -425,7 +425,7 @@ async function loadAccount(arg: string, plugin: Plugin, preventFile?: boolean):
let progressBar = getProgressBar("Decrypting");
return scrypt.scrypt(passwordBytes, saltBytes, (1 << 20), 8, 1, 32, progressBar).then((key) => {
const derivedPassword = ethers.utils.hexlify(key).substring(2);
const node = ethers.utils.HDNode.fromMnemonic(mnemonic, derivedPassword).derivePath(ethers.utils.defaultPath);
const node = ethers.utils.HDNode.fromMnemonic(mnemonic, derivedPassword).derivePath(plugin.mnemonicPath);
return new ethers.Wallet(node.privateKey, plugin.provider);
});
});
Expand Down Expand Up @@ -495,6 +495,7 @@ export abstract class Plugin {

accounts: ReadonlyArray<WrappedSigner>;
mnemonicPassword: boolean;
mnemonicPath: string;
_xxxMnemonicPasswordHard: boolean;

gasLimit: ethers.BigNumber;
Expand Down Expand Up @@ -566,6 +567,18 @@ export abstract class Plugin {
// Accounts

ethers.utils.defineReadOnly(this, "mnemonicPassword", argParser.consumeFlag("mnemonic-password"));

ethers.utils.defineReadOnly(this, "mnemonicPath", (function() {
let mnemonicPath = argParser.consumeOption("mnemonic-path");
if (mnemonicPath) {
if (mnemonicPath.match(/^[0-9]+$/)) {
return `m/44'/60'/${ mnemonicPath }'/0/0`;
}
return mnemonicPath;
}
return ethers.utils.defaultPath;
})());

ethers.utils.defineReadOnly(this, "_xxxMnemonicPasswordHard", argParser.consumeFlag("xxx-mnemonic-password"));

let accounts: Array<WrappedSigner> = [ ];
Expand Down Expand Up @@ -859,6 +872,7 @@ export class CLI {
console.log(" --account-rpc ADDRESS Add the address from a JSON-RPC provider");
console.log(" --account-rpc INDEX Add the index from a JSON-RPC provider");
console.log(" --mnemonic-password Prompt for a password for mnemonics");
console.log(" --mnemonic-path BIP-43 mnemonic path");
console.log(" --xxx-mnemonic-password Prompt for a (experimental) hard password");
console.log("");
}
Expand Down

0 comments on commit 6e08809

Please sign in to comment.