-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ (core) [DSDK-288]: Implement
GetAppAndVersion
command (#73)
- Loading branch information
Showing
5 changed files
with
185 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@ledgerhq/device-sdk-core": minor | ||
--- | ||
|
||
Implement GetAppAndVersion command. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
packages/core/src/api/command/os/GetAppAndVersionCommand.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { Command } from "@api/command/Command"; | ||
import { ApduResponse } from "@internal/device-session/model/ApduResponse"; | ||
|
||
import { | ||
GetAppAndVersionCommand, | ||
GetAppAndVersionResponse, | ||
} from "./GetAppAndVersionCommand"; | ||
|
||
const GET_APP_AND_VERSION_APDU = Uint8Array.from([ | ||
0xb0, 0x01, 0x00, 0x00, 0x00, | ||
]); | ||
|
||
const OS_RESPONSE_HEX = Uint8Array.from([ | ||
0x01, 0x05, 0x42, 0x4f, 0x4c, 0x4f, 0x53, 0x09, 0x31, 0x2e, 0x34, 0x2e, 0x30, | ||
0x2d, 0x72, 0x63, 0x32, 0x90, 0x00, | ||
]); | ||
|
||
const APP_RESPONSE_HEX = Uint8Array.from([ | ||
0x01, 0x07, 0x42, 0x69, 0x74, 0x63, 0x6f, 0x69, 0x6e, 0x0b, 0x32, 0x2e, 0x31, | ||
0x2e, 0x35, 0x2d, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x01, 0x02, 0x90, 0x00, | ||
]); | ||
|
||
const FAILED_RESPONSE_HEX = Uint8Array.from([0x67, 0x00]); | ||
|
||
const ERROR_RESPONSE_HEX = Uint8Array.from([0x04, 0x90, 0x00]); | ||
|
||
describe("GetAppAndVersionCommand", () => { | ||
let command: Command<void, GetAppAndVersionResponse>; | ||
|
||
beforeEach(() => { | ||
command = new GetAppAndVersionCommand(); | ||
}); | ||
|
||
describe("getApdu", () => { | ||
it("should return the GetAppAndVersion APDU", () => { | ||
const apdu = command.getApdu(); | ||
expect(apdu.getRawApdu()).toStrictEqual(GET_APP_AND_VERSION_APDU); | ||
}); | ||
}); | ||
|
||
describe("parseResponse", () => { | ||
it("should parse the response when launching OS (dashboard)", () => { | ||
const OS_RESPONSE = new ApduResponse({ | ||
statusCode: OS_RESPONSE_HEX.slice(-2), | ||
data: OS_RESPONSE_HEX.slice(0, -2), | ||
}); | ||
const parsed = command.parseResponse(OS_RESPONSE); | ||
const expected = { | ||
name: "BOLOS", | ||
version: "1.4.0-rc2", | ||
}; | ||
expect(parsed).toStrictEqual(expected); | ||
}); | ||
it("should parse the response when launching App", () => { | ||
const APP_RESPONSE = new ApduResponse({ | ||
statusCode: APP_RESPONSE_HEX.slice(-2), | ||
data: APP_RESPONSE_HEX.slice(0, -2), | ||
}); | ||
const parsed = command.parseResponse(APP_RESPONSE); | ||
const expected = { | ||
name: "Bitcoin", | ||
version: "2.1.5-alpha", | ||
flags: Uint8Array.from([2]), | ||
}; | ||
expect(parsed).toStrictEqual(expected); | ||
}); | ||
it("should throw an error if the command failed", () => { | ||
const FAILED_RESPONSE = new ApduResponse({ | ||
statusCode: FAILED_RESPONSE_HEX.slice(-2), | ||
data: FAILED_RESPONSE_HEX.slice(0, -2), | ||
}); | ||
|
||
expect(() => command.parseResponse(FAILED_RESPONSE)).toThrow( | ||
"Unexpected status word: 6700", | ||
); | ||
}); | ||
it("should throw an error if the response returned unsupported format", () => { | ||
const ERROR_RESPONSE = new ApduResponse({ | ||
statusCode: ERROR_RESPONSE_HEX.slice(-2), | ||
data: ERROR_RESPONSE_HEX.slice(0, -2), | ||
}); | ||
|
||
expect(() => command.parseResponse(ERROR_RESPONSE)).toThrow( | ||
"getAppAndVersion: format not supported", | ||
); | ||
}); | ||
}); | ||
}); |
53 changes: 53 additions & 0 deletions
53
packages/core/src/api/command/os/GetAppAndVersionCommand.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Apdu } from "@api/apdu/model/Apdu"; | ||
import { ApduBuilder, ApduBuilderArgs } from "@api/apdu/utils/ApduBuilder"; | ||
import { ApduParser } from "@api/apdu/utils/ApduParser"; | ||
import { Command } from "@api/command/Command"; | ||
import { CommandUtils } from "@api/command/utils/CommandUtils"; | ||
import { ApduResponse } from "@internal/device-session/model/ApduResponse"; | ||
|
||
export type GetAppAndVersionResponse = { | ||
name: string; | ||
version: string; | ||
flags?: number | Uint8Array; | ||
}; | ||
|
||
export class GetAppAndVersionCommand | ||
implements Command<void, GetAppAndVersionResponse> | ||
{ | ||
getApdu(): Apdu { | ||
const getAppAndVersionApduArgs: ApduBuilderArgs = { | ||
cla: 0xb0, | ||
ins: 0x01, | ||
p1: 0x00, | ||
p2: 0x00, | ||
} as const; | ||
return new ApduBuilder(getAppAndVersionApduArgs).build(); | ||
} | ||
|
||
parseResponse(apduResponse: ApduResponse): GetAppAndVersionResponse { | ||
const parser = new ApduParser(apduResponse); | ||
// [SHOULD] Implement new error treatment logic | ||
if (!CommandUtils.isSuccessResponse(apduResponse)) { | ||
throw new Error( | ||
`Unexpected status word: ${parser.encodeToHexaString( | ||
apduResponse.statusCode, | ||
)}`, | ||
); | ||
} | ||
|
||
if (parser.extract8BitUint() !== 1) { | ||
// TODO: Make dedicated error object | ||
throw new Error("getAppAndVersion: format not supported"); | ||
} | ||
|
||
const name = parser.encodeToString(parser.extractFieldLVEncoded()); | ||
const version = parser.encodeToString(parser.extractFieldLVEncoded()); | ||
|
||
if (parser.getUnparsedRemainingLength() === 0) { | ||
return { name, version } as GetAppAndVersionResponse; | ||
} | ||
|
||
const flags = parser.extractFieldLVEncoded(); | ||
return { name, version, flags } as GetAppAndVersionResponse; | ||
} | ||
} |