@stellar/stellar-base 12.0.0 → 12.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/address.js CHANGED
@@ -36,7 +36,7 @@ var Address = exports.Address = /*#__PURE__*/function () {
36
36
  this._type = 'contract';
37
37
  this._key = _strkey.StrKey.decodeContract(address);
38
38
  } else {
39
- throw new Error('Unsupported address type');
39
+ throw new Error("Unsupported address type: ".concat(address));
40
40
  }
41
41
  }
42
42
 
package/lib/asset.js CHANGED
@@ -176,12 +176,12 @@ var Asset = exports.Asset = /*#__PURE__*/function () {
176
176
  }, {
177
177
  key: "getAssetType",
178
178
  value: function getAssetType() {
179
- switch (this.getRawAssetType()) {
180
- case _xdr["default"].AssetType.assetTypeNative():
179
+ switch (this.getRawAssetType().value) {
180
+ case _xdr["default"].AssetType.assetTypeNative().value:
181
181
  return 'native';
182
- case _xdr["default"].AssetType.assetTypeCreditAlphanum4():
182
+ case _xdr["default"].AssetType.assetTypeCreditAlphanum4().value:
183
183
  return 'credit_alphanum4';
184
- case _xdr["default"].AssetType.assetTypeCreditAlphanum12():
184
+ case _xdr["default"].AssetType.assetTypeCreditAlphanum12().value:
185
185
  return 'credit_alphanum12';
186
186
  default:
187
187
  return 'unknown';
package/lib/events.js CHANGED
@@ -4,10 +4,8 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.humanizeEvents = humanizeEvents;
7
- var _xdr = _interopRequireDefault(require("./xdr"));
8
7
  var _strkey = require("./strkey");
9
8
  var _scval = require("./scval");
10
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
11
9
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
12
10
  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; }
13
11
  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; }
@@ -30,14 +28,16 @@ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e
30
28
  */
31
29
  function humanizeEvents(events) {
32
30
  return events.map(function (e) {
33
- if (e instanceof _xdr["default"].DiagnosticEvent) {
31
+ // A pseudo-instanceof check for xdr.DiagnosticEvent more reliable
32
+ // in mixed SDK environments:
33
+ if (e.inSuccessfulContractCall) {
34
34
  return extractEvent(e.event());
35
35
  }
36
36
  return extractEvent(e);
37
37
  });
38
38
  }
39
39
  function extractEvent(event) {
40
- return _objectSpread(_objectSpread({}, event.contractId() != null && {
40
+ return _objectSpread(_objectSpread({}, typeof event.contractId === 'function' && event.contractId() != null && {
41
41
  contractId: _strkey.StrKey.encodeContract(event.contractId())
42
42
  }), {}, {
43
43
  type: event.type().name,
@@ -7,21 +7,30 @@ exports.extendFootprintTtl = extendFootprintTtl;
7
7
  var _xdr = _interopRequireDefault(require("../xdr"));
8
8
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
9
9
  /**
10
- * Builds an operation to bump the time-to-live of a footprint (read and written
11
- * ledger keys). Its only parameter is the new, absolute ledger sequence number
12
- * at which the entry will expire.
10
+ * Builds an operation to bump the time-to-live (TTL) of the ledger keys. The
11
+ * keys for extension have to be provided in the read-only footprint of
12
+ * the transaction.
13
13
  *
14
- * The footprint itself is derived from the transaction (see
14
+ * The only parameter of the operation itself is the new minimum TTL for
15
+ * all the provided entries. If an entry already has a higher TTL, then it
16
+ * will just be skipped.
17
+ *
18
+ * TTL is the number of ledgers from the current ledger (exclusive) until
19
+ * the last ledger the entry is still considered alive (inclusive). Thus
20
+ * the exact ledger until the entries will live will only be determined
21
+ * when transaction has been applied.
22
+ *
23
+ * The footprint has to be specified in the transaction. See
15
24
  * {@link TransactionBuilder}'s `opts.sorobanData` parameter, which is a
16
25
  * {@link xdr.SorobanTransactionData} instance that contains fee data & resource
17
- * usage as part of {@link xdr.SorobanResources}).
26
+ * usage as part of {@link xdr.SorobanResources}.
18
27
  *
19
28
  * @function
20
29
  * @alias Operation.extendFootprintTtl
21
30
  *
22
31
  * @param {object} opts - object holding operation parameters
23
- * @param {number} opts.extendTo - the absolute ledger sequence number at which
24
- * the transaction's ledger keys will now expire
32
+ * @param {number} opts.extendTo - the minimum TTL that all the entries in
33
+ * the read-only footprint will have
25
34
  * @param {string} [opts.source] - an optional source account
26
35
  *
27
36
  * @returns {xdr.Operation} an Extend Footprint TTL operation
@@ -30,7 +39,7 @@ function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default":
30
39
  function extendFootprintTtl(opts) {
31
40
  var _opts$extendTo;
32
41
  if (((_opts$extendTo = opts.extendTo) !== null && _opts$extendTo !== void 0 ? _opts$extendTo : -1) <= 0) {
33
- throw new RangeError("extendTo isn't a ledger quantity (uint32)");
42
+ throw new RangeError('extendTo has to be positive');
34
43
  }
35
44
  var extendFootprintOp = new _xdr["default"].ExtendFootprintTtlOp({
36
45
  ext: new _xdr["default"].ExtensionPoint(0),
@@ -7,14 +7,18 @@ exports.restoreFootprint = restoreFootprint;
7
7
  var _xdr = _interopRequireDefault(require("../xdr"));
8
8
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
9
9
  /**
10
- * Builds a footprint restoration operation.
10
+ * Builds an operation to restore the archived ledger entries specified
11
+ * by the ledger keys.
12
+ *
13
+ * The ledger keys to restore are specified separately from the operation
14
+ * in read-write footprint of the transaction.
11
15
  *
12
16
  * It takes no parameters because the relevant footprint is derived from the
13
- * transaction itself (see {@link TransactionBuilder}'s `opts.sorobanData`
17
+ * transaction itself. See {@link TransactionBuilder}'s `opts.sorobanData`
14
18
  * parameter (or {@link TransactionBuilder.setSorobanData} /
15
19
  * {@link TransactionBuilder.setLedgerKeys}), which is a
16
20
  * {@link xdr.SorobanTransactionData} instance that contains fee data & resource
17
- * usage as part of {@link xdr.SorobanTransactionData}).
21
+ * usage as part of {@link xdr.SorobanTransactionData}.
18
22
  *
19
23
  * @function
20
24
  * @alias Operation.restoreFootprint
package/lib/scval.js CHANGED
@@ -150,75 +150,87 @@ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" ==
150
150
  * scValToNative(scv) == gigaMap; // true
151
151
  */
152
152
  function nativeToScVal(val) {
153
- var _val$constructor$name, _val$constructor;
154
153
  var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
155
154
  switch (_typeof(val)) {
156
155
  case 'object':
157
- if (val === null) {
158
- return _xdr["default"].ScVal.scvVoid();
159
- }
160
- if (val instanceof _xdr["default"].ScVal) {
161
- return val; // should we copy?
162
- }
163
- if (val instanceof _address.Address) {
164
- return val.toScVal();
165
- }
166
- if (val instanceof _contract.Contract) {
167
- return val.address().toScVal();
168
- }
169
- if (val instanceof Uint8Array || Buffer.isBuffer(val)) {
170
- var _opts$type;
171
- var copy = Uint8Array.from(val);
172
- switch ((_opts$type = opts === null || opts === void 0 ? void 0 : opts.type) !== null && _opts$type !== void 0 ? _opts$type : 'bytes') {
173
- case 'bytes':
174
- return _xdr["default"].ScVal.scvBytes(copy);
175
- case 'symbol':
176
- return _xdr["default"].ScVal.scvSymbol(copy);
177
- case 'string':
178
- return _xdr["default"].ScVal.scvString(copy);
179
- default:
180
- throw new TypeError("invalid type (".concat(opts.type, ") specified for bytes-like value"));
156
+ {
157
+ var _val$constructor$name, _val$constructor;
158
+ if (val === null) {
159
+ return _xdr["default"].ScVal.scvVoid();
181
160
  }
182
- }
183
- if (Array.isArray(val)) {
184
- if (val.length > 0 && val.some(function (v) {
185
- return _typeof(v) !== _typeof(val[0]);
186
- })) {
187
- throw new TypeError("array values (".concat(val, ") must have the same type (types: ").concat(val.map(function (v) {
188
- return _typeof(v);
189
- }).join(','), ")"));
161
+ if (val instanceof _xdr["default"].ScVal) {
162
+ return val; // should we copy?
163
+ }
164
+ if (val instanceof _address.Address) {
165
+ return val.toScVal();
190
166
  }
191
- return _xdr["default"].ScVal.scvVec(val.map(function (v) {
192
- return nativeToScVal(v, opts);
167
+ if (val instanceof _contract.Contract) {
168
+ return val.address().toScVal();
169
+ }
170
+ if (val instanceof Uint8Array || Buffer.isBuffer(val)) {
171
+ var _opts$type;
172
+ var copy = Uint8Array.from(val);
173
+ switch ((_opts$type = opts === null || opts === void 0 ? void 0 : opts.type) !== null && _opts$type !== void 0 ? _opts$type : 'bytes') {
174
+ case 'bytes':
175
+ return _xdr["default"].ScVal.scvBytes(copy);
176
+ case 'symbol':
177
+ return _xdr["default"].ScVal.scvSymbol(copy);
178
+ case 'string':
179
+ return _xdr["default"].ScVal.scvString(copy);
180
+ default:
181
+ throw new TypeError("invalid type (".concat(opts.type, ") specified for bytes-like value"));
182
+ }
183
+ }
184
+ if (Array.isArray(val)) {
185
+ if (val.length > 0 && val.some(function (v) {
186
+ return _typeof(v) !== _typeof(val[0]);
187
+ })) {
188
+ throw new TypeError("array values (".concat(val, ") must have the same type (types: ").concat(val.map(function (v) {
189
+ return _typeof(v);
190
+ }).join(','), ")"));
191
+ }
192
+ return _xdr["default"].ScVal.scvVec(val.map(function (v) {
193
+ return nativeToScVal(v, opts);
194
+ }));
195
+ }
196
+ 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') {
197
+ var _val$constructor2;
198
+ 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), ")"));
199
+ }
200
+ return _xdr["default"].ScVal.scvMap(Object.entries(val)
201
+ // The Soroban runtime expects maps to have their keys in sorted
202
+ // order, so let's do that here as part of the conversion to prevent
203
+ // confusing error messages on execution.
204
+ .sort(function (_ref, _ref2) {
205
+ var _ref3 = _slicedToArray(_ref, 1),
206
+ key1 = _ref3[0];
207
+ var _ref4 = _slicedToArray(_ref2, 1),
208
+ key2 = _ref4[0];
209
+ return key1.localeCompare(key2);
210
+ }).map(function (_ref5) {
211
+ var _k, _opts$type2;
212
+ var _ref6 = _slicedToArray(_ref5, 2),
213
+ k = _ref6[0],
214
+ v = _ref6[1];
215
+ // the type can be specified with an entry for the key and the value,
216
+ // e.g. val = { 'hello': 1 } and opts.type = { hello: [ 'symbol',
217
+ // 'u128' ]} or you can use `null` for the default interpretation
218
+ var _ref7 = (_k = ((_opts$type2 = opts === null || opts === void 0 ? void 0 : opts.type) !== null && _opts$type2 !== void 0 ? _opts$type2 : {})[k]) !== null && _k !== void 0 ? _k : [null, null],
219
+ _ref8 = _slicedToArray(_ref7, 2),
220
+ keyType = _ref8[0],
221
+ valType = _ref8[1];
222
+ var keyOpts = keyType ? {
223
+ type: keyType
224
+ } : {};
225
+ var valOpts = valType ? {
226
+ type: valType
227
+ } : {};
228
+ return new _xdr["default"].ScMapEntry({
229
+ key: nativeToScVal(k, keyOpts),
230
+ val: nativeToScVal(v, valOpts)
231
+ });
193
232
  }));
194
233
  }
195
- 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') {
196
- var _val$constructor2;
197
- 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), ")"));
198
- }
199
- return _xdr["default"].ScVal.scvMap(Object.entries(val).map(function (_ref) {
200
- var _k, _opts$type2;
201
- var _ref2 = _slicedToArray(_ref, 2),
202
- k = _ref2[0],
203
- v = _ref2[1];
204
- // the type can be specified with an entry for the key and the value,
205
- // e.g. val = { 'hello': 1 } and opts.type = { hello: [ 'symbol',
206
- // 'u128' ]} or you can use `null` for the default interpretation
207
- var _ref3 = (_k = ((_opts$type2 = opts === null || opts === void 0 ? void 0 : opts.type) !== null && _opts$type2 !== void 0 ? _opts$type2 : {})[k]) !== null && _k !== void 0 ? _k : [null, null],
208
- _ref4 = _slicedToArray(_ref3, 2),
209
- keyType = _ref4[0],
210
- valType = _ref4[1];
211
- var keyOpts = keyType ? {
212
- type: keyType
213
- } : {};
214
- var valOpts = valType ? {
215
- type: valType
216
- } : {};
217
- return new _xdr["default"].ScMapEntry({
218
- key: nativeToScVal(k, keyOpts),
219
- val: nativeToScVal(v, valOpts)
220
- });
221
- }));
222
234
  case 'number':
223
235
  case 'bigint':
224
236
  switch (opts === null || opts === void 0 ? void 0 : opts.type) {
@@ -243,6 +255,10 @@ function nativeToScVal(val) {
243
255
  return _xdr["default"].ScVal.scvSymbol(val);
244
256
  case 'address':
245
257
  return new _address.Address(val).toScVal();
258
+ case 'u32':
259
+ return _xdr["default"].ScVal.scvU32(parseInt(val, 10));
260
+ case 'i32':
261
+ return _xdr["default"].ScVal.scvI32(parseInt(val, 10));
246
262
  default:
247
263
  if (_index.XdrLargeInt.isType(optType)) {
248
264
  return new _index.XdrLargeInt(optType, val).toScVal();
@@ -350,21 +366,23 @@ function scValToNative(scv) {
350
366
  case _xdr["default"].ScValType.scvTimepoint().value:
351
367
  case _xdr["default"].ScValType.scvDuration().value:
352
368
  return new _xdr["default"].Uint64(scv.value()).toBigInt();
353
- case _xdr["default"].ScValType.scvStatus().value:
354
- // TODO: Convert each status type into a human-readable error string?
355
- switch (scv.value()["switch"]()) {
356
- case _xdr["default"].ScStatusType.sstOk().value:
357
- case _xdr["default"].ScStatusType.sstUnknownError().value:
358
- case _xdr["default"].ScStatusType.sstHostValueError().value:
359
- case _xdr["default"].ScStatusType.sstHostObjectError().value:
360
- case _xdr["default"].ScStatusType.sstHostFunctionError().value:
361
- case _xdr["default"].ScStatusType.sstHostStorageError().value:
362
- case _xdr["default"].ScStatusType.sstHostContextError().value:
363
- case _xdr["default"].ScStatusType.sstVmError().value:
364
- case _xdr["default"].ScStatusType.sstContractError().value:
365
- case _xdr["default"].ScStatusType.sstHostAuthError().value:
369
+ case _xdr["default"].ScValType.scvError().value:
370
+ switch (scv.error()["switch"]().value) {
371
+ // Distinguish errors from the user contract.
372
+ case _xdr["default"].ScErrorType.sceContract().value:
373
+ return {
374
+ type: 'contract',
375
+ code: scv.error().contractCode()
376
+ };
366
377
  default:
367
- break;
378
+ {
379
+ var err = scv.error();
380
+ return {
381
+ type: 'system',
382
+ code: err.code().value,
383
+ value: err.code().name
384
+ };
385
+ }
368
386
  }
369
387
 
370
388
  // in the fallthrough case, just return the underlying value directly
@@ -206,6 +206,21 @@ var TransactionBuilder = exports.TransactionBuilder = /*#__PURE__*/function () {
206
206
  return this;
207
207
  }
208
208
 
209
+ /**
210
+ * Adds an operation to the transaction at a specific index.
211
+ *
212
+ * @param {xdr.Operation} operation - The xdr operation object to add, use {@link Operation} static methods.
213
+ * @param {number} index - The index at which to insert the operation.
214
+ *
215
+ * @returns {TransactionBuilder} - The TransactionBuilder instance for method chaining.
216
+ */
217
+ }, {
218
+ key: "addOperationAt",
219
+ value: function addOperationAt(operation, index) {
220
+ this.operations.splice(index, 0, operation);
221
+ return this;
222
+ }
223
+
209
224
  /**
210
225
  * Removes the operations from the builder (useful when cloning).
211
226
  * @returns {TransactionBuilder} this builder instance
@@ -217,6 +232,20 @@ var TransactionBuilder = exports.TransactionBuilder = /*#__PURE__*/function () {
217
232
  return this;
218
233
  }
219
234
 
235
+ /**
236
+ * Removes the operation at the specified index from the transaction.
237
+ *
238
+ * @param {number} index - The index of the operation to remove.
239
+ *
240
+ * @returns {TransactionBuilder} The TransactionBuilder instance for method chaining.
241
+ */
242
+ }, {
243
+ key: "clearOperationAt",
244
+ value: function clearOperationAt(index) {
245
+ this.operations.splice(index, 1);
246
+ return this;
247
+ }
248
+
220
249
  /**
221
250
  * Adds a memo to the transaction.
222
251
  * @param {Memo} memo {@link Memo} object
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stellar/stellar-base",
3
- "version": "12.0.0",
3
+ "version": "12.1.0",
4
4
  "description": "Low-level support library for the Stellar network.",
5
5
  "main": "./lib/index.js",
6
6
  "browser": {
@@ -70,15 +70,15 @@
70
70
  },
71
71
  "homepage": "https://github.com/stellar/js-stellar-base",
72
72
  "devDependencies": {
73
- "@babel/cli": "^7.24.6",
74
- "@babel/core": "^7.24.6",
75
- "@babel/eslint-parser": "^7.24.6",
76
- "@babel/eslint-plugin": "^7.24.6",
77
- "@babel/preset-env": "^7.24.6",
73
+ "@babel/cli": "^7.24.8",
74
+ "@babel/core": "^7.24.9",
75
+ "@babel/eslint-parser": "^7.24.8",
76
+ "@babel/eslint-plugin": "^7.24.7",
77
+ "@babel/preset-env": "^7.24.8",
78
78
  "@babel/register": "^7.24.6",
79
79
  "@definitelytyped/dtslint": "^0.0.182",
80
80
  "@istanbuljs/nyc-config-babel": "3.0.0",
81
- "@types/node": "^20.12.13",
81
+ "@types/node": "^20.14.11",
82
82
  "@typescript-eslint/parser": "^6.20.0",
83
83
  "babel-loader": "^9.1.3",
84
84
  "babel-plugin-istanbul": "^6.1.1",
@@ -91,7 +91,7 @@
91
91
  "eslint-plugin-import": "^2.29.1",
92
92
  "eslint-plugin-node": "^11.1.0",
93
93
  "eslint-plugin-prefer-import": "^0.0.1",
94
- "eslint-plugin-prettier": "^5.1.3",
94
+ "eslint-plugin-prettier": "^5.2.1",
95
95
  "eslint-webpack-plugin": "^4.2.0",
96
96
  "ghooks": "^2.0.4",
97
97
  "husky": "^8.0.3",
@@ -103,24 +103,24 @@
103
103
  "karma-mocha": "^2.0.0",
104
104
  "karma-sinon-chai": "^2.0.2",
105
105
  "karma-webpack": "^5.0.1",
106
- "lint-staged": "^15.2.5",
106
+ "lint-staged": "^15.2.7",
107
107
  "minami": "^1.1.1",
108
- "mocha": "^10.4.0",
108
+ "mocha": "^10.6.0",
109
109
  "node-polyfill-webpack-plugin": "^3.0.0",
110
110
  "nyc": "^15.1.0",
111
- "prettier": "^3.2.5",
111
+ "prettier": "^3.3.3",
112
112
  "randombytes": "^2.1.0",
113
113
  "sinon": "^16.1.0",
114
114
  "sinon-chai": "^3.7.0",
115
115
  "taffydb": "^2.7.3",
116
116
  "terser-webpack-plugin": "^5.3.10",
117
117
  "ts-node": "^10.9.2",
118
- "typescript": "^5.4.5",
119
- "webpack": "^5.90.3",
118
+ "typescript": "^5.5.3",
119
+ "webpack": "^5.93.0",
120
120
  "webpack-cli": "^5.1.1"
121
121
  },
122
122
  "dependencies": {
123
- "@stellar/js-xdr": "^3.1.1",
123
+ "@stellar/js-xdr": "^3.1.2",
124
124
  "base32.js": "^0.1.0",
125
125
  "bignumber.js": "^9.1.2",
126
126
  "buffer": "^6.0.3",
package/types/index.d.ts CHANGED
@@ -988,6 +988,7 @@ export namespace StrKey {
988
988
 
989
989
  function encodeContract(data: Buffer): string;
990
990
  function decodeContract(address: string): Buffer;
991
+ function isValidContract(address: string): boolean;
991
992
  }
992
993
 
993
994
  export namespace SignerKey {