Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return new Promise((resolve, reject) => {
const { trezor: { getDeviceListAttempt } } = getState();
if (getDeviceListAttempt) return;
wallet.allowExternalRequest(EXTERNALREQUEST_TREZOR_BRIDGE);
dispatch({ type: TRZ_LOADDEVICELIST_ATTEMPT });
const debug = getState().trezor.debug;
// Convert the protocol buffers definition (i.e. the messages of the comm
// protocol to the bridge/device) to a string so that the transport can be
// configured with it.
const config = JSON.stringify(messages);
trezorTransports.BridgeV2.setFetch(fetch, true);
const transport = new trezorTransports.BridgeV2(null, null, null);
const opts = { debug, debugInfo: debug, config, transport };
const devList = new trezorjs.DeviceList(opts);
let resolvedTransport = false;
devList.on("transport", t => {
debug && console.log("transport", t);
if (resolvedTransport) return;
resolvedTransport = true; // resolved with success
dispatch({ deviceList: devList, type: TRZ_LOADDEVICELIST_SUCCESS });
resolve(t);
});
devList.on("error", err => {
debug && console.log("error", err);
if (!resolvedTransport && err.message.includes("ECONNREFUSED")) {
resolvedTransport = true; // resolved with failure