@ledgerhq/hw-app-eth 6.21.2 → 6.22.2

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 (86) hide show
  1. package/README.md +66 -100
  2. package/erc20.js +1 -1
  3. package/lib/Eth.d.ts +14 -33
  4. package/lib/Eth.d.ts.map +1 -1
  5. package/lib/Eth.js +280 -305
  6. package/lib/Eth.js.map +1 -1
  7. package/lib/services/ledger/contracts.d.ts +14 -0
  8. package/lib/services/ledger/contracts.d.ts.map +1 -0
  9. package/lib/{contracts.js → services/ledger/contracts.js} +0 -0
  10. package/lib/services/ledger/contracts.js.map +1 -0
  11. package/lib/services/ledger/erc20.d.ts +22 -0
  12. package/lib/services/ledger/erc20.d.ts.map +1 -0
  13. package/lib/{erc20.js → services/ledger/erc20.js} +0 -0
  14. package/lib/services/ledger/erc20.js.map +1 -0
  15. package/lib/services/ledger/index.d.ts +4 -0
  16. package/lib/services/ledger/index.d.ts.map +1 -0
  17. package/lib/services/ledger/index.js +191 -0
  18. package/lib/services/ledger/index.js.map +1 -0
  19. package/lib/services/ledger/loadConfig.d.ts +3 -0
  20. package/lib/services/ledger/loadConfig.d.ts.map +1 -0
  21. package/lib/{loadConfig.js → services/ledger/loadConfig.js} +0 -0
  22. package/lib/services/ledger/loadConfig.js.map +1 -0
  23. package/lib/services/ledger/nfts.d.ts +10 -0
  24. package/lib/services/ledger/nfts.d.ts.map +1 -0
  25. package/lib/{nfts.js → services/ledger/nfts.js} +10 -10
  26. package/lib/services/ledger/nfts.js.map +1 -0
  27. package/lib/services/types.d.ts +27 -0
  28. package/lib/services/types.d.ts.map +1 -0
  29. package/lib/services/types.js +3 -0
  30. package/lib/services/types.js.map +1 -0
  31. package/lib/utils.d.ts +8 -27
  32. package/lib/utils.d.ts.map +1 -1
  33. package/lib/utils.js +68 -68
  34. package/lib/utils.js.map +1 -1
  35. package/lib-es/Eth.d.ts +14 -33
  36. package/lib-es/Eth.d.ts.map +1 -1
  37. package/lib-es/Eth.js +268 -296
  38. package/lib-es/Eth.js.map +1 -1
  39. package/{lib → lib-es/services/ledger}/contracts.d.ts +1 -1
  40. package/lib-es/services/ledger/contracts.d.ts.map +1 -0
  41. package/lib-es/services/ledger/contracts.js +96 -0
  42. package/lib-es/services/ledger/contracts.js.map +1 -0
  43. package/{lib → lib-es/services/ledger}/erc20.d.ts +0 -0
  44. package/lib-es/services/ledger/erc20.d.ts.map +1 -0
  45. package/lib-es/services/ledger/erc20.js +64 -0
  46. package/lib-es/services/ledger/erc20.js.map +1 -0
  47. package/lib-es/services/ledger/index.d.ts +4 -0
  48. package/lib-es/services/ledger/index.d.ts.map +1 -0
  49. package/lib-es/services/ledger/index.js +189 -0
  50. package/lib-es/services/ledger/index.js.map +1 -0
  51. package/lib-es/services/ledger/loadConfig.d.ts +3 -0
  52. package/lib-es/services/ledger/loadConfig.d.ts.map +1 -0
  53. package/lib-es/services/ledger/loadConfig.js +20 -0
  54. package/lib-es/services/ledger/loadConfig.js.map +1 -0
  55. package/{lib → lib-es/services/ledger}/nfts.d.ts +2 -3
  56. package/lib-es/services/ledger/nfts.d.ts.map +1 -0
  57. package/lib-es/services/ledger/nfts.js +94 -0
  58. package/lib-es/services/ledger/nfts.js.map +1 -0
  59. package/lib-es/services/types.d.ts +27 -0
  60. package/lib-es/services/types.d.ts.map +1 -0
  61. package/lib-es/services/types.js +2 -0
  62. package/lib-es/services/types.js.map +1 -0
  63. package/lib-es/utils.d.ts +8 -27
  64. package/lib-es/utils.d.ts.map +1 -1
  65. package/lib-es/utils.js +66 -61
  66. package/lib-es/utils.js.map +1 -1
  67. package/package.json +3 -3
  68. package/src/Eth.ts +133 -258
  69. package/src/{contracts.ts → services/ledger/contracts.ts} +1 -1
  70. package/src/{erc20.ts → services/ledger/erc20.ts} +0 -0
  71. package/src/services/ledger/index.ts +125 -0
  72. package/src/services/ledger/loadConfig.ts +14 -0
  73. package/src/{nfts.ts → services/ledger/nfts.ts} +11 -12
  74. package/src/services/types.ts +42 -0
  75. package/src/utils.ts +72 -94
  76. package/tests/Eth.test.ts +2 -2
  77. package/lib/contracts.d.ts.map +0 -1
  78. package/lib/contracts.js.map +0 -1
  79. package/lib/erc20.d.ts.map +0 -1
  80. package/lib/erc20.js.map +0 -1
  81. package/lib/loadConfig.d.ts +0 -7
  82. package/lib/loadConfig.d.ts.map +0 -1
  83. package/lib/loadConfig.js.map +0 -1
  84. package/lib/nfts.d.ts.map +0 -1
  85. package/lib/nfts.js.map +0 -1
  86. package/src/loadConfig.ts +0 -23
package/lib-es/Eth.js CHANGED
@@ -62,14 +62,9 @@ var __values = (this && this.__values) || function(o) {
62
62
  * limitations under the License.
63
63
  ********************************************************************************/
64
64
  // FIXME drop:
65
- import { splitPath, foreach } from "./utils";
66
- import { log } from "@ledgerhq/logs";
67
65
  import { EthAppPleaseEnableContractData } from "@ledgerhq/errors";
68
- import { BigNumber } from "bignumber.js";
69
- import { ethers } from "ethers";
70
- import { byContractAddressAndChainId } from "./erc20";
71
- import { loadInfosForContractMethod } from "./contracts";
72
- import { getNFTInfo, loadNftPlugin } from "./nfts";
66
+ import { decodeTxInfo } from "./utils";
67
+ import ledgerService from "./services/ledger";
73
68
  var starkQuantizationTypeMap = {
74
69
  eth: 1,
75
70
  erc20: 2,
@@ -77,6 +72,21 @@ var starkQuantizationTypeMap = {
77
72
  erc20mintable: 4,
78
73
  erc721mintable: 5
79
74
  };
75
+ function splitPath(path) {
76
+ var result = [];
77
+ var components = path.split("/");
78
+ components.forEach(function (element) {
79
+ var number = parseInt(element, 10);
80
+ if (isNaN(number)) {
81
+ return; // FIXME shouldn't it throws instead?
82
+ }
83
+ if (element.length > 1 && element[element.length - 1] === "'") {
84
+ number += 0x80000000;
85
+ }
86
+ result.push(number);
87
+ });
88
+ return result;
89
+ }
80
90
  function hexBuffer(str) {
81
91
  return Buffer.from(str.startsWith("0x") ? str.slice(2) : str, "hex");
82
92
  }
@@ -164,263 +174,222 @@ var Eth = /** @class */ (function () {
164
174
  });
165
175
  };
166
176
  /**
167
- * This commands provides a trusted description of an ERC 20 token
168
- * to associate a contract address with a ticker and number of decimals.
177
+ * You can sign a transaction and retrieve v, r, s given the raw transaction and the BIP 32 path of the account to sign.
169
178
  *
170
- * It shall be run immediately before performing a transaction involving a contract
171
- * calling this contract address to display the proper token information to the user if necessary.
172
- *
173
- * @param {*} info: a blob from "erc20.js" utilities that contains all token information.
174
- *
175
- * @example
176
- * import { byContractAddressAndChainId } from "@ledgerhq/hw-app-eth/erc20"
177
- * const zrxInfo = byContractAddressAndChainId("0xe41d2489571d322189246dafa5ebde1f4699f498", chainId)
178
- * if (zrxInfo) await appEth.provideERC20TokenInformation(zrxInfo)
179
- * const signed = await appEth.signTransaction(path, rawTxHex)
180
- */
181
- Eth.prototype.provideERC20TokenInformation = function (_a) {
182
- var data = _a.data;
183
- return provideERC20TokenInformation(this.transport, data);
184
- };
185
- /**
186
- * You can sign a transaction and retrieve v, r, s given the raw transaction and the BIP 32 path of the account to sign
179
+ * @param path: the BIP32 path to sign the transaction on
180
+ * @param rawTxHex: the raw ethereum transaction in hexadecimal to sign
181
+ * @param resolution: resolution is an object with all "resolved" metadata necessary to allow the device to clear sign information. This includes: ERC20 token information, plugins, contracts, NFT signatures,... You must explicitly provide something to avoid having a warning. By default, you can use Ledger's service or your own resolution service. See services/types.js for the contract. Setting the value to "null" will fallback everything to blind signing but will still allow the device to sign the transaction.
187
182
  * @example
188
- eth.signTransaction("44'/60'/0'/0/0", "e8018504e3b292008252089428ee52a8f3d6e5d15f8b131996950d7f296c7952872bd72a2487400080").then(result => ...)
183
+ import ledgerService from "@ledgerhq/hw-app-eth/lib/services/ledger"
184
+ const tx = "e8018504e3b292008252089428ee52a8f3d6e5d15f8b131996950d7f296c7952872bd72a2487400080"; // raw tx to sign
185
+ const resolution = await ledgerService.resolveTransaction(tx);
186
+ const result = eth.signTransaction("44'/60'/0'/0/0", tx, resolution);
187
+ console.log(result);
189
188
  */
190
- Eth.prototype.signTransaction = function (path, rawTxHex) {
189
+ Eth.prototype.signTransaction = function (path, rawTxHex, resolution) {
191
190
  return __awaiter(this, void 0, void 0, function () {
192
- var paths, offset, rawTx, VALID_TYPES, txType, rlpData, toSend, response, rlpTx, vrsOffset, chainId, chainIdTruncated, rlpDecoded, decodedTx, rlpVrs, sizeOfListLen, chainIdSrc, chainIdTruncatedBuf, _loop_1, provideForContract, selector, nftPluginPayload, infos, plugin, payload, signature, erc20OfInterest, abi, contract, args, erc20OfInterest_1, erc20OfInterest_1_1, address, e_1_1;
193
- var e_1, _a;
194
- var _this = this;
195
- return __generator(this, function (_b) {
196
- switch (_b.label) {
191
+ var _a, _b, plugin, e_1_1, _c, _d, _e, payload, signature, e_2_1, _f, _g, nft, e_3_1, _h, _j, data, e_4_1, rawTx, _k, vrsOffset, txType, chainId, chainIdTruncated, paths, response, offset, _loop_1, this_1, response_byte, v, oneByteChainId, ecc_parity, r, s;
192
+ var e_1, _l, e_2, _m, e_3, _o, e_4, _p;
193
+ return __generator(this, function (_q) {
194
+ switch (_q.label) {
197
195
  case 0:
198
- paths = splitPath(path);
199
- offset = 0;
200
- rawTx = Buffer.from(rawTxHex, "hex");
201
- VALID_TYPES = [1, 2];
202
- txType = VALID_TYPES.includes(rawTx[0]) ? rawTx[0] : null;
203
- rlpData = txType === null ? rawTx : rawTx.slice(1, rawTxHex.length);
204
- toSend = [];
205
- rlpTx = ethers.utils.RLP.decode(rlpData).map(function (hex) {
206
- return Buffer.from(hex.slice(2), "hex");
207
- });
208
- vrsOffset = 0;
209
- chainId = new BigNumber(0);
210
- chainIdTruncated = 0;
211
- rlpDecoded = ethers.utils.RLP.decode(rlpData);
212
- if (txType === 2) {
213
- // EIP1559
214
- decodedTx = {
215
- data: rlpDecoded[7],
216
- to: rlpDecoded[5],
217
- chainId: rlpTx[0]
218
- };
219
- }
220
- else if (txType === 1) {
221
- // EIP2930
222
- decodedTx = {
223
- data: rlpDecoded[6],
224
- to: rlpDecoded[4],
225
- chainId: rlpTx[0]
226
- };
227
- }
228
- else {
229
- // Legacy tx
230
- decodedTx = {
231
- data: rlpDecoded[5],
232
- to: rlpDecoded[3],
233
- // Default to 1 for non EIP 155 txs
234
- chainId: rlpTx.length > 6 ? rlpTx[6] : Buffer.from("0x01", "hex")
235
- };
236
- }
237
- if (txType === null && rlpTx.length > 6) {
238
- rlpVrs = Buffer.from(ethers.utils.RLP.encode(rlpTx.slice(-3)).slice(2), "hex");
239
- vrsOffset = rawTx.length - (rlpVrs.length - 1);
240
- // First byte > 0xf7 means the length of the list length doesn't fit in a single byte.
241
- if (rlpVrs[0] > 0xf7) {
242
- // Increment vrsOffset to account for that extra byte.
243
- vrsOffset++;
244
- sizeOfListLen = rlpVrs[0] - 0xf7;
245
- // Increase rlpOffset by the size of the list length.
246
- vrsOffset += sizeOfListLen - 1;
247
- }
248
- }
249
- chainIdSrc = decodedTx.chainId;
250
- if (chainIdSrc) {
251
- // Using BigNumber because chainID could be any uint256.
252
- chainId = new BigNumber(chainIdSrc.toString("hex"), 16);
253
- chainIdTruncatedBuf = Buffer.alloc(4);
254
- if (chainIdSrc.length > 4) {
255
- chainIdSrc.copy(chainIdTruncatedBuf);
256
- }
257
- else {
258
- chainIdSrc.copy(chainIdTruncatedBuf, 4 - chainIdSrc.length);
259
- }
260
- chainIdTruncated = chainIdTruncatedBuf.readUInt32BE(0);
261
- }
262
- _loop_1 = function () {
263
- var maxChunkSize = offset === 0 ? 150 - 1 - paths.length * 4 : 150;
264
- var chunkSize = offset + maxChunkSize > rawTx.length
265
- ? rawTx.length - offset
266
- : maxChunkSize;
267
- if (vrsOffset != 0 && offset + chunkSize >= vrsOffset) {
268
- // Make sure that the chunk doesn't end right on the EIP 155 marker if set
269
- chunkSize = rawTx.length - offset;
270
- }
271
- var buffer = Buffer.alloc(offset === 0 ? 1 + paths.length * 4 + chunkSize : chunkSize);
272
- if (offset === 0) {
273
- buffer[0] = paths.length;
274
- paths.forEach(function (element, index) {
275
- buffer.writeUInt32BE(element, 1 + 4 * index);
276
- });
277
- rawTx.copy(buffer, 1 + 4 * paths.length, offset, offset + chunkSize);
278
- }
279
- else {
280
- rawTx.copy(buffer, 0, offset, offset + chunkSize);
281
- }
282
- toSend.push(buffer);
283
- offset += chunkSize;
284
- };
285
- while (offset !== rawTx.length) {
286
- _loop_1();
287
- }
288
- provideForContract = function (address) { return __awaiter(_this, void 0, void 0, function () {
289
- var nftInfo, erc20Info;
290
- return __generator(this, function (_a) {
291
- switch (_a.label) {
292
- case 0: return [4 /*yield*/, getNFTInfo(address, chainIdTruncated, this.loadConfig)];
293
- case 1:
294
- nftInfo = _a.sent();
295
- if (!nftInfo) return [3 /*break*/, 3];
296
- log("ethereum", "loading nft info for " +
297
- nftInfo.contractAddress +
298
- " (" +
299
- nftInfo.collectionName +
300
- ")");
301
- return [4 /*yield*/, provideNFTInformation(this.transport, nftInfo.data)];
302
- case 2:
303
- _a.sent();
304
- return [3 /*break*/, 5];
305
- case 3:
306
- erc20Info = byContractAddressAndChainId(address, chainIdTruncated);
307
- if (!erc20Info) return [3 /*break*/, 5];
308
- log("ethereum", "loading erc20token info for " +
309
- erc20Info.contractAddress +
310
- " (" +
311
- erc20Info.ticker +
312
- ")");
313
- return [4 /*yield*/, provideERC20TokenInformation(this.transport, erc20Info.data)];
314
- case 4:
315
- _a.sent();
316
- _a.label = 5;
317
- case 5: return [2 /*return*/];
318
- }
319
- });
320
- }); };
321
- if (!(decodedTx.data.length >= 10)) return [3 /*break*/, 18];
322
- selector = decodedTx.data.substring(0, 10);
323
- return [4 /*yield*/, loadNftPlugin(decodedTx.to, selector, chainIdTruncated, this.loadConfig)];
196
+ if (!(resolution === undefined)) return [3 /*break*/, 2];
197
+ console.warn("hw-app-eth: signTransaction(path, rawTxHex, resolution): " +
198
+ "please provide the 'resolution' parameter. " +
199
+ "See https://github.com/LedgerHQ/ledgerjs/blob/master/packages/hw-app-eth/README.md " +
200
+ "– the previous signature is deprecated and providing the 3rd 'resolution' parameter explicitly will become mandatory so you have the control on the resolution and the fallback mecanism (e.g. fallback to blind signing or not)." +
201
+ "// Possible solution:\n" +
202
+ " + import ledgerService from '@ledgerhq/hw-app-eth/lib/services/ledger';\n" +
203
+ " + const resolution = await ledgerService.resolveTransaction(rawTxHex);");
204
+ return [4 /*yield*/, ledgerService
205
+ .resolveTransaction(rawTxHex, this.loadConfig, {
206
+ externalPlugins: true,
207
+ erc20: true
208
+ })["catch"](function (e) {
209
+ console.warn("an error occurred in resolveTransaction => fallback to blind signing: " +
210
+ String(e));
211
+ return null;
212
+ })];
324
213
  case 1:
325
- nftPluginPayload = _b.sent();
326
- if (!nftPluginPayload) return [3 /*break*/, 3];
327
- return [4 /*yield*/, setPlugin(this.transport, nftPluginPayload)];
214
+ resolution = _q.sent();
215
+ _q.label = 2;
328
216
  case 2:
329
- _b.sent();
330
- return [3 /*break*/, 16];
331
- case 3: return [4 /*yield*/, loadInfosForContractMethod(decodedTx.to, selector, chainIdTruncated, this.loadConfig)];
217
+ if (!resolution) return [3 /*break*/, 31];
218
+ _q.label = 3;
219
+ case 3:
220
+ _q.trys.push([3, 8, 9, 10]);
221
+ _a = __values(resolution.plugin), _b = _a.next();
222
+ _q.label = 4;
332
223
  case 4:
333
- infos = _b.sent();
334
- if (!infos) return [3 /*break*/, 15];
335
- plugin = infos.plugin, payload = infos.payload, signature = infos.signature, erc20OfInterest = infos.erc20OfInterest, abi = infos.abi;
336
- if (!plugin) return [3 /*break*/, 6];
337
- log("ethereum", "loading plugin for " + selector);
338
- return [4 /*yield*/, setExternalPlugin(this.transport, payload, signature)];
224
+ if (!!_b.done) return [3 /*break*/, 7];
225
+ plugin = _b.value;
226
+ return [4 /*yield*/, setPlugin(this.transport, plugin)];
339
227
  case 5:
340
- _b.sent();
341
- _b.label = 6;
228
+ _q.sent();
229
+ _q.label = 6;
342
230
  case 6:
343
- if (!(erc20OfInterest && erc20OfInterest.length && abi)) return [3 /*break*/, 14];
344
- contract = new ethers.utils.Interface(abi);
345
- args = contract.parseTransaction(decodedTx).args;
346
- _b.label = 7;
347
- case 7:
348
- _b.trys.push([7, 12, 13, 14]);
349
- erc20OfInterest_1 = __values(erc20OfInterest), erc20OfInterest_1_1 = erc20OfInterest_1.next();
350
- _b.label = 8;
231
+ _b = _a.next();
232
+ return [3 /*break*/, 4];
233
+ case 7: return [3 /*break*/, 10];
351
234
  case 8:
352
- if (!!erc20OfInterest_1_1.done) return [3 /*break*/, 11];
353
- path = erc20OfInterest_1_1.value;
354
- address = path.split(".").reduce(function (value, seg) {
355
- if (seg === "-1" && Array.isArray(value)) {
356
- return value[value.length - 1];
357
- }
358
- return value[seg];
359
- }, args);
360
- return [4 /*yield*/, provideForContract(address)];
235
+ e_1_1 = _q.sent();
236
+ e_1 = { error: e_1_1 };
237
+ return [3 /*break*/, 10];
361
238
  case 9:
362
- _b.sent();
363
- _b.label = 10;
239
+ try {
240
+ if (_b && !_b.done && (_l = _a["return"])) _l.call(_a);
241
+ }
242
+ finally { if (e_1) throw e_1.error; }
243
+ return [7 /*endfinally*/];
364
244
  case 10:
365
- erc20OfInterest_1_1 = erc20OfInterest_1.next();
366
- return [3 /*break*/, 8];
367
- case 11: return [3 /*break*/, 14];
245
+ _q.trys.push([10, 15, 16, 17]);
246
+ _c = __values(resolution.externalPlugin), _d = _c.next();
247
+ _q.label = 11;
248
+ case 11:
249
+ if (!!_d.done) return [3 /*break*/, 14];
250
+ _e = _d.value, payload = _e.payload, signature = _e.signature;
251
+ return [4 /*yield*/, setExternalPlugin(this.transport, payload, signature)];
368
252
  case 12:
369
- e_1_1 = _b.sent();
370
- e_1 = { error: e_1_1 };
371
- return [3 /*break*/, 14];
253
+ _q.sent();
254
+ _q.label = 13;
372
255
  case 13:
256
+ _d = _c.next();
257
+ return [3 /*break*/, 11];
258
+ case 14: return [3 /*break*/, 17];
259
+ case 15:
260
+ e_2_1 = _q.sent();
261
+ e_2 = { error: e_2_1 };
262
+ return [3 /*break*/, 17];
263
+ case 16:
373
264
  try {
374
- if (erc20OfInterest_1_1 && !erc20OfInterest_1_1.done && (_a = erc20OfInterest_1["return"])) _a.call(erc20OfInterest_1);
265
+ if (_d && !_d.done && (_m = _c["return"])) _m.call(_c);
375
266
  }
376
- finally { if (e_1) throw e_1.error; }
267
+ finally { if (e_2) throw e_2.error; }
377
268
  return [7 /*endfinally*/];
378
- case 14: return [3 /*break*/, 16];
379
- case 15:
380
- log("ethereum", "no infos for selector " + selector);
381
- _b.label = 16;
382
- case 16: return [4 /*yield*/, provideForContract(decodedTx.to)];
383
269
  case 17:
384
- _b.sent();
385
- _b.label = 18;
386
- case 18: return [2 /*return*/, foreach(toSend, function (data, i) {
387
- return _this.transport
388
- .send(0xe0, 0x04, i === 0 ? 0x00 : 0x80, 0x00, data)
389
- .then(function (apduResponse) {
390
- response = apduResponse;
391
- });
392
- }).then(function () {
393
- var response_byte = response.slice(0, 1)[0];
394
- var v = "";
395
- if (chainId.times(2).plus(35).plus(1).isGreaterThan(255)) {
396
- var oneByteChainId = (chainIdTruncated * 2 + 35) % 256;
397
- var ecc_parity = Math.abs(response_byte - oneByteChainId);
398
- if (txType != null) {
399
- // For EIP2930 and EIP1559 tx, v is simply the parity.
400
- v = ecc_parity % 2 == 1 ? "00" : "01";
401
- }
402
- else {
403
- // Legacy type transaction with a big chain ID
404
- v = chainId.times(2).plus(35).plus(ecc_parity).toString(16);
270
+ _q.trys.push([17, 22, 23, 24]);
271
+ _f = __values(resolution.nfts), _g = _f.next();
272
+ _q.label = 18;
273
+ case 18:
274
+ if (!!_g.done) return [3 /*break*/, 21];
275
+ nft = _g.value;
276
+ return [4 /*yield*/, provideNFTInformation(this.transport, Buffer.from(nft, "hex"))];
277
+ case 19:
278
+ _q.sent();
279
+ _q.label = 20;
280
+ case 20:
281
+ _g = _f.next();
282
+ return [3 /*break*/, 18];
283
+ case 21: return [3 /*break*/, 24];
284
+ case 22:
285
+ e_3_1 = _q.sent();
286
+ e_3 = { error: e_3_1 };
287
+ return [3 /*break*/, 24];
288
+ case 23:
289
+ try {
290
+ if (_g && !_g.done && (_o = _f["return"])) _o.call(_f);
291
+ }
292
+ finally { if (e_3) throw e_3.error; }
293
+ return [7 /*endfinally*/];
294
+ case 24:
295
+ _q.trys.push([24, 29, 30, 31]);
296
+ _h = __values(resolution.erc20Tokens), _j = _h.next();
297
+ _q.label = 25;
298
+ case 25:
299
+ if (!!_j.done) return [3 /*break*/, 28];
300
+ data = _j.value;
301
+ return [4 /*yield*/, provideERC20TokenInformation(this.transport, Buffer.from(data, "hex"))];
302
+ case 26:
303
+ _q.sent();
304
+ _q.label = 27;
305
+ case 27:
306
+ _j = _h.next();
307
+ return [3 /*break*/, 25];
308
+ case 28: return [3 /*break*/, 31];
309
+ case 29:
310
+ e_4_1 = _q.sent();
311
+ e_4 = { error: e_4_1 };
312
+ return [3 /*break*/, 31];
313
+ case 30:
314
+ try {
315
+ if (_j && !_j.done && (_p = _h["return"])) _p.call(_h);
316
+ }
317
+ finally { if (e_4) throw e_4.error; }
318
+ return [7 /*endfinally*/];
319
+ case 31:
320
+ rawTx = Buffer.from(rawTxHex, "hex");
321
+ _k = decodeTxInfo(rawTx), vrsOffset = _k.vrsOffset, txType = _k.txType, chainId = _k.chainId, chainIdTruncated = _k.chainIdTruncated;
322
+ paths = splitPath(path);
323
+ offset = 0;
324
+ _loop_1 = function () {
325
+ var first, maxChunkSize, chunkSize, buffer;
326
+ return __generator(this, function (_r) {
327
+ switch (_r.label) {
328
+ case 0:
329
+ first = offset === 0;
330
+ maxChunkSize = first ? 150 - 1 - paths.length * 4 : 150;
331
+ chunkSize = offset + maxChunkSize > rawTx.length
332
+ ? rawTx.length - offset
333
+ : maxChunkSize;
334
+ if (vrsOffset != 0 && offset + chunkSize >= vrsOffset) {
335
+ // Make sure that the chunk doesn't end right on the EIP 155 marker if set
336
+ chunkSize = rawTx.length - offset;
337
+ }
338
+ buffer = Buffer.alloc(first ? 1 + paths.length * 4 + chunkSize : chunkSize);
339
+ if (first) {
340
+ buffer[0] = paths.length;
341
+ paths.forEach(function (element, index) {
342
+ buffer.writeUInt32BE(element, 1 + 4 * index);
343
+ });
344
+ rawTx.copy(buffer, 1 + 4 * paths.length, offset, offset + chunkSize);
345
+ }
346
+ else {
347
+ rawTx.copy(buffer, 0, offset, offset + chunkSize);
348
+ }
349
+ return [4 /*yield*/, this_1.transport
350
+ .send(0xe0, 0x04, first ? 0x00 : 0x80, 0x00, buffer)["catch"](function (e) {
351
+ throw remapTransactionRelatedErrors(e);
352
+ })];
353
+ case 1:
354
+ response = _r.sent();
355
+ offset += chunkSize;
356
+ return [2 /*return*/];
405
357
  }
358
+ });
359
+ };
360
+ this_1 = this;
361
+ _q.label = 32;
362
+ case 32:
363
+ if (!(offset !== rawTx.length)) return [3 /*break*/, 34];
364
+ return [5 /*yield**/, _loop_1()];
365
+ case 33:
366
+ _q.sent();
367
+ return [3 /*break*/, 32];
368
+ case 34:
369
+ response_byte = response[0];
370
+ v = "";
371
+ if (chainId.times(2).plus(35).plus(1).isGreaterThan(255)) {
372
+ oneByteChainId = (chainIdTruncated * 2 + 35) % 256;
373
+ ecc_parity = Math.abs(response_byte - oneByteChainId);
374
+ if (txType != null) {
375
+ // For EIP2930 and EIP1559 tx, v is simply the parity.
376
+ v = ecc_parity % 2 == 1 ? "00" : "01";
406
377
  }
407
378
  else {
408
- v = response_byte.toString(16);
409
- }
410
- // Make sure v has is prefixed with a 0 if its length is odd ("1" -> "01").
411
- if (v.length % 2 == 1) {
412
- v = "0" + v;
379
+ // Legacy type transaction with a big chain ID
380
+ v = chainId.times(2).plus(35).plus(ecc_parity).toString(16);
413
381
  }
414
- var r = response.slice(1, 1 + 32).toString("hex");
415
- var s = response.slice(1 + 32, 1 + 32 + 32).toString("hex");
416
- return {
417
- v: v,
418
- r: r,
419
- s: s
420
- };
421
- }, function (e) {
422
- throw remapTransactionRelatedErrors(e);
423
- })];
382
+ }
383
+ else {
384
+ v = response_byte.toString(16);
385
+ }
386
+ // Make sure v has is prefixed with a 0 if its length is odd ("1" -> "01").
387
+ if (v.length % 2 == 1) {
388
+ v = "0" + v;
389
+ }
390
+ r = response.slice(1, 1 + 32).toString("hex");
391
+ s = response.slice(1 + 32, 1 + 32 + 32).toString("hex");
392
+ return [2 /*return*/, { v: v, r: r, s: s }];
424
393
  }
425
394
  });
426
395
  });
@@ -451,50 +420,58 @@ var Eth = /** @class */ (function () {
451
420
  })
452
421
  */
453
422
  Eth.prototype.signPersonalMessage = function (path, messageHex) {
454
- var _this = this;
455
- var paths = splitPath(path);
456
- var offset = 0;
457
- var message = Buffer.from(messageHex, "hex");
458
- var toSend = [];
459
- var response;
460
- var _loop_2 = function () {
461
- var maxChunkSize = offset === 0 ? 150 - 1 - paths.length * 4 - 4 : 150;
462
- var chunkSize = offset + maxChunkSize > message.length
463
- ? message.length - offset
464
- : maxChunkSize;
465
- var buffer = Buffer.alloc(offset === 0 ? 1 + paths.length * 4 + 4 + chunkSize : chunkSize);
466
- if (offset === 0) {
467
- buffer[0] = paths.length;
468
- paths.forEach(function (element, index) {
469
- buffer.writeUInt32BE(element, 1 + 4 * index);
470
- });
471
- buffer.writeUInt32BE(message.length, 1 + 4 * paths.length);
472
- message.copy(buffer, 1 + 4 * paths.length + 4, offset, offset + chunkSize);
473
- }
474
- else {
475
- message.copy(buffer, 0, offset, offset + chunkSize);
476
- }
477
- toSend.push(buffer);
478
- offset += chunkSize;
479
- };
480
- while (offset !== message.length) {
481
- _loop_2();
482
- }
483
- return foreach(toSend, function (data, i) {
484
- return _this.transport
485
- .send(0xe0, 0x08, i === 0 ? 0x00 : 0x80, 0x00, data)
486
- .then(function (apduResponse) {
487
- response = apduResponse;
423
+ return __awaiter(this, void 0, void 0, function () {
424
+ var paths, offset, message, response, _loop_2, this_2, v, r, s;
425
+ return __generator(this, function (_a) {
426
+ switch (_a.label) {
427
+ case 0:
428
+ paths = splitPath(path);
429
+ offset = 0;
430
+ message = Buffer.from(messageHex, "hex");
431
+ _loop_2 = function () {
432
+ var maxChunkSize, chunkSize, buffer;
433
+ return __generator(this, function (_b) {
434
+ switch (_b.label) {
435
+ case 0:
436
+ maxChunkSize = offset === 0 ? 150 - 1 - paths.length * 4 - 4 : 150;
437
+ chunkSize = offset + maxChunkSize > message.length
438
+ ? message.length - offset
439
+ : maxChunkSize;
440
+ buffer = Buffer.alloc(offset === 0 ? 1 + paths.length * 4 + 4 + chunkSize : chunkSize);
441
+ if (offset === 0) {
442
+ buffer[0] = paths.length;
443
+ paths.forEach(function (element, index) {
444
+ buffer.writeUInt32BE(element, 1 + 4 * index);
445
+ });
446
+ buffer.writeUInt32BE(message.length, 1 + 4 * paths.length);
447
+ message.copy(buffer, 1 + 4 * paths.length + 4, offset, offset + chunkSize);
448
+ }
449
+ else {
450
+ message.copy(buffer, 0, offset, offset + chunkSize);
451
+ }
452
+ return [4 /*yield*/, this_2.transport.send(0xe0, 0x08, offset === 0 ? 0x00 : 0x80, 0x00, buffer)];
453
+ case 1:
454
+ response = _b.sent();
455
+ offset += chunkSize;
456
+ return [2 /*return*/];
457
+ }
458
+ });
459
+ };
460
+ this_2 = this;
461
+ _a.label = 1;
462
+ case 1:
463
+ if (!(offset !== message.length)) return [3 /*break*/, 3];
464
+ return [5 /*yield**/, _loop_2()];
465
+ case 2:
466
+ _a.sent();
467
+ return [3 /*break*/, 1];
468
+ case 3:
469
+ v = response[0];
470
+ r = response.slice(1, 1 + 32).toString("hex");
471
+ s = response.slice(1 + 32, 1 + 32 + 32).toString("hex");
472
+ return [2 /*return*/, { v: v, r: r, s: s }];
473
+ }
488
474
  });
489
- }).then(function () {
490
- var v = response[0];
491
- var r = response.slice(1, 1 + 32).toString("hex");
492
- var s = response.slice(1 + 32, 1 + 32 + 32).toString("hex");
493
- return {
494
- v: v,
495
- r: r,
496
- s: s
497
- };
498
475
  });
499
476
  };
500
477
  /**
@@ -990,22 +967,17 @@ var Eth = /** @class */ (function () {
990
967
  throw e;
991
968
  });
992
969
  };
993
- /**
994
- * Set the name of the external plugin that should be used to parse the next transaction
995
- *
996
- * @param pluginName string containing the name of the plugin, must have length between 1 and 30 bytes
997
- * @return True if the method was executed successfully
998
- */
970
+ Eth.prototype.provideERC20TokenInformation = function (_a) {
971
+ var data = _a.data;
972
+ console.warn("hw-app-eth: eth.provideERC20TokenInformation is deprecated. signTransaction solves this for you when providing it in `resolution`.");
973
+ return provideERC20TokenInformation(this.transport, data);
974
+ };
999
975
  Eth.prototype.setExternalPlugin = function (pluginName, contractAddress, selector) {
976
+ console.warn("hw-app-eth: eth.setExternalPlugin is deprecated. signTransaction solves this for you when providing it in `resolution`.");
1000
977
  return setExternalPlugin(this.transport, pluginName, selector);
1001
978
  };
1002
- /**
1003
- * Set the plugin (internal or external) that should be used to parse the next transaction
1004
- *
1005
- * @param data string containing the payload and signature that will be parsed and verified by the device.
1006
- * @return True if the method was executed successfully
1007
- */
1008
979
  Eth.prototype.setPlugin = function (data) {
980
+ console.warn("hw-app-eth: eth.setPlugin is deprecated. signTransaction solves this for you when providing it in `resolution`.");
1009
981
  return setPlugin(this.transport, data);
1010
982
  };
1011
983
  return Eth;