Skip to content

Commit cee2ea0

Browse files
committed
Make the transport proxy throw exceptions on every sw except 0x9000
1 parent 1ba263d commit cee2ea0

4 files changed

Lines changed: 22 additions & 8 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"@grpc/grpc-js": "^1.13.4",
4343
"@grpc/proto-loader": "^0.7.15",
4444
"@ledgerhq/hw-transport-http": "^6.30.8",
45+
"@ledgerhq/errors": "^6.23.0",
4546
"axios": "^1.11.0",
4647
"axios-retry": "^4.5.0",
4748
"dockerode": "^4.0.7",

pnpm-lock.yaml

Lines changed: 5 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Zemu.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ import {
6565
type TModel,
6666
} from './types'
6767
import { isTouchDevice, zondaxToggleBlindSigning, zondaxToggleExpertMode, zondaxTouchEnableSpecialMode } from './zondax'
68+
import {
69+
TransportStatusError,
70+
} from "@ledgerhq/errors";
71+
72+
enum ApduError {
73+
NoError = 0x9000,
74+
}
6875

6976
export default class Zemu {
7077
public startOptions!: IStartOptions
@@ -458,6 +465,11 @@ export default class Zemu {
458465
try {
459466
self.lastTransportError = null // Clear previous error
460467
const result = await target.send(cla, ins, p1, p2, data, statusList)
468+
const sw = result.readUInt16BE(result.length - 2);
469+
470+
if (sw !== ApduError.NoError) {
471+
throw new TransportStatusError(sw);
472+
}
461473
return result
462474
} catch (error) {
463475
// Store the error for later checks

tests/error-handling.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@ describe('Error Handling', () => {
2929
const validINS = 0x00
3030
const p1 = 0x00
3131
const p2 = 0x00
32+
const data = Buffer.from([])
33+
// Provide a statusList to make sure zemu throws an exception even on accepted status words
34+
const statusList = [0x9000, 0x6e00]
3235

3336
// Start timer to measure how long the error takes
3437
const startTime = Date.now()
3538

3639
try {
3740
// This should fail with CLA_NOT_SUPPORTED (0x6E00)
38-
await transport.send(invalidCLA, validINS, p1, p2)
41+
await transport.send(invalidCLA, validINS, p1, p2, data, statusList)
3942

4043
// If we get here, the test failed - we expected an error
4144
expect.fail('Expected transport.send to throw an error')

0 commit comments

Comments
 (0)