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

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