Skip to content
This repository has been archived by the owner on Jun 27, 2022. It is now read-only.

Expose a way to get Model from targetId #621

Merged
merged 3 commits into from
Jun 11, 2021
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
18 changes: 16 additions & 2 deletions packages/devices/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ Logic for all Ledger devices.
- [ledgerUSBVendorId](#ledgerusbvendorid)
- [getDeviceModel](#getdevicemodel)
- [Parameters](#parameters-1)
- [identifyUSBProductId](#identifyusbproductid)
- [identifyTargetId](#identifytargetid)
- [Parameters](#parameters-2)
- [identifyUSBProductId](#identifyusbproductid)
- [Parameters](#parameters-3)
- [getBluetoothServiceUuids](#getbluetoothserviceuuids)
- [getInfosForServiceUuid](#getinfosforserviceuuid)
- [Parameters](#parameters-3)
- [Parameters](#parameters-4)
- [DeviceModelId](#devicemodelid)
- [DeviceModel](#devicemodel)
- [Properties](#properties)
Expand Down Expand Up @@ -64,6 +66,17 @@ Type: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Globa

Returns **[DeviceModel](#devicemodel)**

### identifyTargetId

Given a `targetId`, return the deviceModel associated to it,
based on the first two bytes.

#### Parameters

- `targetId` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**

Returns **[DeviceModel](#devicemodel)?**

### identifyUSBProductId

#### Parameters
Expand Down Expand Up @@ -96,6 +109,7 @@ Type: $Keys<any>
- `legacyUsbProductId` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**
- `usbOnly` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
- `memorySize` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**
- `masks` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)>**
- `getBlockSize` **function (firmwareVersion: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)): [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**
- `bluetoothSpec` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<{serviceUuid: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), writeUuid: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), notifyUuid: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)}>?**

Expand Down
16 changes: 16 additions & 0 deletions packages/devices/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const devices = {
usbOnly: true,
memorySize: 480 * 1024,
blockSize: 4 * 1024,
masks: [0x31000000, 0x31010000],
getBlockSize: (_firwareVersion: string): number => 4 * 1024,
},
nanoS: {
Expand All @@ -43,6 +44,7 @@ const devices = {
usbOnly: true,
memorySize: 320 * 1024,
blockSize: 4 * 1024,
masks: [0x31100000],
getBlockSize: (firmwareVersion: string): number =>
semver.lt(semver.coerce(firmwareVersion), "2.0.0") ? 4 * 1024 : 2 * 1024,
},
Expand All @@ -54,6 +56,7 @@ const devices = {
usbOnly: false,
memorySize: 2 * 1024 * 1024,
blockSize: 4 * 1024,
masks: [0x33000000],
getBlockSize: (_firwareVersion: string): number => 4 * 1024,
bluetoothSpec: [
{
Expand Down Expand Up @@ -94,6 +97,18 @@ export const getDeviceModel = (id: DeviceModelId): DeviceModel => {
return info;
};

/**
* Given a `targetId`, return the deviceModel associated to it,
* based on the first two bytes.
*/
export const identifyTargetId = (targetId: number): ?DeviceModel => {
const deviceModel = devicesList.find(({ masks }) =>
masks.find((mask) => (targetId & 0xffff0000) === mask)
);

return deviceModel;
};

/**
*
*/
Expand Down Expand Up @@ -158,6 +173,7 @@ export type DeviceModel = {
legacyUsbProductId: number,
usbOnly: boolean,
memorySize: number,
masks: Array<number>,
// blockSize: number, // THIS FIELD IS DEPRECATED, use getBlockSize
getBlockSize: (firmwareVersion: string) => number,
bluetoothSpec?: Array<{
Expand Down
23 changes: 23 additions & 0 deletions packages/devices/tests/identifyTargetId.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// @flow
import { identifyTargetId } from "../src/";

test("check that known targetIds match known devices", () => {
const knownTargetIds = {
nanoS: [823132162, 823132163, 823132164],
nanoX: [855638020],
blue: [822149124],
};

for (const modelId in knownTargetIds) {
const ids = knownTargetIds[modelId];
ids.forEach((id) => {
const deviceModel = identifyTargetId(id);
expect(deviceModel?.id).toBe(modelId);
});
}
});

test("check that unknown targetId is undefined", () => {
const shouldBeUndefiend = identifyTargetId(0x123456789);
expect(shouldBeUndefiend?.id).toBeFalsy();
});