Skip to content

Commit 58ddedf

Browse files
committed
Skip the connectivity check for U2f/Firefox
1 parent b56410b commit 58ddedf

File tree

2 files changed

+89
-68
lines changed

2 files changed

+89
-68
lines changed

bundle.js

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
3030

3131
require('buffer');
3232

33-
// URL which triggers Ledger Live app to open and handle communication
34-
var BRIDGE_URL = 'ws://localhost:8435';
33+
var SUPPORTED_TRANSPORT_TYPES = {
34+
U2F: 'u2f',
35+
WEB_HID: 'webhid',
36+
LEDGER_LIVE: 'ledgerLive'
37+
38+
// URL which triggers Ledger Live app to open and handle communication
39+
};var BRIDGE_URL = 'ws://localhost:8435';
3540

3641
// Number of seconds to poll for Ledger Live and Ethereum app opening
3742
var TRANSPORT_CHECK_DELAY = 1000;
@@ -42,7 +47,7 @@ var LedgerBridge = function () {
4247
_classCallCheck(this, LedgerBridge);
4348

4449
this.addEventListeners();
45-
this.transportType = 'u2f';
50+
this.transportType = SUPPORTED_TRANSPORT_TYPES.U2F;
4651
}
4752

4853
_createClass(LedgerBridge, [{
@@ -73,12 +78,12 @@ var LedgerBridge = function () {
7378
_this.cleanUp(replyAction, messageId);
7479
break;
7580
case 'ledger-update-transport':
76-
if (params.transportType === 'ledgerLive' || params.useLedgerLive) {
77-
_this.updateTransportTypePreference(replyAction, 'ledgerLive', messageId);
78-
} else if (params.transportType === 'webhid') {
79-
_this.updateTransportTypePreference(replyAction, 'webhid', messageId);
81+
if (params.transportType === SUPPORTED_TRANSPORT_TYPES.LEDGER_LIVE || params.useLedgerLive) {
82+
_this.updateTransportTypePreference(replyAction, SUPPORTED_TRANSPORT_TYPES.LEDGER_LIVE, messageId);
83+
} else if (params.transportType === SUPPORTED_TRANSPORT_TYPES.WEB_HID) {
84+
_this.updateTransportTypePreference(replyAction, SUPPORTED_TRANSPORT_TYPES.WEB_HID, messageId);
8085
} else {
81-
_this.updateTransportTypePreference(replyAction, 'u2f', messageId);
86+
_this.updateTransportTypePreference(replyAction, SUPPORTED_TRANSPORT_TYPES.U2F, messageId);
8287
}
8388
break;
8489
case 'ledger-make-app':
@@ -153,7 +158,7 @@ var LedgerBridge = function () {
153158
}
154159

155160
try {
156-
if (this.transportType === 'ledgerLive') {
161+
if (this.transportType === SUPPORTED_TRANSPORT_TYPES.LEDGER_LIVE) {
157162
var reestablish = false;
158163
try {
159164
await _WebSocketTransport2.default.check(BRIDGE_URL);
@@ -166,7 +171,7 @@ var LedgerBridge = function () {
166171
this.transport = await _WebSocketTransport2.default.open(BRIDGE_URL);
167172
this.app = new _hwAppEth2.default(this.transport);
168173
}
169-
} else if (this.transportType === 'webhid') {
174+
} else if (this.transportType === SUPPORTED_TRANSPORT_TYPES.WEB_HID) {
170175
var device = this.transport && this.transport.device;
171176
var nameOfDeviceType = device && device.constructor.name;
172177
var deviceIsOpen = device && device.opened;
@@ -180,33 +185,38 @@ var LedgerBridge = function () {
180185
this.app = new _hwAppEth2.default(this.transport);
181186
}
182187

183-
// Ensure the correct (Ethereum) app is open; if not, immediately kill
184-
// the connection as the wrong app is open and switching apps will call
185-
// a disconnect from within the Ledger API
186-
try {
187-
var sampleSendResult = await this.transport.send(0xb0, 0x01, 0x00, 0x00);
188-
var bufferResult = Buffer.from(sampleSendResult).toString();
189-
// Ensures the correct app is open
190-
if (bufferResult.includes('Ethereum')) {
191-
// Ensure the device is unlocked by requesting an account
192-
// An error of `6b0c` will throw if locked
193-
var _ref = await this.app.getAddress('44\'/60\'/0\'/0', false, true),
194-
address = _ref.address;
195-
196-
if (address) {
197-
this.sendConnectionMessage(true);
198-
this.transport.on('disconnect', function () {
199-
return _this3.onDisconnect();
200-
});
201-
} else {
202-
this.onDisconnect();
203-
throw Error('LEDGER:::Device appears to be locked');
188+
// The U2F transport is deprecated and the following block will
189+
// throw an error in Firefox, so we cannot detect true
190+
// connection status with U2F
191+
if (this.transportType != SUPPORTED_TRANSPORT_TYPES.U2F) {
192+
// Ensure the correct (Ethereum) app is open; if not, immediately kill
193+
// the connection as the wrong app is open and switching apps will call
194+
// a disconnect from within the Ledger API
195+
try {
196+
var sampleSendResult = await this.transport.send(0xb0, 0x01, 0x00, 0x00);
197+
var bufferResult = Buffer.from(sampleSendResult).toString();
198+
// Ensures the correct app is open
199+
if (bufferResult.includes('Ethereum')) {
200+
// Ensure the device is unlocked by requesting an account
201+
// An error of `6b0c` will throw if locked
202+
var _ref = await this.app.getAddress('44\'/60\'/0\'/0', false, true),
203+
address = _ref.address;
204+
205+
if (address) {
206+
this.sendConnectionMessage(true);
207+
this.transport.on('disconnect', function () {
208+
return _this3.onDisconnect();
209+
});
210+
} else {
211+
this.onDisconnect();
212+
throw Error('LEDGER:::Device appears to be locked');
213+
}
204214
}
215+
} catch (e) {
216+
console.log('LEDGER:::Transport check error', e);
217+
this.onDisconnect();
218+
throw e;
205219
}
206-
} catch (e) {
207-
console.log('LEDGER:::Transport check error', e);
208-
this.onDisconnect();
209-
throw e;
210220
}
211221
} catch (e) {
212222
console.log('LEDGER:::CREATE APP ERROR', e);
@@ -364,7 +374,7 @@ var LedgerBridge = function () {
364374
}, {
365375
key: '_shouldCleanupTransport',
366376
value: function _shouldCleanupTransport() {
367-
return this.transportType === 'u2f';
377+
return this.transportType === SUPPORTED_TRANSPORT_TYPES.U2F;
368378
}
369379
}, {
370380
key: 'ledgerErrToMessage',

ledger-bridge.js

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import TransportWebHID from '@ledgerhq/hw-transport-webhid'
66
import LedgerEth from '@ledgerhq/hw-app-eth'
77
import WebSocketTransport from '@ledgerhq/hw-transport-http/lib/WebSocketTransport'
88

9+
const SUPPORTED_TRANSPORT_TYPES = {
10+
U2F: 'u2f',
11+
WEB_HID: 'webhid',
12+
LEDGER_LIVE: 'ledgerLive',
13+
}
14+
915
// URL which triggers Ledger Live app to open and handle communication
1016
const BRIDGE_URL = 'ws://localhost:8435'
1117

@@ -16,7 +22,7 @@ const TRANSPORT_CHECK_LIMIT = 120
1622
export default class LedgerBridge {
1723
constructor () {
1824
this.addEventListeners()
19-
this.transportType = 'u2f'
25+
this.transportType = SUPPORTED_TRANSPORT_TYPES.U2F
2026
}
2127

2228
addEventListeners () {
@@ -39,12 +45,12 @@ export default class LedgerBridge {
3945
this.cleanUp(replyAction, messageId)
4046
break
4147
case 'ledger-update-transport':
42-
if (params.transportType === 'ledgerLive' || params.useLedgerLive) {
43-
this.updateTransportTypePreference(replyAction, 'ledgerLive', messageId)
44-
} else if (params.transportType === 'webhid') {
45-
this.updateTransportTypePreference(replyAction, 'webhid', messageId)
48+
if (params.transportType === SUPPORTED_TRANSPORT_TYPES.LEDGER_LIVE || params.useLedgerLive) {
49+
this.updateTransportTypePreference(replyAction, SUPPORTED_TRANSPORT_TYPES.LEDGER_LIVE, messageId)
50+
} else if (params.transportType === SUPPORTED_TRANSPORT_TYPES.WEB_HID) {
51+
this.updateTransportTypePreference(replyAction, SUPPORTED_TRANSPORT_TYPES.WEB_HID, messageId)
4652
} else {
47-
this.updateTransportTypePreference(replyAction, 'u2f', messageId)
53+
this.updateTransportTypePreference(replyAction, SUPPORTED_TRANSPORT_TYPES.U2F, messageId)
4854
}
4955
break
5056
case 'ledger-make-app':
@@ -106,7 +112,7 @@ export default class LedgerBridge {
106112
}
107113

108114
try {
109-
if (this.transportType === 'ledgerLive') {
115+
if (this.transportType === SUPPORTED_TRANSPORT_TYPES.LEDGER_LIVE) {
110116
let reestablish = false;
111117
try {
112118
await WebSocketTransport.check(BRIDGE_URL)
@@ -119,7 +125,7 @@ export default class LedgerBridge {
119125
this.transport = await WebSocketTransport.open(BRIDGE_URL)
120126
this.app = new LedgerEth(this.transport)
121127
}
122-
} else if (this.transportType === 'webhid') {
128+
} else if (this.transportType === SUPPORTED_TRANSPORT_TYPES.WEB_HID) {
123129
const device = this.transport && this.transport.device
124130
const nameOfDeviceType = device && device.constructor.name
125131
const deviceIsOpen = device && device.opened
@@ -135,31 +141,36 @@ export default class LedgerBridge {
135141
this.app = new LedgerEth(this.transport)
136142
}
137143

138-
// Ensure the correct (Ethereum) app is open; if not, immediately kill
139-
// the connection as the wrong app is open and switching apps will call
140-
// a disconnect from within the Ledger API
141-
try {
142-
const sampleSendResult = await this.transport.send(0xb0, 0x01, 0x00, 0x00)
143-
const bufferResult = Buffer.from(sampleSendResult).toString()
144-
// Ensures the correct app is open
145-
if(bufferResult.includes('Ethereum')) {
146-
// Ensure the device is unlocked by requesting an account
147-
// An error of `6b0c` will throw if locked
148-
const { address } = await this.app.getAddress(`44'/60'/0'/0`, false, true)
149-
if (address) {
150-
this.sendConnectionMessage(true)
151-
this.transport.on('disconnect', () => this.onDisconnect())
152-
}
153-
else {
154-
this.onDisconnect()
155-
throw Error('LEDGER:::Device appears to be locked')
144+
// The U2F transport is deprecated and the following block will
145+
// throw an error in Firefox, so we cannot detect true
146+
// connection status with U2F
147+
if (this.transportType != SUPPORTED_TRANSPORT_TYPES.U2F) {
148+
// Ensure the correct (Ethereum) app is open; if not, immediately kill
149+
// the connection as the wrong app is open and switching apps will call
150+
// a disconnect from within the Ledger API
151+
try {
152+
const sampleSendResult = await this.transport.send(0xb0, 0x01, 0x00, 0x00)
153+
const bufferResult = Buffer.from(sampleSendResult).toString()
154+
// Ensures the correct app is open
155+
if(bufferResult.includes('Ethereum')) {
156+
// Ensure the device is unlocked by requesting an account
157+
// An error of `6b0c` will throw if locked
158+
const { address } = await this.app.getAddress(`44'/60'/0'/0`, false, true)
159+
if (address) {
160+
this.sendConnectionMessage(true)
161+
this.transport.on('disconnect', () => this.onDisconnect())
162+
}
163+
else {
164+
this.onDisconnect()
165+
throw Error('LEDGER:::Device appears to be locked')
166+
}
156167
}
157168
}
158-
}
159-
catch(e) {
160-
console.log('LEDGER:::Transport check error', e)
161-
this.onDisconnect()
162-
throw e
169+
catch(e) {
170+
console.log('LEDGER:::Transport check error', e)
171+
this.onDisconnect()
172+
throw e
173+
}
163174
}
164175
} catch (e) {
165176
console.log('LEDGER:::CREATE APP ERROR', e)
@@ -312,7 +323,7 @@ export default class LedgerBridge {
312323
}
313324

314325
_shouldCleanupTransport() {
315-
return this.transportType === 'u2f';
326+
return this.transportType === SUPPORTED_TRANSPORT_TYPES.U2F;
316327
}
317328

318329
ledgerErrToMessage (err) {

0 commit comments

Comments
 (0)