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

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,15 @@
1
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
+ };
2
13
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
14
  if (k2 === undefined) k2 = k;
4
15
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -15,56 +26,83 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
15
26
  }) : function(o, v) {
16
27
  o["default"] = v;
17
28
  });
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
- })();
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
+ };
35
72
  var __importDefault = (this && this.__importDefault) || function (mod) {
36
73
  return (mod && mod.__esModule) ? mod : { "default": mod };
37
74
  };
38
75
  Object.defineProperty(exports, "__esModule", { value: true });
39
76
  exports.BaseEthereumProtocol = void 0;
40
- const bignumber_1 = require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber");
77
+ var bignumber_1 = require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber");
41
78
  // @ts-ignore
42
- const bip39_2_5_0_1 = require("@airgap/coinlib-core/dependencies/src/bip39-2.5.0");
79
+ var bip39_2_5_0_1 = require("@airgap/coinlib-core/dependencies/src/bip39-2.5.0");
43
80
  // @ts-ignore
44
- const bitcoinJS = __importStar(require("@airgap/coinlib-core/dependencies/src/bitgo-utxo-lib-5d91049fd7a988382df81c8260e244ee56d57aac/src"));
45
- const errors_1 = require("@airgap/coinlib-core/errors");
46
- const coinlib_error_1 = require("@airgap/coinlib-core/errors/coinlib-error");
47
- const IAirGapTransaction_1 = require("@airgap/coinlib-core/interfaces/IAirGapTransaction");
48
- const ICoinSubProtocol_1 = require("@airgap/coinlib-core/protocols/ICoinSubProtocol");
49
- const ProtocolSymbols_1 = require("@airgap/coinlib-core/utils/ProtocolSymbols");
50
- const common_1 = __importDefault(require("@ethereumjs/common"));
81
+ var bitcoinJS = __importStar(require("@airgap/coinlib-core/dependencies/src/bitgo-utxo-lib-5d91049fd7a988382df81c8260e244ee56d57aac/src"));
82
+ var errors_1 = require("@airgap/coinlib-core/errors");
83
+ var coinlib_error_1 = require("@airgap/coinlib-core/errors/coinlib-error");
84
+ var IAirGapTransaction_1 = require("@airgap/coinlib-core/interfaces/IAirGapTransaction");
85
+ var ICoinSubProtocol_1 = require("@airgap/coinlib-core/protocols/ICoinSubProtocol");
86
+ var ProtocolSymbols_1 = require("@airgap/coinlib-core/utils/ProtocolSymbols");
87
+ var common_1 = __importDefault(require("@ethereumjs/common"));
51
88
  // TODO: ETH TX and ethereumjs-util-5.2.0 removed
52
- const tx_1 = require("@ethereumjs/tx");
53
- const EthereumAddress_1 = require("./EthereumAddress");
54
- const EthereumChainIDs_1 = require("./EthereumChainIDs");
55
- const EthereumCryptoClient_1 = require("./EthereumCryptoClient");
56
- const EthereumProtocolOptions_1 = require("./EthereumProtocolOptions");
57
- const utils_1 = require("./utils/utils");
58
- class BaseEthereumProtocol {
59
- constructor(options = new EthereumProtocolOptions_1.EthereumProtocolOptions()) {
89
+ var tx_1 = require("@ethereumjs/tx");
90
+ var EthereumAddress_1 = require("./EthereumAddress");
91
+ var EthereumChainIDs_1 = require("./EthereumChainIDs");
92
+ var EthereumCryptoClient_1 = require("./EthereumCryptoClient");
93
+ var EthereumProtocolOptions_1 = require("./EthereumProtocolOptions");
94
+ var utils_1 = require("./utils/utils");
95
+ var BaseEthereumProtocol = /** @class */ (function () {
96
+ function BaseEthereumProtocol(options) {
97
+ if (options === void 0) { options = new EthereumProtocolOptions_1.EthereumProtocolOptions(); }
60
98
  this.options = options;
61
99
  this.symbol = 'ETH';
62
100
  this.name = 'Ethereum';
63
101
  this.marketSymbol = 'eth';
64
102
  this.feeSymbol = 'eth';
65
103
  this.feeDefaults = {
66
- low: '0.00021', // 21000 Gas * 10 Gwei
67
- medium: '0.000315', // 21000 Gas * 15 Gwei
104
+ low: '0.00021',
105
+ medium: '0.000315',
68
106
  high: '0.00084' // 21000 Gas * 40 Gwei
69
107
  };
70
108
  this.decimals = 18;
@@ -86,449 +124,823 @@ class BaseEthereumProtocol {
86
124
  }
87
125
  ];
88
126
  this.supportsHD = false;
89
- this.standardDerivationPath = `m/44'/60'/0'/0/0`;
127
+ this.standardDerivationPath = "m/44'/60'/0'/0/0";
90
128
  this.addressIsCaseSensitive = false;
91
129
  this.addressValidationPattern = '^0x[a-fA-F0-9]{40}$';
92
130
  this.addressPlaceholder = '0xabc...';
93
131
  this.cryptoClient = new EthereumCryptoClient_1.EthereumCryptoClient(this);
94
132
  this.network = bitcoinJS.networks.bitcoin;
95
133
  }
96
- async getSymbol() {
97
- return this.symbol;
98
- }
99
- async getName() {
100
- return this.name;
101
- }
102
- async getMarketSymbol() {
103
- return this.marketSymbol;
104
- }
105
- async getAssetSymbol() {
106
- return undefined;
107
- }
108
- async getFeeSymbol() {
109
- return this.feeSymbol;
110
- }
111
- async getFeeDefaults() {
112
- return this.feeDefaults;
113
- }
114
- async getDecimals() {
115
- return this.decimals;
116
- }
117
- async getFeeDecimals() {
118
- return this.feeDecimals;
119
- }
120
- async getIdentifier() {
121
- return this.identifier;
122
- }
123
- async getUnits() {
124
- return this.units;
125
- }
126
- async getSupportsHD() {
127
- return this.supportsHD;
128
- }
129
- async getStandardDerivationPath() {
130
- return this.standardDerivationPath;
131
- }
132
- async getAddressIsCaseSensitive() {
133
- return this.addressIsCaseSensitive;
134
- }
135
- async getAddressValidationPattern() {
136
- return this.addressValidationPattern;
137
- }
138
- async getAddressPlaceholder() {
139
- return this.addressPlaceholder;
140
- }
141
- async getOptions() {
142
- return this.options;
143
- }
144
- async getBlockExplorerLinkForAddress(address) {
145
- return this.options.network.blockExplorer.getAddressLink(address);
146
- }
147
- async getBlockExplorerLinkForTxId(txId) {
148
- return this.options.network.blockExplorer.getTransactionLink(txId);
149
- }
150
- async getPublicKeyFromMnemonic(mnemonic, derivationPath, password) {
151
- const secret = (0, bip39_2_5_0_1.mnemonicToSeed)(mnemonic, password);
152
- return this.getPublicKeyFromHexSecret(secret, derivationPath);
153
- }
154
- async getPrivateKeyFromMnemonic(mnemonic, derivationPath, password) {
155
- const secret = (0, bip39_2_5_0_1.mnemonicToSeed)(mnemonic, password);
156
- return this.getPrivateKeyFromHexSecret(secret, derivationPath);
157
- }
158
- async getExtendedPrivateKeyFromMnemonic(mnemonic, derivationPath, password) {
159
- const secret = (0, bip39_2_5_0_1.mnemonicToSeed)(mnemonic, password);
160
- return this.getExtendedPrivateKeyFromHexSecret(secret, derivationPath);
161
- }
162
- async getExtendedPublicKeyFromMnemonic(mnemonic, derivationPath, password) {
163
- const secret = (0, bip39_2_5_0_1.mnemonicToSeed)(mnemonic, password);
164
- return this.getExtendedPublicKeyFromHexSecret(secret, derivationPath);
165
- }
166
- async getPublicKeyFromHexSecret(secret, derivationPath) {
167
- const ethereumNode = bitcoinJS.HDNode.fromSeedHex(secret, this.network);
168
- return ethereumNode.derivePath(derivationPath).neutered().getPublicKeyBuffer().toString('hex');
169
- }
170
- async getExtendedPublicKeyFromHexSecret(secret, derivationPath) {
171
- const ethereumNode = bitcoinJS.HDNode.fromSeedHex(secret, this.network);
172
- return ethereumNode.derivePath(derivationPath).neutered().toBase58();
173
- }
174
- async getPrivateKeyFromHexSecret(secret, derivationPath) {
175
- const ethereumNode = bitcoinJS.HDNode.fromSeedHex(secret, this.network);
176
- return ethereumNode.derivePath(derivationPath).keyPair.d.toBuffer(32).toString('hex');
177
- }
178
- async getExtendedPrivateKeyFromHexSecret(secret, derivationPath) {
179
- const ethereumNode = bitcoinJS.HDNode.fromSeedHex(secret, this.network);
180
- return ethereumNode.derivePath(derivationPath).toBase58();
181
- }
182
- async getAddressFromPublicKey(publicKey, cursor) {
183
- const address = EthereumAddress_1.EthereumAddress.from(publicKey);
184
- return {
185
- address: address.asString(),
186
- cursor: { hasNext: false }
187
- };
188
- }
189
- async getAddressesFromPublicKey(publicKey, cursor) {
190
- const address = await this.getAddressFromPublicKey(publicKey, cursor);
191
- return [address];
192
- }
193
- async getPublicKeyFromExtendedPublicKey(extendedPublicKey, visibilityDerivationIndex, addressDerivationIndex) {
194
- return bitcoinJS.HDNode.fromBase58(extendedPublicKey, this.network)
195
- .derive(visibilityDerivationIndex)
196
- .derive(addressDerivationIndex)
197
- .getPublicKeyBuffer()
198
- .toString('hex');
199
- }
200
- async getAddressFromExtendedPublicKey(extendedPublicKey, visibilityDerivationIndex, addressDerivationIndex) {
201
- return this.getAddressFromPublicKey(await this.getPublicKeyFromExtendedPublicKey(extendedPublicKey, visibilityDerivationIndex, addressDerivationIndex));
202
- }
203
- getAddressesFromExtendedPublicKey(extendedPublicKey, visibilityDerivationIndex, addressCount, offset) {
204
- const node = bitcoinJS.HDNode.fromBase58(extendedPublicKey, this.network);
205
- const generatorArray = [addressCount].map((x, i) => i + offset);
206
- return Promise.all(generatorArray.map((x) => this.getAddressFromPublicKey(node.derive(visibilityDerivationIndex).derive(x).getPublicKeyBuffer().toString('hex'))));
207
- }
208
- async getPrivateKeyFromExtendedPrivateKey(extendedPrivateKey, childDerivationPath) {
209
- const dp = childDerivationPath ?? '0/0'; // This is the default
210
- if (dp.startsWith('m')) {
211
- throw new Error('Received full derivation path, expected child derivation path');
212
- }
213
- if (dp.toLowerCase().includes('h') || dp.includes(`'`)) {
214
- throw new Error('Child derivation path cannot include hardened children');
215
- }
216
- return dp
217
- .split('/')
218
- .reduce((pv, cv) => pv.derive(Number(cv)), bitcoinJS.HDNode.fromBase58(extendedPrivateKey, this.network))
219
- .keyPair.d.toBuffer(32);
220
- }
221
- async signWithExtendedPrivateKey(extendedPrivateKey, untypedTransaction, childDerivationPath) {
222
- const privateKey = await this.getPrivateKeyFromExtendedPrivateKey(extendedPrivateKey, childDerivationPath);
223
- if (untypedTransaction.serialized &&
224
- untypedTransaction.derivationPath) {
225
- const transaction = untypedTransaction;
226
- let tx = tx_1.TransactionFactory.fromSerializedData(Buffer.from(transaction.serialized, 'hex'));
227
- tx = tx.sign(privateKey);
228
- return tx.serialize().toString('hex');
229
- }
230
- else {
231
- return this.signWithPrivateKey(privateKey.toString('hex'), untypedTransaction);
232
- }
233
- }
234
- async signWithPrivateKey(privateKey, transaction) {
235
- if (!transaction.value.startsWith('0x')) {
236
- transaction.value = utils_1.EthereumUtils.toHex(parseInt(transaction.value, 10));
237
- }
238
- let common;
239
- try {
240
- common = new common_1.default({ chain: transaction.chainId });
241
- }
242
- catch {
243
- common = common_1.default.custom({ chainId: transaction.chainId });
244
- }
245
- let tx = tx_1.TransactionFactory.fromTxData(transaction, { common });
246
- tx = tx.sign(Buffer.from(privateKey, 'hex'));
247
- return tx.serialize().toString('hex');
248
- }
249
- async getTransactionDetails(unsignedTx) {
250
- if (unsignedTx.transaction.serialized) {
251
- const typedTransaction = unsignedTx.transaction;
252
- const transaction = tx_1.TransactionFactory.fromSerializedData(Buffer.from(typedTransaction.serialized, 'hex'));
253
- const dps = typedTransaction.derivationPath.split('/');
254
- const ownAddress = unsignedTx.publicKey.startsWith('x') // xPub
255
- ? await this.getAddressFromExtendedPublicKey(unsignedTx.publicKey, Number(dps[dps.length - 2]), Number(dps[dps.length - 1]))
256
- : await this.getAddressFromPublicKey(unsignedTx.publicKey);
257
- const airGapTransaction = {
258
- from: [ownAddress.address],
259
- to: [transaction.to?.toString() ?? ''],
260
- amount: new bignumber_1.BigNumber(transaction.value.toString(10)).toString(10),
261
- fee: new bignumber_1.BigNumber(transaction.gasLimit.toString(10))
262
- .multipliedBy(new bignumber_1.BigNumber(transaction.maxFeePerGas.toString(10)))
263
- .toString(10),
264
- protocolIdentifier: this.identifier,
265
- network: this.options.network,
266
- isInbound: false,
267
- data: transaction.data.toString('hex'),
268
- transactionDetails: unsignedTx
269
- };
270
- return [
271
- {
272
- ...airGapTransaction,
273
- ...(transaction.chainId.toNumber() !== 1
274
- ? {
275
- warnings: [
134
+ BaseEthereumProtocol.prototype.getSymbol = function () {
135
+ return __awaiter(this, void 0, void 0, function () {
136
+ return __generator(this, function (_a) {
137
+ return [2 /*return*/, this.symbol];
138
+ });
139
+ });
140
+ };
141
+ BaseEthereumProtocol.prototype.getName = function () {
142
+ return __awaiter(this, void 0, void 0, function () {
143
+ return __generator(this, function (_a) {
144
+ return [2 /*return*/, this.name];
145
+ });
146
+ });
147
+ };
148
+ BaseEthereumProtocol.prototype.getMarketSymbol = function () {
149
+ return __awaiter(this, void 0, void 0, function () {
150
+ return __generator(this, function (_a) {
151
+ return [2 /*return*/, this.marketSymbol];
152
+ });
153
+ });
154
+ };
155
+ BaseEthereumProtocol.prototype.getAssetSymbol = function () {
156
+ return __awaiter(this, void 0, void 0, function () {
157
+ return __generator(this, function (_a) {
158
+ return [2 /*return*/, undefined];
159
+ });
160
+ });
161
+ };
162
+ BaseEthereumProtocol.prototype.getFeeSymbol = function () {
163
+ return __awaiter(this, void 0, void 0, function () {
164
+ return __generator(this, function (_a) {
165
+ return [2 /*return*/, this.feeSymbol];
166
+ });
167
+ });
168
+ };
169
+ BaseEthereumProtocol.prototype.getFeeDefaults = function () {
170
+ return __awaiter(this, void 0, void 0, function () {
171
+ return __generator(this, function (_a) {
172
+ return [2 /*return*/, this.feeDefaults];
173
+ });
174
+ });
175
+ };
176
+ BaseEthereumProtocol.prototype.getDecimals = function () {
177
+ return __awaiter(this, void 0, void 0, function () {
178
+ return __generator(this, function (_a) {
179
+ return [2 /*return*/, this.decimals];
180
+ });
181
+ });
182
+ };
183
+ BaseEthereumProtocol.prototype.getFeeDecimals = function () {
184
+ return __awaiter(this, void 0, void 0, function () {
185
+ return __generator(this, function (_a) {
186
+ return [2 /*return*/, this.feeDecimals];
187
+ });
188
+ });
189
+ };
190
+ BaseEthereumProtocol.prototype.getIdentifier = function () {
191
+ return __awaiter(this, void 0, void 0, function () {
192
+ return __generator(this, function (_a) {
193
+ return [2 /*return*/, this.identifier];
194
+ });
195
+ });
196
+ };
197
+ BaseEthereumProtocol.prototype.getUnits = function () {
198
+ return __awaiter(this, void 0, void 0, function () {
199
+ return __generator(this, function (_a) {
200
+ return [2 /*return*/, this.units];
201
+ });
202
+ });
203
+ };
204
+ BaseEthereumProtocol.prototype.getSupportsHD = function () {
205
+ return __awaiter(this, void 0, void 0, function () {
206
+ return __generator(this, function (_a) {
207
+ return [2 /*return*/, this.supportsHD];
208
+ });
209
+ });
210
+ };
211
+ BaseEthereumProtocol.prototype.getStandardDerivationPath = function () {
212
+ return __awaiter(this, void 0, void 0, function () {
213
+ return __generator(this, function (_a) {
214
+ return [2 /*return*/, this.standardDerivationPath];
215
+ });
216
+ });
217
+ };
218
+ BaseEthereumProtocol.prototype.getAddressIsCaseSensitive = function () {
219
+ return __awaiter(this, void 0, void 0, function () {
220
+ return __generator(this, function (_a) {
221
+ return [2 /*return*/, this.addressIsCaseSensitive];
222
+ });
223
+ });
224
+ };
225
+ BaseEthereumProtocol.prototype.getAddressValidationPattern = function () {
226
+ return __awaiter(this, void 0, void 0, function () {
227
+ return __generator(this, function (_a) {
228
+ return [2 /*return*/, this.addressValidationPattern];
229
+ });
230
+ });
231
+ };
232
+ BaseEthereumProtocol.prototype.getAddressPlaceholder = function () {
233
+ return __awaiter(this, void 0, void 0, function () {
234
+ return __generator(this, function (_a) {
235
+ return [2 /*return*/, this.addressPlaceholder];
236
+ });
237
+ });
238
+ };
239
+ BaseEthereumProtocol.prototype.getOptions = function () {
240
+ return __awaiter(this, void 0, void 0, function () {
241
+ return __generator(this, function (_a) {
242
+ return [2 /*return*/, this.options];
243
+ });
244
+ });
245
+ };
246
+ BaseEthereumProtocol.prototype.getBlockExplorerLinkForAddress = function (address) {
247
+ return __awaiter(this, void 0, void 0, function () {
248
+ return __generator(this, function (_a) {
249
+ return [2 /*return*/, this.options.network.blockExplorer.getAddressLink(address)];
250
+ });
251
+ });
252
+ };
253
+ BaseEthereumProtocol.prototype.getBlockExplorerLinkForTxId = function (txId) {
254
+ return __awaiter(this, void 0, void 0, function () {
255
+ return __generator(this, function (_a) {
256
+ return [2 /*return*/, this.options.network.blockExplorer.getTransactionLink(txId)];
257
+ });
258
+ });
259
+ };
260
+ BaseEthereumProtocol.prototype.getPublicKeyFromMnemonic = function (mnemonic, derivationPath, password) {
261
+ return __awaiter(this, void 0, void 0, function () {
262
+ var secret;
263
+ return __generator(this, function (_a) {
264
+ secret = (0, bip39_2_5_0_1.mnemonicToSeed)(mnemonic, password);
265
+ return [2 /*return*/, this.getPublicKeyFromHexSecret(secret, derivationPath)];
266
+ });
267
+ });
268
+ };
269
+ BaseEthereumProtocol.prototype.getPrivateKeyFromMnemonic = function (mnemonic, derivationPath, password) {
270
+ return __awaiter(this, void 0, void 0, function () {
271
+ var secret;
272
+ return __generator(this, function (_a) {
273
+ secret = (0, bip39_2_5_0_1.mnemonicToSeed)(mnemonic, password);
274
+ return [2 /*return*/, this.getPrivateKeyFromHexSecret(secret, derivationPath)];
275
+ });
276
+ });
277
+ };
278
+ BaseEthereumProtocol.prototype.getExtendedPrivateKeyFromMnemonic = function (mnemonic, derivationPath, password) {
279
+ return __awaiter(this, void 0, void 0, function () {
280
+ var secret;
281
+ return __generator(this, function (_a) {
282
+ secret = (0, bip39_2_5_0_1.mnemonicToSeed)(mnemonic, password);
283
+ return [2 /*return*/, this.getExtendedPrivateKeyFromHexSecret(secret, derivationPath)];
284
+ });
285
+ });
286
+ };
287
+ BaseEthereumProtocol.prototype.getExtendedPublicKeyFromMnemonic = function (mnemonic, derivationPath, password) {
288
+ return __awaiter(this, void 0, void 0, function () {
289
+ var secret;
290
+ return __generator(this, function (_a) {
291
+ secret = (0, bip39_2_5_0_1.mnemonicToSeed)(mnemonic, password);
292
+ return [2 /*return*/, this.getExtendedPublicKeyFromHexSecret(secret, derivationPath)];
293
+ });
294
+ });
295
+ };
296
+ BaseEthereumProtocol.prototype.getPublicKeyFromHexSecret = function (secret, derivationPath) {
297
+ return __awaiter(this, void 0, void 0, function () {
298
+ var ethereumNode;
299
+ return __generator(this, function (_a) {
300
+ ethereumNode = bitcoinJS.HDNode.fromSeedHex(secret, this.network);
301
+ return [2 /*return*/, ethereumNode.derivePath(derivationPath).neutered().getPublicKeyBuffer().toString('hex')];
302
+ });
303
+ });
304
+ };
305
+ BaseEthereumProtocol.prototype.getExtendedPublicKeyFromHexSecret = function (secret, derivationPath) {
306
+ return __awaiter(this, void 0, void 0, function () {
307
+ var ethereumNode;
308
+ return __generator(this, function (_a) {
309
+ ethereumNode = bitcoinJS.HDNode.fromSeedHex(secret, this.network);
310
+ return [2 /*return*/, ethereumNode.derivePath(derivationPath).neutered().toBase58()];
311
+ });
312
+ });
313
+ };
314
+ BaseEthereumProtocol.prototype.getPrivateKeyFromHexSecret = function (secret, derivationPath) {
315
+ return __awaiter(this, void 0, void 0, function () {
316
+ var ethereumNode;
317
+ return __generator(this, function (_a) {
318
+ ethereumNode = bitcoinJS.HDNode.fromSeedHex(secret, this.network);
319
+ return [2 /*return*/, ethereumNode.derivePath(derivationPath).keyPair.d.toBuffer(32).toString('hex')];
320
+ });
321
+ });
322
+ };
323
+ BaseEthereumProtocol.prototype.getExtendedPrivateKeyFromHexSecret = function (secret, derivationPath) {
324
+ return __awaiter(this, void 0, void 0, function () {
325
+ var ethereumNode;
326
+ return __generator(this, function (_a) {
327
+ ethereumNode = bitcoinJS.HDNode.fromSeedHex(secret, this.network);
328
+ return [2 /*return*/, ethereumNode.derivePath(derivationPath).toBase58()];
329
+ });
330
+ });
331
+ };
332
+ BaseEthereumProtocol.prototype.getAddressFromPublicKey = function (publicKey, cursor) {
333
+ return __awaiter(this, void 0, void 0, function () {
334
+ var address;
335
+ return __generator(this, function (_a) {
336
+ address = EthereumAddress_1.EthereumAddress.from(publicKey);
337
+ return [2 /*return*/, {
338
+ address: address.asString(),
339
+ cursor: { hasNext: false }
340
+ }];
341
+ });
342
+ });
343
+ };
344
+ BaseEthereumProtocol.prototype.getAddressesFromPublicKey = function (publicKey, cursor) {
345
+ return __awaiter(this, void 0, void 0, function () {
346
+ var address;
347
+ return __generator(this, function (_a) {
348
+ switch (_a.label) {
349
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey, cursor)];
350
+ case 1:
351
+ address = _a.sent();
352
+ return [2 /*return*/, [address]];
353
+ }
354
+ });
355
+ });
356
+ };
357
+ BaseEthereumProtocol.prototype.getPublicKeyFromExtendedPublicKey = function (extendedPublicKey, visibilityDerivationIndex, addressDerivationIndex) {
358
+ return __awaiter(this, void 0, void 0, function () {
359
+ return __generator(this, function (_a) {
360
+ return [2 /*return*/, bitcoinJS.HDNode.fromBase58(extendedPublicKey, this.network)
361
+ .derive(visibilityDerivationIndex)
362
+ .derive(addressDerivationIndex)
363
+ .getPublicKeyBuffer()
364
+ .toString('hex')];
365
+ });
366
+ });
367
+ };
368
+ BaseEthereumProtocol.prototype.getAddressFromExtendedPublicKey = function (extendedPublicKey, visibilityDerivationIndex, addressDerivationIndex) {
369
+ return __awaiter(this, void 0, void 0, function () {
370
+ var _a;
371
+ return __generator(this, function (_b) {
372
+ switch (_b.label) {
373
+ case 0:
374
+ _a = this.getAddressFromPublicKey;
375
+ return [4 /*yield*/, this.getPublicKeyFromExtendedPublicKey(extendedPublicKey, visibilityDerivationIndex, addressDerivationIndex)];
376
+ case 1: return [2 /*return*/, _a.apply(this, [_b.sent()])];
377
+ }
378
+ });
379
+ });
380
+ };
381
+ BaseEthereumProtocol.prototype.getAddressesFromExtendedPublicKey = function (extendedPublicKey, visibilityDerivationIndex, addressCount, offset) {
382
+ var _this = this;
383
+ var node = bitcoinJS.HDNode.fromBase58(extendedPublicKey, this.network);
384
+ var generatorArray = [addressCount].map(function (x, i) { return i + offset; });
385
+ return Promise.all(generatorArray.map(function (x) {
386
+ return _this.getAddressFromPublicKey(node.derive(visibilityDerivationIndex).derive(x).getPublicKeyBuffer().toString('hex'));
387
+ }));
388
+ };
389
+ BaseEthereumProtocol.prototype.getPrivateKeyFromExtendedPrivateKey = function (extendedPrivateKey, childDerivationPath) {
390
+ return __awaiter(this, void 0, void 0, function () {
391
+ var dp;
392
+ return __generator(this, function (_a) {
393
+ dp = childDerivationPath !== null && childDerivationPath !== void 0 ? childDerivationPath : '0/0' // This is the default
394
+ ;
395
+ if (dp.startsWith('m')) {
396
+ throw new Error('Received full derivation path, expected child derivation path');
397
+ }
398
+ if (dp.toLowerCase().includes('h') || dp.includes("'")) {
399
+ throw new Error('Child derivation path cannot include hardened children');
400
+ }
401
+ return [2 /*return*/, dp
402
+ .split('/')
403
+ .reduce(function (pv, cv) { return pv.derive(Number(cv)); }, bitcoinJS.HDNode.fromBase58(extendedPrivateKey, this.network))
404
+ .keyPair.d.toBuffer(32)];
405
+ });
406
+ });
407
+ };
408
+ BaseEthereumProtocol.prototype.signWithExtendedPrivateKey = function (extendedPrivateKey, untypedTransaction, childDerivationPath) {
409
+ return __awaiter(this, void 0, void 0, function () {
410
+ var privateKey, transaction, tx;
411
+ return __generator(this, function (_a) {
412
+ switch (_a.label) {
413
+ case 0: return [4 /*yield*/, this.getPrivateKeyFromExtendedPrivateKey(extendedPrivateKey, childDerivationPath)];
414
+ case 1:
415
+ privateKey = _a.sent();
416
+ if (untypedTransaction.serialized &&
417
+ untypedTransaction.derivationPath) {
418
+ transaction = untypedTransaction;
419
+ tx = tx_1.TransactionFactory.fromSerializedData(Buffer.from(transaction.serialized, 'hex'));
420
+ tx = tx.sign(privateKey);
421
+ return [2 /*return*/, tx.serialize().toString('hex')];
422
+ }
423
+ else {
424
+ return [2 /*return*/, this.signWithPrivateKey(privateKey.toString('hex'), untypedTransaction)];
425
+ }
426
+ return [2 /*return*/];
427
+ }
428
+ });
429
+ });
430
+ };
431
+ BaseEthereumProtocol.prototype.signWithPrivateKey = function (privateKey, transaction) {
432
+ return __awaiter(this, void 0, void 0, function () {
433
+ var common, tx;
434
+ return __generator(this, function (_a) {
435
+ if (!transaction.value.startsWith('0x')) {
436
+ transaction.value = utils_1.EthereumUtils.toHex(parseInt(transaction.value, 10));
437
+ }
438
+ try {
439
+ common = new common_1.default({ chain: transaction.chainId });
440
+ }
441
+ catch (_b) {
442
+ common = common_1.default.custom({ chainId: transaction.chainId });
443
+ }
444
+ tx = tx_1.TransactionFactory.fromTxData(transaction, { common: common });
445
+ tx = tx.sign(Buffer.from(privateKey, 'hex'));
446
+ return [2 /*return*/, tx.serialize().toString('hex')];
447
+ });
448
+ });
449
+ };
450
+ BaseEthereumProtocol.prototype.getTransactionDetails = function (unsignedTx) {
451
+ var _a, _b, _c;
452
+ return __awaiter(this, void 0, void 0, function () {
453
+ var typedTransaction, transaction, dps, ownAddress, _d, airGapTransaction, transaction, ownAddress, _e;
454
+ return __generator(this, function (_f) {
455
+ switch (_f.label) {
456
+ case 0:
457
+ if (!unsignedTx.transaction.serialized) return [3 /*break*/, 5];
458
+ typedTransaction = unsignedTx.transaction;
459
+ transaction = tx_1.TransactionFactory.fromSerializedData(Buffer.from(typedTransaction.serialized, 'hex'));
460
+ dps = typedTransaction.derivationPath.split('/');
461
+ if (!unsignedTx.publicKey.startsWith('x') // xPub
462
+ ) return [3 /*break*/, 2]; // xPub
463
+ return [4 /*yield*/, this.getAddressFromExtendedPublicKey(unsignedTx.publicKey, Number(dps[dps.length - 2]), Number(dps[dps.length - 1]))];
464
+ case 1:
465
+ _d = _f.sent();
466
+ return [3 /*break*/, 4];
467
+ case 2: return [4 /*yield*/, this.getAddressFromPublicKey(unsignedTx.publicKey)];
468
+ case 3:
469
+ _d = _f.sent();
470
+ _f.label = 4;
471
+ case 4:
472
+ ownAddress = _d;
473
+ airGapTransaction = {
474
+ from: [ownAddress.address],
475
+ to: [(_b = (_a = transaction.to) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : ''],
476
+ amount: new bignumber_1.BigNumber(transaction.value.toString(10)).toString(10),
477
+ fee: new bignumber_1.BigNumber(transaction.gasLimit.toString(10))
478
+ .multipliedBy(new bignumber_1.BigNumber(transaction.maxFeePerGas.toString(10)))
479
+ .toString(10),
480
+ protocolIdentifier: this.identifier,
481
+ network: this.options.network,
482
+ isInbound: false,
483
+ data: transaction.data.toString('hex'),
484
+ transactionDetails: unsignedTx
485
+ };
486
+ return [2 /*return*/, [
487
+ __assign(__assign({}, airGapTransaction), (transaction.chainId.toNumber() !== 1
488
+ ? {
489
+ warnings: [
490
+ {
491
+ type: IAirGapTransaction_1.AirGapTransactionWarningType.WARNING,
492
+ title: 'Chain ID',
493
+ description: "Please note that this is not an Ethereum Mainnet transaction, it is from ".concat((_c = EthereumChainIDs_1.EthereumChainIDs.get(transaction.chainId.toNumber())) !== null && _c !== void 0 ? _c : "Chain ID ".concat(transaction.chainId.toNumber()))
494
+ }
495
+ ]
496
+ }
497
+ : {}))
498
+ ]];
499
+ case 5:
500
+ transaction = unsignedTx.transaction;
501
+ if (!unsignedTx.publicKey.startsWith('x') // xPub
502
+ ) return [3 /*break*/, 7]; // xPub
503
+ return [4 /*yield*/, this.getAddressFromExtendedPublicKey(unsignedTx.publicKey, 0, 0)];
504
+ case 6:
505
+ _e = _f.sent();
506
+ return [3 /*break*/, 9];
507
+ case 7: return [4 /*yield*/, this.getAddressFromPublicKey(unsignedTx.publicKey)];
508
+ case 8:
509
+ _e = _f.sent();
510
+ _f.label = 9;
511
+ case 9:
512
+ ownAddress = _e;
513
+ return [2 /*return*/, [
276
514
  {
277
- type: IAirGapTransaction_1.AirGapTransactionWarningType.WARNING,
278
- title: 'Chain ID',
279
- description: `Please note that this is not an Ethereum Mainnet transaction, it is from ${EthereumChainIDs_1.EthereumChainIDs.get(transaction.chainId.toNumber()) ?? `Chain ID ${transaction.chainId.toNumber()}`}`
515
+ from: [ownAddress.address],
516
+ to: [transaction.to],
517
+ amount: new bignumber_1.BigNumber(transaction.value).toString(10),
518
+ fee: new bignumber_1.BigNumber(transaction.gasLimit).multipliedBy(new bignumber_1.BigNumber(transaction.gasPrice)).toString(10),
519
+ protocolIdentifier: this.identifier,
520
+ network: this.options.network,
521
+ isInbound: false,
522
+ data: transaction.data,
523
+ transactionDetails: unsignedTx
280
524
  }
281
- ]
525
+ ]];
526
+ }
527
+ });
528
+ });
529
+ };
530
+ BaseEthereumProtocol.prototype.getTransactionDetailsFromSigned = function (transaction) {
531
+ var _a, _b;
532
+ return __awaiter(this, void 0, void 0, function () {
533
+ var ethTx, tx, hexValue, hexGasPrice, hexGasLimit, hexNonce, chainId, to, feeTx;
534
+ return __generator(this, function (_c) {
535
+ ethTx = tx_1.TransactionFactory.fromSerializedData(Buffer.from(transaction.transaction, 'hex'));
536
+ if (ethTx.type === 0) {
537
+ tx = ethTx;
538
+ hexValue = tx.value.toString('hex') || '0x0';
539
+ hexGasPrice = tx.gasPrice.toString('hex') || '0x0';
540
+ hexGasLimit = tx.gasLimit.toString('hex') || '0x0';
541
+ hexNonce = tx.nonce.toString('hex') || '0x0';
542
+ chainId = tx.common.chainIdBN().toString(10);
543
+ to = tx.to;
544
+ if (!to) {
545
+ throw new Error('No "TO" address');
546
+ }
547
+ return [2 /*return*/, [
548
+ {
549
+ from: [tx.getSenderAddress().toString()],
550
+ to: [to.toString()],
551
+ amount: new bignumber_1.BigNumber(parseInt(hexValue, 16)).toString(10),
552
+ fee: new bignumber_1.BigNumber(parseInt(hexGasLimit, 16)).multipliedBy(new bignumber_1.BigNumber(parseInt(hexGasPrice, 16))).toString(10),
553
+ protocolIdentifier: this.identifier,
554
+ network: this.options.network,
555
+ isInbound: tx.toCreationAddress(),
556
+ hash: "0x".concat(tx.hash().toString('hex')),
557
+ data: "0x".concat(tx.data.toString('hex')),
558
+ extra: {
559
+ chainId: chainId,
560
+ nonce: parseInt(hexNonce, 16)
561
+ },
562
+ transactionDetails: { raw: transaction.transaction }
563
+ }
564
+ ]];
565
+ }
566
+ try {
567
+ feeTx = ethTx;
568
+ return [2 /*return*/, [
569
+ {
570
+ from: [feeTx.getSenderAddress().toString()],
571
+ to: [(_b = (_a = feeTx.to) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : ''],
572
+ amount: new bignumber_1.BigNumber(feeTx.value.toString(10)).toString(10),
573
+ fee: new bignumber_1.BigNumber(feeTx.gasLimit.toString(10)).multipliedBy(new bignumber_1.BigNumber(feeTx.maxFeePerGas.toString(10))).toString(10),
574
+ protocolIdentifier: this.identifier,
575
+ network: this.options.network,
576
+ isInbound: false,
577
+ data: feeTx.data.toString('hex'),
578
+ extra: {
579
+ chainId: feeTx.chainId.toString(10),
580
+ nonce: feeTx.nonce.toString(10)
581
+ },
582
+ transactionDetails: { raw: transaction.transaction }
583
+ }
584
+ ]];
585
+ }
586
+ catch (e) {
587
+ throw new Error("Transaction type \"".concat(ethTx.type, "\" not supported"));
588
+ }
589
+ return [2 /*return*/];
590
+ });
591
+ });
592
+ };
593
+ BaseEthereumProtocol.prototype.getBalanceOfPublicKey = function (publicKey) {
594
+ return __awaiter(this, void 0, void 0, function () {
595
+ var address;
596
+ return __generator(this, function (_a) {
597
+ switch (_a.label) {
598
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
599
+ case 1:
600
+ address = _a.sent();
601
+ return [2 /*return*/, this.getBalanceOfAddresses([address.address])];
602
+ }
603
+ });
604
+ });
605
+ };
606
+ BaseEthereumProtocol.prototype.getBalanceOfAddresses = function (addresses) {
607
+ return __awaiter(this, void 0, void 0, function () {
608
+ var balances;
609
+ var _this = this;
610
+ return __generator(this, function (_a) {
611
+ switch (_a.label) {
612
+ case 0: return [4 /*yield*/, Promise.all(addresses.map(function (address) {
613
+ return _this.options.nodeClient.fetchBalance(address);
614
+ }))];
615
+ case 1:
616
+ balances = _a.sent();
617
+ return [2 /*return*/, balances.reduce(function (a, b) { return a.plus(b); }).toString(10)];
618
+ }
619
+ });
620
+ });
621
+ };
622
+ BaseEthereumProtocol.prototype.getBalanceOfPublicKeyForSubProtocols = function (publicKey, subProtocols) {
623
+ return __awaiter(this, void 0, void 0, function () {
624
+ var address, contractAddresses, balances;
625
+ var _this = this;
626
+ return __generator(this, function (_a) {
627
+ switch (_a.label) {
628
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)
629
+ .then(function (address) { return address.address; })
630
+ .catch(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
631
+ switch (_a.label) {
632
+ case 0: return [4 /*yield*/, this.getAddressFromExtendedPublicKey(publicKey, 0, 0).then(function (address) { return address.address; })];
633
+ case 1: return [2 /*return*/, _a.sent()];
634
+ }
635
+ }); }); })];
636
+ case 1:
637
+ address = _a.sent();
638
+ return [4 /*yield*/, Promise.all(subProtocols.map(function (subProtocol) { return __awaiter(_this, void 0, void 0, function () {
639
+ var subProtocolType, subProtocolContractAddress;
640
+ return __generator(this, function (_a) {
641
+ switch (_a.label) {
642
+ case 0: return [4 /*yield*/, subProtocol.getSubProtocolType()];
643
+ case 1:
644
+ subProtocolType = _a.sent();
645
+ return [4 /*yield*/, subProtocol.getContractAddress()];
646
+ case 2:
647
+ subProtocolContractAddress = _a.sent();
648
+ if (subProtocolType === ICoinSubProtocol_1.SubProtocolType.TOKEN && subProtocolContractAddress) {
649
+ return [2 /*return*/, subProtocolContractAddress];
650
+ }
651
+ else {
652
+ throw new errors_1.UnsupportedError(coinlib_error_1.Domain.ETHEREUM, 'can only retrieve balance of ERC20 tokens');
653
+ }
654
+ return [2 /*return*/];
655
+ }
656
+ });
657
+ }); }))];
658
+ case 2:
659
+ contractAddresses = _a.sent();
660
+ return [4 /*yield*/, this.options.nodeClient.callBalanceOfOnContracts(contractAddresses, address)];
661
+ case 3:
662
+ balances = _a.sent();
663
+ return [2 /*return*/, contractAddresses.map(function (contractAddresse) { var _a, _b; return (_b = (_a = balances[contractAddresse]) === null || _a === void 0 ? void 0 : _a.toFixed()) !== null && _b !== void 0 ? _b : '0'; })];
664
+ }
665
+ });
666
+ });
667
+ };
668
+ BaseEthereumProtocol.prototype.getBalanceOfExtendedPublicKey = function (extendedPublicKey, offset) {
669
+ if (offset === void 0) { offset = 0; }
670
+ return __awaiter(this, void 0, void 0, function () {
671
+ var publicKey;
672
+ return __generator(this, function (_a) {
673
+ switch (_a.label) {
674
+ case 0: return [4 /*yield*/, this.getPublicKeyFromExtendedPublicKey(extendedPublicKey, 0, 0)];
675
+ case 1:
676
+ publicKey = _a.sent();
677
+ return [2 /*return*/, this.getBalanceOfPublicKey(publicKey)];
678
+ }
679
+ });
680
+ });
681
+ };
682
+ BaseEthereumProtocol.prototype.getAvailableBalanceOfAddresses = function (addresses) {
683
+ return __awaiter(this, void 0, void 0, function () {
684
+ return __generator(this, function (_a) {
685
+ return [2 /*return*/, this.getBalanceOfAddresses(addresses)];
686
+ });
687
+ });
688
+ };
689
+ BaseEthereumProtocol.prototype.estimateMaxTransactionValueFromExtendedPublicKey = function (extendedPublicKey, recipients, fee) {
690
+ return __awaiter(this, void 0, void 0, function () {
691
+ var publicKey;
692
+ return __generator(this, function (_a) {
693
+ switch (_a.label) {
694
+ case 0: return [4 /*yield*/, this.getPublicKeyFromExtendedPublicKey(extendedPublicKey, 0, 0)];
695
+ case 1:
696
+ publicKey = _a.sent();
697
+ return [2 /*return*/, this.estimateMaxTransactionValueFromPublicKey(publicKey, recipients, fee)];
698
+ }
699
+ });
700
+ });
701
+ };
702
+ BaseEthereumProtocol.prototype.estimateFeeDefaultsFromExtendedPublicKey = function (extendedPublicKey, recipients, values, data) {
703
+ return __awaiter(this, void 0, void 0, function () {
704
+ var publicKey;
705
+ return __generator(this, function (_a) {
706
+ switch (_a.label) {
707
+ case 0: return [4 /*yield*/, this.getPublicKeyFromExtendedPublicKey(extendedPublicKey, 0, 0)];
708
+ case 1:
709
+ publicKey = _a.sent();
710
+ return [2 /*return*/, this.estimateFeeDefaultsFromPublicKey(publicKey, recipients, values, data)];
711
+ }
712
+ });
713
+ });
714
+ };
715
+ BaseEthereumProtocol.prototype.prepareTransactionFromExtendedPublicKey = function (extendedPublicKey, _offset, recipients, values, fee, data) {
716
+ return __awaiter(this, void 0, void 0, function () {
717
+ var publicKey;
718
+ return __generator(this, function (_a) {
719
+ switch (_a.label) {
720
+ case 0: return [4 /*yield*/, this.getPublicKeyFromExtendedPublicKey(extendedPublicKey, 0, 0)];
721
+ case 1:
722
+ publicKey = _a.sent();
723
+ return [2 /*return*/, this.prepareTransactionFromPublicKey(publicKey, recipients, values, fee, data)];
724
+ }
725
+ });
726
+ });
727
+ };
728
+ BaseEthereumProtocol.prototype.estimateMaxTransactionValueFromPublicKey = function (publicKey, recipients, fee) {
729
+ return __awaiter(this, void 0, void 0, function () {
730
+ var balance, balanceWrapper, maxFee, estimatedFeeDefaults, amountWithoutFees;
731
+ return __generator(this, function (_a) {
732
+ switch (_a.label) {
733
+ case 0: return [4 /*yield*/, this.getBalanceOfPublicKey(publicKey)];
734
+ case 1:
735
+ balance = _a.sent();
736
+ balanceWrapper = new bignumber_1.BigNumber(balance);
737
+ if (!(fee !== undefined)) return [3 /*break*/, 2];
738
+ maxFee = new bignumber_1.BigNumber(fee);
739
+ return [3 /*break*/, 4];
740
+ case 2: return [4 /*yield*/, this.estimateFeeDefaultsFromPublicKey(publicKey, recipients, [balance])];
741
+ case 3:
742
+ estimatedFeeDefaults = _a.sent();
743
+ maxFee = new bignumber_1.BigNumber(estimatedFeeDefaults.medium).shiftedBy(this.decimals);
744
+ if (maxFee.gte(balanceWrapper)) {
745
+ maxFee = new bignumber_1.BigNumber(0);
282
746
  }
283
- : {})
747
+ _a.label = 4;
748
+ case 4:
749
+ amountWithoutFees = balanceWrapper.minus(maxFee);
750
+ if (amountWithoutFees.isNegative()) {
751
+ amountWithoutFees = new bignumber_1.BigNumber(0);
752
+ }
753
+ return [2 /*return*/, amountWithoutFees.toFixed()];
284
754
  }
285
- ];
286
- }
287
- else {
288
- const transaction = unsignedTx.transaction;
289
- const ownAddress = unsignedTx.publicKey.startsWith('x') // xPub
290
- ? await this.getAddressFromExtendedPublicKey(unsignedTx.publicKey, 0, 0)
291
- : await this.getAddressFromPublicKey(unsignedTx.publicKey);
292
- return [
293
- {
294
- from: [ownAddress.address],
295
- to: [transaction.to],
296
- amount: new bignumber_1.BigNumber(transaction.value).toString(10),
297
- fee: new bignumber_1.BigNumber(transaction.gasLimit).multipliedBy(new bignumber_1.BigNumber(transaction.gasPrice)).toString(10),
298
- protocolIdentifier: this.identifier,
299
- network: this.options.network,
300
- isInbound: false,
301
- data: transaction.data,
302
- transactionDetails: unsignedTx
755
+ });
756
+ });
757
+ };
758
+ BaseEthereumProtocol.prototype.estimateFeeDefaultsFromPublicKey = function (publicKey, recipients, values, data) {
759
+ return __awaiter(this, void 0, void 0, function () {
760
+ var address, estimatedGas, gasPrise, feeStepFactor, estimatedFee, lowFee, mediumFee, highFee;
761
+ return __generator(this, function (_a) {
762
+ switch (_a.label) {
763
+ case 0:
764
+ if (recipients.length !== values.length) {
765
+ return [2 /*return*/, Promise.reject('recipients length does not match with values')];
766
+ }
767
+ if (recipients.length !== 1) {
768
+ return [2 /*return*/, Promise.reject('you cannot have 0 recipients')];
769
+ }
770
+ return [4 /*yield*/, this.getAddressFromPublicKey(publicKey).then(function (address) { return address.address; })];
771
+ case 1:
772
+ address = _a.sent();
773
+ return [4 /*yield*/, this.options.nodeClient.estimateTransactionGas(address, recipients[0], utils_1.EthereumUtils.toHex(values[0]), undefined, utils_1.EthereumUtils.toHex(this.MAX_GAS_ESTIMATE))];
774
+ case 2:
775
+ estimatedGas = _a.sent();
776
+ return [4 /*yield*/, this.options.nodeClient.getGasPrice()];
777
+ case 3:
778
+ gasPrise = _a.sent();
779
+ feeStepFactor = new bignumber_1.BigNumber(0.5);
780
+ estimatedFee = estimatedGas.times(gasPrise);
781
+ lowFee = estimatedFee.minus(estimatedFee.times(feeStepFactor).integerValue(bignumber_1.BigNumber.ROUND_FLOOR));
782
+ mediumFee = estimatedFee;
783
+ highFee = mediumFee.plus(mediumFee.times(feeStepFactor).integerValue(bignumber_1.BigNumber.ROUND_FLOOR));
784
+ return [2 /*return*/, {
785
+ low: lowFee.shiftedBy(-this.feeDecimals).toFixed(),
786
+ medium: mediumFee.shiftedBy(-this.feeDecimals).toFixed(),
787
+ high: highFee.shiftedBy(-this.feeDecimals).toFixed()
788
+ }];
303
789
  }
304
- ];
305
- }
306
- }
307
- async getTransactionDetailsFromSigned(transaction) {
308
- const ethTx = tx_1.TransactionFactory.fromSerializedData(Buffer.from(transaction.transaction, 'hex'));
309
- if (ethTx.type === 0) {
310
- const tx = ethTx;
311
- const hexValue = tx.value.toString('hex') || '0x0';
312
- const hexGasPrice = tx.gasPrice.toString('hex') || '0x0';
313
- const hexGasLimit = tx.gasLimit.toString('hex') || '0x0';
314
- const hexNonce = tx.nonce.toString('hex') || '0x0';
315
- const chainId = tx.common.chainIdBN().toString(10);
316
- const to = tx.to;
317
- if (!to) {
318
- throw new Error('No "TO" address');
319
- }
320
- return [
321
- {
322
- from: [tx.getSenderAddress().toString()],
323
- to: [to.toString()],
324
- amount: new bignumber_1.BigNumber(parseInt(hexValue, 16)).toString(10),
325
- fee: new bignumber_1.BigNumber(parseInt(hexGasLimit, 16)).multipliedBy(new bignumber_1.BigNumber(parseInt(hexGasPrice, 16))).toString(10),
326
- protocolIdentifier: this.identifier,
327
- network: this.options.network,
328
- isInbound: tx.toCreationAddress(),
329
- hash: `0x${tx.hash().toString('hex')}`,
330
- data: `0x${tx.data.toString('hex')}`,
331
- extra: {
332
- chainId,
333
- nonce: parseInt(hexNonce, 16)
334
- },
335
- transactionDetails: { raw: transaction.transaction }
790
+ });
791
+ });
792
+ };
793
+ BaseEthereumProtocol.prototype.prepareTransactionFromPublicKey = function (publicKey, recipients, values, fee, data) {
794
+ return __awaiter(this, void 0, void 0, function () {
795
+ var wrappedValues, wrappedFee, address, amount, balance, gasLimit, gasPrice, txCount, transaction;
796
+ return __generator(this, function (_a) {
797
+ switch (_a.label) {
798
+ case 0:
799
+ wrappedValues = values.map(function (value) { return new bignumber_1.BigNumber(value); });
800
+ wrappedFee = new bignumber_1.BigNumber(fee);
801
+ return [4 /*yield*/, this.getAddressFromPublicKey(publicKey).then(function (address) { return address.address; })];
802
+ case 1:
803
+ address = _a.sent();
804
+ if (recipients.length !== values.length) {
805
+ return [2 /*return*/, Promise.reject('recipients length does not match with values')];
806
+ }
807
+ if (recipients.length !== 1) {
808
+ return [2 /*return*/, Promise.reject('you cannot have 0 recipients')];
809
+ }
810
+ amount = utils_1.EthereumUtils.toHex(wrappedValues[0].toFixed());
811
+ return [4 /*yield*/, this.getBalanceOfPublicKey(publicKey)];
812
+ case 2:
813
+ balance = _a.sent();
814
+ return [4 /*yield*/, this.options.nodeClient.estimateTransactionGas(address, recipients[0], amount, undefined, utils_1.EthereumUtils.toHex(this.MAX_GAS_ESTIMATE))];
815
+ case 3:
816
+ gasLimit = _a.sent();
817
+ gasPrice = wrappedFee.div(gasLimit).integerValue(bignumber_1.BigNumber.ROUND_CEIL);
818
+ if (!new bignumber_1.BigNumber(balance).gte(new bignumber_1.BigNumber(wrappedValues[0].plus(wrappedFee)))) return [3 /*break*/, 5];
819
+ return [4 /*yield*/, this.options.nodeClient.fetchTransactionCount(address)];
820
+ case 4:
821
+ txCount = _a.sent();
822
+ transaction = {
823
+ nonce: utils_1.EthereumUtils.toHex(txCount),
824
+ gasLimit: utils_1.EthereumUtils.toHex(gasLimit.toFixed()),
825
+ gasPrice: utils_1.EthereumUtils.toHex(gasPrice.toFixed()),
826
+ to: recipients[0],
827
+ value: amount,
828
+ chainId: this.options.network.extras.chainID,
829
+ data: '0x'
830
+ };
831
+ return [2 /*return*/, transaction];
832
+ case 5: throw new errors_1.BalanceError(coinlib_error_1.Domain.ETHEREUM, 'not enough balance');
336
833
  }
337
- ];
338
- }
339
- try {
340
- const feeTx = ethTx;
341
- return [
342
- {
343
- from: [feeTx.getSenderAddress().toString()],
344
- to: [feeTx.to?.toString() ?? ''],
345
- amount: new bignumber_1.BigNumber(feeTx.value.toString(10)).toString(10),
346
- fee: new bignumber_1.BigNumber(feeTx.gasLimit.toString(10)).multipliedBy(new bignumber_1.BigNumber(feeTx.maxFeePerGas.toString(10))).toString(10),
347
- protocolIdentifier: this.identifier,
348
- network: this.options.network,
349
- isInbound: false,
350
- data: feeTx.data.toString('hex'),
351
- extra: {
352
- chainId: feeTx.chainId.toString(10),
353
- nonce: feeTx.nonce.toString(10)
354
- },
355
- transactionDetails: { raw: transaction.transaction }
834
+ });
835
+ });
836
+ };
837
+ BaseEthereumProtocol.prototype.broadcastTransaction = function (rawTransaction) {
838
+ return __awaiter(this, void 0, void 0, function () {
839
+ return __generator(this, function (_a) {
840
+ return [2 /*return*/, this.options.nodeClient.sendSignedTransaction("0x".concat(rawTransaction))];
841
+ });
842
+ });
843
+ };
844
+ BaseEthereumProtocol.prototype.getTransactionsFromExtendedPublicKey = function (extendedPublicKey, limit, cursor) {
845
+ return __awaiter(this, void 0, void 0, function () {
846
+ var publicKey;
847
+ return __generator(this, function (_a) {
848
+ switch (_a.label) {
849
+ case 0: return [4 /*yield*/, this.getPublicKeyFromExtendedPublicKey(extendedPublicKey, 0, 0)];
850
+ case 1:
851
+ publicKey = _a.sent();
852
+ return [2 /*return*/, this.getTransactionsFromPublicKey(publicKey, limit, cursor)];
356
853
  }
357
- ];
358
- }
359
- catch (e) {
360
- throw new Error(`Transaction type "${ethTx.type}" not supported`);
361
- }
362
- }
363
- async getBalanceOfPublicKey(publicKey) {
364
- const address = await this.getAddressFromPublicKey(publicKey);
365
- return this.getBalanceOfAddresses([address.address]);
366
- }
367
- async getBalanceOfAddresses(addresses) {
368
- const balances = await Promise.all(addresses.map((address) => {
369
- return this.options.nodeClient.fetchBalance(address);
370
- }));
371
- return balances.reduce((a, b) => a.plus(b)).toString(10);
372
- }
373
- async getBalanceOfPublicKeyForSubProtocols(publicKey, subProtocols) {
374
- const address = await this.getAddressFromPublicKey(publicKey)
375
- .then((address) => address.address)
376
- .catch(async () => await this.getAddressFromExtendedPublicKey(publicKey, 0, 0).then((address) => address.address));
377
- const contractAddresses = await Promise.all(subProtocols.map(async (subProtocol) => {
378
- const subProtocolType = await subProtocol.getSubProtocolType();
379
- const subProtocolContractAddress = await subProtocol.getContractAddress();
380
- if (subProtocolType === ICoinSubProtocol_1.SubProtocolType.TOKEN && subProtocolContractAddress) {
381
- return subProtocolContractAddress;
382
- }
383
- else {
384
- throw new errors_1.UnsupportedError(coinlib_error_1.Domain.ETHEREUM, 'can only retrieve balance of ERC20 tokens');
385
- }
386
- }));
387
- const balances = await this.options.nodeClient.callBalanceOfOnContracts(contractAddresses, address);
388
- return contractAddresses.map((contractAddresse) => balances[contractAddresse]?.toFixed() ?? '0');
389
- }
390
- async getBalanceOfExtendedPublicKey(extendedPublicKey, offset = 0) {
391
- const publicKey = await this.getPublicKeyFromExtendedPublicKey(extendedPublicKey, 0, 0);
392
- return this.getBalanceOfPublicKey(publicKey);
393
- }
394
- async getAvailableBalanceOfAddresses(addresses) {
395
- return this.getBalanceOfAddresses(addresses);
396
- }
397
- async estimateMaxTransactionValueFromExtendedPublicKey(extendedPublicKey, recipients, fee) {
398
- const publicKey = await this.getPublicKeyFromExtendedPublicKey(extendedPublicKey, 0, 0);
399
- return this.estimateMaxTransactionValueFromPublicKey(publicKey, recipients, fee);
400
- }
401
- async estimateFeeDefaultsFromExtendedPublicKey(extendedPublicKey, recipients, values, data) {
402
- const publicKey = await this.getPublicKeyFromExtendedPublicKey(extendedPublicKey, 0, 0);
403
- return this.estimateFeeDefaultsFromPublicKey(publicKey, recipients, values, data);
404
- }
405
- async prepareTransactionFromExtendedPublicKey(extendedPublicKey, _offset, recipients, values, fee, data) {
406
- const publicKey = await this.getPublicKeyFromExtendedPublicKey(extendedPublicKey, 0, 0);
407
- return this.prepareTransactionFromPublicKey(publicKey, recipients, values, fee, data);
408
- }
409
- async estimateMaxTransactionValueFromPublicKey(publicKey, recipients, fee) {
410
- const balance = await this.getBalanceOfPublicKey(publicKey);
411
- const balanceWrapper = new bignumber_1.BigNumber(balance);
412
- let maxFee;
413
- if (fee !== undefined) {
414
- maxFee = new bignumber_1.BigNumber(fee);
415
- }
416
- else {
417
- const estimatedFeeDefaults = await this.estimateFeeDefaultsFromPublicKey(publicKey, recipients, [balance]);
418
- maxFee = new bignumber_1.BigNumber(estimatedFeeDefaults.medium).shiftedBy(this.decimals);
419
- if (maxFee.gte(balanceWrapper)) {
420
- maxFee = new bignumber_1.BigNumber(0);
421
- }
422
- }
423
- let amountWithoutFees = balanceWrapper.minus(maxFee);
424
- if (amountWithoutFees.isNegative()) {
425
- amountWithoutFees = new bignumber_1.BigNumber(0);
426
- }
427
- return amountWithoutFees.toFixed();
428
- }
429
- async estimateFeeDefaultsFromPublicKey(publicKey, recipients, values, data) {
430
- if (recipients.length !== values.length) {
431
- return Promise.reject('recipients length does not match with values');
432
- }
433
- if (recipients.length !== 1) {
434
- return Promise.reject('you cannot have 0 recipients');
435
- }
436
- const address = await this.getAddressFromPublicKey(publicKey).then((address) => address.address);
437
- const estimatedGas = await this.options.nodeClient.estimateTransactionGas(address, recipients[0], utils_1.EthereumUtils.toHex(values[0]), undefined, utils_1.EthereumUtils.toHex(this.MAX_GAS_ESTIMATE));
438
- const gasPrise = await this.options.nodeClient.getGasPrice();
439
- const feeStepFactor = new bignumber_1.BigNumber(0.5);
440
- const estimatedFee = estimatedGas.times(gasPrise);
441
- const lowFee = estimatedFee.minus(estimatedFee.times(feeStepFactor).integerValue(bignumber_1.BigNumber.ROUND_FLOOR));
442
- const mediumFee = estimatedFee;
443
- const highFee = mediumFee.plus(mediumFee.times(feeStepFactor).integerValue(bignumber_1.BigNumber.ROUND_FLOOR));
444
- return {
445
- low: lowFee.shiftedBy(-this.feeDecimals).toFixed(),
446
- medium: mediumFee.shiftedBy(-this.feeDecimals).toFixed(),
447
- high: highFee.shiftedBy(-this.feeDecimals).toFixed()
448
- };
449
- }
450
- async prepareTransactionFromPublicKey(publicKey, recipients, values, fee, data) {
451
- const wrappedValues = values.map((value) => new bignumber_1.BigNumber(value));
452
- const wrappedFee = new bignumber_1.BigNumber(fee);
453
- const address = await this.getAddressFromPublicKey(publicKey).then((address) => address.address);
454
- if (recipients.length !== values.length) {
455
- return Promise.reject('recipients length does not match with values');
456
- }
457
- if (recipients.length !== 1) {
458
- return Promise.reject('you cannot have 0 recipients');
459
- }
460
- const amount = utils_1.EthereumUtils.toHex(wrappedValues[0].toFixed());
461
- const balance = await this.getBalanceOfPublicKey(publicKey);
462
- const gasLimit = await this.options.nodeClient.estimateTransactionGas(address, recipients[0], amount, undefined, utils_1.EthereumUtils.toHex(this.MAX_GAS_ESTIMATE));
463
- const gasPrice = wrappedFee.div(gasLimit).integerValue(bignumber_1.BigNumber.ROUND_CEIL);
464
- if (new bignumber_1.BigNumber(balance).gte(new bignumber_1.BigNumber(wrappedValues[0].plus(wrappedFee)))) {
465
- const txCount = await this.options.nodeClient.fetchTransactionCount(address);
466
- const transaction = {
467
- nonce: utils_1.EthereumUtils.toHex(txCount),
468
- gasLimit: utils_1.EthereumUtils.toHex(gasLimit.toFixed()),
469
- gasPrice: utils_1.EthereumUtils.toHex(gasPrice.toFixed()), // 10 Gwei
470
- to: recipients[0],
471
- value: amount,
472
- chainId: this.options.network.extras.chainID,
473
- data: '0x'
474
- };
475
- return transaction;
476
- }
477
- else {
478
- throw new errors_1.BalanceError(coinlib_error_1.Domain.ETHEREUM, 'not enough balance');
479
- }
480
- }
481
- async broadcastTransaction(rawTransaction) {
482
- return this.options.nodeClient.sendSignedTransaction(`0x${rawTransaction}`);
483
- }
484
- async getTransactionsFromExtendedPublicKey(extendedPublicKey, limit, cursor) {
485
- const publicKey = await this.getPublicKeyFromExtendedPublicKey(extendedPublicKey, 0, 0);
486
- return this.getTransactionsFromPublicKey(publicKey, limit, cursor);
487
- }
488
- async getTransactionsFromPublicKey(publicKey, limit = 50, cursor) {
489
- const address = await this.getAddressFromPublicKey(publicKey);
490
- return this.getTransactionsFromAddresses([address.address], limit, cursor);
491
- }
492
- getTransactionsFromAddresses(addresses, limit, cursor) {
493
- return new Promise((overallResolve, overallReject) => {
494
- const promises = [];
495
- for (const address of addresses) {
496
- promises.push(this.options.infoClient.fetchTransactions(this, address, limit, cursor));
854
+ });
855
+ });
856
+ };
857
+ BaseEthereumProtocol.prototype.getTransactionsFromPublicKey = function (publicKey, limit, cursor) {
858
+ if (limit === void 0) { limit = 50; }
859
+ return __awaiter(this, void 0, void 0, function () {
860
+ var address;
861
+ return __generator(this, function (_a) {
862
+ switch (_a.label) {
863
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
864
+ case 1:
865
+ address = _a.sent();
866
+ return [2 /*return*/, this.getTransactionsFromAddresses([address.address], limit, cursor)];
867
+ }
868
+ });
869
+ });
870
+ };
871
+ BaseEthereumProtocol.prototype.getTransactionsFromAddresses = function (addresses, limit, cursor) {
872
+ var _this = this;
873
+ return new Promise(function (overallResolve, overallReject) {
874
+ var promises = [];
875
+ for (var _i = 0, addresses_1 = addresses; _i < addresses_1.length; _i++) {
876
+ var address = addresses_1[_i];
877
+ promises.push(_this.options.infoClient.fetchTransactions(_this, address, limit, cursor));
497
878
  }
498
879
  Promise.all(promises)
499
- .then((values) => {
500
- const page = Math.max(...values.map((txResult) => txResult.cursor.page));
501
- overallResolve(values.reduce((a, b) => {
502
- return { transactions: a.transactions.concat(b.transactions), cursor: { page } };
880
+ .then(function (values) {
881
+ var page = Math.max.apply(Math, values.map(function (txResult) { return txResult.cursor.page; }));
882
+ overallResolve(values.reduce(function (a, b) {
883
+ return { transactions: a.transactions.concat(b.transactions), cursor: { page: page } };
503
884
  }));
504
885
  })
505
886
  .catch(overallReject);
506
887
  });
507
- }
508
- async signMessage(message, keypair) {
509
- return this.cryptoClient.signMessage(message, keypair);
510
- }
511
- async verifyMessage(message, signature, publicKey) {
512
- return this.cryptoClient.verifyMessage(message, signature, publicKey);
513
- }
514
- async encryptAsymmetric(message, publicKey) {
515
- return this.cryptoClient.encryptAsymmetric(message, publicKey);
516
- }
517
- async decryptAsymmetric(message, keypair) {
518
- return this.cryptoClient.decryptAsymmetric(message, keypair);
519
- }
520
- async encryptAES(message, privateKey) {
521
- return this.cryptoClient.encryptAES(message, privateKey);
522
- }
523
- async decryptAES(message, privateKey) {
524
- return this.cryptoClient.decryptAES(message, privateKey);
525
- }
526
- async getTransactionStatuses(transactionHashes) {
527
- const statusPromises = transactionHashes.map((txHash) => {
528
- return this.options.nodeClient.getTransactionStatus(txHash);
888
+ };
889
+ BaseEthereumProtocol.prototype.signMessage = function (message, keypair) {
890
+ return __awaiter(this, void 0, void 0, function () {
891
+ return __generator(this, function (_a) {
892
+ return [2 /*return*/, this.cryptoClient.signMessage(message, keypair)];
893
+ });
529
894
  });
530
- return Promise.all(statusPromises);
531
- }
532
- }
895
+ };
896
+ BaseEthereumProtocol.prototype.verifyMessage = function (message, signature, publicKey) {
897
+ return __awaiter(this, void 0, void 0, function () {
898
+ return __generator(this, function (_a) {
899
+ return [2 /*return*/, this.cryptoClient.verifyMessage(message, signature, publicKey)];
900
+ });
901
+ });
902
+ };
903
+ BaseEthereumProtocol.prototype.encryptAsymmetric = function (message, publicKey) {
904
+ return __awaiter(this, void 0, void 0, function () {
905
+ return __generator(this, function (_a) {
906
+ return [2 /*return*/, this.cryptoClient.encryptAsymmetric(message, publicKey)];
907
+ });
908
+ });
909
+ };
910
+ BaseEthereumProtocol.prototype.decryptAsymmetric = function (message, keypair) {
911
+ return __awaiter(this, void 0, void 0, function () {
912
+ return __generator(this, function (_a) {
913
+ return [2 /*return*/, this.cryptoClient.decryptAsymmetric(message, keypair)];
914
+ });
915
+ });
916
+ };
917
+ BaseEthereumProtocol.prototype.encryptAES = function (message, privateKey) {
918
+ return __awaiter(this, void 0, void 0, function () {
919
+ return __generator(this, function (_a) {
920
+ return [2 /*return*/, this.cryptoClient.encryptAES(message, privateKey)];
921
+ });
922
+ });
923
+ };
924
+ BaseEthereumProtocol.prototype.decryptAES = function (message, privateKey) {
925
+ return __awaiter(this, void 0, void 0, function () {
926
+ return __generator(this, function (_a) {
927
+ return [2 /*return*/, this.cryptoClient.decryptAES(message, privateKey)];
928
+ });
929
+ });
930
+ };
931
+ BaseEthereumProtocol.prototype.getTransactionStatuses = function (transactionHashes) {
932
+ return __awaiter(this, void 0, void 0, function () {
933
+ var statusPromises;
934
+ var _this = this;
935
+ return __generator(this, function (_a) {
936
+ statusPromises = transactionHashes.map(function (txHash) {
937
+ return _this.options.nodeClient.getTransactionStatus(txHash);
938
+ });
939
+ return [2 /*return*/, Promise.all(statusPromises)];
940
+ });
941
+ });
942
+ };
943
+ return BaseEthereumProtocol;
944
+ }());
533
945
  exports.BaseEthereumProtocol = BaseEthereumProtocol;
534
946
  //# sourceMappingURL=BaseEthereumProtocol.js.map