This repository has been archived by the owner on Jun 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 382
/
index.js
265 lines (251 loc) · 7.29 KB
/
index.js
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//@flow
import Transport, { TransportError } from "@ledgerhq/hw-transport";
import { BleManager } from "react-native-ble-plx";
const ServiceUuid = "d973f2e0-b19e-11e2-9e96-0800200c9a66";
const WriteCharacteristicUuid = "d973f2e2-b19e-11e2-9e96-0800200c9a66";
const NotifyCharacteristicUuid = "d973f2e1-b19e-11e2-9e96-0800200c9a66";
const MaxChunkBytes = 20;
const TagId = 0x05;
type Device = *;
type Characteristic = *;
function chunkBuffer(
buffer: Buffer,
sizeForIndex: number => number
): Array<Buffer> {
const chunks = [];
for (
let i = 0, size = sizeForIndex(0);
i < buffer.length;
i += size, size = sizeForIndex(i)
) {
chunks.push(buffer.slice(i, i + size));
}
return chunks;
}
function receive(characteristic, debug) {
let subscription;
const promise = new Promise((resolve, reject) => {
let notifiedIndex = 0,
notifiedDataLength = 0,
notifiedData = Buffer.alloc(0);
subscription = characteristic.monitor((error, c) => {
if (error) return reject(error);
try {
const value = Buffer.from(c.value, "base64");
if (debug) {
console.log("<=", value.toString("hex"));
}
const tag = value.readUInt8(0);
const index = value.readUInt16BE(1);
let data = value.slice(3);
if (tag !== TagId) {
throw new TransportError(
"Invalid tag " + tag.toString(16),
"InvalidTag"
);
}
if (notifiedIndex !== index) {
throw new TransportError(
"BLE: Invalid sequence number. discontinued chunk. Received " +
index +
" but expected " +
notifiedIndex,
"InvalidSequence"
);
}
if (index === 0) {
notifiedDataLength = data.readUInt16BE(0);
data = data.slice(2);
}
notifiedIndex++;
notifiedData = Buffer.concat([notifiedData, data]);
if (notifiedData.length > notifiedDataLength) {
throw new TransportError(
"BLE: received too much data. discontinued chunk. Received " +
notifiedData.length +
" but expected " +
notifiedDataLength,
"BLETooMuchData"
);
}
if (notifiedData.length === notifiedDataLength) {
resolve(notifiedData);
}
} catch (e) {
reject(e);
}
});
});
if (!subscription) throw new Error("subscription defined"); // to satisfy flow
return { promise, subscription };
}
async function send(characteristic, apdu, termination, debug) {
const chunks = chunkBuffer(apdu, i => MaxChunkBytes - (i === 0 ? 5 : 3)).map(
(buffer, i) => {
const head = Buffer.alloc(i === 0 ? 5 : 3);
head.writeUInt8(TagId, 0);
head.writeUInt16BE(i, 1);
if (i === 0) {
head.writeUInt16BE(apdu.length, 3);
}
return Buffer.concat([head, buffer]);
}
);
let terminated = false;
termination.then(() => {
terminated = true;
});
for (let chunk of chunks) {
if (terminated) return;
if (debug) {
console.log("=>", chunk.toString("hex"));
}
await characteristic.writeWithResponse(chunk.toString("base64"));
}
}
/**
* react-native bluetooth BLE implementation
* @example
* import BluetoothTransport from "@ledgerhq/react-native-hw-transport-ble";
*/
export default class BluetoothTransport extends Transport<Device> {
static isSupported = (): Promise<boolean> =>
Promise.resolve(typeof BleManager === "function");
static list = (): * => Promise.resolve([]);
/**
*/
static listen(observer: *) {
let bleManager;
try {
bleManager = new BleManager();
} catch (e) {
// basically for the tests to pass
console.warn(e);
return { unsubscribe: () => {} };
}
const unsubscribe = () => {
sub.remove();
bleManager.stopDeviceScan();
};
const onBleStateChange = (state: string) => {
if (state === "PoweredOn") {
bleManager.startDeviceScan(null, null, (bleError, device) => {
if (bleError) {
observer.error(bleError);
unsubscribe();
return;
}
// FIXME this is not the final filtering. we should eventually use the serviceUUIDs
if (device.name === "Blue" || device.id === "3A:11:99:A9:08:C4") {
observer.next({ type: "add", descriptor: device });
}
});
if (sub) sub.remove();
} else if (state === "Unsupported") {
unsubscribe();
observer.error(
new TransportError(
"Bluetooth BLE is not supported",
"BLENotSupported"
)
);
}
};
const sub = bleManager.onStateChange(onBleStateChange, true);
return { unsubscribe };
}
/**
*/
static async open(device: Device) {
await device.connect();
await device.discoverAllServicesAndCharacteristics();
/*
const services = await device.services();
for (const service of services) {
const characteristics = await service.characteristics();
}
*/
const characteristics = await device.characteristicsForService(ServiceUuid);
if (!characteristics) {
throw new TransportError("service not found", "BLEServiceNotFound");
}
let writeC, notifyC;
for (const c of characteristics) {
if (c.uuid === WriteCharacteristicUuid) {
writeC = c;
} else if (c.uuid === NotifyCharacteristicUuid) {
notifyC = c;
}
}
if (!writeC) {
throw new TransportError(
"write characteristic not found",
"BLEChracteristicNotFound"
);
}
if (!notifyC) {
throw new TransportError(
"notify characteristic not found",
"BLEChracteristicNotFound"
);
}
if (!writeC.isWritableWithResponse) {
throw new TransportError(
"write characteristic not writableWithResponse",
"BLEChracteristicInvalid"
);
}
if (!notifyC.isNotifiable) {
throw new TransportError(
"notify characteristic not notifiable",
"BLEChracteristicInvalid"
);
}
return new BluetoothTransport(device, writeC, notifyC);
}
device: Device;
writeCharacteristic: Characteristic;
notifyCharacteristic: Characteristic;
constructor(
device: Device,
writeCharacteristic: Characteristic,
notifyCharacteristic: Characteristic
) {
super();
this.device = device;
this.writeCharacteristic = writeCharacteristic;
this.notifyCharacteristic = notifyCharacteristic;
device.onDisconnected(e => {
if (this.debug) {
console.log("BLE disconnect", this.device);
}
this.emit("disconnect", e);
});
}
busy = false;
async exchange(apdu: Buffer): Promise<Buffer> {
if (this.busy) {
throw new TransportError(
"exchange() race condition",
"ExchangeRaceCondition"
);
}
this.busy = true;
let receiving;
try {
receiving = receive(this.notifyCharacteristic, this.debug);
send(this.writeCharacteristic, apdu, receiving.promise, this.debug);
const data = await receiving.promise;
return data;
} finally {
this.busy = false;
if (receiving) {
receiving.subscription.remove();
}
}
}
setScrambleKey() {}
close(): Promise<void> {
return this.device.cancelConnection();
}
}