@airgap/aeternity 0.13.7-beta.11

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.
@@ -0,0 +1,697 @@
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
+ extendStatics(d, b);
11
+ function __() { this.constructor = d; }
12
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
+ };
14
+ })();
15
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
16
+ if (k2 === undefined) k2 = k;
17
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
18
+ }) : (function(o, m, k, k2) {
19
+ if (k2 === undefined) k2 = k;
20
+ o[k2] = m[k];
21
+ }));
22
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
23
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
24
+ }) : function(o, v) {
25
+ o["default"] = v;
26
+ });
27
+ var __importStar = (this && this.__importStar) || function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
35
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
36
+ return new (P || (P = Promise))(function (resolve, reject) {
37
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
38
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
39
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
40
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
41
+ });
42
+ };
43
+ var __generator = (this && this.__generator) || function (thisArg, body) {
44
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
45
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
46
+ function verb(n) { return function (v) { return step([n, v]); }; }
47
+ function step(op) {
48
+ if (f) throw new TypeError("Generator is already executing.");
49
+ while (_) try {
50
+ 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;
51
+ if (y = 0, t) op = [op[0] & 2, t.value];
52
+ switch (op[0]) {
53
+ case 0: case 1: t = op; break;
54
+ case 4: _.label++; return { value: op[1], done: false };
55
+ case 5: _.label++; y = op[1]; op = [0]; continue;
56
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
57
+ default:
58
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
59
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
60
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
61
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
62
+ if (t[2]) _.ops.pop();
63
+ _.trys.pop(); continue;
64
+ }
65
+ op = body.call(thisArg, _);
66
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
67
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
68
+ }
69
+ };
70
+ var __importDefault = (this && this.__importDefault) || function (mod) {
71
+ return (mod && mod.__esModule) ? mod : { "default": mod };
72
+ };
73
+ Object.defineProperty(exports, "__esModule", { value: true });
74
+ exports.AeternityProtocol = void 0;
75
+ var index_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/axios-0.19.0/index"));
76
+ var bignumber_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber"));
77
+ var index_2 = require("@airgap/coinlib-core/dependencies/src/bip39-2.5.0/index");
78
+ var bs58check = __importStar(require("@airgap/coinlib-core/dependencies/src/bs58check-2.1.2/index"));
79
+ var index_3 = require("@airgap/coinlib-core/dependencies/src/hd-wallet-js-b216450e56954a6e82ace0aade9474673de5d9d5/src/index");
80
+ var rlp = __importStar(require("@airgap/coinlib-core/dependencies/src/rlp-2.2.3/index"));
81
+ var errors_1 = require("@airgap/coinlib-core/errors");
82
+ var coinlib_error_1 = require("@airgap/coinlib-core/errors/coinlib-error");
83
+ var NonExtendedProtocol_1 = require("@airgap/coinlib-core/protocols/NonExtendedProtocol");
84
+ var base64Check_1 = __importDefault(require("@airgap/coinlib-core/utils/base64Check"));
85
+ var hex_1 = require("@airgap/coinlib-core/utils/hex");
86
+ var ProtocolSymbols_1 = require("@airgap/coinlib-core/utils/ProtocolSymbols");
87
+ var ed25519_1 = require("@stablelib/ed25519");
88
+ var AeternityAddress_1 = require("./AeternityAddress");
89
+ var AeternityCryptoClient_1 = require("./AeternityCryptoClient");
90
+ var AeternityProtocolOptions_1 = require("./AeternityProtocolOptions");
91
+ var AeternityProtocol = /** @class */ (function (_super) {
92
+ __extends(AeternityProtocol, _super);
93
+ function AeternityProtocol(options) {
94
+ if (options === void 0) { options = new AeternityProtocolOptions_1.AeternityProtocolOptions(); }
95
+ var _this = _super.call(this) || this;
96
+ _this.options = options;
97
+ _this.symbol = 'AE';
98
+ _this.name = 'æternity';
99
+ _this.marketSymbol = 'ae';
100
+ _this.feeSymbol = 'ae';
101
+ _this.decimals = 18;
102
+ _this.feeDecimals = 18;
103
+ _this.identifier = ProtocolSymbols_1.MainProtocolSymbols.AE;
104
+ _this.feeDefaults = {
105
+ low: '0.00021',
106
+ medium: '0.000315',
107
+ high: '0.00084'
108
+ };
109
+ _this.units = [
110
+ {
111
+ unitSymbol: 'AE',
112
+ factor: '1'
113
+ }
114
+ ];
115
+ _this.supportsHD = false;
116
+ _this.standardDerivationPath = "m/44h/457h/0h/0h/0h";
117
+ _this.addressIsCaseSensitive = true;
118
+ _this.addressValidationPattern = '^ak_+[1-9A-Za-z]{49,50}$';
119
+ _this.addressPlaceholder = 'ak_abc...';
120
+ // ae specifics
121
+ _this.defaultNetworkId = 'ae_mainnet';
122
+ _this.feesURL = 'https://api-airgap.gke.papers.tech/fees';
123
+ _this.cryptoClient = new AeternityCryptoClient_1.AeternityCryptoClient();
124
+ return _this;
125
+ }
126
+ AeternityProtocol.prototype.getSymbol = function () {
127
+ return __awaiter(this, void 0, void 0, function () {
128
+ return __generator(this, function (_a) {
129
+ return [2 /*return*/, this.symbol];
130
+ });
131
+ });
132
+ };
133
+ AeternityProtocol.prototype.getName = function () {
134
+ return __awaiter(this, void 0, void 0, function () {
135
+ return __generator(this, function (_a) {
136
+ return [2 /*return*/, this.name];
137
+ });
138
+ });
139
+ };
140
+ AeternityProtocol.prototype.getMarketSymbol = function () {
141
+ return __awaiter(this, void 0, void 0, function () {
142
+ return __generator(this, function (_a) {
143
+ return [2 /*return*/, this.marketSymbol];
144
+ });
145
+ });
146
+ };
147
+ AeternityProtocol.prototype.getFeeSymbol = function () {
148
+ return __awaiter(this, void 0, void 0, function () {
149
+ return __generator(this, function (_a) {
150
+ return [2 /*return*/, this.feeSymbol];
151
+ });
152
+ });
153
+ };
154
+ AeternityProtocol.prototype.getDecimals = function () {
155
+ return __awaiter(this, void 0, void 0, function () {
156
+ return __generator(this, function (_a) {
157
+ return [2 /*return*/, this.decimals];
158
+ });
159
+ });
160
+ };
161
+ AeternityProtocol.prototype.getFeeDecimals = function () {
162
+ return __awaiter(this, void 0, void 0, function () {
163
+ return __generator(this, function (_a) {
164
+ return [2 /*return*/, this.feeDecimals];
165
+ });
166
+ });
167
+ };
168
+ AeternityProtocol.prototype.getIdentifier = function () {
169
+ return __awaiter(this, void 0, void 0, function () {
170
+ return __generator(this, function (_a) {
171
+ return [2 /*return*/, this.identifier];
172
+ });
173
+ });
174
+ };
175
+ AeternityProtocol.prototype.getFeeDefaults = function () {
176
+ return __awaiter(this, void 0, void 0, function () {
177
+ return __generator(this, function (_a) {
178
+ return [2 /*return*/, this.feeDefaults];
179
+ });
180
+ });
181
+ };
182
+ AeternityProtocol.prototype.getUnits = function () {
183
+ return __awaiter(this, void 0, void 0, function () {
184
+ return __generator(this, function (_a) {
185
+ return [2 /*return*/, this.units];
186
+ });
187
+ });
188
+ };
189
+ AeternityProtocol.prototype.getSupportsHD = function () {
190
+ return __awaiter(this, void 0, void 0, function () {
191
+ return __generator(this, function (_a) {
192
+ return [2 /*return*/, this.supportsHD];
193
+ });
194
+ });
195
+ };
196
+ AeternityProtocol.prototype.getStandardDerivationPath = function () {
197
+ return __awaiter(this, void 0, void 0, function () {
198
+ return __generator(this, function (_a) {
199
+ return [2 /*return*/, this.standardDerivationPath];
200
+ });
201
+ });
202
+ };
203
+ AeternityProtocol.prototype.getAddressIsCaseSensitive = function () {
204
+ return __awaiter(this, void 0, void 0, function () {
205
+ return __generator(this, function (_a) {
206
+ return [2 /*return*/, this.addressIsCaseSensitive];
207
+ });
208
+ });
209
+ };
210
+ AeternityProtocol.prototype.getAddressValidationPattern = function () {
211
+ return __awaiter(this, void 0, void 0, function () {
212
+ return __generator(this, function (_a) {
213
+ return [2 /*return*/, this.addressValidationPattern];
214
+ });
215
+ });
216
+ };
217
+ AeternityProtocol.prototype.getAddressPlaceholder = function () {
218
+ return __awaiter(this, void 0, void 0, function () {
219
+ return __generator(this, function (_a) {
220
+ return [2 /*return*/, this.addressPlaceholder];
221
+ });
222
+ });
223
+ };
224
+ AeternityProtocol.prototype.getOptions = function () {
225
+ return __awaiter(this, void 0, void 0, function () {
226
+ return __generator(this, function (_a) {
227
+ return [2 /*return*/, this.options];
228
+ });
229
+ });
230
+ };
231
+ AeternityProtocol.prototype.getBlockExplorerLinkForAddress = function (address) {
232
+ return __awaiter(this, void 0, void 0, function () {
233
+ return __generator(this, function (_a) {
234
+ return [2 /*return*/, this.options.network.blockExplorer.getAddressLink(address)];
235
+ });
236
+ });
237
+ };
238
+ AeternityProtocol.prototype.getBlockExplorerLinkForTxId = function (txId) {
239
+ return __awaiter(this, void 0, void 0, function () {
240
+ return __generator(this, function (_a) {
241
+ return [2 /*return*/, this.options.network.blockExplorer.getTransactionLink(txId)];
242
+ });
243
+ });
244
+ };
245
+ AeternityProtocol.prototype.getPublicKeyFromMnemonic = function (mnemonic, derivationPath, password) {
246
+ return __awaiter(this, void 0, void 0, function () {
247
+ var secret;
248
+ return __generator(this, function (_a) {
249
+ secret = index_2.mnemonicToSeed(mnemonic, password);
250
+ return [2 /*return*/, this.getPublicKeyFromHexSecret(secret, derivationPath)];
251
+ });
252
+ });
253
+ };
254
+ AeternityProtocol.prototype.getPrivateKeyFromMnemonic = function (mnemonic, derivationPath, password) {
255
+ return __awaiter(this, void 0, void 0, function () {
256
+ var secret;
257
+ return __generator(this, function (_a) {
258
+ secret = index_2.mnemonicToSeed(mnemonic, password);
259
+ return [2 /*return*/, this.getPrivateKeyFromHexSecret(secret, derivationPath)];
260
+ });
261
+ });
262
+ };
263
+ /**
264
+ * Returns the PublicKey as String, derived from a supplied hex-string
265
+ * @param secret HEX-Secret from BIP39
266
+ * @param derivationPath DerivationPath for Key
267
+ */
268
+ AeternityProtocol.prototype.getPublicKeyFromHexSecret = function (secret, derivationPath) {
269
+ return __awaiter(this, void 0, void 0, function () {
270
+ var publicKey;
271
+ return __generator(this, function (_a) {
272
+ publicKey = index_3.generateWalletUsingDerivationPath(Buffer.from(secret, 'hex'), derivationPath).publicKey;
273
+ return [2 /*return*/, Buffer.from(publicKey).toString('hex')];
274
+ });
275
+ });
276
+ };
277
+ /**
278
+ * Returns the PrivateKey as Buffer, derived from a supplied hex-string
279
+ * @param secret HEX-Secret from BIP39
280
+ * @param derivationPath DerivationPath for Key
281
+ */
282
+ AeternityProtocol.prototype.getPrivateKeyFromHexSecret = function (secret, derivationPath) {
283
+ return __awaiter(this, void 0, void 0, function () {
284
+ var secretKey;
285
+ return __generator(this, function (_a) {
286
+ secretKey = index_3.generateWalletUsingDerivationPath(Buffer.from(secret, 'hex'), derivationPath).secretKey;
287
+ return [2 /*return*/, Buffer.from(secretKey).toString('hex')];
288
+ });
289
+ });
290
+ };
291
+ AeternityProtocol.prototype.getAddressFromPublicKey = function (publicKey, cursor) {
292
+ return __awaiter(this, void 0, void 0, function () {
293
+ return __generator(this, function (_a) {
294
+ return [2 /*return*/, {
295
+ address: AeternityAddress_1.AeternityAddress.from(publicKey).asString(),
296
+ cursor: { hasNext: false }
297
+ }];
298
+ });
299
+ });
300
+ };
301
+ AeternityProtocol.prototype.getAddressesFromPublicKey = function (publicKey, cursor) {
302
+ return __awaiter(this, void 0, void 0, function () {
303
+ var address;
304
+ return __generator(this, function (_a) {
305
+ switch (_a.label) {
306
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey, cursor)];
307
+ case 1:
308
+ address = _a.sent();
309
+ return [2 /*return*/, [address]];
310
+ }
311
+ });
312
+ });
313
+ };
314
+ AeternityProtocol.prototype.getTransactionsFromPublicKey = function (publicKey, limit, cursor) {
315
+ return __awaiter(this, void 0, void 0, function () {
316
+ var address;
317
+ return __generator(this, function (_a) {
318
+ switch (_a.label) {
319
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
320
+ case 1:
321
+ address = _a.sent();
322
+ return [2 /*return*/, this.getTransactionsFromAddresses([address.address], limit, cursor)];
323
+ }
324
+ });
325
+ });
326
+ };
327
+ AeternityProtocol.prototype.getTransactionsFromAddresses = function (addresses, limit, cursor) {
328
+ return __awaiter(this, void 0, void 0, function () {
329
+ var allTransactions, transactions;
330
+ var _this = this;
331
+ return __generator(this, function (_a) {
332
+ switch (_a.label) {
333
+ case 0: return [4 /*yield*/, Promise.all(addresses.map(function (address) {
334
+ var url = cursor
335
+ ? _this.options.network.rpcUrl + "/mdw/txs/backward?account=" + address + "&page=" + cursor.page + "&limit=" + limit
336
+ : _this.options.network.rpcUrl + "/mdw/txs/backward?account=" + address + "&page=1&limit=" + limit;
337
+ return index_1.default.get(url);
338
+ }))];
339
+ case 1:
340
+ allTransactions = _a.sent();
341
+ transactions = [].concat.apply([], allTransactions.map(function (axiosData) {
342
+ return axiosData.data.data || [];
343
+ }));
344
+ transactions = transactions.map(function (obj) {
345
+ var parsedTimestamp = parseInt(obj.micro_time, 10);
346
+ var airGapTx = {
347
+ amount: new bignumber_1.default(obj.tx.amount).toString(10),
348
+ fee: new bignumber_1.default(obj.tx.fee).toString(10),
349
+ from: [obj.tx.sender_id],
350
+ isInbound: addresses.indexOf(obj.tx.recipient_id) !== -1,
351
+ protocolIdentifier: _this.identifier,
352
+ network: _this.options.network,
353
+ to: [obj.tx.recipient_id],
354
+ hash: obj.hash,
355
+ blockHeight: obj.block_height
356
+ };
357
+ if (obj.tx.payload) {
358
+ airGapTx.data = obj.tx.payload;
359
+ }
360
+ if (!isNaN(parsedTimestamp)) {
361
+ airGapTx.timestamp = Math.round(parsedTimestamp / 1000);
362
+ }
363
+ return airGapTx;
364
+ });
365
+ return [2 /*return*/, { transactions: transactions, cursor: { page: cursor ? cursor.page + 1 : 2 } }];
366
+ }
367
+ });
368
+ });
369
+ };
370
+ AeternityProtocol.prototype.getPageNumber = function (limit, offset) {
371
+ if (limit <= 0 || offset < 0) {
372
+ return 1;
373
+ }
374
+ return 1 + Math.floor(offset / limit); // We need +1 here because pages start at 1
375
+ };
376
+ AeternityProtocol.prototype.signWithPrivateKey = function (privateKey, transaction) {
377
+ return __awaiter(this, void 0, void 0, function () {
378
+ var rawTx, signature, txObj, txArray, rlpEncodedTx, signedEncodedTx;
379
+ return __generator(this, function (_a) {
380
+ rawTx = this.decodeTx(transaction.transaction);
381
+ signature = ed25519_1.sign(Buffer.from(privateKey, 'hex'), Buffer.concat([Buffer.from(transaction.networkId), rawTx]));
382
+ txObj = {
383
+ tag: hex_1.toHexBuffer(11),
384
+ version: hex_1.toHexBuffer(1),
385
+ signatures: [Buffer.from(signature)],
386
+ transaction: rawTx
387
+ };
388
+ txArray = Object.keys(txObj).map(function (a) { return txObj[a]; });
389
+ rlpEncodedTx = rlp.encode(txArray);
390
+ signedEncodedTx = "tx_" + base64Check_1.default.encode(rlpEncodedTx);
391
+ return [2 /*return*/, signedEncodedTx];
392
+ });
393
+ });
394
+ };
395
+ AeternityProtocol.prototype.decodeTx = function (transaction) {
396
+ var rawTx;
397
+ try {
398
+ rawTx = base64Check_1.default.decode(transaction.replace('tx_', ''));
399
+ return rawTx;
400
+ }
401
+ catch (error) {
402
+ //
403
+ }
404
+ try {
405
+ rawTx = bs58check.decode(transaction.replace('tx_', ''));
406
+ return rawTx;
407
+ }
408
+ catch (error) {
409
+ //
410
+ }
411
+ throw new errors_1.InvalidValueError(coinlib_error_1.Domain.AETERNITY, 'invalid TX-encoding');
412
+ };
413
+ AeternityProtocol.prototype.getTransactionDetails = function (unsignedTx) {
414
+ return __awaiter(this, void 0, void 0, function () {
415
+ var transaction, rlpEncodedTx, rlpDecodedTx, fromAddress, toAddress, airgapTx;
416
+ return __generator(this, function (_a) {
417
+ switch (_a.label) {
418
+ case 0:
419
+ transaction = unsignedTx.transaction.transaction;
420
+ rlpEncodedTx = this.decodeTx(transaction);
421
+ rlpDecodedTx = rlp.decode(rlpEncodedTx, false);
422
+ return [4 /*yield*/, this.getAddressFromPublicKey(rlpDecodedTx[2].slice(1).toString('hex'))];
423
+ case 1:
424
+ fromAddress = _a.sent();
425
+ return [4 /*yield*/, this.getAddressFromPublicKey(rlpDecodedTx[3].slice(1).toString('hex'))];
426
+ case 2:
427
+ toAddress = _a.sent();
428
+ airgapTx = {
429
+ amount: new bignumber_1.default(parseInt(rlpDecodedTx[4].toString('hex'), 16)).toString(10),
430
+ fee: new bignumber_1.default(parseInt(rlpDecodedTx[5].toString('hex'), 16)).toString(10),
431
+ from: [fromAddress.address],
432
+ isInbound: false,
433
+ protocolIdentifier: this.identifier,
434
+ network: this.options.network,
435
+ to: [toAddress.address],
436
+ data: (rlpDecodedTx[8] || '').toString('utf8'),
437
+ transactionDetails: unsignedTx.transaction
438
+ };
439
+ return [2 /*return*/, [airgapTx]];
440
+ }
441
+ });
442
+ });
443
+ };
444
+ AeternityProtocol.prototype.getTransactionDetailsFromSigned = function (signedTx) {
445
+ return __awaiter(this, void 0, void 0, function () {
446
+ var rlpEncodedTx, rlpDecodedTx, unsignedAeternityTransaction;
447
+ return __generator(this, function (_a) {
448
+ rlpEncodedTx = this.decodeTx(signedTx.transaction);
449
+ rlpDecodedTx = rlp.decode(rlpEncodedTx, false);
450
+ unsignedAeternityTransaction = {
451
+ publicKey: '',
452
+ transaction: {
453
+ networkId: 'ae_mainnet',
454
+ transaction: "tx_" + base64Check_1.default.encode(rlpDecodedTx[3])
455
+ }
456
+ };
457
+ return [2 /*return*/, this.getTransactionDetails(unsignedAeternityTransaction)];
458
+ });
459
+ });
460
+ };
461
+ AeternityProtocol.prototype.getBalanceOfAddresses = function (addresses) {
462
+ return __awaiter(this, void 0, void 0, function () {
463
+ var balance, _i, addresses_1, address, data, error_1;
464
+ return __generator(this, function (_a) {
465
+ switch (_a.label) {
466
+ case 0:
467
+ balance = new bignumber_1.default(0);
468
+ _i = 0, addresses_1 = addresses;
469
+ _a.label = 1;
470
+ case 1:
471
+ if (!(_i < addresses_1.length)) return [3 /*break*/, 6];
472
+ address = addresses_1[_i];
473
+ _a.label = 2;
474
+ case 2:
475
+ _a.trys.push([2, 4, , 5]);
476
+ return [4 /*yield*/, index_1.default.get(this.options.network.rpcUrl + "/v2/accounts/" + address)];
477
+ case 3:
478
+ data = (_a.sent()).data;
479
+ balance = balance.plus(new bignumber_1.default(data.balance));
480
+ return [3 /*break*/, 5];
481
+ case 4:
482
+ error_1 = _a.sent();
483
+ // if node returns 404 (which means 'no account found'), go with 0 balance
484
+ if (error_1.response && error_1.response.status !== 404) {
485
+ throw new errors_1.NetworkError(coinlib_error_1.Domain.AETERNITY, error_1);
486
+ }
487
+ return [3 /*break*/, 5];
488
+ case 5:
489
+ _i++;
490
+ return [3 /*break*/, 1];
491
+ case 6: return [2 /*return*/, balance.toString(10)];
492
+ }
493
+ });
494
+ });
495
+ };
496
+ AeternityProtocol.prototype.getBalanceOfPublicKey = function (publicKey) {
497
+ return __awaiter(this, void 0, void 0, function () {
498
+ var address;
499
+ return __generator(this, function (_a) {
500
+ switch (_a.label) {
501
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
502
+ case 1:
503
+ address = _a.sent();
504
+ return [2 /*return*/, this.getBalanceOfAddresses([address.address])];
505
+ }
506
+ });
507
+ });
508
+ };
509
+ AeternityProtocol.prototype.getBalanceOfPublicKeyForSubProtocols = function (publicKey, subProtocols) {
510
+ return __awaiter(this, void 0, void 0, function () {
511
+ return __generator(this, function (_a) {
512
+ throw Promise.reject('get balance of sub protocols not supported');
513
+ });
514
+ });
515
+ };
516
+ AeternityProtocol.prototype.getAvailableBalanceOfAddresses = function (addresses) {
517
+ return __awaiter(this, void 0, void 0, function () {
518
+ return __generator(this, function (_a) {
519
+ return [2 /*return*/, this.getBalanceOfAddresses(addresses)];
520
+ });
521
+ });
522
+ };
523
+ AeternityProtocol.prototype.estimateMaxTransactionValueFromPublicKey = function (publicKey, recipients, fee) {
524
+ return __awaiter(this, void 0, void 0, function () {
525
+ var balance, balanceWrapper, maxFee, estimatedFeeDefaults, amountWithoutFees;
526
+ return __generator(this, function (_a) {
527
+ switch (_a.label) {
528
+ case 0: return [4 /*yield*/, this.getBalanceOfPublicKey(publicKey)];
529
+ case 1:
530
+ balance = _a.sent();
531
+ balanceWrapper = new bignumber_1.default(balance);
532
+ if (!(fee !== undefined)) return [3 /*break*/, 2];
533
+ maxFee = new bignumber_1.default(fee);
534
+ return [3 /*break*/, 4];
535
+ case 2: return [4 /*yield*/, this.estimateFeeDefaultsFromPublicKey(publicKey, recipients, [balance])];
536
+ case 3:
537
+ estimatedFeeDefaults = _a.sent();
538
+ maxFee = new bignumber_1.default(estimatedFeeDefaults.medium).shiftedBy(this.decimals);
539
+ if (maxFee.gte(balanceWrapper)) {
540
+ maxFee = new bignumber_1.default(0);
541
+ }
542
+ _a.label = 4;
543
+ case 4:
544
+ amountWithoutFees = balanceWrapper.minus(maxFee);
545
+ if (amountWithoutFees.isNegative()) {
546
+ amountWithoutFees = new bignumber_1.default(0);
547
+ }
548
+ return [2 /*return*/, amountWithoutFees.toFixed()];
549
+ }
550
+ });
551
+ });
552
+ };
553
+ AeternityProtocol.prototype.estimateFeeDefaultsFromPublicKey = function (publicKey, recipients, values, data) {
554
+ return __awaiter(this, void 0, void 0, function () {
555
+ return __generator(this, function (_a) {
556
+ switch (_a.label) {
557
+ case 0: return [4 /*yield*/, index_1.default.get(this.feesURL)];
558
+ case 1: return [2 /*return*/, (_a.sent()).data];
559
+ }
560
+ });
561
+ });
562
+ };
563
+ AeternityProtocol.prototype.prepareTransactionFromPublicKey = function (publicKey, recipients, values, fee, data) {
564
+ return __awaiter(this, void 0, void 0, function () {
565
+ var nonce, address, accountResponse, error_2, balance, _a, sender, recipient, txObj, txArray, rlpEncodedTx, preparedTx;
566
+ return __generator(this, function (_b) {
567
+ switch (_b.label) {
568
+ case 0:
569
+ nonce = 1;
570
+ return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
571
+ case 1:
572
+ address = _b.sent();
573
+ _b.label = 2;
574
+ case 2:
575
+ _b.trys.push([2, 4, , 5]);
576
+ return [4 /*yield*/, index_1.default.get(this.options.network.rpcUrl + "/v2/accounts/" + address.address)];
577
+ case 3:
578
+ accountResponse = (_b.sent()).data;
579
+ nonce = accountResponse.nonce + 1;
580
+ return [3 /*break*/, 5];
581
+ case 4:
582
+ error_2 = _b.sent();
583
+ // if node returns 404 (which means 'no account found'), go with nonce 0
584
+ if (error_2.response && error_2.response.status !== 404) {
585
+ throw new errors_1.NetworkError(coinlib_error_1.Domain.AETERNITY, error_2);
586
+ }
587
+ return [3 /*break*/, 5];
588
+ case 5:
589
+ _a = bignumber_1.default.bind;
590
+ return [4 /*yield*/, this.getBalanceOfPublicKey(publicKey)];
591
+ case 6:
592
+ balance = new (_a.apply(bignumber_1.default, [void 0, _b.sent()]))();
593
+ if (balance.isLessThan(fee)) {
594
+ throw new errors_1.BalanceError(coinlib_error_1.Domain.AETERNITY, 'not enough balance');
595
+ }
596
+ sender = publicKey;
597
+ recipient = bs58check.decode(recipients[0].replace('ak_', ''));
598
+ txObj = {
599
+ tag: hex_1.toHexBuffer(12),
600
+ version: hex_1.toHexBuffer(1),
601
+ sender_id: Buffer.concat([hex_1.toHexBuffer(1), Buffer.from(sender, 'hex')]),
602
+ recipient_id: Buffer.concat([hex_1.toHexBuffer(1), recipient]),
603
+ amount: hex_1.toHexBuffer(new bignumber_1.default(values[0])),
604
+ fee: hex_1.toHexBuffer(new bignumber_1.default(fee)),
605
+ ttl: hex_1.toHexBuffer(0),
606
+ nonce: hex_1.toHexBuffer(nonce),
607
+ payload: Buffer.from((data === null || data === void 0 ? void 0 : data.payload) || '')
608
+ };
609
+ txArray = Object.keys(txObj).map(function (a) { return txObj[a]; });
610
+ rlpEncodedTx = rlp.encode(txArray);
611
+ preparedTx = "tx_" + base64Check_1.default.encode(rlpEncodedTx);
612
+ return [2 /*return*/, {
613
+ transaction: preparedTx,
614
+ networkId: this.defaultNetworkId
615
+ }];
616
+ }
617
+ });
618
+ });
619
+ };
620
+ /**
621
+ * This is a function that we only use to fix incompatibilitis with old vault versions that are unable to understand b64 encoded Txs.
622
+ *
623
+ * @deprecated
624
+ * @param preparedTx
625
+ */
626
+ AeternityProtocol.prototype.convertTxToBase58 = function (preparedTx) {
627
+ return {
628
+ transaction: bs58check.encode(base64Check_1.default.decode(preparedTx.transaction)),
629
+ networkId: preparedTx.networkId
630
+ };
631
+ };
632
+ AeternityProtocol.prototype.broadcastTransaction = function (rawTransaction) {
633
+ return __awaiter(this, void 0, void 0, function () {
634
+ var data;
635
+ return __generator(this, function (_a) {
636
+ switch (_a.label) {
637
+ case 0: return [4 /*yield*/, index_1.default.post(this.options.network.rpcUrl + "/v2/transactions", { tx: rawTransaction }, { headers: { 'Content-Type': 'application/json' } })];
638
+ case 1:
639
+ data = (_a.sent()).data;
640
+ return [2 /*return*/, data.tx_hash];
641
+ }
642
+ });
643
+ });
644
+ };
645
+ AeternityProtocol.prototype.signMessage = function (message, keypair) {
646
+ return __awaiter(this, void 0, void 0, function () {
647
+ return __generator(this, function (_a) {
648
+ return [2 /*return*/, this.cryptoClient.signMessage(message, keypair)];
649
+ });
650
+ });
651
+ };
652
+ AeternityProtocol.prototype.verifyMessage = function (message, signature, publicKey) {
653
+ return __awaiter(this, void 0, void 0, function () {
654
+ return __generator(this, function (_a) {
655
+ return [2 /*return*/, this.cryptoClient.verifyMessage(message, signature, publicKey)];
656
+ });
657
+ });
658
+ };
659
+ AeternityProtocol.prototype.encryptAsymmetric = function (message, publicKey) {
660
+ return __awaiter(this, void 0, void 0, function () {
661
+ return __generator(this, function (_a) {
662
+ return [2 /*return*/, this.cryptoClient.encryptAsymmetric(message, publicKey)];
663
+ });
664
+ });
665
+ };
666
+ AeternityProtocol.prototype.decryptAsymmetric = function (message, keypair) {
667
+ return __awaiter(this, void 0, void 0, function () {
668
+ return __generator(this, function (_a) {
669
+ return [2 /*return*/, this.cryptoClient.decryptAsymmetric(message, keypair)];
670
+ });
671
+ });
672
+ };
673
+ AeternityProtocol.prototype.encryptAES = function (message, privateKey) {
674
+ return __awaiter(this, void 0, void 0, function () {
675
+ return __generator(this, function (_a) {
676
+ return [2 /*return*/, this.cryptoClient.encryptAES(message, privateKey)];
677
+ });
678
+ });
679
+ };
680
+ AeternityProtocol.prototype.decryptAES = function (message, privateKey) {
681
+ return __awaiter(this, void 0, void 0, function () {
682
+ return __generator(this, function (_a) {
683
+ return [2 /*return*/, this.cryptoClient.decryptAES(message, privateKey)];
684
+ });
685
+ });
686
+ };
687
+ AeternityProtocol.prototype.getTransactionStatuses = function (transactionHashes) {
688
+ return __awaiter(this, void 0, void 0, function () {
689
+ return __generator(this, function (_a) {
690
+ return [2 /*return*/, Promise.reject('Transaction status not implemented')];
691
+ });
692
+ });
693
+ };
694
+ return AeternityProtocol;
695
+ }(NonExtendedProtocol_1.NonExtendedProtocol));
696
+ exports.AeternityProtocol = AeternityProtocol;
697
+ //# sourceMappingURL=AeternityProtocol.js.map