Skip to content

Commit

Permalink
repeat long prompt for account import (#4721)
Browse files Browse the repository at this point in the history
* large prompt for account import

* repeat until user passes an input

* large prompt follows cli ux required interface

* using recursion

* creating readline interface out of the recursive loop

* updates to use recursive method

* fixing import name

---------

Co-authored-by: Joe <[email protected]>
  • Loading branch information
patnir and jowparks authored Feb 13, 2024
1 parent 2d90d90 commit 00811ee
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
12 changes: 4 additions & 8 deletions ironfish-cli/src/commands/wallet/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { PromiseUtils, RPC_ERROR_CODES, RpcRequestError } from '@ironfish/sdk'
import { CliUx, Flags } from '@oclif/core'
import { IronfishCommand } from '../../command'
import { RemoteFlags } from '../../flags'
import { longPrompt } from '../../utils/longPrompt'

export class ImportCommand extends IronfishCommand {
static description = `Import an account`
Expand Down Expand Up @@ -149,13 +150,8 @@ export class ImportCommand extends IronfishCommand {
}

async importTTY(): Promise<string> {
const userInput = await CliUx.ux.prompt(
'Paste the output of wallet:export, or your spending key',
{
required: true,
},
)

return userInput.trim()
return await longPrompt('Paste the output of wallet:export, or your spending key: ', {
required: true,
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { Flags } from '@oclif/core'
import { IronfishCommand } from '../../../command'
import { RemoteFlags } from '../../../flags'
import { largePrompt } from '../../../utils/longPrompt'
import { longPrompt } from '../../../utils/longPrompt'

export class CreateSignatureShareCommand extends IronfishCommand {
static description = `Creates a signature share for a participant for a given transaction`
Expand Down Expand Up @@ -36,11 +36,11 @@ export class CreateSignatureShareCommand extends IronfishCommand {
let signingPackage = flags.signingPackage?.trim()

if (!unsignedTransaction) {
unsignedTransaction = await largePrompt('Enter the unsigned transaction: ')
unsignedTransaction = await longPrompt('Enter the unsigned transaction: ')
}

if (!signingPackage) {
signingPackage = await largePrompt('Enter the signing package: ')
signingPackage = await longPrompt('Enter the signing package: ')
}

const client = await this.sdk.connectRpc()
Expand Down
29 changes: 22 additions & 7 deletions ironfish-cli/src/utils/longPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,31 @@
import readline from 'readline'

// Most effective way to take in a large textual prompt input without affecting UX
export function largePrompt(question: string): Promise<string> {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
export async function longPrompt(
question: string,
options?: {
required?: boolean
},
readlineInterface?: readline.Interface,
): Promise<string> {
const rl =
readlineInterface ||
readline.createInterface({
input: process.stdin,
output: process.stdout,
})

return new Promise((resolve) => {
const userInput = await new Promise<string>((resolve) => {
rl.question(question, (answer) => {
rl.close()
resolve(answer.trim())
})
})

if (userInput.length === 0 && options?.required) {
return longPrompt(question, options, rl)
}

rl.close()

return userInput
}

0 comments on commit 00811ee

Please sign in to comment.