@stellar/stellar-base 10.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 (80) hide show
  1. package/CHANGELOG.md +1262 -0
  2. package/LICENSE +202 -0
  3. package/README.md +198 -0
  4. package/dist/stellar-base.js +30777 -0
  5. package/dist/stellar-base.min.js +2 -0
  6. package/lib/account.js +80 -0
  7. package/lib/address.js +169 -0
  8. package/lib/asset.js +323 -0
  9. package/lib/auth.js +253 -0
  10. package/lib/claimant.js +193 -0
  11. package/lib/contract.js +113 -0
  12. package/lib/events.js +42 -0
  13. package/lib/fee_bump_transaction.js +134 -0
  14. package/lib/generated/curr_generated.js +8315 -0
  15. package/lib/generated/next_generated.js +8315 -0
  16. package/lib/get_liquidity_pool_id.js +57 -0
  17. package/lib/hashing.js +12 -0
  18. package/lib/index.js +385 -0
  19. package/lib/invocation.js +195 -0
  20. package/lib/keypair.js +308 -0
  21. package/lib/liquidity_pool_asset.js +126 -0
  22. package/lib/liquidity_pool_id.js +101 -0
  23. package/lib/memo.js +270 -0
  24. package/lib/muxed_account.js +159 -0
  25. package/lib/network.js +22 -0
  26. package/lib/numbers/index.js +85 -0
  27. package/lib/numbers/int128.js +50 -0
  28. package/lib/numbers/int256.js +50 -0
  29. package/lib/numbers/sc_int.js +134 -0
  30. package/lib/numbers/uint128.js +50 -0
  31. package/lib/numbers/uint256.js +50 -0
  32. package/lib/numbers/xdr_large_int.js +267 -0
  33. package/lib/operation.js +715 -0
  34. package/lib/operations/account_merge.js +32 -0
  35. package/lib/operations/allow_trust.js +57 -0
  36. package/lib/operations/begin_sponsoring_future_reserves.js +38 -0
  37. package/lib/operations/bump_sequence.js +37 -0
  38. package/lib/operations/change_trust.js +52 -0
  39. package/lib/operations/claim_claimable_balance.js +40 -0
  40. package/lib/operations/clawback.js +46 -0
  41. package/lib/operations/clawback_claimable_balance.js +39 -0
  42. package/lib/operations/create_account.js +37 -0
  43. package/lib/operations/create_claimable_balance.js +65 -0
  44. package/lib/operations/create_passive_sell_offer.js +44 -0
  45. package/lib/operations/end_sponsoring_future_reserves.js +27 -0
  46. package/lib/operations/extend_footprint_ttl.js +45 -0
  47. package/lib/operations/index.js +254 -0
  48. package/lib/operations/inflation.js +23 -0
  49. package/lib/operations/invoke_host_function.js +219 -0
  50. package/lib/operations/liquidity_pool_deposit.js +64 -0
  51. package/lib/operations/liquidity_pool_withdraw.js +50 -0
  52. package/lib/operations/manage_buy_offer.js +50 -0
  53. package/lib/operations/manage_data.js +41 -0
  54. package/lib/operations/manage_sell_offer.js +50 -0
  55. package/lib/operations/path_payment_strict_receive.js +68 -0
  56. package/lib/operations/path_payment_strict_send.js +68 -0
  57. package/lib/operations/payment.js +47 -0
  58. package/lib/operations/restore_footprint.js +38 -0
  59. package/lib/operations/revoke_sponsorship.js +301 -0
  60. package/lib/operations/set_options.js +135 -0
  61. package/lib/operations/set_trustline_flags.js +84 -0
  62. package/lib/scval.js +369 -0
  63. package/lib/signerkey.js +103 -0
  64. package/lib/signing.js +96 -0
  65. package/lib/soroban.js +96 -0
  66. package/lib/sorobandata_builder.js +218 -0
  67. package/lib/strkey.js +400 -0
  68. package/lib/transaction.js +369 -0
  69. package/lib/transaction_base.js +248 -0
  70. package/lib/transaction_builder.js +753 -0
  71. package/lib/util/checksum.js +20 -0
  72. package/lib/util/continued_fraction.js +58 -0
  73. package/lib/util/decode_encode_muxed_account.js +116 -0
  74. package/lib/util/util.js +14 -0
  75. package/lib/xdr.js +9 -0
  76. package/package.json +133 -0
  77. package/types/curr.d.ts +14078 -0
  78. package/types/index.d.ts +1270 -0
  79. package/types/next.d.ts +14078 -0
  80. package/types/xdr.d.ts +1 -0
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+
3
+ 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); }
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.ScInt = void 0;
8
+ var _xdr_large_int = require("./xdr_large_int");
9
+ 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); } }
10
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
11
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
12
+ 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); }
13
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
14
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
15
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
16
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
17
+ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
18
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
19
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
20
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
21
+ /**
22
+ * Provides an easier way to manipulate large numbers for Stellar operations.
23
+ *
24
+ * You can instantiate this value either from bigints, strings, or numbers
25
+ * (whole numbers, or this will throw).
26
+ *
27
+ * If you need to create a native BigInt from a list of integer "parts" (for
28
+ * example, you have a series of encoded 32-bit integers that represent a larger
29
+ * value), you can use the lower level abstraction {@link XdrLargeInt}. For
30
+ * example, you could do `new XdrLargeInt('u128', bytes...).toBigInt()`.
31
+ *
32
+ * @example
33
+ * import { xdr, ScInt } from "@stellar/stellar-base";
34
+ *
35
+ * // You have an ScVal from a contract and want to parse it into JS native.
36
+ * const value = xdr.ScVal.fromXDR(someXdr, "base64");
37
+ * const bigi = ScInt.fromScVal(value); // grab it as a BigInt
38
+ * let sci = new ScInt(bigi);
39
+ *
40
+ * sci.toNumber(); // gives native JS type (w/ size check)
41
+ * sci.toBigInt(); // gives the native BigInt value
42
+ * sci.toU64(); // gives ScValType-specific XDR constructs (with size checks)
43
+ *
44
+ * // You have a number and want to shove it into a contract.
45
+ * sci = ScInt(0xdeadcafebabe);
46
+ * sci.toBigInt() // returns 244838016400062n
47
+ * sci.toNumber() // throws: too large
48
+ *
49
+ * // Pass any to e.g. a Contract.call(), conversion happens automatically
50
+ * // regardless of the initial type.
51
+ * const scValU128 = sci.toU128();
52
+ * const scValI256 = sci.toI256();
53
+ * const scValU64 = sci.toU64();
54
+ *
55
+ * // Lots of ways to initialize:
56
+ * ScInt("123456789123456789")
57
+ * ScInt(123456789123456789n);
58
+ * ScInt(1n << 140n);
59
+ * ScInt(-42);
60
+ * ScInt.fromScVal(scValU128); // from above
61
+ *
62
+ * // If you know the type ahead of time (accessing `.raw` is faster than
63
+ * // conversions), you can specify the type directly (otherwise, it's
64
+ * // interpreted from the numbers you pass in):
65
+ * const i = ScInt(123456789n, { type: "u256" });
66
+ *
67
+ * // For example, you can use the underlying `sdk.U256` and convert it to an
68
+ * // `xdr.ScVal` directly like so:
69
+ * const scv = new xdr.ScVal.scvU256(i.raw);
70
+ *
71
+ * // Or reinterpret it as a different type (size permitting):
72
+ * const scv = i.toI64();
73
+ *
74
+ * @param {number|bigint|string} value - a single, integer-like value which will
75
+ * be interpreted in the smallest appropriate XDR type supported by Stellar
76
+ * (64, 128, or 256 bit integer values). signed values are supported, though
77
+ * they are sanity-checked against `opts.type`. if you need 32-bit values,
78
+ * you can construct them directly without needing this wrapper, e.g.
79
+ * `xdr.ScVal.scvU32(1234)`.
80
+ *
81
+ * @param {object} [opts] - an optional object controlling optional parameters
82
+ * @param {string} [opts.type] - force a specific data type. the type choices
83
+ * are: 'i64', 'u64', 'i128', 'u128', 'i256', and 'u256' (default: the
84
+ * smallest one that fits the `value`)
85
+ *
86
+ * @throws {RangeError} if the `value` is invalid (e.g. floating point), too
87
+ * large (i.e. exceeds a 256-bit value), or doesn't fit in the `opts.type`
88
+ *
89
+ * @throws {TypeError} on missing parameters, or if the "signedness" of `opts`
90
+ * doesn't match input `value`, e.g. passing `{type: 'u64'}` yet passing -1n
91
+ *
92
+ * @throws {SyntaxError} if a string `value` can't be parsed as a big integer
93
+ */
94
+ var ScInt = exports.ScInt = /*#__PURE__*/function (_XdrLargeInt) {
95
+ _inherits(ScInt, _XdrLargeInt);
96
+ var _super = _createSuper(ScInt);
97
+ function ScInt(value, opts) {
98
+ var _opts$type;
99
+ _classCallCheck(this, ScInt);
100
+ var signed = value < 0;
101
+ var type = (_opts$type = opts === null || opts === void 0 ? void 0 : opts.type) !== null && _opts$type !== void 0 ? _opts$type : '';
102
+ if (type.startsWith('u') && signed) {
103
+ throw TypeError("specified type ".concat(opts.type, " yet negative (").concat(value, ")"));
104
+ }
105
+
106
+ // If unspecified, we make a best guess at the type based on the bit length
107
+ // of the value, treating 64 as a minimum and 256 as a maximum.
108
+ if (type === '') {
109
+ type = signed ? 'i' : 'u';
110
+ var bitlen = nearestBigIntSize(value);
111
+ switch (bitlen) {
112
+ case 64:
113
+ case 128:
114
+ case 256:
115
+ type += bitlen.toString();
116
+ break;
117
+ default:
118
+ throw RangeError("expected 64/128/256 bits for input (".concat(value, "), got ").concat(bitlen));
119
+ }
120
+ }
121
+ return _super.call(this, type, value);
122
+ }
123
+ return _createClass(ScInt);
124
+ }(_xdr_large_int.XdrLargeInt);
125
+ function nearestBigIntSize(bigI) {
126
+ var _find;
127
+ // Note: Even though BigInt.toString(2) includes the negative sign for
128
+ // negative values (???), the following is still accurate, because the
129
+ // negative sign would be represented by a sign bit.
130
+ var bitlen = bigI.toString(2).length;
131
+ return (_find = [64, 128, 256].find(function (len) {
132
+ return bitlen <= len;
133
+ })) !== null && _find !== void 0 ? _find : bitlen;
134
+ }
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+
3
+ 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); }
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.Uint128 = void 0;
8
+ var _jsXdr = require("@stellar/js-xdr");
9
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
10
+ 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); } }
11
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
12
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
13
+ 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); }
14
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
15
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
16
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
17
+ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
18
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
19
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
20
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
21
+ var Uint128 = exports.Uint128 = /*#__PURE__*/function (_LargeInt) {
22
+ _inherits(Uint128, _LargeInt);
23
+ var _super = _createSuper(Uint128);
24
+ /**
25
+ * Construct an unsigned 128-bit integer that can be XDR-encoded.
26
+ *
27
+ * @param {Array<number|bigint|string>} args - one or more slices to encode
28
+ * in big-endian format (i.e. earlier elements are higher bits)
29
+ */
30
+ function Uint128() {
31
+ _classCallCheck(this, Uint128);
32
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
33
+ args[_key] = arguments[_key];
34
+ }
35
+ return _super.call(this, args);
36
+ }
37
+ _createClass(Uint128, [{
38
+ key: "unsigned",
39
+ get: function get() {
40
+ return true;
41
+ }
42
+ }, {
43
+ key: "size",
44
+ get: function get() {
45
+ return 128;
46
+ }
47
+ }]);
48
+ return Uint128;
49
+ }(_jsXdr.LargeInt);
50
+ Uint128.defineIntBoundaries();
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+
3
+ 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); }
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.Uint256 = void 0;
8
+ var _jsXdr = require("@stellar/js-xdr");
9
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
10
+ 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); } }
11
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
12
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
13
+ 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); }
14
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
15
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
16
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
17
+ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
18
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
19
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
20
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
21
+ var Uint256 = exports.Uint256 = /*#__PURE__*/function (_LargeInt) {
22
+ _inherits(Uint256, _LargeInt);
23
+ var _super = _createSuper(Uint256);
24
+ /**
25
+ * Construct an unsigned 256-bit integer that can be XDR-encoded.
26
+ *
27
+ * @param {Array<number|bigint|string>} args - one or more slices to encode
28
+ * in big-endian format (i.e. earlier elements are higher bits)
29
+ */
30
+ function Uint256() {
31
+ _classCallCheck(this, Uint256);
32
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
33
+ args[_key] = arguments[_key];
34
+ }
35
+ return _super.call(this, args);
36
+ }
37
+ _createClass(Uint256, [{
38
+ key: "unsigned",
39
+ get: function get() {
40
+ return true;
41
+ }
42
+ }, {
43
+ key: "size",
44
+ get: function get() {
45
+ return 256;
46
+ }
47
+ }]);
48
+ return Uint256;
49
+ }(_jsXdr.LargeInt);
50
+ Uint256.defineIntBoundaries();
@@ -0,0 +1,267 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.XdrLargeInt = void 0;
7
+ var _jsXdr = require("@stellar/js-xdr");
8
+ var _uint = require("./uint128");
9
+ var _uint2 = require("./uint256");
10
+ var _int = require("./int128");
11
+ var _int2 = require("./int256");
12
+ var _xdr = _interopRequireDefault(require("../xdr"));
13
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
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); } /* eslint no-bitwise: ["error", {"allow": [">>"]}] */
21
+ /**
22
+ * A wrapper class to represent large XDR-encodable integers.
23
+ *
24
+ * This operates at a lower level than {@link ScInt} by forcing you to specify
25
+ * the type / width / size in bits of the integer you're targeting, regardless
26
+ * of the input value(s) you provide.
27
+ *
28
+ * @param {string} type - force a specific data type. the type choices are:
29
+ * 'i64', 'u64', 'i128', 'u128', 'i256', and 'u256' (default: the smallest
30
+ * one that fits the `value`) (see {@link XdrLargeInt.isType})
31
+ * @param {number|bigint|string|Array<number|bigint|string>} values a list of
32
+ * integer-like values interpreted in big-endian order
33
+ */
34
+ var XdrLargeInt = exports.XdrLargeInt = /*#__PURE__*/function () {
35
+ function XdrLargeInt(type, values) {
36
+ _classCallCheck(this, XdrLargeInt);
37
+ /** @type {xdr.LargeInt} */
38
+ _defineProperty(this, "int", void 0);
39
+ // child class of a jsXdr.LargeInt
40
+ /** @type {string} */
41
+ _defineProperty(this, "type", void 0);
42
+ if (!(values instanceof Array)) {
43
+ values = [values];
44
+ }
45
+
46
+ // normalize values to one type
47
+ values = values.map(function (i) {
48
+ // micro-optimization to no-op on the likeliest input value:
49
+ if (typeof i === 'bigint') {
50
+ return i;
51
+ }
52
+ if (i instanceof XdrLargeInt) {
53
+ return i.toBigInt();
54
+ }
55
+ return BigInt(i);
56
+ });
57
+ switch (type) {
58
+ case 'i64':
59
+ this["int"] = new _jsXdr.Hyper(values);
60
+ break;
61
+ case 'i128':
62
+ this["int"] = new _int.Int128(values);
63
+ break;
64
+ case 'i256':
65
+ this["int"] = new _int2.Int256(values);
66
+ break;
67
+ case 'u64':
68
+ this["int"] = new _jsXdr.UnsignedHyper(values);
69
+ break;
70
+ case 'u128':
71
+ this["int"] = new _uint.Uint128(values);
72
+ break;
73
+ case 'u256':
74
+ this["int"] = new _uint2.Uint256(values);
75
+ break;
76
+ default:
77
+ throw TypeError("invalid type: ".concat(type));
78
+ }
79
+ this.type = type;
80
+ }
81
+
82
+ /**
83
+ * @returns {number}
84
+ * @throws {RangeError} if the value can't fit into a Number
85
+ */
86
+ _createClass(XdrLargeInt, [{
87
+ key: "toNumber",
88
+ value: function toNumber() {
89
+ var bi = this["int"].toBigInt();
90
+ if (bi > Number.MAX_SAFE_INTEGER || bi < Number.MIN_SAFE_INTEGER) {
91
+ throw RangeError("value ".concat(bi, " not in range for Number ") + "[".concat(Number.MAX_SAFE_INTEGER, ", ").concat(Number.MIN_SAFE_INTEGER, "]"));
92
+ }
93
+ return Number(bi);
94
+ }
95
+
96
+ /** @returns {bigint} */
97
+ }, {
98
+ key: "toBigInt",
99
+ value: function toBigInt() {
100
+ return this["int"].toBigInt();
101
+ }
102
+
103
+ /** @returns {xdr.ScVal} the integer encoded with `ScValType = I64` */
104
+ }, {
105
+ key: "toI64",
106
+ value: function toI64() {
107
+ this._sizeCheck(64);
108
+ var v = this.toBigInt();
109
+ if (BigInt.asIntN(64, v) !== v) {
110
+ throw RangeError("value too large for i64: ".concat(v));
111
+ }
112
+ return _xdr["default"].ScVal.scvI64(new _xdr["default"].Int64(v));
113
+ }
114
+
115
+ /** @returns {xdr.ScVal} the integer encoded with `ScValType = U64` */
116
+ }, {
117
+ key: "toU64",
118
+ value: function toU64() {
119
+ this._sizeCheck(64);
120
+ return _xdr["default"].ScVal.scvU64(new _xdr["default"].Uint64(BigInt.asUintN(64, this.toBigInt())) // reiterpret as unsigned
121
+ );
122
+ }
123
+
124
+ /**
125
+ * @returns {xdr.ScVal} the integer encoded with `ScValType = I128`
126
+ * @throws {RangeError} if the value cannot fit in 128 bits
127
+ */
128
+ }, {
129
+ key: "toI128",
130
+ value: function toI128() {
131
+ this._sizeCheck(128);
132
+ var v = this["int"].toBigInt();
133
+ var hi64 = BigInt.asIntN(64, v >> 64n); // encode top 64 w/ sign bit
134
+ var lo64 = BigInt.asUintN(64, v); // grab btm 64, encode sign
135
+
136
+ return _xdr["default"].ScVal.scvI128(new _xdr["default"].Int128Parts({
137
+ hi: new _xdr["default"].Int64(hi64),
138
+ lo: new _xdr["default"].Uint64(lo64)
139
+ }));
140
+ }
141
+
142
+ /**
143
+ * @returns {xdr.ScVal} the integer encoded with `ScValType = U128`
144
+ * @throws {RangeError} if the value cannot fit in 128 bits
145
+ */
146
+ }, {
147
+ key: "toU128",
148
+ value: function toU128() {
149
+ this._sizeCheck(128);
150
+ var v = this["int"].toBigInt();
151
+ return _xdr["default"].ScVal.scvU128(new _xdr["default"].UInt128Parts({
152
+ hi: new _xdr["default"].Uint64(BigInt.asUintN(64, v >> 64n)),
153
+ lo: new _xdr["default"].Uint64(BigInt.asUintN(64, v))
154
+ }));
155
+ }
156
+
157
+ /** @returns {xdr.ScVal} the integer encoded with `ScValType = I256` */
158
+ }, {
159
+ key: "toI256",
160
+ value: function toI256() {
161
+ var v = this["int"].toBigInt();
162
+ var hiHi64 = BigInt.asIntN(64, v >> 192n); // keep sign bit
163
+ var hiLo64 = BigInt.asUintN(64, v >> 128n);
164
+ var loHi64 = BigInt.asUintN(64, v >> 64n);
165
+ var loLo64 = BigInt.asUintN(64, v);
166
+ return _xdr["default"].ScVal.scvI256(new _xdr["default"].Int256Parts({
167
+ hiHi: new _xdr["default"].Int64(hiHi64),
168
+ hiLo: new _xdr["default"].Uint64(hiLo64),
169
+ loHi: new _xdr["default"].Uint64(loHi64),
170
+ loLo: new _xdr["default"].Uint64(loLo64)
171
+ }));
172
+ }
173
+
174
+ /** @returns {xdr.ScVal} the integer encoded with `ScValType = U256` */
175
+ }, {
176
+ key: "toU256",
177
+ value: function toU256() {
178
+ var v = this["int"].toBigInt();
179
+ var hiHi64 = BigInt.asUintN(64, v >> 192n); // encode sign bit
180
+ var hiLo64 = BigInt.asUintN(64, v >> 128n);
181
+ var loHi64 = BigInt.asUintN(64, v >> 64n);
182
+ var loLo64 = BigInt.asUintN(64, v);
183
+ return _xdr["default"].ScVal.scvU256(new _xdr["default"].UInt256Parts({
184
+ hiHi: new _xdr["default"].Uint64(hiHi64),
185
+ hiLo: new _xdr["default"].Uint64(hiLo64),
186
+ loHi: new _xdr["default"].Uint64(loHi64),
187
+ loLo: new _xdr["default"].Uint64(loLo64)
188
+ }));
189
+ }
190
+
191
+ /** @returns {xdr.ScVal} the smallest interpretation of the stored value */
192
+ }, {
193
+ key: "toScVal",
194
+ value: function toScVal() {
195
+ switch (this.type) {
196
+ case 'i64':
197
+ return this.toI64();
198
+ case 'i128':
199
+ return this.toI128();
200
+ case 'i256':
201
+ return this.toI256();
202
+ case 'u64':
203
+ return this.toU64();
204
+ case 'u128':
205
+ return this.toU128();
206
+ case 'u256':
207
+ return this.toU256();
208
+ default:
209
+ throw TypeError("invalid type: ".concat(this.type));
210
+ }
211
+ }
212
+ }, {
213
+ key: "valueOf",
214
+ value: function valueOf() {
215
+ return this["int"].valueOf();
216
+ }
217
+ }, {
218
+ key: "toString",
219
+ value: function toString() {
220
+ return this["int"].toString();
221
+ }
222
+ }, {
223
+ key: "toJSON",
224
+ value: function toJSON() {
225
+ return {
226
+ value: this.toBigInt().toString(),
227
+ type: this.type
228
+ };
229
+ }
230
+ }, {
231
+ key: "_sizeCheck",
232
+ value: function _sizeCheck(bits) {
233
+ if (this["int"].size > bits) {
234
+ throw RangeError("value too large for ".concat(bits, " bits (").concat(this.type, ")"));
235
+ }
236
+ }
237
+ }], [{
238
+ key: "isType",
239
+ value: function isType(type) {
240
+ switch (type) {
241
+ case 'i64':
242
+ case 'i128':
243
+ case 'i256':
244
+ case 'u64':
245
+ case 'u128':
246
+ case 'u256':
247
+ return true;
248
+ default:
249
+ return false;
250
+ }
251
+ }
252
+
253
+ /**
254
+ * Convert the raw `ScValType` string (e.g. 'scvI128', generated by the XDR)
255
+ * to a type description for {@link XdrLargeInt} construction (e.g. 'i128')
256
+ *
257
+ * @param {string} scvType the `xdr.ScValType` as a string
258
+ * @returns {string} a suitable equivalent type to construct this object
259
+ */
260
+ }, {
261
+ key: "getType",
262
+ value: function getType(scvType) {
263
+ return scvType.slice(3).toLowerCase();
264
+ }
265
+ }]);
266
+ return XdrLargeInt;
267
+ }();