Skip to content

Commit

Permalink
feat: add utility lock and unlock commands (#464)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cafe137 committed Aug 14, 2023
1 parent df48771 commit c8b1648
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/command/utility/index.ts
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'))
}
28 changes: 28 additions & 0 deletions src/command/utility/lock.ts
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))
}
}
30 changes: 30 additions & 0 deletions src/command/utility/unlock.ts
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())
}
}
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Stake } from './command/stake'
import { Stamp } from './command/stamp'
import { Status } from './command/status'
import { Upload } from './command/upload'
import { Utility } from './command/utility'

export const beeApiUrl: IOption<string> = {
key: 'bee-api-url',
Expand Down Expand Up @@ -123,4 +124,5 @@ export const rootCommandClasses = [
Addresses,
Manifest,
Stake,
Utility,
]

0 comments on commit c8b1648

Please sign in to comment.