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 +}