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