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

Move ledger UI functions into UI #5486

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions ironfish-cli/src/commands/wallet/chainport/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { Flags, ux } from '@oclif/core'
import inquirer from 'inquirer'
import { IronfishCommand } from '../../../command'
import { HexFlag, IronFlag, RemoteFlags, ValueFlag } from '../../../flags'
import { sendTransactionWithLedger } from '../../../ledger'
import * as ui from '../../../ui'
import {
ChainportBridgeTransaction,
Expand Down Expand Up @@ -135,7 +134,7 @@ export class BridgeCommand extends IronfishCommand {
}

if (flags.ledger) {
await sendTransactionWithLedger(
await ui.sendTransactionWithLedger(
client,
rawTransaction,
from,
Expand Down
3 changes: 1 addition & 2 deletions ironfish-cli/src/commands/wallet/mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
import { Flags, ux } from '@oclif/core'
import { IronfishCommand } from '../../command'
import { IronFlag, RemoteFlags, ValueFlag } from '../../flags'
import { sendTransactionWithLedger } from '../../ledger'
import * as ui from '../../ui'
import { useAccount } from '../../utils'
import { promptCurrency } from '../../utils/currency'
Expand Down Expand Up @@ -314,7 +313,7 @@ This will create tokens and increase supply for a given asset.`
)

if (flags.ledger) {
await sendTransactionWithLedger(
await ui.sendTransactionWithLedger(
client,
raw,
account,
Expand Down
3 changes: 1 addition & 2 deletions ironfish-cli/src/commands/wallet/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
import { Flags } from '@oclif/core'
import { IronfishCommand } from '../../command'
import { HexFlag, IronFlag, RemoteFlags, ValueFlag } from '../../flags'
import { sendTransactionWithLedger } from '../../ledger'
import * as ui from '../../ui'
import { useAccount } from '../../utils'
import { promptCurrency } from '../../utils/currency'
Expand Down Expand Up @@ -258,7 +257,7 @@ export class Send extends IronfishCommand {
}

if (flags.ledger) {
await sendTransactionWithLedger(
await ui.sendTransactionWithLedger(
client,
raw,
from,
Expand Down
1 change: 0 additions & 1 deletion ironfish-cli/src/ledger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
export * from './ledger'
export * from './ledgerMultiSigner'
export * from './ledgerSingleSigner'
export * from './ui'
98 changes: 0 additions & 98 deletions ironfish-cli/src/ledger/ui.ts

This file was deleted.

98 changes: 96 additions & 2 deletions ironfish-cli/src/ui/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@
* 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 { PromiseUtils } from '@ironfish/sdk'
import { ux } from '@oclif/core'
import {
CurrencyUtils,
Logger,
PromiseUtils,
RawTransaction,
RawTransactionSerde,
RpcClient,
Transaction,
} from '@ironfish/sdk'
import { Errors, ux } from '@oclif/core'
import inquirer from 'inquirer'
import {
Ledger,
Expand All @@ -14,7 +22,10 @@ import {
LedgerDeviceLockedError,
LedgerGPAuthFailed,
LedgerPortIsBusyError,
LedgerSingleSigner,
} from '../ledger'
import * as ui from '../ui'
import { watchTransaction } from '../utils/transaction'

export async function ledger<TResult>({
ledger,
Expand Down Expand Up @@ -101,3 +112,86 @@ export async function ledger<TResult>({
}
}
}

export async function sendTransactionWithLedger(
client: RpcClient,
raw: RawTransaction,
from: string | undefined,
watch: boolean,
confirm: boolean,
logger?: Logger,
): Promise<void> {
const ledger = new LedgerSingleSigner(logger)

try {
await ledger.connect()
} catch (e) {
if (e instanceof Error) {
Errors.error(e.message)
} else {
throw e
}
}

const publicKey = (await client.wallet.getAccountPublicKey({ account: from })).content
.publicKey

const ledgerPublicKey = await ledger.getPublicAddress()

if (publicKey !== ledgerPublicKey) {
Errors.error(
`The public key on the ledger device does not match the public key of the account '${from}'`,
)
}

const buildTransactionResponse = await client.wallet.buildTransaction({
account: from,
rawTransaction: RawTransactionSerde.serialize(raw).toString('hex'),
})

const unsignedTransaction = buildTransactionResponse.content.unsignedTransaction

ux.stdout('Please confirm the transaction on your Ledger device')

const signature = (await ledger.sign(unsignedTransaction)).toString('hex')

ux.stdout(`\nSignature: ${signature}`)

const addSignatureResponse = await client.wallet.addSignature({
unsignedTransaction,
signature,
})

const signedTransaction = addSignatureResponse.content.transaction
const bytes = Buffer.from(signedTransaction, 'hex')

const transaction = new Transaction(bytes)

ux.stdout(`\nSigned Transaction: ${signedTransaction}`)
ux.stdout(`\nHash: ${transaction.hash().toString('hex')}`)
ux.stdout(`Fee: ${CurrencyUtils.render(transaction.fee(), true)}`)

await ui.confirmOrQuit('Would you like to broadcast this transaction?', confirm)

const addTransactionResponse = await client.wallet.addTransaction({
transaction: signedTransaction,
broadcast: true,
})

if (addTransactionResponse.content.accepted === false) {
Errors.error(
`Transaction '${transaction.hash().toString('hex')}' was not accepted into the mempool`,
)
}

if (watch) {
ux.stdout('')

await watchTransaction({
client,
logger,
account: from,
hash: transaction.hash().toString('hex'),
})
}
}