-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add utility lock and unlock commands (#464)
- Loading branch information
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import Wallet from 'ethereumjs-wallet' | ||
import { readFile } from 'fs/promises' | ||
import { GroupCommand } from 'furious-commander' | ||
import { fileExists } from '../../utils' | ||
import { CommandLog } from '../root-command/command-log' | ||
import { Lock } from './lock' | ||
import { Unlock } from './unlock' | ||
|
||
export class Utility implements GroupCommand { | ||
public readonly name = 'utility' | ||
|
||
public readonly description = 'Utility commands for managing wallets' | ||
|
||
public subCommandClasses = [Lock, Unlock] | ||
} | ||
|
||
export async function createWallet(pathOrPrivateKey: string, console: CommandLog): Promise<Wallet> { | ||
if (fileExists(pathOrPrivateKey)) { | ||
const json = await readFile(pathOrPrivateKey, 'utf8') | ||
const password = await console.askForPassword('Enter password to decrypt key file') | ||
const wallet = await Wallet.fromV3(json, password) | ||
|
||
return wallet | ||
} | ||
|
||
if (pathOrPrivateKey.startsWith('0x')) { | ||
pathOrPrivateKey = pathOrPrivateKey.slice(2) | ||
} | ||
|
||
return new Wallet(Buffer.from(pathOrPrivateKey, 'hex')) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Argument, LeafCommand } from 'furious-commander' | ||
import { createWallet } from '.' | ||
import { RootCommand } from '../root-command' | ||
|
||
export class Lock extends RootCommand implements LeafCommand { | ||
public readonly name = 'lock' | ||
|
||
public readonly description = 'Takes a wallet and locks it with a password' | ||
|
||
@Argument({ | ||
key: 'wallet-source', | ||
description: 'Wallet source (path or private key string)', | ||
required: true, | ||
autocompletePath: true, | ||
conflicts: 'stdin', | ||
}) | ||
public walletSource!: string | ||
|
||
public async run(): Promise<void> { | ||
await super.init() | ||
const wallet = await createWallet(this.walletSource, this.console) | ||
const password = await this.console.askForPasswordWithConfirmation( | ||
'Enter a new password to encrypt key file', | ||
'Confirm password', | ||
) | ||
this.console.all(await wallet.toV3String(password)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Argument, LeafCommand } from 'furious-commander' | ||
import { createWallet } from '.' | ||
import { RootCommand } from '../root-command' | ||
|
||
export class Unlock extends RootCommand implements LeafCommand { | ||
public readonly name = 'unlock' | ||
|
||
public readonly description = 'Unlocks a V3 wallet with a password and prints the private key string' | ||
|
||
@Argument({ | ||
key: 'wallet-source', | ||
description: 'Wallet source (path or private key string)', | ||
required: true, | ||
autocompletePath: true, | ||
conflicts: 'stdin', | ||
}) | ||
public walletSource!: string | ||
|
||
public async run(): Promise<void> { | ||
await super.init() | ||
const wallet = await createWallet(this.walletSource, this.console) | ||
|
||
if (!this.yes) { | ||
await this.console.confirm( | ||
'The private key will be printed to the console. Make sure no one is looking at your screen. Continue?', | ||
) | ||
} | ||
this.console.all(wallet.getPrivateKeyString()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters