@airgap/ethereum 0.13.11-beta.1 → 0.13.11-beta.3

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