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