-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect and react to transport and connection events
- Loading branch information
Showing
11 changed files
with
2,087 additions
and
1,001 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import * as trezorjs from "trezor.js"; | ||
import trezorTransports from "trezor-link"; | ||
import * as wallet from "wallet"; | ||
import { EXTERNALREQUEST_TREZOR_BRIDGE } from "main_dev/externalRequests"; | ||
|
||
export const TRZ_LOADDEVICELIST_ATTEMPT = "TRZ_LOADDEVICELIST_ATTEMPT"; | ||
export const TRZ_LOADDEVICELIST_FAILED = "TRZ_LOADDEVICELIST_FAILED"; | ||
export const TRZ_LOADDEVICELIST_SUCCESS = "TRZ_LOADDEVICELIST_SUCCESS"; | ||
export const TRZ_DEVICELISTTRANSPORT_LOST = "TRZ_DEVICELISTTRANSPORT_LOST"; | ||
export const TRZ_SELECTEDDEVICE_CHANGED = "TRZ_SELECTEDDEVICE_CHANGED"; | ||
|
||
export const loadDeviceList = () => (dispatch, getState) => { | ||
return new Promise((resolve, reject) => { | ||
if (!getState().trezor.enabled) return; | ||
wallet.allowExternalRequest(EXTERNALREQUEST_TREZOR_BRIDGE); | ||
|
||
dispatch({ type: TRZ_LOADDEVICELIST_ATTEMPT }); | ||
const debug = getState().trezor.debug; | ||
|
||
// TODO: decide whether we want to provide our own config blob. | ||
const configUrl = "https://wallet.trezor.io/data/config_signed.bin?" | ||
+ Date.now(); | ||
|
||
const opts = { debug, debugInfo: debug, configUrl, | ||
transport: new trezorTransports.BridgeV2() }; | ||
const devList = new trezorjs.DeviceList(opts); | ||
let resolvedTransport = false; | ||
|
||
devList.on("transport", t => { | ||
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 => { | ||
console.log("error", err); | ||
if (!resolvedTransport && err.message.includes("ECONNREFUSED")) { | ||
resolvedTransport = true; // resolved with failure | ||
dispatch({ error: err.message, type: TRZ_LOADDEVICELIST_FAILED }); | ||
reject(err); | ||
} else if (err.message.includes("socket hang up")) { | ||
// this might happen any time throughout the app lifetime if the bridge | ||
// service is shutdown for any reason | ||
dispatch({ error: err.message, type: TRZ_DEVICELISTTRANSPORT_LOST }); | ||
} | ||
}); | ||
|
||
devList.on("connect", device => { | ||
console.log("connect", Object.keys(devList.devices), device); | ||
const currentDevice = getState().trezor.device; | ||
if (!currentDevice) { | ||
// first device connected. Use it. | ||
dispatch({ device, type: TRZ_SELECTEDDEVICE_CHANGED }); | ||
} | ||
}); | ||
|
||
devList.on("disconnect", device => { | ||
console.log("disconnect", Object.keys(devList.devices), device); | ||
const currentDevice = getState().trezor.device; | ||
if (currentDevice && device.originalDescriptor.path === currentDevice.originalDescriptor.path ) { | ||
const devicePaths = Object.keys(devList.devices); | ||
|
||
// we were using the device that was just disconnected. Pick a new | ||
// device to use. | ||
if (devicePaths.length === 0) { | ||
// no more devices left to use | ||
dispatch({ device: null, type: TRZ_SELECTEDDEVICE_CHANGED }); | ||
} else { | ||
dispatch({ device: devList.devices[devicePaths[0]], type: TRZ_SELECTEDDEVICE_CHANGED }); | ||
} | ||
} | ||
}); | ||
|
||
devList.on("connectUnacquired", device => { | ||
console.log("connect unacquired", device); | ||
}); | ||
|
||
devList.on("disconnectUnacquired", device => { | ||
console.log("disconnect unacquired", device); | ||
}); | ||
|
||
}); | ||
}; | ||
|
||
export const selectDevice = (path) => async (dispatch, getState) => { | ||
const devList = getState().trezor.deviceList; | ||
if (!devList.devices[path]) return; | ||
dispatch({ device: devList.devices[path], type: TRZ_SELECTEDDEVICE_CHANGED }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { | ||
TRZ_LOADDEVICELIST_ATTEMPT, TRZ_LOADDEVICELIST_FAILED, TRZ_LOADDEVICELIST_SUCCESS, | ||
TRZ_DEVICELISTTRANSPORT_LOST, | ||
TRZ_SELECTEDDEVICE_CHANGED, | ||
} from "actions/TrezorActions"; | ||
|
||
export default function trezor(state = {}, action) { | ||
switch (action.type) { | ||
case TRZ_LOADDEVICELIST_ATTEMPT: | ||
return { ...state, | ||
deviceList: null, | ||
transportError: false, | ||
device: null, | ||
}; | ||
case TRZ_LOADDEVICELIST_SUCCESS: | ||
return { ...state, | ||
deviceList: action.deviceList, | ||
transportError: false, | ||
}; | ||
case TRZ_LOADDEVICELIST_FAILED: | ||
return { ...state, | ||
transportError: action.error, | ||
}; | ||
case TRZ_DEVICELISTTRANSPORT_LOST: | ||
return { ...state, | ||
deviceList: null, | ||
transportError: action.error, | ||
device: null, | ||
}; | ||
case TRZ_SELECTEDDEVICE_CHANGED: | ||
return { ...state, | ||
device: action.device, | ||
}; | ||
default: | ||
return state; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.