Skip to content

Commit

Permalink
feat(cli): Hide passphrases in encryption commands (#5343)
Browse files Browse the repository at this point in the history
  • Loading branch information
rohanjadvani committed Aug 28, 2024
1 parent d5868c8 commit d456d62
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
4 changes: 3 additions & 1 deletion ironfish-cli/src/commands/wallet/decrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export class DecryptCommand extends IronfishCommand {

let passphrase = flags.passphrase
if (!passphrase) {
passphrase = await inputPrompt('Enter a passphrase to decrypt the wallet', true)
passphrase = await inputPrompt('Enter a passphrase to decrypt the wallet', true, {
password: true,
})
}

try {
Expand Down
18 changes: 17 additions & 1 deletion ironfish-cli/src/commands/wallet/encrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export class EncryptCommand extends IronfishCommand {
passphrase: Flags.string({
description: 'Passphrase to encrypt the wallet with',
}),
confirm: Flags.boolean({
description: 'Suppress the passphrase confirmation prompt',
}),
}

async start(): Promise<void> {
Expand All @@ -32,7 +35,20 @@ export class EncryptCommand extends IronfishCommand {

let passphrase = flags.passphrase
if (!passphrase) {
passphrase = await inputPrompt('Enter a passphrase to encrypt the wallet', true)
passphrase = await inputPrompt('Enter a passphrase to encrypt the wallet', true, {
password: true,
})
}

if (!flags.confirm) {
const confirmedPassphrase = await inputPrompt('Confirm your passphrase', true, {
password: true,
})

if (confirmedPassphrase !== passphrase) {
this.log('Passphrases do not match')
this.exit(1)
}
}

try {
Expand Down
4 changes: 3 additions & 1 deletion ironfish-cli/src/commands/wallet/unlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export class UnlockCommand extends IronfishCommand {

let passphrase = flags.passphrase
if (!passphrase) {
passphrase = await inputPrompt('Enter a passphrase to unlock the wallet', true)
passphrase = await inputPrompt('Enter a passphrase to unlock the wallet', true, {
password: true,
})
}

try {
Expand Down
14 changes: 9 additions & 5 deletions ironfish-cli/src/ui/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@
import { ux } from '@oclif/core'
import inquirer from 'inquirer'

async function _inputPrompt(message: string): Promise<string> {
async function _inputPrompt(message: string, options?: { password: boolean }): Promise<string> {
const result: { prompt: string } = await inquirer.prompt({
type: 'input',
type: options?.password ? 'password' : 'input',
name: 'prompt',
message: `${message}:`,
})
return result.prompt.trim()
}

export async function inputPrompt(message: string, required: boolean = false): Promise<string> {
export async function inputPrompt(
message: string,
required: boolean = false,
options?: { password: boolean },
): Promise<string> {
let userInput: string = ''

if (required) {
while (!userInput) {
userInput = await _inputPrompt(message)
userInput = await _inputPrompt(message, options)
}
} else {
userInput = await _inputPrompt(message)
userInput = await _inputPrompt(message, options)
}

return userInput
Expand Down

0 comments on commit d456d62

Please sign in to comment.