@wallfree-dev/evmc 0.13.42

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