@stellar/stellar-sdk 11.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/CHANGELOG.md +1275 -0
  2. package/LICENSE +228 -0
  3. package/README.md +274 -0
  4. package/dist/stellar-sdk.js +56470 -0
  5. package/dist/stellar-sdk.min.js +2 -0
  6. package/dist/stellar-sdk.min.js.LICENSE.txt +71 -0
  7. package/lib/browser.d.ts +6 -0
  8. package/lib/browser.js +37 -0
  9. package/lib/config.d.ts +52 -0
  10. package/lib/config.js +49 -0
  11. package/lib/contract_spec.d.ts +130 -0
  12. package/lib/contract_spec.js +549 -0
  13. package/lib/errors.d.ts +59 -0
  14. package/lib/errors.js +105 -0
  15. package/lib/federation/api.d.ts +11 -0
  16. package/lib/federation/api.js +7 -0
  17. package/lib/federation/index.d.ts +2 -0
  18. package/lib/federation/index.js +24 -0
  19. package/lib/federation/server.d.ts +118 -0
  20. package/lib/federation/server.js +253 -0
  21. package/lib/friendbot/index.d.ts +5 -0
  22. package/lib/friendbot/index.js +7 -0
  23. package/lib/horizon/account_call_builder.d.ts +55 -0
  24. package/lib/horizon/account_call_builder.js +64 -0
  25. package/lib/horizon/account_response.d.ts +58 -0
  26. package/lib/horizon/account_response.js +50 -0
  27. package/lib/horizon/assets_call_builder.d.ts +27 -0
  28. package/lib/horizon/assets_call_builder.js +45 -0
  29. package/lib/horizon/call_builder.d.ts +128 -0
  30. package/lib/horizon/call_builder.js +360 -0
  31. package/lib/horizon/claimable_balances_call_builder.d.ts +49 -0
  32. package/lib/horizon/claimable_balances_call_builder.js +58 -0
  33. package/lib/horizon/effect_call_builder.d.ts +53 -0
  34. package/lib/horizon/effect_call_builder.js +58 -0
  35. package/lib/horizon/friendbot_builder.d.ts +5 -0
  36. package/lib/horizon/friendbot_builder.js +33 -0
  37. package/lib/horizon/horizon_api.d.ts +523 -0
  38. package/lib/horizon/horizon_api.js +96 -0
  39. package/lib/horizon/horizon_axios_client.d.ts +30 -0
  40. package/lib/horizon/horizon_axios_client.js +48 -0
  41. package/lib/horizon/index.d.ts +7 -0
  42. package/lib/horizon/index.js +78 -0
  43. package/lib/horizon/ledger_call_builder.d.ts +22 -0
  44. package/lib/horizon/ledger_call_builder.js +39 -0
  45. package/lib/horizon/liquidity_pool_call_builder.d.ts +38 -0
  46. package/lib/horizon/liquidity_pool_call_builder.js +61 -0
  47. package/lib/horizon/offer_call_builder.d.ts +64 -0
  48. package/lib/horizon/offer_call_builder.js +81 -0
  49. package/lib/horizon/operation_call_builder.d.ts +68 -0
  50. package/lib/horizon/operation_call_builder.js +71 -0
  51. package/lib/horizon/orderbook_call_builder.d.ts +16 -0
  52. package/lib/horizon/orderbook_call_builder.js +46 -0
  53. package/lib/horizon/path_call_builder.d.ts +31 -0
  54. package/lib/horizon/path_call_builder.js +42 -0
  55. package/lib/horizon/payment_call_builder.d.ts +36 -0
  56. package/lib/horizon/payment_call_builder.js +48 -0
  57. package/lib/horizon/server.d.ts +361 -0
  58. package/lib/horizon/server.js +507 -0
  59. package/lib/horizon/server_api.d.ts +279 -0
  60. package/lib/horizon/server_api.js +19 -0
  61. package/lib/horizon/strict_receive_path_call_builder.d.ts +34 -0
  62. package/lib/horizon/strict_receive_path_call_builder.js +51 -0
  63. package/lib/horizon/strict_send_path_call_builder.d.ts +34 -0
  64. package/lib/horizon/strict_send_path_call_builder.js +51 -0
  65. package/lib/horizon/trade_aggregation_call_builder.d.ts +48 -0
  66. package/lib/horizon/trade_aggregation_call_builder.js +82 -0
  67. package/lib/horizon/trades_call_builder.d.ts +50 -0
  68. package/lib/horizon/trades_call_builder.js +74 -0
  69. package/lib/horizon/transaction_call_builder.d.ts +58 -0
  70. package/lib/horizon/transaction_call_builder.js +66 -0
  71. package/lib/horizon/types/account.d.ts +5 -0
  72. package/lib/horizon/types/account.js +5 -0
  73. package/lib/horizon/types/assets.d.ts +19 -0
  74. package/lib/horizon/types/assets.js +5 -0
  75. package/lib/horizon/types/effects.d.ts +285 -0
  76. package/lib/horizon/types/effects.js +62 -0
  77. package/lib/horizon/types/offer.d.ts +20 -0
  78. package/lib/horizon/types/offer.js +5 -0
  79. package/lib/horizon/types/trade.d.ts +13 -0
  80. package/lib/horizon/types/trade.js +5 -0
  81. package/lib/index.d.ts +14 -0
  82. package/lib/index.js +79 -0
  83. package/lib/soroban/api.d.ts +247 -0
  84. package/lib/soroban/api.js +32 -0
  85. package/lib/soroban/axios.d.ts +3 -0
  86. package/lib/soroban/axios.js +16 -0
  87. package/lib/soroban/browser.d.ts +6 -0
  88. package/lib/soroban/browser.js +37 -0
  89. package/lib/soroban/index.d.ts +8 -0
  90. package/lib/soroban/index.js +72 -0
  91. package/lib/soroban/jsonrpc.d.ts +33 -0
  92. package/lib/soroban/jsonrpc.js +93 -0
  93. package/lib/soroban/parsers.d.ts +17 -0
  94. package/lib/soroban/parsers.js +111 -0
  95. package/lib/soroban/server.d.ts +427 -0
  96. package/lib/soroban/server.js +598 -0
  97. package/lib/soroban/transaction.d.ts +20 -0
  98. package/lib/soroban/transaction.js +55 -0
  99. package/lib/soroban/utils.d.ts +1 -0
  100. package/lib/soroban/utils.js +9 -0
  101. package/lib/stellartoml/index.d.ts +121 -0
  102. package/lib/stellartoml/index.js +78 -0
  103. package/lib/utils.d.ts +12 -0
  104. package/lib/utils.js +32 -0
  105. package/lib/webauth/errors.d.ts +4 -0
  106. package/lib/webauth/errors.js +37 -0
  107. package/lib/webauth/index.d.ts +2 -0
  108. package/lib/webauth/index.js +27 -0
  109. package/lib/webauth/utils.d.ts +294 -0
  110. package/lib/webauth/utils.js +332 -0
  111. package/package.json +153 -0
  112. package/types/dom-monkeypatch.d.ts +126 -0
@@ -0,0 +1,549 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.ContractSpec = void 0;
7
+ var _stellarBase = require("@stellar/stellar-base");
8
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
9
+ 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
+ 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
+ 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
+ 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
+ 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); }
21
+ var ContractSpec = exports.ContractSpec = function () {
22
+ function ContractSpec(entries) {
23
+ _classCallCheck(this, ContractSpec);
24
+ _defineProperty(this, "entries", []);
25
+ if (entries.length == 0) {
26
+ throw new Error('Contract spec must have at least one entry');
27
+ }
28
+ var entry = entries[0];
29
+ if (typeof entry === 'string') {
30
+ this.entries = entries.map(function (s) {
31
+ return _stellarBase.xdr.ScSpecEntry.fromXDR(s, 'base64');
32
+ });
33
+ } else {
34
+ this.entries = entries;
35
+ }
36
+ }
37
+ _createClass(ContractSpec, [{
38
+ key: "getFunc",
39
+ value: function getFunc(name) {
40
+ var entry = this.findEntry(name);
41
+ if (entry.switch().value !== _stellarBase.xdr.ScSpecEntryKind.scSpecEntryFunctionV0().value) {
42
+ throw new Error("".concat(name, " is not a function"));
43
+ }
44
+ return entry.value();
45
+ }
46
+ }, {
47
+ key: "funcArgsToScVals",
48
+ value: function funcArgsToScVals(name, args) {
49
+ var _this = this;
50
+ var fn = this.getFunc(name);
51
+ return fn.inputs().map(function (input) {
52
+ return _this.nativeToScVal(readObj(args, input), input.type());
53
+ });
54
+ }
55
+ }, {
56
+ key: "funcResToNative",
57
+ 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;
59
+ var func = this.getFunc(name);
60
+ var outputs = func.outputs();
61
+ if (outputs.length === 0) {
62
+ var type = val.switch();
63
+ if (type.value !== _stellarBase.xdr.ScValType.scvVoid().value) {
64
+ throw new Error("Expected void, got ".concat(type.name));
65
+ }
66
+ return null;
67
+ }
68
+ if (outputs.length > 1) {
69
+ throw new Error("Multiple outputs not supported");
70
+ }
71
+ var output = outputs[0];
72
+ if (output.switch().value === _stellarBase.xdr.ScSpecType.scSpecTypeResult().value) {
73
+ return this.scValToNative(val, output.result().okType());
74
+ }
75
+ return this.scValToNative(val, output);
76
+ }
77
+ }, {
78
+ key: "findEntry",
79
+ value: function findEntry(name) {
80
+ var entry = this.entries.find(function (entry) {
81
+ return entry.value().name().toString() === name;
82
+ });
83
+ if (!entry) {
84
+ throw new Error("no such entry: ".concat(name));
85
+ }
86
+ return entry;
87
+ }
88
+ }, {
89
+ key: "nativeToScVal",
90
+ value: function nativeToScVal(val, ty) {
91
+ var _this2 = this;
92
+ var t = ty.switch();
93
+ var value = t.value;
94
+ if (t.value === _stellarBase.xdr.ScSpecType.scSpecTypeUdt().value) {
95
+ var udt = ty.value();
96
+ return this.nativeToUdt(val, udt.name().toString());
97
+ }
98
+ if (value === _stellarBase.xdr.ScSpecType.scSpecTypeOption().value) {
99
+ var opt = ty.value();
100
+ if (val === undefined) {
101
+ return _stellarBase.xdr.ScVal.scvVoid();
102
+ }
103
+ return this.nativeToScVal(val, opt.valueType());
104
+ }
105
+ switch (_typeof(val)) {
106
+ case 'object':
107
+ {
108
+ var _val$constructor$name, _val$constructor;
109
+ if (val === null) {
110
+ switch (value) {
111
+ case _stellarBase.xdr.ScSpecType.scSpecTypeVoid().value:
112
+ return _stellarBase.xdr.ScVal.scvVoid();
113
+ default:
114
+ throw new TypeError("Type ".concat(ty, " was not void, but value was null"));
115
+ }
116
+ }
117
+ if (val instanceof _stellarBase.xdr.ScVal) {
118
+ return val;
119
+ }
120
+ if (val instanceof _stellarBase.Address) {
121
+ if (ty.switch().value !== _stellarBase.xdr.ScSpecType.scSpecTypeAddress().value) {
122
+ throw new TypeError("Type ".concat(ty, " was not address, but value was Address"));
123
+ }
124
+ return val.toScVal();
125
+ }
126
+ if (val instanceof _stellarBase.Contract) {
127
+ if (ty.switch().value !== _stellarBase.xdr.ScSpecType.scSpecTypeAddress().value) {
128
+ throw new TypeError("Type ".concat(ty, " was not address, but value was Address"));
129
+ }
130
+ return val.address().toScVal();
131
+ }
132
+ if (val instanceof Uint8Array || Buffer.isBuffer(val)) {
133
+ var copy = Uint8Array.from(val);
134
+ switch (value) {
135
+ case _stellarBase.xdr.ScSpecType.scSpecTypeBytesN().value:
136
+ {
137
+ var bytes_n = ty.value();
138
+ if (copy.length !== bytes_n.n()) {
139
+ throw new TypeError("expected ".concat(bytes_n.n(), " bytes, but got ").concat(copy.length));
140
+ }
141
+ return _stellarBase.xdr.ScVal.scvBytes(copy);
142
+ }
143
+ case _stellarBase.xdr.ScSpecType.scSpecTypeBytes().value:
144
+ return _stellarBase.xdr.ScVal.scvBytes(copy);
145
+ default:
146
+ throw new TypeError("invalid type (".concat(ty, ") specified for Bytes and BytesN"));
147
+ }
148
+ }
149
+ 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"));
167
+ }
168
+ }
169
+ if (val.constructor === Map) {
170
+ if (value !== _stellarBase.xdr.ScSpecType.scSpecTypeMap().value) {
171
+ throw new TypeError("Type ".concat(ty, " was not map, but value was Map"));
172
+ }
173
+ var scMap = ty.value();
174
+ var map = val;
175
+ var entries = [];
176
+ var values = map.entries();
177
+ var res = values.next();
178
+ while (!res.done) {
179
+ var _res$value = _slicedToArray(res.value, 2),
180
+ k = _res$value[0],
181
+ v = _res$value[1];
182
+ var key = this.nativeToScVal(k, scMap.keyType());
183
+ var _val = this.nativeToScVal(v, scMap.valueType());
184
+ entries.push(new _stellarBase.xdr.ScMapEntry({
185
+ key: key,
186
+ val: _val
187
+ }));
188
+ res = values.next();
189
+ }
190
+ return _stellarBase.xdr.ScVal.scvMap(entries);
191
+ }
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') {
193
+ var _val$constructor2;
194
+ 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
+ }
196
+ throw new TypeError("Received object ".concat(val, " did not match the provided type ").concat(ty));
197
+ }
198
+ case 'number':
199
+ case 'bigint':
200
+ {
201
+ 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:
212
+ {
213
+ var intType = t.name.substring(10).toLowerCase();
214
+ return new _stellarBase.XdrLargeInt(intType, val).toScVal();
215
+ }
216
+ default:
217
+ throw new TypeError("invalid type (".concat(ty, ") specified for integer"));
218
+ }
219
+ }
220
+ case 'string':
221
+ return stringToScVal(val, t);
222
+ case 'boolean':
223
+ {
224
+ if (value !== _stellarBase.xdr.ScSpecType.scSpecTypeBool().value) {
225
+ throw TypeError("Type ".concat(ty, " was not bool, but value was bool"));
226
+ }
227
+ return _stellarBase.xdr.ScVal.scvBool(val);
228
+ }
229
+ case 'undefined':
230
+ {
231
+ if (!ty) {
232
+ return _stellarBase.xdr.ScVal.scvVoid();
233
+ }
234
+ switch (value) {
235
+ case _stellarBase.xdr.ScSpecType.scSpecTypeVoid().value:
236
+ case _stellarBase.xdr.ScSpecType.scSpecTypeOption().value:
237
+ return _stellarBase.xdr.ScVal.scvVoid();
238
+ default:
239
+ throw new TypeError("Type ".concat(ty, " was not void, but value was undefined"));
240
+ }
241
+ }
242
+ case 'function':
243
+ return this.nativeToScVal(val(), ty);
244
+ default:
245
+ throw new TypeError("failed to convert typeof ".concat(_typeof(val), " (").concat(val, ")"));
246
+ }
247
+ }
248
+ }, {
249
+ key: "nativeToUdt",
250
+ value: function nativeToUdt(val, name) {
251
+ var entry = this.findEntry(name);
252
+ switch (entry.switch()) {
253
+ case _stellarBase.xdr.ScSpecEntryKind.scSpecEntryUdtEnumV0():
254
+ if (typeof val !== 'number') {
255
+ throw new TypeError("expected number for enum ".concat(name, ", but got ").concat(_typeof(val)));
256
+ }
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());
262
+ default:
263
+ throw new Error("failed to parse udt ".concat(name));
264
+ }
265
+ }
266
+ }, {
267
+ key: "nativeToUnion",
268
+ value: function nativeToUnion(val, union_) {
269
+ var _this3 = this;
270
+ var entry_name = val.tag;
271
+ var case_ = union_.cases().find(function (entry) {
272
+ var case_ = entry.value().name().toString();
273
+ return case_ === entry_name;
274
+ });
275
+ if (!case_) {
276
+ throw new TypeError("no such enum entry: ".concat(entry_name, " in ").concat(union_));
277
+ }
278
+ var key = _stellarBase.xdr.ScVal.scvSymbol(entry_name);
279
+ switch (case_.switch()) {
280
+ case _stellarBase.xdr.ScSpecUdtUnionCaseV0Kind.scSpecUdtUnionCaseVoidV0():
281
+ {
282
+ return _stellarBase.xdr.ScVal.scvVec([key]);
283
+ }
284
+ case _stellarBase.xdr.ScSpecUdtUnionCaseV0Kind.scSpecUdtUnionCaseTupleV0():
285
+ {
286
+ var types = case_.value().type();
287
+ if (Array.isArray(val.values)) {
288
+ if (val.values.length != types.length) {
289
+ throw new TypeError("union ".concat(union_, " expects ").concat(types.length, " values, but got ").concat(val.values.length));
290
+ }
291
+ var scvals = val.values.map(function (v, i) {
292
+ return _this3.nativeToScVal(v, types[i]);
293
+ });
294
+ scvals.unshift(key);
295
+ return _stellarBase.xdr.ScVal.scvVec(scvals);
296
+ }
297
+ throw new Error("failed to parse union case ".concat(case_, " with ").concat(val));
298
+ }
299
+ default:
300
+ throw new Error("failed to parse union ".concat(union_, " with ").concat(val));
301
+ }
302
+ }
303
+ }, {
304
+ key: "nativeToStruct",
305
+ value: function nativeToStruct(val, struct) {
306
+ var _this4 = this;
307
+ var fields = struct.fields();
308
+ if (fields.some(isNumeric)) {
309
+ if (!fields.every(isNumeric)) {
310
+ throw new Error('mixed numeric and non-numeric field names are not allowed');
311
+ }
312
+ return _stellarBase.xdr.ScVal.scvVec(fields.map(function (_, i) {
313
+ return _this4.nativeToScVal(val[i], fields[i].type());
314
+ }));
315
+ }
316
+ return _stellarBase.xdr.ScVal.scvMap(fields.map(function (field) {
317
+ var name = field.name().toString();
318
+ return new _stellarBase.xdr.ScMapEntry({
319
+ key: _this4.nativeToScVal(name, _stellarBase.xdr.ScSpecTypeDef.scSpecTypeSymbol()),
320
+ val: _this4.nativeToScVal(val[name], field.type())
321
+ });
322
+ }));
323
+ }
324
+ }, {
325
+ key: "nativeToEnum",
326
+ value: function nativeToEnum(val, enum_) {
327
+ if (enum_.cases().some(function (entry) {
328
+ return entry.value() === val;
329
+ })) {
330
+ return _stellarBase.xdr.ScVal.scvU32(val);
331
+ }
332
+ throw new TypeError("no such enum entry: ".concat(val, " in ").concat(enum_));
333
+ }
334
+ }, {
335
+ key: "scValStrToNative",
336
+ value: function scValStrToNative(scv, typeDef) {
337
+ return this.scValToNative(_stellarBase.xdr.ScVal.fromXDR(scv, 'base64'), typeDef);
338
+ }
339
+ }, {
340
+ key: "scValToNative",
341
+ value: function scValToNative(scv, typeDef) {
342
+ var _this5 = this;
343
+ var t = typeDef.switch();
344
+ var value = t.value;
345
+ if (value === _stellarBase.xdr.ScSpecType.scSpecTypeUdt().value) {
346
+ return this.scValUdtToNative(scv, typeDef.value());
347
+ }
348
+ switch (scv.switch().value) {
349
+ case _stellarBase.xdr.ScValType.scvVoid().value:
350
+ 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:
359
+ {
360
+ if (value == _stellarBase.xdr.ScSpecType.scSpecTypeVec().value) {
361
+ var _scv$vec;
362
+ var vec = typeDef.value();
363
+ return ((_scv$vec = scv.vec()) !== null && _scv$vec !== void 0 ? _scv$vec : []).map(function (elm) {
364
+ return _this5.scValToNative(elm, vec.elementType());
365
+ });
366
+ } else if (value == _stellarBase.xdr.ScSpecType.scSpecTypeTuple().value) {
367
+ var _scv$vec2;
368
+ var tuple = typeDef.value();
369
+ var valTypes = tuple.valueTypes();
370
+ return ((_scv$vec2 = scv.vec()) !== null && _scv$vec2 !== void 0 ? _scv$vec2 : []).map(function (elm, i) {
371
+ return _this5.scValToNative(elm, valTypes[i]);
372
+ });
373
+ }
374
+ throw new TypeError("Type ".concat(typeDef, " was not vec, but ").concat(scv, " is"));
375
+ }
376
+ case _stellarBase.xdr.ScValType.scvAddress().value:
377
+ return _stellarBase.Address.fromScVal(scv);
378
+ case _stellarBase.xdr.ScValType.scvMap().value:
379
+ {
380
+ var _scv$map;
381
+ 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();
384
+ var keyType = type_.keyType();
385
+ var valueType = type_.valueType();
386
+ return new Map(map.map(function (entry) {
387
+ return [_this5.scValToNative(entry.key(), keyType), _this5.scValToNative(entry.val(), valueType)];
388
+ }));
389
+ }
390
+ throw new TypeError("ScSpecType ".concat(t.name, " was not map, but ").concat(JSON.stringify(scv, null, 2), " is"));
391
+ }
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:
396
+ return scv.value();
397
+ case _stellarBase.xdr.ScValType.scvString().value:
398
+ case _stellarBase.xdr.ScValType.scvSymbol().value:
399
+ {
400
+ var _scv$value;
401
+ if (value !== _stellarBase.xdr.ScSpecType.scSpecTypeString().value && value !== _stellarBase.xdr.ScSpecType.scSpecTypeSymbol().value) {
402
+ throw new Error("ScSpecType ".concat(t.name, " was not string or symbol, but ").concat(JSON.stringify(scv, null, 2), " is"));
403
+ }
404
+ return (_scv$value = scv.value()) === null || _scv$value === void 0 ? void 0 : _scv$value.toString();
405
+ }
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()));
409
+ default:
410
+ throw new TypeError("failed to convert ".concat(JSON.stringify(scv, null, 2), " to native type from type ").concat(t.name));
411
+ }
412
+ }
413
+ }, {
414
+ key: "scValUdtToNative",
415
+ value: function scValUdtToNative(scv, udt) {
416
+ var entry = this.findEntry(udt.name().toString());
417
+ 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());
424
+ default:
425
+ throw new Error("failed to parse udt ".concat(udt.name().toString(), ": ").concat(entry));
426
+ }
427
+ }
428
+ }, {
429
+ key: "unionToNative",
430
+ value: function unionToNative(val, udt) {
431
+ var vec = val.vec();
432
+ if (!vec) {
433
+ throw new Error("".concat(JSON.stringify(val, null, 2), " is not a vec"));
434
+ }
435
+ if (vec.length === 0 && udt.cases.length !== 0) {
436
+ throw new Error("".concat(val, " has length 0, but the there are at least one case in the union"));
437
+ }
438
+ var name = vec[0].sym().toString();
439
+ if (vec[0].switch().value != _stellarBase.xdr.ScValType.scvSymbol().value) {
440
+ throw new Error("{vec[0]} is not a symbol");
441
+ }
442
+ var entry = udt.cases().find(findCase(name));
443
+ if (!entry) {
444
+ throw new Error("failed to find entry ".concat(name, " in union {udt.name().toString()}"));
445
+ }
446
+ var res = {
447
+ tag: name,
448
+ values: undefined
449
+ };
450
+ if (entry.switch().value === _stellarBase.xdr.ScSpecUdtUnionCaseV0Kind.scSpecUdtUnionCaseTupleV0().value) {
451
+ var tuple = entry.value();
452
+ 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;
463
+ }
464
+ return res;
465
+ }
466
+ }, {
467
+ key: "structToNative",
468
+ value: function structToNative(val, udt) {
469
+ var _this6 = this,
470
+ _val$map;
471
+ var res = {};
472
+ var fields = udt.fields();
473
+ if (fields.some(isNumeric)) {
474
+ var _val$vec;
475
+ 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());
477
+ });
478
+ return r;
479
+ }
480
+ (_val$map = val.map()) === null || _val$map === void 0 || _val$map.forEach(function (entry, i) {
481
+ var field = fields[i];
482
+ res[field.name().toString()] = _this6.scValToNative(entry.val(), field.type());
483
+ });
484
+ return res;
485
+ }
486
+ }, {
487
+ key: "enumToNative",
488
+ value: function enumToNative(scv, udt) {
489
+ if (scv.switch().value !== _stellarBase.xdr.ScValType.scvU32().value) {
490
+ throw new Error("Enum must have a u32 value");
491
+ }
492
+ var num = scv.value();
493
+ if (udt.cases().some(function (entry) {
494
+ return entry.value() === num;
495
+ })) {}
496
+ return num;
497
+ }
498
+ }]);
499
+ return ContractSpec;
500
+ }();
501
+ function stringToScVal(str, ty) {
502
+ 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:
508
+ {
509
+ var addr = _stellarBase.Address.fromString(str);
510
+ return _stellarBase.xdr.ScVal.scvAddress(addr.toScAddress());
511
+ }
512
+ default:
513
+ throw new TypeError("invalid type ".concat(ty.name, " specified for string value"));
514
+ }
515
+ }
516
+ function isNumeric(field) {
517
+ return /^\d+$/.test(field.name().toString());
518
+ }
519
+ function findCase(name) {
520
+ return function matches(entry) {
521
+ switch (entry.switch().value) {
522
+ case _stellarBase.xdr.ScSpecUdtUnionCaseV0Kind.scSpecUdtUnionCaseTupleV0().value:
523
+ {
524
+ var tuple = entry.value();
525
+ return tuple.name().toString() === name;
526
+ }
527
+ case _stellarBase.xdr.ScSpecUdtUnionCaseV0Kind.scSpecUdtUnionCaseVoidV0().value:
528
+ {
529
+ var void_case = entry.value();
530
+ return void_case.name().toString() === name;
531
+ }
532
+ default:
533
+ return false;
534
+ }
535
+ };
536
+ }
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));
547
+ }
548
+ return entry[1];
549
+ }
@@ -0,0 +1,59 @@
1
+ import { HorizonApi } from "./horizon/horizon_api";
2
+ export declare class NetworkError extends Error {
3
+ response: {
4
+ data?: HorizonApi.ErrorResponseData;
5
+ status?: number;
6
+ statusText?: string;
7
+ url?: string;
8
+ };
9
+ __proto__: NetworkError;
10
+ constructor(message: string, response: any);
11
+ getResponse(): {
12
+ data?: HorizonApi.ErrorResponseData | undefined;
13
+ status?: number | undefined;
14
+ statusText?: string | undefined;
15
+ url?: string | undefined;
16
+ };
17
+ }
18
+ export declare class NotFoundError extends NetworkError {
19
+ constructor(message: string, response: any);
20
+ }
21
+ export declare class BadRequestError extends NetworkError {
22
+ constructor(message: string, response: any);
23
+ }
24
+ export declare class BadResponseError extends NetworkError {
25
+ constructor(message: string, response: any);
26
+ }
27
+ /**
28
+ * AccountRequiresMemoError is raised when a transaction is trying to submit an
29
+ * operation to an account which requires a memo. See
30
+ * [SEP0029](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0029.md)
31
+ * for more information.
32
+ *
33
+ * This error contains two attributes to help you identify the account requiring
34
+ * the memo and the operation where the account is the destination
35
+ *
36
+ * ```
37
+ * console.log('The following account requires a memo ', err.accountId)
38
+ * console.log('The account is used in operation: ', err.operationIndex)
39
+ * ```
40
+ *
41
+ */
42
+ export declare class AccountRequiresMemoError extends Error {
43
+ __proto__: AccountRequiresMemoError;
44
+ /**
45
+ * accountId account which requires a memo.
46
+ */
47
+ accountId: string;
48
+ /**
49
+ * operationIndex operation where accountId is the destination.
50
+ */
51
+ operationIndex: number;
52
+ /**
53
+ * Create an AccountRequiresMemoError
54
+ * @param {message} message - error message
55
+ * @param {string} accountId - The account which requires a memo.
56
+ * @param {number} operationIndex - The index of the operation where `accountId` is the destination.
57
+ */
58
+ constructor(message: string, accountId: string, operationIndex: number);
59
+ }