@@ -94,6 +94,12 @@ var LedgerBridge = function () {
94
94
case 'ledger-sign-typed-data':
95
95
_this.signTypedData(replyAction, params.hdPath, params.domainSeparatorHex, params.hashStructMessageHex, messageId);
96
96
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;
97
103
}
98
104
}
99
105
}, false);
@@ -187,30 +193,54 @@ var LedgerBridge = function () {
187
193
this.app = new _hwAppEth2.default(this.transport);
188
194
}
189
195
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
+ });
206
200
} catch (e) {
207
201
console.log('LEDGER:::CREATE APP ERROR', e);
208
202
throw e;
209
203
}
210
204
}
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
+ }
211
235
}, {
212
236
key: 'checkConnectionStatus',
213
237
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
+
214
244
// Ensure the correct (Ethereum) app is open; if not, immediately kill
215
245
// the connection as the wrong app is open and switching apps will call
216
246
// a disconnect from within the Ledger API
@@ -234,20 +264,21 @@ var LedgerBridge = function () {
234
264
} else {
235
265
// Wrong app
236
266
this.onDisconnect();
267
+ return false;
237
268
}
238
269
} catch (e) {
239
270
console.log('LEDGER:::Transport check error', e);
240
271
this.onDisconnect();
241
272
throw e;
242
273
}
243
-
244
- return false;
245
274
}
246
275
}, {
247
276
key: 'updateTransportTypePreference',
248
277
value: function updateTransportTypePreference(replyAction, transportType, messageId) {
249
- this.transportType = transportType;
250
- this.cleanUp();
278
+ if (transportType != this.transportType) {
279
+ this.transportType = transportType;
280
+ this.cleanUp();
281
+ }
251
282
this.sendMessageToExtension({
252
283
action: replyAction,
253
284
success: true,
@@ -262,9 +293,7 @@ var LedgerBridge = function () {
262
293
await this.transport.close();
263
294
this.transport = null;
264
295
}
265
- if (this.pollingInterval) {
266
- clearInterval(this.pollingInterval);
267
- }
296
+ this.stopConnectionPolling();
268
297
if (replyAction) {
269
298
this.sendMessageToExtension({
270
299
action: replyAction,
0 commit comments