Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying account name on import using flag #3754

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 36 additions & 16 deletions ironfish-cli/src/commands/wallet/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { AccountImport } from '@ironfish/sdk/src/wallet/walletdb/accountValue'
import { CliUx, Flags } from '@oclif/core'
import { IronfishCommand } from '../../command'
import { RemoteFlags } from '../../flags'
import { CommandFlags } from '../../types'
import { LANGUAGE_VALUES } from '../../utils/language'

export class ImportCommand extends IronfishCommand {
Expand All @@ -21,7 +22,9 @@ export class ImportCommand extends IronfishCommand {
}),
path: Flags.string({
description: 'the path to the file containing the account to import',
flagName: 'path',
}),
name: Flags.string({
description: 'the name to use for the account',
}),
}

Expand All @@ -42,19 +45,19 @@ export class ImportCommand extends IronfishCommand {

let account: AccountImport
if (blob) {
account = await this.stringToAccountImport(blob)
account = await this.stringToAccountImport(blob, flags)
} else if (flags.path) {
account = await this.importFile(flags.path)
account = await this.importFile(flags.path, flags)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think we can just use flags as a single argument

} else if (process.stdin.isTTY) {
account = await this.importTTY()
account = await this.importTTY(flags)
} else if (!process.stdin.isTTY) {
account = await this.importPipe()
account = await this.importPipe(flags)
} else {
CliUx.ux.error(`Invalid import type`)
}

if (!account.version) {
account.version = ACCOUNT_SCHEMA_VERSION as number
account.version = ACCOUNT_SCHEMA_VERSION
}

if (!account.createdAt) {
Expand Down Expand Up @@ -121,7 +124,10 @@ export class ImportCommand extends IronfishCommand {
}
}

async stringToAccountImport(data: string): Promise<AccountImport> {
async stringToAccountImport(
data: string,
flags: CommandFlags<typeof ImportCommand>,
): Promise<AccountImport> {
// bech32 encoded json
const [decoded, _] = Bech32m.decode(data)
if (decoded) {
Expand All @@ -139,6 +145,10 @@ export class ImportCommand extends IronfishCommand {
data.version = 2
}

if (flags.name) {
data.name = flags.name
}

return data
}

Expand All @@ -147,9 +157,12 @@ export class ImportCommand extends IronfishCommand {
ImportCommand.mnemonicWordsToKey(data) || ImportCommand.verifySpendingKey(data)

if (spendingKey) {
const name = await CliUx.ux.prompt('Enter a new account name', {
required: true,
})
const name =
flags.name ||
(await CliUx.ux.prompt('Enter a new account name', {
required: true,
}))

const key = generateKeyFromPrivateKey(spendingKey)
return { name, version: ACCOUNT_SCHEMA_VERSION, createdAt: null, ...key }
}
Expand All @@ -170,19 +183,26 @@ export class ImportCommand extends IronfishCommand {
json.version = 2
}

if (flags.name) {
json.name = flags.name
}

return json
} catch (e) {
CliUx.ux.error(`Import failed for the given input: ${data}`)
}
}

async importFile(path: string): Promise<AccountImport> {
async importFile(
path: string,
flags: CommandFlags<typeof ImportCommand>,
): Promise<AccountImport> {
const resolved = this.sdk.fileSystem.resolve(path)
const data = await this.sdk.fileSystem.readFile(resolved)
return this.stringToAccountImport(data.trim())
return this.stringToAccountImport(data.trim(), flags)
}

async importPipe(): Promise<AccountImport> {
async importPipe(flags: CommandFlags<typeof ImportCommand>): Promise<AccountImport> {
let data = ''

const onData = (dataIn: string): void => {
Expand All @@ -198,17 +218,17 @@ export class ImportCommand extends IronfishCommand {

process.stdin.off('data', onData)

return this.stringToAccountImport(data)
return this.stringToAccountImport(data, flags)
}

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

return await this.stringToAccountImport(userInput)
return await this.stringToAccountImport(userInput, flags)
}
}