/
index.ts
238 lines (221 loc) · 6.38 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import semver from "semver";
/**
* The USB product IDs will be defined as MMII, encoding a model (MM) and an interface bitfield (II)
*
** Model
* Ledger Nano S : 0x10
* Ledger Blue : 0x00
* Ledger Nano X : 0x40
*
** Interface support bitfield
* Generic HID : 0x01
* Keyboard HID : 0x02
* U2F : 0x04
* CCID : 0x08
* WebUSB : 0x10
*/
export const IIGenericHID = 0x01;
export const IIKeyboardHID = 0x02;
export const IIU2F = 0x04;
export const IICCID = 0x08;
export const IIWebUSB = 0x10;
export enum DeviceModelId {
blue = "blue",
nanoS = "nanoS",
nanoSP = "nanoSP",
nanoX = "nanoX",
stax = "stax",
europa = "europa",
}
const devices: { [key in DeviceModelId]: DeviceModel } = {
[DeviceModelId.blue]: {
id: DeviceModelId.blue,
productName: "Ledger Blue",
productIdMM: 0x00,
legacyUsbProductId: 0x0000,
usbOnly: true,
memorySize: 480 * 1024,
masks: [0x31000000, 0x31010000],
getBlockSize: (_firwareVersion: string): number => 4 * 1024,
},
[DeviceModelId.nanoS]: {
id: DeviceModelId.nanoS,
productName: "Ledger Nano S",
productIdMM: 0x10,
legacyUsbProductId: 0x0001,
usbOnly: true,
memorySize: 320 * 1024,
masks: [0x31100000],
getBlockSize: (firmwareVersion: string): number =>
semver.lt(semver.coerce(firmwareVersion) ?? "", "2.0.0") ? 4 * 1024 : 2 * 1024,
},
[DeviceModelId.nanoX]: {
id: DeviceModelId.nanoX,
productName: "Ledger Nano X",
productIdMM: 0x40,
legacyUsbProductId: 0x0004,
usbOnly: false,
memorySize: 2 * 1024 * 1024,
masks: [0x33000000],
getBlockSize: (_firwareVersion: string): number => 4 * 1024,
bluetoothSpec: [
{
serviceUuid: "13d63400-2c97-0004-0000-4c6564676572",
notifyUuid: "13d63400-2c97-0004-0001-4c6564676572",
writeUuid: "13d63400-2c97-0004-0002-4c6564676572",
writeCmdUuid: "13d63400-2c97-0004-0003-4c6564676572",
},
],
},
[DeviceModelId.nanoSP]: {
id: DeviceModelId.nanoSP,
productName: "Ledger Nano S Plus",
productIdMM: 0x50,
legacyUsbProductId: 0x0005,
usbOnly: true,
memorySize: 1533 * 1024,
masks: [0x33100000],
getBlockSize: (_firmwareVersion: string): number => 32,
},
[DeviceModelId.stax]: {
id: DeviceModelId.stax,
productName: "Ledger Stax",
productIdMM: 0x60,
legacyUsbProductId: 0x0006,
usbOnly: false,
memorySize: 1533 * 1024,
masks: [0x33200000],
getBlockSize: (_firmwareVersion: string): number => 32,
bluetoothSpec: [
{
serviceUuid: "13d63400-2c97-6004-0000-4c6564676572",
notifyUuid: "13d63400-2c97-6004-0001-4c6564676572",
writeUuid: "13d63400-2c97-6004-0002-4c6564676572",
writeCmdUuid: "13d63400-2c97-6004-0003-4c6564676572",
},
],
},
[DeviceModelId.europa]: {
id: DeviceModelId.europa, // TODO: change to the real product's name one day
productName: "Ledger Europa", // TODO: change to the real product's name one day
productIdMM: 0x70,
legacyUsbProductId: 0x0007,
usbOnly: false,
memorySize: 1533 * 1024,
masks: [0x33300000],
getBlockSize: (_firmwareVersion: string): number => 32,
bluetoothSpec: [
{
serviceUuid: "13d63400-2c97-3004-0000-4c6564676572",
notifyUuid: "13d63400-2c97-3004-0001-4c6564676572",
writeUuid: "13d63400-2c97-3004-0002-4c6564676572",
writeCmdUuid: "13d63400-2c97-3004-0003-4c6564676572",
},
],
},
};
const productMap = {
Blue: DeviceModelId.blue,
"Nano S": DeviceModelId.nanoS,
"Nano S Plus": DeviceModelId.nanoSP,
"Nano X": DeviceModelId.nanoX,
Stax: DeviceModelId.stax,
Europa: DeviceModelId.europa,
};
const devicesList: DeviceModel[] = Object.values(devices);
/**
*
*/
export const ledgerUSBVendorId = 0x2c97;
/**
*
*/
export const getDeviceModel = (id: DeviceModelId): DeviceModel => {
const info = devices[id];
if (!info) throw new Error("device '" + id + "' does not exist");
return info;
};
/**
* Given a `targetId`, return the deviceModel associated to it,
* based on the first two bytes.
*/
export const identifyTargetId = (targetId: number): DeviceModel | null | undefined => {
const deviceModel = devicesList.find(({ masks }) =>
masks.find(mask => (targetId & 0xffff0000) === mask),
);
return deviceModel;
};
/**
* From a given USB product id, return the deviceModel associated to it.
*
* The mapping from the product id is only based on the 2 most significant bytes.
* For example, Stax is defined with a product id of 0x60ii, a product id 0x6011 would be mapped to it.
*/
export const identifyUSBProductId = (usbProductId: number): DeviceModel | null | undefined => {
const legacy = devicesList.find(d => d.legacyUsbProductId === usbProductId);
if (legacy) return legacy;
const mm = usbProductId >> 8;
const deviceModel = devicesList.find(d => d.productIdMM === mm);
return deviceModel;
};
export const identifyProductName = (productName: string): DeviceModel | null | undefined => {
const deviceModel = devicesList.find(d => d.id === productMap[productName]);
return deviceModel;
};
const bluetoothServices: string[] = [];
const serviceUuidToInfos: Record<string, BluetoothInfos> = {};
for (const id in devices) {
const deviceModel = devices[id];
const { bluetoothSpec } = deviceModel;
if (bluetoothSpec) {
for (let i = 0; i < bluetoothSpec.length; i++) {
const spec = bluetoothSpec[i];
bluetoothServices.push(spec.serviceUuid);
serviceUuidToInfos[spec.serviceUuid] = serviceUuidToInfos[
spec.serviceUuid.replace(/-/g, "")
] = {
deviceModel,
...spec,
};
}
}
}
/**
*
*/
export const getBluetoothServiceUuids = (): string[] => bluetoothServices;
/**
*
*/
export const getInfosForServiceUuid = (uuid: string): BluetoothInfos | undefined =>
serviceUuidToInfos[uuid.toLowerCase()];
/**
*
*/
export interface DeviceModel {
id: DeviceModelId;
productName: string;
productIdMM: number;
legacyUsbProductId: number;
usbOnly: boolean;
memorySize: number;
masks: number[];
// blockSize: number, // THIS FIELD IS DEPRECATED, use getBlockSize
getBlockSize: (firmwareVersion: string) => number;
bluetoothSpec?: {
serviceUuid: string;
writeUuid: string;
writeCmdUuid: string;
notifyUuid: string;
}[];
}
/**
*
*/
export interface BluetoothInfos {
deviceModel: DeviceModel;
serviceUuid: string;
writeUuid: string;
writeCmdUuid: string;
notifyUuid: string;
}