Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const getHID = (): HID => {
// $FlowFixMe
const { hid } = navigator;
if (!hid)
throw new TransportError(
"navigator.hid is not supported",
"HIDNotSupported"
);
return hid;
};
function wrapU2FTransportError(originalError, message, id) {
const err = new TransportError(message, id);
// $FlowFixMe
err.originalError = originalError;
return err;
}
const attemptExchange = (
apdu: Buffer,
timeout: number,
scrambleKey: ?Buffer
): Promise => {
if (!scrambleKey) {
throw new TransportError(
"transport.setScrambleKey must be used to set a scramble key. Refer to documentation.",
"NoScrambleKey"
);
}
if (!navigator.credentials) {
throw new TransportError("WebAuthn not supported", "NotSupported");
}
return (
navigator.credentials
// $FlowFixMe
.get({
publicKey: {
timeout,
challenge: new Uint8Array(32),
allowCredentials: [
{
type: "public-key",
id: new Uint8Array(wrapApdu(apdu, scrambleKey))
}
]
}
send = async (
cla: number,
ins: number,
p1: number,
p2: number,
data: Buffer = Buffer.alloc(0),
statusList: Array = [StatusCodes.OK]
): Promise => {
if (data.length >= 256) {
throw new TransportError(
"data.length exceed 256 bytes limit. Got: " + data.length,
"DataLengthTooBig"
);
}
const response = await this.exchange(
Buffer.concat([
Buffer.from([cla, ins, p1, p2]),
Buffer.from([data.length]),
data
])
);
const sw = response.readUInt16BE(response.length - 2);
if (!statusList.some(s => s === sw)) {
throw new TransportStatusError(sw);
}
return response;
complete: () => {
o.error(new TransportError("Disconnected", "Disconnected"));
sub.unsubscribe();
},
error: e => {
? setTimeout(() => {
sub.unsubscribe();
reject(
new TransportError(
this.ErrorMessage_ListenTimeout,
"ListenTimeout"
)
);
}, listenTimeout)
: null;
export const retrieveServiceAndCharacteristics = async (device: *) => {
const [service] = await discoverDeviceServices(device);
const infos = getInfosForServiceUuid(service.uuid);
if (!infos) {
throw new TransportError("service not found", "BLEServiceNotFound");
}
const characteristics = await discoverServiceCharacteristics(service);
let writeC;
let notifyC;
for (const c of characteristics) {
if (c.uuid === infos.writeUuid.replace(/-/g, "")) {
writeC = c;
} else if (c.uuid === infos.notifyUuid.replace(/-/g, "")) {
notifyC = c;
}
}
if (!writeC || !notifyC) {
throw new TransportError(
"missing characteristics",
"BLEMissingCharacteristics"
);
notifiedIndex,
"InvalidSequence"
)
);
return;
}
if (index === 0) {
notifiedDataLength = data.readUInt16BE(0);
data = data.slice(2);
}
notifiedIndex++;
notifiedData = Buffer.concat([notifiedData, data]);
if (notifiedData.length > notifiedDataLength) {
o.error(
new TransportError(
"BLE: received too much data. discontinued chunk. Received " +
notifiedData.length +
" but expected " +
notifiedDataLength,
"BLETooMuchData"
)
);
return;
}
if (notifiedData.length === notifiedDataLength) {
o.next(notifiedData);
o.complete();
sub.unsubscribe();
}
}
});
socket.onmessage = e => {
if (typeof e.data !== "string") return;
const data = JSON.parse(e.data);
switch (data.type) {
case "opened":
return resolve(exchangeMethods);
case "error":
reject(new Error(data.error));
return exchangeMethods.rejectExchange(
new TransportError(data.error, "WSError")
);
case "response":
return exchangeMethods.resolveExchange(
Buffer.from(data.data, "hex")
);
}
};
} catch (e) {
next: value => {
const tag = value.readUInt8(0);
const index = value.readUInt16BE(1);
let data = value.slice(3);
if (tag !== TagId) {
o.error(
new TransportError("Invalid tag " + tag.toString(16), "InvalidTag")
);
return;
}
if (notifiedIndex !== index) {
o.error(
new TransportError(
"BLE: Invalid sequence number. discontinued chunk. Received " +
index +
" but expected " +
notifiedIndex,
"InvalidSequence"
)
);
return;
}
if (index === 0) {
notifiedDataLength = data.readUInt16BE(0);
data = data.slice(2);
}
notifiedIndex++;
notifiedData = Buffer.concat([notifiedData, data]);