@metamask/eth-ledger-bridge-keyring 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/index.js +24 -4
  3. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.11.0]
11
+ ### Added
12
+ - Add a new `isConnected` method which allows determining if the device is last known to be connected. ([#131](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/131))
13
+ ### Changed
14
+ - Messaging now runs off of message IDs instead of assuming the response received is from the last message sent, which will not always been true. ([#132](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/132))
15
+
10
16
  ## [0.10.0]
11
17
  ### Added
12
18
  - Add a new `attemptMakeApp` method which allows clients to attempt a creation of the Ledger transport for the purposes of detecting/catching potential connection errors. ([#126](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/126))
package/index.js CHANGED
@@ -18,6 +18,8 @@ const NETWORK_API_URLS = {
18
18
  mainnet: 'https://api.etherscan.io',
19
19
  }
20
20
 
21
+ const CONNECTION_EVENT = 'ledger-connection-change'
22
+
21
23
  class LedgerBridgeKeyring extends EventEmitter {
22
24
  constructor (opts = {}) {
23
25
  super()
@@ -36,6 +38,10 @@ class LedgerBridgeKeyring extends EventEmitter {
36
38
 
37
39
  this.iframeLoaded = false
38
40
  this._setupIframe()
41
+
42
+ this.currentMessageId = 0
43
+ this.messageCallbacks = {}
44
+ this._setupListener()
39
45
  }
40
46
 
41
47
  serialize () {
@@ -97,6 +103,10 @@ class LedgerBridgeKeyring extends EventEmitter {
97
103
  return Boolean(this.hdk && this.hdk.publicKey)
98
104
  }
99
105
 
106
+ isConnected () {
107
+ return this.isDeviceConnected
108
+ }
109
+
100
110
  setAccountToUnlock (index) {
101
111
  this.unlockedAccount = parseInt(index, 10)
102
112
  }
@@ -467,18 +477,28 @@ class LedgerBridgeKeyring extends EventEmitter {
467
477
 
468
478
  _sendMessage (msg, cb) {
469
479
  msg.target = 'LEDGER-IFRAME'
480
+
481
+ this.currentMessageId += 1
482
+ msg.messageId = this.currentMessageId
483
+
484
+ this.messageCallbacks[this.currentMessageId] = cb
470
485
  this.iframe.contentWindow.postMessage(msg, '*')
486
+ }
487
+
488
+ _setupListener () {
471
489
  const eventListener = ({ origin, data }) => {
472
490
  if (origin !== this._getOrigin()) {
473
491
  return false
474
492
  }
475
493
 
476
- if (data && data.action && data.action === `${msg.action}-reply` && cb) {
477
- cb(data)
478
- return undefined
494
+ if (data) {
495
+ if (this.messageCallbacks[data.messageId]) {
496
+ this.messageCallbacks[data.messageId](data)
497
+ } else if (data.action === CONNECTION_EVENT) {
498
+ this.isDeviceConnected = data.payload.connected
499
+ }
479
500
  }
480
501
 
481
- window.removeEventListener('message', eventListener)
482
502
  return undefined
483
503
  }
484
504
  window.addEventListener('message', eventListener)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/eth-ledger-bridge-keyring",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "description": "A MetaMask compatible keyring, for ledger hardware wallets",
5
5
  "main": "index.js",
6
6
  "files": [