@@ -94,6 +94,12 @@ var LedgerBridge = function () {
9494 case 'ledger-sign-typed-data':
9595 _this.signTypedData(replyAction, params.hdPath, params.domainSeparatorHex, params.hashStructMessageHex, messageId);
9696 break;
97+ case 'ledger-start-polling':
98+ _this.startConnectionPolling(replyAction, messageId);
99+ break;
100+ case 'ledger-stop-polling':
101+ _this.stopConnectionPolling(replyAction, messageId);
102+ break;
97103 }
98104 }
99105 }, false);
@@ -187,30 +193,54 @@ var LedgerBridge = function () {
187193 this.app = new _hwAppEth2.default(this.transport);
188194 }
189195
190- // The U2F transport is deprecated and the following block will
191- // throw an error in Firefox, so we cannot detect true
192- // connection status with U2F
193- if (this.transportType != SUPPORTED_TRANSPORT_TYPES.U2F) {
194- // Upon Ledger disconnect from user's machine, signal disconnect
195- this.transport.on('disconnect', function () {
196- return _this3.onDisconnect();
197- });
198-
199- // We need to poll for connection status because going into
200- // sleep mode doesn't trigger the "disconnect" event
201- var connected = await this.checkConnectionStatus();
202- this.pollingInterval = setInterval(function () {
203- _this3.checkConnectionStatus();
204- }, POLLING_INTERVAL);
205- }
196+ // Upon Ledger disconnect from user's machine, signal disconnect
197+ this.transport.on('disconnect', function () {
198+ return _this3.onDisconnect();
199+ });
206200 } catch (e) {
207201 console.log('LEDGER:::CREATE APP ERROR', e);
208202 throw e;
209203 }
210204 }
205+ }, {
206+ key: 'startConnectionPolling',
207+ value: async function startConnectionPolling(replyAction, messageId) {
208+ var _this4 = this;
209+
210+ // Prevent the possibility that there could be more than one
211+ // polling interval if stopConnectionPolling hasn't been called
212+ if (this.pollingInterval) {
213+ return false;
214+ }
215+
216+ // The U2F transport is deprecated and the following block will
217+ // throw an error in Firefox, so we cannot detect true
218+ // connection status with U2F
219+ if (this.transportType !== SUPPORTED_TRANSPORT_TYPES.U2F) {
220+ // We need to poll for connection status because going into
221+ // sleep mode doesn't trigger the "disconnect" event
222+ var connected = await this.checkConnectionStatus();
223+ this.pollingInterval = setInterval(function () {
224+ _this4.checkConnectionStatus();
225+ }, POLLING_INTERVAL);
226+ }
227+ }
228+ }, {
229+ key: 'stopConnectionPolling',
230+ value: function stopConnectionPolling(replyAction, messageId) {
231+ if (this.pollingInterval) {
232+ clearInterval(this.pollingInterval);
233+ }
234+ }
211235 }, {
212236 key: 'checkConnectionStatus',
213237 value: async function checkConnectionStatus() {
238+ // If there's no app or transport, leave and signal disconnect
239+ if (!this.app || !this.transport) {
240+ this.onDisconnect();
241+ return false;
242+ }
243+
214244 // Ensure the correct (Ethereum) app is open; if not, immediately kill
215245 // the connection as the wrong app is open and switching apps will call
216246 // a disconnect from within the Ledger API
@@ -234,20 +264,25 @@ var LedgerBridge = function () {
234264 } else {
235265 // Wrong app
236266 this.onDisconnect();
267+ return false;
237268 }
238269 } catch (e) {
239270 console.log('LEDGER:::Transport check error', e);
240- this.onDisconnect();
271+ // A race condition for checking connection status isn't a sign that
272+ // connection status has changed, so don't disconnect on this type of error
273+ if (e.name !== 'TransportRaceCondition') {
274+ this.onDisconnect();
275+ }
241276 throw e;
242277 }
243-
244- return false;
245278 }
246279 }, {
247280 key: 'updateTransportTypePreference',
248281 value: function updateTransportTypePreference(replyAction, transportType, messageId) {
249- this.transportType = transportType;
250- this.cleanUp();
282+ if (transportType !== this.transportType) {
283+ this.transportType = transportType;
284+ this.cleanUp();
285+ }
251286 this.sendMessageToExtension({
252287 action: replyAction,
253288 success: true,
@@ -262,9 +297,6 @@ var LedgerBridge = function () {
262297 await this.transport.close();
263298 this.transport = null;
264299 }
265- if (this.pollingInterval) {
266- clearInterval(this.pollingInterval);
267- }
268300 if (replyAction) {
269301 this.sendMessageToExtension({
270302 action: replyAction,
0 commit comments