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