Skip to content

Commit

Permalink
import ledger account through cli (#5100)
Browse files Browse the repository at this point in the history
  • Loading branch information
patnir authored Jul 9, 2024
1 parent c80830a commit 88f33b1
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions ironfish-cli/src/commands/wallet/import.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
/* 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 { RPC_ERROR_CODES, RpcRequestError } from '@ironfish/sdk'
import {
AccountFormat,
encodeAccountImport,
RPC_ERROR_CODES,
RpcRequestError,
} from '@ironfish/sdk'
import { Args, Flags, ux } from '@oclif/core'
import { IronfishCommand } from '../../command'
import { RemoteFlags } from '../../flags'
import { importFile, importPipe, longPrompt } from '../../utils/input'
import { Ledger } from '../../utils/ledger'

export class ImportCommand extends IronfishCommand {
static description = `Import an account`
Expand All @@ -26,6 +32,11 @@ export class ImportCommand extends IronfishCommand {
createdAt: Flags.integer({
description: 'Block sequence to begin scanning from for the imported account',
}),
ledger: Flags.boolean({
description: 'import a view-only account from a ledger device',
default: false,
exclusive: ['path'],
}),
}

static args = {
Expand All @@ -43,14 +54,23 @@ export class ImportCommand extends IronfishCommand {

let account: string

if (blob && blob.length !== 0 && flags.path && flags.path.length !== 0) {
if (
blob &&
blob.length !== 0 &&
((flags.path && flags.path.length !== 0) || flags.ledger)
) {
this.error(
`Your command includes an unexpected argument. Please pass either --path or the output of wallet:export.`,
`Your command includes an unexpected argument. Please pass only 1 of the following:
1. the output of wallet:export OR
2. --path to import an account from a file OR
3. --ledger to import an account from a ledger device`,
)
}

if (blob) {
account = blob
} else if (flags.ledger) {
account = await this.importLedger()
} else if (flags.path) {
account = await importFile(this.sdk.fileSystem, flags.path)
} else if (process.stdin.isTTY) {
Expand Down Expand Up @@ -129,4 +149,19 @@ export class ImportCommand extends IronfishCommand {
this.log(`Run "ironfish wallet:use ${name}" to set the account as default`)
}
}

async importLedger(): Promise<string> {
try {
const ledger = new Ledger(this.logger)
await ledger.connect()
const account = await ledger.importAccount()
return encodeAccountImport(account, AccountFormat.Base64Json)
} catch (e) {
if (e instanceof Error) {
this.error(e.message)
} else {
this.error('Unknown error while importing account from ledger device.')
}
}
}
}

0 comments on commit 88f33b1

Please sign in to comment.