Skip to content

Commit

Permalink
reviews tx on InvalidTxHash error
Browse files Browse the repository at this point in the history
updates dkgGetCommitments and dkgSign to catch Ledger error caused by unreviewed
transaction (InvalidTxHash), call reviewTransaction, and finally recursively
call themselves

adds error type for LedgerInvalidTxHash
  • Loading branch information
hughy committed Oct 8, 2024
1 parent 60e7f3b commit 3bba940
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
2 changes: 2 additions & 0 deletions ironfish-cli/src/commands/wallet/multisig/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ export class SignMultisigTransactionCommand extends IronfishCommand {
const frostSignatureShare = await ui.ledger({
ledger,
message: 'Sign Transaction',
approval: true,
action: () =>
ledger.dkgSign(
unsignedTransaction,
Expand Down Expand Up @@ -541,6 +542,7 @@ export class SignMultisigTransactionCommand extends IronfishCommand {
const rawCommitments = await ui.ledger({
ledger,
message: 'Get Commitments',
approval: true,
action: () => ledger.dkgGetCommitments(unsignedTransaction),
})

Expand Down
4 changes: 4 additions & 0 deletions ironfish-cli/src/ledger/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const IronfishLedgerStatusCodes = {
COMMAND_NOT_ALLOWED: 0x6986,
APP_NOT_OPEN: 0x6e01,
UNKNOWN_TRANSPORT_ERROR: 0xffff,
INVALID_TX_HASH: 0xb025,
}

export class Ledger {
Expand Down Expand Up @@ -77,6 +78,8 @@ export class Ledger {
throw new LedgerAppNotOpen(
`Unable to connect to Ironfish app on Ledger. Please check that the device is unlocked and the app is open.`,
)
} else if (error.returnCode === IronfishLedgerStatusCodes.INVALID_TX_HASH) {
throw new LedgerInvalidTxHash()
} else if (e instanceof TransportStatusError) {
throw new LedgerConnectError()
}
Expand Down Expand Up @@ -161,3 +164,4 @@ export class LedgerGPAuthFailed extends LedgerError {}
export class LedgerClaNotSupportedError extends LedgerError {}
export class LedgerAppNotOpen extends LedgerError {}
export class LedgerActionRejected extends LedgerError {}
export class LedgerInvalidTxHash extends LedgerError {}
51 changes: 37 additions & 14 deletions ironfish-cli/src/ledger/ledgerMultiSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import {
ResponseDkgRound1,
ResponseDkgRound2,
} from '@zondax/ledger-ironfish'
import { isResponseAddress, isResponseProofGenKey, isResponseViewKey, Ledger } from './ledger'
import {
isResponseAddress,
isResponseProofGenKey,
isResponseViewKey,
Ledger,
LedgerInvalidTxHash,
} from './ledger'

export class LedgerMultiSigner extends Ledger {
constructor() {
Expand Down Expand Up @@ -112,25 +118,42 @@ export class LedgerMultiSigner extends Ledger {
}

dkgGetCommitments = async (transaction: UnsignedTransaction): Promise<Buffer> => {
const { commitments } = await this.tryInstruction(async (app) => {
const { hash } = await app.reviewTransaction(transaction.serialize().toString('hex'))
return app.dkgGetCommitments(hash.toString('hex'))
})

return commitments
try {
const { commitments } = await this.tryInstruction(async (app) => {
return app.dkgGetCommitments(transaction.hash().toString('hex'))
})
return commitments
} catch (e) {
if (e instanceof LedgerInvalidTxHash) {
await this.reviewTransaction(transaction.serialize().toString('hex'))
return this.dkgGetCommitments(transaction)
}

throw e
}
}

dkgSign = async (
transaction: UnsignedTransaction,
frostSigningPackage: string,
): Promise<Buffer> => {
const randomness = transaction.publicKeyRandomness()
const { signature } = await this.tryInstruction(async (app) => {
const { hash } = await app.reviewTransaction(transaction.serialize().toString('hex'))
return app.dkgSign(randomness, frostSigningPackage, hash.toString('hex'))
})

return signature
try {
const { signature } = await this.tryInstruction(async (app) => {
return app.dkgSign(
transaction.publicKeyRandomness(),
frostSigningPackage,
transaction.hash().toString('hex'),
)
})
return signature
} catch (e) {
if (e instanceof LedgerInvalidTxHash) {
await this.reviewTransaction(transaction.serialize().toString('hex'))
return this.dkgSign(transaction, frostSigningPackage)
}

throw e
}
}

dkgBackupKeys = async (): Promise<Buffer> => {
Expand Down

0 comments on commit 3bba940

Please sign in to comment.