@metamask/eth-ledger-bridge-keyring 0.10.0 → 0.13.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 +14 -0
  2. package/index.js +36 -10
  3. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -7,6 +7,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.13.0]
11
+ ### Added
12
+ - hdk.publicKey and hdk.chainCode should not be updated when unlocking using hdPath for an account. ([#146](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/146))
13
+
14
+ ## [0.12.0]
15
+ ### Added
16
+ - Add a new `destroy` method which will remove the `message` event listener from window. ([#145](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/145))
17
+
18
+ ## [0.11.0]
19
+ ### Added
20
+ - 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))
21
+ ### Changed
22
+ - 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))
23
+
10
24
  ## [0.10.0]
11
25
  ### Added
12
26
  - 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
  }
@@ -109,7 +119,7 @@ class LedgerBridgeKeyring extends EventEmitter {
109
119
  this.hdPath = hdPath
110
120
  }
111
121
 
112
- unlock (hdPath) {
122
+ unlock (hdPath, updateHdk = true) {
113
123
  if (this.isUnlocked() && !hdPath) {
114
124
  return Promise.resolve('already unlocked')
115
125
  }
@@ -123,8 +133,10 @@ class LedgerBridgeKeyring extends EventEmitter {
123
133
  },
124
134
  ({ success, payload }) => {
125
135
  if (success) {
126
- this.hdk.publicKey = Buffer.from(payload.publicKey, 'hex')
127
- this.hdk.chainCode = Buffer.from(payload.chainCode, 'hex')
136
+ if (updateHdk) {
137
+ this.hdk.publicKey = Buffer.from(payload.publicKey, 'hex')
138
+ this.hdk.chainCode = Buffer.from(payload.chainCode, 'hex')
139
+ }
128
140
  resolve(payload.address)
129
141
  } else {
130
142
  reject(payload.error || new Error('Unknown error'))
@@ -363,7 +375,7 @@ class LedgerBridgeKeyring extends EventEmitter {
363
375
  throw new Error(`Ledger: Account for address '${checksummedAddress}' not found`)
364
376
  }
365
377
  const { hdPath } = this.accountDetails[checksummedAddress]
366
- const unlockedAddress = await this.unlock(hdPath)
378
+ const unlockedAddress = await this.unlock(hdPath, false)
367
379
 
368
380
  // unlock resolves to the address for the given hdPath as reported by the ledger device
369
381
  // if that address is not the requested address, then this account belongs to a different device or seed
@@ -467,21 +479,35 @@ class LedgerBridgeKeyring extends EventEmitter {
467
479
 
468
480
  _sendMessage (msg, cb) {
469
481
  msg.target = 'LEDGER-IFRAME'
482
+
483
+ this.currentMessageId += 1
484
+ msg.messageId = this.currentMessageId
485
+
486
+ this.messageCallbacks[this.currentMessageId] = cb
470
487
  this.iframe.contentWindow.postMessage(msg, '*')
471
- const eventListener = ({ origin, data }) => {
488
+ }
489
+
490
+ _setupListener () {
491
+ this._eventListener = ({ origin, data }) => {
472
492
  if (origin !== this._getOrigin()) {
473
493
  return false
474
494
  }
475
495
 
476
- if (data && data.action && data.action === `${msg.action}-reply` && cb) {
477
- cb(data)
478
- return undefined
496
+ if (data) {
497
+ if (this.messageCallbacks[data.messageId]) {
498
+ this.messageCallbacks[data.messageId](data)
499
+ } else if (data.action === CONNECTION_EVENT) {
500
+ this.isDeviceConnected = data.payload.connected
501
+ }
479
502
  }
480
503
 
481
- window.removeEventListener('message', eventListener)
482
504
  return undefined
483
505
  }
484
- window.addEventListener('message', eventListener)
506
+ window.addEventListener('message', this._eventListener)
507
+ }
508
+
509
+ destroy () {
510
+ window.removeEventListener('message', this._eventListener)
485
511
  }
486
512
 
487
513
  async __getPage (increment) {
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.13.0",
4
4
  "description": "A MetaMask compatible keyring, for ledger hardware wallets",
5
5
  "main": "index.js",
6
6
  "files": [