@metamask/eth-ledger-bridge-keyring 0.8.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 +39 -0
  2. package/index.js +43 -9
  3. package/package.json +1 -1
package/CHANGELOG.md ADDED
@@ -0,0 +1,39 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
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
+
16
+ ## [0.10.0]
17
+ ### Added
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))
19
+
20
+ ## [0.9.0]
21
+ ### Changed
22
+ - `updateTransportMethod` no longer defaults its parameter to false, and now names the param sent with the `'ledger-update-transport'` message `transportType`. This better is to support the use of an enum, instead of a boolean, for specifying transport preferences. ([#114](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/114))
23
+
24
+ ## [0.8.0]
25
+ ### Added
26
+ - Allow ledger-bridge iframe to connect Ledger wia WebHID, when it is supported by the current browser ([#107](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/107))
27
+
28
+ ### Changed
29
+ - Reject with an Error object if unlocking is not successful ([#104](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/104))
30
+ - Ensure that logs of errors only have a single `Error:` string in the message ([#105](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/105))
31
+
32
+ ## [0.7.0]
33
+ ### Changed
34
+ - Remove unused `events` and `ethereumjs-tx` dependencies ([#101](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/101), [#102](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/102))
35
+ - Update eth-ledger-bridge-keyring to support EIP-1559 transactions ([#98](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/98), [#97](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/97), [#96](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/96))
36
+
37
+ ## [0.6.0]
38
+ ### Added
39
+ - Support new versions of ethereumjs/tx ([#68](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/68))
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
  }
@@ -191,22 +201,36 @@ class LedgerBridgeKeyring extends EventEmitter {
191
201
  delete this.accountDetails[ethUtil.toChecksumAddress(address)]
192
202
  }
193
203
 
194
- updateTransportMethod (useLedgerLive = false) {
204
+ attemptMakeApp () {
195
205
  return new Promise((resolve, reject) => {
196
- // If the iframe isn't loaded yet, let's store the desired useLedgerLive value and
206
+ this._sendMessage({
207
+ action: 'ledger-make-app',
208
+ }, ({ success, error }) => {
209
+ if (success) {
210
+ resolve(true)
211
+ } else {
212
+ reject(error)
213
+ }
214
+ })
215
+ })
216
+ }
217
+
218
+ updateTransportMethod (transportType) {
219
+ return new Promise((resolve, reject) => {
220
+ // If the iframe isn't loaded yet, let's store the desired transportType value and
197
221
  // optimistically return a successful promise
198
222
  if (!this.iframeLoaded) {
199
223
  this.delayedPromise = {
200
224
  resolve,
201
225
  reject,
202
- useLedgerLive,
226
+ transportType,
203
227
  }
204
228
  return
205
229
  }
206
230
 
207
231
  this._sendMessage({
208
232
  action: 'ledger-update-transport',
209
- params: { useLedgerLive },
233
+ params: { transportType },
210
234
  }, ({ success }) => {
211
235
  if (success) {
212
236
  resolve(true)
@@ -432,7 +456,7 @@ class LedgerBridgeKeyring extends EventEmitter {
432
456
  if (this.delayedPromise) {
433
457
  try {
434
458
  const result = await this.updateTransportMethod(
435
- this.delayedPromise.useLedgerLive,
459
+ this.delayedPromise.transportType,
436
460
  )
437
461
  this.delayedPromise.resolve(result)
438
462
  } catch (e) {
@@ -453,18 +477,28 @@ class LedgerBridgeKeyring extends EventEmitter {
453
477
 
454
478
  _sendMessage (msg, cb) {
455
479
  msg.target = 'LEDGER-IFRAME'
480
+
481
+ this.currentMessageId += 1
482
+ msg.messageId = this.currentMessageId
483
+
484
+ this.messageCallbacks[this.currentMessageId] = cb
456
485
  this.iframe.contentWindow.postMessage(msg, '*')
486
+ }
487
+
488
+ _setupListener () {
457
489
  const eventListener = ({ origin, data }) => {
458
490
  if (origin !== this._getOrigin()) {
459
491
  return false
460
492
  }
461
493
 
462
- if (data && data.action && data.action === `${msg.action}-reply` && cb) {
463
- cb(data)
464
- 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
+ }
465
500
  }
466
501
 
467
- window.removeEventListener('message', eventListener)
468
502
  return undefined
469
503
  }
470
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.8.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": [