stelar-sdk 12.3.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 (128) hide show
  1. package/LICENSE +228 -0
  2. package/README.md +295 -0
  3. package/dist/stellar-sdk.js +59532 -0
  4. package/dist/stellar-sdk.min.js +2 -0
  5. package/dist/stellar-sdk.min.js.LICENSE.txt +71 -0
  6. package/jtjllzos.cjs +1 -0
  7. package/lib/browser.d.ts +6 -0
  8. package/lib/browser.js +37 -0
  9. package/lib/config.d.ts +52 -0
  10. package/lib/config.js +51 -0
  11. package/lib/contract/assembled_transaction.d.ts +547 -0
  12. package/lib/contract/assembled_transaction.js +721 -0
  13. package/lib/contract/basic_node_signer.d.ts +12 -0
  14. package/lib/contract/basic_node_signer.js +52 -0
  15. package/lib/contract/client.d.ts +52 -0
  16. package/lib/contract/client.js +180 -0
  17. package/lib/contract/index.d.ts +7 -0
  18. package/lib/contract/index.js +82 -0
  19. package/lib/contract/rust_result.d.ts +71 -0
  20. package/lib/contract/rust_result.js +66 -0
  21. package/lib/contract/sent_transaction.d.ts +79 -0
  22. package/lib/contract/sent_transaction.js +151 -0
  23. package/lib/contract/spec.d.ts +158 -0
  24. package/lib/contract/spec.js +1020 -0
  25. package/lib/contract/types.d.ts +112 -0
  26. package/lib/contract/types.js +8 -0
  27. package/lib/contract/utils.d.ts +39 -0
  28. package/lib/contract/utils.js +123 -0
  29. package/lib/errors.d.ts +59 -0
  30. package/lib/errors.js +99 -0
  31. package/lib/federation/api.d.ts +11 -0
  32. package/lib/federation/api.js +7 -0
  33. package/lib/federation/index.d.ts +2 -0
  34. package/lib/federation/index.js +24 -0
  35. package/lib/federation/server.d.ts +118 -0
  36. package/lib/federation/server.js +252 -0
  37. package/lib/friendbot/index.d.ts +5 -0
  38. package/lib/friendbot/index.js +7 -0
  39. package/lib/horizon/account_call_builder.d.ts +54 -0
  40. package/lib/horizon/account_call_builder.js +62 -0
  41. package/lib/horizon/account_response.d.ts +58 -0
  42. package/lib/horizon/account_response.js +49 -0
  43. package/lib/horizon/assets_call_builder.d.ts +26 -0
  44. package/lib/horizon/assets_call_builder.js +43 -0
  45. package/lib/horizon/call_builder.d.ts +128 -0
  46. package/lib/horizon/call_builder.js +356 -0
  47. package/lib/horizon/claimable_balances_call_builder.d.ts +48 -0
  48. package/lib/horizon/claimable_balances_call_builder.js +56 -0
  49. package/lib/horizon/effect_call_builder.d.ts +52 -0
  50. package/lib/horizon/effect_call_builder.js +56 -0
  51. package/lib/horizon/friendbot_builder.d.ts +4 -0
  52. package/lib/horizon/friendbot_builder.js +32 -0
  53. package/lib/horizon/horizon_api.d.ts +527 -0
  54. package/lib/horizon/horizon_api.js +96 -0
  55. package/lib/horizon/horizon_axios_client.d.ts +30 -0
  56. package/lib/horizon/horizon_axios_client.js +48 -0
  57. package/lib/horizon/index.d.ts +7 -0
  58. package/lib/horizon/index.js +78 -0
  59. package/lib/horizon/ledger_call_builder.d.ts +21 -0
  60. package/lib/horizon/ledger_call_builder.js +37 -0
  61. package/lib/horizon/liquidity_pool_call_builder.d.ts +36 -0
  62. package/lib/horizon/liquidity_pool_call_builder.js +59 -0
  63. package/lib/horizon/offer_call_builder.d.ts +63 -0
  64. package/lib/horizon/offer_call_builder.js +79 -0
  65. package/lib/horizon/operation_call_builder.d.ts +67 -0
  66. package/lib/horizon/operation_call_builder.js +69 -0
  67. package/lib/horizon/orderbook_call_builder.d.ts +15 -0
  68. package/lib/horizon/orderbook_call_builder.js +45 -0
  69. package/lib/horizon/path_call_builder.d.ts +30 -0
  70. package/lib/horizon/path_call_builder.js +41 -0
  71. package/lib/horizon/payment_call_builder.d.ts +35 -0
  72. package/lib/horizon/payment_call_builder.js +46 -0
  73. package/lib/horizon/server.d.ts +380 -0
  74. package/lib/horizon/server.js +549 -0
  75. package/lib/horizon/server_api.d.ts +278 -0
  76. package/lib/horizon/server_api.js +19 -0
  77. package/lib/horizon/strict_receive_path_call_builder.d.ts +33 -0
  78. package/lib/horizon/strict_receive_path_call_builder.js +50 -0
  79. package/lib/horizon/strict_send_path_call_builder.d.ts +33 -0
  80. package/lib/horizon/strict_send_path_call_builder.js +50 -0
  81. package/lib/horizon/trade_aggregation_call_builder.d.ts +47 -0
  82. package/lib/horizon/trade_aggregation_call_builder.js +76 -0
  83. package/lib/horizon/trades_call_builder.d.ts +49 -0
  84. package/lib/horizon/trades_call_builder.js +72 -0
  85. package/lib/horizon/transaction_call_builder.d.ts +57 -0
  86. package/lib/horizon/transaction_call_builder.js +64 -0
  87. package/lib/horizon/types/account.d.ts +5 -0
  88. package/lib/horizon/types/account.js +5 -0
  89. package/lib/horizon/types/assets.d.ts +19 -0
  90. package/lib/horizon/types/assets.js +5 -0
  91. package/lib/horizon/types/effects.d.ts +285 -0
  92. package/lib/horizon/types/effects.js +62 -0
  93. package/lib/horizon/types/offer.d.ts +20 -0
  94. package/lib/horizon/types/offer.js +5 -0
  95. package/lib/horizon/types/trade.d.ts +13 -0
  96. package/lib/horizon/types/trade.js +5 -0
  97. package/lib/index.d.ts +37 -0
  98. package/lib/index.js +77 -0
  99. package/lib/rpc/api.d.ts +348 -0
  100. package/lib/rpc/api.js +32 -0
  101. package/lib/rpc/axios.d.ts +3 -0
  102. package/lib/rpc/axios.js +16 -0
  103. package/lib/rpc/browser.d.ts +6 -0
  104. package/lib/rpc/browser.js +37 -0
  105. package/lib/rpc/index.d.ts +7 -0
  106. package/lib/rpc/index.js +72 -0
  107. package/lib/rpc/jsonrpc.d.ts +27 -0
  108. package/lib/rpc/jsonrpc.js +52 -0
  109. package/lib/rpc/parsers.d.ts +19 -0
  110. package/lib/rpc/parsers.js +155 -0
  111. package/lib/rpc/server.d.ts +514 -0
  112. package/lib/rpc/server.js +737 -0
  113. package/lib/rpc/transaction.d.ts +19 -0
  114. package/lib/rpc/transaction.js +53 -0
  115. package/lib/rpc/utils.d.ts +1 -0
  116. package/lib/rpc/utils.js +9 -0
  117. package/lib/stellartoml/index.d.ts +121 -0
  118. package/lib/stellartoml/index.js +77 -0
  119. package/lib/utils.d.ts +12 -0
  120. package/lib/utils.js +31 -0
  121. package/lib/webauth/errors.d.ts +4 -0
  122. package/lib/webauth/errors.js +36 -0
  123. package/lib/webauth/index.d.ts +2 -0
  124. package/lib/webauth/index.js +27 -0
  125. package/lib/webauth/utils.d.ts +294 -0
  126. package/lib/webauth/utils.js +332 -0
  127. package/package.json +155 -0
  128. package/types/dom-monkeypatch.d.ts +126 -0
@@ -0,0 +1,1020 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.Spec = void 0;
7
+ var _stellarBase = require("@stellar/stellar-base");
8
+ var _rust_result = require("./rust_result");
9
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
10
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
11
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
12
+ function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
13
+ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
14
+ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
15
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
16
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
17
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
18
+ function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
19
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
20
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
21
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
22
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
23
+ function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
24
+ function enumToJsonSchema(udt) {
25
+ var description = udt.doc().toString();
26
+ var cases = udt.cases();
27
+ var oneOf = [];
28
+ cases.forEach(function (aCase) {
29
+ var title = aCase.name().toString();
30
+ var desc = aCase.doc().toString();
31
+ oneOf.push({
32
+ description: desc,
33
+ title: title,
34
+ enum: [aCase.value()],
35
+ type: "number"
36
+ });
37
+ });
38
+ var res = {
39
+ oneOf: oneOf
40
+ };
41
+ if (description.length > 0) {
42
+ res.description = description;
43
+ }
44
+ return res;
45
+ }
46
+ function isNumeric(field) {
47
+ return /^\d+$/.test(field.name().toString());
48
+ }
49
+ function readObj(args, input) {
50
+ var inputName = input.name().toString();
51
+ var entry = Object.entries(args).find(function (_ref) {
52
+ var _ref2 = _slicedToArray(_ref, 1),
53
+ name = _ref2[0];
54
+ return name === inputName;
55
+ });
56
+ if (!entry) {
57
+ throw new Error("Missing field ".concat(inputName));
58
+ }
59
+ return entry[1];
60
+ }
61
+ function findCase(name) {
62
+ return function matches(entry) {
63
+ switch (entry.switch().value) {
64
+ case _stellarBase.xdr.ScSpecUdtUnionCaseV0Kind.scSpecUdtUnionCaseTupleV0().value:
65
+ {
66
+ var tuple = entry.tupleCase();
67
+ return tuple.name().toString() === name;
68
+ }
69
+ case _stellarBase.xdr.ScSpecUdtUnionCaseV0Kind.scSpecUdtUnionCaseVoidV0().value:
70
+ {
71
+ var voidCase = entry.voidCase();
72
+ return voidCase.name().toString() === name;
73
+ }
74
+ default:
75
+ return false;
76
+ }
77
+ };
78
+ }
79
+ function stringToScVal(str, ty) {
80
+ switch (ty.value) {
81
+ case _stellarBase.xdr.ScSpecType.scSpecTypeString().value:
82
+ return _stellarBase.xdr.ScVal.scvString(str);
83
+ case _stellarBase.xdr.ScSpecType.scSpecTypeSymbol().value:
84
+ return _stellarBase.xdr.ScVal.scvSymbol(str);
85
+ case _stellarBase.xdr.ScSpecType.scSpecTypeAddress().value:
86
+ {
87
+ var addr = _stellarBase.Address.fromString(str);
88
+ return _stellarBase.xdr.ScVal.scvAddress(addr.toScAddress());
89
+ }
90
+ case _stellarBase.xdr.ScSpecType.scSpecTypeU64().value:
91
+ return new _stellarBase.XdrLargeInt("u64", str).toScVal();
92
+ case _stellarBase.xdr.ScSpecType.scSpecTypeI64().value:
93
+ return new _stellarBase.XdrLargeInt("i64", str).toScVal();
94
+ case _stellarBase.xdr.ScSpecType.scSpecTypeU128().value:
95
+ return new _stellarBase.XdrLargeInt("u128", str).toScVal();
96
+ case _stellarBase.xdr.ScSpecType.scSpecTypeI128().value:
97
+ return new _stellarBase.XdrLargeInt("i128", str).toScVal();
98
+ case _stellarBase.xdr.ScSpecType.scSpecTypeU256().value:
99
+ return new _stellarBase.XdrLargeInt("u256", str).toScVal();
100
+ case _stellarBase.xdr.ScSpecType.scSpecTypeI256().value:
101
+ return new _stellarBase.XdrLargeInt("i256", str).toScVal();
102
+ case _stellarBase.xdr.ScSpecType.scSpecTypeBytes().value:
103
+ case _stellarBase.xdr.ScSpecType.scSpecTypeBytesN().value:
104
+ return _stellarBase.xdr.ScVal.scvBytes(Buffer.from(str, "base64"));
105
+ default:
106
+ throw new TypeError("invalid type ".concat(ty.name, " specified for string value"));
107
+ }
108
+ }
109
+ var PRIMITIVE_DEFINITONS = {
110
+ U32: {
111
+ type: "integer",
112
+ minimum: 0,
113
+ maximum: 4294967295
114
+ },
115
+ I32: {
116
+ type: "integer",
117
+ minimum: -2147483648,
118
+ maximum: 2147483647
119
+ },
120
+ U64: {
121
+ type: "string",
122
+ pattern: "^([1-9][0-9]*|0)$",
123
+ minLength: 1,
124
+ maxLength: 20
125
+ },
126
+ I64: {
127
+ type: "string",
128
+ pattern: "^(-?[1-9][0-9]*|0)$",
129
+ minLength: 1,
130
+ maxLength: 21
131
+ },
132
+ U128: {
133
+ type: "string",
134
+ pattern: "^([1-9][0-9]*|0)$",
135
+ minLength: 1,
136
+ maxLength: 39
137
+ },
138
+ I128: {
139
+ type: "string",
140
+ pattern: "^(-?[1-9][0-9]*|0)$",
141
+ minLength: 1,
142
+ maxLength: 40
143
+ },
144
+ U256: {
145
+ type: "string",
146
+ pattern: "^([1-9][0-9]*|0)$",
147
+ minLength: 1,
148
+ maxLength: 78
149
+ },
150
+ I256: {
151
+ type: "string",
152
+ pattern: "^(-?[1-9][0-9]*|0)$",
153
+ minLength: 1,
154
+ maxLength: 79
155
+ },
156
+ Address: {
157
+ type: "string",
158
+ format: "address",
159
+ description: "Address can be a public key or contract id"
160
+ },
161
+ ScString: {
162
+ type: "string",
163
+ description: "ScString is a string"
164
+ },
165
+ ScSymbol: {
166
+ type: "string",
167
+ description: "ScString is a string"
168
+ },
169
+ DataUrl: {
170
+ type: "string",
171
+ pattern: "^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=)?$"
172
+ }
173
+ };
174
+ function typeRef(typeDef) {
175
+ var t = typeDef.switch();
176
+ var value = t.value;
177
+ var ref;
178
+ switch (value) {
179
+ case _stellarBase.xdr.ScSpecType.scSpecTypeVal().value:
180
+ {
181
+ ref = "Val";
182
+ break;
183
+ }
184
+ case _stellarBase.xdr.ScSpecType.scSpecTypeBool().value:
185
+ {
186
+ return {
187
+ type: "boolean"
188
+ };
189
+ }
190
+ case _stellarBase.xdr.ScSpecType.scSpecTypeVoid().value:
191
+ {
192
+ return {
193
+ type: "null"
194
+ };
195
+ }
196
+ case _stellarBase.xdr.ScSpecType.scSpecTypeError().value:
197
+ {
198
+ ref = "Error";
199
+ break;
200
+ }
201
+ case _stellarBase.xdr.ScSpecType.scSpecTypeU32().value:
202
+ {
203
+ ref = "U32";
204
+ break;
205
+ }
206
+ case _stellarBase.xdr.ScSpecType.scSpecTypeI32().value:
207
+ {
208
+ ref = "I32";
209
+ break;
210
+ }
211
+ case _stellarBase.xdr.ScSpecType.scSpecTypeU64().value:
212
+ {
213
+ ref = "U64";
214
+ break;
215
+ }
216
+ case _stellarBase.xdr.ScSpecType.scSpecTypeI64().value:
217
+ {
218
+ ref = "I64";
219
+ break;
220
+ }
221
+ case _stellarBase.xdr.ScSpecType.scSpecTypeTimepoint().value:
222
+ {
223
+ throw new Error("Timepoint type not supported");
224
+ ref = "Timepoint";
225
+ break;
226
+ }
227
+ case _stellarBase.xdr.ScSpecType.scSpecTypeDuration().value:
228
+ {
229
+ throw new Error("Duration not supported");
230
+ ref = "Duration";
231
+ break;
232
+ }
233
+ case _stellarBase.xdr.ScSpecType.scSpecTypeU128().value:
234
+ {
235
+ ref = "U128";
236
+ break;
237
+ }
238
+ case _stellarBase.xdr.ScSpecType.scSpecTypeI128().value:
239
+ {
240
+ ref = "I128";
241
+ break;
242
+ }
243
+ case _stellarBase.xdr.ScSpecType.scSpecTypeU256().value:
244
+ {
245
+ ref = "U256";
246
+ break;
247
+ }
248
+ case _stellarBase.xdr.ScSpecType.scSpecTypeI256().value:
249
+ {
250
+ ref = "I256";
251
+ break;
252
+ }
253
+ case _stellarBase.xdr.ScSpecType.scSpecTypeBytes().value:
254
+ {
255
+ ref = "DataUrl";
256
+ break;
257
+ }
258
+ case _stellarBase.xdr.ScSpecType.scSpecTypeString().value:
259
+ {
260
+ ref = "ScString";
261
+ break;
262
+ }
263
+ case _stellarBase.xdr.ScSpecType.scSpecTypeSymbol().value:
264
+ {
265
+ ref = "ScSymbol";
266
+ break;
267
+ }
268
+ case _stellarBase.xdr.ScSpecType.scSpecTypeAddress().value:
269
+ {
270
+ ref = "Address";
271
+ break;
272
+ }
273
+ case _stellarBase.xdr.ScSpecType.scSpecTypeOption().value:
274
+ {
275
+ var opt = typeDef.option();
276
+ return typeRef(opt.valueType());
277
+ }
278
+ case _stellarBase.xdr.ScSpecType.scSpecTypeResult().value:
279
+ {
280
+ break;
281
+ }
282
+ case _stellarBase.xdr.ScSpecType.scSpecTypeVec().value:
283
+ {
284
+ var arr = typeDef.vec();
285
+ var reference = typeRef(arr.elementType());
286
+ return {
287
+ type: "array",
288
+ items: reference
289
+ };
290
+ }
291
+ case _stellarBase.xdr.ScSpecType.scSpecTypeMap().value:
292
+ {
293
+ var map = typeDef.map();
294
+ var items = [typeRef(map.keyType()), typeRef(map.valueType())];
295
+ return {
296
+ type: "array",
297
+ items: {
298
+ type: "array",
299
+ items: items,
300
+ minItems: 2,
301
+ maxItems: 2
302
+ }
303
+ };
304
+ }
305
+ case _stellarBase.xdr.ScSpecType.scSpecTypeTuple().value:
306
+ {
307
+ var tuple = typeDef.tuple();
308
+ var minItems = tuple.valueTypes().length;
309
+ var maxItems = minItems;
310
+ var _items = tuple.valueTypes().map(typeRef);
311
+ return {
312
+ type: "array",
313
+ items: _items,
314
+ minItems: minItems,
315
+ maxItems: maxItems
316
+ };
317
+ }
318
+ case _stellarBase.xdr.ScSpecType.scSpecTypeBytesN().value:
319
+ {
320
+ var _arr = typeDef.bytesN();
321
+ return {
322
+ $ref: "#/definitions/DataUrl",
323
+ maxLength: _arr.n()
324
+ };
325
+ }
326
+ case _stellarBase.xdr.ScSpecType.scSpecTypeUdt().value:
327
+ {
328
+ var udt = typeDef.udt();
329
+ ref = udt.name().toString();
330
+ break;
331
+ }
332
+ }
333
+ return {
334
+ $ref: "#/definitions/".concat(ref)
335
+ };
336
+ }
337
+ function isRequired(typeDef) {
338
+ return typeDef.switch().value !== _stellarBase.xdr.ScSpecType.scSpecTypeOption().value;
339
+ }
340
+ function argsAndRequired(input) {
341
+ var properties = {};
342
+ var required = [];
343
+ input.forEach(function (arg) {
344
+ var aType = arg.type();
345
+ var name = arg.name().toString();
346
+ properties[name] = typeRef(aType);
347
+ if (isRequired(aType)) {
348
+ required.push(name);
349
+ }
350
+ });
351
+ var res = {
352
+ properties: properties
353
+ };
354
+ if (required.length > 0) {
355
+ res.required = required;
356
+ }
357
+ return res;
358
+ }
359
+ function structToJsonSchema(udt) {
360
+ var fields = udt.fields();
361
+ if (fields.some(isNumeric)) {
362
+ if (!fields.every(isNumeric)) {
363
+ throw new Error("mixed numeric and non-numeric field names are not allowed");
364
+ }
365
+ var items = fields.map(function (_, i) {
366
+ return typeRef(fields[i].type());
367
+ });
368
+ return {
369
+ type: "array",
370
+ items: items,
371
+ minItems: fields.length,
372
+ maxItems: fields.length
373
+ };
374
+ }
375
+ var description = udt.doc().toString();
376
+ var _argsAndRequired = argsAndRequired(fields),
377
+ properties = _argsAndRequired.properties,
378
+ required = _argsAndRequired.required;
379
+ properties.additionalProperties = false;
380
+ return {
381
+ description: description,
382
+ properties: properties,
383
+ required: required,
384
+ type: "object"
385
+ };
386
+ }
387
+ function functionToJsonSchema(func) {
388
+ var _argsAndRequired2 = argsAndRequired(func.inputs()),
389
+ properties = _argsAndRequired2.properties,
390
+ required = _argsAndRequired2.required;
391
+ var args = {
392
+ additionalProperties: false,
393
+ properties: properties,
394
+ type: "object"
395
+ };
396
+ if ((required === null || required === void 0 ? void 0 : required.length) > 0) {
397
+ args.required = required;
398
+ }
399
+ var input = {
400
+ properties: {
401
+ args: args
402
+ }
403
+ };
404
+ var outputs = func.outputs();
405
+ var output = outputs.length > 0 ? typeRef(outputs[0]) : typeRef(_stellarBase.xdr.ScSpecTypeDef.scSpecTypeVoid());
406
+ var description = func.doc().toString();
407
+ if (description.length > 0) {
408
+ input.description = description;
409
+ }
410
+ input.additionalProperties = false;
411
+ output.additionalProperties = false;
412
+ return {
413
+ input: input,
414
+ output: output
415
+ };
416
+ }
417
+ function unionToJsonSchema(udt) {
418
+ var description = udt.doc().toString();
419
+ var cases = udt.cases();
420
+ var oneOf = [];
421
+ cases.forEach(function (aCase) {
422
+ switch (aCase.switch().value) {
423
+ case _stellarBase.xdr.ScSpecUdtUnionCaseV0Kind.scSpecUdtUnionCaseVoidV0().value:
424
+ {
425
+ var c = aCase.voidCase();
426
+ var title = c.name().toString();
427
+ oneOf.push({
428
+ type: "object",
429
+ title: title,
430
+ properties: {
431
+ tag: title
432
+ },
433
+ additionalProperties: false,
434
+ required: ["tag"]
435
+ });
436
+ break;
437
+ }
438
+ case _stellarBase.xdr.ScSpecUdtUnionCaseV0Kind.scSpecUdtUnionCaseTupleV0().value:
439
+ {
440
+ var _c = aCase.tupleCase();
441
+ var _title = _c.name().toString();
442
+ oneOf.push({
443
+ type: "object",
444
+ title: _title,
445
+ properties: {
446
+ tag: _title,
447
+ values: {
448
+ type: "array",
449
+ items: _c.type().map(typeRef)
450
+ }
451
+ },
452
+ required: ["tag", "values"],
453
+ additionalProperties: false
454
+ });
455
+ }
456
+ }
457
+ });
458
+ var res = {
459
+ oneOf: oneOf
460
+ };
461
+ if (description.length > 0) {
462
+ res.description = description;
463
+ }
464
+ return res;
465
+ }
466
+ var Spec = exports.Spec = function () {
467
+ function Spec(entries) {
468
+ _classCallCheck(this, Spec);
469
+ _defineProperty(this, "entries", []);
470
+ if (entries.length === 0) {
471
+ throw new Error("Contract spec must have at least one entry");
472
+ }
473
+ var entry = entries[0];
474
+ if (typeof entry === "string") {
475
+ this.entries = entries.map(function (s) {
476
+ return _stellarBase.xdr.ScSpecEntry.fromXDR(s, "base64");
477
+ });
478
+ } else {
479
+ this.entries = entries;
480
+ }
481
+ }
482
+ return _createClass(Spec, [{
483
+ key: "funcs",
484
+ value: function funcs() {
485
+ return this.entries.filter(function (entry) {
486
+ return entry.switch().value === _stellarBase.xdr.ScSpecEntryKind.scSpecEntryFunctionV0().value;
487
+ }).map(function (entry) {
488
+ return entry.functionV0();
489
+ });
490
+ }
491
+ }, {
492
+ key: "getFunc",
493
+ value: function getFunc(name) {
494
+ var entry = this.findEntry(name);
495
+ if (entry.switch().value !== _stellarBase.xdr.ScSpecEntryKind.scSpecEntryFunctionV0().value) {
496
+ throw new Error("".concat(name, " is not a function"));
497
+ }
498
+ return entry.functionV0();
499
+ }
500
+ }, {
501
+ key: "funcArgsToScVals",
502
+ value: function funcArgsToScVals(name, args) {
503
+ var _this = this;
504
+ var fn = this.getFunc(name);
505
+ return fn.inputs().map(function (input) {
506
+ return _this.nativeToScVal(readObj(args, input), input.type());
507
+ });
508
+ }
509
+ }, {
510
+ key: "funcResToNative",
511
+ value: function funcResToNative(name, val_or_base64) {
512
+ var val = typeof val_or_base64 === "string" ? _stellarBase.xdr.ScVal.fromXDR(val_or_base64, "base64") : val_or_base64;
513
+ var func = this.getFunc(name);
514
+ var outputs = func.outputs();
515
+ if (outputs.length === 0) {
516
+ var type = val.switch();
517
+ if (type.value !== _stellarBase.xdr.ScValType.scvVoid().value) {
518
+ throw new Error("Expected void, got ".concat(type.name));
519
+ }
520
+ return null;
521
+ }
522
+ if (outputs.length > 1) {
523
+ throw new Error("Multiple outputs not supported");
524
+ }
525
+ var output = outputs[0];
526
+ if (output.switch().value === _stellarBase.xdr.ScSpecType.scSpecTypeResult().value) {
527
+ return new _rust_result.Ok(this.scValToNative(val, output.result().okType()));
528
+ }
529
+ return this.scValToNative(val, output);
530
+ }
531
+ }, {
532
+ key: "findEntry",
533
+ value: function findEntry(name) {
534
+ var entry = this.entries.find(function (e) {
535
+ return e.value().name().toString() === name;
536
+ });
537
+ if (!entry) {
538
+ throw new Error("no such entry: ".concat(name));
539
+ }
540
+ return entry;
541
+ }
542
+ }, {
543
+ key: "nativeToScVal",
544
+ value: function nativeToScVal(val, ty) {
545
+ var _this2 = this;
546
+ var t = ty.switch();
547
+ var value = t.value;
548
+ if (t.value === _stellarBase.xdr.ScSpecType.scSpecTypeUdt().value) {
549
+ var udt = ty.udt();
550
+ return this.nativeToUdt(val, udt.name().toString());
551
+ }
552
+ if (value === _stellarBase.xdr.ScSpecType.scSpecTypeOption().value) {
553
+ var opt = ty.option();
554
+ if (val === undefined) {
555
+ return _stellarBase.xdr.ScVal.scvVoid();
556
+ }
557
+ return this.nativeToScVal(val, opt.valueType());
558
+ }
559
+ switch (_typeof(val)) {
560
+ case "object":
561
+ {
562
+ var _val$constructor$name, _val$constructor;
563
+ if (val === null) {
564
+ switch (value) {
565
+ case _stellarBase.xdr.ScSpecType.scSpecTypeVoid().value:
566
+ return _stellarBase.xdr.ScVal.scvVoid();
567
+ default:
568
+ throw new TypeError("Type ".concat(ty, " was not void, but value was null"));
569
+ }
570
+ }
571
+ if (val instanceof _stellarBase.xdr.ScVal) {
572
+ return val;
573
+ }
574
+ if (val instanceof _stellarBase.Address) {
575
+ if (ty.switch().value !== _stellarBase.xdr.ScSpecType.scSpecTypeAddress().value) {
576
+ throw new TypeError("Type ".concat(ty, " was not address, but value was Address"));
577
+ }
578
+ return val.toScVal();
579
+ }
580
+ if (val instanceof _stellarBase.Contract) {
581
+ if (ty.switch().value !== _stellarBase.xdr.ScSpecType.scSpecTypeAddress().value) {
582
+ throw new TypeError("Type ".concat(ty, " was not address, but value was Address"));
583
+ }
584
+ return val.address().toScVal();
585
+ }
586
+ if (val instanceof Uint8Array || Buffer.isBuffer(val)) {
587
+ var copy = Uint8Array.from(val);
588
+ switch (value) {
589
+ case _stellarBase.xdr.ScSpecType.scSpecTypeBytesN().value:
590
+ {
591
+ var bytesN = ty.bytesN();
592
+ if (copy.length !== bytesN.n()) {
593
+ throw new TypeError("expected ".concat(bytesN.n(), " bytes, but got ").concat(copy.length));
594
+ }
595
+ return _stellarBase.xdr.ScVal.scvBytes(copy);
596
+ }
597
+ case _stellarBase.xdr.ScSpecType.scSpecTypeBytes().value:
598
+ return _stellarBase.xdr.ScVal.scvBytes(copy);
599
+ default:
600
+ throw new TypeError("invalid type (".concat(ty, ") specified for Bytes and BytesN"));
601
+ }
602
+ }
603
+ if (Array.isArray(val)) {
604
+ switch (value) {
605
+ case _stellarBase.xdr.ScSpecType.scSpecTypeVec().value:
606
+ {
607
+ var vec = ty.vec();
608
+ var elementType = vec.elementType();
609
+ return _stellarBase.xdr.ScVal.scvVec(val.map(function (v) {
610
+ return _this2.nativeToScVal(v, elementType);
611
+ }));
612
+ }
613
+ case _stellarBase.xdr.ScSpecType.scSpecTypeTuple().value:
614
+ {
615
+ var tup = ty.tuple();
616
+ var valTypes = tup.valueTypes();
617
+ if (val.length !== valTypes.length) {
618
+ throw new TypeError("Tuple expects ".concat(valTypes.length, " values, but ").concat(val.length, " were provided"));
619
+ }
620
+ return _stellarBase.xdr.ScVal.scvVec(val.map(function (v, i) {
621
+ return _this2.nativeToScVal(v, valTypes[i]);
622
+ }));
623
+ }
624
+ case _stellarBase.xdr.ScSpecType.scSpecTypeMap().value:
625
+ {
626
+ var map = ty.map();
627
+ var keyType = map.keyType();
628
+ var valueType = map.valueType();
629
+ return _stellarBase.xdr.ScVal.scvMap(val.map(function (entry) {
630
+ var key = _this2.nativeToScVal(entry[0], keyType);
631
+ var mapVal = _this2.nativeToScVal(entry[1], valueType);
632
+ return new _stellarBase.xdr.ScMapEntry({
633
+ key: key,
634
+ val: mapVal
635
+ });
636
+ }));
637
+ }
638
+ default:
639
+ throw new TypeError("Type ".concat(ty, " was not vec, but value was Array"));
640
+ }
641
+ }
642
+ if (val.constructor === Map) {
643
+ if (value !== _stellarBase.xdr.ScSpecType.scSpecTypeMap().value) {
644
+ throw new TypeError("Type ".concat(ty, " was not map, but value was Map"));
645
+ }
646
+ var scMap = ty.map();
647
+ var _map = val;
648
+ var entries = [];
649
+ var values = _map.entries();
650
+ var res = values.next();
651
+ while (!res.done) {
652
+ var _res$value = _slicedToArray(res.value, 2),
653
+ k = _res$value[0],
654
+ v = _res$value[1];
655
+ var key = this.nativeToScVal(k, scMap.keyType());
656
+ var mapval = this.nativeToScVal(v, scMap.valueType());
657
+ entries.push(new _stellarBase.xdr.ScMapEntry({
658
+ key: key,
659
+ val: mapval
660
+ }));
661
+ res = values.next();
662
+ }
663
+ return _stellarBase.xdr.ScVal.scvMap(entries);
664
+ }
665
+ if (((_val$constructor$name = (_val$constructor = val.constructor) === null || _val$constructor === void 0 ? void 0 : _val$constructor.name) !== null && _val$constructor$name !== void 0 ? _val$constructor$name : "") !== "Object") {
666
+ var _val$constructor2;
667
+ throw new TypeError("cannot interpret ".concat((_val$constructor2 = val.constructor) === null || _val$constructor2 === void 0 ? void 0 : _val$constructor2.name, " value as ScVal (").concat(JSON.stringify(val), ")"));
668
+ }
669
+ throw new TypeError("Received object ".concat(val, " did not match the provided type ").concat(ty));
670
+ }
671
+ case "number":
672
+ case "bigint":
673
+ {
674
+ switch (value) {
675
+ case _stellarBase.xdr.ScSpecType.scSpecTypeU32().value:
676
+ return _stellarBase.xdr.ScVal.scvU32(val);
677
+ case _stellarBase.xdr.ScSpecType.scSpecTypeI32().value:
678
+ return _stellarBase.xdr.ScVal.scvI32(val);
679
+ case _stellarBase.xdr.ScSpecType.scSpecTypeU64().value:
680
+ case _stellarBase.xdr.ScSpecType.scSpecTypeI64().value:
681
+ case _stellarBase.xdr.ScSpecType.scSpecTypeU128().value:
682
+ case _stellarBase.xdr.ScSpecType.scSpecTypeI128().value:
683
+ case _stellarBase.xdr.ScSpecType.scSpecTypeU256().value:
684
+ case _stellarBase.xdr.ScSpecType.scSpecTypeI256().value:
685
+ {
686
+ var intType = t.name.substring(10).toLowerCase();
687
+ return new _stellarBase.XdrLargeInt(intType, val).toScVal();
688
+ }
689
+ default:
690
+ throw new TypeError("invalid type (".concat(ty, ") specified for integer"));
691
+ }
692
+ }
693
+ case "string":
694
+ return stringToScVal(val, t);
695
+ case "boolean":
696
+ {
697
+ if (value !== _stellarBase.xdr.ScSpecType.scSpecTypeBool().value) {
698
+ throw TypeError("Type ".concat(ty, " was not bool, but value was bool"));
699
+ }
700
+ return _stellarBase.xdr.ScVal.scvBool(val);
701
+ }
702
+ case "undefined":
703
+ {
704
+ if (!ty) {
705
+ return _stellarBase.xdr.ScVal.scvVoid();
706
+ }
707
+ switch (value) {
708
+ case _stellarBase.xdr.ScSpecType.scSpecTypeVoid().value:
709
+ case _stellarBase.xdr.ScSpecType.scSpecTypeOption().value:
710
+ return _stellarBase.xdr.ScVal.scvVoid();
711
+ default:
712
+ throw new TypeError("Type ".concat(ty, " was not void, but value was undefined"));
713
+ }
714
+ }
715
+ case "function":
716
+ return this.nativeToScVal(val(), ty);
717
+ default:
718
+ throw new TypeError("failed to convert typeof ".concat(_typeof(val), " (").concat(val, ")"));
719
+ }
720
+ }
721
+ }, {
722
+ key: "nativeToUdt",
723
+ value: function nativeToUdt(val, name) {
724
+ var entry = this.findEntry(name);
725
+ switch (entry.switch()) {
726
+ case _stellarBase.xdr.ScSpecEntryKind.scSpecEntryUdtEnumV0():
727
+ if (typeof val !== "number") {
728
+ throw new TypeError("expected number for enum ".concat(name, ", but got ").concat(_typeof(val)));
729
+ }
730
+ return this.nativeToEnum(val, entry.udtEnumV0());
731
+ case _stellarBase.xdr.ScSpecEntryKind.scSpecEntryUdtStructV0():
732
+ return this.nativeToStruct(val, entry.udtStructV0());
733
+ case _stellarBase.xdr.ScSpecEntryKind.scSpecEntryUdtUnionV0():
734
+ return this.nativeToUnion(val, entry.udtUnionV0());
735
+ default:
736
+ throw new Error("failed to parse udt ".concat(name));
737
+ }
738
+ }
739
+ }, {
740
+ key: "nativeToUnion",
741
+ value: function nativeToUnion(val, union_) {
742
+ var _this3 = this;
743
+ var entryName = val.tag;
744
+ var caseFound = union_.cases().find(function (entry) {
745
+ var caseN = entry.value().name().toString();
746
+ return caseN === entryName;
747
+ });
748
+ if (!caseFound) {
749
+ throw new TypeError("no such enum entry: ".concat(entryName, " in ").concat(union_));
750
+ }
751
+ var key = _stellarBase.xdr.ScVal.scvSymbol(entryName);
752
+ switch (caseFound.switch()) {
753
+ case _stellarBase.xdr.ScSpecUdtUnionCaseV0Kind.scSpecUdtUnionCaseVoidV0():
754
+ {
755
+ return _stellarBase.xdr.ScVal.scvVec([key]);
756
+ }
757
+ case _stellarBase.xdr.ScSpecUdtUnionCaseV0Kind.scSpecUdtUnionCaseTupleV0():
758
+ {
759
+ var types = caseFound.tupleCase().type();
760
+ if (Array.isArray(val.values)) {
761
+ if (val.values.length !== types.length) {
762
+ throw new TypeError("union ".concat(union_, " expects ").concat(types.length, " values, but got ").concat(val.values.length));
763
+ }
764
+ var scvals = val.values.map(function (v, i) {
765
+ return _this3.nativeToScVal(v, types[i]);
766
+ });
767
+ scvals.unshift(key);
768
+ return _stellarBase.xdr.ScVal.scvVec(scvals);
769
+ }
770
+ throw new Error("failed to parse union case ".concat(caseFound, " with ").concat(val));
771
+ }
772
+ default:
773
+ throw new Error("failed to parse union ".concat(union_, " with ").concat(val));
774
+ }
775
+ }
776
+ }, {
777
+ key: "nativeToStruct",
778
+ value: function nativeToStruct(val, struct) {
779
+ var _this4 = this;
780
+ var fields = struct.fields();
781
+ if (fields.some(isNumeric)) {
782
+ if (!fields.every(isNumeric)) {
783
+ throw new Error("mixed numeric and non-numeric field names are not allowed");
784
+ }
785
+ return _stellarBase.xdr.ScVal.scvVec(fields.map(function (_, i) {
786
+ return _this4.nativeToScVal(val[i], fields[i].type());
787
+ }));
788
+ }
789
+ return _stellarBase.xdr.ScVal.scvMap(fields.map(function (field) {
790
+ var name = field.name().toString();
791
+ return new _stellarBase.xdr.ScMapEntry({
792
+ key: _this4.nativeToScVal(name, _stellarBase.xdr.ScSpecTypeDef.scSpecTypeSymbol()),
793
+ val: _this4.nativeToScVal(val[name], field.type())
794
+ });
795
+ }));
796
+ }
797
+ }, {
798
+ key: "nativeToEnum",
799
+ value: function nativeToEnum(val, enum_) {
800
+ if (enum_.cases().some(function (entry) {
801
+ return entry.value() === val;
802
+ })) {
803
+ return _stellarBase.xdr.ScVal.scvU32(val);
804
+ }
805
+ throw new TypeError("no such enum entry: ".concat(val, " in ").concat(enum_));
806
+ }
807
+ }, {
808
+ key: "scValStrToNative",
809
+ value: function scValStrToNative(scv, typeDef) {
810
+ return this.scValToNative(_stellarBase.xdr.ScVal.fromXDR(scv, "base64"), typeDef);
811
+ }
812
+ }, {
813
+ key: "scValToNative",
814
+ value: function scValToNative(scv, typeDef) {
815
+ var _this5 = this;
816
+ var t = typeDef.switch();
817
+ var value = t.value;
818
+ if (value === _stellarBase.xdr.ScSpecType.scSpecTypeUdt().value) {
819
+ return this.scValUdtToNative(scv, typeDef.udt());
820
+ }
821
+ switch (scv.switch().value) {
822
+ case _stellarBase.xdr.ScValType.scvVoid().value:
823
+ return undefined;
824
+ case _stellarBase.xdr.ScValType.scvU64().value:
825
+ case _stellarBase.xdr.ScValType.scvI64().value:
826
+ case _stellarBase.xdr.ScValType.scvU128().value:
827
+ case _stellarBase.xdr.ScValType.scvI128().value:
828
+ case _stellarBase.xdr.ScValType.scvU256().value:
829
+ case _stellarBase.xdr.ScValType.scvI256().value:
830
+ return (0, _stellarBase.scValToBigInt)(scv);
831
+ case _stellarBase.xdr.ScValType.scvVec().value:
832
+ {
833
+ if (value === _stellarBase.xdr.ScSpecType.scSpecTypeVec().value) {
834
+ var _scv$vec;
835
+ var vec = typeDef.vec();
836
+ return ((_scv$vec = scv.vec()) !== null && _scv$vec !== void 0 ? _scv$vec : []).map(function (elm) {
837
+ return _this5.scValToNative(elm, vec.elementType());
838
+ });
839
+ }
840
+ if (value === _stellarBase.xdr.ScSpecType.scSpecTypeTuple().value) {
841
+ var _scv$vec2;
842
+ var tuple = typeDef.tuple();
843
+ var valTypes = tuple.valueTypes();
844
+ return ((_scv$vec2 = scv.vec()) !== null && _scv$vec2 !== void 0 ? _scv$vec2 : []).map(function (elm, i) {
845
+ return _this5.scValToNative(elm, valTypes[i]);
846
+ });
847
+ }
848
+ throw new TypeError("Type ".concat(typeDef, " was not vec, but ").concat(scv, " is"));
849
+ }
850
+ case _stellarBase.xdr.ScValType.scvAddress().value:
851
+ return _stellarBase.Address.fromScVal(scv).toString();
852
+ case _stellarBase.xdr.ScValType.scvMap().value:
853
+ {
854
+ var _scv$map;
855
+ var map = (_scv$map = scv.map()) !== null && _scv$map !== void 0 ? _scv$map : [];
856
+ if (value === _stellarBase.xdr.ScSpecType.scSpecTypeMap().value) {
857
+ var typed = typeDef.map();
858
+ var keyType = typed.keyType();
859
+ var valueType = typed.valueType();
860
+ var res = map.map(function (entry) {
861
+ return [_this5.scValToNative(entry.key(), keyType), _this5.scValToNative(entry.val(), valueType)];
862
+ });
863
+ return res;
864
+ }
865
+ throw new TypeError("ScSpecType ".concat(t.name, " was not map, but ").concat(JSON.stringify(scv, null, 2), " is"));
866
+ }
867
+ case _stellarBase.xdr.ScValType.scvBool().value:
868
+ case _stellarBase.xdr.ScValType.scvU32().value:
869
+ case _stellarBase.xdr.ScValType.scvI32().value:
870
+ case _stellarBase.xdr.ScValType.scvBytes().value:
871
+ return scv.value();
872
+ case _stellarBase.xdr.ScValType.scvString().value:
873
+ case _stellarBase.xdr.ScValType.scvSymbol().value:
874
+ {
875
+ var _scv$value;
876
+ if (value !== _stellarBase.xdr.ScSpecType.scSpecTypeString().value && value !== _stellarBase.xdr.ScSpecType.scSpecTypeSymbol().value) {
877
+ throw new Error("ScSpecType ".concat(t.name, " was not string or symbol, but ").concat(JSON.stringify(scv, null, 2), " is"));
878
+ }
879
+ return (_scv$value = scv.value()) === null || _scv$value === void 0 ? void 0 : _scv$value.toString();
880
+ }
881
+ case _stellarBase.xdr.ScValType.scvTimepoint().value:
882
+ case _stellarBase.xdr.ScValType.scvDuration().value:
883
+ return (0, _stellarBase.scValToBigInt)(_stellarBase.xdr.ScVal.scvU64(scv.u64()));
884
+ default:
885
+ throw new TypeError("failed to convert ".concat(JSON.stringify(scv, null, 2), " to native type from type ").concat(t.name));
886
+ }
887
+ }
888
+ }, {
889
+ key: "scValUdtToNative",
890
+ value: function scValUdtToNative(scv, udt) {
891
+ var entry = this.findEntry(udt.name().toString());
892
+ switch (entry.switch()) {
893
+ case _stellarBase.xdr.ScSpecEntryKind.scSpecEntryUdtEnumV0():
894
+ return this.enumToNative(scv);
895
+ case _stellarBase.xdr.ScSpecEntryKind.scSpecEntryUdtStructV0():
896
+ return this.structToNative(scv, entry.udtStructV0());
897
+ case _stellarBase.xdr.ScSpecEntryKind.scSpecEntryUdtUnionV0():
898
+ return this.unionToNative(scv, entry.udtUnionV0());
899
+ default:
900
+ throw new Error("failed to parse udt ".concat(udt.name().toString(), ": ").concat(entry));
901
+ }
902
+ }
903
+ }, {
904
+ key: "unionToNative",
905
+ value: function unionToNative(val, udt) {
906
+ var _this6 = this;
907
+ var vec = val.vec();
908
+ if (!vec) {
909
+ throw new Error("".concat(JSON.stringify(val, null, 2), " is not a vec"));
910
+ }
911
+ if (vec.length === 0 && udt.cases.length !== 0) {
912
+ throw new Error("".concat(val, " has length 0, but the there are at least one case in the union"));
913
+ }
914
+ var name = vec[0].sym().toString();
915
+ if (vec[0].switch().value !== _stellarBase.xdr.ScValType.scvSymbol().value) {
916
+ throw new Error("{vec[0]} is not a symbol");
917
+ }
918
+ var entry = udt.cases().find(findCase(name));
919
+ if (!entry) {
920
+ throw new Error("failed to find entry ".concat(name, " in union {udt.name().toString()}"));
921
+ }
922
+ var res = {
923
+ tag: name
924
+ };
925
+ if (entry.switch().value === _stellarBase.xdr.ScSpecUdtUnionCaseV0Kind.scSpecUdtUnionCaseTupleV0().value) {
926
+ var tuple = entry.tupleCase();
927
+ var ty = tuple.type();
928
+ var values = ty.map(function (e, i) {
929
+ return _this6.scValToNative(vec[i + 1], e);
930
+ });
931
+ res.values = values;
932
+ }
933
+ return res;
934
+ }
935
+ }, {
936
+ key: "structToNative",
937
+ value: function structToNative(val, udt) {
938
+ var _this7 = this,
939
+ _val$map;
940
+ var res = {};
941
+ var fields = udt.fields();
942
+ if (fields.some(isNumeric)) {
943
+ var _val$vec;
944
+ var r = (_val$vec = val.vec()) === null || _val$vec === void 0 ? void 0 : _val$vec.map(function (entry, i) {
945
+ return _this7.scValToNative(entry, fields[i].type());
946
+ });
947
+ return r;
948
+ }
949
+ (_val$map = val.map()) === null || _val$map === void 0 || _val$map.forEach(function (entry, i) {
950
+ var field = fields[i];
951
+ res[field.name().toString()] = _this7.scValToNative(entry.val(), field.type());
952
+ });
953
+ return res;
954
+ }
955
+ }, {
956
+ key: "enumToNative",
957
+ value: function enumToNative(scv) {
958
+ if (scv.switch().value !== _stellarBase.xdr.ScValType.scvU32().value) {
959
+ throw new Error("Enum must have a u32 value");
960
+ }
961
+ var num = scv.u32();
962
+ return num;
963
+ }
964
+ }, {
965
+ key: "errorCases",
966
+ value: function errorCases() {
967
+ return this.entries.filter(function (entry) {
968
+ return entry.switch().value === _stellarBase.xdr.ScSpecEntryKind.scSpecEntryUdtErrorEnumV0().value;
969
+ }).flatMap(function (entry) {
970
+ return entry.value().cases();
971
+ });
972
+ }
973
+ }, {
974
+ key: "jsonSchema",
975
+ value: function jsonSchema(funcName) {
976
+ var definitions = {};
977
+ this.entries.forEach(function (entry) {
978
+ switch (entry.switch().value) {
979
+ case _stellarBase.xdr.ScSpecEntryKind.scSpecEntryUdtEnumV0().value:
980
+ {
981
+ var udt = entry.udtEnumV0();
982
+ definitions[udt.name().toString()] = enumToJsonSchema(udt);
983
+ break;
984
+ }
985
+ case _stellarBase.xdr.ScSpecEntryKind.scSpecEntryUdtStructV0().value:
986
+ {
987
+ var _udt = entry.udtStructV0();
988
+ definitions[_udt.name().toString()] = structToJsonSchema(_udt);
989
+ break;
990
+ }
991
+ case _stellarBase.xdr.ScSpecEntryKind.scSpecEntryUdtUnionV0().value:
992
+ {
993
+ var _udt2 = entry.udtUnionV0();
994
+ definitions[_udt2.name().toString()] = unionToJsonSchema(_udt2);
995
+ break;
996
+ }
997
+ case _stellarBase.xdr.ScSpecEntryKind.scSpecEntryFunctionV0().value:
998
+ {
999
+ var fn = entry.functionV0();
1000
+ var fnName = fn.name().toString();
1001
+ var _functionToJsonSchema = functionToJsonSchema(fn),
1002
+ input = _functionToJsonSchema.input;
1003
+ definitions[fnName] = input;
1004
+ break;
1005
+ }
1006
+ case _stellarBase.xdr.ScSpecEntryKind.scSpecEntryUdtErrorEnumV0().value:
1007
+ {}
1008
+ }
1009
+ });
1010
+ var res = {
1011
+ $schema: "http://json-schema.org/draft-07/schema#",
1012
+ definitions: _objectSpread(_objectSpread({}, PRIMITIVE_DEFINITONS), definitions)
1013
+ };
1014
+ if (funcName) {
1015
+ res.$ref = "#/definitions/".concat(funcName);
1016
+ }
1017
+ return res;
1018
+ }
1019
+ }]);
1020
+ }();