1055 lines
39 KiB
JavaScript
Raw Permalink Normal View History

2025-06-21 23:30:33 +03:00
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ContractSpec = void 0;
var _2 = require(".");
var _rust_types = require("./rust_types");
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; }
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; }
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; } } }; }
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); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
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; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
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); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
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."); }
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); }
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; }
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; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function readObj(args, input) {
var inputName = input.name().toString();
var entry = Object.entries(args).find(function (_ref) {
var _ref2 = _slicedToArray(_ref, 2),
name = _ref2[0],
_ = _ref2[1];
return name === inputName;
});
if (!entry) {
throw new Error("Missing field ".concat(inputName));
}
return entry[1];
}
var ContractSpec = exports.ContractSpec = function () {
function ContractSpec(entries) {
_classCallCheck(this, ContractSpec);
_defineProperty(this, "entries", []);
if (entries.length == 0) {
throw new Error("Contract spec must have at least one entry");
}
var entry = entries[0];
if (typeof entry === "string") {
this.entries = entries.map(function (s) {
return _2.xdr.ScSpecEntry.fromXDR(s, "base64");
});
} else {
this.entries = entries;
}
}
return _createClass(ContractSpec, [{
key: "funcs",
value: function funcs() {
return this.entries.filter(function (entry) {
return entry.switch().value === _2.xdr.ScSpecEntryKind.scSpecEntryFunctionV0().value;
}).map(function (entry) {
return entry.functionV0();
});
}
}, {
key: "getFunc",
value: function getFunc(name) {
var entry = this.findEntry(name);
if (entry.switch().value !== _2.xdr.ScSpecEntryKind.scSpecEntryFunctionV0().value) {
throw new Error("".concat(name, " is not a function"));
}
return entry.functionV0();
}
}, {
key: "funcArgsToScVals",
value: function funcArgsToScVals(name, args) {
var _this = this;
var fn = this.getFunc(name);
return fn.inputs().map(function (input) {
return _this.nativeToScVal(readObj(args, input), input.type());
});
}
}, {
key: "funcResToNative",
value: function funcResToNative(name, val_or_base64) {
var val = typeof val_or_base64 === "string" ? _2.xdr.ScVal.fromXDR(val_or_base64, "base64") : val_or_base64;
var func = this.getFunc(name);
var outputs = func.outputs();
if (outputs.length === 0) {
var type = val.switch();
if (type.value !== _2.xdr.ScValType.scvVoid().value) {
throw new Error("Expected void, got ".concat(type.name));
}
return null;
}
if (outputs.length > 1) {
throw new Error("Multiple outputs not supported");
}
var output = outputs[0];
if (output.switch().value === _2.xdr.ScSpecType.scSpecTypeResult().value) {
return new _rust_types.Ok(this.scValToNative(val, output.result().okType()));
}
return this.scValToNative(val, output);
}
}, {
key: "findEntry",
value: function findEntry(name) {
var entry = this.entries.find(function (entry) {
return entry.value().name().toString() === name;
});
if (!entry) {
throw new Error("no such entry: ".concat(name));
}
return entry;
}
}, {
key: "nativeToScVal",
value: function nativeToScVal(val, ty) {
var _this2 = this;
var t = ty.switch();
var value = t.value;
if (t.value === _2.xdr.ScSpecType.scSpecTypeUdt().value) {
var udt = ty.udt();
return this.nativeToUdt(val, udt.name().toString());
}
if (value === _2.xdr.ScSpecType.scSpecTypeOption().value) {
var opt = ty.option();
if (val === undefined) {
return _2.xdr.ScVal.scvVoid();
}
return this.nativeToScVal(val, opt.valueType());
}
switch (_typeof(val)) {
case "object":
{
var _val$constructor$name, _val$constructor;
if (val === null) {
switch (value) {
case _2.xdr.ScSpecType.scSpecTypeVoid().value:
return _2.xdr.ScVal.scvVoid();
default:
throw new TypeError("Type ".concat(ty, " was not void, but value was null"));
}
}
if (val instanceof _2.xdr.ScVal) {
return val;
}
if (val instanceof _2.Address) {
if (ty.switch().value !== _2.xdr.ScSpecType.scSpecTypeAddress().value) {
throw new TypeError("Type ".concat(ty, " was not address, but value was Address"));
}
return val.toScVal();
}
if (val instanceof _2.Contract) {
if (ty.switch().value !== _2.xdr.ScSpecType.scSpecTypeAddress().value) {
throw new TypeError("Type ".concat(ty, " was not address, but value was Address"));
}
return val.address().toScVal();
}
if (val instanceof Uint8Array || Buffer.isBuffer(val)) {
var copy = Uint8Array.from(val);
switch (value) {
case _2.xdr.ScSpecType.scSpecTypeBytesN().value:
{
var bytes_n = ty.bytesN();
if (copy.length !== bytes_n.n()) {
throw new TypeError("expected ".concat(bytes_n.n(), " bytes, but got ").concat(copy.length));
}
return _2.xdr.ScVal.scvBytes(copy);
}
case _2.xdr.ScSpecType.scSpecTypeBytes().value:
return _2.xdr.ScVal.scvBytes(copy);
default:
throw new TypeError("invalid type (".concat(ty, ") specified for Bytes and BytesN"));
}
}
if (Array.isArray(val)) {
switch (value) {
case _2.xdr.ScSpecType.scSpecTypeVec().value:
{
var vec = ty.vec();
var elementType = vec.elementType();
return _2.xdr.ScVal.scvVec(val.map(function (v) {
return _this2.nativeToScVal(v, elementType);
}));
}
case _2.xdr.ScSpecType.scSpecTypeTuple().value:
{
var tup = ty.tuple();
var valTypes = tup.valueTypes();
if (val.length !== valTypes.length) {
throw new TypeError("Tuple expects ".concat(valTypes.length, " values, but ").concat(val.length, " were provided"));
}
return _2.xdr.ScVal.scvVec(val.map(function (v, i) {
return _this2.nativeToScVal(v, valTypes[i]);
}));
}
case _2.xdr.ScSpecType.scSpecTypeMap().value:
{
var map = ty.map();
var keyType = map.keyType();
var valueType = map.valueType();
return _2.xdr.ScVal.scvMap(val.map(function (entry) {
var key = _this2.nativeToScVal(entry[0], keyType);
var val = _this2.nativeToScVal(entry[1], valueType);
return new _2.xdr.ScMapEntry({
key: key,
val: val
});
}));
}
default:
throw new TypeError("Type ".concat(ty, " was not vec, but value was Array"));
}
}
if (val.constructor === Map) {
if (value !== _2.xdr.ScSpecType.scSpecTypeMap().value) {
throw new TypeError("Type ".concat(ty, " was not map, but value was Map"));
}
var scMap = ty.map();
var _map = val;
var entries = [];
var values = _map.entries();
var res = values.next();
while (!res.done) {
var _res$value = _slicedToArray(res.value, 2),
k = _res$value[0],
v = _res$value[1];
var _key = this.nativeToScVal(k, scMap.keyType());
var _val = this.nativeToScVal(v, scMap.valueType());
entries.push(new _2.xdr.ScMapEntry({
key: _key,
val: _val
}));
res = values.next();
}
return _2.xdr.ScVal.scvMap(entries);
}
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") {
var _val$constructor2;
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), ")"));
}
throw new TypeError("Received object ".concat(val, " did not match the provided type ").concat(ty));
}
case "number":
case "bigint":
{
switch (value) {
case _2.xdr.ScSpecType.scSpecTypeU32().value:
return _2.xdr.ScVal.scvU32(val);
case _2.xdr.ScSpecType.scSpecTypeI32().value:
return _2.xdr.ScVal.scvI32(val);
case _2.xdr.ScSpecType.scSpecTypeU64().value:
case _2.xdr.ScSpecType.scSpecTypeI64().value:
case _2.xdr.ScSpecType.scSpecTypeU128().value:
case _2.xdr.ScSpecType.scSpecTypeI128().value:
case _2.xdr.ScSpecType.scSpecTypeU256().value:
case _2.xdr.ScSpecType.scSpecTypeI256().value:
{
var intType = t.name.substring(10).toLowerCase();
return new _2.XdrLargeInt(intType, val).toScVal();
}
default:
throw new TypeError("invalid type (".concat(ty, ") specified for integer"));
}
}
case "string":
return stringToScVal(val, t);
case "boolean":
{
if (value !== _2.xdr.ScSpecType.scSpecTypeBool().value) {
throw TypeError("Type ".concat(ty, " was not bool, but value was bool"));
}
return _2.xdr.ScVal.scvBool(val);
}
case "undefined":
{
if (!ty) {
return _2.xdr.ScVal.scvVoid();
}
switch (value) {
case _2.xdr.ScSpecType.scSpecTypeVoid().value:
case _2.xdr.ScSpecType.scSpecTypeOption().value:
return _2.xdr.ScVal.scvVoid();
default:
throw new TypeError("Type ".concat(ty, " was not void, but value was undefined"));
}
}
case "function":
return this.nativeToScVal(val(), ty);
default:
throw new TypeError("failed to convert typeof ".concat(_typeof(val), " (").concat(val, ")"));
}
}
}, {
key: "nativeToUdt",
value: function nativeToUdt(val, name) {
var entry = this.findEntry(name);
switch (entry.switch()) {
case _2.xdr.ScSpecEntryKind.scSpecEntryUdtEnumV0():
if (typeof val !== "number") {
throw new TypeError("expected number for enum ".concat(name, ", but got ").concat(_typeof(val)));
}
return this.nativeToEnum(val, entry.udtEnumV0());
case _2.xdr.ScSpecEntryKind.scSpecEntryUdtStructV0():
return this.nativeToStruct(val, entry.udtStructV0());
case _2.xdr.ScSpecEntryKind.scSpecEntryUdtUnionV0():
return this.nativeToUnion(val, entry.udtUnionV0());
default:
throw new Error("failed to parse udt ".concat(name));
}
}
}, {
key: "nativeToUnion",
value: function nativeToUnion(val, union_) {
var _this3 = this;
var entry_name = val.tag;
var case_ = union_.cases().find(function (entry) {
var case_ = entry.value().name().toString();
return case_ === entry_name;
});
if (!case_) {
throw new TypeError("no such enum entry: ".concat(entry_name, " in ").concat(union_));
}
var key = _2.xdr.ScVal.scvSymbol(entry_name);
switch (case_.switch()) {
case _2.xdr.ScSpecUdtUnionCaseV0Kind.scSpecUdtUnionCaseVoidV0():
{
return _2.xdr.ScVal.scvVec([key]);
}
case _2.xdr.ScSpecUdtUnionCaseV0Kind.scSpecUdtUnionCaseTupleV0():
{
var types = case_.tupleCase().type();
if (Array.isArray(val.values)) {
if (val.values.length != types.length) {
throw new TypeError("union ".concat(union_, " expects ").concat(types.length, " values, but got ").concat(val.values.length));
}
var scvals = val.values.map(function (v, i) {
return _this3.nativeToScVal(v, types[i]);
});
scvals.unshift(key);
return _2.xdr.ScVal.scvVec(scvals);
}
throw new Error("failed to parse union case ".concat(case_, " with ").concat(val));
}
default:
throw new Error("failed to parse union ".concat(union_, " with ").concat(val));
}
}
}, {
key: "nativeToStruct",
value: function nativeToStruct(val, struct) {
var _this4 = this;
var fields = struct.fields();
if (fields.some(isNumeric)) {
if (!fields.every(isNumeric)) {
throw new Error("mixed numeric and non-numeric field names are not allowed");
}
return _2.xdr.ScVal.scvVec(fields.map(function (_, i) {
return _this4.nativeToScVal(val[i], fields[i].type());
}));
}
return _2.xdr.ScVal.scvMap(fields.map(function (field) {
var name = field.name().toString();
return new _2.xdr.ScMapEntry({
key: _this4.nativeToScVal(name, _2.xdr.ScSpecTypeDef.scSpecTypeSymbol()),
val: _this4.nativeToScVal(val[name], field.type())
});
}));
}
}, {
key: "nativeToEnum",
value: function nativeToEnum(val, enum_) {
if (enum_.cases().some(function (entry) {
return entry.value() === val;
})) {
return _2.xdr.ScVal.scvU32(val);
}
throw new TypeError("no such enum entry: ".concat(val, " in ").concat(enum_));
}
}, {
key: "scValStrToNative",
value: function scValStrToNative(scv, typeDef) {
return this.scValToNative(_2.xdr.ScVal.fromXDR(scv, "base64"), typeDef);
}
}, {
key: "scValToNative",
value: function scValToNative(scv, typeDef) {
var _this5 = this;
var t = typeDef.switch();
var value = t.value;
if (value === _2.xdr.ScSpecType.scSpecTypeUdt().value) {
return this.scValUdtToNative(scv, typeDef.udt());
}
switch (scv.switch().value) {
case _2.xdr.ScValType.scvVoid().value:
return void 0;
case _2.xdr.ScValType.scvU64().value:
case _2.xdr.ScValType.scvI64().value:
case _2.xdr.ScValType.scvU128().value:
case _2.xdr.ScValType.scvI128().value:
case _2.xdr.ScValType.scvU256().value:
case _2.xdr.ScValType.scvI256().value:
return (0, _2.scValToBigInt)(scv);
case _2.xdr.ScValType.scvVec().value:
{
if (value == _2.xdr.ScSpecType.scSpecTypeVec().value) {
var _scv$vec;
var vec = typeDef.vec();
return ((_scv$vec = scv.vec()) !== null && _scv$vec !== void 0 ? _scv$vec : []).map(function (elm) {
return _this5.scValToNative(elm, vec.elementType());
});
} else if (value == _2.xdr.ScSpecType.scSpecTypeTuple().value) {
var _scv$vec2;
var tuple = typeDef.tuple();
var valTypes = tuple.valueTypes();
return ((_scv$vec2 = scv.vec()) !== null && _scv$vec2 !== void 0 ? _scv$vec2 : []).map(function (elm, i) {
return _this5.scValToNative(elm, valTypes[i]);
});
}
throw new TypeError("Type ".concat(typeDef, " was not vec, but ").concat(scv, " is"));
}
case _2.xdr.ScValType.scvAddress().value:
return _2.Address.fromScVal(scv).toString();
case _2.xdr.ScValType.scvMap().value:
{
var _scv$map;
var map = (_scv$map = scv.map()) !== null && _scv$map !== void 0 ? _scv$map : [];
if (value == _2.xdr.ScSpecType.scSpecTypeMap().value) {
var type_ = typeDef.map();
var keyType = type_.keyType();
var valueType = type_.valueType();
var res = map.map(function (entry) {
return [_this5.scValToNative(entry.key(), keyType), _this5.scValToNative(entry.val(), valueType)];
});
return res;
}
throw new TypeError("ScSpecType ".concat(t.name, " was not map, but ").concat(JSON.stringify(scv, null, 2), " is"));
}
case _2.xdr.ScValType.scvBool().value:
case _2.xdr.ScValType.scvU32().value:
case _2.xdr.ScValType.scvI32().value:
case _2.xdr.ScValType.scvBytes().value:
return scv.value();
case _2.xdr.ScValType.scvString().value:
case _2.xdr.ScValType.scvSymbol().value:
{
var _scv$value;
if (value !== _2.xdr.ScSpecType.scSpecTypeString().value && value !== _2.xdr.ScSpecType.scSpecTypeSymbol().value) {
throw new Error("ScSpecType ".concat(t.name, " was not string or symbol, but ").concat(JSON.stringify(scv, null, 2), " is"));
}
return (_scv$value = scv.value()) === null || _scv$value === void 0 ? void 0 : _scv$value.toString();
}
case _2.xdr.ScValType.scvTimepoint().value:
case _2.xdr.ScValType.scvDuration().value:
return (0, _2.scValToBigInt)(_2.xdr.ScVal.scvU64(scv.u64()));
default:
throw new TypeError("failed to convert ".concat(JSON.stringify(scv, null, 2), " to native type from type ").concat(t.name));
}
}
}, {
key: "scValUdtToNative",
value: function scValUdtToNative(scv, udt) {
var entry = this.findEntry(udt.name().toString());
switch (entry.switch()) {
case _2.xdr.ScSpecEntryKind.scSpecEntryUdtEnumV0():
return this.enumToNative(scv);
case _2.xdr.ScSpecEntryKind.scSpecEntryUdtStructV0():
return this.structToNative(scv, entry.udtStructV0());
case _2.xdr.ScSpecEntryKind.scSpecEntryUdtUnionV0():
return this.unionToNative(scv, entry.udtUnionV0());
default:
throw new Error("failed to parse udt ".concat(udt.name().toString(), ": ").concat(entry));
}
}
}, {
key: "unionToNative",
value: function unionToNative(val, udt) {
var _this6 = this;
var vec = val.vec();
if (!vec) {
throw new Error("".concat(JSON.stringify(val, null, 2), " is not a vec"));
}
if (vec.length === 0 && udt.cases.length !== 0) {
throw new Error("".concat(val, " has length 0, but the there are at least one case in the union"));
}
var name = vec[0].sym().toString();
if (vec[0].switch().value != _2.xdr.ScValType.scvSymbol().value) {
throw new Error("{vec[0]} is not a symbol");
}
var entry = udt.cases().find(findCase(name));
if (!entry) {
throw new Error("failed to find entry ".concat(name, " in union {udt.name().toString()}"));
}
var res = {
tag: name
};
if (entry.switch().value === _2.xdr.ScSpecUdtUnionCaseV0Kind.scSpecUdtUnionCaseTupleV0().value) {
var tuple = entry.tupleCase();
var ty = tuple.type();
var values = ty.map(function (entry, i) {
return _this6.scValToNative(vec[i + 1], entry);
});
res.values = values;
}
return res;
}
}, {
key: "structToNative",
value: function structToNative(val, udt) {
var _this7 = this,
_val$map;
var res = {};
var fields = udt.fields();
if (fields.some(isNumeric)) {
var _val$vec;
var r = (_val$vec = val.vec()) === null || _val$vec === void 0 ? void 0 : _val$vec.map(function (entry, i) {
return _this7.scValToNative(entry, fields[i].type());
});
return r;
}
(_val$map = val.map()) === null || _val$map === void 0 || _val$map.forEach(function (entry, i) {
var field = fields[i];
res[field.name().toString()] = _this7.scValToNative(entry.val(), field.type());
});
return res;
}
}, {
key: "enumToNative",
value: function enumToNative(scv) {
if (scv.switch().value !== _2.xdr.ScValType.scvU32().value) {
throw new Error("Enum must have a u32 value");
}
var num = scv.u32();
return num;
}
}, {
key: "errorCases",
value: function errorCases() {
return this.entries.filter(function (entry) {
return entry.switch().value === _2.xdr.ScSpecEntryKind.scSpecEntryUdtErrorEnumV0().value;
}).flatMap(function (entry) {
return entry.value().cases();
});
}
}, {
key: "jsonSchema",
value: function jsonSchema(funcName) {
var definitions = {};
var _iterator = _createForOfIteratorHelper(this.entries),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var entry = _step.value;
switch (entry.switch().value) {
case _2.xdr.ScSpecEntryKind.scSpecEntryUdtEnumV0().value:
{
var _udt = entry.udtEnumV0();
definitions[_udt.name().toString()] = enumToJsonSchema(_udt);
break;
}
case _2.xdr.ScSpecEntryKind.scSpecEntryUdtStructV0().value:
{
var _udt2 = entry.udtStructV0();
definitions[_udt2.name().toString()] = structToJsonSchema(_udt2);
break;
}
case _2.xdr.ScSpecEntryKind.scSpecEntryUdtUnionV0().value:
var udt = entry.udtUnionV0();
definitions[udt.name().toString()] = unionToJsonSchema(udt);
break;
case _2.xdr.ScSpecEntryKind.scSpecEntryFunctionV0().value:
{
var fn = entry.functionV0();
var fnName = fn.name().toString();
var _functionToJsonSchema = functionToJsonSchema(fn),
input = _functionToJsonSchema.input;
definitions[fnName] = input;
break;
}
case _2.xdr.ScSpecEntryKind.scSpecEntryUdtErrorEnumV0().value:
{}
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
var res = {
$schema: "http://json-schema.org/draft-07/schema#",
definitions: _objectSpread(_objectSpread({}, PRIMITIVE_DEFINITONS), definitions)
};
if (funcName) {
res["$ref"] = "#/definitions/".concat(funcName);
}
return res;
}
}]);
}();
function stringToScVal(str, ty) {
switch (ty.value) {
case _2.xdr.ScSpecType.scSpecTypeString().value:
return _2.xdr.ScVal.scvString(str);
case _2.xdr.ScSpecType.scSpecTypeSymbol().value:
return _2.xdr.ScVal.scvSymbol(str);
case _2.xdr.ScSpecType.scSpecTypeAddress().value:
{
var addr = _2.Address.fromString(str);
return _2.xdr.ScVal.scvAddress(addr.toScAddress());
}
case _2.xdr.ScSpecType.scSpecTypeU64().value:
return new _2.XdrLargeInt("u64", str).toScVal();
case _2.xdr.ScSpecType.scSpecTypeI64().value:
return new _2.XdrLargeInt("i64", str).toScVal();
case _2.xdr.ScSpecType.scSpecTypeU128().value:
return new _2.XdrLargeInt("u128", str).toScVal();
case _2.xdr.ScSpecType.scSpecTypeI128().value:
return new _2.XdrLargeInt("i128", str).toScVal();
case _2.xdr.ScSpecType.scSpecTypeU256().value:
return new _2.XdrLargeInt("u256", str).toScVal();
case _2.xdr.ScSpecType.scSpecTypeI256().value:
return new _2.XdrLargeInt("i256", str).toScVal();
case _2.xdr.ScSpecType.scSpecTypeBytes().value:
case _2.xdr.ScSpecType.scSpecTypeBytesN().value:
return _2.xdr.ScVal.scvBytes(Buffer.from(str, "base64"));
default:
throw new TypeError("invalid type ".concat(ty.name, " specified for string value"));
}
}
function isNumeric(field) {
return /^\d+$/.test(field.name().toString());
}
function findCase(name) {
return function matches(entry) {
switch (entry.switch().value) {
case _2.xdr.ScSpecUdtUnionCaseV0Kind.scSpecUdtUnionCaseTupleV0().value:
{
var tuple = entry.tupleCase();
return tuple.name().toString() === name;
}
case _2.xdr.ScSpecUdtUnionCaseV0Kind.scSpecUdtUnionCaseVoidV0().value:
{
var void_case = entry.voidCase();
return void_case.name().toString() === name;
}
default:
return false;
}
};
}
var PRIMITIVE_DEFINITONS = {
U32: {
type: "integer",
minimum: 0,
maximum: 4294967295
},
I32: {
type: "integer",
minimum: -2147483648,
maximum: 2147483647
},
U64: {
type: "string",
pattern: "^([1-9][0-9]*|0)$",
minLength: 1,
maxLength: 20
},
I64: {
type: "string",
pattern: "^(-?[1-9][0-9]*|0)$",
minLength: 1,
maxLength: 21
},
U128: {
type: "string",
pattern: "^([1-9][0-9]*|0)$",
minLength: 1,
maxLength: 39
},
I128: {
type: "string",
pattern: "^(-?[1-9][0-9]*|0)$",
minLength: 1,
maxLength: 40
},
U256: {
type: "string",
pattern: "^([1-9][0-9]*|0)$",
minLength: 1,
maxLength: 78
},
I256: {
type: "string",
pattern: "^(-?[1-9][0-9]*|0)$",
minLength: 1,
maxLength: 79
},
Address: {
type: "string",
format: "address",
description: "Address can be a public key or contract id"
},
ScString: {
type: "string",
description: "ScString is a string"
},
ScSymbol: {
type: "string",
description: "ScString is a string"
},
DataUrl: {
type: "string",
pattern: "^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=)?$"
}
};
function typeRef(typeDef) {
var t = typeDef.switch();
var value = t.value;
var ref;
switch (value) {
case _2.xdr.ScSpecType.scSpecTypeVal().value:
{
ref = "Val";
break;
}
case _2.xdr.ScSpecType.scSpecTypeBool().value:
{
return {
type: "boolean"
};
}
case _2.xdr.ScSpecType.scSpecTypeVoid().value:
{
return {
type: "null"
};
}
case _2.xdr.ScSpecType.scSpecTypeError().value:
{
ref = "Error";
break;
}
case _2.xdr.ScSpecType.scSpecTypeU32().value:
{
ref = "U32";
break;
}
case _2.xdr.ScSpecType.scSpecTypeI32().value:
{
ref = "I32";
break;
}
case _2.xdr.ScSpecType.scSpecTypeU64().value:
{
ref = "U64";
break;
}
case _2.xdr.ScSpecType.scSpecTypeI64().value:
{
ref = "I64";
break;
}
case _2.xdr.ScSpecType.scSpecTypeTimepoint().value:
{
throw new Error("Timepoint type not supported");
ref = "Timepoint";
break;
}
case _2.xdr.ScSpecType.scSpecTypeDuration().value:
{
throw new Error("Duration not supported");
ref = "Duration";
break;
}
case _2.xdr.ScSpecType.scSpecTypeU128().value:
{
ref = "U128";
break;
}
case _2.xdr.ScSpecType.scSpecTypeI128().value:
{
ref = "I128";
break;
}
case _2.xdr.ScSpecType.scSpecTypeU256().value:
{
ref = "U256";
break;
}
case _2.xdr.ScSpecType.scSpecTypeI256().value:
{
ref = "I256";
break;
}
case _2.xdr.ScSpecType.scSpecTypeBytes().value:
{
ref = "DataUrl";
break;
}
case _2.xdr.ScSpecType.scSpecTypeString().value:
{
ref = "ScString";
break;
}
case _2.xdr.ScSpecType.scSpecTypeSymbol().value:
{
ref = "ScSymbol";
break;
}
case _2.xdr.ScSpecType.scSpecTypeAddress().value:
{
ref = "Address";
break;
}
case _2.xdr.ScSpecType.scSpecTypeOption().value:
{
var opt = typeDef.option();
return typeRef(opt.valueType());
}
case _2.xdr.ScSpecType.scSpecTypeResult().value:
{
break;
}
case _2.xdr.ScSpecType.scSpecTypeVec().value:
{
var arr = typeDef.vec();
var _ref3 = typeRef(arr.elementType());
return {
type: "array",
items: _ref3
};
}
case _2.xdr.ScSpecType.scSpecTypeMap().value:
{
var map = typeDef.map();
var items = [typeRef(map.keyType()), typeRef(map.valueType())];
return {
type: "array",
items: {
type: "array",
items: items,
minItems: 2,
maxItems: 2
}
};
}
case _2.xdr.ScSpecType.scSpecTypeTuple().value:
{
var tuple = typeDef.tuple();
var minItems = tuple.valueTypes().length;
var maxItems = minItems;
var _items = tuple.valueTypes().map(typeRef);
return {
type: "array",
items: _items,
minItems: minItems,
maxItems: maxItems
};
}
case _2.xdr.ScSpecType.scSpecTypeBytesN().value:
{
var _arr = typeDef.bytesN();
return {
$ref: "#/definitions/DataUrl",
maxLength: _arr.n()
};
}
case _2.xdr.ScSpecType.scSpecTypeUdt().value:
{
var udt = typeDef.udt();
ref = udt.name().toString();
break;
}
}
return {
$ref: "#/definitions/".concat(ref)
};
}
function isRequired(typeDef) {
return typeDef.switch().value != _2.xdr.ScSpecType.scSpecTypeOption().value;
}
function structToJsonSchema(udt) {
var fields = udt.fields();
if (fields.some(isNumeric)) {
if (!fields.every(isNumeric)) {
throw new Error("mixed numeric and non-numeric field names are not allowed");
}
var items = fields.map(function (_, i) {
return typeRef(fields[i].type());
});
return {
type: "array",
items: items,
minItems: fields.length,
maxItems: fields.length
};
}
var description = udt.doc().toString();
var _args_and_required = args_and_required(fields),
properties = _args_and_required.properties,
required = _args_and_required.required;
properties["additionalProperties"] = false;
return {
description: description,
properties: properties,
required: required,
type: "object"
};
}
function args_and_required(input) {
var properties = {};
var required = [];
var _iterator2 = _createForOfIteratorHelper(input),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var arg = _step2.value;
var type_ = arg.type();
var name = arg.name().toString();
properties[name] = typeRef(type_);
if (isRequired(type_)) {
required.push(name);
}
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
var res = {
properties: properties
};
if (required.length > 0) {
res.required = required;
}
return res;
}
function functionToJsonSchema(func) {
var _args_and_required2 = args_and_required(func.inputs()),
properties = _args_and_required2.properties,
required = _args_and_required2.required;
var args = {
additionalProperties: false,
properties: properties,
type: "object"
};
if ((required === null || required === void 0 ? void 0 : required.length) > 0) {
args.required = required;
}
var input = {
properties: {
args: args
}
};
var outputs = func.outputs();
var output = outputs.length > 0 ? typeRef(outputs[0]) : typeRef(_2.xdr.ScSpecTypeDef.scSpecTypeVoid());
var description = func.doc().toString();
if (description.length > 0) {
input.description = description;
}
input.additionalProperties = false;
output.additionalProperties = false;
return {
input: input,
output: output
};
}
function unionToJsonSchema(udt) {
var description = udt.doc().toString();
var cases = udt.cases();
var oneOf = [];
var _iterator3 = _createForOfIteratorHelper(cases),
_step3;
try {
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
var case_ = _step3.value;
switch (case_.switch().value) {
case _2.xdr.ScSpecUdtUnionCaseV0Kind.scSpecUdtUnionCaseVoidV0().value:
{
var c = case_.voidCase();
var title = c.name().toString();
oneOf.push({
type: "object",
title: title,
properties: {
tag: title
},
additionalProperties: false,
required: ["tag"]
});
break;
}
case _2.xdr.ScSpecUdtUnionCaseV0Kind.scSpecUdtUnionCaseTupleV0().value:
{
var _c = case_.tupleCase();
var _title = _c.name().toString();
oneOf.push({
type: "object",
title: _title,
properties: {
tag: _title,
values: {
type: "array",
items: _c.type().map(typeRef)
}
},
required: ["tag", "values"],
additionalProperties: false
});
}
}
}
} catch (err) {
_iterator3.e(err);
} finally {
_iterator3.f();
}
var res = {
oneOf: oneOf
};
if (description.length > 0) {
res.description = description;
}
return res;
}
function enumToJsonSchema(udt) {
var description = udt.doc().toString();
var cases = udt.cases();
var oneOf = [];
var _iterator4 = _createForOfIteratorHelper(cases),
_step4;
try {
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
var case_ = _step4.value;
var title = case_.name().toString();
var _description = case_.doc().toString();
oneOf.push({
description: _description,
title: title,
enum: [case_.value()],
type: "number"
});
}
} catch (err) {
_iterator4.e(err);
} finally {
_iterator4.f();
}
var res = {
oneOf: oneOf
};
if (description.length > 0) {
res.description = description;
}
return res;
}