@stellar/stellar-base 10.0.0

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 (80) hide show
  1. package/CHANGELOG.md +1262 -0
  2. package/LICENSE +202 -0
  3. package/README.md +198 -0
  4. package/dist/stellar-base.js +30777 -0
  5. package/dist/stellar-base.min.js +2 -0
  6. package/lib/account.js +80 -0
  7. package/lib/address.js +169 -0
  8. package/lib/asset.js +323 -0
  9. package/lib/auth.js +253 -0
  10. package/lib/claimant.js +193 -0
  11. package/lib/contract.js +113 -0
  12. package/lib/events.js +42 -0
  13. package/lib/fee_bump_transaction.js +134 -0
  14. package/lib/generated/curr_generated.js +8315 -0
  15. package/lib/generated/next_generated.js +8315 -0
  16. package/lib/get_liquidity_pool_id.js +57 -0
  17. package/lib/hashing.js +12 -0
  18. package/lib/index.js +385 -0
  19. package/lib/invocation.js +195 -0
  20. package/lib/keypair.js +308 -0
  21. package/lib/liquidity_pool_asset.js +126 -0
  22. package/lib/liquidity_pool_id.js +101 -0
  23. package/lib/memo.js +270 -0
  24. package/lib/muxed_account.js +159 -0
  25. package/lib/network.js +22 -0
  26. package/lib/numbers/index.js +85 -0
  27. package/lib/numbers/int128.js +50 -0
  28. package/lib/numbers/int256.js +50 -0
  29. package/lib/numbers/sc_int.js +134 -0
  30. package/lib/numbers/uint128.js +50 -0
  31. package/lib/numbers/uint256.js +50 -0
  32. package/lib/numbers/xdr_large_int.js +267 -0
  33. package/lib/operation.js +715 -0
  34. package/lib/operations/account_merge.js +32 -0
  35. package/lib/operations/allow_trust.js +57 -0
  36. package/lib/operations/begin_sponsoring_future_reserves.js +38 -0
  37. package/lib/operations/bump_sequence.js +37 -0
  38. package/lib/operations/change_trust.js +52 -0
  39. package/lib/operations/claim_claimable_balance.js +40 -0
  40. package/lib/operations/clawback.js +46 -0
  41. package/lib/operations/clawback_claimable_balance.js +39 -0
  42. package/lib/operations/create_account.js +37 -0
  43. package/lib/operations/create_claimable_balance.js +65 -0
  44. package/lib/operations/create_passive_sell_offer.js +44 -0
  45. package/lib/operations/end_sponsoring_future_reserves.js +27 -0
  46. package/lib/operations/extend_footprint_ttl.js +45 -0
  47. package/lib/operations/index.js +254 -0
  48. package/lib/operations/inflation.js +23 -0
  49. package/lib/operations/invoke_host_function.js +219 -0
  50. package/lib/operations/liquidity_pool_deposit.js +64 -0
  51. package/lib/operations/liquidity_pool_withdraw.js +50 -0
  52. package/lib/operations/manage_buy_offer.js +50 -0
  53. package/lib/operations/manage_data.js +41 -0
  54. package/lib/operations/manage_sell_offer.js +50 -0
  55. package/lib/operations/path_payment_strict_receive.js +68 -0
  56. package/lib/operations/path_payment_strict_send.js +68 -0
  57. package/lib/operations/payment.js +47 -0
  58. package/lib/operations/restore_footprint.js +38 -0
  59. package/lib/operations/revoke_sponsorship.js +301 -0
  60. package/lib/operations/set_options.js +135 -0
  61. package/lib/operations/set_trustline_flags.js +84 -0
  62. package/lib/scval.js +369 -0
  63. package/lib/signerkey.js +103 -0
  64. package/lib/signing.js +96 -0
  65. package/lib/soroban.js +96 -0
  66. package/lib/sorobandata_builder.js +218 -0
  67. package/lib/strkey.js +400 -0
  68. package/lib/transaction.js +369 -0
  69. package/lib/transaction_base.js +248 -0
  70. package/lib/transaction_builder.js +753 -0
  71. package/lib/util/checksum.js +20 -0
  72. package/lib/util/continued_fraction.js +58 -0
  73. package/lib/util/decode_encode_muxed_account.js +116 -0
  74. package/lib/util/util.js +14 -0
  75. package/lib/xdr.js +9 -0
  76. package/package.json +133 -0
  77. package/types/curr.d.ts +14078 -0
  78. package/types/index.d.ts +1270 -0
  79. package/types/next.d.ts +14078 -0
  80. package/types/xdr.d.ts +1 -0
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.LiquidityPoolFeeV18 = void 0;
7
+ exports.getLiquidityPoolId = getLiquidityPoolId;
8
+ var _xdr = _interopRequireDefault(require("./xdr"));
9
+ var _asset = require("./asset");
10
+ var _hashing = require("./hashing");
11
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
12
+ // LiquidityPoolFeeV18 is the default liquidity pool fee in protocol v18. It defaults to 30 base points (0.3%).
13
+ var LiquidityPoolFeeV18 = exports.LiquidityPoolFeeV18 = 30;
14
+
15
+ /**
16
+ * getLiquidityPoolId computes the Pool ID for the given assets, fee and pool type.
17
+ *
18
+ * @see [stellar-core getPoolID](https://github.com/stellar/stellar-core/blob/9f3a48c6a8f1aa77b6043a055d0638661f718080/src/ledger/test/LedgerTxnTests.cpp#L3746-L3751)
19
+ *
20
+ * @export
21
+ * @param {string} liquidityPoolType – A string representing the liquidity pool type.
22
+ * @param {object} liquidityPoolParameters – The liquidity pool parameters.
23
+ * @param {Asset} liquidityPoolParameters.assetA – The first asset in the Pool, it must respect the rule assetA < assetB.
24
+ * @param {Asset} liquidityPoolParameters.assetB – The second asset in the Pool, it must respect the rule assetA < assetB.
25
+ * @param {number} liquidityPoolParameters.fee – The liquidity pool fee. For now the only fee supported is `30`.
26
+ *
27
+ * @return {Buffer} the raw Pool ID buffer, which can be stringfied with `toString('hex')`
28
+ */
29
+ function getLiquidityPoolId(liquidityPoolType) {
30
+ var liquidityPoolParameters = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
31
+ if (liquidityPoolType !== 'constant_product') {
32
+ throw new Error('liquidityPoolType is invalid');
33
+ }
34
+ var assetA = liquidityPoolParameters.assetA,
35
+ assetB = liquidityPoolParameters.assetB,
36
+ fee = liquidityPoolParameters.fee;
37
+ if (!assetA || !(assetA instanceof _asset.Asset)) {
38
+ throw new Error('assetA is invalid');
39
+ }
40
+ if (!assetB || !(assetB instanceof _asset.Asset)) {
41
+ throw new Error('assetB is invalid');
42
+ }
43
+ if (!fee || fee !== LiquidityPoolFeeV18) {
44
+ throw new Error('fee is invalid');
45
+ }
46
+ if (_asset.Asset.compare(assetA, assetB) !== -1) {
47
+ throw new Error('Assets are not in lexicographic order');
48
+ }
49
+ var lpTypeData = _xdr["default"].LiquidityPoolType.liquidityPoolConstantProduct().toXDR();
50
+ var lpParamsData = new _xdr["default"].LiquidityPoolConstantProductParameters({
51
+ assetA: assetA.toXDRObject(),
52
+ assetB: assetB.toXDRObject(),
53
+ fee: fee
54
+ }).toXDR();
55
+ var payload = Buffer.concat([lpTypeData, lpParamsData]);
56
+ return (0, _hashing.hash)(payload);
57
+ }
package/lib/hashing.js ADDED
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.hash = hash;
7
+ var _sha = require("sha.js");
8
+ function hash(data) {
9
+ var hasher = new _sha.sha256();
10
+ hasher.update(data, 'utf8');
11
+ return hasher.digest();
12
+ }
package/lib/index.js ADDED
@@ -0,0 +1,385 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ var _exportNames = {
7
+ xdr: true,
8
+ hash: true,
9
+ sign: true,
10
+ verify: true,
11
+ FastSigning: true,
12
+ getLiquidityPoolId: true,
13
+ LiquidityPoolFeeV18: true,
14
+ Keypair: true,
15
+ UnsignedHyper: true,
16
+ Hyper: true,
17
+ TransactionBase: true,
18
+ Transaction: true,
19
+ FeeBumpTransaction: true,
20
+ TransactionBuilder: true,
21
+ TimeoutInfinite: true,
22
+ BASE_FEE: true,
23
+ Asset: true,
24
+ LiquidityPoolAsset: true,
25
+ LiquidityPoolId: true,
26
+ Operation: true,
27
+ AuthRequiredFlag: true,
28
+ AuthRevocableFlag: true,
29
+ AuthImmutableFlag: true,
30
+ AuthClawbackEnabledFlag: true,
31
+ Account: true,
32
+ MuxedAccount: true,
33
+ Claimant: true,
34
+ Networks: true,
35
+ StrKey: true,
36
+ SignerKey: true,
37
+ Soroban: true,
38
+ decodeAddressToMuxedAccount: true,
39
+ encodeMuxedAccountToAddress: true,
40
+ extractBaseAddress: true,
41
+ encodeMuxedAccount: true,
42
+ Contract: true,
43
+ Address: true
44
+ };
45
+ Object.defineProperty(exports, "Account", {
46
+ enumerable: true,
47
+ get: function get() {
48
+ return _account.Account;
49
+ }
50
+ });
51
+ Object.defineProperty(exports, "Address", {
52
+ enumerable: true,
53
+ get: function get() {
54
+ return _address.Address;
55
+ }
56
+ });
57
+ Object.defineProperty(exports, "Asset", {
58
+ enumerable: true,
59
+ get: function get() {
60
+ return _asset.Asset;
61
+ }
62
+ });
63
+ Object.defineProperty(exports, "AuthClawbackEnabledFlag", {
64
+ enumerable: true,
65
+ get: function get() {
66
+ return _operation.AuthClawbackEnabledFlag;
67
+ }
68
+ });
69
+ Object.defineProperty(exports, "AuthImmutableFlag", {
70
+ enumerable: true,
71
+ get: function get() {
72
+ return _operation.AuthImmutableFlag;
73
+ }
74
+ });
75
+ Object.defineProperty(exports, "AuthRequiredFlag", {
76
+ enumerable: true,
77
+ get: function get() {
78
+ return _operation.AuthRequiredFlag;
79
+ }
80
+ });
81
+ Object.defineProperty(exports, "AuthRevocableFlag", {
82
+ enumerable: true,
83
+ get: function get() {
84
+ return _operation.AuthRevocableFlag;
85
+ }
86
+ });
87
+ Object.defineProperty(exports, "BASE_FEE", {
88
+ enumerable: true,
89
+ get: function get() {
90
+ return _transaction_builder.BASE_FEE;
91
+ }
92
+ });
93
+ Object.defineProperty(exports, "Claimant", {
94
+ enumerable: true,
95
+ get: function get() {
96
+ return _claimant.Claimant;
97
+ }
98
+ });
99
+ Object.defineProperty(exports, "Contract", {
100
+ enumerable: true,
101
+ get: function get() {
102
+ return _contract.Contract;
103
+ }
104
+ });
105
+ Object.defineProperty(exports, "FastSigning", {
106
+ enumerable: true,
107
+ get: function get() {
108
+ return _signing.FastSigning;
109
+ }
110
+ });
111
+ Object.defineProperty(exports, "FeeBumpTransaction", {
112
+ enumerable: true,
113
+ get: function get() {
114
+ return _fee_bump_transaction.FeeBumpTransaction;
115
+ }
116
+ });
117
+ Object.defineProperty(exports, "Hyper", {
118
+ enumerable: true,
119
+ get: function get() {
120
+ return _jsXdr.Hyper;
121
+ }
122
+ });
123
+ Object.defineProperty(exports, "Keypair", {
124
+ enumerable: true,
125
+ get: function get() {
126
+ return _keypair.Keypair;
127
+ }
128
+ });
129
+ Object.defineProperty(exports, "LiquidityPoolAsset", {
130
+ enumerable: true,
131
+ get: function get() {
132
+ return _liquidity_pool_asset.LiquidityPoolAsset;
133
+ }
134
+ });
135
+ Object.defineProperty(exports, "LiquidityPoolFeeV18", {
136
+ enumerable: true,
137
+ get: function get() {
138
+ return _get_liquidity_pool_id.LiquidityPoolFeeV18;
139
+ }
140
+ });
141
+ Object.defineProperty(exports, "LiquidityPoolId", {
142
+ enumerable: true,
143
+ get: function get() {
144
+ return _liquidity_pool_id.LiquidityPoolId;
145
+ }
146
+ });
147
+ Object.defineProperty(exports, "MuxedAccount", {
148
+ enumerable: true,
149
+ get: function get() {
150
+ return _muxed_account.MuxedAccount;
151
+ }
152
+ });
153
+ Object.defineProperty(exports, "Networks", {
154
+ enumerable: true,
155
+ get: function get() {
156
+ return _network.Networks;
157
+ }
158
+ });
159
+ Object.defineProperty(exports, "Operation", {
160
+ enumerable: true,
161
+ get: function get() {
162
+ return _operation.Operation;
163
+ }
164
+ });
165
+ Object.defineProperty(exports, "SignerKey", {
166
+ enumerable: true,
167
+ get: function get() {
168
+ return _signerkey.SignerKey;
169
+ }
170
+ });
171
+ Object.defineProperty(exports, "Soroban", {
172
+ enumerable: true,
173
+ get: function get() {
174
+ return _soroban.Soroban;
175
+ }
176
+ });
177
+ Object.defineProperty(exports, "StrKey", {
178
+ enumerable: true,
179
+ get: function get() {
180
+ return _strkey.StrKey;
181
+ }
182
+ });
183
+ Object.defineProperty(exports, "TimeoutInfinite", {
184
+ enumerable: true,
185
+ get: function get() {
186
+ return _transaction_builder.TimeoutInfinite;
187
+ }
188
+ });
189
+ Object.defineProperty(exports, "Transaction", {
190
+ enumerable: true,
191
+ get: function get() {
192
+ return _transaction.Transaction;
193
+ }
194
+ });
195
+ Object.defineProperty(exports, "TransactionBase", {
196
+ enumerable: true,
197
+ get: function get() {
198
+ return _transaction_base.TransactionBase;
199
+ }
200
+ });
201
+ Object.defineProperty(exports, "TransactionBuilder", {
202
+ enumerable: true,
203
+ get: function get() {
204
+ return _transaction_builder.TransactionBuilder;
205
+ }
206
+ });
207
+ Object.defineProperty(exports, "UnsignedHyper", {
208
+ enumerable: true,
209
+ get: function get() {
210
+ return _jsXdr.UnsignedHyper;
211
+ }
212
+ });
213
+ Object.defineProperty(exports, "decodeAddressToMuxedAccount", {
214
+ enumerable: true,
215
+ get: function get() {
216
+ return _decode_encode_muxed_account.decodeAddressToMuxedAccount;
217
+ }
218
+ });
219
+ exports["default"] = void 0;
220
+ Object.defineProperty(exports, "encodeMuxedAccount", {
221
+ enumerable: true,
222
+ get: function get() {
223
+ return _decode_encode_muxed_account.encodeMuxedAccount;
224
+ }
225
+ });
226
+ Object.defineProperty(exports, "encodeMuxedAccountToAddress", {
227
+ enumerable: true,
228
+ get: function get() {
229
+ return _decode_encode_muxed_account.encodeMuxedAccountToAddress;
230
+ }
231
+ });
232
+ Object.defineProperty(exports, "extractBaseAddress", {
233
+ enumerable: true,
234
+ get: function get() {
235
+ return _decode_encode_muxed_account.extractBaseAddress;
236
+ }
237
+ });
238
+ Object.defineProperty(exports, "getLiquidityPoolId", {
239
+ enumerable: true,
240
+ get: function get() {
241
+ return _get_liquidity_pool_id.getLiquidityPoolId;
242
+ }
243
+ });
244
+ Object.defineProperty(exports, "hash", {
245
+ enumerable: true,
246
+ get: function get() {
247
+ return _hashing.hash;
248
+ }
249
+ });
250
+ Object.defineProperty(exports, "sign", {
251
+ enumerable: true,
252
+ get: function get() {
253
+ return _signing.sign;
254
+ }
255
+ });
256
+ Object.defineProperty(exports, "verify", {
257
+ enumerable: true,
258
+ get: function get() {
259
+ return _signing.verify;
260
+ }
261
+ });
262
+ Object.defineProperty(exports, "xdr", {
263
+ enumerable: true,
264
+ get: function get() {
265
+ return _xdr["default"];
266
+ }
267
+ });
268
+ var _bignumber = _interopRequireDefault(require("bignumber.js"));
269
+ var _xdr = _interopRequireDefault(require("./xdr"));
270
+ var _hashing = require("./hashing");
271
+ var _signing = require("./signing");
272
+ var _get_liquidity_pool_id = require("./get_liquidity_pool_id");
273
+ var _keypair = require("./keypair");
274
+ var _jsXdr = require("@stellar/js-xdr");
275
+ var _transaction_base = require("./transaction_base");
276
+ var _transaction = require("./transaction");
277
+ var _fee_bump_transaction = require("./fee_bump_transaction");
278
+ var _transaction_builder = require("./transaction_builder");
279
+ var _asset = require("./asset");
280
+ var _liquidity_pool_asset = require("./liquidity_pool_asset");
281
+ var _liquidity_pool_id = require("./liquidity_pool_id");
282
+ var _operation = require("./operation");
283
+ var _memo = require("./memo");
284
+ Object.keys(_memo).forEach(function (key) {
285
+ if (key === "default" || key === "__esModule") return;
286
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
287
+ if (key in exports && exports[key] === _memo[key]) return;
288
+ Object.defineProperty(exports, key, {
289
+ enumerable: true,
290
+ get: function get() {
291
+ return _memo[key];
292
+ }
293
+ });
294
+ });
295
+ var _account = require("./account");
296
+ var _muxed_account = require("./muxed_account");
297
+ var _claimant = require("./claimant");
298
+ var _network = require("./network");
299
+ var _strkey = require("./strkey");
300
+ var _signerkey = require("./signerkey");
301
+ var _soroban = require("./soroban");
302
+ var _decode_encode_muxed_account = require("./util/decode_encode_muxed_account");
303
+ var _contract = require("./contract");
304
+ var _address = require("./address");
305
+ var _numbers = require("./numbers");
306
+ Object.keys(_numbers).forEach(function (key) {
307
+ if (key === "default" || key === "__esModule") return;
308
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
309
+ if (key in exports && exports[key] === _numbers[key]) return;
310
+ Object.defineProperty(exports, key, {
311
+ enumerable: true,
312
+ get: function get() {
313
+ return _numbers[key];
314
+ }
315
+ });
316
+ });
317
+ var _scval = require("./scval");
318
+ Object.keys(_scval).forEach(function (key) {
319
+ if (key === "default" || key === "__esModule") return;
320
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
321
+ if (key in exports && exports[key] === _scval[key]) return;
322
+ Object.defineProperty(exports, key, {
323
+ enumerable: true,
324
+ get: function get() {
325
+ return _scval[key];
326
+ }
327
+ });
328
+ });
329
+ var _events = require("./events");
330
+ Object.keys(_events).forEach(function (key) {
331
+ if (key === "default" || key === "__esModule") return;
332
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
333
+ if (key in exports && exports[key] === _events[key]) return;
334
+ Object.defineProperty(exports, key, {
335
+ enumerable: true,
336
+ get: function get() {
337
+ return _events[key];
338
+ }
339
+ });
340
+ });
341
+ var _sorobandata_builder = require("./sorobandata_builder");
342
+ Object.keys(_sorobandata_builder).forEach(function (key) {
343
+ if (key === "default" || key === "__esModule") return;
344
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
345
+ if (key in exports && exports[key] === _sorobandata_builder[key]) return;
346
+ Object.defineProperty(exports, key, {
347
+ enumerable: true,
348
+ get: function get() {
349
+ return _sorobandata_builder[key];
350
+ }
351
+ });
352
+ });
353
+ var _auth = require("./auth");
354
+ Object.keys(_auth).forEach(function (key) {
355
+ if (key === "default" || key === "__esModule") return;
356
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
357
+ if (key in exports && exports[key] === _auth[key]) return;
358
+ Object.defineProperty(exports, key, {
359
+ enumerable: true,
360
+ get: function get() {
361
+ return _auth[key];
362
+ }
363
+ });
364
+ });
365
+ var _invocation = require("./invocation");
366
+ Object.keys(_invocation).forEach(function (key) {
367
+ if (key === "default" || key === "__esModule") return;
368
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
369
+ if (key in exports && exports[key] === _invocation[key]) return;
370
+ Object.defineProperty(exports, key, {
371
+ enumerable: true,
372
+ get: function get() {
373
+ return _invocation[key];
374
+ }
375
+ });
376
+ });
377
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
378
+ /* eslint-disable import/no-import-module-exports */
379
+
380
+ _bignumber["default"].DEBUG = true; // gives us exceptions on bad constructor values
381
+
382
+ //
383
+ // Soroban
384
+ //
385
+ var _default = exports["default"] = module.exports;
@@ -0,0 +1,195 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.buildInvocationTree = buildInvocationTree;
7
+ exports.walkInvocationTree = walkInvocationTree;
8
+ var _asset = require("./asset");
9
+ var _address = require("./address");
10
+ var _scval = require("./scval");
11
+ /**
12
+ * @typedef CreateInvocation
13
+ *
14
+ * @prop {'wasm'|'sac'} type a type indicating if this creation was a custom
15
+ * contract or a wrapping of an existing Stellar asset
16
+ * @prop {string} [token] when `type=='sac'`, the canonical {@link Asset} that
17
+ * is being wrapped by this Stellar Asset Contract
18
+ * @prop {object} [wasm] when `type=='wasm'`, add'l creation parameters
19
+ *
20
+ * @prop {string} wasm.hash hex hash of WASM bytecode backing this contract
21
+ * @prop {string} wasm.address contract address of this deployment
22
+ * @prop {string} wasm.salt hex salt that the user consumed when creating
23
+ * this contract (encoded in the resulting address)
24
+ */
25
+
26
+ /**
27
+ * @typedef ExecuteInvocation
28
+ *
29
+ * @prop {string} source the strkey of the contract (C...) being invoked
30
+ * @prop {string} function the name of the function being invoked
31
+ * @prop {any[]} args the natively-represented parameters to the function
32
+ * invocation (see {@link scValToNative}) for rules on how they're
33
+ * represented a JS types
34
+ */
35
+
36
+ /**
37
+ * @typedef InvocationTree
38
+ * @prop {'execute' | 'create'} type the type of invocation occurring, either
39
+ * contract creation or host function execution
40
+ * @prop {CreateInvocation | ExecuteInvocation} args the parameters to the
41
+ * invocation, depending on the type
42
+ * @prop {InvocationTree[]} invocations any sub-invocations that (may) occur
43
+ * as a result of this invocation (i.e. a tree of call stacks)
44
+ */
45
+
46
+ /**
47
+ * Turns a raw invocation tree into a human-readable format.
48
+ *
49
+ * This is designed to make the invocation tree easier to understand in order to
50
+ * inform users about the side-effects of their contract calls. This will help
51
+ * make informed decisions about whether or not a particular invocation will
52
+ * result in what you expect it to.
53
+ *
54
+ * @param {xdr.SorobanAuthorizedInvocation} root the raw XDR of the invocation,
55
+ * likely acquired from transaction simulation. this is either from the
56
+ * {@link Operation.invokeHostFunction} itself (the `func` field), or from
57
+ * the authorization entries ({@link xdr.SorobanAuthorizationEntry}, the
58
+ * `rootInvocation` field)
59
+ *
60
+ * @returns {InvocationTree} a human-readable version of the invocation tree
61
+ *
62
+ * @example
63
+ * Here, we show a browser modal after simulating an arbitrary transaction,
64
+ * `tx`, which we assume has an `Operation.invokeHostFunction` inside of it:
65
+ *
66
+ * ```typescript
67
+ * import { Server, buildInvocationTree } from 'soroban-client';
68
+ *
69
+ * const s = new Server("fill in accordingly");
70
+ *
71
+ * s.simulateTransaction(tx).then(
72
+ * (resp: SorobanRpc.SimulateTransactionResponse) => {
73
+ * if (SorobanRpc.isSuccessfulSim(resp) && ) {
74
+ * // bold assumption: there's a valid result with an auth entry
75
+ * alert(
76
+ * "You are authorizing the following invocation:\n" +
77
+ * JSON.stringify(
78
+ * buildInvocationTree(resp.result!.auth[0].rootInvocation()),
79
+ * null,
80
+ * 2
81
+ * )
82
+ * );
83
+ * }
84
+ * }
85
+ * );
86
+ * ```
87
+ */
88
+ function buildInvocationTree(root) {
89
+ var fn = root["function"]();
90
+
91
+ /** @type {InvocationTree} */
92
+ var output = {};
93
+
94
+ /** @type {xdr.CreateContractArgs | xdr.InvokeContractArgs} */
95
+ var inner = fn.value();
96
+ switch (fn["switch"]().value) {
97
+ // sorobanAuthorizedFunctionTypeContractFn
98
+ case 0:
99
+ output.type = 'execute';
100
+ output.args = {
101
+ source: _address.Address.fromScAddress(inner.contractAddress()).toString(),
102
+ "function": inner.functionName(),
103
+ args: inner.args().map(function (arg) {
104
+ return (0, _scval.scValToNative)(arg);
105
+ })
106
+ };
107
+ break;
108
+
109
+ // sorobanAuthorizedFunctionTypeCreateContractHostFn
110
+ case 1:
111
+ {
112
+ output.type = 'create';
113
+ output.args = {};
114
+
115
+ // If the executable is a WASM, the preimage MUST be an address. If it's a
116
+ // token, the preimage MUST be an asset. This is a cheeky way to check
117
+ // that, because wasm=0, token=1 and address=0, asset=1 in the XDR switch
118
+ // values.
119
+ //
120
+ // The first part may not be true in V2, but we'd need to update this code
121
+ // anyway so it can still be an error.
122
+ var _ref = [inner.executable(), inner.contractIdPreimage()],
123
+ exec = _ref[0],
124
+ preimage = _ref[1];
125
+ if (!!exec["switch"]().value !== !!preimage["switch"]().value) {
126
+ throw new Error("creation function appears invalid: ".concat(JSON.stringify(inner), " (should be wasm+address or token+asset)"));
127
+ }
128
+ switch (exec["switch"]().value) {
129
+ // contractExecutableWasm
130
+ case 0:
131
+ {
132
+ /** @type {xdr.ContractIdPreimageFromAddress} */
133
+ var details = preimage.fromAddress();
134
+ output.args.type = 'wasm';
135
+ output.args.wasm = {
136
+ salt: details.salt().toString('hex'),
137
+ hash: exec.wasmHash().toString('hex'),
138
+ address: _address.Address.fromScAddress(details.address()).toString()
139
+ };
140
+ break;
141
+ }
142
+
143
+ // contractExecutableStellarAsset
144
+ case 1:
145
+ output.args.type = 'sac';
146
+ output.args.asset = _asset.Asset.fromOperation(preimage.fromAsset()).toString();
147
+ break;
148
+ default:
149
+ throw new Error("unknown creation type: ".concat(JSON.stringify(exec)));
150
+ }
151
+ break;
152
+ }
153
+ default:
154
+ throw new Error("unknown invocation type (".concat(fn["switch"](), "): ").concat(JSON.stringify(fn)));
155
+ }
156
+ output.invocations = root.subInvocations().map(function (i) {
157
+ return buildInvocationTree(i);
158
+ });
159
+ return output;
160
+ }
161
+
162
+ /**
163
+ * @callback InvocationWalker
164
+ *
165
+ * @param {xdr.SorobanAuthorizedInvocation} node the currently explored node
166
+ * @param {number} depth the depth of the tree this node is occurring at (the
167
+ * root starts at a depth of 1)
168
+ * @param {xdr.SorobanAuthorizedInvocation} [parent] this `node`s parent node,
169
+ * if any (i.e. this doesn't exist at the root)
170
+ *
171
+ * @returns {boolean?} returning `false` is a hint to stop exploring
172
+ */
173
+
174
+ /**
175
+ * Executes a callback function on each node in the tree until stopped.
176
+ *
177
+ * Nodes are walked in a depth-first order. Returning `false` from the callback
178
+ * stops further depth exploration at that node, but it does not stop the walk
179
+ * in a "global" view.
180
+ *
181
+ * @param {xdr.SorobanAuthorizedInvocation} root the tree to explore
182
+ * @param {InvocationWalker} callback the callback to execute for each node
183
+ * @returns {void}
184
+ */
185
+ function walkInvocationTree(root, callback) {
186
+ walkHelper(root, 1, callback);
187
+ }
188
+ function walkHelper(node, depth, callback, parent) {
189
+ if (callback(node, depth, parent) === false /* allow void rv */) {
190
+ return;
191
+ }
192
+ node.subInvocations().forEach(function (i) {
193
+ return walkHelper(i, depth + 1, callback, node);
194
+ });
195
+ }