Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import Transport from '@ledgerhq/hw-transport';
// $ExpectType Promise
Transport.isSupported();
// $ExpectType Promise>
Transport.list();
// $ExpectType Promise>
Transport.open('test');
const test = Transport.open('test').then(transport => {
// $ExpectType void
transport.setScrambleKey('test');
// $ExpectType Promise
transport.exchange(Buffer.from('test', 'hex'));
// $ExpectType Promise
transport.send(0, 1, 2, 3);
// $ExpectType Promise
transport.send(0, 1, 2, 3, Buffer.from('abcd', 'hex'));
// $ExpectType Promise
transport.send(0, 1, 2, 3, Buffer.from('abcd', 'hex'), [0x1111, 0x9000]);
// $ExpectType Promise
import Transport from '@ledgerhq/hw-transport';
// $ExpectType Promise
Transport.isSupported();
// $ExpectType Promise>
Transport.list();
// $ExpectType Promise>
Transport.open('test');
const test = Transport.open('test').then(transport => {
// $ExpectType void
transport.setScrambleKey('test');
// $ExpectType Promise
transport.exchange(Buffer.from('test', 'hex'));
// $ExpectType Promise
transport.send(0, 1, 2, 3);
// $ExpectType Promise
transport.send(0, 1, 2, 3, Buffer.from('abcd', 'hex'));
// $ExpectType Promise
transport.send(0, 1, 2, 3, Buffer.from('abcd', 'hex'), [0x1111, 0x9000]);
// $ExpectType Promise
transport.close();
});
import Transport from '@ledgerhq/hw-transport';
// $ExpectType Promise
Transport.isSupported();
// $ExpectType Promise>
Transport.list();
// $ExpectType Promise>
Transport.open('test');
const test = Transport.open('test').then(transport => {
// $ExpectType void
transport.setScrambleKey('test');
// $ExpectType Promise
transport.exchange(Buffer.from('test', 'hex'));
// $ExpectType Promise
transport.send(0, 1, 2, 3);
// $ExpectType Promise
transport.send(0, 1, 2, 3, Buffer.from('abcd', 'hex'));
import Transport from '@ledgerhq/hw-transport';
// $ExpectType Promise
Transport.isSupported();
// $ExpectType Promise>
Transport.list();
// $ExpectType Promise>
Transport.open('test');
const test = Transport.open('test').then(transport => {
// $ExpectType void
transport.setScrambleKey('test');
// $ExpectType Promise
transport.exchange(Buffer.from('test', 'hex'));
// $ExpectType Promise
transport.send(0, 1, 2, 3);
// $ExpectType Promise
transport.send(0, 1, 2, 3, Buffer.from('abcd', 'hex'));
// $ExpectType Promise
transport.send(0, 1, 2, 3, Buffer.from('abcd', 'hex'), [0x1111, 0x9000]);
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);
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);
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 " +
const characteristics = await device.characteristicsForService(ServiceUuid);
if (!characteristics) {
throw new TransportError("service not found", "BLEServiceNotFound");
}
let writeC;
let 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 (!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",
);
}
logSubject.next({ type: "verbose", message: `device.mtu=${device.mtu}` });
const notifyObservable = monitorCharacteristic(notifyC).pipe(
tap(value => {
logSubject.next({
}
}
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);
}