@airgap/ethereum 0.13.45-beta.3 → 0.13.45-beta.5

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 (82) hide show
  1. package/package.json +5 -5
  2. package/v0/index.js +10 -10
  3. package/v0/index.js.map +1 -1
  4. package/v0/protocol/BaseEthereumProtocol.d.ts +1 -0
  5. package/v0/protocol/BaseEthereumProtocol.js +872 -460
  6. package/v0/protocol/BaseEthereumProtocol.js.map +1 -1
  7. package/v0/protocol/EthereumAddress.d.ts +1 -0
  8. package/v0/protocol/EthereumAddress.js +17 -26
  9. package/v0/protocol/EthereumAddress.js.map +1 -1
  10. package/v0/protocol/EthereumClassicProtocol.js +23 -6
  11. package/v0/protocol/EthereumClassicProtocol.js.map +1 -1
  12. package/v0/protocol/EthereumCryptoClient.js +125 -66
  13. package/v0/protocol/EthereumCryptoClient.js.map +1 -1
  14. package/v0/protocol/EthereumProtocol.js +28 -9
  15. package/v0/protocol/EthereumProtocol.js.map +1 -1
  16. package/v0/protocol/EthereumProtocolOptions.d.ts +1 -1
  17. package/v0/protocol/EthereumProtocolOptions.js +117 -36
  18. package/v0/protocol/EthereumProtocolOptions.js.map +1 -1
  19. package/v0/protocol/EthereumRopstenProtocol.js +23 -6
  20. package/v0/protocol/EthereumRopstenProtocol.js.map +1 -1
  21. package/v0/protocol/clients/info-clients/EtherscanInfoClient.js +186 -86
  22. package/v0/protocol/clients/info-clients/EtherscanInfoClient.js.map +1 -1
  23. package/v0/protocol/clients/info-clients/InfoClient.js +4 -3
  24. package/v0/protocol/clients/info-clients/InfoClient.js.map +1 -1
  25. package/v0/protocol/clients/node-clients/AirGapNodeClient.js +298 -126
  26. package/v0/protocol/clients/node-clients/AirGapNodeClient.js.map +1 -1
  27. package/v0/protocol/clients/node-clients/NodeClient.js +4 -3
  28. package/v0/protocol/clients/node-clients/NodeClient.js.map +1 -1
  29. package/v0/protocol/erc20/ERC20.js +4 -4
  30. package/v0/protocol/erc20/ERC20.js.map +1 -1
  31. package/v0/protocol/erc20/GenericERC20.js +300 -161
  32. package/v0/protocol/erc20/GenericERC20.js.map +1 -1
  33. package/v0/protocol/utils/utils.js +55 -52
  34. package/v0/protocol/utils/utils.js.map +1 -1
  35. package/v0/serializer/validators/transaction-validator.js +31 -23
  36. package/v0/serializer/validators/transaction-validator.js.map +1 -1
  37. package/v0/serializer/validators/validators.js +73 -25
  38. package/v0/serializer/validators/validators.js.map +1 -1
  39. package/v1/block-explorer/EtherscanBlockExplorer.js +61 -12
  40. package/v1/block-explorer/EtherscanBlockExplorer.js.map +1 -1
  41. package/v1/clients/crypto/EthereumCryptoClient.js +124 -69
  42. package/v1/clients/crypto/EthereumCryptoClient.js.map +1 -1
  43. package/v1/clients/info/EthereumInfoClient.d.ts +1 -1
  44. package/v1/clients/info/EthereumInfoClient.js +4 -3
  45. package/v1/clients/info/EthereumInfoClient.js.map +1 -1
  46. package/v1/clients/info/EtherscanInfoClient.js +162 -84
  47. package/v1/clients/info/EtherscanInfoClient.js.map +1 -1
  48. package/v1/clients/node/HttpEthereumNodeClient.d.ts +2 -2
  49. package/v1/clients/node/HttpEthereumNodeClient.js +375 -164
  50. package/v1/clients/node/HttpEthereumNodeClient.js.map +1 -1
  51. package/v1/data/EthereumAddress.js +20 -29
  52. package/v1/data/EthereumAddress.js.map +1 -1
  53. package/v1/index.js +14 -14
  54. package/v1/index.js.map +1 -1
  55. package/v1/module/EthereumModule.js +112 -43
  56. package/v1/module/EthereumModule.js.map +1 -1
  57. package/v1/module.js +3 -2
  58. package/v1/module.js.map +1 -1
  59. package/v1/protocol/EthereumBaseProtocol.js +788 -467
  60. package/v1/protocol/EthereumBaseProtocol.js.map +1 -1
  61. package/v1/protocol/EthereumProtocol.js +46 -15
  62. package/v1/protocol/EthereumProtocol.js.map +1 -1
  63. package/v1/protocol/erc20/ERC20Protocol.js +360 -213
  64. package/v1/protocol/erc20/ERC20Protocol.js.map +1 -1
  65. package/v1/protocol/erc20/ERC20Token.d.ts +1 -1
  66. package/v1/protocol/erc20/ERC20Token.js +113 -35
  67. package/v1/protocol/erc20/ERC20Token.js.map +1 -1
  68. package/v1/serializer/v3/schemas/converter/transaction-converter.js +42 -25
  69. package/v1/serializer/v3/schemas/converter/transaction-converter.js.map +1 -1
  70. package/v1/serializer/v3/serializer-companion.js +148 -71
  71. package/v1/serializer/v3/serializer-companion.js.map +1 -1
  72. package/v1/serializer/v3/validators/transaction-validator.js +17 -14
  73. package/v1/serializer/v3/validators/transaction-validator.js.map +1 -1
  74. package/v1/types/crypto.d.ts +1 -1
  75. package/v1/types/protocol.d.ts +1 -1
  76. package/v1/types/transaction.d.ts +1 -1
  77. package/v1/utils/EthereumUtils.js +60 -57
  78. package/v1/utils/EthereumUtils.js.map +1 -1
  79. package/v1/utils/key.js +19 -28
  80. package/v1/utils/key.js.map +1 -1
  81. package/v1/utils/protocol.js +21 -33
  82. package/v1/utils/protocol.js.map +1 -1
@@ -1,4 +1,30 @@
1
1
  "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
2
28
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
29
  if (k2 === undefined) k2 = k;
4
30
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -15,45 +41,71 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
15
41
  }) : function(o, v) {
16
42
  o["default"] = v;
17
43
  });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
44
+ var __importStar = (this && this.__importStar) || function (mod) {
45
+ if (mod && mod.__esModule) return mod;
46
+ var result = {};
47
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
48
+ __setModuleDefault(result, mod);
49
+ return result;
50
+ };
51
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
52
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
53
+ return new (P || (P = Promise))(function (resolve, reject) {
54
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
55
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
56
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
57
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
58
+ });
59
+ };
60
+ var __generator = (this && this.__generator) || function (thisArg, body) {
61
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
62
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
63
+ function verb(n) { return function (v) { return step([n, v]); }; }
64
+ function step(op) {
65
+ if (f) throw new TypeError("Generator is already executing.");
66
+ while (_) try {
67
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
68
+ if (y = 0, t) op = [op[0] & 2, t.value];
69
+ switch (op[0]) {
70
+ case 0: case 1: t = op; break;
71
+ case 4: _.label++; return { value: op[1], done: false };
72
+ case 5: _.label++; y = op[1]; op = [0]; continue;
73
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
74
+ default:
75
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
76
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
77
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
78
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
79
+ if (t[2]) _.ops.pop();
80
+ _.trys.pop(); continue;
81
+ }
82
+ op = body.call(thisArg, _);
83
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
84
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
85
+ }
86
+ };
35
87
  var __importDefault = (this && this.__importDefault) || function (mod) {
36
88
  return (mod && mod.__esModule) ? mod : { "default": mod };
37
89
  };
38
90
  Object.defineProperty(exports, "__esModule", { value: true });
39
91
  exports.DefaultEthereumBaseProtocolImpl = exports.EthereumBaseProtocolImpl = exports.DEFAULT_ETHEREUM_UNITS_METADATA = void 0;
40
92
  // tslint:disable: max-classes-per-file
41
- const coinlib_core_1 = require("@airgap/coinlib-core");
42
- const bignumber_1 = require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber");
93
+ var coinlib_core_1 = require("@airgap/coinlib-core");
94
+ var bignumber_1 = require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber");
43
95
  // @ts-ignore
44
- const BitGo = __importStar(require("@airgap/coinlib-core/dependencies/src/bitgo-utxo-lib-5d91049fd7a988382df81c8260e244ee56d57aac/src"));
45
- const errors_1 = require("@airgap/coinlib-core/errors");
46
- const hex_1 = require("@airgap/coinlib-core/utils/hex");
47
- const crypto_1 = require("@airgap/crypto");
48
- const module_kit_1 = require("@airgap/module-kit");
49
- const common_1 = __importDefault(require("@ethereumjs/common"));
96
+ var BitGo = __importStar(require("@airgap/coinlib-core/dependencies/src/bitgo-utxo-lib-5d91049fd7a988382df81c8260e244ee56d57aac/src"));
97
+ var errors_1 = require("@airgap/coinlib-core/errors");
98
+ var hex_1 = require("@airgap/coinlib-core/utils/hex");
99
+ var crypto_1 = require("@airgap/crypto");
100
+ var module_kit_1 = require("@airgap/module-kit");
101
+ var common_1 = __importDefault(require("@ethereumjs/common"));
50
102
  // TODO: ETH TX and ethereumjs-util-5.2.0 removed
51
- const tx_1 = require("@ethereumjs/tx");
52
- const EthereumCryptoClient_1 = require("../clients/crypto/EthereumCryptoClient");
53
- const EthereumAddress_1 = require("../data/EthereumAddress");
54
- const EthereumUtils_1 = require("../utils/EthereumUtils");
55
- const key_1 = require("../utils/key");
56
- const EthereumChainIds_1 = require("./EthereumChainIds");
103
+ var tx_1 = require("@ethereumjs/tx");
104
+ var EthereumCryptoClient_1 = require("../clients/crypto/EthereumCryptoClient");
105
+ var EthereumAddress_1 = require("../data/EthereumAddress");
106
+ var EthereumUtils_1 = require("../utils/EthereumUtils");
107
+ var key_1 = require("../utils/key");
108
+ var EthereumChainIds_1 = require("./EthereumChainIds");
57
109
  // Implementation
58
110
  exports.DEFAULT_ETHEREUM_UNITS_METADATA = {
59
111
  ETH: {
@@ -69,10 +121,11 @@ exports.DEFAULT_ETHEREUM_UNITS_METADATA = {
69
121
  decimals: 0
70
122
  }
71
123
  };
72
- const MAX_GAS_ESTIMATE = 300000;
73
- const WALLET_CONNECT_NAMESPACE = 'eip155';
74
- class EthereumBaseProtocolImpl {
75
- constructor(nodeClient, infoClient, options) {
124
+ var MAX_GAS_ESTIMATE = 300000;
125
+ var WALLET_CONNECT_NAMESPACE = 'eip155';
126
+ var EthereumBaseProtocolImpl = /** @class */ (function () {
127
+ function EthereumBaseProtocolImpl(nodeClient, infoClient, options) {
128
+ var _a, _b;
76
129
  this.bitcoinJS = {
77
130
  lib: BitGo,
78
131
  config: { network: BitGo.networks.bitcoin }
@@ -87,7 +140,7 @@ class EthereumBaseProtocolImpl {
87
140
  this.infoClient = infoClient;
88
141
  this.cryptoClient = new EthereumCryptoClient_1.EthereumCryptoClient();
89
142
  this.units = options.units;
90
- this.feeDefaults = options.feeDefaults ?? {
143
+ this.feeDefaults = (_a = options.feeDefaults) !== null && _a !== void 0 ? _a : {
91
144
  low: (0, module_kit_1.newAmount)(0.00021 /* 21000 GAS * 10 GWEI */, 'ETH').blockchain(this.feeUnits),
92
145
  medium: (0, module_kit_1.newAmount)(0.000315 /* 21000 GAS * 15 GWEI */, 'ETH').blockchain(this.feeUnits),
93
146
  high: (0, module_kit_1.newAmount)(0.00084 /* 21000 GAS * 40 GWEI */, 'ETH').blockchain(this.feeUnits)
@@ -103,7 +156,7 @@ class EthereumBaseProtocolImpl {
103
156
  mainUnit: 'ETH'
104
157
  },
105
158
  account: {
106
- standardDerivationPath: options.standardDerivationPath ?? `m/44'/60'/0'`,
159
+ standardDerivationPath: (_b = options.standardDerivationPath) !== null && _b !== void 0 ? _b : "m/44'/60'/0'",
107
160
  address: {
108
161
  isCaseSensitive: false,
109
162
  placeholder: '0xabc...',
@@ -117,455 +170,723 @@ class EthereumBaseProtocolImpl {
117
170
  }
118
171
  };
119
172
  }
120
- async getMetadata() {
121
- return this.metadata;
122
- }
123
- async getAddressFromPublicKey(publicKey) {
124
- return EthereumAddress_1.EthereumAddress.from(this.nonExtendedPublicKey(publicKey)).asString();
125
- }
126
- async deriveFromExtendedPublicKey(extendedPublicKey, visibilityIndex, addressIndex) {
127
- return this.getPublicKeyFromExtendedPublicKey(extendedPublicKey, visibilityIndex, addressIndex);
128
- }
129
- async getDetailsFromTransaction(transaction, publicKey) {
130
- return publicKey.type === 'pub'
131
- ? this.getDetailsFromTransactionWithPublicKey(transaction, publicKey)
132
- : this.getDetailsFromTransactionWithExtendedPublicKey(transaction, publicKey);
133
- }
134
- async getDetailsFromTransactionWithPublicKey(transaction, publicKey) {
135
- switch (transaction.type) {
136
- case 'signed':
137
- return this.getDetailsFromSignedTransaction(transaction);
138
- case 'unsigned':
139
- const ownAddress = await this.getAddressFromPublicKey(publicKey);
140
- if (transaction.ethereumType === 'typed') {
141
- return this.getDetailsFromTypedUnsignedTransaction(transaction, ownAddress);
173
+ EthereumBaseProtocolImpl.prototype.getMetadata = function () {
174
+ return __awaiter(this, void 0, void 0, function () {
175
+ return __generator(this, function (_a) {
176
+ return [2 /*return*/, this.metadata];
177
+ });
178
+ });
179
+ };
180
+ EthereumBaseProtocolImpl.prototype.getAddressFromPublicKey = function (publicKey) {
181
+ return __awaiter(this, void 0, void 0, function () {
182
+ return __generator(this, function (_a) {
183
+ return [2 /*return*/, EthereumAddress_1.EthereumAddress.from(this.nonExtendedPublicKey(publicKey)).asString()];
184
+ });
185
+ });
186
+ };
187
+ EthereumBaseProtocolImpl.prototype.deriveFromExtendedPublicKey = function (extendedPublicKey, visibilityIndex, addressIndex) {
188
+ return __awaiter(this, void 0, void 0, function () {
189
+ return __generator(this, function (_a) {
190
+ return [2 /*return*/, this.getPublicKeyFromExtendedPublicKey(extendedPublicKey, visibilityIndex, addressIndex)];
191
+ });
192
+ });
193
+ };
194
+ EthereumBaseProtocolImpl.prototype.getDetailsFromTransaction = function (transaction, publicKey) {
195
+ return __awaiter(this, void 0, void 0, function () {
196
+ return __generator(this, function (_a) {
197
+ return [2 /*return*/, publicKey.type === 'pub'
198
+ ? this.getDetailsFromTransactionWithPublicKey(transaction, publicKey)
199
+ : this.getDetailsFromTransactionWithExtendedPublicKey(transaction, publicKey)];
200
+ });
201
+ });
202
+ };
203
+ EthereumBaseProtocolImpl.prototype.getDetailsFromTransactionWithPublicKey = function (transaction, publicKey) {
204
+ return __awaiter(this, void 0, void 0, function () {
205
+ var _a, ownAddress;
206
+ return __generator(this, function (_b) {
207
+ switch (_b.label) {
208
+ case 0:
209
+ _a = transaction.type;
210
+ switch (_a) {
211
+ case 'signed': return [3 /*break*/, 1];
212
+ case 'unsigned': return [3 /*break*/, 2];
213
+ }
214
+ return [3 /*break*/, 4];
215
+ case 1: return [2 /*return*/, this.getDetailsFromSignedTransaction(transaction)];
216
+ case 2: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
217
+ case 3:
218
+ ownAddress = _b.sent();
219
+ if (transaction.ethereumType === 'typed') {
220
+ return [2 /*return*/, this.getDetailsFromTypedUnsignedTransaction(transaction, ownAddress)];
221
+ }
222
+ else {
223
+ return [2 /*return*/, this.getDetailsFromRawUnsignedTransaction(transaction, ownAddress)];
224
+ }
225
+ _b.label = 4;
226
+ case 4:
227
+ (0, coinlib_core_1.assertNever)(transaction);
228
+ throw new errors_1.UnsupportedError(coinlib_core_1.Domain.ETHEREUM, 'Unsupported transaction type.');
142
229
  }
143
- else {
144
- return this.getDetailsFromRawUnsignedTransaction(transaction, ownAddress);
230
+ });
231
+ });
232
+ };
233
+ EthereumBaseProtocolImpl.prototype.getDetailsFromTransactionWithExtendedPublicKey = function (transaction, extendedPublicKey) {
234
+ return __awaiter(this, void 0, void 0, function () {
235
+ var _a, dps, derivedPublicKey, ownAddress, derivedPublicKey, ownAddress;
236
+ return __generator(this, function (_b) {
237
+ switch (_b.label) {
238
+ case 0:
239
+ _a = transaction.type;
240
+ switch (_a) {
241
+ case 'signed': return [3 /*break*/, 1];
242
+ case 'unsigned': return [3 /*break*/, 2];
243
+ }
244
+ return [3 /*break*/, 6];
245
+ case 1: return [2 /*return*/, this.getDetailsFromSignedTransaction(transaction)];
246
+ case 2:
247
+ if (!(transaction.ethereumType === 'typed')) return [3 /*break*/, 4];
248
+ dps = transaction.derivationPath.split('/');
249
+ derivedPublicKey = this.getPublicKeyFromExtendedPublicKey(extendedPublicKey, Number(dps[dps.length - 2]), Number(dps[dps.length - 1]));
250
+ return [4 /*yield*/, this.getAddressFromPublicKey(derivedPublicKey)];
251
+ case 3:
252
+ ownAddress = _b.sent();
253
+ return [2 /*return*/, this.getDetailsFromTypedUnsignedTransaction(transaction, ownAddress)];
254
+ case 4:
255
+ derivedPublicKey = this.getPublicKeyFromExtendedPublicKey(extendedPublicKey, 0, 0);
256
+ return [4 /*yield*/, this.getAddressFromPublicKey(derivedPublicKey)];
257
+ case 5:
258
+ ownAddress = _b.sent();
259
+ return [2 /*return*/, this.getDetailsFromRawUnsignedTransaction(transaction, ownAddress)];
260
+ case 6:
261
+ (0, coinlib_core_1.assertNever)(transaction);
262
+ throw new errors_1.UnsupportedError(coinlib_core_1.Domain.ETHEREUM, 'Unsupported transaction type.');
145
263
  }
146
- default:
147
- (0, coinlib_core_1.assertNever)(transaction);
148
- throw new errors_1.UnsupportedError(coinlib_core_1.Domain.ETHEREUM, 'Unsupported transaction type.');
149
- }
150
- }
151
- async getDetailsFromTransactionWithExtendedPublicKey(transaction, extendedPublicKey) {
152
- switch (transaction.type) {
153
- case 'signed':
154
- return this.getDetailsFromSignedTransaction(transaction);
155
- case 'unsigned':
156
- if (transaction.ethereumType === 'typed') {
157
- const dps = transaction.derivationPath.split('/');
158
- const derivedPublicKey = this.getPublicKeyFromExtendedPublicKey(extendedPublicKey, Number(dps[dps.length - 2]), Number(dps[dps.length - 1]));
159
- const ownAddress = await this.getAddressFromPublicKey(derivedPublicKey);
160
- return this.getDetailsFromTypedUnsignedTransaction(transaction, ownAddress);
264
+ });
265
+ });
266
+ };
267
+ EthereumBaseProtocolImpl.prototype.getDetailsFromSignedTransaction = function (transaction) {
268
+ var _a, _b;
269
+ return __awaiter(this, void 0, void 0, function () {
270
+ var ethTx, tx, hexValue, hexGasPrice, hexGasLimit, hexNonce, chainId, to, feeTx;
271
+ return __generator(this, function (_c) {
272
+ ethTx = tx_1.TransactionFactory.fromSerializedData(Buffer.from(transaction.serialized, 'hex'));
273
+ if (ethTx.type === 0) {
274
+ tx = ethTx;
275
+ hexValue = tx.value.toString('hex') || '0x0';
276
+ hexGasPrice = tx.gasPrice.toString('hex') || '0x0';
277
+ hexGasLimit = tx.gasLimit.toString('hex') || '0x0';
278
+ hexNonce = tx.nonce.toString('hex') || '0x0';
279
+ chainId = tx.common.chainIdBN().toString(10);
280
+ to = tx.to;
281
+ if (!to) {
282
+ throw new Error('No "TO" address');
283
+ }
284
+ return [2 /*return*/, [
285
+ {
286
+ from: [tx.getSenderAddress().toString()],
287
+ to: [to.toString()],
288
+ isInbound: tx.toCreationAddress(),
289
+ amount: (0, module_kit_1.newAmount)(parseInt(hexValue, 16), 'blockchain'),
290
+ fee: (0, module_kit_1.newAmount)(new bignumber_1.BigNumber(parseInt(hexGasLimit, 16)).multipliedBy(parseInt(hexGasPrice, 16)), 'blockchain'),
291
+ network: this.options.network,
292
+ status: {
293
+ type: 'unknown',
294
+ hash: "0x".concat(tx.hash().toString('hex'))
295
+ },
296
+ arbitraryData: "0x".concat(tx.data.toString('hex')),
297
+ extra: {
298
+ chainId: chainId,
299
+ nonce: parseInt(hexNonce, 16)
300
+ }
301
+ }
302
+ ]];
161
303
  }
162
- else {
163
- const derivedPublicKey = this.getPublicKeyFromExtendedPublicKey(extendedPublicKey, 0, 0);
164
- const ownAddress = await this.getAddressFromPublicKey(derivedPublicKey);
165
- return this.getDetailsFromRawUnsignedTransaction(transaction, ownAddress);
304
+ try {
305
+ feeTx = ethTx;
306
+ return [2 /*return*/, [
307
+ {
308
+ from: [feeTx.getSenderAddress().toString()],
309
+ to: [(_b = (_a = feeTx.to) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : ''],
310
+ isInbound: false,
311
+ amount: (0, module_kit_1.newAmount)(feeTx.value.toString(10), 'blockchain'),
312
+ fee: (0, module_kit_1.newAmount)(new bignumber_1.BigNumber(feeTx.gasLimit.toString(10)).multipliedBy(feeTx.maxFeePerGas.toString(10)), 'blockchain'),
313
+ network: this.options.network,
314
+ arbitraryData: feeTx.data.toString('hex'),
315
+ extra: {
316
+ chainId: feeTx.chainId.toNumber(),
317
+ nonce: feeTx.nonce.toNumber()
318
+ }
319
+ }
320
+ ]];
166
321
  }
167
- default:
168
- (0, coinlib_core_1.assertNever)(transaction);
169
- throw new errors_1.UnsupportedError(coinlib_core_1.Domain.ETHEREUM, 'Unsupported transaction type.');
170
- }
171
- }
172
- async getDetailsFromSignedTransaction(transaction) {
173
- const ethTx = tx_1.TransactionFactory.fromSerializedData(Buffer.from(transaction.serialized, 'hex'));
174
- if (ethTx.type === 0) {
175
- const tx = ethTx;
176
- const hexValue = tx.value.toString('hex') || '0x0';
177
- const hexGasPrice = tx.gasPrice.toString('hex') || '0x0';
178
- const hexGasLimit = tx.gasLimit.toString('hex') || '0x0';
179
- const hexNonce = tx.nonce.toString('hex') || '0x0';
180
- const chainId = tx.common.chainIdBN().toString(10);
181
- const to = tx.to;
182
- if (!to) {
183
- throw new Error('No "TO" address');
184
- }
185
- return [
186
- {
187
- from: [tx.getSenderAddress().toString()],
188
- to: [to.toString()],
189
- isInbound: tx.toCreationAddress(),
190
- amount: (0, module_kit_1.newAmount)(parseInt(hexValue, 16), 'blockchain'),
191
- fee: (0, module_kit_1.newAmount)(new bignumber_1.BigNumber(parseInt(hexGasLimit, 16)).multipliedBy(parseInt(hexGasPrice, 16)), 'blockchain'),
192
- network: this.options.network,
193
- status: {
194
- type: 'unknown',
195
- hash: `0x${tx.hash().toString('hex')}`
196
- },
197
- arbitraryData: `0x${tx.data.toString('hex')}`,
198
- extra: {
199
- chainId,
200
- nonce: parseInt(hexNonce, 16)
201
- }
322
+ catch (e) {
323
+ throw new Error("Transaction type \"".concat(ethTx.type, "\" not supported"));
202
324
  }
203
- ];
204
- }
205
- try {
206
- const feeTx = ethTx;
207
- return [
208
- {
209
- from: [feeTx.getSenderAddress().toString()],
210
- to: [feeTx.to?.toString() ?? ''],
325
+ return [2 /*return*/];
326
+ });
327
+ });
328
+ };
329
+ EthereumBaseProtocolImpl.prototype.getDetailsFromTypedUnsignedTransaction = function (transaction, ownAddress) {
330
+ var _a, _b, _c;
331
+ return __awaiter(this, void 0, void 0, function () {
332
+ var typedTransaction, airGapTransaction;
333
+ return __generator(this, function (_d) {
334
+ typedTransaction = tx_1.TransactionFactory.fromSerializedData(Buffer.from(transaction.serialized, 'hex'));
335
+ airGapTransaction = {
336
+ from: [ownAddress],
337
+ to: [(_b = (_a = typedTransaction.to) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : ''],
211
338
  isInbound: false,
212
- amount: (0, module_kit_1.newAmount)(feeTx.value.toString(10), 'blockchain'),
213
- fee: (0, module_kit_1.newAmount)(new bignumber_1.BigNumber(feeTx.gasLimit.toString(10)).multipliedBy(feeTx.maxFeePerGas.toString(10)), 'blockchain'),
339
+ amount: (0, module_kit_1.newAmount)(typedTransaction.value.toString(10), 'blockchain'),
340
+ fee: (0, module_kit_1.newAmount)(new bignumber_1.BigNumber(typedTransaction.gasLimit.toString(10)).multipliedBy(typedTransaction.maxFeePerGas.toString(10)), 'blockchain'),
214
341
  network: this.options.network,
215
- arbitraryData: feeTx.data.toString('hex'),
216
- extra: {
217
- chainId: feeTx.chainId.toNumber(),
218
- nonce: feeTx.nonce.toNumber()
219
- }
342
+ arbitraryData: typedTransaction.data.toString('hex'),
343
+ uiAlerts: typedTransaction.chainId.toNumber() !== 1
344
+ ? [
345
+ (0, module_kit_1.newWarningUIAlert)({
346
+ title: (0, module_kit_1.newPlainUIText)('Chain ID'),
347
+ description: (0, module_kit_1.newPlainUIText)("Please note that this is not an Ethereum Mainnet transaction, it is from ".concat((_c = EthereumChainIds_1.ETHEREUM_CHAIN_IDS[typedTransaction.chainId.toNumber()]) !== null && _c !== void 0 ? _c : "Chain ID ".concat(typedTransaction.chainId.toNumber())))
348
+ })
349
+ ]
350
+ : undefined
351
+ };
352
+ return [2 /*return*/, [airGapTransaction]];
353
+ });
354
+ });
355
+ };
356
+ EthereumBaseProtocolImpl.prototype.getDetailsFromRawUnsignedTransaction = function (transaction, ownAddress) {
357
+ return __awaiter(this, void 0, void 0, function () {
358
+ return __generator(this, function (_a) {
359
+ return [2 /*return*/, [
360
+ {
361
+ from: [ownAddress],
362
+ to: [transaction.to],
363
+ isInbound: false,
364
+ amount: (0, module_kit_1.newAmount)(transaction.value, 'blockchain'),
365
+ fee: (0, module_kit_1.newAmount)(new bignumber_1.BigNumber(transaction.gasLimit).multipliedBy(transaction.gasPrice), 'blockchain'),
366
+ network: this.options.network,
367
+ arbitraryData: transaction.data
368
+ }
369
+ ]];
370
+ });
371
+ });
372
+ };
373
+ EthereumBaseProtocolImpl.prototype.verifyMessageWithPublicKey = function (message, signature, publicKey) {
374
+ return __awaiter(this, void 0, void 0, function () {
375
+ var hexSignature, hexPublicKey;
376
+ return __generator(this, function (_a) {
377
+ hexSignature = signature;
378
+ hexPublicKey = (0, key_1.convertPublicKey)(this.nonExtendedPublicKey(publicKey), 'hex');
379
+ return [2 /*return*/, this.cryptoClient.verifyMessage(message, hexSignature.value, hexPublicKey.value)];
380
+ });
381
+ });
382
+ };
383
+ EthereumBaseProtocolImpl.prototype.encryptAsymmetricWithPublicKey = function (payload, publicKey) {
384
+ return __awaiter(this, void 0, void 0, function () {
385
+ var hexPublicKey;
386
+ return __generator(this, function (_a) {
387
+ hexPublicKey = (0, key_1.convertPublicKey)(this.nonExtendedPublicKey(publicKey), 'hex');
388
+ return [2 /*return*/, this.cryptoClient.encryptAsymmetric(payload, hexPublicKey.value)];
389
+ });
390
+ });
391
+ };
392
+ EthereumBaseProtocolImpl.prototype.getCryptoConfiguration = function () {
393
+ return __awaiter(this, void 0, void 0, function () {
394
+ return __generator(this, function (_a) {
395
+ return [2 /*return*/, this.cryptoConfiguration];
396
+ });
397
+ });
398
+ };
399
+ EthereumBaseProtocolImpl.prototype.getKeyPairFromDerivative = function (derivative) {
400
+ return __awaiter(this, void 0, void 0, function () {
401
+ var node;
402
+ return __generator(this, function (_a) {
403
+ node = this.derivativeToBip32Node(derivative);
404
+ return [2 /*return*/, {
405
+ secretKey: (0, module_kit_1.newSecretKey)(node.keyPair.getPrivateKeyBuffer().toString('hex'), 'hex'),
406
+ publicKey: (0, module_kit_1.newPublicKey)(node.neutered().keyPair.getPublicKeyBuffer().toString('hex'), 'hex')
407
+ }];
408
+ });
409
+ });
410
+ };
411
+ EthereumBaseProtocolImpl.prototype.getExtendedKeyPairFromDerivative = function (derivative) {
412
+ return __awaiter(this, void 0, void 0, function () {
413
+ var node;
414
+ return __generator(this, function (_a) {
415
+ node = this.derivativeToBip32Node(derivative);
416
+ return [2 /*return*/, {
417
+ secretKey: (0, module_kit_1.newExtendedSecretKey)(node.toBase58(), 'encoded'),
418
+ publicKey: (0, module_kit_1.newExtendedPublicKey)(node.neutered().toBase58(), 'encoded')
419
+ }];
420
+ });
421
+ });
422
+ };
423
+ EthereumBaseProtocolImpl.prototype.deriveFromExtendedSecretKey = function (extendedSecretKey, visibilityIndex, addressIndex) {
424
+ return __awaiter(this, void 0, void 0, function () {
425
+ return __generator(this, function (_a) {
426
+ return [2 /*return*/, this.getSecretKeyFromExtendedSecretKey(extendedSecretKey, visibilityIndex, addressIndex)];
427
+ });
428
+ });
429
+ };
430
+ EthereumBaseProtocolImpl.prototype.signTransactionWithSecretKey = function (transaction, secretKey) {
431
+ return __awaiter(this, void 0, void 0, function () {
432
+ return __generator(this, function (_a) {
433
+ return [2 /*return*/, transaction.ethereumType === 'typed'
434
+ ? this.signTypedUnsignedTransactionWithSecretKey(transaction, this.nonExtendedSecretKey(secretKey))
435
+ : this.signRawUnsignedTransactionWithSecretKey(transaction, this.nonExtendedSecretKey(secretKey))];
436
+ });
437
+ });
438
+ };
439
+ EthereumBaseProtocolImpl.prototype.signTypedUnsignedTransactionWithSecretKey = function (transaction, secretKey) {
440
+ return __awaiter(this, void 0, void 0, function () {
441
+ var typedTransaction;
442
+ return __generator(this, function (_a) {
443
+ typedTransaction = tx_1.TransactionFactory.fromSerializedData(Buffer.from(transaction.serialized, 'hex'));
444
+ return [2 /*return*/, this.signTypedTransactionWithSecretKey(typedTransaction, secretKey)];
445
+ });
446
+ });
447
+ };
448
+ EthereumBaseProtocolImpl.prototype.signRawUnsignedTransactionWithSecretKey = function (transaction, secretKey) {
449
+ return __awaiter(this, void 0, void 0, function () {
450
+ var txData, common, typedTransaction;
451
+ return __generator(this, function (_a) {
452
+ txData = {
453
+ nonce: transaction.nonce,
454
+ gasPrice: transaction.gasPrice,
455
+ gasLimit: transaction.gasLimit,
456
+ to: transaction.to,
457
+ value: transaction.value.startsWith('0x') ? transaction.value : EthereumUtils_1.EthereumUtils.toHex(parseInt(transaction.value, 10)),
458
+ data: transaction.data
459
+ };
460
+ try {
461
+ common = new common_1.default({ chain: transaction.chainId });
220
462
  }
221
- ];
222
- }
223
- catch (e) {
224
- throw new Error(`Transaction type "${ethTx.type}" not supported`);
225
- }
226
- }
227
- async getDetailsFromTypedUnsignedTransaction(transaction, ownAddress) {
228
- const typedTransaction = tx_1.TransactionFactory.fromSerializedData(Buffer.from(transaction.serialized, 'hex'));
229
- const airGapTransaction = {
230
- from: [ownAddress],
231
- to: [typedTransaction.to?.toString() ?? ''],
232
- isInbound: false,
233
- amount: (0, module_kit_1.newAmount)(typedTransaction.value.toString(10), 'blockchain'),
234
- fee: (0, module_kit_1.newAmount)(new bignumber_1.BigNumber(typedTransaction.gasLimit.toString(10)).multipliedBy(typedTransaction.maxFeePerGas.toString(10)), 'blockchain'),
235
- network: this.options.network,
236
- arbitraryData: typedTransaction.data.toString('hex'),
237
- uiAlerts: typedTransaction.chainId.toNumber() !== 1
238
- ? [
239
- (0, module_kit_1.newWarningUIAlert)({
240
- title: (0, module_kit_1.newPlainUIText)('Chain ID'),
241
- description: (0, module_kit_1.newPlainUIText)(`Please note that this is not an Ethereum Mainnet transaction, it is from ${EthereumChainIds_1.ETHEREUM_CHAIN_IDS[typedTransaction.chainId.toNumber()] ?? `Chain ID ${typedTransaction.chainId.toNumber()}`}`)
242
- })
243
- ]
244
- : undefined
245
- };
246
- return [airGapTransaction];
247
- }
248
- async getDetailsFromRawUnsignedTransaction(transaction, ownAddress) {
249
- return [
250
- {
251
- from: [ownAddress],
252
- to: [transaction.to],
253
- isInbound: false,
254
- amount: (0, module_kit_1.newAmount)(transaction.value, 'blockchain'),
255
- fee: (0, module_kit_1.newAmount)(new bignumber_1.BigNumber(transaction.gasLimit).multipliedBy(transaction.gasPrice), 'blockchain'),
256
- network: this.options.network,
257
- arbitraryData: transaction.data
258
- }
259
- ];
260
- }
261
- async verifyMessageWithPublicKey(message, signature, publicKey) {
262
- const hexSignature = signature;
263
- const hexPublicKey = (0, key_1.convertPublicKey)(this.nonExtendedPublicKey(publicKey), 'hex');
264
- return this.cryptoClient.verifyMessage(message, hexSignature.value, hexPublicKey.value);
265
- }
266
- async encryptAsymmetricWithPublicKey(payload, publicKey) {
267
- const hexPublicKey = (0, key_1.convertPublicKey)(this.nonExtendedPublicKey(publicKey), 'hex');
268
- return this.cryptoClient.encryptAsymmetric(payload, hexPublicKey.value);
269
- }
270
- async getCryptoConfiguration() {
271
- return this.cryptoConfiguration;
272
- }
273
- async getKeyPairFromDerivative(derivative) {
274
- const node = this.derivativeToBip32Node(derivative);
275
- return {
276
- secretKey: (0, module_kit_1.newSecretKey)(node.keyPair.getPrivateKeyBuffer().toString('hex'), 'hex'),
277
- publicKey: (0, module_kit_1.newPublicKey)(node.neutered().keyPair.getPublicKeyBuffer().toString('hex'), 'hex')
278
- };
279
- }
280
- async getExtendedKeyPairFromDerivative(derivative) {
281
- const node = this.derivativeToBip32Node(derivative);
282
- return {
283
- secretKey: (0, module_kit_1.newExtendedSecretKey)(node.toBase58(), 'encoded'),
284
- publicKey: (0, module_kit_1.newExtendedPublicKey)(node.neutered().toBase58(), 'encoded')
285
- };
286
- }
287
- async deriveFromExtendedSecretKey(extendedSecretKey, visibilityIndex, addressIndex) {
288
- return this.getSecretKeyFromExtendedSecretKey(extendedSecretKey, visibilityIndex, addressIndex);
289
- }
290
- async signTransactionWithSecretKey(transaction, secretKey) {
291
- return transaction.ethereumType === 'typed'
292
- ? this.signTypedUnsignedTransactionWithSecretKey(transaction, this.nonExtendedSecretKey(secretKey))
293
- : this.signRawUnsignedTransactionWithSecretKey(transaction, this.nonExtendedSecretKey(secretKey));
294
- }
295
- async signTypedUnsignedTransactionWithSecretKey(transaction, secretKey) {
296
- const typedTransaction = tx_1.TransactionFactory.fromSerializedData(Buffer.from(transaction.serialized, 'hex'));
297
- return this.signTypedTransactionWithSecretKey(typedTransaction, secretKey);
298
- }
299
- async signRawUnsignedTransactionWithSecretKey(transaction, secretKey) {
300
- const txData = {
301
- nonce: transaction.nonce,
302
- gasPrice: transaction.gasPrice,
303
- gasLimit: transaction.gasLimit,
304
- to: transaction.to,
305
- value: transaction.value.startsWith('0x') ? transaction.value : EthereumUtils_1.EthereumUtils.toHex(parseInt(transaction.value, 10)),
306
- data: transaction.data
307
- };
308
- let common;
309
- try {
310
- common = new common_1.default({ chain: transaction.chainId });
311
- }
312
- catch {
313
- common = common_1.default.custom({ chainId: transaction.chainId });
314
- }
315
- const typedTransaction = tx_1.TransactionFactory.fromTxData(txData, { common });
316
- return this.signTypedTransactionWithSecretKey(typedTransaction, secretKey);
317
- }
318
- async signTypedTransactionWithSecretKey(transaction, secretKey) {
319
- const hexSecretKey = (0, key_1.convertSecretKey)(secretKey, 'hex');
320
- const signedTransaction = transaction.sign(Buffer.from(hexSecretKey.value, 'hex'));
321
- return (0, module_kit_1.newSignedTransaction)({
322
- serialized: signedTransaction.serialize().toString('hex')
463
+ catch (_b) {
464
+ common = common_1.default.custom({ chainId: transaction.chainId });
465
+ }
466
+ typedTransaction = tx_1.TransactionFactory.fromTxData(txData, { common: common });
467
+ return [2 /*return*/, this.signTypedTransactionWithSecretKey(typedTransaction, secretKey)];
468
+ });
323
469
  });
324
- }
325
- async signMessageWithKeyPair(message, keyPair) {
326
- const hexSecretKey = (0, key_1.convertSecretKey)(this.nonExtendedSecretKey(keyPair.secretKey), 'hex');
327
- const signature = await this.cryptoClient.signMessage(message, { privateKey: hexSecretKey.value });
328
- return (0, module_kit_1.newSignature)(signature, 'hex');
329
- }
330
- async decryptAsymmetricWithKeyPair(payload, keyPair) {
331
- const hexSecretKey = (0, key_1.convertSecretKey)(this.nonExtendedSecretKey(keyPair.secretKey), 'hex');
332
- const hexPublicKey = (0, key_1.convertPublicKey)(this.nonExtendedPublicKey(keyPair.publicKey), 'hex');
333
- return this.cryptoClient.decryptAsymmetric(payload, { privateKey: hexSecretKey.value, publicKey: hexPublicKey.value });
334
- }
335
- async encryptAESWithSecretKey(payload, secretKey) {
336
- const hexSecretKey = (0, key_1.convertSecretKey)(this.nonExtendedSecretKey(secretKey), 'hex');
337
- return this.cryptoClient.encryptAES(payload, hexSecretKey.value);
338
- }
339
- async decryptAESWithSecretKey(payload, secretKey) {
340
- const hexSecretKey = (0, key_1.convertSecretKey)(this.nonExtendedSecretKey(secretKey), 'hex');
341
- return this.cryptoClient.decryptAES(payload, hexSecretKey.value);
342
- }
470
+ };
471
+ EthereumBaseProtocolImpl.prototype.signTypedTransactionWithSecretKey = function (transaction, secretKey) {
472
+ return __awaiter(this, void 0, void 0, function () {
473
+ var hexSecretKey, signedTransaction;
474
+ return __generator(this, function (_a) {
475
+ hexSecretKey = (0, key_1.convertSecretKey)(secretKey, 'hex');
476
+ signedTransaction = transaction.sign(Buffer.from(hexSecretKey.value, 'hex'));
477
+ return [2 /*return*/, (0, module_kit_1.newSignedTransaction)({
478
+ serialized: signedTransaction.serialize().toString('hex')
479
+ })];
480
+ });
481
+ });
482
+ };
483
+ EthereumBaseProtocolImpl.prototype.signMessageWithKeyPair = function (message, keyPair) {
484
+ return __awaiter(this, void 0, void 0, function () {
485
+ var hexSecretKey, signature;
486
+ return __generator(this, function (_a) {
487
+ switch (_a.label) {
488
+ case 0:
489
+ hexSecretKey = (0, key_1.convertSecretKey)(this.nonExtendedSecretKey(keyPair.secretKey), 'hex');
490
+ return [4 /*yield*/, this.cryptoClient.signMessage(message, { privateKey: hexSecretKey.value })];
491
+ case 1:
492
+ signature = _a.sent();
493
+ return [2 /*return*/, (0, module_kit_1.newSignature)(signature, 'hex')];
494
+ }
495
+ });
496
+ });
497
+ };
498
+ EthereumBaseProtocolImpl.prototype.decryptAsymmetricWithKeyPair = function (payload, keyPair) {
499
+ return __awaiter(this, void 0, void 0, function () {
500
+ var hexSecretKey, hexPublicKey;
501
+ return __generator(this, function (_a) {
502
+ hexSecretKey = (0, key_1.convertSecretKey)(this.nonExtendedSecretKey(keyPair.secretKey), 'hex');
503
+ hexPublicKey = (0, key_1.convertPublicKey)(this.nonExtendedPublicKey(keyPair.publicKey), 'hex');
504
+ return [2 /*return*/, this.cryptoClient.decryptAsymmetric(payload, { privateKey: hexSecretKey.value, publicKey: hexPublicKey.value })];
505
+ });
506
+ });
507
+ };
508
+ EthereumBaseProtocolImpl.prototype.encryptAESWithSecretKey = function (payload, secretKey) {
509
+ return __awaiter(this, void 0, void 0, function () {
510
+ var hexSecretKey;
511
+ return __generator(this, function (_a) {
512
+ hexSecretKey = (0, key_1.convertSecretKey)(this.nonExtendedSecretKey(secretKey), 'hex');
513
+ return [2 /*return*/, this.cryptoClient.encryptAES(payload, hexSecretKey.value)];
514
+ });
515
+ });
516
+ };
517
+ EthereumBaseProtocolImpl.prototype.decryptAESWithSecretKey = function (payload, secretKey) {
518
+ return __awaiter(this, void 0, void 0, function () {
519
+ var hexSecretKey;
520
+ return __generator(this, function (_a) {
521
+ hexSecretKey = (0, key_1.convertSecretKey)(this.nonExtendedSecretKey(secretKey), 'hex');
522
+ return [2 /*return*/, this.cryptoClient.decryptAES(payload, hexSecretKey.value)];
523
+ });
524
+ });
525
+ };
343
526
  // Online
344
- async getNetwork() {
345
- return this.options.network;
346
- }
347
- async getTransactionsForPublicKey(publicKey, limit, cursor) {
348
- const address = await this.getAddressFromPublicKey(publicKey);
349
- return this.getTransactionsForAddress(address, limit, cursor);
350
- }
351
- async getTransactionsForAddress(address, limit, cursor) {
352
- return this.getTransactionsForAddresses([address], limit, cursor);
353
- }
354
- async getTransactionsForAddresses(addresses, limit, cursor) {
355
- return new Promise((overallResolve, overallReject) => {
356
- const promises = [];
357
- for (const address of addresses) {
358
- promises.push(this.infoClient.fetchTransactions(address, limit, cursor));
359
- }
360
- Promise.all(promises)
361
- .then((values) => {
362
- const page = Math.max(...values.map((txResult) => txResult.cursor.page));
363
- const transactions = values.reduce((acc, current) => {
364
- return acc.concat(current.transactions.map((tx) => ({
365
- ...tx,
366
- amount: (0, module_kit_1.newAmount)(tx.amount.value, 'blockchain'),
367
- fee: (0, module_kit_1.newAmount)(tx.fee.value, 'blockchain'),
368
- network: this.options.network
369
- })));
370
- }, []);
371
- const hasNext = transactions.length >= limit;
372
- overallResolve({
373
- transactions,
374
- cursor: {
375
- hasNext,
376
- page: hasNext ? page : undefined
377
- }
378
- });
379
- })
380
- .catch(overallReject);
527
+ EthereumBaseProtocolImpl.prototype.getNetwork = function () {
528
+ return __awaiter(this, void 0, void 0, function () {
529
+ return __generator(this, function (_a) {
530
+ return [2 /*return*/, this.options.network];
531
+ });
381
532
  });
382
- }
383
- async getTransactionStatus(transactionIds) {
384
- const statuses = await Promise.all(transactionIds.map(async (txHash) => {
385
- return [txHash, await this.nodeClient.getTransactionStatus(txHash)];
386
- }));
387
- return statuses.reduce((obj, next) => Object.assign(obj, { [next[0]]: next[1] }), {});
388
- }
389
- async getBalanceOfPublicKey(publicKey) {
390
- const address = await this.getAddressFromPublicKey(publicKey);
391
- return this.getBalanceOfAddress(address);
392
- }
393
- async getBalanceOfAddress(address) {
394
- return this.getBalanceOfAddresses([address]);
395
- }
396
- async getBalanceOfAddresses(addresses) {
397
- const balances = await Promise.all(addresses.map((address) => {
398
- return this.nodeClient.fetchBalance(address);
399
- }));
400
- return {
401
- total: (0, module_kit_1.newAmount)(balances.reduce((a, b) => a.plus(b)), 'blockchain')
402
- };
403
- }
404
- async getTokenBalancesOfPublicKey(publicKey, tokens) {
405
- const address = await this.getAddressFromPublicKey(publicKey);
406
- const contractAddresses = tokens.map((token) => token.contractAddress);
407
- const balances = await this.nodeClient.callBalanceOfOnContracts(contractAddresses, address);
408
- return tokens.reduce((obj, next) => {
409
- const balance = balances[next.contractAddress];
410
- if (balance === undefined) {
411
- return obj;
412
- }
413
- return Object.assign(obj, { [next.identifier]: (0, module_kit_1.newAmount)(balance, 'blockchain') });
414
- }, {});
415
- }
416
- async getTransactionMaxAmountWithPublicKey(publicKey, to, configuration) {
417
- const { total, transferable } = await this.getBalanceOfPublicKey(publicKey);
418
- const balance = new bignumber_1.BigNumber((0, module_kit_1.newAmount)(transferable ?? total).blockchain(this.units).value);
419
- let fee;
420
- if (configuration?.fee !== undefined) {
421
- fee = configuration.fee;
422
- }
423
- else {
424
- const estimatedFee = await this.getTransactionFeeWithPublicKey(publicKey, to.map((recipient) => ({
425
- to: recipient,
426
- amount: (0, module_kit_1.newAmount)(balance.div(to.length).decimalPlaces(0, bignumber_1.BigNumber.ROUND_CEIL), 'blockchain')
427
- })));
428
- fee = (0, module_kit_1.newAmount)(estimatedFee.medium).blockchain(this.feeUnits);
429
- if (balance.lte(fee.value)) {
430
- fee = (0, module_kit_1.newAmount)(0, 'blockchain');
431
- }
432
- }
433
- let amountWithoutFees = balance.minus(fee.value);
434
- if (amountWithoutFees.isNegative()) {
435
- amountWithoutFees = new bignumber_1.BigNumber(0);
436
- }
437
- return (0, module_kit_1.newAmount)(amountWithoutFees, 'blockchain');
438
- }
439
- async getTransactionFeeWithPublicKey(publicKey, details, configuration) {
440
- if (details.length !== 1) {
441
- throw new errors_1.ConditionViolationError(coinlib_core_1.Domain.ETHEREUM, 'you cannot have 0 transaction details');
442
- }
443
- const address = await this.getAddressFromPublicKey(publicKey);
444
- const estimatedGas = await this.estimateGas(address, details[0].to, (0, module_kit_1.newAmount)(details[0].amount).blockchain(this.units));
445
- const gasPrice = await this.nodeClient.getGasPrice();
446
- const feeStepFactor = new bignumber_1.BigNumber(0.5);
447
- const estimatedFee = estimatedGas.times(gasPrice);
448
- const lowFee = estimatedFee.minus(estimatedFee.times(feeStepFactor).integerValue(bignumber_1.BigNumber.ROUND_FLOOR));
449
- const mediumFee = estimatedFee;
450
- const highFee = mediumFee.plus(mediumFee.times(feeStepFactor).integerValue(bignumber_1.BigNumber.ROUND_FLOOR));
451
- return {
452
- low: (0, module_kit_1.newAmount)(lowFee, 'blockchain'),
453
- medium: (0, module_kit_1.newAmount)(mediumFee, 'blockchain'),
454
- high: (0, module_kit_1.newAmount)(highFee, 'blockchain')
455
- };
456
- }
457
- async prepareTransactionWithPublicKey(publicKey, details, configuration) {
458
- if (details.length !== 1) {
459
- throw new errors_1.ConditionViolationError(coinlib_core_1.Domain.ETHEREUM, 'you cannot have 0 transaction details');
460
- }
461
- let fee;
462
- if (configuration?.fee !== undefined) {
463
- fee = configuration.fee;
464
- }
465
- else {
466
- const estimatedFee = await this.getTransactionFeeWithPublicKey(publicKey, details);
467
- fee = estimatedFee.medium;
468
- }
469
- const wrappedFee = new bignumber_1.BigNumber((0, module_kit_1.newAmount)(fee).blockchain(this.feeUnits).value);
470
- const wrappedAmount = new bignumber_1.BigNumber((0, module_kit_1.newAmount)(details[0].amount).blockchain(this.units).value);
471
- const address = await this.getAddressFromPublicKey(publicKey);
472
- const hexAmount = EthereumUtils_1.EthereumUtils.toHex(wrappedAmount.toFixed());
473
- const balance = await this.getBalanceOfPublicKey(publicKey);
474
- const availableBalance = (0, module_kit_1.newAmount)(balance.transferable ?? balance.total).blockchain(this.units);
475
- const gasLimit = await this.estimateGas(address, details[0].to, hexAmount);
476
- const gasPrice = wrappedFee.div(gasLimit).integerValue(bignumber_1.BigNumber.ROUND_CEIL);
477
- if (new bignumber_1.BigNumber(availableBalance.value).gte(wrappedAmount.plus(wrappedFee))) {
478
- const txCount = await this.nodeClient.fetchTransactionCount(address);
479
- const transaction = (0, module_kit_1.newUnsignedTransaction)({
480
- ethereumType: 'raw',
481
- nonce: EthereumUtils_1.EthereumUtils.toHex(txCount),
482
- gasLimit: EthereumUtils_1.EthereumUtils.toHex(gasLimit.toFixed()),
483
- gasPrice: EthereumUtils_1.EthereumUtils.toHex(gasPrice.toFixed()), // 10 Gwei
484
- to: details[0].to,
485
- value: hexAmount,
486
- chainId: this.options.network.chainId,
487
- data: '0x'
488
- });
489
- return transaction;
490
- }
491
- else {
492
- throw new errors_1.BalanceError(coinlib_core_1.Domain.ETHEREUM, 'not enough balance');
493
- }
494
- }
495
- async getWalletConnectChain() {
496
- return `${WALLET_CONNECT_NAMESPACE}:${this.options.network.chainId}`;
497
- }
498
- async prepareWalletConnectTransactionWithPublicKey(publicKey, request) {
499
- const gasPricePromise = request.gasPrice
500
- ? Promise.resolve(request.gasPrice)
501
- : this.nodeClient.getGasPrice().then((gasPrice) => `0x${gasPrice.toString(16)}`);
502
- const noncePromise = request.nonce
503
- ? Promise.resolve(request.nonce)
504
- : this.getAddressFromPublicKey(publicKey)
505
- .then((address) => this.nodeClient.fetchTransactionCount(address))
506
- .then((transactionCount) => `0x${new bignumber_1.BigNumber(transactionCount).toString(16)}`);
507
- const [gasPrice, nonce] = await Promise.all([gasPricePromise, noncePromise]);
508
- return (0, module_kit_1.newUnsignedTransaction)({
509
- ethereumType: 'raw',
510
- nonce,
511
- gasLimit: `0x${(300000).toString(16)}`,
512
- gasPrice,
513
- to: request.to ?? '',
514
- value: request.value ?? '0x00',
515
- chainId: this.options.network.chainId,
516
- data: request.data ?? '0x'
533
+ };
534
+ EthereumBaseProtocolImpl.prototype.getTransactionsForPublicKey = function (publicKey, limit, cursor) {
535
+ return __awaiter(this, void 0, void 0, function () {
536
+ var address;
537
+ return __generator(this, function (_a) {
538
+ switch (_a.label) {
539
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
540
+ case 1:
541
+ address = _a.sent();
542
+ return [2 /*return*/, this.getTransactionsForAddress(address, limit, cursor)];
543
+ }
544
+ });
517
545
  });
518
- }
519
- async broadcastTransaction(transaction) {
520
- return this.nodeClient.sendSignedTransaction(`0x${transaction.serialized.replace(/^0x/, '')}`);
521
- }
546
+ };
547
+ EthereumBaseProtocolImpl.prototype.getTransactionsForAddress = function (address, limit, cursor) {
548
+ return __awaiter(this, void 0, void 0, function () {
549
+ return __generator(this, function (_a) {
550
+ return [2 /*return*/, this.getTransactionsForAddresses([address], limit, cursor)];
551
+ });
552
+ });
553
+ };
554
+ EthereumBaseProtocolImpl.prototype.getTransactionsForAddresses = function (addresses, limit, cursor) {
555
+ return __awaiter(this, void 0, void 0, function () {
556
+ var _this = this;
557
+ return __generator(this, function (_a) {
558
+ return [2 /*return*/, new Promise(function (overallResolve, overallReject) {
559
+ var promises = [];
560
+ for (var _i = 0, addresses_1 = addresses; _i < addresses_1.length; _i++) {
561
+ var address = addresses_1[_i];
562
+ promises.push(_this.infoClient.fetchTransactions(address, limit, cursor));
563
+ }
564
+ Promise.all(promises)
565
+ .then(function (values) {
566
+ var page = Math.max.apply(Math, values.map(function (txResult) { return txResult.cursor.page; }));
567
+ var transactions = values.reduce(function (acc, current) {
568
+ return acc.concat(current.transactions.map(function (tx) { return (__assign(__assign({}, tx), { amount: (0, module_kit_1.newAmount)(tx.amount.value, 'blockchain'), fee: (0, module_kit_1.newAmount)(tx.fee.value, 'blockchain'), network: _this.options.network })); }));
569
+ }, []);
570
+ var hasNext = transactions.length >= limit;
571
+ overallResolve({
572
+ transactions: transactions,
573
+ cursor: {
574
+ hasNext: hasNext,
575
+ page: hasNext ? page : undefined
576
+ }
577
+ });
578
+ })
579
+ .catch(overallReject);
580
+ })];
581
+ });
582
+ });
583
+ };
584
+ EthereumBaseProtocolImpl.prototype.getTransactionStatus = function (transactionIds) {
585
+ return __awaiter(this, void 0, void 0, function () {
586
+ var statuses;
587
+ var _this = this;
588
+ return __generator(this, function (_a) {
589
+ switch (_a.label) {
590
+ case 0: return [4 /*yield*/, Promise.all(transactionIds.map(function (txHash) { return __awaiter(_this, void 0, void 0, function () {
591
+ var _a;
592
+ return __generator(this, function (_b) {
593
+ switch (_b.label) {
594
+ case 0:
595
+ _a = [txHash];
596
+ return [4 /*yield*/, this.nodeClient.getTransactionStatus(txHash)];
597
+ case 1: return [2 /*return*/, _a.concat([_b.sent()])];
598
+ }
599
+ });
600
+ }); }))];
601
+ case 1:
602
+ statuses = _a.sent();
603
+ return [2 /*return*/, statuses.reduce(function (obj, next) {
604
+ var _a;
605
+ return Object.assign(obj, (_a = {}, _a[next[0]] = next[1], _a));
606
+ }, {})];
607
+ }
608
+ });
609
+ });
610
+ };
611
+ EthereumBaseProtocolImpl.prototype.getBalanceOfPublicKey = function (publicKey) {
612
+ return __awaiter(this, void 0, void 0, function () {
613
+ var address;
614
+ return __generator(this, function (_a) {
615
+ switch (_a.label) {
616
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
617
+ case 1:
618
+ address = _a.sent();
619
+ return [2 /*return*/, this.getBalanceOfAddress(address)];
620
+ }
621
+ });
622
+ });
623
+ };
624
+ EthereumBaseProtocolImpl.prototype.getBalanceOfAddress = function (address) {
625
+ return __awaiter(this, void 0, void 0, function () {
626
+ return __generator(this, function (_a) {
627
+ return [2 /*return*/, this.getBalanceOfAddresses([address])];
628
+ });
629
+ });
630
+ };
631
+ EthereumBaseProtocolImpl.prototype.getBalanceOfAddresses = function (addresses) {
632
+ return __awaiter(this, void 0, void 0, function () {
633
+ var balances;
634
+ var _this = this;
635
+ return __generator(this, function (_a) {
636
+ switch (_a.label) {
637
+ case 0: return [4 /*yield*/, Promise.all(addresses.map(function (address) {
638
+ return _this.nodeClient.fetchBalance(address);
639
+ }))];
640
+ case 1:
641
+ balances = _a.sent();
642
+ return [2 /*return*/, {
643
+ total: (0, module_kit_1.newAmount)(balances.reduce(function (a, b) { return a.plus(b); }), 'blockchain')
644
+ }];
645
+ }
646
+ });
647
+ });
648
+ };
649
+ EthereumBaseProtocolImpl.prototype.getTokenBalancesOfPublicKey = function (publicKey, tokens) {
650
+ return __awaiter(this, void 0, void 0, function () {
651
+ var address, contractAddresses, balances;
652
+ return __generator(this, function (_a) {
653
+ switch (_a.label) {
654
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
655
+ case 1:
656
+ address = _a.sent();
657
+ contractAddresses = tokens.map(function (token) { return token.contractAddress; });
658
+ return [4 /*yield*/, this.nodeClient.callBalanceOfOnContracts(contractAddresses, address)];
659
+ case 2:
660
+ balances = _a.sent();
661
+ return [2 /*return*/, tokens.reduce(function (obj, next) {
662
+ var _a;
663
+ var balance = balances[next.contractAddress];
664
+ if (balance === undefined) {
665
+ return obj;
666
+ }
667
+ return Object.assign(obj, (_a = {}, _a[next.identifier] = (0, module_kit_1.newAmount)(balance, 'blockchain'), _a));
668
+ }, {})];
669
+ }
670
+ });
671
+ });
672
+ };
673
+ EthereumBaseProtocolImpl.prototype.getTransactionMaxAmountWithPublicKey = function (publicKey, to, configuration) {
674
+ return __awaiter(this, void 0, void 0, function () {
675
+ var _a, total, transferable, balance, fee, estimatedFee, amountWithoutFees;
676
+ return __generator(this, function (_b) {
677
+ switch (_b.label) {
678
+ case 0: return [4 /*yield*/, this.getBalanceOfPublicKey(publicKey)];
679
+ case 1:
680
+ _a = _b.sent(), total = _a.total, transferable = _a.transferable;
681
+ balance = new bignumber_1.BigNumber((0, module_kit_1.newAmount)(transferable !== null && transferable !== void 0 ? transferable : total).blockchain(this.units).value);
682
+ if (!((configuration === null || configuration === void 0 ? void 0 : configuration.fee) !== undefined)) return [3 /*break*/, 2];
683
+ fee = configuration.fee;
684
+ return [3 /*break*/, 4];
685
+ case 2: return [4 /*yield*/, this.getTransactionFeeWithPublicKey(publicKey, to.map(function (recipient) { return ({
686
+ to: recipient,
687
+ amount: (0, module_kit_1.newAmount)(balance.div(to.length).decimalPlaces(0, bignumber_1.BigNumber.ROUND_CEIL), 'blockchain')
688
+ }); }))];
689
+ case 3:
690
+ estimatedFee = _b.sent();
691
+ fee = (0, module_kit_1.newAmount)(estimatedFee.medium).blockchain(this.feeUnits);
692
+ if (balance.lte(fee.value)) {
693
+ fee = (0, module_kit_1.newAmount)(0, 'blockchain');
694
+ }
695
+ _b.label = 4;
696
+ case 4:
697
+ amountWithoutFees = balance.minus(fee.value);
698
+ if (amountWithoutFees.isNegative()) {
699
+ amountWithoutFees = new bignumber_1.BigNumber(0);
700
+ }
701
+ return [2 /*return*/, (0, module_kit_1.newAmount)(amountWithoutFees, 'blockchain')];
702
+ }
703
+ });
704
+ });
705
+ };
706
+ EthereumBaseProtocolImpl.prototype.getTransactionFeeWithPublicKey = function (publicKey, details, configuration) {
707
+ return __awaiter(this, void 0, void 0, function () {
708
+ var address, estimatedGas, gasPrice, feeStepFactor, estimatedFee, lowFee, mediumFee, highFee;
709
+ return __generator(this, function (_a) {
710
+ switch (_a.label) {
711
+ case 0:
712
+ if (details.length !== 1) {
713
+ throw new errors_1.ConditionViolationError(coinlib_core_1.Domain.ETHEREUM, 'you cannot have 0 transaction details');
714
+ }
715
+ return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
716
+ case 1:
717
+ address = _a.sent();
718
+ return [4 /*yield*/, this.estimateGas(address, details[0].to, (0, module_kit_1.newAmount)(details[0].amount).blockchain(this.units))];
719
+ case 2:
720
+ estimatedGas = _a.sent();
721
+ return [4 /*yield*/, this.nodeClient.getGasPrice()];
722
+ case 3:
723
+ gasPrice = _a.sent();
724
+ feeStepFactor = new bignumber_1.BigNumber(0.5);
725
+ estimatedFee = estimatedGas.times(gasPrice);
726
+ lowFee = estimatedFee.minus(estimatedFee.times(feeStepFactor).integerValue(bignumber_1.BigNumber.ROUND_FLOOR));
727
+ mediumFee = estimatedFee;
728
+ highFee = mediumFee.plus(mediumFee.times(feeStepFactor).integerValue(bignumber_1.BigNumber.ROUND_FLOOR));
729
+ return [2 /*return*/, {
730
+ low: (0, module_kit_1.newAmount)(lowFee, 'blockchain'),
731
+ medium: (0, module_kit_1.newAmount)(mediumFee, 'blockchain'),
732
+ high: (0, module_kit_1.newAmount)(highFee, 'blockchain')
733
+ }];
734
+ }
735
+ });
736
+ });
737
+ };
738
+ EthereumBaseProtocolImpl.prototype.prepareTransactionWithPublicKey = function (publicKey, details, configuration) {
739
+ var _a;
740
+ return __awaiter(this, void 0, void 0, function () {
741
+ var fee, estimatedFee, wrappedFee, wrappedAmount, address, hexAmount, balance, availableBalance, gasLimit, gasPrice, txCount, transaction;
742
+ return __generator(this, function (_b) {
743
+ switch (_b.label) {
744
+ case 0:
745
+ if (details.length !== 1) {
746
+ throw new errors_1.ConditionViolationError(coinlib_core_1.Domain.ETHEREUM, 'you cannot have 0 transaction details');
747
+ }
748
+ if (!((configuration === null || configuration === void 0 ? void 0 : configuration.fee) !== undefined)) return [3 /*break*/, 1];
749
+ fee = configuration.fee;
750
+ return [3 /*break*/, 3];
751
+ case 1: return [4 /*yield*/, this.getTransactionFeeWithPublicKey(publicKey, details)];
752
+ case 2:
753
+ estimatedFee = _b.sent();
754
+ fee = estimatedFee.medium;
755
+ _b.label = 3;
756
+ case 3:
757
+ wrappedFee = new bignumber_1.BigNumber((0, module_kit_1.newAmount)(fee).blockchain(this.feeUnits).value);
758
+ wrappedAmount = new bignumber_1.BigNumber((0, module_kit_1.newAmount)(details[0].amount).blockchain(this.units).value);
759
+ return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
760
+ case 4:
761
+ address = _b.sent();
762
+ hexAmount = EthereumUtils_1.EthereumUtils.toHex(wrappedAmount.toFixed());
763
+ return [4 /*yield*/, this.getBalanceOfPublicKey(publicKey)];
764
+ case 5:
765
+ balance = _b.sent();
766
+ availableBalance = (0, module_kit_1.newAmount)((_a = balance.transferable) !== null && _a !== void 0 ? _a : balance.total).blockchain(this.units);
767
+ return [4 /*yield*/, this.estimateGas(address, details[0].to, hexAmount)];
768
+ case 6:
769
+ gasLimit = _b.sent();
770
+ gasPrice = wrappedFee.div(gasLimit).integerValue(bignumber_1.BigNumber.ROUND_CEIL);
771
+ if (!new bignumber_1.BigNumber(availableBalance.value).gte(wrappedAmount.plus(wrappedFee))) return [3 /*break*/, 8];
772
+ return [4 /*yield*/, this.nodeClient.fetchTransactionCount(address)];
773
+ case 7:
774
+ txCount = _b.sent();
775
+ transaction = (0, module_kit_1.newUnsignedTransaction)({
776
+ ethereumType: 'raw',
777
+ nonce: EthereumUtils_1.EthereumUtils.toHex(txCount),
778
+ gasLimit: EthereumUtils_1.EthereumUtils.toHex(gasLimit.toFixed()),
779
+ gasPrice: EthereumUtils_1.EthereumUtils.toHex(gasPrice.toFixed()),
780
+ to: details[0].to,
781
+ value: hexAmount,
782
+ chainId: this.options.network.chainId,
783
+ data: '0x'
784
+ });
785
+ return [2 /*return*/, transaction];
786
+ case 8: throw new errors_1.BalanceError(coinlib_core_1.Domain.ETHEREUM, 'not enough balance');
787
+ }
788
+ });
789
+ });
790
+ };
791
+ EthereumBaseProtocolImpl.prototype.getWalletConnectChain = function () {
792
+ return __awaiter(this, void 0, void 0, function () {
793
+ return __generator(this, function (_a) {
794
+ return [2 /*return*/, "".concat(WALLET_CONNECT_NAMESPACE, ":").concat(this.options.network.chainId)];
795
+ });
796
+ });
797
+ };
798
+ EthereumBaseProtocolImpl.prototype.prepareWalletConnectTransactionWithPublicKey = function (publicKey, request) {
799
+ var _a, _b, _c;
800
+ return __awaiter(this, void 0, void 0, function () {
801
+ var gasPricePromise, noncePromise, _d, gasPrice, nonce;
802
+ var _this = this;
803
+ return __generator(this, function (_e) {
804
+ switch (_e.label) {
805
+ case 0:
806
+ gasPricePromise = request.gasPrice
807
+ ? Promise.resolve(request.gasPrice)
808
+ : this.nodeClient.getGasPrice().then(function (gasPrice) { return "0x".concat(gasPrice.toString(16)); });
809
+ noncePromise = request.nonce
810
+ ? Promise.resolve(request.nonce)
811
+ : this.getAddressFromPublicKey(publicKey)
812
+ .then(function (address) { return _this.nodeClient.fetchTransactionCount(address); })
813
+ .then(function (transactionCount) { return "0x".concat(new bignumber_1.BigNumber(transactionCount).toString(16)); });
814
+ return [4 /*yield*/, Promise.all([gasPricePromise, noncePromise])];
815
+ case 1:
816
+ _d = _e.sent(), gasPrice = _d[0], nonce = _d[1];
817
+ return [2 /*return*/, (0, module_kit_1.newUnsignedTransaction)({
818
+ ethereumType: 'raw',
819
+ nonce: nonce,
820
+ gasLimit: "0x".concat((300000).toString(16)),
821
+ gasPrice: gasPrice,
822
+ to: (_a = request.to) !== null && _a !== void 0 ? _a : '',
823
+ value: (_b = request.value) !== null && _b !== void 0 ? _b : '0x00',
824
+ chainId: this.options.network.chainId,
825
+ data: (_c = request.data) !== null && _c !== void 0 ? _c : '0x'
826
+ })];
827
+ }
828
+ });
829
+ });
830
+ };
831
+ EthereumBaseProtocolImpl.prototype.broadcastTransaction = function (transaction) {
832
+ return __awaiter(this, void 0, void 0, function () {
833
+ return __generator(this, function (_a) {
834
+ return [2 /*return*/, this.nodeClient.sendSignedTransaction("0x".concat(transaction.serialized.replace(/^0x/, '')))];
835
+ });
836
+ });
837
+ };
522
838
  // Custom
523
- nonExtendedPublicKey(publicKey) {
839
+ EthereumBaseProtocolImpl.prototype.nonExtendedPublicKey = function (publicKey) {
524
840
  return publicKey.type === 'pub' ? publicKey : this.getPublicKeyFromExtendedPublicKey(publicKey);
525
- }
526
- nonExtendedSecretKey(secretKey) {
841
+ };
842
+ EthereumBaseProtocolImpl.prototype.nonExtendedSecretKey = function (secretKey) {
527
843
  return secretKey.type === 'priv' ? secretKey : this.getSecretKeyFromExtendedSecretKey(secretKey);
528
- }
529
- getPublicKeyFromExtendedPublicKey(extendedPublicKey, visibilityIndex = 0, addressIndex = 0) {
530
- const encodedExtendedPublicKey = (0, key_1.convertExtendedPublicKey)(extendedPublicKey, 'encoded');
531
- const derivedNode = this.deriveNode(encodedExtendedPublicKey.value, visibilityIndex, addressIndex);
844
+ };
845
+ EthereumBaseProtocolImpl.prototype.getPublicKeyFromExtendedPublicKey = function (extendedPublicKey, visibilityIndex, addressIndex) {
846
+ if (visibilityIndex === void 0) { visibilityIndex = 0; }
847
+ if (addressIndex === void 0) { addressIndex = 0; }
848
+ var encodedExtendedPublicKey = (0, key_1.convertExtendedPublicKey)(extendedPublicKey, 'encoded');
849
+ var derivedNode = this.deriveNode(encodedExtendedPublicKey.value, visibilityIndex, addressIndex);
532
850
  return (0, module_kit_1.newPublicKey)(derivedNode.neutered().keyPair.getPublicKeyBuffer().toString('hex'), 'hex');
533
- }
534
- getSecretKeyFromExtendedSecretKey(extendedSecretKey, visibilityIndex = 0, addressIndex = 0) {
535
- const encodedExtendedSecretKey = (0, key_1.convertExtendedSecretKey)(extendedSecretKey, 'encoded');
536
- const derivedNode = this.deriveNode(encodedExtendedSecretKey.value, visibilityIndex, addressIndex);
851
+ };
852
+ EthereumBaseProtocolImpl.prototype.getSecretKeyFromExtendedSecretKey = function (extendedSecretKey, visibilityIndex, addressIndex) {
853
+ if (visibilityIndex === void 0) { visibilityIndex = 0; }
854
+ if (addressIndex === void 0) { addressIndex = 0; }
855
+ var encodedExtendedSecretKey = (0, key_1.convertExtendedSecretKey)(extendedSecretKey, 'encoded');
856
+ var derivedNode = this.deriveNode(encodedExtendedSecretKey.value, visibilityIndex, addressIndex);
537
857
  return (0, module_kit_1.newSecretKey)(derivedNode.keyPair.getPrivateKeyBuffer().toString('hex'), 'hex');
538
- }
539
- deriveNode(base58, visibilityIndex, addressIndex) {
540
- return [visibilityIndex, addressIndex].reduce((node, index) => node.derive(index), this.bitcoinJS.lib.HDNode.fromBase58(base58, this.bitcoinJS.config.network));
541
- }
542
- async estimateGas(fromAddress, toAddress, amount, data) {
543
- let hexAmount;
544
- if (typeof amount === 'string' && (0, hex_1.isHex)(amount)) {
545
- hexAmount = amount;
546
- }
547
- else {
548
- const blockchainAmount = (0, module_kit_1.isAmount)(amount) ? (0, module_kit_1.newAmount)(amount).blockchain(this.units) : (0, module_kit_1.newAmount)(amount, 'blockchain');
549
- hexAmount = EthereumUtils_1.EthereumUtils.toHex(blockchainAmount.value);
550
- }
551
- return this.nodeClient.estimateTransactionGas(fromAddress, toAddress, hexAmount, data, EthereumUtils_1.EthereumUtils.toHex(MAX_GAS_ESTIMATE));
552
- }
553
- derivativeToBip32Node(derivative) {
554
- const bip32Node = (0, crypto_1.encodeDerivative)('bip32', derivative);
858
+ };
859
+ EthereumBaseProtocolImpl.prototype.deriveNode = function (base58, visibilityIndex, addressIndex) {
860
+ return [visibilityIndex, addressIndex].reduce(function (node, index) { return node.derive(index); }, this.bitcoinJS.lib.HDNode.fromBase58(base58, this.bitcoinJS.config.network));
861
+ };
862
+ EthereumBaseProtocolImpl.prototype.estimateGas = function (fromAddress, toAddress, amount, data) {
863
+ return __awaiter(this, void 0, void 0, function () {
864
+ var hexAmount, blockchainAmount;
865
+ return __generator(this, function (_a) {
866
+ if (typeof amount === 'string' && (0, hex_1.isHex)(amount)) {
867
+ hexAmount = amount;
868
+ }
869
+ else {
870
+ blockchainAmount = (0, module_kit_1.isAmount)(amount) ? (0, module_kit_1.newAmount)(amount).blockchain(this.units) : (0, module_kit_1.newAmount)(amount, 'blockchain');
871
+ hexAmount = EthereumUtils_1.EthereumUtils.toHex(blockchainAmount.value);
872
+ }
873
+ return [2 /*return*/, this.nodeClient.estimateTransactionGas(fromAddress, toAddress, hexAmount, data, EthereumUtils_1.EthereumUtils.toHex(MAX_GAS_ESTIMATE))];
874
+ });
875
+ });
876
+ };
877
+ EthereumBaseProtocolImpl.prototype.derivativeToBip32Node = function (derivative) {
878
+ var bip32Node = (0, crypto_1.encodeDerivative)('bip32', derivative);
555
879
  return this.bitcoinJS.lib.HDNode.fromBase58(bip32Node.secretKey, this.bitcoinJS.config.network);
556
- }
557
- }
880
+ };
881
+ return EthereumBaseProtocolImpl;
882
+ }());
558
883
  exports.EthereumBaseProtocolImpl = EthereumBaseProtocolImpl;
559
- class DefaultEthereumBaseProtocolImpl extends EthereumBaseProtocolImpl {
560
- constructor(nodeClient, infoClient, options) {
561
- super(nodeClient, infoClient, {
562
- ...options,
563
- identifier: coinlib_core_1.MainProtocolSymbols.ETH,
564
- name: 'Ethereum',
565
- units: exports.DEFAULT_ETHEREUM_UNITS_METADATA,
566
- mainUnit: 'ETH'
567
- });
884
+ var DefaultEthereumBaseProtocolImpl = /** @class */ (function (_super) {
885
+ __extends(DefaultEthereumBaseProtocolImpl, _super);
886
+ function DefaultEthereumBaseProtocolImpl(nodeClient, infoClient, options) {
887
+ return _super.call(this, nodeClient, infoClient, __assign(__assign({}, options), { identifier: coinlib_core_1.MainProtocolSymbols.ETH, name: 'Ethereum', units: exports.DEFAULT_ETHEREUM_UNITS_METADATA, mainUnit: 'ETH' })) || this;
568
888
  }
569
- }
889
+ return DefaultEthereumBaseProtocolImpl;
890
+ }(EthereumBaseProtocolImpl));
570
891
  exports.DefaultEthereumBaseProtocolImpl = DefaultEthereumBaseProtocolImpl;
571
892
  //# sourceMappingURL=EthereumBaseProtocol.js.map