@airgap/stellar 0.13.45-beta.3 → 0.13.45-beta.5

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 (36) hide show
  1. package/package.json +4 -4
  2. package/v1/block-explorer/StellarBlockExplorer.js +61 -12
  3. package/v1/block-explorer/StellarBlockExplorer.js.map +1 -1
  4. package/v1/data/StellarAddress.js +14 -13
  5. package/v1/data/StellarAddress.js.map +1 -1
  6. package/v1/index.js +4 -4
  7. package/v1/index.js.map +1 -1
  8. package/v1/module/StellarModule.js +110 -41
  9. package/v1/module/StellarModule.js.map +1 -1
  10. package/v1/module.js +3 -2
  11. package/v1/module.js.map +1 -1
  12. package/v1/protocol/StellarProtocol.js +859 -591
  13. package/v1/protocol/StellarProtocol.js.map +1 -1
  14. package/v1/protocol/stellarAssets/StellarAsset.js +466 -259
  15. package/v1/protocol/stellarAssets/StellarAsset.js.map +1 -1
  16. package/v1/serializer/v3/schemas/converter/transaction-converter.js +20 -8
  17. package/v1/serializer/v3/schemas/converter/transaction-converter.js.map +1 -1
  18. package/v1/serializer/v3/serializer-companion.js +134 -61
  19. package/v1/serializer/v3/serializer-companion.js.map +1 -1
  20. package/v1/serializer/v3/validators/transaction-validator.js +61 -14
  21. package/v1/serializer/v3/validators/transaction-validator.js.map +1 -1
  22. package/v1/serializer/v3/validators/validators.js +62 -18
  23. package/v1/serializer/v3/validators/validators.js.map +1 -1
  24. package/v1/types/crypto.d.ts +1 -1
  25. package/v1/types/protocol.d.ts +2 -2
  26. package/v1/types/protocol.js +2 -2
  27. package/v1/types/protocol.js.map +1 -1
  28. package/v1/utils/convert.js +7 -6
  29. package/v1/utils/convert.js.map +1 -1
  30. package/v1/utils/key.js +10 -9
  31. package/v1/utils/key.js.map +1 -1
  32. package/v1/utils/signature.js +5 -4
  33. package/v1/utils/signature.js.map +1 -1
  34. package/v1/utils/transaction.d.ts +1 -0
  35. package/v1/utils/transaction.js +10 -9
  36. package/v1/utils/transaction.js.map +1 -1
@@ -1,36 +1,99 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (_) try {
29
+ 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;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
2
49
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
50
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
51
  };
5
52
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.createStellarAssetProtocol = createStellarAssetProtocol;
7
- const coinlib_core_1 = require("@airgap/coinlib-core");
8
- const module_kit_1 = require("@airgap/module-kit");
9
- const protocol_1 = require("../../types/protocol");
10
- const StellarProtocol_1 = require("../StellarProtocol");
11
- const index_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/axios-0.19.0/index"));
12
- const bignumber_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber"));
13
- const errors_1 = require("@airgap/coinlib-core/errors");
14
- const stellar_sdk_1 = require("@stellar/stellar-sdk");
15
- class StellarAssetProtocolImpl {
16
- constructor(metadata, options) {
17
- const completeOptions = (0, StellarProtocol_1.createStellarProtocolOptions)(options.network);
53
+ exports.createStellarAssetProtocol = void 0;
54
+ var coinlib_core_1 = require("@airgap/coinlib-core");
55
+ var module_kit_1 = require("@airgap/module-kit");
56
+ var protocol_1 = require("../../types/protocol");
57
+ var StellarProtocol_1 = require("../StellarProtocol");
58
+ var index_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/axios-0.19.0/index"));
59
+ var bignumber_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber"));
60
+ var errors_1 = require("@airgap/coinlib-core/errors");
61
+ var stellar_sdk_1 = require("@stellar/stellar-sdk");
62
+ var StellarAssetProtocolImpl = /** @class */ (function () {
63
+ function StellarAssetProtocolImpl(metadata, options) {
64
+ var completeOptions = (0, StellarProtocol_1.createStellarProtocolOptions)(options.network);
18
65
  this.metadata = metadata;
19
66
  this.stellar = (0, StellarProtocol_1.createStellarProtocol)(completeOptions);
20
67
  }
21
- async getCryptoConfiguration() {
22
- return this.stellar.getCryptoConfiguration();
23
- }
24
- async getKeyPairFromDerivative(derivative) {
25
- return this.stellar.getKeyPairFromDerivative(derivative);
26
- }
27
- async signTransactionWithSecretKey(transaction, secretKey) {
28
- return this.stellar.signTransactionWithSecretKey(transaction, secretKey);
29
- }
30
- async getAddressFromPublicKey(publicKey) {
31
- return this.stellar.getAddressFromPublicKey(publicKey);
32
- }
33
- getDetailsFromTransaction(transaction, publicKey) {
68
+ StellarAssetProtocolImpl.prototype.getCryptoConfiguration = function () {
69
+ return __awaiter(this, void 0, void 0, function () {
70
+ return __generator(this, function (_a) {
71
+ return [2 /*return*/, this.stellar.getCryptoConfiguration()];
72
+ });
73
+ });
74
+ };
75
+ StellarAssetProtocolImpl.prototype.getKeyPairFromDerivative = function (derivative) {
76
+ return __awaiter(this, void 0, void 0, function () {
77
+ return __generator(this, function (_a) {
78
+ return [2 /*return*/, this.stellar.getKeyPairFromDerivative(derivative)];
79
+ });
80
+ });
81
+ };
82
+ StellarAssetProtocolImpl.prototype.signTransactionWithSecretKey = function (transaction, secretKey) {
83
+ return __awaiter(this, void 0, void 0, function () {
84
+ return __generator(this, function (_a) {
85
+ return [2 /*return*/, this.stellar.signTransactionWithSecretKey(transaction, secretKey)];
86
+ });
87
+ });
88
+ };
89
+ StellarAssetProtocolImpl.prototype.getAddressFromPublicKey = function (publicKey) {
90
+ return __awaiter(this, void 0, void 0, function () {
91
+ return __generator(this, function (_a) {
92
+ return [2 /*return*/, this.stellar.getAddressFromPublicKey(publicKey)];
93
+ });
94
+ });
95
+ };
96
+ StellarAssetProtocolImpl.prototype.getDetailsFromTransaction = function (transaction, publicKey) {
34
97
  switch (transaction.type) {
35
98
  case 'signed':
36
99
  return this.getDetailsFromEncodedTransaction(transaction.transaction, publicKey);
@@ -40,252 +103,396 @@ class StellarAssetProtocolImpl {
40
103
  (0, coinlib_core_1.assertNever)(transaction);
41
104
  throw new errors_1.UnsupportedError(coinlib_core_1.Domain.STELLAR, 'Unsupported transaction type.');
42
105
  }
43
- }
44
- async getDetailsFromEncodedTransaction(transaction, publicKey) {
45
- const transactions = [];
46
- const tx = stellar_sdk_1.TransactionBuilder.fromXDR(transaction, stellar_sdk_1.Networks.PUBLIC);
47
- for (const txDetails of tx.operations) {
48
- let amount;
49
- let op;
50
- let to;
51
- let fee;
52
- let isInbound;
53
- let type;
54
- if (txDetails.type === protocol_1.StellarTransactionType.PAYMENT) {
55
- op = txDetails;
56
- const multiply = new bignumber_1.default(10).exponentiatedBy(Number(this.metadata.decimals));
57
- amount = new bignumber_1.default(op.amount).multipliedBy(multiply).toString();
58
- fee = tx.fee;
59
- to = op.destination;
60
- type = protocol_1.StellarTransactionType.PAYMENT;
61
- isInbound = to.toLowerCase() === publicKey.value.toLowerCase();
62
- }
63
- else if (txDetails.type === protocol_1.StellarTransactionType.CHANGE_TRUST) {
64
- op = txDetails;
65
- amount = new bignumber_1.default(0).toString();
66
- const asetType = op.line;
67
- to = asetType.getIssuer();
68
- fee = new bignumber_1.default(tx.fee).dividedBy(tx.operations.length).toString();
69
- isInbound = to.toLowerCase() === publicKey.value.toLowerCase();
70
- type = protocol_1.StellarTransactionType.CHANGE_TRUST;
71
- }
72
- else {
73
- throw new errors_1.UnsupportedError(coinlib_core_1.Domain.STELLAR, 'Unsupported operation type.');
74
- }
75
- const memo = tx.memo.type === 'text' || tx.memo.type === 'id'
76
- ? tx.memo.value?.toString()
77
- : tx.memo.type === 'hash' || tx.memo.type === 'return'
78
- ? tx.memo.value?.toString('hex')
79
- : undefined;
80
- transactions.push({
81
- from: [tx.source],
82
- to: [to || ''],
83
- isInbound,
84
- amount: (0, module_kit_1.newAmount)(amount, 'blockchain'),
85
- fee: (0, module_kit_1.newAmount)(fee, 'blockchain'),
86
- network: this.stellar.options.network,
87
- arbitraryData: memo,
88
- type,
89
- json: JSON.stringify(tx)
106
+ };
107
+ StellarAssetProtocolImpl.prototype.getDetailsFromEncodedTransaction = function (transaction, publicKey) {
108
+ var _a, _b;
109
+ return __awaiter(this, void 0, void 0, function () {
110
+ var transactions, tx, _i, _c, txDetails, amount, op, to, fee, isInbound, type, multiply, asetType, memo;
111
+ return __generator(this, function (_d) {
112
+ transactions = [];
113
+ tx = stellar_sdk_1.TransactionBuilder.fromXDR(transaction, stellar_sdk_1.Networks.PUBLIC);
114
+ for (_i = 0, _c = tx.operations; _i < _c.length; _i++) {
115
+ txDetails = _c[_i];
116
+ amount = void 0;
117
+ op = void 0;
118
+ to = void 0;
119
+ fee = void 0;
120
+ isInbound = void 0;
121
+ type = void 0;
122
+ if (txDetails.type === protocol_1.StellarTransactionType.PAYMENT) {
123
+ op = txDetails;
124
+ multiply = new bignumber_1.default(10).exponentiatedBy(Number(this.metadata.decimals));
125
+ amount = new bignumber_1.default(op.amount).multipliedBy(multiply).toString();
126
+ fee = tx.fee;
127
+ to = op.destination;
128
+ type = protocol_1.StellarTransactionType.PAYMENT;
129
+ isInbound = to.toLowerCase() === publicKey.value.toLowerCase();
130
+ }
131
+ else if (txDetails.type === protocol_1.StellarTransactionType.CHANGE_TRUST) {
132
+ op = txDetails;
133
+ amount = new bignumber_1.default(0).toString();
134
+ asetType = op.line;
135
+ to = asetType.getIssuer();
136
+ fee = new bignumber_1.default(tx.fee).dividedBy(tx.operations.length).toString();
137
+ isInbound = to.toLowerCase() === publicKey.value.toLowerCase();
138
+ type = protocol_1.StellarTransactionType.CHANGE_TRUST;
139
+ }
140
+ else {
141
+ throw new errors_1.UnsupportedError(coinlib_core_1.Domain.STELLAR, 'Unsupported operation type.');
142
+ }
143
+ memo = tx.memo.type === 'text' || tx.memo.type === 'id'
144
+ ? (_a = tx.memo.value) === null || _a === void 0 ? void 0 : _a.toString()
145
+ : tx.memo.type === 'hash' || tx.memo.type === 'return'
146
+ ? (_b = tx.memo.value) === null || _b === void 0 ? void 0 : _b.toString('hex')
147
+ : undefined;
148
+ transactions.push({
149
+ from: [tx.source],
150
+ to: [to || ''],
151
+ isInbound: isInbound,
152
+ amount: (0, module_kit_1.newAmount)(amount, 'blockchain'),
153
+ fee: (0, module_kit_1.newAmount)(fee, 'blockchain'),
154
+ network: this.stellar.options.network,
155
+ arbitraryData: memo,
156
+ type: type,
157
+ json: JSON.stringify(tx)
158
+ });
159
+ }
160
+ return [2 /*return*/, transactions];
90
161
  });
91
- }
92
- return transactions;
93
- }
94
- async getNetwork() {
95
- return this.stellar.getNetwork();
96
- }
97
- async getTransactionsForPublicKey(publicKey, limit, cursor) {
98
- const address = await this.getAddressFromPublicKey(publicKey);
99
- return this.getTransactionsForAddress(address, limit, cursor);
100
- }
101
- async getBalanceOfPublicKey(publicKey, configuration) {
102
- const address = await this.getAddressFromPublicKey(publicKey);
103
- return this.getBalanceOfAddress(address);
104
- }
105
- async getTransactionMaxAmountWithPublicKey(publicKey, to, configuration) {
106
- try {
107
- const balance = await this.getBalanceOfPublicKey(publicKey);
108
- return (0, module_kit_1.newAmount)(balance.total.value, 'blockchain');
109
- }
110
- catch (error) {
111
- throw new coinlib_core_1.NetworkError(coinlib_core_1.Domain.STELLAR, error);
112
- }
113
- }
114
- async getTransactionFeeWithPublicKey(publicKey, details, configuration) {
115
- return this.stellar.getTransactionFeeWithPublicKey(publicKey, details, configuration);
116
- }
117
- async prepareTransactionWithPublicKey(publicKey, details, configuration) {
118
- const address = await this.getAddressFromPublicKey(publicKey);
119
- const account = await this.stellar.server.loadAccount(address);
120
- const fee = configuration?.fee !== undefined ? configuration?.fee?.value : stellar_sdk_1.BASE_FEE;
121
- const txBuilder = new stellar_sdk_1.TransactionBuilder(account, {
122
- fee,
123
- networkPassphrase: stellar_sdk_1.Networks.PUBLIC
124
162
  });
125
- for (const detail of details) {
126
- let destinationExists = true;
127
- try {
128
- await this.stellar.server.loadAccount(detail.to);
129
- }
130
- catch (e) {
131
- if (e.response?.status === 404) {
132
- destinationExists = false;
163
+ };
164
+ StellarAssetProtocolImpl.prototype.getNetwork = function () {
165
+ return __awaiter(this, void 0, void 0, function () {
166
+ return __generator(this, function (_a) {
167
+ return [2 /*return*/, this.stellar.getNetwork()];
168
+ });
169
+ });
170
+ };
171
+ StellarAssetProtocolImpl.prototype.getTransactionsForPublicKey = function (publicKey, limit, cursor) {
172
+ return __awaiter(this, void 0, void 0, function () {
173
+ var address;
174
+ return __generator(this, function (_a) {
175
+ switch (_a.label) {
176
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
177
+ case 1:
178
+ address = _a.sent();
179
+ return [2 /*return*/, this.getTransactionsForAddress(address, limit, cursor)];
133
180
  }
134
- else {
135
- throw e;
181
+ });
182
+ });
183
+ };
184
+ StellarAssetProtocolImpl.prototype.getBalanceOfPublicKey = function (publicKey, configuration) {
185
+ return __awaiter(this, void 0, void 0, function () {
186
+ var address;
187
+ return __generator(this, function (_a) {
188
+ switch (_a.label) {
189
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
190
+ case 1:
191
+ address = _a.sent();
192
+ return [2 /*return*/, this.getBalanceOfAddress(address)];
136
193
  }
137
- }
138
- const divisor = new bignumber_1.default(10).exponentiatedBy(Number(this.metadata.decimals));
139
- const amount = new bignumber_1.default(detail.amount.value).dividedBy(divisor).toString();
140
- if (destinationExists) {
141
- txBuilder.addOperation(stellar_sdk_1.Operation.payment({
142
- destination: detail.to,
143
- asset: new stellar_sdk_1.Asset(this.metadata.assetCode, this.metadata.issuer),
144
- amount
145
- }));
146
- }
147
- else {
148
- throw new errors_1.UnsupportedError(coinlib_core_1.Domain.STELLAR, `Destination account ${detail.to} does not exist.`);
149
- }
150
- }
151
- if (configuration?.arbitraryData) {
152
- const memo = configuration?.arbitraryData;
153
- txBuilder.addMemo(stellar_sdk_1.Memo.id(memo));
154
- }
155
- const tx = txBuilder.setTimeout(600).build();
156
- return (0, module_kit_1.newUnsignedTransaction)({
157
- transaction: tx.toXDR()
194
+ });
158
195
  });
159
- }
160
- async broadcastTransaction(transaction) {
161
- return this.stellar.broadcastTransaction(transaction);
162
- }
163
- async getTransactionsForAddress(address, limit, cursor) {
164
- try {
165
- const endpoint = cursor?.next ?? `/accounts/${address}/payments?limit=${100}&order=desc&join=transactions`;
166
- const url = `${this.stellar.options.network.rpcUrl}${endpoint}`;
167
- const response = await index_1.default.get(url);
168
- const data = response.data;
169
- const paymentTransactions = data._embedded?.records ?? [];
170
- const divisor = new bignumber_1.default(10).exponentiatedBy(Number(this.metadata.decimals));
171
- const transactions = paymentTransactions
172
- .filter((tx) => (tx.type === 'payment' || tx.type === 'path_payment_strict_send' || tx.type == undefined) &&
173
- tx.asset_code === this.metadata.assetCode &&
174
- tx.asset_issuer === this.metadata.issuer)
175
- .map((tx) => {
176
- const txData = tx.transaction;
177
- const amount = new bignumber_1.default(tx.amount).multipliedBy(divisor).toString();
178
- const isInbound = tx.to.toLowerCase() === address.toLowerCase();
179
- return {
180
- from: [tx.from],
181
- to: [tx.to],
182
- isInbound,
183
- amount: (0, module_kit_1.newAmount)(amount, 'blockchain'),
184
- fee: (0, module_kit_1.newAmount)(txData.fee_charged, 'blockchain'),
185
- network: this.stellar.options.network,
186
- timestamp: Math.floor(new Date(tx.created_at).getTime() / 1000),
187
- status: {
188
- type: tx.transaction_successful ? 'applied' : 'failed',
189
- hash: tx.transaction_hash,
190
- block: txData.ledger.toString()
191
- },
192
- arbitraryData: txData.memo
193
- };
196
+ };
197
+ StellarAssetProtocolImpl.prototype.getTransactionMaxAmountWithPublicKey = function (publicKey, to, configuration) {
198
+ return __awaiter(this, void 0, void 0, function () {
199
+ var balance, error_1;
200
+ return __generator(this, function (_a) {
201
+ switch (_a.label) {
202
+ case 0:
203
+ _a.trys.push([0, 2, , 3]);
204
+ return [4 /*yield*/, this.getBalanceOfPublicKey(publicKey)];
205
+ case 1:
206
+ balance = _a.sent();
207
+ return [2 /*return*/, (0, module_kit_1.newAmount)(balance.total.value, 'blockchain')];
208
+ case 2:
209
+ error_1 = _a.sent();
210
+ throw new coinlib_core_1.NetworkError(coinlib_core_1.Domain.STELLAR, error_1);
211
+ case 3: return [2 /*return*/];
212
+ }
194
213
  });
195
- return {
196
- transactions,
197
- cursor: {
198
- hasNext: data._links?.next?.href !== undefined,
199
- next: data._links?.next?.href?.replace(this.stellar.options.network.rpcUrl, '') ?? ''
214
+ });
215
+ };
216
+ StellarAssetProtocolImpl.prototype.getTransactionFeeWithPublicKey = function (publicKey, details, configuration) {
217
+ return __awaiter(this, void 0, void 0, function () {
218
+ return __generator(this, function (_a) {
219
+ return [2 /*return*/, this.stellar.getTransactionFeeWithPublicKey(publicKey, details, configuration)];
220
+ });
221
+ });
222
+ };
223
+ StellarAssetProtocolImpl.prototype.prepareTransactionWithPublicKey = function (publicKey, details, configuration) {
224
+ var _a, _b;
225
+ return __awaiter(this, void 0, void 0, function () {
226
+ var address, account, fee, txBuilder, _i, details_1, detail, destinationExists, e_1, divisor, amount, memo, tx;
227
+ return __generator(this, function (_c) {
228
+ switch (_c.label) {
229
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
230
+ case 1:
231
+ address = _c.sent();
232
+ return [4 /*yield*/, this.stellar.server.loadAccount(address)];
233
+ case 2:
234
+ account = _c.sent();
235
+ fee = (configuration === null || configuration === void 0 ? void 0 : configuration.fee) !== undefined ? (_a = configuration === null || configuration === void 0 ? void 0 : configuration.fee) === null || _a === void 0 ? void 0 : _a.value : stellar_sdk_1.BASE_FEE;
236
+ txBuilder = new stellar_sdk_1.TransactionBuilder(account, {
237
+ fee: fee,
238
+ networkPassphrase: stellar_sdk_1.Networks.PUBLIC
239
+ });
240
+ _i = 0, details_1 = details;
241
+ _c.label = 3;
242
+ case 3:
243
+ if (!(_i < details_1.length)) return [3 /*break*/, 9];
244
+ detail = details_1[_i];
245
+ destinationExists = true;
246
+ _c.label = 4;
247
+ case 4:
248
+ _c.trys.push([4, 6, , 7]);
249
+ return [4 /*yield*/, this.stellar.server.loadAccount(detail.to)];
250
+ case 5:
251
+ _c.sent();
252
+ return [3 /*break*/, 7];
253
+ case 6:
254
+ e_1 = _c.sent();
255
+ if (((_b = e_1.response) === null || _b === void 0 ? void 0 : _b.status) === 404) {
256
+ destinationExists = false;
257
+ }
258
+ else {
259
+ throw e_1;
260
+ }
261
+ return [3 /*break*/, 7];
262
+ case 7:
263
+ divisor = new bignumber_1.default(10).exponentiatedBy(Number(this.metadata.decimals));
264
+ amount = new bignumber_1.default(detail.amount.value).dividedBy(divisor).toString();
265
+ if (destinationExists) {
266
+ txBuilder.addOperation(stellar_sdk_1.Operation.payment({
267
+ destination: detail.to,
268
+ asset: new stellar_sdk_1.Asset(this.metadata.assetCode, this.metadata.issuer),
269
+ amount: amount
270
+ }));
271
+ }
272
+ else {
273
+ throw new errors_1.UnsupportedError(coinlib_core_1.Domain.STELLAR, "Destination account ".concat(detail.to, " does not exist."));
274
+ }
275
+ _c.label = 8;
276
+ case 8:
277
+ _i++;
278
+ return [3 /*break*/, 3];
279
+ case 9:
280
+ if (configuration === null || configuration === void 0 ? void 0 : configuration.arbitraryData) {
281
+ memo = configuration === null || configuration === void 0 ? void 0 : configuration.arbitraryData;
282
+ txBuilder.addMemo(stellar_sdk_1.Memo.id(memo));
283
+ }
284
+ tx = txBuilder.setTimeout(600).build();
285
+ return [2 /*return*/, (0, module_kit_1.newUnsignedTransaction)({
286
+ transaction: tx.toXDR()
287
+ })];
200
288
  }
201
- };
202
- }
203
- catch (error) {
204
- return {
205
- transactions: [],
206
- cursor: {
207
- hasNext: false,
208
- next: ''
289
+ });
290
+ });
291
+ };
292
+ StellarAssetProtocolImpl.prototype.broadcastTransaction = function (transaction) {
293
+ return __awaiter(this, void 0, void 0, function () {
294
+ return __generator(this, function (_a) {
295
+ return [2 /*return*/, this.stellar.broadcastTransaction(transaction)];
296
+ });
297
+ });
298
+ };
299
+ StellarAssetProtocolImpl.prototype.getTransactionsForAddress = function (address, limit, cursor) {
300
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
301
+ return __awaiter(this, void 0, void 0, function () {
302
+ var endpoint, url, response, data, paymentTransactions, divisor_1, transactions, error_2;
303
+ var _this = this;
304
+ return __generator(this, function (_k) {
305
+ switch (_k.label) {
306
+ case 0:
307
+ _k.trys.push([0, 2, , 3]);
308
+ endpoint = (_a = cursor === null || cursor === void 0 ? void 0 : cursor.next) !== null && _a !== void 0 ? _a : "/accounts/".concat(address, "/payments?limit=").concat(100, "&order=desc&join=transactions");
309
+ url = "".concat(this.stellar.options.network.rpcUrl).concat(endpoint);
310
+ return [4 /*yield*/, index_1.default.get(url)];
311
+ case 1:
312
+ response = _k.sent();
313
+ data = response.data;
314
+ paymentTransactions = (_c = (_b = data._embedded) === null || _b === void 0 ? void 0 : _b.records) !== null && _c !== void 0 ? _c : [];
315
+ divisor_1 = new bignumber_1.default(10).exponentiatedBy(Number(this.metadata.decimals));
316
+ transactions = paymentTransactions
317
+ .filter(function (tx) {
318
+ return (tx.type === 'payment' || tx.type === 'path_payment_strict_send' || tx.type == undefined) &&
319
+ tx.asset_code === _this.metadata.assetCode &&
320
+ tx.asset_issuer === _this.metadata.issuer;
321
+ })
322
+ .map(function (tx) {
323
+ var txData = tx.transaction;
324
+ var amount = new bignumber_1.default(tx.amount).multipliedBy(divisor_1).toString();
325
+ var isInbound = tx.to.toLowerCase() === address.toLowerCase();
326
+ return {
327
+ from: [tx.from],
328
+ to: [tx.to],
329
+ isInbound: isInbound,
330
+ amount: (0, module_kit_1.newAmount)(amount, 'blockchain'),
331
+ fee: (0, module_kit_1.newAmount)(txData.fee_charged, 'blockchain'),
332
+ network: _this.stellar.options.network,
333
+ timestamp: Math.floor(new Date(tx.created_at).getTime() / 1000),
334
+ status: {
335
+ type: tx.transaction_successful ? 'applied' : 'failed',
336
+ hash: tx.transaction_hash,
337
+ block: txData.ledger.toString()
338
+ },
339
+ arbitraryData: txData.memo
340
+ };
341
+ });
342
+ return [2 /*return*/, {
343
+ transactions: transactions,
344
+ cursor: {
345
+ hasNext: ((_e = (_d = data._links) === null || _d === void 0 ? void 0 : _d.next) === null || _e === void 0 ? void 0 : _e.href) !== undefined,
346
+ next: (_j = (_h = (_g = (_f = data._links) === null || _f === void 0 ? void 0 : _f.next) === null || _g === void 0 ? void 0 : _g.href) === null || _h === void 0 ? void 0 : _h.replace(this.stellar.options.network.rpcUrl, '')) !== null && _j !== void 0 ? _j : ''
347
+ }
348
+ }];
349
+ case 2:
350
+ error_2 = _k.sent();
351
+ return [2 /*return*/, {
352
+ transactions: [],
353
+ cursor: {
354
+ hasNext: false,
355
+ next: ''
356
+ }
357
+ }];
358
+ case 3: return [2 /*return*/];
209
359
  }
210
- };
211
- }
212
- }
213
- async getBalanceOfAddress(address, configuration) {
214
- try {
215
- const account = await this.stellar.server.loadAccount(address);
216
- const balanceEntry = account.balances.find((b) => (b.asset_type === 'credit_alphanum4' || b.asset_type === 'credit_alphanum12') &&
217
- b.asset_code === this.metadata.assetCode &&
218
- b.asset_issuer === this.metadata.issuer);
219
- const divisor = new bignumber_1.default(10).exponentiatedBy(Number(this.metadata.decimals));
220
- const btnBalance = new bignumber_1.default(balanceEntry?.balance ?? 0).multipliedBy(divisor).toString();
221
- return {
222
- total: (0, module_kit_1.newAmount)(btnBalance, 'blockchain')
223
- };
224
- }
225
- catch (error) {
226
- return {
227
- total: (0, module_kit_1.newAmount)('0', 'blockchain')
228
- };
229
- }
230
- }
360
+ });
361
+ });
362
+ };
363
+ StellarAssetProtocolImpl.prototype.getBalanceOfAddress = function (address, configuration) {
364
+ var _a;
365
+ return __awaiter(this, void 0, void 0, function () {
366
+ var account, balanceEntry, divisor, btnBalance, error_3;
367
+ var _this = this;
368
+ return __generator(this, function (_b) {
369
+ switch (_b.label) {
370
+ case 0:
371
+ _b.trys.push([0, 2, , 3]);
372
+ return [4 /*yield*/, this.stellar.server.loadAccount(address)];
373
+ case 1:
374
+ account = _b.sent();
375
+ balanceEntry = account.balances.find(function (b) {
376
+ return (b.asset_type === 'credit_alphanum4' || b.asset_type === 'credit_alphanum12') &&
377
+ b.asset_code === _this.metadata.assetCode &&
378
+ b.asset_issuer === _this.metadata.issuer;
379
+ });
380
+ divisor = new bignumber_1.default(10).exponentiatedBy(Number(this.metadata.decimals));
381
+ btnBalance = new bignumber_1.default((_a = balanceEntry === null || balanceEntry === void 0 ? void 0 : balanceEntry.balance) !== null && _a !== void 0 ? _a : 0).multipliedBy(divisor).toString();
382
+ return [2 /*return*/, {
383
+ total: (0, module_kit_1.newAmount)(btnBalance, 'blockchain')
384
+ }];
385
+ case 2:
386
+ error_3 = _b.sent();
387
+ return [2 /*return*/, {
388
+ total: (0, module_kit_1.newAmount)('0', 'blockchain')
389
+ }];
390
+ case 3: return [2 /*return*/];
391
+ }
392
+ });
393
+ });
394
+ };
231
395
  // SubProtocol
232
- async getType() {
233
- return 'token';
234
- }
235
- async mainProtocol() {
236
- return coinlib_core_1.MainProtocolSymbols.STELLAR;
237
- }
238
- async getMetadata() {
239
- const stellarMetadata = await this.stellar.getMetadata();
240
- return {
241
- ...stellarMetadata,
242
- name: this.metadata.name,
243
- identifier: this.metadata.identifier,
244
- units: {
245
- [this.metadata.assetCode]: {
246
- symbol: { value: this.metadata.assetCode },
247
- decimals: this.metadata.decimals
396
+ StellarAssetProtocolImpl.prototype.getType = function () {
397
+ return __awaiter(this, void 0, void 0, function () {
398
+ return __generator(this, function (_a) {
399
+ return [2 /*return*/, 'token'];
400
+ });
401
+ });
402
+ };
403
+ StellarAssetProtocolImpl.prototype.mainProtocol = function () {
404
+ return __awaiter(this, void 0, void 0, function () {
405
+ return __generator(this, function (_a) {
406
+ return [2 /*return*/, coinlib_core_1.MainProtocolSymbols.STELLAR];
407
+ });
408
+ });
409
+ };
410
+ StellarAssetProtocolImpl.prototype.getMetadata = function () {
411
+ return __awaiter(this, void 0, void 0, function () {
412
+ var stellarMetadata;
413
+ var _a;
414
+ return __generator(this, function (_b) {
415
+ switch (_b.label) {
416
+ case 0: return [4 /*yield*/, this.stellar.getMetadata()];
417
+ case 1:
418
+ stellarMetadata = _b.sent();
419
+ return [2 /*return*/, __assign(__assign({}, stellarMetadata), { name: this.metadata.name, identifier: this.metadata.identifier, units: (_a = {},
420
+ _a[this.metadata.assetCode] = {
421
+ symbol: { value: this.metadata.assetCode },
422
+ decimals: this.metadata.decimals
423
+ },
424
+ _a), mainUnit: this.metadata.assetCode })];
248
425
  }
249
- },
250
- mainUnit: this.metadata.assetCode
251
- };
252
- }
253
- async getTrustBalance(publicKey) {
254
- const address = await this.getAddressFromPublicKey(publicKey);
255
- try {
256
- const account = await this.stellar.server.loadAccount(address);
257
- const balanceEntry = account.balances.find((b) => (b.asset_type === 'credit_alphanum4' || b.asset_type === 'credit_alphanum12') &&
258
- b.asset_code === this.metadata.assetCode &&
259
- b.asset_issuer === this.metadata.issuer);
260
- const btnBalance = new bignumber_1.default(balanceEntry?.limit ?? 0).toString();
261
- return {
262
- total: (0, module_kit_1.newAmount)(btnBalance, 'blockchain')
263
- };
264
- }
265
- catch (error) {
266
- return {
267
- total: (0, module_kit_1.newAmount)('0', 'blockchain')
268
- };
269
- }
270
- }
271
- async setTrustline(publicKey, assetMeta, limit) {
272
- const address = await this.getAddressFromPublicKey(publicKey);
273
- const account = await this.stellar.server.loadAccount(address);
274
- const txBuilder = new stellar_sdk_1.TransactionBuilder(account, {
275
- fee: stellar_sdk_1.BASE_FEE,
276
- networkPassphrase: stellar_sdk_1.Networks.PUBLIC
426
+ });
277
427
  });
278
- txBuilder.addOperation(stellar_sdk_1.Operation.changeTrust({
279
- asset: new stellar_sdk_1.Asset(assetMeta.assetCode, assetMeta.issuer),
280
- limit: limit ? limit : undefined
281
- }));
282
- const tx = txBuilder.setTimeout(600).build();
283
- return (0, module_kit_1.newUnsignedTransaction)({
284
- transaction: tx.toXDR()
428
+ };
429
+ StellarAssetProtocolImpl.prototype.getTrustBalance = function (publicKey) {
430
+ var _a;
431
+ return __awaiter(this, void 0, void 0, function () {
432
+ var address, account, balanceEntry, btnBalance, error_4;
433
+ var _this = this;
434
+ return __generator(this, function (_b) {
435
+ switch (_b.label) {
436
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
437
+ case 1:
438
+ address = _b.sent();
439
+ _b.label = 2;
440
+ case 2:
441
+ _b.trys.push([2, 4, , 5]);
442
+ return [4 /*yield*/, this.stellar.server.loadAccount(address)];
443
+ case 3:
444
+ account = _b.sent();
445
+ balanceEntry = account.balances.find(function (b) {
446
+ return (b.asset_type === 'credit_alphanum4' || b.asset_type === 'credit_alphanum12') &&
447
+ b.asset_code === _this.metadata.assetCode &&
448
+ b.asset_issuer === _this.metadata.issuer;
449
+ });
450
+ btnBalance = new bignumber_1.default((_a = balanceEntry === null || balanceEntry === void 0 ? void 0 : balanceEntry.limit) !== null && _a !== void 0 ? _a : 0).toString();
451
+ return [2 /*return*/, {
452
+ total: (0, module_kit_1.newAmount)(btnBalance, 'blockchain')
453
+ }];
454
+ case 4:
455
+ error_4 = _b.sent();
456
+ return [2 /*return*/, {
457
+ total: (0, module_kit_1.newAmount)('0', 'blockchain')
458
+ }];
459
+ case 5: return [2 /*return*/];
460
+ }
461
+ });
285
462
  });
286
- }
287
- }
288
- function createStellarAssetProtocol(metadata, options = {}) {
463
+ };
464
+ StellarAssetProtocolImpl.prototype.setTrustline = function (publicKey, assetMeta, limit) {
465
+ return __awaiter(this, void 0, void 0, function () {
466
+ var address, account, txBuilder, tx;
467
+ return __generator(this, function (_a) {
468
+ switch (_a.label) {
469
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
470
+ case 1:
471
+ address = _a.sent();
472
+ return [4 /*yield*/, this.stellar.server.loadAccount(address)];
473
+ case 2:
474
+ account = _a.sent();
475
+ txBuilder = new stellar_sdk_1.TransactionBuilder(account, {
476
+ fee: stellar_sdk_1.BASE_FEE,
477
+ networkPassphrase: stellar_sdk_1.Networks.PUBLIC
478
+ });
479
+ txBuilder.addOperation(stellar_sdk_1.Operation.changeTrust({
480
+ asset: new stellar_sdk_1.Asset(assetMeta.assetCode, assetMeta.issuer),
481
+ limit: limit ? limit : undefined
482
+ }));
483
+ tx = txBuilder.setTimeout(600).build();
484
+ return [2 /*return*/, (0, module_kit_1.newUnsignedTransaction)({
485
+ transaction: tx.toXDR()
486
+ })];
487
+ }
488
+ });
489
+ });
490
+ };
491
+ return StellarAssetProtocolImpl;
492
+ }());
493
+ function createStellarAssetProtocol(metadata, options) {
494
+ if (options === void 0) { options = {}; }
289
495
  return new StellarAssetProtocolImpl(metadata, options);
290
496
  }
497
+ exports.createStellarAssetProtocol = createStellarAssetProtocol;
291
498
  //# sourceMappingURL=StellarAsset.js.map