-
-
Notifications
You must be signed in to change notification settings - Fork 111
gh-pages: Add method to share connection status #147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: gh-pages
Are you sure you want to change the base?
Changes from 27 commits
859f6f0
2b2a162
6a5b97f
2c8b235
4fa846e
f8c0eed
3c4f0e2
d5fb774
f9834d9
01c1f4c
cbd0077
356e5d4
e411cd6
be6c19d
c1f58cc
33a7d0d
742de94
d0648c3
bfe5188
25b09cb
47bdc6f
af2d214
52662e0
e2c37ef
e378116
343cb0d
376c15e
9e79046
ce80fc5
d5ec804
b56410b
58ddedf
62bae55
917aa33
94dce64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,15 +80,15 @@ export default class LedgerBridge { | |
|
|
||
| async attemptMakeApp (replyAction, messageId) { | ||
| try { | ||
| await this.makeApp({ openOnly: true }); | ||
| await this.cleanUp(); | ||
| await this.makeApp({ openOnly: true }) | ||
| await this.cleanUp() | ||
| this.sendMessageToExtension({ | ||
| action: replyAction, | ||
| success: true, | ||
| messageId, | ||
| }) | ||
| } catch (error) { | ||
| await this.cleanUp(); | ||
| await this.cleanUp() | ||
| this.sendMessageToExtension({ | ||
| action: replyAction, | ||
| success: false, | ||
|
|
@@ -99,15 +99,21 @@ export default class LedgerBridge { | |
| } | ||
|
|
||
| async makeApp (config = {}) { | ||
| // It's possible that a connection to the device could already exist | ||
| // at the time a user tries to sign; in that case, simply bail! | ||
| if(this.transport) { | ||
| return Promise.resolve(true); | ||
| } | ||
|
|
||
| try { | ||
| if (this.transportType === 'ledgerLive') { | ||
| let reestablish = false; | ||
| let reestablish = false | ||
| try { | ||
| await WebSocketTransport.check(BRIDGE_URL) | ||
| } catch (_err) { | ||
| window.open('ledgerlive://bridge?appName=Ethereum') | ||
| await this.checkTransportLoop() | ||
| reestablish = true; | ||
| reestablish = true | ||
| } | ||
| if (!this.app || reestablish) { | ||
| this.transport = await WebSocketTransport.open(BRIDGE_URL) | ||
|
|
@@ -118,7 +124,7 @@ export default class LedgerBridge { | |
| const nameOfDeviceType = device && device.constructor.name | ||
| const deviceIsOpen = device && device.opened | ||
| if (this.app && nameOfDeviceType === 'HIDDevice' && deviceIsOpen) { | ||
| return; | ||
| return | ||
| } | ||
| this.transport = config.openOnly | ||
| ? await TransportWebHID.openConnected() | ||
|
|
@@ -128,6 +134,36 @@ export default class LedgerBridge { | |
| this.transport = await TransportU2F.create() | ||
| this.app = new LedgerEth(this.transport) | ||
| } | ||
|
|
||
| // Ensure the correct (Ethereum) app is open; if not, immediately kill | ||
| // the connection as the wrong app is open and switching apps will call | ||
| // a disconnect from within the Ledger API | ||
| try { | ||
|
||
| const sampleSendResult = await this.transport.send(0xb0, 0x01, 0x00, 0x00) | ||
| const bufferResult = Buffer.from(sampleSendResult).toString() | ||
| // Ensures the correct app is open | ||
| if(bufferResult.includes('Ethereum')) { | ||
| // Ensure the device is unlocked by requesting an account | ||
| // An error of `6b0c` will throw if locked | ||
| const { address } = await this.app.getAddress(`44'/60'/0'/0`, false, true) | ||
| if (address) { | ||
| this.sendConnectionMessage(true) | ||
|
|
||
| this.transport.on('disconnect', (event) => { | ||
| this.onDisconnect() | ||
| }) | ||
| } | ||
| else { | ||
| this.onDisconnect() | ||
darkwing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
| catch(e) { | ||
| console.log('LEDGER:::Transport check error', e) | ||
| this.sendConnectionMessage(false) | ||
|
||
| this.onDisconnect() | ||
| throw e | ||
| } | ||
| } catch (e) { | ||
| console.log('LEDGER:::CREATE APP ERROR', e) | ||
| throw e | ||
|
|
@@ -179,7 +215,7 @@ export default class LedgerBridge { | |
| }) | ||
| } finally { | ||
| if (this.transportType !== 'ledgerLive') { | ||
| this.cleanUp() | ||
| // await this.cleanUp() | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -206,7 +242,7 @@ export default class LedgerBridge { | |
|
|
||
| } finally { | ||
| if (this.transportType !== 'ledgerLive') { | ||
| this.cleanUp() | ||
| await this.cleanUp() | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -233,7 +269,7 @@ export default class LedgerBridge { | |
|
|
||
| } finally { | ||
| if (this.transportType !== 'ledgerLive') { | ||
| this.cleanUp() | ||
| await this.cleanUp() | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -259,10 +295,23 @@ export default class LedgerBridge { | |
| }) | ||
|
|
||
| } finally { | ||
| this.cleanUp() | ||
| await this.cleanUp() | ||
| } | ||
| } | ||
|
|
||
| onDisconnect() { | ||
| this.cleanUp() | ||
| this.sendConnectionMessage(false) | ||
| } | ||
|
|
||
| sendConnectionMessage(connected) { | ||
| this.sendMessageToExtension({ | ||
| action: 'ledger-connection-change', | ||
| success: true, | ||
| payload: { connected } | ||
| }) | ||
| } | ||
|
|
||
| ledgerErrToMessage (err) { | ||
| const isU2FError = (err) => !!err && !!(err).metaData | ||
| const isStringError = (err) => typeof err === 'string' | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,5 @@ | ||
| 'use strict' | ||
| import LedgerBridge from './ledger-bridge' | ||
|
|
||
| (async () => { | ||
| const bridge = new LedgerBridge() | ||
| })() | ||
| new LedgerBridge() | ||
| console.log(`MetaMask < = > Ledger Bridge initialized from ${window.location}!`) |
Uh oh!
There was an error while loading. Please reload this page.