@airgap/ethereum 0.13.45-beta.2 → 0.13.45-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.
- package/package.json +5 -5
- package/v0/index.js +10 -10
- package/v0/index.js.map +1 -1
- package/v0/protocol/BaseEthereumProtocol.d.ts +0 -1
- package/v0/protocol/BaseEthereumProtocol.js +460 -872
- package/v0/protocol/BaseEthereumProtocol.js.map +1 -1
- package/v0/protocol/EthereumAddress.d.ts +0 -1
- package/v0/protocol/EthereumAddress.js +26 -17
- package/v0/protocol/EthereumAddress.js.map +1 -1
- package/v0/protocol/EthereumClassicProtocol.js +6 -23
- package/v0/protocol/EthereumClassicProtocol.js.map +1 -1
- package/v0/protocol/EthereumCryptoClient.js +66 -125
- package/v0/protocol/EthereumCryptoClient.js.map +1 -1
- package/v0/protocol/EthereumProtocol.js +9 -28
- package/v0/protocol/EthereumProtocol.js.map +1 -1
- package/v0/protocol/EthereumProtocolOptions.d.ts +1 -1
- package/v0/protocol/EthereumProtocolOptions.js +36 -117
- package/v0/protocol/EthereumProtocolOptions.js.map +1 -1
- package/v0/protocol/EthereumRopstenProtocol.js +6 -23
- package/v0/protocol/EthereumRopstenProtocol.js.map +1 -1
- package/v0/protocol/clients/info-clients/EtherscanInfoClient.js +86 -186
- package/v0/protocol/clients/info-clients/EtherscanInfoClient.js.map +1 -1
- package/v0/protocol/clients/info-clients/InfoClient.js +3 -4
- package/v0/protocol/clients/info-clients/InfoClient.js.map +1 -1
- package/v0/protocol/clients/node-clients/AirGapNodeClient.js +126 -298
- package/v0/protocol/clients/node-clients/AirGapNodeClient.js.map +1 -1
- package/v0/protocol/clients/node-clients/NodeClient.js +3 -4
- package/v0/protocol/clients/node-clients/NodeClient.js.map +1 -1
- package/v0/protocol/erc20/ERC20.js +4 -4
- package/v0/protocol/erc20/ERC20.js.map +1 -1
- package/v0/protocol/erc20/GenericERC20.js +161 -300
- package/v0/protocol/erc20/GenericERC20.js.map +1 -1
- package/v0/protocol/utils/utils.js +52 -55
- package/v0/protocol/utils/utils.js.map +1 -1
- package/v0/serializer/validators/transaction-validator.js +23 -31
- package/v0/serializer/validators/transaction-validator.js.map +1 -1
- package/v0/serializer/validators/validators.js +25 -73
- package/v0/serializer/validators/validators.js.map +1 -1
- package/v1/block-explorer/EtherscanBlockExplorer.js +12 -61
- package/v1/block-explorer/EtherscanBlockExplorer.js.map +1 -1
- package/v1/clients/crypto/EthereumCryptoClient.js +69 -124
- package/v1/clients/crypto/EthereumCryptoClient.js.map +1 -1
- package/v1/clients/info/EthereumInfoClient.d.ts +1 -1
- package/v1/clients/info/EthereumInfoClient.js +3 -4
- package/v1/clients/info/EthereumInfoClient.js.map +1 -1
- package/v1/clients/info/EtherscanInfoClient.js +84 -162
- package/v1/clients/info/EtherscanInfoClient.js.map +1 -1
- package/v1/clients/node/HttpEthereumNodeClient.d.ts +2 -2
- package/v1/clients/node/HttpEthereumNodeClient.js +164 -375
- package/v1/clients/node/HttpEthereumNodeClient.js.map +1 -1
- package/v1/data/EthereumAddress.js +29 -20
- package/v1/data/EthereumAddress.js.map +1 -1
- package/v1/index.js +14 -14
- package/v1/index.js.map +1 -1
- package/v1/module/EthereumModule.js +43 -112
- package/v1/module/EthereumModule.js.map +1 -1
- package/v1/module.js +2 -3
- package/v1/module.js.map +1 -1
- package/v1/protocol/EthereumBaseProtocol.js +467 -788
- package/v1/protocol/EthereumBaseProtocol.js.map +1 -1
- package/v1/protocol/EthereumProtocol.js +15 -46
- package/v1/protocol/EthereumProtocol.js.map +1 -1
- package/v1/protocol/erc20/ERC20Protocol.js +213 -360
- package/v1/protocol/erc20/ERC20Protocol.js.map +1 -1
- package/v1/protocol/erc20/ERC20Token.d.ts +1 -1
- package/v1/protocol/erc20/ERC20Token.js +35 -113
- package/v1/protocol/erc20/ERC20Token.js.map +1 -1
- package/v1/serializer/v3/schemas/converter/transaction-converter.js +25 -42
- package/v1/serializer/v3/schemas/converter/transaction-converter.js.map +1 -1
- package/v1/serializer/v3/serializer-companion.js +71 -148
- package/v1/serializer/v3/serializer-companion.js.map +1 -1
- package/v1/serializer/v3/validators/transaction-validator.js +14 -17
- package/v1/serializer/v3/validators/transaction-validator.js.map +1 -1
- package/v1/types/crypto.d.ts +1 -1
- package/v1/types/protocol.d.ts +1 -1
- package/v1/types/transaction.d.ts +1 -1
- package/v1/utils/EthereumUtils.js +57 -60
- package/v1/utils/EthereumUtils.js.map +1 -1
- package/v1/utils/key.js +28 -19
- package/v1/utils/key.js.map +1 -1
- package/v1/utils/protocol.js +33 -21
- package/v1/utils/protocol.js.map +1 -1
|
@@ -1,15 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
14
3
|
if (k2 === undefined) k2 = k;
|
|
15
4
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
@@ -26,83 +15,56 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
26
15
|
}) : function(o, v) {
|
|
27
16
|
o["default"] = v;
|
|
28
17
|
});
|
|
29
|
-
var __importStar = (this && this.__importStar) || function (
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
};
|
|
45
|
-
|
|
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
|
-
};
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
72
35
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
73
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
74
37
|
};
|
|
75
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
76
39
|
exports.BaseEthereumProtocol = void 0;
|
|
77
|
-
|
|
40
|
+
const bignumber_1 = require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber");
|
|
78
41
|
// @ts-ignore
|
|
79
|
-
|
|
42
|
+
const bip39_2_5_0_1 = require("@airgap/coinlib-core/dependencies/src/bip39-2.5.0");
|
|
80
43
|
// @ts-ignore
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
44
|
+
const bitcoinJS = __importStar(require("@airgap/coinlib-core/dependencies/src/bitgo-utxo-lib-5d91049fd7a988382df81c8260e244ee56d57aac/src"));
|
|
45
|
+
const errors_1 = require("@airgap/coinlib-core/errors");
|
|
46
|
+
const coinlib_error_1 = require("@airgap/coinlib-core/errors/coinlib-error");
|
|
47
|
+
const IAirGapTransaction_1 = require("@airgap/coinlib-core/interfaces/IAirGapTransaction");
|
|
48
|
+
const ICoinSubProtocol_1 = require("@airgap/coinlib-core/protocols/ICoinSubProtocol");
|
|
49
|
+
const ProtocolSymbols_1 = require("@airgap/coinlib-core/utils/ProtocolSymbols");
|
|
50
|
+
const common_1 = __importDefault(require("@ethereumjs/common"));
|
|
88
51
|
// TODO: ETH TX and ethereumjs-util-5.2.0 removed
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
if (options === void 0) { options = new EthereumProtocolOptions_1.EthereumProtocolOptions(); }
|
|
52
|
+
const tx_1 = require("@ethereumjs/tx");
|
|
53
|
+
const EthereumAddress_1 = require("./EthereumAddress");
|
|
54
|
+
const EthereumChainIDs_1 = require("./EthereumChainIDs");
|
|
55
|
+
const EthereumCryptoClient_1 = require("./EthereumCryptoClient");
|
|
56
|
+
const EthereumProtocolOptions_1 = require("./EthereumProtocolOptions");
|
|
57
|
+
const utils_1 = require("./utils/utils");
|
|
58
|
+
class BaseEthereumProtocol {
|
|
59
|
+
constructor(options = new EthereumProtocolOptions_1.EthereumProtocolOptions()) {
|
|
98
60
|
this.options = options;
|
|
99
61
|
this.symbol = 'ETH';
|
|
100
62
|
this.name = 'Ethereum';
|
|
101
63
|
this.marketSymbol = 'eth';
|
|
102
64
|
this.feeSymbol = 'eth';
|
|
103
65
|
this.feeDefaults = {
|
|
104
|
-
low: '0.00021',
|
|
105
|
-
medium: '0.000315',
|
|
66
|
+
low: '0.00021', // 21000 Gas * 10 Gwei
|
|
67
|
+
medium: '0.000315', // 21000 Gas * 15 Gwei
|
|
106
68
|
high: '0.00084' // 21000 Gas * 40 Gwei
|
|
107
69
|
};
|
|
108
70
|
this.decimals = 18;
|
|
@@ -124,823 +86,449 @@ var BaseEthereumProtocol = /** @class */ (function () {
|
|
|
124
86
|
}
|
|
125
87
|
];
|
|
126
88
|
this.supportsHD = false;
|
|
127
|
-
this.standardDerivationPath =
|
|
89
|
+
this.standardDerivationPath = `m/44'/60'/0'/0/0`;
|
|
128
90
|
this.addressIsCaseSensitive = false;
|
|
129
91
|
this.addressValidationPattern = '^0x[a-fA-F0-9]{40}$';
|
|
130
92
|
this.addressPlaceholder = '0xabc...';
|
|
131
93
|
this.cryptoClient = new EthereumCryptoClient_1.EthereumCryptoClient(this);
|
|
132
94
|
this.network = bitcoinJS.networks.bitcoin;
|
|
133
95
|
}
|
|
134
|
-
|
|
135
|
-
return
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
return
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
return
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
return
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
return
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
return
|
|
309
|
-
|
|
310
|
-
|
|
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*/, [
|
|
96
|
+
async getSymbol() {
|
|
97
|
+
return this.symbol;
|
|
98
|
+
}
|
|
99
|
+
async getName() {
|
|
100
|
+
return this.name;
|
|
101
|
+
}
|
|
102
|
+
async getMarketSymbol() {
|
|
103
|
+
return this.marketSymbol;
|
|
104
|
+
}
|
|
105
|
+
async getAssetSymbol() {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
async getFeeSymbol() {
|
|
109
|
+
return this.feeSymbol;
|
|
110
|
+
}
|
|
111
|
+
async getFeeDefaults() {
|
|
112
|
+
return this.feeDefaults;
|
|
113
|
+
}
|
|
114
|
+
async getDecimals() {
|
|
115
|
+
return this.decimals;
|
|
116
|
+
}
|
|
117
|
+
async getFeeDecimals() {
|
|
118
|
+
return this.feeDecimals;
|
|
119
|
+
}
|
|
120
|
+
async getIdentifier() {
|
|
121
|
+
return this.identifier;
|
|
122
|
+
}
|
|
123
|
+
async getUnits() {
|
|
124
|
+
return this.units;
|
|
125
|
+
}
|
|
126
|
+
async getSupportsHD() {
|
|
127
|
+
return this.supportsHD;
|
|
128
|
+
}
|
|
129
|
+
async getStandardDerivationPath() {
|
|
130
|
+
return this.standardDerivationPath;
|
|
131
|
+
}
|
|
132
|
+
async getAddressIsCaseSensitive() {
|
|
133
|
+
return this.addressIsCaseSensitive;
|
|
134
|
+
}
|
|
135
|
+
async getAddressValidationPattern() {
|
|
136
|
+
return this.addressValidationPattern;
|
|
137
|
+
}
|
|
138
|
+
async getAddressPlaceholder() {
|
|
139
|
+
return this.addressPlaceholder;
|
|
140
|
+
}
|
|
141
|
+
async getOptions() {
|
|
142
|
+
return this.options;
|
|
143
|
+
}
|
|
144
|
+
async getBlockExplorerLinkForAddress(address) {
|
|
145
|
+
return this.options.network.blockExplorer.getAddressLink(address);
|
|
146
|
+
}
|
|
147
|
+
async getBlockExplorerLinkForTxId(txId) {
|
|
148
|
+
return this.options.network.blockExplorer.getTransactionLink(txId);
|
|
149
|
+
}
|
|
150
|
+
async getPublicKeyFromMnemonic(mnemonic, derivationPath, password) {
|
|
151
|
+
const secret = (0, bip39_2_5_0_1.mnemonicToSeed)(mnemonic, password);
|
|
152
|
+
return this.getPublicKeyFromHexSecret(secret, derivationPath);
|
|
153
|
+
}
|
|
154
|
+
async getPrivateKeyFromMnemonic(mnemonic, derivationPath, password) {
|
|
155
|
+
const secret = (0, bip39_2_5_0_1.mnemonicToSeed)(mnemonic, password);
|
|
156
|
+
return this.getPrivateKeyFromHexSecret(secret, derivationPath);
|
|
157
|
+
}
|
|
158
|
+
async getExtendedPrivateKeyFromMnemonic(mnemonic, derivationPath, password) {
|
|
159
|
+
const secret = (0, bip39_2_5_0_1.mnemonicToSeed)(mnemonic, password);
|
|
160
|
+
return this.getExtendedPrivateKeyFromHexSecret(secret, derivationPath);
|
|
161
|
+
}
|
|
162
|
+
async getExtendedPublicKeyFromMnemonic(mnemonic, derivationPath, password) {
|
|
163
|
+
const secret = (0, bip39_2_5_0_1.mnemonicToSeed)(mnemonic, password);
|
|
164
|
+
return this.getExtendedPublicKeyFromHexSecret(secret, derivationPath);
|
|
165
|
+
}
|
|
166
|
+
async getPublicKeyFromHexSecret(secret, derivationPath) {
|
|
167
|
+
const ethereumNode = bitcoinJS.HDNode.fromSeedHex(secret, this.network);
|
|
168
|
+
return ethereumNode.derivePath(derivationPath).neutered().getPublicKeyBuffer().toString('hex');
|
|
169
|
+
}
|
|
170
|
+
async getExtendedPublicKeyFromHexSecret(secret, derivationPath) {
|
|
171
|
+
const ethereumNode = bitcoinJS.HDNode.fromSeedHex(secret, this.network);
|
|
172
|
+
return ethereumNode.derivePath(derivationPath).neutered().toBase58();
|
|
173
|
+
}
|
|
174
|
+
async getPrivateKeyFromHexSecret(secret, derivationPath) {
|
|
175
|
+
const ethereumNode = bitcoinJS.HDNode.fromSeedHex(secret, this.network);
|
|
176
|
+
return ethereumNode.derivePath(derivationPath).keyPair.d.toBuffer(32).toString('hex');
|
|
177
|
+
}
|
|
178
|
+
async getExtendedPrivateKeyFromHexSecret(secret, derivationPath) {
|
|
179
|
+
const ethereumNode = bitcoinJS.HDNode.fromSeedHex(secret, this.network);
|
|
180
|
+
return ethereumNode.derivePath(derivationPath).toBase58();
|
|
181
|
+
}
|
|
182
|
+
async getAddressFromPublicKey(publicKey, cursor) {
|
|
183
|
+
const address = EthereumAddress_1.EthereumAddress.from(publicKey);
|
|
184
|
+
return {
|
|
185
|
+
address: address.asString(),
|
|
186
|
+
cursor: { hasNext: false }
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
async getAddressesFromPublicKey(publicKey, cursor) {
|
|
190
|
+
const address = await this.getAddressFromPublicKey(publicKey, cursor);
|
|
191
|
+
return [address];
|
|
192
|
+
}
|
|
193
|
+
async getPublicKeyFromExtendedPublicKey(extendedPublicKey, visibilityDerivationIndex, addressDerivationIndex) {
|
|
194
|
+
return bitcoinJS.HDNode.fromBase58(extendedPublicKey, this.network)
|
|
195
|
+
.derive(visibilityDerivationIndex)
|
|
196
|
+
.derive(addressDerivationIndex)
|
|
197
|
+
.getPublicKeyBuffer()
|
|
198
|
+
.toString('hex');
|
|
199
|
+
}
|
|
200
|
+
async getAddressFromExtendedPublicKey(extendedPublicKey, visibilityDerivationIndex, addressDerivationIndex) {
|
|
201
|
+
return this.getAddressFromPublicKey(await this.getPublicKeyFromExtendedPublicKey(extendedPublicKey, visibilityDerivationIndex, addressDerivationIndex));
|
|
202
|
+
}
|
|
203
|
+
getAddressesFromExtendedPublicKey(extendedPublicKey, visibilityDerivationIndex, addressCount, offset) {
|
|
204
|
+
const node = bitcoinJS.HDNode.fromBase58(extendedPublicKey, this.network);
|
|
205
|
+
const generatorArray = [addressCount].map((x, i) => i + offset);
|
|
206
|
+
return Promise.all(generatorArray.map((x) => this.getAddressFromPublicKey(node.derive(visibilityDerivationIndex).derive(x).getPublicKeyBuffer().toString('hex'))));
|
|
207
|
+
}
|
|
208
|
+
async getPrivateKeyFromExtendedPrivateKey(extendedPrivateKey, childDerivationPath) {
|
|
209
|
+
const dp = childDerivationPath ?? '0/0'; // This is the default
|
|
210
|
+
if (dp.startsWith('m')) {
|
|
211
|
+
throw new Error('Received full derivation path, expected child derivation path');
|
|
212
|
+
}
|
|
213
|
+
if (dp.toLowerCase().includes('h') || dp.includes(`'`)) {
|
|
214
|
+
throw new Error('Child derivation path cannot include hardened children');
|
|
215
|
+
}
|
|
216
|
+
return dp
|
|
217
|
+
.split('/')
|
|
218
|
+
.reduce((pv, cv) => pv.derive(Number(cv)), bitcoinJS.HDNode.fromBase58(extendedPrivateKey, this.network))
|
|
219
|
+
.keyPair.d.toBuffer(32);
|
|
220
|
+
}
|
|
221
|
+
async signWithExtendedPrivateKey(extendedPrivateKey, untypedTransaction, childDerivationPath) {
|
|
222
|
+
const privateKey = await this.getPrivateKeyFromExtendedPrivateKey(extendedPrivateKey, childDerivationPath);
|
|
223
|
+
if (untypedTransaction.serialized &&
|
|
224
|
+
untypedTransaction.derivationPath) {
|
|
225
|
+
const transaction = untypedTransaction;
|
|
226
|
+
let tx = tx_1.TransactionFactory.fromSerializedData(Buffer.from(transaction.serialized, 'hex'));
|
|
227
|
+
tx = tx.sign(privateKey);
|
|
228
|
+
return tx.serialize().toString('hex');
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
return this.signWithPrivateKey(privateKey.toString('hex'), untypedTransaction);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
async signWithPrivateKey(privateKey, transaction) {
|
|
235
|
+
if (!transaction.value.startsWith('0x')) {
|
|
236
|
+
transaction.value = utils_1.EthereumUtils.toHex(parseInt(transaction.value, 10));
|
|
237
|
+
}
|
|
238
|
+
let common;
|
|
239
|
+
try {
|
|
240
|
+
common = new common_1.default({ chain: transaction.chainId });
|
|
241
|
+
}
|
|
242
|
+
catch {
|
|
243
|
+
common = common_1.default.custom({ chainId: transaction.chainId });
|
|
244
|
+
}
|
|
245
|
+
let tx = tx_1.TransactionFactory.fromTxData(transaction, { common });
|
|
246
|
+
tx = tx.sign(Buffer.from(privateKey, 'hex'));
|
|
247
|
+
return tx.serialize().toString('hex');
|
|
248
|
+
}
|
|
249
|
+
async getTransactionDetails(unsignedTx) {
|
|
250
|
+
if (unsignedTx.transaction.serialized) {
|
|
251
|
+
const typedTransaction = unsignedTx.transaction;
|
|
252
|
+
const transaction = tx_1.TransactionFactory.fromSerializedData(Buffer.from(typedTransaction.serialized, 'hex'));
|
|
253
|
+
const dps = typedTransaction.derivationPath.split('/');
|
|
254
|
+
const ownAddress = unsignedTx.publicKey.startsWith('x') // xPub
|
|
255
|
+
? await this.getAddressFromExtendedPublicKey(unsignedTx.publicKey, Number(dps[dps.length - 2]), Number(dps[dps.length - 1]))
|
|
256
|
+
: await this.getAddressFromPublicKey(unsignedTx.publicKey);
|
|
257
|
+
const airGapTransaction = {
|
|
258
|
+
from: [ownAddress.address],
|
|
259
|
+
to: [transaction.to?.toString() ?? ''],
|
|
260
|
+
amount: new bignumber_1.BigNumber(transaction.value.toString(10)).toString(10),
|
|
261
|
+
fee: new bignumber_1.BigNumber(transaction.gasLimit.toString(10))
|
|
262
|
+
.multipliedBy(new bignumber_1.BigNumber(transaction.maxFeePerGas.toString(10)))
|
|
263
|
+
.toString(10),
|
|
264
|
+
protocolIdentifier: this.identifier,
|
|
265
|
+
network: this.options.network,
|
|
266
|
+
isInbound: false,
|
|
267
|
+
data: transaction.data.toString('hex'),
|
|
268
|
+
transactionDetails: unsignedTx
|
|
269
|
+
};
|
|
270
|
+
return [
|
|
271
|
+
{
|
|
272
|
+
...airGapTransaction,
|
|
273
|
+
...(transaction.chainId.toNumber() !== 1
|
|
274
|
+
? {
|
|
275
|
+
warnings: [
|
|
514
276
|
{
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
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
|
|
277
|
+
type: IAirGapTransaction_1.AirGapTransactionWarningType.WARNING,
|
|
278
|
+
title: 'Chain ID',
|
|
279
|
+
description: `Please note that this is not an Ethereum Mainnet transaction, it is from ${EthereumChainIDs_1.EthereumChainIDs.get(transaction.chainId.toNumber()) ?? `Chain ID ${transaction.chainId.toNumber()}`}`
|
|
524
280
|
}
|
|
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')];
|
|
281
|
+
]
|
|
766
282
|
}
|
|
767
|
-
|
|
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
|
-
}];
|
|
283
|
+
: {})
|
|
789
284
|
}
|
|
790
|
-
|
|
791
|
-
}
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
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');
|
|
285
|
+
];
|
|
286
|
+
}
|
|
287
|
+
else {
|
|
288
|
+
const transaction = unsignedTx.transaction;
|
|
289
|
+
const ownAddress = unsignedTx.publicKey.startsWith('x') // xPub
|
|
290
|
+
? await this.getAddressFromExtendedPublicKey(unsignedTx.publicKey, 0, 0)
|
|
291
|
+
: await this.getAddressFromPublicKey(unsignedTx.publicKey);
|
|
292
|
+
return [
|
|
293
|
+
{
|
|
294
|
+
from: [ownAddress.address],
|
|
295
|
+
to: [transaction.to],
|
|
296
|
+
amount: new bignumber_1.BigNumber(transaction.value).toString(10),
|
|
297
|
+
fee: new bignumber_1.BigNumber(transaction.gasLimit).multipliedBy(new bignumber_1.BigNumber(transaction.gasPrice)).toString(10),
|
|
298
|
+
protocolIdentifier: this.identifier,
|
|
299
|
+
network: this.options.network,
|
|
300
|
+
isInbound: false,
|
|
301
|
+
data: transaction.data,
|
|
302
|
+
transactionDetails: unsignedTx
|
|
833
303
|
}
|
|
834
|
-
|
|
835
|
-
}
|
|
836
|
-
}
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
304
|
+
];
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
async getTransactionDetailsFromSigned(transaction) {
|
|
308
|
+
const ethTx = tx_1.TransactionFactory.fromSerializedData(Buffer.from(transaction.transaction, 'hex'));
|
|
309
|
+
if (ethTx.type === 0) {
|
|
310
|
+
const tx = ethTx;
|
|
311
|
+
const hexValue = tx.value.toString('hex') || '0x0';
|
|
312
|
+
const hexGasPrice = tx.gasPrice.toString('hex') || '0x0';
|
|
313
|
+
const hexGasLimit = tx.gasLimit.toString('hex') || '0x0';
|
|
314
|
+
const hexNonce = tx.nonce.toString('hex') || '0x0';
|
|
315
|
+
const chainId = tx.common.chainIdBN().toString(10);
|
|
316
|
+
const to = tx.to;
|
|
317
|
+
if (!to) {
|
|
318
|
+
throw new Error('No "TO" address');
|
|
319
|
+
}
|
|
320
|
+
return [
|
|
321
|
+
{
|
|
322
|
+
from: [tx.getSenderAddress().toString()],
|
|
323
|
+
to: [to.toString()],
|
|
324
|
+
amount: new bignumber_1.BigNumber(parseInt(hexValue, 16)).toString(10),
|
|
325
|
+
fee: new bignumber_1.BigNumber(parseInt(hexGasLimit, 16)).multipliedBy(new bignumber_1.BigNumber(parseInt(hexGasPrice, 16))).toString(10),
|
|
326
|
+
protocolIdentifier: this.identifier,
|
|
327
|
+
network: this.options.network,
|
|
328
|
+
isInbound: tx.toCreationAddress(),
|
|
329
|
+
hash: `0x${tx.hash().toString('hex')}`,
|
|
330
|
+
data: `0x${tx.data.toString('hex')}`,
|
|
331
|
+
extra: {
|
|
332
|
+
chainId,
|
|
333
|
+
nonce: parseInt(hexNonce, 16)
|
|
334
|
+
},
|
|
335
|
+
transactionDetails: { raw: transaction.transaction }
|
|
853
336
|
}
|
|
854
|
-
|
|
855
|
-
}
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
337
|
+
];
|
|
338
|
+
}
|
|
339
|
+
try {
|
|
340
|
+
const feeTx = ethTx;
|
|
341
|
+
return [
|
|
342
|
+
{
|
|
343
|
+
from: [feeTx.getSenderAddress().toString()],
|
|
344
|
+
to: [feeTx.to?.toString() ?? ''],
|
|
345
|
+
amount: new bignumber_1.BigNumber(feeTx.value.toString(10)).toString(10),
|
|
346
|
+
fee: new bignumber_1.BigNumber(feeTx.gasLimit.toString(10)).multipliedBy(new bignumber_1.BigNumber(feeTx.maxFeePerGas.toString(10))).toString(10),
|
|
347
|
+
protocolIdentifier: this.identifier,
|
|
348
|
+
network: this.options.network,
|
|
349
|
+
isInbound: false,
|
|
350
|
+
data: feeTx.data.toString('hex'),
|
|
351
|
+
extra: {
|
|
352
|
+
chainId: feeTx.chainId.toString(10),
|
|
353
|
+
nonce: feeTx.nonce.toString(10)
|
|
354
|
+
},
|
|
355
|
+
transactionDetails: { raw: transaction.transaction }
|
|
867
356
|
}
|
|
868
|
-
|
|
869
|
-
}
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
357
|
+
];
|
|
358
|
+
}
|
|
359
|
+
catch (e) {
|
|
360
|
+
throw new Error(`Transaction type "${ethTx.type}" not supported`);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
async getBalanceOfPublicKey(publicKey) {
|
|
364
|
+
const address = await this.getAddressFromPublicKey(publicKey);
|
|
365
|
+
return this.getBalanceOfAddresses([address.address]);
|
|
366
|
+
}
|
|
367
|
+
async getBalanceOfAddresses(addresses) {
|
|
368
|
+
const balances = await Promise.all(addresses.map((address) => {
|
|
369
|
+
return this.options.nodeClient.fetchBalance(address);
|
|
370
|
+
}));
|
|
371
|
+
return balances.reduce((a, b) => a.plus(b)).toString(10);
|
|
372
|
+
}
|
|
373
|
+
async getBalanceOfPublicKeyForSubProtocols(publicKey, subProtocols) {
|
|
374
|
+
const address = await this.getAddressFromPublicKey(publicKey)
|
|
375
|
+
.then((address) => address.address)
|
|
376
|
+
.catch(async () => await this.getAddressFromExtendedPublicKey(publicKey, 0, 0).then((address) => address.address));
|
|
377
|
+
const contractAddresses = await Promise.all(subProtocols.map(async (subProtocol) => {
|
|
378
|
+
const subProtocolType = await subProtocol.getSubProtocolType();
|
|
379
|
+
const subProtocolContractAddress = await subProtocol.getContractAddress();
|
|
380
|
+
if (subProtocolType === ICoinSubProtocol_1.SubProtocolType.TOKEN && subProtocolContractAddress) {
|
|
381
|
+
return subProtocolContractAddress;
|
|
382
|
+
}
|
|
383
|
+
else {
|
|
384
|
+
throw new errors_1.UnsupportedError(coinlib_error_1.Domain.ETHEREUM, 'can only retrieve balance of ERC20 tokens');
|
|
385
|
+
}
|
|
386
|
+
}));
|
|
387
|
+
const balances = await this.options.nodeClient.callBalanceOfOnContracts(contractAddresses, address);
|
|
388
|
+
return contractAddresses.map((contractAddresse) => balances[contractAddresse]?.toFixed() ?? '0');
|
|
389
|
+
}
|
|
390
|
+
async getBalanceOfExtendedPublicKey(extendedPublicKey, offset = 0) {
|
|
391
|
+
const publicKey = await this.getPublicKeyFromExtendedPublicKey(extendedPublicKey, 0, 0);
|
|
392
|
+
return this.getBalanceOfPublicKey(publicKey);
|
|
393
|
+
}
|
|
394
|
+
async getAvailableBalanceOfAddresses(addresses) {
|
|
395
|
+
return this.getBalanceOfAddresses(addresses);
|
|
396
|
+
}
|
|
397
|
+
async estimateMaxTransactionValueFromExtendedPublicKey(extendedPublicKey, recipients, fee) {
|
|
398
|
+
const publicKey = await this.getPublicKeyFromExtendedPublicKey(extendedPublicKey, 0, 0);
|
|
399
|
+
return this.estimateMaxTransactionValueFromPublicKey(publicKey, recipients, fee);
|
|
400
|
+
}
|
|
401
|
+
async estimateFeeDefaultsFromExtendedPublicKey(extendedPublicKey, recipients, values, data) {
|
|
402
|
+
const publicKey = await this.getPublicKeyFromExtendedPublicKey(extendedPublicKey, 0, 0);
|
|
403
|
+
return this.estimateFeeDefaultsFromPublicKey(publicKey, recipients, values, data);
|
|
404
|
+
}
|
|
405
|
+
async prepareTransactionFromExtendedPublicKey(extendedPublicKey, _offset, recipients, values, fee, data) {
|
|
406
|
+
const publicKey = await this.getPublicKeyFromExtendedPublicKey(extendedPublicKey, 0, 0);
|
|
407
|
+
return this.prepareTransactionFromPublicKey(publicKey, recipients, values, fee, data);
|
|
408
|
+
}
|
|
409
|
+
async estimateMaxTransactionValueFromPublicKey(publicKey, recipients, fee) {
|
|
410
|
+
const balance = await this.getBalanceOfPublicKey(publicKey);
|
|
411
|
+
const balanceWrapper = new bignumber_1.BigNumber(balance);
|
|
412
|
+
let maxFee;
|
|
413
|
+
if (fee !== undefined) {
|
|
414
|
+
maxFee = new bignumber_1.BigNumber(fee);
|
|
415
|
+
}
|
|
416
|
+
else {
|
|
417
|
+
const estimatedFeeDefaults = await this.estimateFeeDefaultsFromPublicKey(publicKey, recipients, [balance]);
|
|
418
|
+
maxFee = new bignumber_1.BigNumber(estimatedFeeDefaults.medium).shiftedBy(this.decimals);
|
|
419
|
+
if (maxFee.gte(balanceWrapper)) {
|
|
420
|
+
maxFee = new bignumber_1.BigNumber(0);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
let amountWithoutFees = balanceWrapper.minus(maxFee);
|
|
424
|
+
if (amountWithoutFees.isNegative()) {
|
|
425
|
+
amountWithoutFees = new bignumber_1.BigNumber(0);
|
|
426
|
+
}
|
|
427
|
+
return amountWithoutFees.toFixed();
|
|
428
|
+
}
|
|
429
|
+
async estimateFeeDefaultsFromPublicKey(publicKey, recipients, values, data) {
|
|
430
|
+
if (recipients.length !== values.length) {
|
|
431
|
+
return Promise.reject('recipients length does not match with values');
|
|
432
|
+
}
|
|
433
|
+
if (recipients.length !== 1) {
|
|
434
|
+
return Promise.reject('you cannot have 0 recipients');
|
|
435
|
+
}
|
|
436
|
+
const address = await this.getAddressFromPublicKey(publicKey).then((address) => address.address);
|
|
437
|
+
const estimatedGas = await this.options.nodeClient.estimateTransactionGas(address, recipients[0], utils_1.EthereumUtils.toHex(values[0]), undefined, utils_1.EthereumUtils.toHex(this.MAX_GAS_ESTIMATE));
|
|
438
|
+
const gasPrise = await this.options.nodeClient.getGasPrice();
|
|
439
|
+
const feeStepFactor = new bignumber_1.BigNumber(0.5);
|
|
440
|
+
const estimatedFee = estimatedGas.times(gasPrise);
|
|
441
|
+
const lowFee = estimatedFee.minus(estimatedFee.times(feeStepFactor).integerValue(bignumber_1.BigNumber.ROUND_FLOOR));
|
|
442
|
+
const mediumFee = estimatedFee;
|
|
443
|
+
const highFee = mediumFee.plus(mediumFee.times(feeStepFactor).integerValue(bignumber_1.BigNumber.ROUND_FLOOR));
|
|
444
|
+
return {
|
|
445
|
+
low: lowFee.shiftedBy(-this.feeDecimals).toFixed(),
|
|
446
|
+
medium: mediumFee.shiftedBy(-this.feeDecimals).toFixed(),
|
|
447
|
+
high: highFee.shiftedBy(-this.feeDecimals).toFixed()
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
async prepareTransactionFromPublicKey(publicKey, recipients, values, fee, data) {
|
|
451
|
+
const wrappedValues = values.map((value) => new bignumber_1.BigNumber(value));
|
|
452
|
+
const wrappedFee = new bignumber_1.BigNumber(fee);
|
|
453
|
+
const address = await this.getAddressFromPublicKey(publicKey).then((address) => address.address);
|
|
454
|
+
if (recipients.length !== values.length) {
|
|
455
|
+
return Promise.reject('recipients length does not match with values');
|
|
456
|
+
}
|
|
457
|
+
if (recipients.length !== 1) {
|
|
458
|
+
return Promise.reject('you cannot have 0 recipients');
|
|
459
|
+
}
|
|
460
|
+
const amount = utils_1.EthereumUtils.toHex(wrappedValues[0].toFixed());
|
|
461
|
+
const balance = await this.getBalanceOfPublicKey(publicKey);
|
|
462
|
+
const gasLimit = await this.options.nodeClient.estimateTransactionGas(address, recipients[0], amount, undefined, utils_1.EthereumUtils.toHex(this.MAX_GAS_ESTIMATE));
|
|
463
|
+
const gasPrice = wrappedFee.div(gasLimit).integerValue(bignumber_1.BigNumber.ROUND_CEIL);
|
|
464
|
+
if (new bignumber_1.BigNumber(balance).gte(new bignumber_1.BigNumber(wrappedValues[0].plus(wrappedFee)))) {
|
|
465
|
+
const txCount = await this.options.nodeClient.fetchTransactionCount(address);
|
|
466
|
+
const transaction = {
|
|
467
|
+
nonce: utils_1.EthereumUtils.toHex(txCount),
|
|
468
|
+
gasLimit: utils_1.EthereumUtils.toHex(gasLimit.toFixed()),
|
|
469
|
+
gasPrice: utils_1.EthereumUtils.toHex(gasPrice.toFixed()), // 10 Gwei
|
|
470
|
+
to: recipients[0],
|
|
471
|
+
value: amount,
|
|
472
|
+
chainId: this.options.network.extras.chainID,
|
|
473
|
+
data: '0x'
|
|
474
|
+
};
|
|
475
|
+
return transaction;
|
|
476
|
+
}
|
|
477
|
+
else {
|
|
478
|
+
throw new errors_1.BalanceError(coinlib_error_1.Domain.ETHEREUM, 'not enough balance');
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
async broadcastTransaction(rawTransaction) {
|
|
482
|
+
return this.options.nodeClient.sendSignedTransaction(`0x${rawTransaction}`);
|
|
483
|
+
}
|
|
484
|
+
async getTransactionsFromExtendedPublicKey(extendedPublicKey, limit, cursor) {
|
|
485
|
+
const publicKey = await this.getPublicKeyFromExtendedPublicKey(extendedPublicKey, 0, 0);
|
|
486
|
+
return this.getTransactionsFromPublicKey(publicKey, limit, cursor);
|
|
487
|
+
}
|
|
488
|
+
async getTransactionsFromPublicKey(publicKey, limit = 50, cursor) {
|
|
489
|
+
const address = await this.getAddressFromPublicKey(publicKey);
|
|
490
|
+
return this.getTransactionsFromAddresses([address.address], limit, cursor);
|
|
491
|
+
}
|
|
492
|
+
getTransactionsFromAddresses(addresses, limit, cursor) {
|
|
493
|
+
return new Promise((overallResolve, overallReject) => {
|
|
494
|
+
const promises = [];
|
|
495
|
+
for (const address of addresses) {
|
|
496
|
+
promises.push(this.options.infoClient.fetchTransactions(this, address, limit, cursor));
|
|
878
497
|
}
|
|
879
498
|
Promise.all(promises)
|
|
880
|
-
.then(
|
|
881
|
-
|
|
882
|
-
overallResolve(values.reduce(
|
|
883
|
-
return { transactions: a.transactions.concat(b.transactions), cursor: { page
|
|
499
|
+
.then((values) => {
|
|
500
|
+
const page = Math.max(...values.map((txResult) => txResult.cursor.page));
|
|
501
|
+
overallResolve(values.reduce((a, b) => {
|
|
502
|
+
return { transactions: a.transactions.concat(b.transactions), cursor: { page } };
|
|
884
503
|
}));
|
|
885
504
|
})
|
|
886
505
|
.catch(overallReject);
|
|
887
506
|
});
|
|
888
|
-
}
|
|
889
|
-
|
|
890
|
-
return
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
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
|
-
});
|
|
507
|
+
}
|
|
508
|
+
async signMessage(message, keypair) {
|
|
509
|
+
return this.cryptoClient.signMessage(message, keypair);
|
|
510
|
+
}
|
|
511
|
+
async verifyMessage(message, signature, publicKey) {
|
|
512
|
+
return this.cryptoClient.verifyMessage(message, signature, publicKey);
|
|
513
|
+
}
|
|
514
|
+
async encryptAsymmetric(message, publicKey) {
|
|
515
|
+
return this.cryptoClient.encryptAsymmetric(message, publicKey);
|
|
516
|
+
}
|
|
517
|
+
async decryptAsymmetric(message, keypair) {
|
|
518
|
+
return this.cryptoClient.decryptAsymmetric(message, keypair);
|
|
519
|
+
}
|
|
520
|
+
async encryptAES(message, privateKey) {
|
|
521
|
+
return this.cryptoClient.encryptAES(message, privateKey);
|
|
522
|
+
}
|
|
523
|
+
async decryptAES(message, privateKey) {
|
|
524
|
+
return this.cryptoClient.decryptAES(message, privateKey);
|
|
525
|
+
}
|
|
526
|
+
async getTransactionStatuses(transactionHashes) {
|
|
527
|
+
const statusPromises = transactionHashes.map((txHash) => {
|
|
528
|
+
return this.options.nodeClient.getTransactionStatus(txHash);
|
|
941
529
|
});
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
}
|
|
530
|
+
return Promise.all(statusPromises);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
945
533
|
exports.BaseEthereumProtocol = BaseEthereumProtocol;
|
|
946
534
|
//# sourceMappingURL=BaseEthereumProtocol.js.map
|