@wallfree-dev/ethereum 0.13.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (167) hide show
  1. package/LICENSE.md +7 -0
  2. package/index.d.ts +1 -0
  3. package/index.js +18 -0
  4. package/index.js.map +1 -0
  5. package/package.json +67 -0
  6. package/readme.md +121 -0
  7. package/v0/index.d.ts +12 -0
  8. package/v0/index.js +39 -0
  9. package/v0/index.js.map +1 -0
  10. package/v0/protocol/BaseEthereumProtocol.d.ts +99 -0
  11. package/v0/protocol/BaseEthereumProtocol.js +946 -0
  12. package/v0/protocol/BaseEthereumProtocol.js.map +1 -0
  13. package/v0/protocol/EthereumAddress.d.ts +8 -0
  14. package/v0/protocol/EthereumAddress.js +43 -0
  15. package/v0/protocol/EthereumAddress.js.map +1 -0
  16. package/v0/protocol/EthereumChainIDs.d.ts +1 -0
  17. package/v0/protocol/EthereumChainIDs.js +340 -0
  18. package/v0/protocol/EthereumChainIDs.js.map +1 -0
  19. package/v0/protocol/EthereumClassicProtocol.d.ts +6 -0
  20. package/v0/protocol/EthereumClassicProtocol.js +30 -0
  21. package/v0/protocol/EthereumClassicProtocol.js.map +1 -0
  22. package/v0/protocol/EthereumCryptoClient.d.ts +12 -0
  23. package/v0/protocol/EthereumCryptoClient.js +148 -0
  24. package/v0/protocol/EthereumCryptoClient.js.map +1 -0
  25. package/v0/protocol/EthereumProtocol.d.ts +10 -0
  26. package/v0/protocol/EthereumProtocol.js +34 -0
  27. package/v0/protocol/EthereumProtocol.js.map +1 -0
  28. package/v0/protocol/EthereumProtocolOptions.d.ts +46 -0
  29. package/v0/protocol/EthereumProtocolOptions.js +150 -0
  30. package/v0/protocol/EthereumProtocolOptions.js.map +1 -0
  31. package/v0/protocol/EthereumRopstenProtocol.d.ts +6 -0
  32. package/v0/protocol/EthereumRopstenProtocol.js +30 -0
  33. package/v0/protocol/EthereumRopstenProtocol.js.map +1 -0
  34. package/v0/protocol/EthereumTypes.d.ts +14 -0
  35. package/v0/protocol/EthereumTypes.js +3 -0
  36. package/v0/protocol/EthereumTypes.js.map +1 -0
  37. package/v0/protocol/clients/info-clients/EtherscanInfoClient.d.ts +8 -0
  38. package/v0/protocol/clients/info-clients/EtherscanInfoClient.js +195 -0
  39. package/v0/protocol/clients/info-clients/EtherscanInfoClient.js.map +1 -0
  40. package/v0/protocol/clients/info-clients/InfoClient.d.ts +8 -0
  41. package/v0/protocol/clients/info-clients/InfoClient.js +11 -0
  42. package/v0/protocol/clients/info-clients/InfoClient.js.map +1 -0
  43. package/v0/protocol/clients/node-clients/AirGapNodeClient.d.ts +41 -0
  44. package/v0/protocol/clients/node-clients/AirGapNodeClient.js +353 -0
  45. package/v0/protocol/clients/node-clients/AirGapNodeClient.js.map +1 -0
  46. package/v0/protocol/clients/node-clients/NodeClient.d.ts +17 -0
  47. package/v0/protocol/clients/node-clients/NodeClient.js +11 -0
  48. package/v0/protocol/clients/node-clients/NodeClient.js.map +1 -0
  49. package/v0/protocol/erc20/ERC20.d.ts +3 -0
  50. package/v0/protocol/erc20/ERC20.js +9 -0
  51. package/v0/protocol/erc20/ERC20.js.map +1 -0
  52. package/v0/protocol/erc20/GenericERC20.d.ts +32 -0
  53. package/v0/protocol/erc20/GenericERC20.js +335 -0
  54. package/v0/protocol/erc20/GenericERC20.js.map +1 -0
  55. package/v0/protocol/utils/utils.d.ts +13 -0
  56. package/v0/protocol/utils/utils.js +140 -0
  57. package/v0/protocol/utils/utils.js.map +1 -0
  58. package/v0/serializer/schemas/v2/transaction-sign-request-ethereum.json +50 -0
  59. package/v0/serializer/schemas/v2/transaction-sign-response-ethereum.json +19 -0
  60. package/v0/serializer/schemas/v3/transaction-sign-request-ethereum-typed.json +38 -0
  61. package/v0/serializer/schemas/v3/transaction-sign-request-ethereum.json +50 -0
  62. package/v0/serializer/schemas/v3/transaction-sign-response-ethereum.json +19 -0
  63. package/v0/serializer/validators/transaction-validator.d.ts +15 -0
  64. package/v0/serializer/validators/transaction-validator.js +96 -0
  65. package/v0/serializer/validators/transaction-validator.js.map +1 -0
  66. package/v0/serializer/validators/validators.d.ts +1 -0
  67. package/v0/serializer/validators/validators.js +78 -0
  68. package/v0/serializer/validators/validators.js.map +1 -0
  69. package/v0/types/signed-transaction-ethereum.d.ts +5 -0
  70. package/v0/types/signed-transaction-ethereum.js +3 -0
  71. package/v0/types/signed-transaction-ethereum.js.map +1 -0
  72. package/v0/types/transaction-ethereum.d.ts +14 -0
  73. package/v0/types/transaction-ethereum.js +3 -0
  74. package/v0/types/transaction-ethereum.js.map +1 -0
  75. package/v0/types/unsigned-transaction-ethereum-typed.d.ts +11 -0
  76. package/v0/types/unsigned-transaction-ethereum-typed.js +3 -0
  77. package/v0/types/unsigned-transaction-ethereum-typed.js.map +1 -0
  78. package/v0/types/unsigned-transaction-ethereum.d.ts +15 -0
  79. package/v0/types/unsigned-transaction-ethereum.js +3 -0
  80. package/v0/types/unsigned-transaction-ethereum.js.map +1 -0
  81. package/v1/block-explorer/EtherscanBlockExplorer.d.ts +9 -0
  82. package/v1/block-explorer/EtherscanBlockExplorer.js +72 -0
  83. package/v1/block-explorer/EtherscanBlockExplorer.js.map +1 -0
  84. package/v1/clients/crypto/EthereumCryptoClient.d.ts +7 -0
  85. package/v1/clients/crypto/EthereumCryptoClient.js +147 -0
  86. package/v1/clients/crypto/EthereumCryptoClient.js.map +1 -0
  87. package/v1/clients/info/EthereumInfoClient.d.ts +15 -0
  88. package/v1/clients/info/EthereumInfoClient.js +11 -0
  89. package/v1/clients/info/EthereumInfoClient.js.map +1 -0
  90. package/v1/clients/info/EtherscanInfoClient.d.ts +8 -0
  91. package/v1/clients/info/EtherscanInfoClient.js +173 -0
  92. package/v1/clients/info/EtherscanInfoClient.js.map +1 -0
  93. package/v1/clients/node/EthereumNodeClient.d.ts +18 -0
  94. package/v1/clients/node/EthereumNodeClient.js +3 -0
  95. package/v1/clients/node/EthereumNodeClient.js.map +1 -0
  96. package/v1/clients/node/HttpEthereumNodeClient.d.ts +66 -0
  97. package/v1/clients/node/HttpEthereumNodeClient.js +451 -0
  98. package/v1/clients/node/HttpEthereumNodeClient.js.map +1 -0
  99. package/v1/data/EthereumAddress.d.ts +7 -0
  100. package/v1/data/EthereumAddress.js +46 -0
  101. package/v1/data/EthereumAddress.js.map +1 -0
  102. package/v1/index.d.ts +30 -0
  103. package/v1/index.js +43 -0
  104. package/v1/index.js.map +1 -0
  105. package/v1/module/ERC20Tokens.d.ts +3 -0
  106. package/v1/module/ERC20Tokens.js +847 -0
  107. package/v1/module/ERC20Tokens.js.map +1 -0
  108. package/v1/module/EthereumModule.d.ts +16 -0
  109. package/v1/module/EthereumModule.js +127 -0
  110. package/v1/module/EthereumModule.js.map +1 -0
  111. package/v1/module.d.ts +3 -0
  112. package/v1/module.js +24 -0
  113. package/v1/module.js.map +1 -0
  114. package/v1/protocol/EthereumBaseProtocol.d.ts +87 -0
  115. package/v1/protocol/EthereumBaseProtocol.js +892 -0
  116. package/v1/protocol/EthereumBaseProtocol.js.map +1 -0
  117. package/v1/protocol/EthereumChainIds.d.ts +1 -0
  118. package/v1/protocol/EthereumChainIds.js +340 -0
  119. package/v1/protocol/EthereumChainIds.js.map +1 -0
  120. package/v1/protocol/EthereumProtocol.d.ts +8 -0
  121. package/v1/protocol/EthereumProtocol.js +65 -0
  122. package/v1/protocol/EthereumProtocol.js.map +1 -0
  123. package/v1/protocol/erc20/ERC20Protocol.d.ts +32 -0
  124. package/v1/protocol/erc20/ERC20Protocol.js +397 -0
  125. package/v1/protocol/erc20/ERC20Protocol.js.map +1 -0
  126. package/v1/protocol/erc20/ERC20Token.d.ts +19 -0
  127. package/v1/protocol/erc20/ERC20Token.js +131 -0
  128. package/v1/protocol/erc20/ERC20Token.js.map +1 -0
  129. package/v1/serializer/v3/schemas/converter/transaction-converter.d.ts +8 -0
  130. package/v1/serializer/v3/schemas/converter/transaction-converter.js +82 -0
  131. package/v1/serializer/v3/schemas/converter/transaction-converter.js.map +1 -0
  132. package/v1/serializer/v3/schemas/definitions/transaction-sign-request-ethereum-typed.d.ts +10 -0
  133. package/v1/serializer/v3/schemas/definitions/transaction-sign-request-ethereum-typed.js +3 -0
  134. package/v1/serializer/v3/schemas/definitions/transaction-sign-request-ethereum-typed.js.map +1 -0
  135. package/v1/serializer/v3/schemas/definitions/transaction-sign-request-ethereum.d.ts +14 -0
  136. package/v1/serializer/v3/schemas/definitions/transaction-sign-request-ethereum.js +3 -0
  137. package/v1/serializer/v3/schemas/definitions/transaction-sign-request-ethereum.js.map +1 -0
  138. package/v1/serializer/v3/schemas/definitions/transaction-sign-response-ethereum.d.ts +3 -0
  139. package/v1/serializer/v3/schemas/definitions/transaction-sign-response-ethereum.js +3 -0
  140. package/v1/serializer/v3/schemas/definitions/transaction-sign-response-ethereum.js.map +1 -0
  141. package/v1/serializer/v3/schemas/generated/transaction-sign-request-ethereum-typed.json +41 -0
  142. package/v1/serializer/v3/schemas/generated/transaction-sign-request-ethereum.json +58 -0
  143. package/v1/serializer/v3/schemas/generated/transaction-sign-response-ethereum.json +19 -0
  144. package/v1/serializer/v3/serializer-companion.d.ts +13 -0
  145. package/v1/serializer/v3/serializer-companion.js +182 -0
  146. package/v1/serializer/v3/serializer-companion.js.map +1 -0
  147. package/v1/serializer/v3/validators/transaction-validator.d.ts +7 -0
  148. package/v1/serializer/v3/validators/transaction-validator.js +71 -0
  149. package/v1/serializer/v3/validators/transaction-validator.js.map +1 -0
  150. package/v1/types/crypto.d.ts +2 -0
  151. package/v1/types/crypto.js +3 -0
  152. package/v1/types/crypto.js.map +1 -0
  153. package/v1/types/protocol.d.ts +35 -0
  154. package/v1/types/protocol.js +3 -0
  155. package/v1/types/protocol.js.map +1 -0
  156. package/v1/types/transaction.d.ts +24 -0
  157. package/v1/types/transaction.js +3 -0
  158. package/v1/types/transaction.js.map +1 -0
  159. package/v1/utils/EthereumUtils.d.ts +16 -0
  160. package/v1/utils/EthereumUtils.js +155 -0
  161. package/v1/utils/EthereumUtils.js.map +1 -0
  162. package/v1/utils/key.d.ts +5 -0
  163. package/v1/utils/key.js +101 -0
  164. package/v1/utils/key.js.map +1 -0
  165. package/v1/utils/protocol.d.ts +13 -0
  166. package/v1/utils/protocol.js +38 -0
  167. package/v1/utils/protocol.js.map +1 -0
@@ -0,0 +1,946 @@
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("@wallfree-dev/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber");
78
+ // @ts-ignore
79
+ var bip39_2_5_0_1 = require("@wallfree-dev/coinlib-core/dependencies/src/bip39-2.5.0");
80
+ // @ts-ignore
81
+ var bitcoinJS = __importStar(require("@wallfree-dev/coinlib-core/dependencies/src/bitgo-utxo-lib-5d91049fd7a988382df81c8260e244ee56d57aac/src"));
82
+ var errors_1 = require("@wallfree-dev/coinlib-core/errors");
83
+ var coinlib_error_1 = require("@wallfree-dev/coinlib-core/errors/coinlib-error");
84
+ var IAirGapTransaction_1 = require("@wallfree-dev/coinlib-core/interfaces/IAirGapTransaction");
85
+ var ICoinSubProtocol_1 = require("@wallfree-dev/coinlib-core/protocols/ICoinSubProtocol");
86
+ var ProtocolSymbols_1 = require("@wallfree-dev/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.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*/, [
514
+ {
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
524
+ }
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);
746
+ }
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()];
754
+ }
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
+ }];
789
+ }
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');
833
+ }
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)];
853
+ }
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));
878
+ }
879
+ Promise.all(promises)
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 } };
884
+ }));
885
+ })
886
+ .catch(overallReject);
887
+ });
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
+ });
894
+ });
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
+ }());
945
+ exports.BaseEthereumProtocol = BaseEthereumProtocol;
946
+ //# sourceMappingURL=BaseEthereumProtocol.js.map