From 5553a92fa9653eb53515a084d2a71021d4bae496 Mon Sep 17 00:00:00 2001 From: Hugh Cunningham Date: Fri, 20 Sep 2024 10:16:08 -0700 Subject: [PATCH] prompts for new name and retries import in round3 extracts the import/name prompt loop logic from the 'wallet:import' command into an 'importAccount' util uses the 'importAccount' util to import the account at the end of round3 so that import is retried in the case of name collisions --- ironfish-cli/src/commands/wallet/import.ts | 58 +++-------------- .../commands/wallet/multisig/dkg/round3.ts | 13 ++-- ironfish-cli/src/utils/account.ts | 63 ++++++++++++++++++- 3 files changed, 79 insertions(+), 55 deletions(-) diff --git a/ironfish-cli/src/commands/wallet/import.ts b/ironfish-cli/src/commands/wallet/import.ts index 4ff92863f9..f80fed408d 100644 --- a/ironfish-cli/src/commands/wallet/import.ts +++ b/ironfish-cli/src/commands/wallet/import.ts @@ -1,17 +1,13 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -import { - AccountFormat, - encodeAccountImport, - RPC_ERROR_CODES, - RpcRequestError, -} from '@ironfish/sdk' +import { AccountFormat, encodeAccountImport } from '@ironfish/sdk' import { Args, Flags, ux } from '@oclif/core' import { IronfishCommand } from '../../command' import { RemoteFlags } from '../../flags' import { checkWalletUnlocked, inputPrompt } from '../../ui' import { importFile, importPipe, longPrompt } from '../../ui/longPrompt' +import { importAccount } from '../../utils' import { Ledger } from '../../utils/ledger' export class ImportCommand extends IronfishCommand { @@ -102,49 +98,15 @@ export class ImportCommand extends IronfishCommand { flags.name = name } - let result - - while (!result) { - try { - result = await client.wallet.importAccount({ - account, - rescan: flags.rescan, - name: flags.name, - createdAt: flags.createdAt, - }) - } catch (e) { - if ( - e instanceof RpcRequestError && - (e.code === RPC_ERROR_CODES.DUPLICATE_ACCOUNT_NAME.toString() || - e.code === RPC_ERROR_CODES.IMPORT_ACCOUNT_NAME_REQUIRED.toString() || - e.code === RPC_ERROR_CODES.DUPLICATE_IDENTITY_NAME.toString()) - ) { - const message = 'Enter a name for the account' - - if (e.code === RPC_ERROR_CODES.DUPLICATE_ACCOUNT_NAME.toString()) { - this.log() - this.log(e.codeMessage) - } - - if (e.code === RPC_ERROR_CODES.DUPLICATE_IDENTITY_NAME.toString()) { - this.log() - this.log(e.codeMessage) - } - - const name = await inputPrompt(message, true) - if (name === flags.name) { - this.error(`Entered the same name: '${name}'`) - } - - flags.name = name - continue - } - - throw e - } - } + const { name, isDefaultAccount } = await importAccount( + client, + account, + this.logger, + flags.name, + flags.createdAt, + flags.rescan, + ) - const { name, isDefaultAccount } = result.content this.log(`Account ${name} imported.`) if (isDefaultAccount) { diff --git a/ironfish-cli/src/commands/wallet/multisig/dkg/round3.ts b/ironfish-cli/src/commands/wallet/multisig/dkg/round3.ts index bd718ecd3d..a672784928 100644 --- a/ironfish-cli/src/commands/wallet/multisig/dkg/round3.ts +++ b/ironfish-cli/src/commands/wallet/multisig/dkg/round3.ts @@ -10,6 +10,7 @@ import { Flags } from '@oclif/core' import { IronfishCommand } from '../../../../command' import { RemoteFlags } from '../../../../flags' import * as ui from '../../../../ui' +import { importAccount } from '../../../../utils' import { Ledger } from '../../../../utils/ledger' export class DkgRound3Command extends IronfishCommand { @@ -216,14 +217,14 @@ export class DkgRound3Command extends IronfishCommand { } // Import multisig account - const response = await client.wallet.importAccount({ - account: encodeAccountImport(accountImport, AccountFormat.Base64Json), - }) + const { name } = await importAccount( + client, + encodeAccountImport(accountImport, AccountFormat.Base64Json), + this.logger, + ) this.log() - this.log( - `Account ${response.content.name} imported with public address: ${dkgKeys.publicAddress}`, - ) + this.log(`Account ${name} imported with public address: ${dkgKeys.publicAddress}`) this.log() this.log('Creating an encrypted backup of multisig keys from your Ledger device...') diff --git a/ironfish-cli/src/utils/account.ts b/ironfish-cli/src/utils/account.ts index a867d86707..2f0f20dbb4 100644 --- a/ironfish-cli/src/utils/account.ts +++ b/ironfish-cli/src/utils/account.ts @@ -2,8 +2,15 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -import { RpcClient } from '@ironfish/sdk' +import { + ImportResponse, + Logger, + RPC_ERROR_CODES, + RpcClient, + RpcRequestError, +} from '@ironfish/sdk' import * as ui from '../ui' +import { inputPrompt } from '../ui' export async function useAccount( client: RpcClient, @@ -27,3 +34,57 @@ export async function useAccount( return ui.accountPrompt(client, message) } + +export async function importAccount( + client: RpcClient, + account: string, + logger: Logger, + accountName?: string, + createdAt?: number, + rescan?: boolean, +): Promise { + let name = accountName + + let result + while (!result) { + try { + result = await client.wallet.importAccount({ + account, + name, + rescan, + createdAt, + }) + } catch (e) { + if ( + e instanceof RpcRequestError && + (e.code === RPC_ERROR_CODES.DUPLICATE_ACCOUNT_NAME.toString() || + e.code === RPC_ERROR_CODES.IMPORT_ACCOUNT_NAME_REQUIRED.toString() || + e.code === RPC_ERROR_CODES.DUPLICATE_IDENTITY_NAME.toString()) + ) { + const message = 'Enter a name for the account' + + if (e.code === RPC_ERROR_CODES.DUPLICATE_ACCOUNT_NAME.toString()) { + logger.info('') + logger.info(e.codeMessage) + } + + if (e.code === RPC_ERROR_CODES.DUPLICATE_IDENTITY_NAME.toString()) { + logger.info('') + logger.info(e.codeMessage) + } + + const inputName = await inputPrompt(message, true) + if (inputName === name) { + throw new Error(`Entered the same name: '${name}'`) + } + + name = inputName + continue + } + + throw e + } + } + + return result.content +}