@@ -30,8 +30,13 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
3030
3131require('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
3742var 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',
0 commit comments