Skip to content

Commit e722119

Browse files
authored
Avoid connect handshake and handle all error cases (#5512)
This adds new documentation about what and which errors are thrown. It also removes appInfo() and getVersion() during tryInstruction because it duplicates the error cdoes you get from just running any app commands. This also transforms ledger app errors from TransportStatusError into ResponseError so we can handle all the error codes in a single way.
1 parent 27db10e commit e722119

File tree

2 files changed

+47
-47
lines changed

2 files changed

+47
-47
lines changed

ironfish-cli/src/ledger/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Ledger
2+
3+
#### IronfishApp.appInfo() (OS CLA)
4+
C APP
5+
If Dashboard Open:
6+
If Locked: throw 0x5515 DeviceLocked
7+
If Unlocked: throw 0x6e01 (APP NOT OPEN)
8+
If App Open:
9+
If Locked: throw 0x5515 Device Locked
10+
If Unlocked: returns successfully
11+
RUST APP (OS RPC)
12+
If Dashboard Open:
13+
If Locked: throw 0x5515 DeviceLocked
14+
If Unlocked: throw 0x6e01 (APP NOT OPEN)
15+
If App Open:
16+
If Locked: throw INS_NOT_SUPPORTED
17+
If Unlocked: returns successfully
18+
19+
##### IronfishApp.getVersion (APP CLA)
20+
C APP
21+
If Dashboard Open:
22+
If Locked: throw 0x5515 DeviceLocked
23+
If Unlocked: throw 0x6e01 (APP NOT OPEN)
24+
If App Open:
25+
If Locked: throw 0x5515 Device Locked
26+
If Unlocked: returns successfully
27+
RUST APP
28+
If Dashboard Open:
29+
If Locked: throw 0x5515 DeviceLocked
30+
If Unlocked: throw 0x6e01 (APP NOT OPEN)
31+
If App Open:
32+
If Locked: throw INS_NOT_SUPPORTED
33+
If Unlocked: returns successfully()

ironfish-cli/src/ledger/ledger.ts

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { ResponseError, Transport } from '@zondax/ledger-js'
1515
export const IronfishLedgerStatusCodes = {
1616
...StatusCodes,
1717
COMMAND_NOT_ALLOWED: 0x6986,
18+
APP_NOT_OPEN: 0x6e01,
19+
UNKNOWN_TRANSPORT_ERROR: 0xffff,
1820
}
1921

2022
export class Ledger {
@@ -35,61 +37,25 @@ export class Ledger {
3537

3638
Assert.isNotUndefined(this.app, 'Unable to establish connection with Ledger device')
3739

38-
const app = this.app
39-
40-
// App info is a request to the dashboard CLA. The purpose of this it to
41-
// produce a Locked Device error and works if an app is open or closed.
42-
try {
43-
await app.appInfo()
44-
} catch (error) {
45-
if (
46-
error instanceof ResponseError &&
47-
error.message.includes('Attempt to read beyond buffer length') &&
48-
error.returnCode === IronfishLedgerStatusCodes.TECHNICAL_PROBLEM
49-
) {
50-
// Catch this error and swollow it until the SDK fix merges to fix
51-
// this
52-
}
53-
}
54-
55-
// This is an app specific request. This is useful because this throws
56-
// INS_NOT_SUPPORTED in the case that the app is locked which is useful to
57-
// know versus the device is locked.
58-
try {
59-
await app.getVersion()
60-
} catch (error) {
61-
if (
62-
error instanceof ResponseError &&
63-
error.returnCode === IronfishLedgerStatusCodes.INS_NOT_SUPPORTED
64-
) {
65-
throw new LedgerAppLocked()
66-
}
40+
return await instruction(this.app)
41+
} catch (e: unknown) {
42+
let error = e
6743

68-
throw error
69-
}
70-
71-
return await instruction(app)
72-
} catch (error: unknown) {
73-
if (LedgerPortIsBusyError.IsError(error)) {
44+
if (LedgerPortIsBusyError.IsError(e)) {
7445
throw new LedgerPortIsBusyError()
75-
} else if (LedgerConnectError.IsError(error)) {
46+
} else if (LedgerConnectError.IsError(e)) {
7647
throw new LedgerConnectError()
7748
}
7849

7950
if (error instanceof TransportStatusError) {
80-
if (
81-
error.statusCode === IronfishLedgerStatusCodes.COMMAND_NOT_ALLOWED ||
82-
error.statusCode === IronfishLedgerStatusCodes.CONDITIONS_OF_USE_NOT_SATISFIED
83-
) {
84-
throw new LedgerActionRejected()
85-
} else {
86-
throw new LedgerConnectError()
87-
}
51+
error = new ResponseError(error.statusCode, error.statusText)
8852
}
8953

9054
if (error instanceof ResponseError) {
9155
if (error.returnCode === IronfishLedgerStatusCodes.LOCKED_DEVICE) {
9256
throw new LedgerDeviceLockedError()
57+
} else if (error.returnCode === IronfishLedgerStatusCodes.INS_NOT_SUPPORTED) {
58+
throw new LedgerAppLocked()
9359
} else if (error.returnCode === IronfishLedgerStatusCodes.CLA_NOT_SUPPORTED) {
9460
throw new LedgerClaNotSupportedError()
9561
} else if (error.returnCode === IronfishLedgerStatusCodes.GP_AUTH_FAILED) {
@@ -103,15 +69,16 @@ export class Ledger {
10369
throw new LedgerActionRejected()
10470
} else if (
10571
[
106-
IronfishLedgerStatusCodes.INS_NOT_SUPPORTED,
10772
IronfishLedgerStatusCodes.TECHNICAL_PROBLEM,
108-
0xffff, // Unknown transport error
109-
0x6e01, // App not open
73+
IronfishLedgerStatusCodes.UNKNOWN_TRANSPORT_ERROR,
74+
IronfishLedgerStatusCodes.APP_NOT_OPEN,
11075
].includes(error.returnCode)
11176
) {
11277
throw new LedgerAppNotOpen(
11378
`Unable to connect to Ironfish app on Ledger. Please check that the device is unlocked and the app is open.`,
11479
)
80+
} else if (e instanceof TransportStatusError) {
81+
throw new LedgerConnectError()
11582
}
11683

11784
throw new LedgerError(error.message)

0 commit comments

Comments
 (0)