@metamask/eth-ledger-bridge-keyring 4.1.1 → 4.1.3
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.
- package/CHANGELOG.md +88 -27
- package/dist/ledger-hw-app.js +2 -3
- package/dist/ledger-hw-app.js.map +1 -1
- package/dist/ledger-iframe-bridge.d.ts +1 -0
- package/dist/ledger-iframe-bridge.js +7 -4
- package/dist/ledger-iframe-bridge.js.map +1 -1
- package/dist/ledger-keyring.js +5 -6
- package/dist/ledger-keyring.js.map +1 -1
- package/package.json +15 -30
package/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
# Changelog
|
|
2
|
+
|
|
2
3
|
All notable changes to this project will be documented in this file.
|
|
3
4
|
|
|
4
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
@@ -6,30 +7,55 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
6
7
|
|
|
7
8
|
## [Unreleased]
|
|
8
9
|
|
|
10
|
+
## [4.1.3]
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- Bump `@metamask/*` and `@lavamoat/*` dependencies ([#46](https://github.com/MetaMask/accounts/pull/46))
|
|
15
|
+
- Move `deepmerge` to `devDependencies` ([#44](https://github.com/MetaMask/accounts/pull/44))
|
|
16
|
+
|
|
17
|
+
## [4.1.2]
|
|
18
|
+
|
|
19
|
+
### Changed
|
|
20
|
+
|
|
21
|
+
- Convert to monorepo
|
|
22
|
+
- Package name does not change (`@metamask/eth-ledger-bridge-keyring`) and sources have been moved to: `packages/keyring-eth-ledger-bridge`.
|
|
23
|
+
- You can find all the changes [here](https://github.com/MetaMask/accounts/compare/6da58b4...38794aa).
|
|
24
|
+
|
|
9
25
|
## [4.1.1]
|
|
26
|
+
|
|
10
27
|
### Fixed
|
|
28
|
+
|
|
11
29
|
- The promise returned by the `init` method now resolves only after iframe has been loaded ([#236](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/236))
|
|
12
30
|
- `updateTransportMethod` will throw an error when called before `init`
|
|
13
31
|
|
|
14
32
|
## [4.1.0]
|
|
33
|
+
|
|
15
34
|
### Changed
|
|
35
|
+
|
|
16
36
|
- Refactor error message in `LedgerKeyring` ([#232](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/232))
|
|
17
37
|
- Extend `LedgerMobileBridge` type from `LedgerBridge` ([#233](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/233))
|
|
18
38
|
- Create new `MetaMaskLedgerHwAppEth` instance instead of re-using previous instance ([#231](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/231))
|
|
19
39
|
|
|
20
40
|
## [4.0.0]
|
|
41
|
+
|
|
21
42
|
### Added
|
|
43
|
+
|
|
22
44
|
- Add classes `LedgerMobileBridge`, `LedgerTransportMiddleware`, and `MetaMaskLedgerHwAppEth` to support Bluetooth as a HW Ledger Transport option ([#225](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/225))
|
|
23
45
|
|
|
24
46
|
### Changed
|
|
47
|
+
|
|
25
48
|
- **BREAKING**: The `LedgerKeyring` method `setAccountToUnlock` now only accepts an input of type `number` ([#225](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/225))
|
|
26
49
|
- **BREAKING**: Removed the `chainCode` property from the `GetPublicKeyResponse` type ([#225](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/225))
|
|
27
50
|
|
|
28
51
|
## [3.0.0]
|
|
52
|
+
|
|
29
53
|
### Added
|
|
30
|
-
|
|
54
|
+
|
|
55
|
+
- Add `getOptions` and `setOptions` methods to `LedgerBridge` interface ([#210](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/210))
|
|
31
56
|
|
|
32
57
|
### Changed
|
|
58
|
+
|
|
33
59
|
- **BREAKING**: `LedgerIframeBridge` class constructor now takes an options object with `bridgeUrl` ([#210](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/210))
|
|
34
60
|
- **BREAKING**: `LedgerBridge` `init` function now takes no parameters ([#210](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/210))
|
|
35
61
|
- **BREAKING**: `LedgerBridgeKeyringOptions` no longer contain `bridgeUrl` ([#210](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/210))
|
|
@@ -37,28 +63,38 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
37
63
|
- Minor performance enhancement ([#211](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/211))
|
|
38
64
|
|
|
39
65
|
### Fixed
|
|
66
|
+
|
|
40
67
|
- **BREAKING**: `IFrameMessageResponse` now has more restrictive typings (#207) ([#207](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/207))
|
|
41
68
|
|
|
42
69
|
## [2.0.1]
|
|
70
|
+
|
|
43
71
|
### Fixed
|
|
72
|
+
|
|
44
73
|
- Fix `invalid rlp data` error for legacy transactions in `2.0.0` ([#212](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/212))
|
|
45
74
|
|
|
46
75
|
## [2.0.0]
|
|
76
|
+
|
|
47
77
|
### Changed
|
|
78
|
+
|
|
48
79
|
- **BREAKING**: Remove support for major node versions 14,15,17,19. Minimum Node.js version is now 16. ([#204](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/204))
|
|
49
80
|
- Bump `@metamask/eth-sig-util` from `^6.0.1` to `^7.0.0` ([#205](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/205))
|
|
50
81
|
|
|
51
82
|
### Fixed
|
|
83
|
+
|
|
52
84
|
- Move `@metamask/utils` from deendencies to devDependencies ([#209](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/209))
|
|
53
85
|
|
|
54
86
|
## [1.0.1]
|
|
87
|
+
|
|
55
88
|
### Fixed
|
|
89
|
+
|
|
56
90
|
- Bump dependency `hdkey` from `0.8.0` to `^2.1.0` ([#196](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/196))
|
|
57
91
|
- Replace dependency `eth-sig-util@^2` with `@metamask/eth-sig-util@^6` ([#157](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/157))
|
|
58
92
|
- Replace dependency `ethereumjs-util@^7.0.9` with `@ethereumjs/util@^8.0.0` and `@ethereumjs/rlp` ([#153](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/153))
|
|
59
93
|
|
|
60
94
|
## [1.0.0]
|
|
95
|
+
|
|
61
96
|
### Changed
|
|
97
|
+
|
|
62
98
|
- **BREAKING:** Separate the bridge from the keyring ([#156](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/156))
|
|
63
99
|
- The Ledger bridge is now a separate class (`LedgerIframeBridge`), which must be constructed separately from the keyring and passed in as a constructor argument.
|
|
64
100
|
- The bridge initialization has been moved from the keyring constructor to the keyring `init` method. The bridge is expected to be passed to the keyring uninitialized, and the keyring `init` method is expected to be called after keyring construction (before the keyring is used).
|
|
@@ -68,72 +104,97 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
68
104
|
- Add TypeScript types ([#174](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/174))
|
|
69
105
|
|
|
70
106
|
## [0.15.0]
|
|
107
|
+
|
|
71
108
|
### Changed
|
|
109
|
+
|
|
72
110
|
- **BREAKING:** @ethereumjs/tx upgraded to major version 4, which includes a shift from storing numerical values as BNs to storing them as native BigInts. This is a breaking change for users of this keyring who access the values of the tx object, or that use those tx objects to interact with other libraries that depend on @ethereumsjs/tx versions under 4.0.0. ([#181](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/181))
|
|
73
111
|
|
|
74
112
|
## [0.14.0]
|
|
113
|
+
|
|
75
114
|
### Changed
|
|
115
|
+
|
|
76
116
|
- **BREAKING:** The minimum version of Node.js required for this package has been bumped to v14. ([#169](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/169))
|
|
77
117
|
|
|
78
118
|
### Fixed
|
|
119
|
+
|
|
79
120
|
- Fix incorrect `v` for EIP-712 signatures and `personal_sign` ([#152](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/152))
|
|
80
121
|
|
|
81
122
|
## [0.13.0]
|
|
123
|
+
|
|
82
124
|
### Added
|
|
83
|
-
|
|
125
|
+
|
|
126
|
+
- 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))
|
|
84
127
|
|
|
85
128
|
## [0.12.0]
|
|
129
|
+
|
|
86
130
|
### Added
|
|
87
|
-
|
|
131
|
+
|
|
132
|
+
- 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))
|
|
88
133
|
|
|
89
134
|
## [0.11.0]
|
|
135
|
+
|
|
90
136
|
### Added
|
|
91
|
-
|
|
137
|
+
|
|
138
|
+
- 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))
|
|
92
139
|
|
|
93
140
|
### Changed
|
|
94
|
-
|
|
141
|
+
|
|
142
|
+
- 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))
|
|
95
143
|
|
|
96
144
|
## [0.10.0]
|
|
145
|
+
|
|
97
146
|
### Added
|
|
98
|
-
|
|
147
|
+
|
|
148
|
+
- 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))
|
|
99
149
|
|
|
100
150
|
## [0.9.0]
|
|
151
|
+
|
|
101
152
|
### Changed
|
|
102
|
-
|
|
153
|
+
|
|
154
|
+
- `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))
|
|
103
155
|
|
|
104
156
|
## [0.8.0]
|
|
157
|
+
|
|
105
158
|
### Added
|
|
159
|
+
|
|
106
160
|
- 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))
|
|
107
161
|
|
|
108
162
|
### Changed
|
|
109
|
-
|
|
163
|
+
|
|
164
|
+
- Reject with an Error object if unlocking is not successful ([#104](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/104))
|
|
110
165
|
- 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))
|
|
111
166
|
|
|
112
167
|
## [0.7.0]
|
|
168
|
+
|
|
113
169
|
### Changed
|
|
170
|
+
|
|
114
171
|
- 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))
|
|
115
172
|
- 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))
|
|
116
173
|
|
|
117
174
|
## [0.6.0]
|
|
175
|
+
|
|
118
176
|
### Added
|
|
177
|
+
|
|
119
178
|
- Support new versions of ethereumjs/tx ([#68](https://github.com/MetaMask/eth-ledger-bridge-keyring/pull/68))
|
|
120
179
|
|
|
121
|
-
[Unreleased]: https://github.com/MetaMask/eth-ledger-bridge-keyring
|
|
122
|
-
[4.1.
|
|
123
|
-
[4.1.
|
|
124
|
-
[4.
|
|
125
|
-
[
|
|
126
|
-
[
|
|
127
|
-
[
|
|
128
|
-
[
|
|
129
|
-
[
|
|
130
|
-
[0.
|
|
131
|
-
[0.
|
|
132
|
-
[0.
|
|
133
|
-
[0.
|
|
134
|
-
[0.
|
|
135
|
-
[0.
|
|
136
|
-
[0.
|
|
137
|
-
[0.
|
|
138
|
-
[0.
|
|
139
|
-
[0.
|
|
180
|
+
[Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/eth-ledger-bridge-keyring@4.1.3...HEAD
|
|
181
|
+
[4.1.3]: https://github.com/MetaMask/accounts/compare/@metamask/eth-ledger-bridge-keyring@4.1.2...@metamask/eth-ledger-bridge-keyring@4.1.3
|
|
182
|
+
[4.1.2]: https://github.com/MetaMask/accounts/compare/@metamask/eth-ledger-bridge-keyring@4.1.1...@metamask/eth-ledger-bridge-keyring@4.1.2
|
|
183
|
+
[4.1.1]: https://github.com/MetaMask/accounts/compare/@metamask/eth-ledger-bridge-keyring@4.1.0...@metamask/eth-ledger-bridge-keyring@4.1.1
|
|
184
|
+
[4.1.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-ledger-bridge-keyring@4.0.0...@metamask/eth-ledger-bridge-keyring@4.1.0
|
|
185
|
+
[4.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-ledger-bridge-keyring@3.0.0...@metamask/eth-ledger-bridge-keyring@4.0.0
|
|
186
|
+
[3.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-ledger-bridge-keyring@2.0.1...@metamask/eth-ledger-bridge-keyring@3.0.0
|
|
187
|
+
[2.0.1]: https://github.com/MetaMask/accounts/compare/@metamask/eth-ledger-bridge-keyring@2.0.0...@metamask/eth-ledger-bridge-keyring@2.0.1
|
|
188
|
+
[2.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-ledger-bridge-keyring@1.0.1...@metamask/eth-ledger-bridge-keyring@2.0.0
|
|
189
|
+
[1.0.1]: https://github.com/MetaMask/accounts/compare/@metamask/eth-ledger-bridge-keyring@1.0.0...@metamask/eth-ledger-bridge-keyring@1.0.1
|
|
190
|
+
[1.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-ledger-bridge-keyring@0.15.0...@metamask/eth-ledger-bridge-keyring@1.0.0
|
|
191
|
+
[0.15.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-ledger-bridge-keyring@0.14.0...@metamask/eth-ledger-bridge-keyring@0.15.0
|
|
192
|
+
[0.14.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-ledger-bridge-keyring@0.13.0...@metamask/eth-ledger-bridge-keyring@0.14.0
|
|
193
|
+
[0.13.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-ledger-bridge-keyring@0.12.0...@metamask/eth-ledger-bridge-keyring@0.13.0
|
|
194
|
+
[0.12.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-ledger-bridge-keyring@0.11.0...@metamask/eth-ledger-bridge-keyring@0.12.0
|
|
195
|
+
[0.11.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-ledger-bridge-keyring@0.10.0...@metamask/eth-ledger-bridge-keyring@0.11.0
|
|
196
|
+
[0.10.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-ledger-bridge-keyring@0.9.0...@metamask/eth-ledger-bridge-keyring@0.10.0
|
|
197
|
+
[0.9.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-ledger-bridge-keyring@0.8.0...@metamask/eth-ledger-bridge-keyring@0.9.0
|
|
198
|
+
[0.8.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-ledger-bridge-keyring@0.7.0...@metamask/eth-ledger-bridge-keyring@0.8.0
|
|
199
|
+
[0.7.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-ledger-bridge-keyring@0.6.0...@metamask/eth-ledger-bridge-keyring@0.7.0
|
|
200
|
+
[0.6.0]: https://github.com/MetaMask/accounts/releases/tag/@metamask/eth-ledger-bridge-keyring@0.6.0
|
package/dist/ledger-hw-app.js
CHANGED
|
@@ -34,18 +34,17 @@ class MetaMaskLedgerHwAppEth extends hw_app_eth_1.default {
|
|
|
34
34
|
* @returns An object contains appName and version.
|
|
35
35
|
*/
|
|
36
36
|
async getAppNameAndVersion() {
|
|
37
|
-
var _a, _b;
|
|
38
37
|
const response = await this.transport.send(0xb0, 0x01, 0x00, 0x00);
|
|
39
38
|
if (response[0] !== 1) {
|
|
40
39
|
throw new Error('Incorrect format return from getAppNameAndVersion.');
|
|
41
40
|
}
|
|
42
41
|
let i = 1;
|
|
43
|
-
const nameLength =
|
|
42
|
+
const nameLength = response[i] ?? 0;
|
|
44
43
|
i += 1;
|
|
45
44
|
const appName = response
|
|
46
45
|
.slice(i, (i += nameLength))
|
|
47
46
|
.toString(this.transportEncoding);
|
|
48
|
-
const versionLength =
|
|
47
|
+
const versionLength = response[i] ?? 0;
|
|
49
48
|
i += 1;
|
|
50
49
|
const version = response
|
|
51
50
|
.slice(i, (i += versionLength))
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ledger-hw-app.js","sourceRoot":"","sources":["../src/ledger-hw-app.ts"],"names":[],"mappings":";;;;;;AAAA,sEAAkD;AAClD,oDAAoD;AACpD,mCAAgC;AAWhC,MAAa,sBACX,SAAQ,oBAAc;IADxB;;QAIW,gBAAW,GAAG,OAAO,CAAC;QAEtB,eAAU,GAAG,UAAU,CAAC;QAExB,sBAAiB,GAAG,OAAO,CAAC;IAuDvC,CAAC;IArDC;;;OAGG;IACH,KAAK,CAAC,UAAU;QACd,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CACvB,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,eAAM,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,iBAAiB,CAAC,CACrD,CAAC;IACJ,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,SAAS;QACb,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;IACpD,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,oBAAoB
|
|
1
|
+
{"version":3,"file":"ledger-hw-app.js","sourceRoot":"","sources":["../src/ledger-hw-app.ts"],"names":[],"m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CAAC;QAEP,MAAM,OAAO,GAAG,QAAQ;aACrB,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,aAAa,CAAC,CAAC;aAC9B,QAAQ,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;QAEpC,OAAO;YACL,OAAO;YACP,OAAO;SACR,CAAC;IACJ,CAAC;CACF;AA/DD,wDA+DC","sourcesContent":["import LedgerHwAppEth from '@ledgerhq/hw-app-eth';\n// eslint-disable-next-line import/no-nodejs-modules\nimport { Buffer } from 'buffer';\n\nimport { GetAppNameAndVersionResponse } from './type';\n\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface MetaMaskLedgerHwAppEth extends LedgerHwAppEth {\n openEthApp(): void;\n closeApps(): void;\n getAppNameAndVersion(): Promise<GetAppNameAndVersionResponse>;\n}\n\nexport class MetaMaskLedgerHwAppEth\n extends LedgerHwAppEth\n implements MetaMaskLedgerHwAppEth\n{\n readonly mainAppName = 'BOLOS';\n\n readonly ethAppName = 'Ethereum';\n\n readonly transportEncoding = 'ascii';\n\n /**\n * Method to open ethereum application on ledger device.\n *\n */\n async openEthApp(): Promise<void> {\n await this.transport.send(\n 0xe0,\n 0xd8,\n 0x00,\n 0x00,\n Buffer.from(this.ethAppName, this.transportEncoding),\n );\n }\n\n /**\n * Method to close all running application on ledger device.\n *\n */\n async closeApps(): Promise<void> {\n await this.transport.send(0xb0, 0xa7, 0x00, 0x00);\n }\n\n /**\n * Method to retrieve the name and version of the running application in ledger device.\n *\n * @returns An object contains appName and version.\n */\n async getAppNameAndVersion(): Promise<GetAppNameAndVersionResponse> {\n const response = await this.transport.send(0xb0, 0x01, 0x00, 0x00);\n if (response[0] !== 1) {\n throw new Error('Incorrect format return from getAppNameAndVersion.');\n }\n\n let i = 1;\n const nameLength = response[i] ?? 0;\n i += 1;\n\n const appName = response\n .slice(i, (i += nameLength))\n .toString(this.transportEncoding);\n\n const versionLength = response[i] ?? 0;\n i += 1;\n\n const version = response\n .slice(i, (i += versionLength))\n .toString(this.transportEncoding);\n\n return {\n appName,\n version,\n };\n }\n}\n"]}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/// <reference types="web" />
|
|
1
2
|
import { GetPublicKeyParams, GetPublicKeyResponse, LedgerBridge, LedgerSignMessageParams, LedgerSignMessageResponse, LedgerSignTransactionParams, LedgerSignTransactionResponse, LedgerSignTypedDataParams, LedgerSignTypedDataResponse } from './ledger-bridge';
|
|
2
3
|
export declare enum IFrameMessageAction {
|
|
3
4
|
LedgerConnectionChange = "ledger-connection-change",
|
|
@@ -36,7 +36,7 @@ class LedgerIframeBridge {
|
|
|
36
36
|
this.messageCallbacks = {};
|
|
37
37
|
__classPrivateFieldGet(this, _LedgerIframeBridge_instances, "m", _LedgerIframeBridge_validateConfiguration).call(this, opts);
|
|
38
38
|
__classPrivateFieldSet(this, _LedgerIframeBridge_opts, {
|
|
39
|
-
bridgeUrl: opts
|
|
39
|
+
bridgeUrl: opts?.bridgeUrl,
|
|
40
40
|
}, "f");
|
|
41
41
|
}
|
|
42
42
|
async init() {
|
|
@@ -53,9 +53,8 @@ class LedgerIframeBridge {
|
|
|
53
53
|
return __classPrivateFieldGet(this, _LedgerIframeBridge_opts, "f");
|
|
54
54
|
}
|
|
55
55
|
async setOptions(opts) {
|
|
56
|
-
var _a;
|
|
57
56
|
__classPrivateFieldGet(this, _LedgerIframeBridge_instances, "m", _LedgerIframeBridge_validateConfiguration).call(this, opts);
|
|
58
|
-
if (
|
|
57
|
+
if (__classPrivateFieldGet(this, _LedgerIframeBridge_opts, "f")?.bridgeUrl !== opts.bridgeUrl) {
|
|
59
58
|
__classPrivateFieldGet(this, _LedgerIframeBridge_opts, "f").bridgeUrl = opts.bridgeUrl;
|
|
60
59
|
await this.destroy();
|
|
61
60
|
await this.init();
|
|
@@ -157,7 +156,11 @@ _LedgerIframeBridge_opts = new WeakMap(), _LedgerIframeBridge_instances = new We
|
|
|
157
156
|
}
|
|
158
157
|
}, _LedgerIframeBridge_sendMessage = function _LedgerIframeBridge_sendMessage(message, callback) {
|
|
159
158
|
this.currentMessageId += 1;
|
|
160
|
-
const postMsg =
|
|
159
|
+
const postMsg = {
|
|
160
|
+
...message,
|
|
161
|
+
messageId: this.currentMessageId,
|
|
162
|
+
target: LEDGER_IFRAME_ID,
|
|
163
|
+
};
|
|
161
164
|
this.messageCallbacks[this.currentMessageId] = callback;
|
|
162
165
|
if (!this.iframeLoaded || !this.iframe || !this.iframe.contentWindow) {
|
|
163
166
|
throw new Error('The iframe is not loaded yet');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ledger-iframe-bridge.js","sourceRoot":"","sources":["../src/ledger-iframe-bridge.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAYA,MAAM,gBAAgB,GAAG,eAAe,CAAC;AAEzC,IAAY,mBAQX;AARD,WAAY,mBAAmB;IAC7B,0EAAmD,CAAA;IACnD,qDAA8B,CAAA;IAC9B,wDAAiC,CAAA;IACjC,wEAAiD,CAAA;IACjD,wEAAiD,CAAA;IACjD,iFAA0D,CAAA;IAC1D,qEAA8C,CAAA;AAChD,CAAC,EARW,mBAAmB,GAAnB,2BAAmB,KAAnB,2BAAmB,QAQ9B;AAqED,MAAa,kBAAkB;IAqB7B,YACE,OAAkC;QAChC,SAAS,EAAE,sDAAsD;KAClE;;QAnBH,iBAAY,GAAG,KAAK,CAAC;QAErB,2CAAiC;QAOjC,sBAAiB,GAAG,KAAK,CAAC;QAE1B,qBAAgB,GAAG,CAAC,CAAC;QAErB,qBAAgB,GACd,EAAE,CAAC;QAOH,uBAAA,IAAI,gFAAuB,MAA3B,IAAI,EAAwB,IAAI,CAAC,CAAC;QAClC,uBAAA,IAAI,4BAAS;YACX,SAAS,EAAE,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,SAAS;SAC3B,MAAA,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,IAAI;QACR,MAAM,uBAAA,IAAI,sEAAa,MAAjB,IAAI,EA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MAAM,CAAC,aAAa,EAAE;QACpE,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;KACjD;IAED,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,WAAW,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;AACtD,CAAC,iGAEsB,IAA+B;IACpD,IAAI,OAAO,IAAI,CAAC,SAAS,KAAK,QAAQ,IAAI,IAAI,CAAC,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE;QACrE,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;KACjD;AACH,CAAC","sourcesContent":["import {\n GetPublicKeyParams,\n GetPublicKeyResponse,\n LedgerBridge,\n LedgerSignMessageParams,\n LedgerSignMessageResponse,\n LedgerSignTransactionParams,\n LedgerSignTransactionResponse,\n LedgerSignTypedDataParams,\n LedgerSignTypedDataResponse,\n} from './ledger-bridge';\n\nconst LEDGER_IFRAME_ID = 'LEDGER-IFRAME';\n\nexport enum IFrameMessageAction {\n LedgerConnectionChange = 'ledger-connection-change',\n LedgerUnlock = 'ledger-unlock',\n LedgerMakeApp = 'ledger-make-app',\n LedgerUpdateTransport = 'ledger-update-transport',\n LedgerSignTransaction = 'ledger-sign-transaction',\n LedgerSignPersonalMessage = 'ledger-sign-personal-message',\n LedgerSignTypedData = 'ledger-sign-typed-data',\n}\n\ntype IFrameMessageResponseStub<\n SuccessResult extends Record<string, unknown>,\n FailureResult = Error,\n> = {\n messageId: number;\n} & (\n | { success: true; payload: SuccessResult }\n | { success: false; payload: { error: FailureResult } }\n);\n\ntype LedgerConnectionChangeActionResponse = {\n messageId: number;\n action: IFrameMessageAction.LedgerConnectionChange;\n payload: { connected: boolean };\n};\n\ntype LedgerMakeAppActionResponse = {\n messageId: number;\n action: IFrameMessageAction.LedgerMakeApp;\n} & ({ success: true } | { success: false; error?: unknown });\n\ntype LedgerUpdateTransportActionResponse = {\n messageId: number;\n action: IFrameMessageAction.LedgerUpdateTransport;\n success: boolean;\n};\n\ntype LedgerUnlockActionResponse = {\n action: IFrameMessageAction.LedgerUnlock;\n} & IFrameMessageResponseStub<GetPublicKeyResponse>;\n\ntype LedgerSignTransactionActionResponse = {\n action: IFrameMessageAction.LedgerSignTransaction;\n} & IFrameMessageResponseStub<LedgerSignTransactionResponse>;\n\ntype LedgerSignPersonalMessageActionResponse = {\n action: IFrameMessageAction.LedgerSignPersonalMessage;\n} & IFrameMessageResponseStub<LedgerSignMessageResponse>;\n\ntype LedgerSignTypedDataActionResponse = {\n action: IFrameMessageAction.LedgerSignTypedData;\n} & IFrameMessageResponseStub<LedgerSignTypedDataResponse>;\n\nexport type IFrameMessageResponse =\n | LedgerConnectionChangeActionResponse\n | LedgerMakeAppActionResponse\n | LedgerUpdateTransportActionResponse\n | LedgerUnlockActionResponse\n | LedgerSignTransactionActionResponse\n | LedgerSignPersonalMessageActionResponse\n | LedgerSignTypedDataActionResponse;\n\ntype IFrameMessage<TAction extends IFrameMessageAction> = {\n action: TAction;\n params?: Readonly<Record<string, unknown>>;\n};\n\ntype IFramePostMessage<TAction extends IFrameMessageAction> =\n IFrameMessage<TAction> & {\n messageId: number;\n target: typeof LEDGER_IFRAME_ID;\n };\n\nexport type LedgerIframeBridgeOptions = {\n bridgeUrl: string;\n};\n\nexport class LedgerIframeBridge\n implements LedgerBridge<LedgerIframeBridgeOptions>\n{\n iframe?: HTMLIFrameElement;\n\n iframeLoaded = false;\n\n #opts: LedgerIframeBridgeOptions;\n\n eventListener?: (eventMessage: {\n origin: string;\n data: IFrameMessageResponse;\n }) => void;\n\n isDeviceConnected = false;\n\n currentMessageId = 0;\n\n messageCallbacks: Record<number, (response: IFrameMessageResponse) => void> =\n {};\n\n constructor(\n opts: LedgerIframeBridgeOptions = {\n bridgeUrl: 'https://metamask.github.io/eth-ledger-bridge-keyring',\n },\n ) {\n this.#validateConfiguration(opts);\n this.#opts = {\n bridgeUrl: opts?.bridgeUrl,\n };\n }\n\n async init() {\n await this.#setupIframe(this.#opts.bridgeUrl);\n\n this.eventListener = this.#eventListener.bind(this, this.#opts.bridgeUrl);\n\n window.addEventListener('message', this.eventListener);\n }\n\n async destroy() {\n if (this.eventListener) {\n window.removeEventListener('message', this.eventListener);\n }\n }\n\n async getOptions(): Promise<LedgerIframeBridgeOptions> {\n return this.#opts;\n }\n\n async setOptions(opts: LedgerIframeBridgeOptions): Promise<void> {\n this.#validateConfiguration(opts);\n if (this.#opts?.bridgeUrl !== opts.bridgeUrl) {\n this.#opts.bridgeUrl = opts.bridgeUrl;\n await this.destroy();\n await this.init();\n }\n }\n\n async attemptMakeApp(): Promise<boolean> {\n return new Promise((resolve, reject) => {\n this.#sendMessage(\n {\n action: IFrameMessageAction.LedgerMakeApp,\n },\n (response) => {\n if ('success' in response && response.success) {\n resolve(true);\n } else if ('error' in response) {\n reject(response.error);\n } else {\n reject(new Error('Unknown error occurred'));\n }\n },\n );\n });\n }\n\n async updateTransportMethod(transportType: string): Promise<boolean> {\n return new Promise((resolve, reject) => {\n // If the iframe isn't loaded yet, let's store the desired transportType value and\n // optimistically return a successful promise\n if (!this.iframeLoaded) {\n throw new Error('The iframe is not loaded yet');\n }\n\n this.#sendMessage(\n {\n action: IFrameMessageAction.LedgerUpdateTransport,\n params: { transportType },\n },\n (response) => {\n if ('success' in response && response.success) {\n return resolve(true);\n }\n return reject(new Error('Ledger transport could not be updated'));\n },\n );\n });\n }\n\n async getPublicKey(\n params: GetPublicKeyParams,\n ): Promise<GetPublicKeyResponse> {\n return this.#deviceActionMessage(IFrameMessageAction.LedgerUnlock, params);\n }\n\n async deviceSignTransaction(\n params: LedgerSignTransactionParams,\n ): Promise<LedgerSignTransactionResponse> {\n return this.#deviceActionMessage(\n IFrameMessageAction.LedgerSignTransaction,\n params,\n );\n }\n\n async deviceSignMessage(\n params: LedgerSignMessageParams,\n ): Promise<LedgerSignMessageResponse> {\n return this.#deviceActionMessage(\n IFrameMessageAction.LedgerSignPersonalMessage,\n params,\n );\n }\n\n async deviceSignTypedData(\n params: LedgerSignTypedDataParams,\n ): Promise<LedgerSignTypedDataResponse> {\n return this.#deviceActionMessage(\n IFrameMessageAction.LedgerSignTypedData,\n params,\n );\n }\n\n async #deviceActionMessage(\n action: IFrameMessageAction.LedgerUnlock,\n params: GetPublicKeyParams,\n ): Promise<GetPublicKeyResponse>;\n\n async #deviceActionMessage(\n action: IFrameMessageAction.LedgerSignTransaction,\n params: LedgerSignTransactionParams,\n ): Promise<LedgerSignTransactionResponse>;\n\n async #deviceActionMessage(\n action: IFrameMessageAction.LedgerSignPersonalMessage,\n params: LedgerSignMessageParams,\n ): Promise<LedgerSignMessageResponse>;\n\n async #deviceActionMessage(\n action: IFrameMessageAction.LedgerSignTypedData,\n params: LedgerSignTypedDataParams,\n ): Promise<LedgerSignTypedDataResponse>;\n\n async #deviceActionMessage(\n ...[action, params]:\n | [IFrameMessageAction.LedgerUnlock, GetPublicKeyParams]\n | [IFrameMessageAction.LedgerSignTransaction, LedgerSignTransactionParams]\n | [IFrameMessageAction.LedgerSignPersonalMessage, LedgerSignMessageParams]\n | [IFrameMessageAction.LedgerSignTypedData, LedgerSignTypedDataParams]\n ) {\n return new Promise((resolve, reject) => {\n this.#sendMessage(\n {\n action,\n params,\n },\n (response) => {\n if ('payload' in response && response.payload) {\n if ('success' in response && response.success) {\n return resolve(response.payload);\n }\n if ('error' in response.payload) {\n return reject(response.payload.error);\n }\n }\n return reject(new Error('Unknown error occurred'));\n },\n );\n });\n }\n\n async #setupIframe(bridgeUrl: string): Promise<void> {\n return new Promise((resolve) => {\n this.iframe = document.createElement('iframe');\n this.iframe.src = bridgeUrl;\n this.iframe.allow = `hid 'src'`;\n this.iframe.onload = async () => {\n this.iframeLoaded = true;\n resolve();\n };\n document.head.appendChild(this.iframe);\n });\n }\n\n #getOrigin(bridgeUrl: string) {\n const tmp = bridgeUrl.split('/');\n tmp.splice(-1, 1);\n return tmp.join('/');\n }\n\n #eventListener(\n bridgeUrl: string,\n eventMessage: {\n origin: string;\n data: IFrameMessageResponse;\n },\n ) {\n if (eventMessage.origin !== this.#getOrigin(bridgeUrl)) {\n return;\n }\n\n if (eventMessage.data) {\n const messageCallback =\n this.messageCallbacks[eventMessage.data.messageId];\n if (messageCallback) {\n messageCallback(eventMessage.data);\n } else if (\n eventMessage.data.action === IFrameMessageAction.LedgerConnectionChange\n ) {\n this.isDeviceConnected = eventMessage.data.payload.connected;\n }\n }\n }\n\n #sendMessage<TAction extends IFrameMessageAction>(\n message: IFrameMessage<TAction>,\n callback: (response: IFrameMessageResponse) => void,\n ) {\n this.currentMessageId += 1;\n\n const postMsg: IFramePostMessage<TAction> = {\n ...message,\n messageId: this.currentMessageId,\n target: LEDGER_IFRAME_ID,\n };\n\n this.messageCallbacks[this.currentMessageId] = callback;\n\n if (!this.iframeLoaded || !this.iframe || !this.iframe.contentWindow) {\n throw new Error('The iframe is not loaded yet');\n }\n\n this.iframe.contentWindow.postMessage(postMsg, '*');\n }\n\n #validateConfiguration(opts: LedgerIframeBridgeOptions): void {\n if (typeof opts.bridgeUrl !== 'string' || opts.bridgeUrl.length === 0) {\n throw new Error('bridgeURL is not a valid URL');\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"ledger-iframe-bridge.js","sourceRoot":"","sources":["../src/ledger-iframe-bridge.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAYA,MAAM,gBAAgB,GAAG,eAAe,CAAC;AAEzC,IAAY,mBAQX;AARD,WAAY,mBAAmB;IAC7B,0EAAmD,CAAA;IACnD,qDAA8B,CAAA;IAC9B,wDAAiC,CAAA;IACjC,wEAAiD,CAAA;IACjD,wEAAiD,CAAA;IACjD,iFAA0D,CAAA;IAC1D,qEAA8C,CAAA;AAChD,CAAC,EARW,mBAAmB,GAAnB,2BAAmB,KAAnB,2BAAmB,QAQ9B;AAqED,MAAa,kBAAkB;IAqB7B,YACE,OAAkC;QAChC,SAAS,EAAE,sDAAsD;KAClE;;QAnBH,iBAAY,GAAG,KAAK,CAAC;QAErB,2CAAiC;QAOjC,sBAAi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AAgB,IAAI,CAAC,CAAC;IAE3B,MAAM,OAAO,GAA+B;QAC1C,GAAG,OAAO;QACV,SAAS,EAAE,IAAI,CAAC,gBAAgB;QAChC,MAAM,EAAE,gBAAgB;KACzB,CAAC;IAEF,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,gBAAgB,CAAC,GAAG,QAAQ,CAAC;IAExD,IAAI,CAAC,IAAI,CAAC,YAAY,IAAI,CAAC,IAAI,CAAC,MAAM,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,aAAa,EAAE;QACpE,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;KACjD;IAED,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,WAAW,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;AACtD,CAAC,iGAEsB,IAA+B;IACpD,IAAI,OAAO,IAAI,CAAC,SAAS,KAAK,QAAQ,IAAI,IAAI,CAAC,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE;QACrE,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;KACjD;AACH,CAAC","sourcesContent":["import {\n GetPublicKeyParams,\n GetPublicKeyResponse,\n LedgerBridge,\n LedgerSignMessageParams,\n LedgerSignMessageResponse,\n LedgerSignTransactionParams,\n LedgerSignTransactionResponse,\n LedgerSignTypedDataParams,\n LedgerSignTypedDataResponse,\n} from './ledger-bridge';\n\nconst LEDGER_IFRAME_ID = 'LEDGER-IFRAME';\n\nexport enum IFrameMessageAction {\n LedgerConnectionChange = 'ledger-connection-change',\n LedgerUnlock = 'ledger-unlock',\n LedgerMakeApp = 'ledger-make-app',\n LedgerUpdateTransport = 'ledger-update-transport',\n LedgerSignTransaction = 'ledger-sign-transaction',\n LedgerSignPersonalMessage = 'ledger-sign-personal-message',\n LedgerSignTypedData = 'ledger-sign-typed-data',\n}\n\ntype IFrameMessageResponseStub<\n SuccessResult extends Record<string, unknown>,\n FailureResult = Error,\n> = {\n messageId: number;\n} & (\n | { success: true; payload: SuccessResult }\n | { success: false; payload: { error: FailureResult } }\n);\n\ntype LedgerConnectionChangeActionResponse = {\n messageId: number;\n action: IFrameMessageAction.LedgerConnectionChange;\n payload: { connected: boolean };\n};\n\ntype LedgerMakeAppActionResponse = {\n messageId: number;\n action: IFrameMessageAction.LedgerMakeApp;\n} & ({ success: true } | { success: false; error?: unknown });\n\ntype LedgerUpdateTransportActionResponse = {\n messageId: number;\n action: IFrameMessageAction.LedgerUpdateTransport;\n success: boolean;\n};\n\ntype LedgerUnlockActionResponse = {\n action: IFrameMessageAction.LedgerUnlock;\n} & IFrameMessageResponseStub<GetPublicKeyResponse>;\n\ntype LedgerSignTransactionActionResponse = {\n action: IFrameMessageAction.LedgerSignTransaction;\n} & IFrameMessageResponseStub<LedgerSignTransactionResponse>;\n\ntype LedgerSignPersonalMessageActionResponse = {\n action: IFrameMessageAction.LedgerSignPersonalMessage;\n} & IFrameMessageResponseStub<LedgerSignMessageResponse>;\n\ntype LedgerSignTypedDataActionResponse = {\n action: IFrameMessageAction.LedgerSignTypedData;\n} & IFrameMessageResponseStub<LedgerSignTypedDataResponse>;\n\nexport type IFrameMessageResponse =\n | LedgerConnectionChangeActionResponse\n | LedgerMakeAppActionResponse\n | LedgerUpdateTransportActionResponse\n | LedgerUnlockActionResponse\n | LedgerSignTransactionActionResponse\n | LedgerSignPersonalMessageActionResponse\n | LedgerSignTypedDataActionResponse;\n\ntype IFrameMessage<TAction extends IFrameMessageAction> = {\n action: TAction;\n params?: Readonly<Record<string, unknown>>;\n};\n\ntype IFramePostMessage<TAction extends IFrameMessageAction> =\n IFrameMessage<TAction> & {\n messageId: number;\n target: typeof LEDGER_IFRAME_ID;\n };\n\nexport type LedgerIframeBridgeOptions = {\n bridgeUrl: string;\n};\n\nexport class LedgerIframeBridge\n implements LedgerBridge<LedgerIframeBridgeOptions>\n{\n iframe?: HTMLIFrameElement;\n\n iframeLoaded = false;\n\n #opts: LedgerIframeBridgeOptions;\n\n eventListener?: (eventMessage: {\n origin: string;\n data: IFrameMessageResponse;\n }) => void;\n\n isDeviceConnected = false;\n\n currentMessageId = 0;\n\n messageCallbacks: Record<number, (response: IFrameMessageResponse) => void> =\n {};\n\n constructor(\n opts: LedgerIframeBridgeOptions = {\n bridgeUrl: 'https://metamask.github.io/eth-ledger-bridge-keyring',\n },\n ) {\n this.#validateConfiguration(opts);\n this.#opts = {\n bridgeUrl: opts?.bridgeUrl,\n };\n }\n\n async init() {\n await this.#setupIframe(this.#opts.bridgeUrl);\n\n this.eventListener = this.#eventListener.bind(this, this.#opts.bridgeUrl);\n\n window.addEventListener('message', this.eventListener);\n }\n\n async destroy() {\n if (this.eventListener) {\n window.removeEventListener('message', this.eventListener);\n }\n }\n\n async getOptions(): Promise<LedgerIframeBridgeOptions> {\n return this.#opts;\n }\n\n async setOptions(opts: LedgerIframeBridgeOptions): Promise<void> {\n this.#validateConfiguration(opts);\n if (this.#opts?.bridgeUrl !== opts.bridgeUrl) {\n this.#opts.bridgeUrl = opts.bridgeUrl;\n await this.destroy();\n await this.init();\n }\n }\n\n async attemptMakeApp(): Promise<boolean> {\n return new Promise((resolve, reject) => {\n this.#sendMessage(\n {\n action: IFrameMessageAction.LedgerMakeApp,\n },\n (response) => {\n if ('success' in response && response.success) {\n resolve(true);\n } else if ('error' in response) {\n reject(response.error);\n } else {\n reject(new Error('Unknown error occurred'));\n }\n },\n );\n });\n }\n\n async updateTransportMethod(transportType: string): Promise<boolean> {\n return new Promise((resolve, reject) => {\n // If the iframe isn't loaded yet, let's store the desired transportType value and\n // optimistically return a successful promise\n if (!this.iframeLoaded) {\n throw new Error('The iframe is not loaded yet');\n }\n\n this.#sendMessage(\n {\n action: IFrameMessageAction.LedgerUpdateTransport,\n params: { transportType },\n },\n (response) => {\n if ('success' in response && response.success) {\n return resolve(true);\n }\n return reject(new Error('Ledger transport could not be updated'));\n },\n );\n });\n }\n\n async getPublicKey(\n params: GetPublicKeyParams,\n ): Promise<GetPublicKeyResponse> {\n return this.#deviceActionMessage(IFrameMessageAction.LedgerUnlock, params);\n }\n\n async deviceSignTransaction(\n params: LedgerSignTransactionParams,\n ): Promise<LedgerSignTransactionResponse> {\n return this.#deviceActionMessage(\n IFrameMessageAction.LedgerSignTransaction,\n params,\n );\n }\n\n async deviceSignMessage(\n params: LedgerSignMessageParams,\n ): Promise<LedgerSignMessageResponse> {\n return this.#deviceActionMessage(\n IFrameMessageAction.LedgerSignPersonalMessage,\n params,\n );\n }\n\n async deviceSignTypedData(\n params: LedgerSignTypedDataParams,\n ): Promise<LedgerSignTypedDataResponse> {\n return this.#deviceActionMessage(\n IFrameMessageAction.LedgerSignTypedData,\n params,\n );\n }\n\n async #deviceActionMessage(\n action: IFrameMessageAction.LedgerUnlock,\n params: GetPublicKeyParams,\n ): Promise<GetPublicKeyResponse>;\n\n async #deviceActionMessage(\n action: IFrameMessageAction.LedgerSignTransaction,\n params: LedgerSignTransactionParams,\n ): Promise<LedgerSignTransactionResponse>;\n\n async #deviceActionMessage(\n action: IFrameMessageAction.LedgerSignPersonalMessage,\n params: LedgerSignMessageParams,\n ): Promise<LedgerSignMessageResponse>;\n\n async #deviceActionMessage(\n action: IFrameMessageAction.LedgerSignTypedData,\n params: LedgerSignTypedDataParams,\n ): Promise<LedgerSignTypedDataResponse>;\n\n async #deviceActionMessage(\n ...[action, params]:\n | [IFrameMessageAction.LedgerUnlock, GetPublicKeyParams]\n | [IFrameMessageAction.LedgerSignTransaction, LedgerSignTransactionParams]\n | [IFrameMessageAction.LedgerSignPersonalMessage, LedgerSignMessageParams]\n | [IFrameMessageAction.LedgerSignTypedData, LedgerSignTypedDataParams]\n ) {\n return new Promise((resolve, reject) => {\n this.#sendMessage(\n {\n action,\n params,\n },\n (response) => {\n if ('payload' in response && response.payload) {\n if ('success' in response && response.success) {\n return resolve(response.payload);\n }\n if ('error' in response.payload) {\n return reject(response.payload.error);\n }\n }\n return reject(new Error('Unknown error occurred'));\n },\n );\n });\n }\n\n async #setupIframe(bridgeUrl: string): Promise<void> {\n return new Promise((resolve) => {\n this.iframe = document.createElement('iframe');\n this.iframe.src = bridgeUrl;\n this.iframe.allow = `hid 'src'`;\n this.iframe.onload = async () => {\n this.iframeLoaded = true;\n resolve();\n };\n document.head.appendChild(this.iframe);\n });\n }\n\n #getOrigin(bridgeUrl: string) {\n const tmp = bridgeUrl.split('/');\n tmp.splice(-1, 1);\n return tmp.join('/');\n }\n\n #eventListener(\n bridgeUrl: string,\n eventMessage: {\n origin: string;\n data: IFrameMessageResponse;\n },\n ) {\n if (eventMessage.origin !== this.#getOrigin(bridgeUrl)) {\n return;\n }\n\n if (eventMessage.data) {\n const messageCallback =\n this.messageCallbacks[eventMessage.data.messageId];\n if (messageCallback) {\n messageCallback(eventMessage.data);\n } else if (\n eventMessage.data.action === IFrameMessageAction.LedgerConnectionChange\n ) {\n this.isDeviceConnected = eventMessage.data.payload.connected;\n }\n }\n }\n\n #sendMessage<TAction extends IFrameMessageAction>(\n message: IFrameMessage<TAction>,\n callback: (response: IFrameMessageResponse) => void,\n ) {\n this.currentMessageId += 1;\n\n const postMsg: IFramePostMessage<TAction> = {\n ...message,\n messageId: this.currentMessageId,\n target: LEDGER_IFRAME_ID,\n };\n\n this.messageCallbacks[this.currentMessageId] = callback;\n\n if (!this.iframeLoaded || !this.iframe || !this.iframe.contentWindow) {\n throw new Error('The iframe is not loaded yet');\n }\n\n this.iframe.contentWindow.postMessage(postMsg, '*');\n }\n\n #validateConfiguration(opts: LedgerIframeBridgeOptions): void {\n if (typeof opts.bridgeUrl !== 'string' || opts.bridgeUrl.length === 0) {\n throw new Error('bridgeURL is not a valid URL');\n }\n }\n}\n"]}
|
package/dist/ledger-keyring.js
CHANGED
|
@@ -107,15 +107,14 @@ class LedgerKeyring extends events_1.EventEmitter {
|
|
|
107
107
|
};
|
|
108
108
|
}
|
|
109
109
|
async deserialize(opts = {}) {
|
|
110
|
-
|
|
111
|
-
this.
|
|
112
|
-
this.
|
|
113
|
-
this.
|
|
114
|
-
this.accountDetails = (_d = opts.accountDetails) !== null && _d !== void 0 ? _d : {};
|
|
110
|
+
this.hdPath = opts.hdPath ?? hdPathString;
|
|
111
|
+
this.accounts = opts.accounts ?? [];
|
|
112
|
+
this.deviceId = opts.deviceId ?? '';
|
|
113
|
+
this.accountDetails = opts.accountDetails ?? {};
|
|
115
114
|
if (!opts.accountDetails) {
|
|
116
115
|
__classPrivateFieldGet(this, _LedgerKeyring_instances, "m", _LedgerKeyring_migrateAccountDetails).call(this, opts);
|
|
117
116
|
}
|
|
118
|
-
this.implementFullBIP44 =
|
|
117
|
+
this.implementFullBIP44 = opts.implementFullBIP44 ?? false;
|
|
119
118
|
const keys = new Set(Object.keys(this.accountDetails));
|
|
120
119
|
// Remove accounts that don't have corresponding account details
|
|
121
120
|
this.accounts = this.accounts.filter((account) => keys.has(ethUtil.toChecksumAddress(account)));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ledger-keyring.js","sourceRoot":"","sources":["../src/ledger-keyring.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,yCAAsC;AACtC,uCAA8E;AAC9E,0DAA4C;AAE5C,yDAKgC;AAChC,oDAAoD;AACpD,mCAAgC;AAEhC,oDAAoD;AACpD,mCAAsC;AACtC,kDAA0B;AAK1B,MAAM,QAAQ,GAAG,GAAG,CAAC;AACrB,MAAM,YAAY,GAAG,GAAG,QAAQ,aAAa,CAAC;AAC9C,MAAM,WAAW,GAAG,iBAAiB,CAAC;AAEtC,+GAA+G;AAC/G,MAAM,SAAS,GAAG,IAAI,CAAC;AAEvB,IAAK,cAKJ;AALD,WAAK,cAAc;IACjB,8DAA4C,CAAA;IAC5C,0DAAwC,CAAA;IACxC,8DAA4C,CAAA;IAC5C,sDAAoC,CAAA;AACtC,CAAC,EALI,cAAc,KAAd,cAAc,QAKlB;AAqBD;;;;;;;;;;;;GAYG;AACH,SAAS,sBAAsB,CAC7B,EAA0C;IAE1C,OAAO,YAAY,IAAI,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C5C,MAAM,MAAM,GAAG,uBAAA,IAAI,0DAAW,MAAf,IAAI,CAAa,CAAC;IACjC,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,KAAK,CACjC,GAAG,MAAM,6CAA6C,OAAO,6BAA6B,CAC3F,CAAC;IACF,MAAM,cAAc,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;IAC7C,OAAO,cAAc,CAAC,MAAM,KAAK,GAAG,IAAI,cAAc,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC;AAC3E,CAAC;IAGC,OAAO,IAAI,CAAC,OAAO,CAAC;AACtB,CAAC;AAllBM,kBAAI,GAAW,WAAW,CAAC","sourcesContent":["import { RLP } from '@ethereumjs/rlp';\nimport { TransactionFactory, TxData, TypedTransaction } from '@ethereumjs/tx';\nimport * as ethUtil from '@ethereumjs/util';\nimport type { MessageTypes, TypedMessage } from '@metamask/eth-sig-util';\nimport {\n recoverPersonalSignature,\n recoverTypedSignature,\n SignTypedDataVersion,\n TypedDataUtils,\n} from '@metamask/eth-sig-util';\n// eslint-disable-next-line import/no-nodejs-modules\nimport { Buffer } from 'buffer';\nimport type OldEthJsTransaction from 'ethereumjs-tx';\n// eslint-disable-next-line import/no-nodejs-modules\nimport { EventEmitter } from 'events';\nimport HDKey from 'hdkey';\n\nimport { LedgerBridge, LedgerBridgeOptions } from './ledger-bridge';\nimport { LedgerIframeBridgeOptions } from './ledger-iframe-bridge';\n\nconst pathBase = 'm';\nconst hdPathString = `${pathBase}/44'/60'/0'`;\nconst keyringType = 'Ledger Hardware';\n\n// This number causes one of our failing tests to run very slowly, as the for loop needs to iterate 1000 times.\nconst MAX_INDEX = 1000;\n\nenum NetworkApiUrls {\n Ropsten = 'https://api-ropsten.etherscan.io',\n Kovan = 'https://api-kovan.etherscan.io',\n Rinkeby = 'https://api-rinkeby.etherscan.io',\n Mainnet = `https://api.etherscan.io`,\n}\n\ntype SignTransactionPayload = Awaited<\n ReturnType<LedgerBridge<LedgerIframeBridgeOptions>['deviceSignTransaction']>\n>;\n\nexport type AccountDetails = {\n index?: number;\n bip44?: boolean;\n hdPath?: string;\n};\n\nexport type LedgerBridgeKeyringOptions = {\n hdPath: string;\n accounts: readonly string[];\n deviceId: string;\n accountDetails: Readonly<Record<string, AccountDetails>>;\n accountIndexes: Readonly<Record<string, number>>;\n implementFullBIP44: boolean;\n};\n\n/**\n * Check if the given transaction is made with ethereumjs-tx or @ethereumjs/tx\n *\n * Transactions built with older versions of ethereumjs-tx have a\n * getChainId method that newer versions do not.\n * Older versions are mutable\n * while newer versions default to being immutable.\n * Expected shape and type\n * of data for v, r and s differ (Buffer (old) vs BN (new)).\n *\n * @param tx - Transaction to check, instance of either ethereumjs-tx or @ethereumjs/tx.\n * @returns Returns `true` if tx is an old-style ethereumjs-tx transaction.\n */\nfunction isOldStyleEthereumjsTx(\n tx: TypedTransaction | OldEthJsTransaction,\n): tx is OldEthJsTransaction {\n return 'getChainId' in tx && typeof tx.getChainId === 'function';\n}\n\nexport class LedgerKeyring extends EventEmitter {\n static type: string = keyringType;\n\n deviceId = '';\n\n readonly type: string = keyringType;\n\n page = 0;\n\n perPage = 5;\n\n unlockedAccount = 0;\n\n accounts: readonly string[] = [];\n\n accountDetails: Record<string, AccountDetails> = {};\n\n hdk = new HDKey();\n\n hdPath = hdPathString;\n\n paths: Record<string, number> = {};\n\n network: NetworkApiUrls = NetworkApiUrls.Mainnet;\n\n implementFullBIP44 = false;\n\n bridge: LedgerBridge<LedgerBridgeOptions>;\n\n constructor({ bridge }: { bridge: LedgerBridge<LedgerBridgeOptions> }) {\n super();\n\n if (!bridge) {\n throw new Error('Bridge is a required dependency for the keyring');\n }\n\n this.bridge = bridge;\n }\n\n async init() {\n return this.bridge.init();\n }\n\n async destroy() {\n return this.bridge.destroy();\n }\n\n async serialize() {\n return {\n hdPath: this.hdPath,\n accounts: this.accounts,\n deviceId: this.deviceId,\n accountDetails: this.accountDetails,\n implementFullBIP44: false,\n };\n }\n\n async deserialize(opts: Partial<LedgerBridgeKeyringOptions> = {}) {\n this.hdPath = opts.hdPath ?? hdPathString;\n this.accounts = opts.accounts ?? [];\n this.deviceId = opts.deviceId ?? '';\n this.accountDetails = opts.accountDetails ?? {};\n\n if (!opts.accountDetails) {\n this.#migrateAccountDetails(opts);\n }\n\n this.implementFullBIP44 = opts.implementFullBIP44 ?? false;\n\n const keys = new Set<string>(Object.keys(this.accountDetails));\n // Remove accounts that don't have corresponding account details\n this.accounts = this.accounts.filter((account) =>\n keys.has(ethUtil.toChecksumAddress(account)),\n );\n\n return Promise.resolve();\n }\n\n public setDeviceId(deviceId: string) {\n this.deviceId = deviceId;\n }\n\n public getDeviceId() {\n return this.deviceId;\n }\n\n #migrateAccountDetails(opts: Partial<LedgerBridgeKeyringOptions>) {\n if (this.#isLedgerLiveHdPath() && opts.accountIndexes) {\n for (const [account, index] of Object.entries(opts.accountIndexes)) {\n this.accountDetails[account] = {\n bip44: true,\n hdPath: this.#getPathForIndex(index),\n };\n }\n }\n const keys = new Set<string>(Object.keys(this.accountDetails));\n // try to migrate non-LedgerLive accounts too\n if (!this.#isLedgerLiveHdPath()) {\n this.accounts.forEach((account) => {\n const key = ethUtil.toChecksumAddress(account);\n\n if (!keys.has(key)) {\n this.accountDetails[key] = {\n bip44: false,\n hdPath: this.#pathFromAddress(account),\n };\n }\n });\n }\n }\n\n isUnlocked() {\n return Boolean(this.hdk.publicKey);\n }\n\n isConnected() {\n return this.bridge.isDeviceConnected;\n }\n\n setAccountToUnlock(index: number) {\n this.unlockedAccount = index;\n }\n\n setHdPath(hdPath: string) {\n // Reset HDKey if the path changes\n if (this.hdPath !== hdPath) {\n this.hdk = new HDKey();\n }\n this.hdPath = hdPath;\n }\n\n async unlock(hdPath?: string, updateHdk = true): Promise<string> {\n if (this.isUnlocked() && !hdPath) {\n return 'already unlocked';\n }\n const path = hdPath ? this.#toLedgerPath(hdPath) : this.hdPath;\n\n let payload;\n try {\n payload = await this.bridge.getPublicKey({\n hdPath: path,\n });\n } catch (error) {\n throw error instanceof Error\n ? error\n : new Error('Ledger Ethereum app closed. Open it to unlock.');\n }\n\n if (updateHdk && payload.chainCode) {\n this.hdk.publicKey = Buffer.from(payload.publicKey, 'hex');\n this.hdk.chainCode = Buffer.from(payload.chainCode, 'hex');\n }\n\n return payload.address;\n }\n\n async addAccounts(amount = 1): Promise<string[]> {\n return new Promise((resolve, reject) => {\n this.unlock()\n .then(async (_) => {\n const from = this.unlockedAccount;\n const to = from + amount;\n for (let i = from; i < to; i++) {\n const path = this.#getPathForIndex(i);\n let address;\n if (this.#isLedgerLiveHdPath()) {\n address = await this.unlock(path);\n } else {\n address = this.#addressFromIndex(pathBase, i);\n }\n\n this.accountDetails[ethUtil.toChecksumAddress(address)] = {\n // TODO: consider renaming this property, as the current name is misleading\n // It's currently used to represent whether an account uses the Ledger Live path.\n bip44: this.#isLedgerLiveHdPath(),\n hdPath: path,\n };\n\n if (!this.accounts.includes(address)) {\n this.accounts = [...this.accounts, address];\n }\n this.page = 0;\n }\n resolve(this.accounts.slice());\n })\n .catch(reject);\n });\n }\n\n getName() {\n return keyringType;\n }\n\n async getFirstPage() {\n this.page = 0;\n return this.#getPage(1);\n }\n\n async getNextPage() {\n return this.#getPage(1);\n }\n\n async getPreviousPage() {\n return this.#getPage(-1);\n }\n\n async getAccounts() {\n return Promise.resolve(this.accounts.slice());\n }\n\n removeAccount(address: string) {\n const filteredAccounts = this.accounts.filter(\n (a) => a.toLowerCase() !== address.toLowerCase(),\n );\n\n if (filteredAccounts.length === this.accounts.length) {\n throw new Error(`Address ${address} not found in this keyring`);\n }\n\n this.accounts = filteredAccounts;\n delete this.accountDetails[ethUtil.toChecksumAddress(address)];\n }\n\n async attemptMakeApp() {\n return this.bridge.attemptMakeApp();\n }\n\n async updateTransportMethod(transportType: string) {\n return this.bridge.updateTransportMethod(transportType);\n }\n\n // tx is an instance of the ethereumjs-transaction class.\n async signTransaction(\n address: string,\n tx: TypedTransaction | OldEthJsTransaction,\n ): Promise<TypedTransaction | OldEthJsTransaction> {\n let rawTxHex;\n // transactions built with older versions of ethereumjs-tx have a\n // getChainId method that newer versions do not. Older versions are mutable\n // while newer versions default to being immutable. Expected shape and type\n // of data for v, r and s differ (Buffer (old) vs BN (new))\n if (isOldStyleEthereumjsTx(tx)) {\n // In this version of ethereumjs-tx we must add the chainId in hex format\n // to the initial v value. The chainId must be included in the serialized\n // transaction which is only communicated to ethereumjs-tx in this\n // value. In newer versions the chainId is communicated via the 'Common'\n // object.\n // @ts-expect-error tx.v should be a Buffer, but we are assigning a string\n tx.v = ethUtil.bufferToHex(tx.getChainId());\n // @ts-expect-error tx.r should be a Buffer, but we are assigning a string\n tx.r = '0x00';\n // @ts-expect-error tx.s should be a Buffer, but we are assigning a string\n tx.s = '0x00';\n\n rawTxHex = tx.serialize().toString('hex');\n\n return this.#signTransaction(address, rawTxHex, (payload) => {\n tx.v = Buffer.from(payload.v, 'hex');\n tx.r = Buffer.from(payload.r, 'hex');\n tx.s = Buffer.from(payload.s, 'hex');\n return tx;\n });\n }\n\n // The below `encode` call is only necessary for legacy transactions, as `getMessageToSign`\n // calls `rlp.encode` internally for non-legacy transactions. As per the \"Transaction Execution\"\n // section of the ethereum yellow paper, transactions need to be \"well-formed RLP, with no additional\n // trailing bytes\".\n\n // Note also that `getMessageToSign` will return valid RLP for all transaction types, whereas the\n // `serialize` method will not for any transaction type except legacy. This is because `serialize` includes\n // empty r, s and v values in the encoded rlp. This is why we use `getMessageToSign` here instead of `serialize`.\n const messageToSign = tx.getMessageToSign(false);\n\n rawTxHex = Buffer.isBuffer(messageToSign)\n ? messageToSign.toString('hex')\n : Buffer.from(RLP.encode(messageToSign)).toString('hex');\n\n return this.#signTransaction(address, rawTxHex, (payload) => {\n // Because tx will be immutable, first get a plain javascript object that\n // represents the transaction. Using txData here as it aligns with the\n // nomenclature of ethereumjs/tx.\n const txData: TxData = tx.toJSON();\n // The fromTxData utility expects a type to support transactions with a type other than 0\n txData.type = tx.type;\n // The fromTxData utility expects v,r and s to be hex prefixed\n txData.v = ethUtil.addHexPrefix(payload.v);\n txData.r = ethUtil.addHexPrefix(payload.r);\n txData.s = ethUtil.addHexPrefix(payload.s);\n // Adopt the 'common' option from the original transaction and set the\n // returned object to be frozen if the original is frozen.\n return TransactionFactory.fromTxData(txData, {\n common: tx.common,\n freeze: Object.isFrozen(tx),\n });\n });\n }\n\n async #signTransaction(\n address: string,\n rawTxHex: string,\n handleSigning: (\n payload: SignTransactionPayload,\n ) => TypedTransaction | OldEthJsTransaction,\n ): Promise<TypedTransaction | OldEthJsTransaction> {\n const hdPath = await this.unlockAccountByAddress(address);\n\n if (!hdPath) {\n throw new Error('Ledger: Unknown error while signing transaction');\n }\n\n let payload;\n try {\n payload = await this.bridge.deviceSignTransaction({\n tx: rawTxHex,\n hdPath,\n });\n } catch (error) {\n throw error instanceof Error\n ? error\n : new Error('Ledger: Unknown error while signing transaction');\n }\n\n const newOrMutatedTx = handleSigning(payload);\n const valid = newOrMutatedTx.verifySignature();\n if (valid) {\n return newOrMutatedTx;\n }\n throw new Error('Ledger: The transaction signature is not valid');\n }\n\n async signMessage(withAccount: string, data: string) {\n return this.signPersonalMessage(withAccount, data);\n }\n\n // For personal_sign, we need to prefix the message:\n async signPersonalMessage(withAccount: string, message: string) {\n const hdPath = await this.unlockAccountByAddress(withAccount);\n\n if (!hdPath) {\n throw new Error('Ledger: Unknown error while signing message');\n }\n\n let payload;\n try {\n payload = await this.bridge.deviceSignMessage({\n hdPath,\n message: ethUtil.stripHexPrefix(message),\n });\n } catch (error) {\n throw error instanceof Error\n ? error\n : new Error('Ledger: Unknown error while signing message');\n }\n\n let modifiedV = parseInt(String(payload.v), 10).toString(16);\n if (modifiedV.length < 2) {\n modifiedV = `0${modifiedV}`;\n }\n\n const signature = `0x${payload.r}${payload.s}${modifiedV}`;\n const addressSignedWith = recoverPersonalSignature({\n data: message,\n signature,\n });\n if (\n ethUtil.toChecksumAddress(addressSignedWith) !==\n ethUtil.toChecksumAddress(withAccount)\n ) {\n throw new Error('Ledger: The signature doesnt match the right address');\n }\n return signature;\n }\n\n async unlockAccountByAddress(address: string) {\n const checksummedAddress = ethUtil.toChecksumAddress(address);\n const accountDetails = this.accountDetails[checksummedAddress];\n if (!accountDetails) {\n throw new Error(\n `Ledger: Account for address '${checksummedAddress}' not found`,\n );\n }\n const { hdPath } = accountDetails;\n const unlockedAddress = await this.unlock(hdPath, false);\n\n // unlock resolves to the address for the given hdPath as reported by the ledger device\n // if that address is not the requested address, then this account belongs to a different device or seed\n if (unlockedAddress.toLowerCase() !== address.toLowerCase()) {\n throw new Error(\n `Ledger: Account ${address} does not belong to the connected device`,\n );\n }\n return hdPath;\n }\n\n async signTypedData<T extends MessageTypes>(\n withAccount: string,\n data: TypedMessage<T>,\n options: { version?: string } = {},\n ) {\n const isV4 = options.version === 'V4';\n if (!isV4) {\n throw new Error(\n 'Ledger: Only version 4 of typed data signing is supported',\n );\n }\n\n const { domain, types, primaryType, message } =\n TypedDataUtils.sanitizeData(data);\n const domainSeparatorHex = TypedDataUtils.hashStruct(\n 'EIP712Domain',\n domain,\n types,\n SignTypedDataVersion.V4,\n ).toString('hex');\n const hashStructMessageHex = TypedDataUtils.hashStruct(\n primaryType.toString(),\n message,\n types,\n SignTypedDataVersion.V4,\n ).toString('hex');\n\n const hdPath = await this.unlockAccountByAddress(withAccount);\n\n if (!hdPath) {\n throw new Error('Ledger: Unknown error while signing message');\n }\n\n let payload;\n try {\n payload = await this.bridge.deviceSignTypedData({\n hdPath,\n domainSeparatorHex,\n hashStructMessageHex,\n });\n } catch (error) {\n throw error instanceof Error\n ? error\n : new Error('Ledger: Unknown error while signing message');\n }\n\n let recoveryId = parseInt(String(payload.v), 10).toString(16);\n if (recoveryId.length < 2) {\n recoveryId = `0${recoveryId}`;\n }\n const signature = `0x${payload.r}${payload.s}${recoveryId}`;\n const addressSignedWith = recoverTypedSignature({\n data,\n signature,\n version: SignTypedDataVersion.V4,\n });\n\n if (\n ethUtil.toChecksumAddress(addressSignedWith) !==\n ethUtil.toChecksumAddress(withAccount)\n ) {\n throw new Error('Ledger: The signature doesnt match the right address');\n }\n return signature;\n }\n\n exportAccount() {\n throw new Error('Not supported on this device');\n }\n\n forgetDevice() {\n this.accounts = [];\n this.page = 0;\n this.unlockedAccount = 0;\n this.paths = {};\n this.accountDetails = {};\n this.hdk = new HDKey();\n }\n\n /* PRIVATE METHODS */\n async #getPage(increment: number) {\n this.page += increment;\n\n if (this.page <= 0) {\n this.page = 1;\n }\n const from = (this.page - 1) * this.perPage;\n const to = from + this.perPage;\n\n await this.unlock();\n let accounts;\n if (this.#isLedgerLiveHdPath()) {\n accounts = await this.#getAccountsBIP44(from, to);\n } else {\n accounts = this.#getAccountsLegacy(from, to);\n }\n return accounts;\n }\n\n async #getAccountsBIP44(from: number, to: number) {\n const accounts: {\n address: string;\n balance: number | null;\n index: number;\n }[] = [];\n\n for (let i = from; i < to; i++) {\n const path = this.#getPathForIndex(i);\n const address = await this.unlock(path);\n const valid = this.implementFullBIP44\n ? await this.#hasPreviousTransactions(address)\n : true;\n accounts.push({\n address,\n balance: null,\n index: i,\n });\n\n // PER BIP44\n // \"Software should prevent a creation of an account if\n // a previous account does not have a transaction history\n // (meaning none of its addresses have been used before).\"\n if (!valid) {\n break;\n }\n }\n return accounts;\n }\n\n #getAccountsLegacy(from: number, to: number) {\n const accounts: {\n address: string;\n balance: number | null;\n index: number;\n }[] = [];\n\n for (let i = from; i < to; i++) {\n const address = this.#addressFromIndex(pathBase, i);\n accounts.push({\n address,\n balance: null,\n index: i,\n });\n this.paths[ethUtil.toChecksumAddress(address)] = i;\n }\n return accounts;\n }\n\n #addressFromIndex(basePath: string, i: number) {\n const dkey = this.hdk.derive(`${basePath}/${i}`);\n const address = ethUtil\n .publicToAddress(dkey.publicKey, true)\n .toString('hex');\n return ethUtil.toChecksumAddress(`0x${address}`);\n }\n\n #pathFromAddress(address: string) {\n const checksummedAddress = ethUtil.toChecksumAddress(address);\n let index = this.paths[checksummedAddress];\n if (typeof index === 'undefined') {\n for (let i = 0; i < MAX_INDEX; i++) {\n if (checksummedAddress === this.#addressFromIndex(pathBase, i)) {\n index = i;\n break;\n }\n }\n }\n\n if (typeof index === 'undefined') {\n throw new Error('Unknown address');\n }\n return this.#getPathForIndex(index);\n }\n\n #getPathForIndex(index: number) {\n // Check if the path is BIP 44 (Ledger Live)\n return this.#isLedgerLiveHdPath()\n ? `m/44'/60'/${index}'/0/0`\n : `${this.hdPath}/${index}`;\n }\n\n #isLedgerLiveHdPath() {\n return this.hdPath === `m/44'/60'/0'/0/0`;\n }\n\n #toLedgerPath(path: string) {\n return path.toString().replace('m/', '');\n }\n\n async #hasPreviousTransactions(address: string) {\n const apiUrl = this.#getApiUrl();\n const response = await window.fetch(\n `${apiUrl}/api?module=account&action=txlist&address=${address}&tag=latest&page=1&offset=1`,\n );\n const parsedResponse = await response.json();\n return parsedResponse.status !== '0' && parsedResponse.result.length > 0;\n }\n\n #getApiUrl() {\n return this.network;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"ledger-keyring.js","sourceRoot":"","sources":["../src/ledger-keyring.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,yCAAsC;AACtC,uCAA8E;AAC9E,0DAA4C;AAE5C,yDAKgC;AAChC,oDAAoD;AACpD,mCAAgC;AAEhC,oDAAoD;AACpD,mCAAsC;AACtC,kDAA0B;AAK1B,MAAM,QAAQ,GAAG,GAAG,CAAC;AACrB,MAAM,YAAY,GAAG,GAAG,QAAQ,aAAa,CAAC;AAC9C,MAAM,WAAW,GAAG,iBAAiB,CAAC;AAEtC,+GAA+G;AAC/G,MAAM,SAAS,GAAG,IAAI,CAAC;AAEvB,IAAK,cAKJ;AALD,WAAK,cAAc;IACjB,8DAA4C,CAAA;IAC5C,0DAAwC,CAAA;IACxC,8DAA4C,CAAA;IAC5C,sDAAoC,CAAA;AACtC,CAAC,EALI,cAAc,KAAd,cAAc,QA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AAC,CAAC;AAC3C,CAAC,2CAED,KAAK,iDAA0B,OAAe;IAC5C,MAAM,MAAM,GAAG,uBAAA,IAAI,0DAAW,MAAf,IAAI,CAAa,CAAC;IACjC,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,KAAK,CACjC,GAAG,MAAM,6CAA6C,OAAO,6BAA6B,CAC3F,CAAC;IACF,MAAM,cAAc,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;IAC7C,OAAO,cAAc,CAAC,MAAM,KAAK,GAAG,IAAI,cAAc,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC;AAC3E,CAAC;IAGC,OAAO,IAAI,CAAC,OAAO,CAAC;AACtB,CAAC;AAllBM,kBAAI,GAAW,WAAW,CAAC","sourcesContent":["import { RLP } from '@ethereumjs/rlp';\nimport { TransactionFactory, TxData, TypedTransaction } from '@ethereumjs/tx';\nimport * as ethUtil from '@ethereumjs/util';\nimport type { MessageTypes, TypedMessage } from '@metamask/eth-sig-util';\nimport {\n recoverPersonalSignature,\n recoverTypedSignature,\n SignTypedDataVersion,\n TypedDataUtils,\n} from '@metamask/eth-sig-util';\n// eslint-disable-next-line import/no-nodejs-modules\nimport { Buffer } from 'buffer';\nimport type OldEthJsTransaction from 'ethereumjs-tx';\n// eslint-disable-next-line import/no-nodejs-modules\nimport { EventEmitter } from 'events';\nimport HDKey from 'hdkey';\n\nimport { LedgerBridge, LedgerBridgeOptions } from './ledger-bridge';\nimport { LedgerIframeBridgeOptions } from './ledger-iframe-bridge';\n\nconst pathBase = 'm';\nconst hdPathString = `${pathBase}/44'/60'/0'`;\nconst keyringType = 'Ledger Hardware';\n\n// This number causes one of our failing tests to run very slowly, as the for loop needs to iterate 1000 times.\nconst MAX_INDEX = 1000;\n\nenum NetworkApiUrls {\n Ropsten = 'https://api-ropsten.etherscan.io',\n Kovan = 'https://api-kovan.etherscan.io',\n Rinkeby = 'https://api-rinkeby.etherscan.io',\n Mainnet = `https://api.etherscan.io`,\n}\n\ntype SignTransactionPayload = Awaited<\n ReturnType<LedgerBridge<LedgerIframeBridgeOptions>['deviceSignTransaction']>\n>;\n\nexport type AccountDetails = {\n index?: number;\n bip44?: boolean;\n hdPath?: string;\n};\n\nexport type LedgerBridgeKeyringOptions = {\n hdPath: string;\n accounts: readonly string[];\n deviceId: string;\n accountDetails: Readonly<Record<string, AccountDetails>>;\n accountIndexes: Readonly<Record<string, number>>;\n implementFullBIP44: boolean;\n};\n\n/**\n * Check if the given transaction is made with ethereumjs-tx or @ethereumjs/tx\n *\n * Transactions built with older versions of ethereumjs-tx have a\n * getChainId method that newer versions do not.\n * Older versions are mutable\n * while newer versions default to being immutable.\n * Expected shape and type\n * of data for v, r and s differ (Buffer (old) vs BN (new)).\n *\n * @param tx - Transaction to check, instance of either ethereumjs-tx or @ethereumjs/tx.\n * @returns Returns `true` if tx is an old-style ethereumjs-tx transaction.\n */\nfunction isOldStyleEthereumjsTx(\n tx: TypedTransaction | OldEthJsTransaction,\n): tx is OldEthJsTransaction {\n return 'getChainId' in tx && typeof tx.getChainId === 'function';\n}\n\nexport class LedgerKeyring extends EventEmitter {\n static type: string = keyringType;\n\n deviceId = '';\n\n readonly type: string = keyringType;\n\n page = 0;\n\n perPage = 5;\n\n unlockedAccount = 0;\n\n accounts: readonly string[] = [];\n\n accountDetails: Record<string, AccountDetails> = {};\n\n hdk = new HDKey();\n\n hdPath = hdPathString;\n\n paths: Record<string, number> = {};\n\n network: NetworkApiUrls = NetworkApiUrls.Mainnet;\n\n implementFullBIP44 = false;\n\n bridge: LedgerBridge<LedgerBridgeOptions>;\n\n constructor({ bridge }: { bridge: LedgerBridge<LedgerBridgeOptions> }) {\n super();\n\n if (!bridge) {\n throw new Error('Bridge is a required dependency for the keyring');\n }\n\n this.bridge = bridge;\n }\n\n async init() {\n return this.bridge.init();\n }\n\n async destroy() {\n return this.bridge.destroy();\n }\n\n async serialize() {\n return {\n hdPath: this.hdPath,\n accounts: this.accounts,\n deviceId: this.deviceId,\n accountDetails: this.accountDetails,\n implementFullBIP44: false,\n };\n }\n\n async deserialize(opts: Partial<LedgerBridgeKeyringOptions> = {}) {\n this.hdPath = opts.hdPath ?? hdPathString;\n this.accounts = opts.accounts ?? [];\n this.deviceId = opts.deviceId ?? '';\n this.accountDetails = opts.accountDetails ?? {};\n\n if (!opts.accountDetails) {\n this.#migrateAccountDetails(opts);\n }\n\n this.implementFullBIP44 = opts.implementFullBIP44 ?? false;\n\n const keys = new Set<string>(Object.keys(this.accountDetails));\n // Remove accounts that don't have corresponding account details\n this.accounts = this.accounts.filter((account) =>\n keys.has(ethUtil.toChecksumAddress(account)),\n );\n\n return Promise.resolve();\n }\n\n public setDeviceId(deviceId: string) {\n this.deviceId = deviceId;\n }\n\n public getDeviceId() {\n return this.deviceId;\n }\n\n #migrateAccountDetails(opts: Partial<LedgerBridgeKeyringOptions>) {\n if (this.#isLedgerLiveHdPath() && opts.accountIndexes) {\n for (const [account, index] of Object.entries(opts.accountIndexes)) {\n this.accountDetails[account] = {\n bip44: true,\n hdPath: this.#getPathForIndex(index),\n };\n }\n }\n const keys = new Set<string>(Object.keys(this.accountDetails));\n // try to migrate non-LedgerLive accounts too\n if (!this.#isLedgerLiveHdPath()) {\n this.accounts.forEach((account) => {\n const key = ethUtil.toChecksumAddress(account);\n\n if (!keys.has(key)) {\n this.accountDetails[key] = {\n bip44: false,\n hdPath: this.#pathFromAddress(account),\n };\n }\n });\n }\n }\n\n isUnlocked() {\n return Boolean(this.hdk.publicKey);\n }\n\n isConnected() {\n return this.bridge.isDeviceConnected;\n }\n\n setAccountToUnlock(index: number) {\n this.unlockedAccount = index;\n }\n\n setHdPath(hdPath: string) {\n // Reset HDKey if the path changes\n if (this.hdPath !== hdPath) {\n this.hdk = new HDKey();\n }\n this.hdPath = hdPath;\n }\n\n async unlock(hdPath?: string, updateHdk = true): Promise<string> {\n if (this.isUnlocked() && !hdPath) {\n return 'already unlocked';\n }\n const path = hdPath ? this.#toLedgerPath(hdPath) : this.hdPath;\n\n let payload;\n try {\n payload = await this.bridge.getPublicKey({\n hdPath: path,\n });\n } catch (error) {\n throw error instanceof Error\n ? error\n : new Error('Ledger Ethereum app closed. Open it to unlock.');\n }\n\n if (updateHdk && payload.chainCode) {\n this.hdk.publicKey = Buffer.from(payload.publicKey, 'hex');\n this.hdk.chainCode = Buffer.from(payload.chainCode, 'hex');\n }\n\n return payload.address;\n }\n\n async addAccounts(amount = 1): Promise<string[]> {\n return new Promise((resolve, reject) => {\n this.unlock()\n .then(async (_) => {\n const from = this.unlockedAccount;\n const to = from + amount;\n for (let i = from; i < to; i++) {\n const path = this.#getPathForIndex(i);\n let address;\n if (this.#isLedgerLiveHdPath()) {\n address = await this.unlock(path);\n } else {\n address = this.#addressFromIndex(pathBase, i);\n }\n\n this.accountDetails[ethUtil.toChecksumAddress(address)] = {\n // TODO: consider renaming this property, as the current name is misleading\n // It's currently used to represent whether an account uses the Ledger Live path.\n bip44: this.#isLedgerLiveHdPath(),\n hdPath: path,\n };\n\n if (!this.accounts.includes(address)) {\n this.accounts = [...this.accounts, address];\n }\n this.page = 0;\n }\n resolve(this.accounts.slice());\n })\n .catch(reject);\n });\n }\n\n getName() {\n return keyringType;\n }\n\n async getFirstPage() {\n this.page = 0;\n return this.#getPage(1);\n }\n\n async getNextPage() {\n return this.#getPage(1);\n }\n\n async getPreviousPage() {\n return this.#getPage(-1);\n }\n\n async getAccounts() {\n return Promise.resolve(this.accounts.slice());\n }\n\n removeAccount(address: string) {\n const filteredAccounts = this.accounts.filter(\n (a) => a.toLowerCase() !== address.toLowerCase(),\n );\n\n if (filteredAccounts.length === this.accounts.length) {\n throw new Error(`Address ${address} not found in this keyring`);\n }\n\n this.accounts = filteredAccounts;\n delete this.accountDetails[ethUtil.toChecksumAddress(address)];\n }\n\n async attemptMakeApp() {\n return this.bridge.attemptMakeApp();\n }\n\n async updateTransportMethod(transportType: string) {\n return this.bridge.updateTransportMethod(transportType);\n }\n\n // tx is an instance of the ethereumjs-transaction class.\n async signTransaction(\n address: string,\n tx: TypedTransaction | OldEthJsTransaction,\n ): Promise<TypedTransaction | OldEthJsTransaction> {\n let rawTxHex;\n // transactions built with older versions of ethereumjs-tx have a\n // getChainId method that newer versions do not. Older versions are mutable\n // while newer versions default to being immutable. Expected shape and type\n // of data for v, r and s differ (Buffer (old) vs BN (new))\n if (isOldStyleEthereumjsTx(tx)) {\n // In this version of ethereumjs-tx we must add the chainId in hex format\n // to the initial v value. The chainId must be included in the serialized\n // transaction which is only communicated to ethereumjs-tx in this\n // value. In newer versions the chainId is communicated via the 'Common'\n // object.\n // @ts-expect-error tx.v should be a Buffer, but we are assigning a string\n tx.v = ethUtil.bufferToHex(tx.getChainId());\n // @ts-expect-error tx.r should be a Buffer, but we are assigning a string\n tx.r = '0x00';\n // @ts-expect-error tx.s should be a Buffer, but we are assigning a string\n tx.s = '0x00';\n\n rawTxHex = tx.serialize().toString('hex');\n\n return this.#signTransaction(address, rawTxHex, (payload) => {\n tx.v = Buffer.from(payload.v, 'hex');\n tx.r = Buffer.from(payload.r, 'hex');\n tx.s = Buffer.from(payload.s, 'hex');\n return tx;\n });\n }\n\n // The below `encode` call is only necessary for legacy transactions, as `getMessageToSign`\n // calls `rlp.encode` internally for non-legacy transactions. As per the \"Transaction Execution\"\n // section of the ethereum yellow paper, transactions need to be \"well-formed RLP, with no additional\n // trailing bytes\".\n\n // Note also that `getMessageToSign` will return valid RLP for all transaction types, whereas the\n // `serialize` method will not for any transaction type except legacy. This is because `serialize` includes\n // empty r, s and v values in the encoded rlp. This is why we use `getMessageToSign` here instead of `serialize`.\n const messageToSign = tx.getMessageToSign(false);\n\n rawTxHex = Buffer.isBuffer(messageToSign)\n ? messageToSign.toString('hex')\n : Buffer.from(RLP.encode(messageToSign)).toString('hex');\n\n return this.#signTransaction(address, rawTxHex, (payload) => {\n // Because tx will be immutable, first get a plain javascript object that\n // represents the transaction. Using txData here as it aligns with the\n // nomenclature of ethereumjs/tx.\n const txData: TxData = tx.toJSON();\n // The fromTxData utility expects a type to support transactions with a type other than 0\n txData.type = tx.type;\n // The fromTxData utility expects v,r and s to be hex prefixed\n txData.v = ethUtil.addHexPrefix(payload.v);\n txData.r = ethUtil.addHexPrefix(payload.r);\n txData.s = ethUtil.addHexPrefix(payload.s);\n // Adopt the 'common' option from the original transaction and set the\n // returned object to be frozen if the original is frozen.\n return TransactionFactory.fromTxData(txData, {\n common: tx.common,\n freeze: Object.isFrozen(tx),\n });\n });\n }\n\n async #signTransaction(\n address: string,\n rawTxHex: string,\n handleSigning: (\n payload: SignTransactionPayload,\n ) => TypedTransaction | OldEthJsTransaction,\n ): Promise<TypedTransaction | OldEthJsTransaction> {\n const hdPath = await this.unlockAccountByAddress(address);\n\n if (!hdPath) {\n throw new Error('Ledger: Unknown error while signing transaction');\n }\n\n let payload;\n try {\n payload = await this.bridge.deviceSignTransaction({\n tx: rawTxHex,\n hdPath,\n });\n } catch (error) {\n throw error instanceof Error\n ? error\n : new Error('Ledger: Unknown error while signing transaction');\n }\n\n const newOrMutatedTx = handleSigning(payload);\n const valid = newOrMutatedTx.verifySignature();\n if (valid) {\n return newOrMutatedTx;\n }\n throw new Error('Ledger: The transaction signature is not valid');\n }\n\n async signMessage(withAccount: string, data: string) {\n return this.signPersonalMessage(withAccount, data);\n }\n\n // For personal_sign, we need to prefix the message:\n async signPersonalMessage(withAccount: string, message: string) {\n const hdPath = await this.unlockAccountByAddress(withAccount);\n\n if (!hdPath) {\n throw new Error('Ledger: Unknown error while signing message');\n }\n\n let payload;\n try {\n payload = await this.bridge.deviceSignMessage({\n hdPath,\n message: ethUtil.stripHexPrefix(message),\n });\n } catch (error) {\n throw error instanceof Error\n ? error\n : new Error('Ledger: Unknown error while signing message');\n }\n\n let modifiedV = parseInt(String(payload.v), 10).toString(16);\n if (modifiedV.length < 2) {\n modifiedV = `0${modifiedV}`;\n }\n\n const signature = `0x${payload.r}${payload.s}${modifiedV}`;\n const addressSignedWith = recoverPersonalSignature({\n data: message,\n signature,\n });\n if (\n ethUtil.toChecksumAddress(addressSignedWith) !==\n ethUtil.toChecksumAddress(withAccount)\n ) {\n throw new Error('Ledger: The signature doesnt match the right address');\n }\n return signature;\n }\n\n async unlockAccountByAddress(address: string) {\n const checksummedAddress = ethUtil.toChecksumAddress(address);\n const accountDetails = this.accountDetails[checksummedAddress];\n if (!accountDetails) {\n throw new Error(\n `Ledger: Account for address '${checksummedAddress}' not found`,\n );\n }\n const { hdPath } = accountDetails;\n const unlockedAddress = await this.unlock(hdPath, false);\n\n // unlock resolves to the address for the given hdPath as reported by the ledger device\n // if that address is not the requested address, then this account belongs to a different device or seed\n if (unlockedAddress.toLowerCase() !== address.toLowerCase()) {\n throw new Error(\n `Ledger: Account ${address} does not belong to the connected device`,\n );\n }\n return hdPath;\n }\n\n async signTypedData<T extends MessageTypes>(\n withAccount: string,\n data: TypedMessage<T>,\n options: { version?: string } = {},\n ) {\n const isV4 = options.version === 'V4';\n if (!isV4) {\n throw new Error(\n 'Ledger: Only version 4 of typed data signing is supported',\n );\n }\n\n const { domain, types, primaryType, message } =\n TypedDataUtils.sanitizeData(data);\n const domainSeparatorHex = TypedDataUtils.hashStruct(\n 'EIP712Domain',\n domain,\n types,\n SignTypedDataVersion.V4,\n ).toString('hex');\n const hashStructMessageHex = TypedDataUtils.hashStruct(\n primaryType.toString(),\n message,\n types,\n SignTypedDataVersion.V4,\n ).toString('hex');\n\n const hdPath = await this.unlockAccountByAddress(withAccount);\n\n if (!hdPath) {\n throw new Error('Ledger: Unknown error while signing message');\n }\n\n let payload;\n try {\n payload = await this.bridge.deviceSignTypedData({\n hdPath,\n domainSeparatorHex,\n hashStructMessageHex,\n });\n } catch (error) {\n throw error instanceof Error\n ? error\n : new Error('Ledger: Unknown error while signing message');\n }\n\n let recoveryId = parseInt(String(payload.v), 10).toString(16);\n if (recoveryId.length < 2) {\n recoveryId = `0${recoveryId}`;\n }\n const signature = `0x${payload.r}${payload.s}${recoveryId}`;\n const addressSignedWith = recoverTypedSignature({\n data,\n signature,\n version: SignTypedDataVersion.V4,\n });\n\n if (\n ethUtil.toChecksumAddress(addressSignedWith) !==\n ethUtil.toChecksumAddress(withAccount)\n ) {\n throw new Error('Ledger: The signature doesnt match the right address');\n }\n return signature;\n }\n\n exportAccount() {\n throw new Error('Not supported on this device');\n }\n\n forgetDevice() {\n this.accounts = [];\n this.page = 0;\n this.unlockedAccount = 0;\n this.paths = {};\n this.accountDetails = {};\n this.hdk = new HDKey();\n }\n\n /* PRIVATE METHODS */\n async #getPage(increment: number) {\n this.page += increment;\n\n if (this.page <= 0) {\n this.page = 1;\n }\n const from = (this.page - 1) * this.perPage;\n const to = from + this.perPage;\n\n await this.unlock();\n let accounts;\n if (this.#isLedgerLiveHdPath()) {\n accounts = await this.#getAccountsBIP44(from, to);\n } else {\n accounts = this.#getAccountsLegacy(from, to);\n }\n return accounts;\n }\n\n async #getAccountsBIP44(from: number, to: number) {\n const accounts: {\n address: string;\n balance: number | null;\n index: number;\n }[] = [];\n\n for (let i = from; i < to; i++) {\n const path = this.#getPathForIndex(i);\n const address = await this.unlock(path);\n const valid = this.implementFullBIP44\n ? await this.#hasPreviousTransactions(address)\n : true;\n accounts.push({\n address,\n balance: null,\n index: i,\n });\n\n // PER BIP44\n // \"Software should prevent a creation of an account if\n // a previous account does not have a transaction history\n // (meaning none of its addresses have been used before).\"\n if (!valid) {\n break;\n }\n }\n return accounts;\n }\n\n #getAccountsLegacy(from: number, to: number) {\n const accounts: {\n address: string;\n balance: number | null;\n index: number;\n }[] = [];\n\n for (let i = from; i < to; i++) {\n const address = this.#addressFromIndex(pathBase, i);\n accounts.push({\n address,\n balance: null,\n index: i,\n });\n this.paths[ethUtil.toChecksumAddress(address)] = i;\n }\n return accounts;\n }\n\n #addressFromIndex(basePath: string, i: number) {\n const dkey = this.hdk.derive(`${basePath}/${i}`);\n const address = ethUtil\n .publicToAddress(dkey.publicKey, true)\n .toString('hex');\n return ethUtil.toChecksumAddress(`0x${address}`);\n }\n\n #pathFromAddress(address: string) {\n const checksummedAddress = ethUtil.toChecksumAddress(address);\n let index = this.paths[checksummedAddress];\n if (typeof index === 'undefined') {\n for (let i = 0; i < MAX_INDEX; i++) {\n if (checksummedAddress === this.#addressFromIndex(pathBase, i)) {\n index = i;\n break;\n }\n }\n }\n\n if (typeof index === 'undefined') {\n throw new Error('Unknown address');\n }\n return this.#getPathForIndex(index);\n }\n\n #getPathForIndex(index: number) {\n // Check if the path is BIP 44 (Ledger Live)\n return this.#isLedgerLiveHdPath()\n ? `m/44'/60'/${index}'/0/0`\n : `${this.hdPath}/${index}`;\n }\n\n #isLedgerLiveHdPath() {\n return this.hdPath === `m/44'/60'/0'/0/0`;\n }\n\n #toLedgerPath(path: string) {\n return path.toString().replace('m/', '');\n }\n\n async #hasPreviousTransactions(address: string) {\n const apiUrl = this.#getApiUrl();\n const response = await window.fetch(\n `${apiUrl}/api?module=account&action=txlist&address=${address}&tag=latest&page=1&offset=1`,\n );\n const parsedResponse = await response.json();\n return parsedResponse.status !== '0' && parsedResponse.result.length > 0;\n }\n\n #getApiUrl() {\n return this.network;\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask/eth-ledger-bridge-keyring",
|
|
3
|
-
"version": "4.1.
|
|
3
|
+
"version": "4.1.3",
|
|
4
4
|
"description": "A MetaMask compatible keyring, for ledger hardware wallets",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ethereum",
|
|
@@ -24,16 +24,16 @@
|
|
|
24
24
|
"dist/"
|
|
25
25
|
],
|
|
26
26
|
"scripts": {
|
|
27
|
-
"build": "tsc --
|
|
27
|
+
"build": "tsc --build tsconfig.build.json",
|
|
28
28
|
"build:clean": "rimraf dist && yarn build",
|
|
29
29
|
"build:docs": "typedoc",
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' '**/*.yml' '!.yarnrc.yml' --ignore-path .gitignore --no-error-on-unmatched-pattern",
|
|
35
|
-
"prepack": "./scripts/prepack.sh",
|
|
30
|
+
"build:force": "tsc --build tsconfig.build.json --force",
|
|
31
|
+
"changelog:update": "../../scripts/update-changelog.sh @metamask/eth-ledger-bridge-keyring",
|
|
32
|
+
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/eth-ledger-bridge-keyring",
|
|
33
|
+
"publish:preview": "yarn npm publish --tag preview",
|
|
36
34
|
"test": "jest && jest-it-up",
|
|
35
|
+
"test:clean": "jest --clearCache",
|
|
36
|
+
"test:verbose": "jest --verbose",
|
|
37
37
|
"test:watch": "jest --watch"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
@@ -41,50 +41,35 @@
|
|
|
41
41
|
"@ethereumjs/tx": "^4.2.0",
|
|
42
42
|
"@ethereumjs/util": "^8.0.0",
|
|
43
43
|
"@ledgerhq/hw-app-eth": "6.26.1",
|
|
44
|
-
"@metamask/eth-sig-util": "^7.0.
|
|
44
|
+
"@metamask/eth-sig-util": "^7.0.3",
|
|
45
45
|
"hdkey": "^2.1.0"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@ethereumjs/common": "^3.2.0",
|
|
49
|
-
"@lavamoat/allow-scripts": "^2.
|
|
49
|
+
"@lavamoat/allow-scripts": "^3.2.1",
|
|
50
50
|
"@ledgerhq/hw-transport": "^6.24.1",
|
|
51
51
|
"@ledgerhq/types-cryptoassets": "^7.6.0",
|
|
52
52
|
"@ledgerhq/types-devices": "^6.22.4",
|
|
53
|
-
"@metamask/auto-changelog": "^3.
|
|
54
|
-
"@metamask/
|
|
55
|
-
"@metamask/eslint-config-browser": "^11.0.0",
|
|
56
|
-
"@metamask/eslint-config-jest": "^11.0.0",
|
|
57
|
-
"@metamask/eslint-config-nodejs": "^11.0.0",
|
|
58
|
-
"@metamask/eslint-config-typescript": "^11.0.0",
|
|
59
|
-
"@metamask/utils": "^8.2.0",
|
|
53
|
+
"@metamask/auto-changelog": "^3.4.4",
|
|
54
|
+
"@metamask/utils": "^9.2.1",
|
|
60
55
|
"@types/ethereumjs-tx": "^1.0.1",
|
|
61
56
|
"@types/hdkey": "^2.0.1",
|
|
62
57
|
"@types/jest": "^28.1.6",
|
|
63
58
|
"@types/node": "^16.18.59",
|
|
64
|
-
"@
|
|
65
|
-
"
|
|
59
|
+
"@types/web": "^0.0.69",
|
|
60
|
+
"deepmerge": "^4.2.2",
|
|
66
61
|
"depcheck": "^1.4.3",
|
|
67
|
-
"eslint": "^8.27.0",
|
|
68
|
-
"eslint-config-prettier": "^8.5.0",
|
|
69
|
-
"eslint-plugin-import": "^2.26.0",
|
|
70
|
-
"eslint-plugin-jest": "^27.1.5",
|
|
71
|
-
"eslint-plugin-jsdoc": "^39.6.2",
|
|
72
|
-
"eslint-plugin-node": "^11.1.0",
|
|
73
|
-
"eslint-plugin-prettier": "^4.2.1",
|
|
74
62
|
"ethereumjs-tx": "^1.3.4",
|
|
75
63
|
"jest": "^28.1.3",
|
|
76
64
|
"jest-it-up": "^2.2.0",
|
|
77
|
-
"prettier": "^2.7.1",
|
|
78
|
-
"prettier-plugin-packagejson": "^2.2.12",
|
|
79
65
|
"rimraf": "^4.1.2",
|
|
80
66
|
"ts-jest": "^28.0.7",
|
|
81
67
|
"ts-node": "^10.7.0",
|
|
82
68
|
"typedoc": "^0.23.15",
|
|
83
69
|
"typescript": "~4.8.4"
|
|
84
70
|
},
|
|
85
|
-
"packageManager": "yarn@3.4.1",
|
|
86
71
|
"engines": {
|
|
87
|
-
"node": "^
|
|
72
|
+
"node": "^18.18 || >=20"
|
|
88
73
|
},
|
|
89
74
|
"publishConfig": {
|
|
90
75
|
"access": "public",
|