starknet 11.0.1 → 11.0.2

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.
@@ -260,6 +260,7 @@ var starknet = (() => {
260
260
  OutsideExecutionCallerAny: () => OutsideExecutionCallerAny,
261
261
  PAYMASTER_RPC_NODES: () => PAYMASTER_RPC_NODES,
262
262
  PRIME: () => PRIME,
263
+ RANGE_CLASS_HASH: () => RANGE_CLASS_HASH,
263
264
  RANGE_CONTRACT_ADDRESS: () => RANGE_CONTRACT_ADDRESS,
264
265
  RANGE_ETH_ADDRESS: () => RANGE_ETH_ADDRESS,
265
266
  RANGE_FELT: () => RANGE_FELT,
@@ -1234,6 +1235,7 @@ var starknet = (() => {
1234
1235
  var RANGE_I128 = range(-(2n ** 127n), 2n ** 127n - 1n);
1235
1236
  var RANGE_ETH_ADDRESS = range(ZERO, 2n ** 160n - 1n);
1236
1237
  var RANGE_CONTRACT_ADDRESS = range(ZERO, ADDR_BOUND - 1n);
1238
+ var RANGE_CLASS_HASH = range(ZERO, 2n ** 251n - 1n);
1237
1239
  var LegacyUDC = {
1238
1240
  ADDRESS: "0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf",
1239
1241
  ENTRYPOINT: "deployContract"
@@ -2306,6 +2308,7 @@ ${indent}}` : "}";
2306
2308
  hexToBytes: () => hexToBytes3,
2307
2309
  hexToDecimalString: () => hexToDecimalString,
2308
2310
  isBigNumberish: () => isBigNumberish,
2311
+ isEmptyHex: () => isEmptyHex,
2309
2312
  isHex: () => isHex2,
2310
2313
  isHexString: () => isHexString2,
2311
2314
  isStringWholeNumber: () => isStringWholeNumber,
@@ -3052,6 +3055,7 @@ ${indent}}` : "}";
3052
3055
  }
3053
3056
  var isHexString2 = isHex2;
3054
3057
  function toBigInt(value) {
3058
+ assert(!isEmptyHex(value), "Invalid input: '0x' holds no hexadecimal digit");
3055
3059
  return BigInt(value);
3056
3060
  }
3057
3061
  function tryToBigInt(value) {
@@ -3076,13 +3080,13 @@ ${indent}}` : "}";
3076
3080
  return BigInt(addHexPrefix(hex)).toString(10);
3077
3081
  }
3078
3082
  function assertInRange(input, lowerBound, upperBound, inputName = "") {
3079
- const messageSuffix = inputName === "" ? "invalid length" : `invalid ${inputName} length`;
3080
3083
  const inputBigInt = BigInt(input);
3081
3084
  const lowerBoundBigInt = BigInt(lowerBound);
3082
3085
  const upperBoundBigInt = BigInt(upperBound);
3086
+ const rangeName = inputName === "" ? "range" : `${inputName} range`;
3083
3087
  assert(
3084
3088
  inputBigInt >= lowerBoundBigInt && inputBigInt <= upperBoundBigInt,
3085
- `Message not signable, ${messageSuffix}.`
3089
+ `Value is out of ${rangeName} [${lowerBoundBigInt}, ${upperBoundBigInt}]`
3086
3090
  );
3087
3091
  }
3088
3092
  function bigNumberishArrayToDecimalStringArray(data) {
@@ -3135,8 +3139,11 @@ ${indent}}` : "}";
3135
3139
  const result = int312(BigInt(addHexPrefix(buf2hex(sha256(utf8ToBytes(str))))));
3136
3140
  return hexToBytes3(toHex(result));
3137
3141
  }
3142
+ function isEmptyHex(value) {
3143
+ return isString(value) && /^0x$/i.test(value);
3144
+ }
3138
3145
  function isBigNumberish(input) {
3139
- return isNumber2(input) || isBigInt(input) || isString(input) && (isHex2(input) || isStringWholeNumber(input));
3146
+ return isNumber2(input) || isBigInt(input) || isString(input) && (isHex2(input) && !isEmptyHex(input) || isStringWholeNumber(input));
3140
3147
  }
3141
3148
  function getNext(iterator) {
3142
3149
  const it = iterator.next();
@@ -6625,16 +6632,18 @@ ${indent}}` : "}";
6625
6632
  /**
6626
6633
  * Throw unless the value can be carried by a felt252.
6627
6634
  *
6628
- * Null, undefined and any type this class does not read are refused first, each with its own
6629
- * message, then the value is converted and its range checked. A negative number does not reach
6630
- * that range check : converting it fails earlier, and the message comes from the conversion.
6635
+ * Null, undefined, any type this class does not read, and `'0x'` are refused first, each with its
6636
+ * own message, then the value is converted and its range checked. A negative number does not
6637
+ * reach that range check : converting it fails earlier, and the message comes from the conversion.
6631
6638
  * @param {BigNumberish | boolean} data the value to check
6632
- * @throws {Error} when the value is null, undefined, of an unread type, or outside [0, P)
6639
+ * @throws {Error} when the value is null, undefined, of an unread type, `'0x'`, or outside [0, P)
6633
6640
  * @example
6634
6641
  * ```typescript
6635
6642
  * CairoFelt252.validate('Hello'); // passes
6636
6643
  * CairoFelt252.validate({});
6637
6644
  * // throws Error("Unsupported data type 'object' for felt252. Expected string, number, bigint, or boolean")
6645
+ * CairoFelt252.validate('0x');
6646
+ * // throws Error("Invalid input: '0x' holds no hexadecimal digit")
6638
6647
  * CairoFelt252.validate(-1);
6639
6648
  * // throws Error("Cannot convert negative bigint -1 to Uint8Array")
6640
6649
  * ```
@@ -6646,6 +6655,7 @@ ${indent}}` : "}";
6646
6655
  isString(data) || isNumber2(data) || isBigInt(data) || isBoolean(data),
6647
6656
  `Unsupported data type '${typeof data}' for felt252. Expected string, number, bigint, or boolean`
6648
6657
  );
6658
+ assert(!isEmptyHex(data), "Invalid input: '0x' holds no hexadecimal digit");
6649
6659
  const value = _CairoFelt252.__processData(data);
6650
6660
  const bn = uint8ArrayToBigInt(value);
6651
6661
  _CairoFelt252.assertRange(bn);
@@ -7462,7 +7472,7 @@ ${indent}}` : "}";
7462
7472
  * otherwise. Text therefore only fits here up to 4 ASCII characters : one more already
7463
7473
  * makes a number past 4294967295.
7464
7474
  * @param {BigNumberish | boolean} data the value to carry, within [0, 4294967295]
7465
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
7475
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
7466
7476
  * @example
7467
7477
  * ```typescript
7468
7478
  * const result = new CairoUint32('abcd').toApiRequest();
@@ -7550,11 +7560,11 @@ ${indent}}` : "}";
7550
7560
  /**
7551
7561
  * Throw unless the value can be carried by a u32.
7552
7562
  *
7553
- * Four things are refused, each with its own message : a null or undefined value, an object or
7554
- * an array, a number with a decimal part, and a value outside [0, 4294967295]. A text string reaches
7563
+ * Five things are refused, each with its own message : a null or undefined value, an object or
7564
+ * an array, a number with a decimal part, `'0x'`, and a value outside [0, 4294967295]. A text string reaches
7555
7565
  * that last check as the number its bytes spell, so `'abcde'` is refused for being out of range.
7556
7566
  * @param {BigNumberish | boolean} data the value to check
7557
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
7567
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
7558
7568
  * @example
7559
7569
  * ```typescript
7560
7570
  * CairoUint32.validate(70000); // passes
@@ -7569,6 +7579,7 @@ ${indent}}` : "}";
7569
7579
  !isNumber2(data) || Number.isInteger(data),
7570
7580
  "Invalid input: decimal numbers are not supported, only integers"
7571
7581
  );
7582
+ assert(!isEmptyHex(data), "Invalid input: '0x' holds no hexadecimal digit");
7572
7583
  const value = _CairoUint32.__processData(data);
7573
7584
  assert(
7574
7585
  value >= RANGE_U32.min && value <= RANGE_U32.max,
@@ -8200,25 +8211,37 @@ ${indent}}` : "}";
8200
8211
  * Throw unless a whole value can be represented as a u256, and give back its number.
8201
8212
  *
8202
8213
  * A string is only accepted while it spells a number, in base 10 or 16 : one that does not is
8203
- * refused for its type.
8214
+ * refused as text, and `'0x'`, which holds no digit, is refused too. So is an object, a `Uint256`
8215
+ * one included : its two halves are checked by {@link CairoUint256.validateProps}, which is what
8216
+ * the constructor calls for it.
8204
8217
  * @param {BigNumberish} bigNumberish the value to check
8205
8218
  * @returns {bigint} the value as a number, once accepted
8206
- * @throws {Error} when the value is null, undefined, of an unread type, or out of range
8219
+ * @throws {Error} when the value is null, undefined, text, `'0x'`, of an unread type, a decimal number, or out of range
8207
8220
  * @example
8208
8221
  * ```typescript
8209
8222
  * const result = CairoUint256.validate(255);
8210
8223
  * // result = 255n
8211
8224
  * CairoUint256.validate(-1);
8212
8225
  * // throws Error("bigNumberish is smaller than UINT_256_MIN")
8226
+ * CairoUint256.validate('abc');
8227
+ * // throws Error("Invalid input: a u256 cannot be built from text")
8228
+ * CairoUint256.validate(1.5);
8229
+ * // throws Error("Invalid input: decimal numbers are not supported, only integers")
8213
8230
  * ```
8214
8231
  */
8215
8232
  static validate(bigNumberish) {
8216
8233
  assert(bigNumberish !== null, "null value is not allowed for u256");
8217
8234
  assert(bigNumberish !== void 0, "undefined value is not allowed for u256");
8235
+ assert(!isText(bigNumberish), "Invalid input: a u256 cannot be built from text");
8236
+ assert(!isEmptyHex(bigNumberish), "Invalid input: '0x' holds no hexadecimal digit");
8218
8237
  assert(
8219
- isBigNumberish(bigNumberish) || isObject2(bigNumberish),
8238
+ isBigNumberish(bigNumberish),
8220
8239
  `Unsupported data type '${typeof bigNumberish}' for u256. Expected a numeric string (decimal or hexadecimal), number, bigint, or Uint256 object`
8221
8240
  );
8241
+ assert(
8242
+ !isNumber2(bigNumberish) || Number.isInteger(bigNumberish),
8243
+ "Invalid input: decimal numbers are not supported, only integers"
8244
+ );
8222
8245
  const bigInt = BigInt(bigNumberish);
8223
8246
  assert(bigInt >= UINT_256_MIN, "bigNumberish is smaller than UINT_256_MIN");
8224
8247
  assert(bigInt <= UINT_256_MAX, "bigNumberish is bigger than UINT_256_MAX");
@@ -8232,18 +8255,31 @@ ${indent}}` : "}";
8232
8255
  * @param {BigNumberish} low the bottom 128 bits
8233
8256
  * @param {BigNumberish} high the top 128 bits
8234
8257
  * @returns {{low: bigint, high: bigint}} the two halves as numbers
8235
- * @throws {Error} when either half is outside the u128 range
8258
+ * @throws {Error} when either half is null, undefined, text, `'0x'`, not a number, a decimal number, or outside the u128 range
8236
8259
  * @example
8237
8260
  * ```typescript
8238
8261
  * const result = CairoUint256.validateProps(5, 4);
8239
8262
  * // result = { low: 5n, high: 4n }
8240
8263
  * CairoUint256.validateProps(-1, 4);
8241
8264
  * // throws Error("low is out of range UINT_256_LOW_MIN - UINT_256_LOW_MAX")
8265
+ * CairoUint256.validateProps(1.5, 4);
8266
+ * // throws Error("low must be an integer")
8267
+ * CairoUint256.validateProps('abc', 4);
8268
+ * // throws Error("low cannot be built from text")
8242
8269
  * ```
8243
8270
  */
8244
8271
  static validateProps(low, high) {
8245
- const bigIntLow = BigInt(low);
8246
- const bigIntHigh = BigInt(high);
8272
+ const validateHalf = (half, name) => {
8273
+ assert(half !== null, `${name} cannot be null`);
8274
+ assert(half !== void 0, `${name} cannot be undefined`);
8275
+ assert(!isText(half), `${name} cannot be built from text`);
8276
+ assert(!isEmptyHex(half), `${name} cannot be '0x', which holds no hexadecimal digit`);
8277
+ assert(isBigNumberish(half), `${name} must be a BigNumberish`);
8278
+ assert(!isNumber2(half) || Number.isInteger(half), `${name} must be an integer`);
8279
+ return BigInt(half);
8280
+ };
8281
+ const bigIntLow = validateHalf(low, "low");
8282
+ const bigIntHigh = validateHalf(high, "high");
8247
8283
  assert(
8248
8284
  bigIntLow >= UINT_256_LOW_MIN && bigIntLow <= UINT_256_LOW_MAX,
8249
8285
  "low is out of range UINT_256_LOW_MIN - UINT_256_LOW_MAX"
@@ -8452,25 +8488,37 @@ ${indent}}` : "}";
8452
8488
  * Throw unless a whole value can be represented as a u512, and give back its number.
8453
8489
  *
8454
8490
  * A string is only accepted while it spells a number, in base 10 or 16 : one that does not is
8455
- * refused for its type.
8491
+ * refused as text, and `'0x'`, which holds no digit, is refused too. So is an object, a `Uint512`
8492
+ * one included : its four limbs are checked by {@link CairoUint512.validateProps}, which is what
8493
+ * the constructor calls for it.
8456
8494
  * @param {BigNumberish} bigNumberish the value to check
8457
8495
  * @returns {bigint} the value as a number, once accepted
8458
- * @throws {Error} when the value is null, undefined, of an unread type, or out of range
8496
+ * @throws {Error} when the value is null, undefined, text, `'0x'`, of an unread type, a decimal number, or out of range
8459
8497
  * @example
8460
8498
  * ```typescript
8461
8499
  * const result = CairoUint512.validate(255);
8462
8500
  * // result = 255n
8463
8501
  * CairoUint512.validate(-1);
8464
8502
  * // throws Error("bigNumberish is smaller than UINT_512_MIN.")
8503
+ * CairoUint512.validate('abc');
8504
+ * // throws Error("Invalid input: a u512 cannot be built from text")
8505
+ * CairoUint512.validate(1.5);
8506
+ * // throws Error("Invalid input: decimal numbers are not supported, only integers")
8465
8507
  * ```
8466
8508
  */
8467
8509
  static validate(bigNumberish) {
8468
8510
  assert(bigNumberish !== null, "null value is not allowed for u512");
8469
8511
  assert(bigNumberish !== void 0, "undefined value is not allowed for u512");
8512
+ assert(!isText(bigNumberish), "Invalid input: a u512 cannot be built from text");
8513
+ assert(!isEmptyHex(bigNumberish), "Invalid input: '0x' holds no hexadecimal digit");
8470
8514
  assert(
8471
- isBigNumberish(bigNumberish) || isObject2(bigNumberish),
8515
+ isBigNumberish(bigNumberish),
8472
8516
  `Unsupported data type '${typeof bigNumberish}' for u512. Expected a numeric string (decimal or hexadecimal), number, bigint, or Uint512 object`
8473
8517
  );
8518
+ assert(
8519
+ !isNumber2(bigNumberish) || Number.isInteger(bigNumberish),
8520
+ "Invalid input: decimal numbers are not supported, only integers"
8521
+ );
8474
8522
  const bigInt = BigInt(bigNumberish);
8475
8523
  assert(bigInt >= UINT_512_MIN, "bigNumberish is smaller than UINT_512_MIN.");
8476
8524
  assert(bigInt <= UINT_512_MAX, "bigNumberish is bigger than UINT_512_MAX.");
@@ -8486,20 +8534,33 @@ ${indent}}` : "}";
8486
8534
  * @param {BigNumberish} limb2 bits 256 to 383
8487
8535
  * @param {BigNumberish} limb3 bits 384 to 511
8488
8536
  * @returns {{limb0: bigint, limb1: bigint, limb2: bigint, limb3: bigint}} the four limbs as numbers
8489
- * @throws {Error} when any limb is outside the u128 range
8537
+ * @throws {Error} when any limb is null, undefined, text, `'0x'`, not a number, a decimal number, or outside the u128 range
8490
8538
  * @example
8491
8539
  * ```typescript
8492
8540
  * const result = CairoUint512.validateProps(7, 0, 0, 64);
8493
8541
  * // result = { limb0: 7n, limb1: 0n, limb2: 0n, limb3: 64n }
8494
8542
  * CairoUint512.validateProps(-1, 0, 0, 0);
8495
8543
  * // throws Error("limb0 is not in the range of a u128 number")
8544
+ * CairoUint512.validateProps(1.5, 0, 0, 0);
8545
+ * // throws Error("limb0 must be an integer")
8546
+ * CairoUint512.validateProps('abc', 0, 0, 0);
8547
+ * // throws Error("limb0 cannot be built from text")
8496
8548
  * ```
8497
8549
  */
8498
8550
  static validateProps(limb0, limb1, limb2, limb3) {
8499
- const l0 = BigInt(limb0);
8500
- const l1 = BigInt(limb1);
8501
- const l2 = BigInt(limb2);
8502
- const l3 = BigInt(limb3);
8551
+ const validateLimb = (limb, name) => {
8552
+ assert(limb !== null, `${name} cannot be null`);
8553
+ assert(limb !== void 0, `${name} cannot be undefined`);
8554
+ assert(!isText(limb), `${name} cannot be built from text`);
8555
+ assert(!isEmptyHex(limb), `${name} cannot be '0x', which holds no hexadecimal digit`);
8556
+ assert(isBigNumberish(limb), `${name} must be a BigNumberish`);
8557
+ assert(!isNumber2(limb) || Number.isInteger(limb), `${name} must be an integer`);
8558
+ return BigInt(limb);
8559
+ };
8560
+ const l0 = validateLimb(limb0, "limb0");
8561
+ const l1 = validateLimb(limb1, "limb1");
8562
+ const l2 = validateLimb(limb2, "limb2");
8563
+ const l3 = validateLimb(limb3, "limb3");
8503
8564
  [l0, l1, l2, l3].forEach((value, index) => {
8504
8565
  assert(
8505
8566
  value >= UINT_128_MIN && value <= UINT_128_MAX,
@@ -8671,7 +8732,7 @@ ${indent}}` : "}";
8671
8732
  * otherwise. Text therefore only fits here up to 12 ASCII characters : one more already
8672
8733
  * makes a number past 79228162514264337593543950335.
8673
8734
  * @param {BigNumberish | boolean} data the value to carry, within [0, 79228162514264337593543950335]
8674
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
8735
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
8675
8736
  * @example
8676
8737
  * ```typescript
8677
8738
  * const result = new CairoUint96('abcdefghijkl').toApiRequest();
@@ -8759,11 +8820,11 @@ ${indent}}` : "}";
8759
8820
  /**
8760
8821
  * Throw unless the value can be carried by a u96.
8761
8822
  *
8762
- * Four things are refused, each with its own message : a null or undefined value, an object or
8763
- * an array, a number with a decimal part, and a value outside [0, 79228162514264337593543950335]. A text string reaches
8823
+ * Five things are refused, each with its own message : a null or undefined value, an object or
8824
+ * an array, a number with a decimal part, `'0x'`, and a value outside [0, 79228162514264337593543950335]. A text string reaches
8764
8825
  * that last check as the number its bytes spell, so `'abcdefghijklm'` is refused for being out of range.
8765
8826
  * @param {BigNumberish | boolean} data the value to check
8766
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
8827
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
8767
8828
  * @example
8768
8829
  * ```typescript
8769
8830
  * CairoUint96.validate(5000000000); // passes
@@ -8778,6 +8839,7 @@ ${indent}}` : "}";
8778
8839
  !isNumber2(data) || Number.isInteger(data),
8779
8840
  "Invalid input: decimal numbers are not supported, only integers"
8780
8841
  );
8842
+ assert(!isEmptyHex(data), "Invalid input: '0x' holds no hexadecimal digit");
8781
8843
  const value = _CairoUint96.__processData(data);
8782
8844
  assert(
8783
8845
  value >= RANGE_U96.min && value <= RANGE_U96.max,
@@ -9164,7 +9226,7 @@ ${indent}}` : "}";
9164
9226
  * otherwise. Text therefore only fits here while it is a single ASCII character : two characters
9165
9227
  * already make a number past 255.
9166
9228
  * @param {BigNumberish | boolean} data the value to carry, within [0, 255]
9167
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
9229
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
9168
9230
  * @example
9169
9231
  * ```typescript
9170
9232
  * const result = new CairoUint8('a').toApiRequest();
@@ -9254,11 +9316,11 @@ ${indent}}` : "}";
9254
9316
  /**
9255
9317
  * Throw unless the value can be carried by a u8.
9256
9318
  *
9257
- * Four things are refused, each with its own message : a null or undefined value, an object or
9258
- * an array, a number with a decimal part, and a value outside [0, 255]. A text string reaches
9319
+ * Five things are refused, each with its own message : a null or undefined value, an object or
9320
+ * an array, a number with a decimal part, `'0x'`, and a value outside [0, 255]. A text string reaches
9259
9321
  * that last check as the number its bytes spell, so `'ab'` is refused for being out of range.
9260
9322
  * @param {BigNumberish | boolean} data the value to check
9261
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
9323
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
9262
9324
  * @example
9263
9325
  * ```typescript
9264
9326
  * CairoUint8.validate(44); // passes
@@ -9273,6 +9335,7 @@ ${indent}}` : "}";
9273
9335
  !isNumber2(data) || Number.isInteger(data),
9274
9336
  "Invalid input: decimal numbers are not supported, only integers"
9275
9337
  );
9338
+ assert(!isEmptyHex(data), "Invalid input: '0x' holds no hexadecimal digit");
9276
9339
  const value = _CairoUint8.__processData(data);
9277
9340
  assert(
9278
9341
  value >= RANGE_U8.min && value <= RANGE_U8.max,
@@ -9363,7 +9426,7 @@ ${indent}}` : "}";
9363
9426
  * otherwise. Text therefore only fits here up to 2 ASCII characters : one more already
9364
9427
  * makes a number past 65535.
9365
9428
  * @param {BigNumberish | boolean} data the value to carry, within [0, 65535]
9366
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
9429
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
9367
9430
  * @example
9368
9431
  * ```typescript
9369
9432
  * const result = new CairoUint16('ab').toApiRequest();
@@ -9451,11 +9514,11 @@ ${indent}}` : "}";
9451
9514
  /**
9452
9515
  * Throw unless the value can be carried by a u16.
9453
9516
  *
9454
- * Four things are refused, each with its own message : a null or undefined value, an object or
9455
- * an array, a number with a decimal part, and a value outside [0, 65535]. A text string reaches
9517
+ * Five things are refused, each with its own message : a null or undefined value, an object or
9518
+ * an array, a number with a decimal part, `'0x'`, and a value outside [0, 65535]. A text string reaches
9456
9519
  * that last check as the number its bytes spell, so `'abc'` is refused for being out of range.
9457
9520
  * @param {BigNumberish | boolean} data the value to check
9458
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
9521
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
9459
9522
  * @example
9460
9523
  * ```typescript
9461
9524
  * CairoUint16.validate(300); // passes
@@ -9470,6 +9533,7 @@ ${indent}}` : "}";
9470
9533
  !isNumber2(data) || Number.isInteger(data),
9471
9534
  "Invalid input: decimal numbers are not supported, only integers"
9472
9535
  );
9536
+ assert(!isEmptyHex(data), "Invalid input: '0x' holds no hexadecimal digit");
9473
9537
  const value = _CairoUint16.__processData(data);
9474
9538
  assert(
9475
9539
  value >= RANGE_U16.min && value <= RANGE_U16.max,
@@ -9560,7 +9624,7 @@ ${indent}}` : "}";
9560
9624
  * otherwise. Text therefore only fits here up to 8 ASCII characters : one more already
9561
9625
  * makes a number past 18446744073709551615.
9562
9626
  * @param {BigNumberish | boolean} data the value to carry, within [0, 18446744073709551615]
9563
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
9627
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
9564
9628
  * @example
9565
9629
  * ```typescript
9566
9630
  * const result = new CairoUint64('abcdefgh').toApiRequest();
@@ -9648,11 +9712,11 @@ ${indent}}` : "}";
9648
9712
  /**
9649
9713
  * Throw unless the value can be carried by a u64.
9650
9714
  *
9651
- * Four things are refused, each with its own message : a null or undefined value, an object or
9652
- * an array, a number with a decimal part, and a value outside [0, 18446744073709551615]. A text string reaches
9715
+ * Five things are refused, each with its own message : a null or undefined value, an object or
9716
+ * an array, a number with a decimal part, `'0x'`, and a value outside [0, 18446744073709551615]. A text string reaches
9653
9717
  * that last check as the number its bytes spell, so `'abcdefghi'` is refused for being out of range.
9654
9718
  * @param {BigNumberish | boolean} data the value to check
9655
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
9719
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
9656
9720
  * @example
9657
9721
  * ```typescript
9658
9722
  * CairoUint64.validate(5000000000); // passes
@@ -9667,6 +9731,7 @@ ${indent}}` : "}";
9667
9731
  !isNumber2(data) || Number.isInteger(data),
9668
9732
  "Invalid input: decimal numbers are not supported, only integers"
9669
9733
  );
9734
+ assert(!isEmptyHex(data), "Invalid input: '0x' holds no hexadecimal digit");
9670
9735
  const value = _CairoUint64.__processData(data);
9671
9736
  assert(
9672
9737
  value >= RANGE_U64.min && value <= RANGE_U64.max,
@@ -9757,7 +9822,7 @@ ${indent}}` : "}";
9757
9822
  * otherwise. Text therefore only fits here up to 16 ASCII characters : one more already
9758
9823
  * makes a number past 340282366920938463463374607431768211455.
9759
9824
  * @param {BigNumberish | boolean} data the value to carry, within [0, 340282366920938463463374607431768211455]
9760
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
9825
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
9761
9826
  * @example
9762
9827
  * ```typescript
9763
9828
  * const result = new CairoUint128('abcdefghijklmnop').toApiRequest();
@@ -9845,11 +9910,11 @@ ${indent}}` : "}";
9845
9910
  /**
9846
9911
  * Throw unless the value can be carried by a u128.
9847
9912
  *
9848
- * Four things are refused, each with its own message : a null or undefined value, an object or
9849
- * an array, a number with a decimal part, and a value outside [0, 340282366920938463463374607431768211455]. A text string reaches
9913
+ * Five things are refused, each with its own message : a null or undefined value, an object or
9914
+ * an array, a number with a decimal part, `'0x'`, and a value outside [0, 340282366920938463463374607431768211455]. A text string reaches
9850
9915
  * that last check as the number its bytes spell, so `'abcdefghijklmnopq'` is refused for being out of range.
9851
9916
  * @param {BigNumberish | boolean} data the value to check
9852
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
9917
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
9853
9918
  * @example
9854
9919
  * ```typescript
9855
9920
  * CairoUint128.validate(5000000000); // passes
@@ -9864,6 +9929,7 @@ ${indent}}` : "}";
9864
9929
  !isNumber2(data) || Number.isInteger(data),
9865
9930
  "Invalid input: decimal numbers are not supported, only integers"
9866
9931
  );
9932
+ assert(!isEmptyHex(data), "Invalid input: '0x' holds no hexadecimal digit");
9867
9933
  const value = _CairoUint128.__processData(data);
9868
9934
  assert(
9869
9935
  value >= RANGE_U128.min && value <= RANGE_U128.max,
@@ -9950,15 +10016,17 @@ ${indent}}` : "}";
9950
10016
  /**
9951
10017
  * Build from a number, a string or a boolean, refusing anything out of the i8 range.
9952
10018
  *
9953
- * A string is read as a number when it spells one — decimal or hexadecimal — and as UTF-8 text
9954
- * otherwise. Text therefore only fits here up to a single ASCII character : one more already
9955
- * makes a number past 127.
10019
+ * A string is read as a number when it spells one — decimal or hexadecimal, with or without a
10020
+ * leading minus sign — and as UTF-8 text otherwise. Text therefore only fits here up to a single
10021
+ * ASCII character : one more already makes a number past 127.
9956
10022
  * @param {BigNumberish | boolean} data the value to carry, within [-128, 127]
9957
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
10023
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
9958
10024
  * @example
9959
10025
  * ```typescript
9960
10026
  * const result = new CairoInt8('a').toApiRequest();
9961
10027
  * // result = ["97"]
10028
+ * const result2 = new CairoInt8('-5').toApiRequest();
10029
+ * // result2 = ["3618502788666131213697322783095070105623107215331596699973092056135872020476"]
9962
10030
  * ```
9963
10031
  */
9964
10032
  constructor(data) {
@@ -9970,6 +10038,9 @@ ${indent}}` : "}";
9970
10038
  *
9971
10039
  * Nothing here refuses a value : `validate` is what reads this number and decides. So an input
9972
10040
  * far outside the i8 range comes back untouched rather than raising.
10041
+ *
10042
+ * A minus sign in front of a number makes it negative rather than text : `'-5'` and `'-0x5'` are
10043
+ * both -5. This holds for the signed integers only — for any other type, such a string is text.
9973
10044
  * @param {BigNumberish | boolean} data the value to convert
9974
10045
  * @returns {bigint} the number the input spells, of whatever size
9975
10046
  * @example
@@ -9978,9 +10049,14 @@ ${indent}}` : "}";
9978
10049
  * // result = 97n
9979
10050
  * const result2 = CairoInt8.__processData('ab');
9980
10051
  * // result2 = 24930n (past the i8 range, and returned all the same)
10052
+ * const result3 = CairoInt8.__processData('-0x5');
10053
+ * // result3 = -5n (a negative number, not the text "-0x5")
9981
10054
  * ```
9982
10055
  */
9983
10056
  static __processData(data) {
10057
+ if (isString(data) && data.startsWith("-") && isBigNumberish(data.slice(1))) {
10058
+ return -BigInt(data.slice(1));
10059
+ }
9984
10060
  if (isString(data) && isText(data)) {
9985
10061
  return utf8ToBigInt(data);
9986
10062
  }
@@ -10059,11 +10135,12 @@ ${indent}}` : "}";
10059
10135
  /**
10060
10136
  * Throw unless the value can be carried by an i8.
10061
10137
  *
10062
- * Four things are refused, each with its own message : a null or undefined value, an object or
10063
- * an array, a number with a decimal part, and a value outside [-128, 127]. A text string reaches
10064
- * that last check as the number its bytes spell, so `'ab'` is refused for being out of range.
10138
+ * Five things are refused, each with its own message : a null or undefined value, an object or
10139
+ * an array, a number with a decimal part, `'0x'`, and a value outside [-128, 127]. A text string
10140
+ * reaches that last check as the number its bytes spell, so `'ab'` is refused for being out of
10141
+ * range.
10065
10142
  * @param {BigNumberish | boolean} data the value to check
10066
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
10143
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
10067
10144
  * @example
10068
10145
  * ```typescript
10069
10146
  * CairoInt8.validate(5); // passes
@@ -10078,6 +10155,7 @@ ${indent}}` : "}";
10078
10155
  !isNumber2(data) || Number.isInteger(data),
10079
10156
  "Invalid input: decimal numbers are not supported, only integers"
10080
10157
  );
10158
+ assert(!isEmptyHex(data), "Invalid input: '0x' holds no hexadecimal digit");
10081
10159
  const value = _CairoInt8.__processData(data);
10082
10160
  assert(
10083
10161
  value >= RANGE_I8.min && value <= RANGE_I8.max,
@@ -10168,15 +10246,17 @@ ${indent}}` : "}";
10168
10246
  /**
10169
10247
  * Build from a number, a string or a boolean, refusing anything out of the i16 range.
10170
10248
  *
10171
- * A string is read as a number when it spells one — decimal or hexadecimal — and as UTF-8 text
10172
- * otherwise. Text therefore only fits here up to 2 ASCII characters : one more already
10173
- * makes a number past 32767.
10249
+ * A string is read as a number when it spells one — decimal or hexadecimal, with or without a
10250
+ * leading minus sign — and as UTF-8 text otherwise. Text therefore only fits here up to 2 ASCII
10251
+ * characters : one more already makes a number past 32767.
10174
10252
  * @param {BigNumberish | boolean} data the value to carry, within [-32768, 32767]
10175
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
10253
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
10176
10254
  * @example
10177
10255
  * ```typescript
10178
10256
  * const result = new CairoInt16('ab').toApiRequest();
10179
10257
  * // result = ["24930"]
10258
+ * const result2 = new CairoInt16('-5').toApiRequest();
10259
+ * // result2 = ["3618502788666131213697322783095070105623107215331596699973092056135872020476"]
10180
10260
  * ```
10181
10261
  */
10182
10262
  constructor(data) {
@@ -10188,6 +10268,9 @@ ${indent}}` : "}";
10188
10268
  *
10189
10269
  * Nothing here refuses a value : `validate` is what reads this number and decides. So an input
10190
10270
  * far outside the i16 range comes back untouched rather than raising.
10271
+ *
10272
+ * A minus sign in front of a number makes it negative rather than text : `'-5'` and `'-0x5'` are
10273
+ * both -5. This holds for the signed integers only — for any other type, such a string is text.
10191
10274
  * @param {BigNumberish | boolean} data the value to convert
10192
10275
  * @returns {bigint} the number the input spells, of whatever size
10193
10276
  * @example
@@ -10196,9 +10279,14 @@ ${indent}}` : "}";
10196
10279
  * // result = 24930n
10197
10280
  * const result2 = CairoInt16.__processData('abc');
10198
10281
  * // result2 = 6382179n (past the i16 range, and returned all the same)
10282
+ * const result3 = CairoInt16.__processData('-0x5');
10283
+ * // result3 = -5n (a negative number, not the text "-0x5")
10199
10284
  * ```
10200
10285
  */
10201
10286
  static __processData(data) {
10287
+ if (isString(data) && data.startsWith("-") && isBigNumberish(data.slice(1))) {
10288
+ return -BigInt(data.slice(1));
10289
+ }
10202
10290
  if (isString(data) && isText(data)) {
10203
10291
  return utf8ToBigInt(data);
10204
10292
  }
@@ -10277,11 +10365,11 @@ ${indent}}` : "}";
10277
10365
  /**
10278
10366
  * Throw unless the value can be carried by an i16.
10279
10367
  *
10280
- * Four things are refused, each with its own message : a null or undefined value, an object or
10281
- * an array, a number with a decimal part, and a value outside [-32768, 32767]. A text string reaches
10368
+ * Five things are refused, each with its own message : a null or undefined value, an object or
10369
+ * an array, a number with a decimal part, `'0x'`, and a value outside [-32768, 32767]. A text string reaches
10282
10370
  * that last check as the number its bytes spell, so `'abc'` is refused for being out of range.
10283
10371
  * @param {BigNumberish | boolean} data the value to check
10284
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
10372
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
10285
10373
  * @example
10286
10374
  * ```typescript
10287
10375
  * CairoInt16.validate(300); // passes
@@ -10296,6 +10384,7 @@ ${indent}}` : "}";
10296
10384
  !isNumber2(data) || Number.isInteger(data),
10297
10385
  "Invalid input: decimal numbers are not supported, only integers"
10298
10386
  );
10387
+ assert(!isEmptyHex(data), "Invalid input: '0x' holds no hexadecimal digit");
10299
10388
  const value = _CairoInt16.__processData(data);
10300
10389
  assert(
10301
10390
  value >= RANGE_I16.min && value <= RANGE_I16.max,
@@ -10386,15 +10475,17 @@ ${indent}}` : "}";
10386
10475
  /**
10387
10476
  * Build from a number, a string or a boolean, refusing anything out of the i32 range.
10388
10477
  *
10389
- * A string is read as a number when it spells one — decimal or hexadecimal — and as UTF-8 text
10390
- * otherwise. Text therefore only fits here up to 4 ASCII characters : one more already
10391
- * makes a number past 2147483647.
10478
+ * A string is read as a number when it spells one — decimal or hexadecimal, with or without a
10479
+ * leading minus sign — and as UTF-8 text otherwise. Text therefore only fits here up to 4 ASCII
10480
+ * characters : one more already makes a number past 2147483647.
10392
10481
  * @param {BigNumberish | boolean} data the value to carry, within [-2147483648, 2147483647]
10393
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
10482
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
10394
10483
  * @example
10395
10484
  * ```typescript
10396
10485
  * const result = new CairoInt32('abcd').toApiRequest();
10397
10486
  * // result = ["1633837924"]
10487
+ * const result2 = new CairoInt32('-5').toApiRequest();
10488
+ * // result2 = ["3618502788666131213697322783095070105623107215331596699973092056135872020476"]
10398
10489
  * ```
10399
10490
  */
10400
10491
  constructor(data) {
@@ -10406,6 +10497,9 @@ ${indent}}` : "}";
10406
10497
  *
10407
10498
  * Nothing here refuses a value : `validate` is what reads this number and decides. So an input
10408
10499
  * far outside the i32 range comes back untouched rather than raising.
10500
+ *
10501
+ * A minus sign in front of a number makes it negative rather than text : `'-5'` and `'-0x5'` are
10502
+ * both -5. This holds for the signed integers only — for any other type, such a string is text.
10409
10503
  * @param {BigNumberish | boolean} data the value to convert
10410
10504
  * @returns {bigint} the number the input spells, of whatever size
10411
10505
  * @example
@@ -10414,9 +10508,14 @@ ${indent}}` : "}";
10414
10508
  * // result = 1633837924n
10415
10509
  * const result2 = CairoInt32.__processData('abcde');
10416
10510
  * // result2 = 418262508645n (past the i32 range, and returned all the same)
10511
+ * const result3 = CairoInt32.__processData('-0x5');
10512
+ * // result3 = -5n (a negative number, not the text "-0x5")
10417
10513
  * ```
10418
10514
  */
10419
10515
  static __processData(data) {
10516
+ if (isString(data) && data.startsWith("-") && isBigNumberish(data.slice(1))) {
10517
+ return -BigInt(data.slice(1));
10518
+ }
10420
10519
  if (isString(data) && isText(data)) {
10421
10520
  return utf8ToBigInt(data);
10422
10521
  }
@@ -10495,11 +10594,11 @@ ${indent}}` : "}";
10495
10594
  /**
10496
10595
  * Throw unless the value can be carried by an i32.
10497
10596
  *
10498
- * Four things are refused, each with its own message : a null or undefined value, an object or
10499
- * an array, a number with a decimal part, and a value outside [-2147483648, 2147483647]. A text string reaches
10597
+ * Five things are refused, each with its own message : a null or undefined value, an object or
10598
+ * an array, a number with a decimal part, `'0x'`, and a value outside [-2147483648, 2147483647]. A text string reaches
10500
10599
  * that last check as the number its bytes spell, so `'abcde'` is refused for being out of range.
10501
10600
  * @param {BigNumberish | boolean} data the value to check
10502
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
10601
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
10503
10602
  * @example
10504
10603
  * ```typescript
10505
10604
  * CairoInt32.validate(70000); // passes
@@ -10514,6 +10613,7 @@ ${indent}}` : "}";
10514
10613
  !isNumber2(data) || Number.isInteger(data),
10515
10614
  "Invalid input: decimal numbers are not supported, only integers"
10516
10615
  );
10616
+ assert(!isEmptyHex(data), "Invalid input: '0x' holds no hexadecimal digit");
10517
10617
  const value = _CairoInt32.__processData(data);
10518
10618
  assert(
10519
10619
  value >= RANGE_I32.min && value <= RANGE_I32.max,
@@ -10604,15 +10704,17 @@ ${indent}}` : "}";
10604
10704
  /**
10605
10705
  * Build from a number, a string or a boolean, refusing anything out of the i64 range.
10606
10706
  *
10607
- * A string is read as a number when it spells one — decimal or hexadecimal — and as UTF-8 text
10608
- * otherwise. Text therefore only fits here up to 8 ASCII characters : one more already
10609
- * makes a number past 9223372036854775807.
10707
+ * A string is read as a number when it spells one — decimal or hexadecimal, with or without a
10708
+ * leading minus sign — and as UTF-8 text otherwise. Text therefore only fits here up to 8 ASCII
10709
+ * characters : one more already makes a number past 9223372036854775807.
10610
10710
  * @param {BigNumberish | boolean} data the value to carry, within [-9223372036854775808, 9223372036854775807]
10611
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
10711
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
10612
10712
  * @example
10613
10713
  * ```typescript
10614
10714
  * const result = new CairoInt64('abcdefgh').toApiRequest();
10615
10715
  * // result = ["7017280452245743464"]
10716
+ * const result2 = new CairoInt64('-5').toApiRequest();
10717
+ * // result2 = ["3618502788666131213697322783095070105623107215331596699973092056135872020476"]
10616
10718
  * ```
10617
10719
  */
10618
10720
  constructor(data) {
@@ -10624,6 +10726,9 @@ ${indent}}` : "}";
10624
10726
  *
10625
10727
  * Nothing here refuses a value : `validate` is what reads this number and decides. So an input
10626
10728
  * far outside the i64 range comes back untouched rather than raising.
10729
+ *
10730
+ * A minus sign in front of a number makes it negative rather than text : `'-5'` and `'-0x5'` are
10731
+ * both -5. This holds for the signed integers only — for any other type, such a string is text.
10627
10732
  * @param {BigNumberish | boolean} data the value to convert
10628
10733
  * @returns {bigint} the number the input spells, of whatever size
10629
10734
  * @example
@@ -10632,9 +10737,14 @@ ${indent}}` : "}";
10632
10737
  * // result = 7017280452245743464n
10633
10738
  * const result2 = CairoInt64.__processData('abcdefghi');
10634
10739
  * // result2 = 1796423795774910326889n (past the i64 range, and returned all the same)
10740
+ * const result3 = CairoInt64.__processData('-0x5');
10741
+ * // result3 = -5n (a negative number, not the text "-0x5")
10635
10742
  * ```
10636
10743
  */
10637
10744
  static __processData(data) {
10745
+ if (isString(data) && data.startsWith("-") && isBigNumberish(data.slice(1))) {
10746
+ return -BigInt(data.slice(1));
10747
+ }
10638
10748
  if (isString(data) && isText(data)) {
10639
10749
  return utf8ToBigInt(data);
10640
10750
  }
@@ -10713,11 +10823,11 @@ ${indent}}` : "}";
10713
10823
  /**
10714
10824
  * Throw unless the value can be carried by an i64.
10715
10825
  *
10716
- * Four things are refused, each with its own message : a null or undefined value, an object or
10717
- * an array, a number with a decimal part, and a value outside [-9223372036854775808, 9223372036854775807]. A text string reaches
10826
+ * Five things are refused, each with its own message : a null or undefined value, an object or
10827
+ * an array, a number with a decimal part, `'0x'`, and a value outside [-9223372036854775808, 9223372036854775807]. A text string reaches
10718
10828
  * that last check as the number its bytes spell, so `'abcdefghi'` is refused for being out of range.
10719
10829
  * @param {BigNumberish | boolean} data the value to check
10720
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
10830
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
10721
10831
  * @example
10722
10832
  * ```typescript
10723
10833
  * CairoInt64.validate(5000000000); // passes
@@ -10732,6 +10842,7 @@ ${indent}}` : "}";
10732
10842
  !isNumber2(data) || Number.isInteger(data),
10733
10843
  "Invalid input: decimal numbers are not supported, only integers"
10734
10844
  );
10845
+ assert(!isEmptyHex(data), "Invalid input: '0x' holds no hexadecimal digit");
10735
10846
  const value = _CairoInt64.__processData(data);
10736
10847
  assert(
10737
10848
  value >= RANGE_I64.min && value <= RANGE_I64.max,
@@ -10822,15 +10933,17 @@ ${indent}}` : "}";
10822
10933
  /**
10823
10934
  * Build from a number, a string or a boolean, refusing anything out of the i128 range.
10824
10935
  *
10825
- * A string is read as a number when it spells one — decimal or hexadecimal — and as UTF-8 text
10826
- * otherwise. Text therefore only fits here up to 16 ASCII characters : one more already
10827
- * makes a number past 170141183460469231731687303715884105727.
10936
+ * A string is read as a number when it spells one — decimal or hexadecimal, with or without a
10937
+ * leading minus sign — and as UTF-8 text otherwise. Text therefore only fits here up to 16 ASCII
10938
+ * characters : one more already makes a number past 170141183460469231731687303715884105727.
10828
10939
  * @param {BigNumberish | boolean} data the value to carry, within [-170141183460469231731687303715884105728, 170141183460469231731687303715884105727]
10829
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
10940
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
10830
10941
  * @example
10831
10942
  * ```typescript
10832
10943
  * const result = new CairoInt128('abcdefghijklmnop').toApiRequest();
10833
10944
  * // result = ["129445976596022050476432668810952994672"]
10945
+ * const result2 = new CairoInt128('-5').toApiRequest();
10946
+ * // result2 = ["3618502788666131213697322783095070105623107215331596699973092056135872020476"]
10834
10947
  * ```
10835
10948
  */
10836
10949
  constructor(data) {
@@ -10842,6 +10955,9 @@ ${indent}}` : "}";
10842
10955
  *
10843
10956
  * Nothing here refuses a value : `validate` is what reads this number and decides. So an input
10844
10957
  * far outside the i128 range comes back untouched rather than raising.
10958
+ *
10959
+ * A minus sign in front of a number makes it negative rather than text : `'-5'` and `'-0x5'` are
10960
+ * both -5. This holds for the signed integers only — for any other type, such a string is text.
10845
10961
  * @param {BigNumberish | boolean} data the value to convert
10846
10962
  * @returns {bigint} the number the input spells, of whatever size
10847
10963
  * @example
@@ -10850,9 +10966,14 @@ ${indent}}` : "}";
10850
10966
  * // result = 129445976596022050476432668810952994672n
10851
10967
  * const result2 = CairoInt128.__processData('abcdefghijklmnopq');
10852
10968
  * // result2 = 33138170008581644921966763215603966636145n (past the i128 range, and returned all the same)
10969
+ * const result3 = CairoInt128.__processData('-0x5');
10970
+ * // result3 = -5n (a negative number, not the text "-0x5")
10853
10971
  * ```
10854
10972
  */
10855
10973
  static __processData(data) {
10974
+ if (isString(data) && data.startsWith("-") && isBigNumberish(data.slice(1))) {
10975
+ return -BigInt(data.slice(1));
10976
+ }
10856
10977
  if (isString(data) && isText(data)) {
10857
10978
  return utf8ToBigInt(data);
10858
10979
  }
@@ -10931,11 +11052,11 @@ ${indent}}` : "}";
10931
11052
  /**
10932
11053
  * Throw unless the value can be carried by an i128.
10933
11054
  *
10934
- * Four things are refused, each with its own message : a null or undefined value, an object or
10935
- * an array, a number with a decimal part, and a value outside [-170141183460469231731687303715884105728, 170141183460469231731687303715884105727]. A text string reaches
11055
+ * Five things are refused, each with its own message : a null or undefined value, an object or
11056
+ * an array, a number with a decimal part, `'0x'`, and a value outside [-170141183460469231731687303715884105728, 170141183460469231731687303715884105727]. A text string reaches
10936
11057
  * that last check as the number its bytes spell, so `'abcdefghijklmnopq'` is refused for being out of range.
10937
11058
  * @param {BigNumberish | boolean} data the value to check
10938
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
11059
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
10939
11060
  * @example
10940
11061
  * ```typescript
10941
11062
  * CairoInt128.validate(5000000000); // passes
@@ -10950,6 +11071,7 @@ ${indent}}` : "}";
10950
11071
  !isNumber2(data) || Number.isInteger(data),
10951
11072
  "Invalid input: decimal numbers are not supported, only integers"
10952
11073
  );
11074
+ assert(!isEmptyHex(data), "Invalid input: '0x' holds no hexadecimal digit");
10953
11075
  const value = _CairoInt128.__processData(data);
10954
11076
  assert(
10955
11077
  value >= RANGE_I128.min && value <= RANGE_I128.max,
@@ -11040,7 +11162,7 @@ ${indent}}` : "}";
11040
11162
  /**
11041
11163
  * Build from a boolean, or from the numbers 0 and 1.
11042
11164
  * @param {BigNumberish | boolean} data the value to carry : a boolean, 0 or 1
11043
- * @throws {Error} when the value is text, is not a felt252 input, or is a number other than 0 or 1
11165
+ * @throws {Error} when the value is text, a decimal number, not a felt252 input, or a number other than 0 or 1
11044
11166
  * @example
11045
11167
  * ```typescript
11046
11168
  * const result = new CairoBool(false).toApiRequest();
@@ -11113,21 +11235,28 @@ ${indent}}` : "}";
11113
11235
  /**
11114
11236
  * Throw unless the value can be carried by a bool.
11115
11237
  *
11116
- * Text is refused first, then the value is read as a felt252 — which is what refuses a null, an
11117
- * object or an unsupported type — and finally checked to be one of the only two numbers a bool
11118
- * can hold.
11238
+ * Text is refused first, then a number with a decimal part. A number or a bigint is read as it
11239
+ * is, so that a negative one reaches the last check. Anything else is read as a felt252 — which
11240
+ * is what refuses a null, an object or an unsupported type. The value is finally checked to be
11241
+ * one of the only two numbers a bool can hold.
11119
11242
  * @param {BigNumberish | boolean} data the value to check
11120
- * @throws {Error} when the value is text, is not a felt252 input, or is a number other than 0 or 1
11243
+ * @throws {Error} when the value is text, a decimal number, not a felt252 input, or a number other than 0 or 1
11121
11244
  * @example
11122
11245
  * ```typescript
11123
11246
  * CairoBool.validate(true); // passes
11124
11247
  * CairoBool.validate(2);
11125
11248
  * // throws Error("Only values 0 or 1 are possible in a core::bool, received 2")
11249
+ * CairoBool.validate(-1);
11250
+ * // throws Error("Only values 0 or 1 are possible in a core::bool, received -1")
11126
11251
  * ```
11127
11252
  */
11128
11253
  static validate(data) {
11129
11254
  assert(!isText(data), "Invalid input: a core::bool cannot be built from text");
11130
- const value = new CairoFelt252(data).toBigInt();
11255
+ assert(
11256
+ !isNumber2(data) || Number.isInteger(data),
11257
+ "Invalid input: decimal numbers are not supported, only integers"
11258
+ );
11259
+ const value = isNumber2(data) || isBigInt(data) ? BigInt(data) : new CairoFelt252(data).toBigInt();
11131
11260
  assert(
11132
11261
  value === 0n || value === 1n,
11133
11262
  `Only values 0 or 1 are possible in a core::bool, received ${value}`
@@ -11242,7 +11371,9 @@ ${indent}}` : "}";
11242
11371
  /**
11243
11372
  * @param {any[]} arr either one value — a 512-bit number or a {@link Secp256k1PointStruct} — or
11244
11373
  * the four limbs
11245
- * @throws {Error} when a value is out of range, or when the argument count is neither 1 nor 4
11374
+ * @throws {Error} when a value is null, undefined, of an unread type, text, a decimal number, or out
11375
+ * of range — see {@link CairoSecp256k1Point.validate} and {@link CairoSecp256k1Point.validateProps}
11376
+ * — or when the argument count is neither 1 nor 4
11246
11377
  * @example
11247
11378
  * ```typescript
11248
11379
  * const result = new CairoSecp256k1Point(1n).toApiRequest();
@@ -11289,22 +11420,32 @@ ${indent}}` : "}";
11289
11420
  * the constructor needs it, and computing it twice would mean splitting a 512-bit value twice.
11290
11421
  * @param {BigNumberish} input the 512-bit value to check
11291
11422
  * @returns {bigint} the value, once checked
11292
- * @throws {Error} when the value is null, undefined, of an unread type, or outside [0, 2^512 - 1]
11423
+ * @throws {Error} when the value is null, undefined, text, `'0x'`, of an unread type, a decimal number, or outside [0, 2^512 - 1]
11293
11424
  * @example
11294
11425
  * ```typescript
11295
11426
  * const result = CairoSecp256k1Point.validate('0x1234');
11296
11427
  * // result = 4660n
11297
11428
  * CairoSecp256k1Point.validate(SECP256K1_POINT_MAX + 1n);
11298
11429
  * // throws Error("input is bigger than SECP256K1_POINT_MAX")
11430
+ * CairoSecp256k1Point.validate('abc');
11431
+ * // throws Error("Invalid input: a Secp256k1Point cannot be built from text")
11432
+ * CairoSecp256k1Point.validate(1.5);
11433
+ * // throws Error("Invalid input: decimal numbers are not supported, only integers")
11299
11434
  * ```
11300
11435
  */
11301
11436
  static validate(input) {
11302
11437
  assert(input !== null, "null value is not allowed for Secp256k1Point");
11303
11438
  assert(input !== void 0, "undefined value is not allowed for Secp256k1Point");
11439
+ assert(!isText(input), "Invalid input: a Secp256k1Point cannot be built from text");
11440
+ assert(!isEmptyHex(input), "Invalid input: '0x' holds no hexadecimal digit");
11304
11441
  assert(
11305
11442
  isBigNumberish(input),
11306
11443
  `Unsupported input for Secp256k1Point. Expected a number, a bigint, or a string spelling one, received '${typeof input}'`
11307
11444
  );
11445
+ assert(
11446
+ !isNumber2(input) || Number.isInteger(input),
11447
+ "Invalid input: decimal numbers are not supported, only integers"
11448
+ );
11308
11449
  const bigInt = BigInt(input);
11309
11450
  assert(bigInt >= SECP256K1_POINT_MIN, "input is smaller than SECP256K1_POINT_MIN");
11310
11451
  assert(bigInt <= SECP256K1_POINT_MAX, "input is bigger than SECP256K1_POINT_MAX");
@@ -11317,20 +11458,27 @@ ${indent}}` : "}";
11317
11458
  * @param {BigNumberish} yLow the low 128 bits of y
11318
11459
  * @param {BigNumberish} yHigh the high 128 bits of y
11319
11460
  * @returns {{xLow: bigint, xHigh: bigint, yLow: bigint, yHigh: bigint}} the four limbs, checked
11320
- * @throws {Error} when a limb is null, undefined, not a number, negative, or wider than 128 bits
11461
+ * @throws {Error} when a limb is null, undefined, text, `'0x'`, not a number, a decimal number, negative, or wider than 128 bits
11321
11462
  * @example
11322
11463
  * ```typescript
11323
11464
  * const result = CairoSecp256k1Point.validateProps(1, 2, 3, 4);
11324
11465
  * // result = { xLow: 1n, xHigh: 2n, yLow: 3n, yHigh: 4n }
11325
11466
  * CairoSecp256k1Point.validateProps(1, 2, 3, 2n ** 128n);
11326
11467
  * // throws Error("yHigh must fit in 128 bits")
11468
+ * CairoSecp256k1Point.validateProps(1.5, 2, 3, 4);
11469
+ * // throws Error("xLow must be an integer")
11470
+ * CairoSecp256k1Point.validateProps('abc', 2, 3, 4);
11471
+ * // throws Error("xLow cannot be built from text")
11327
11472
  * ```
11328
11473
  */
11329
11474
  static validateProps(xLow, xHigh, yLow, yHigh) {
11330
11475
  const validateLimb = (limb, name) => {
11331
11476
  assert(limb !== null, `${name} cannot be null`);
11332
11477
  assert(limb !== void 0, `${name} cannot be undefined`);
11478
+ assert(!isText(limb), `${name} cannot be built from text`);
11479
+ assert(!isEmptyHex(limb), `${name} cannot be '0x', which holds no hexadecimal digit`);
11333
11480
  assert(isBigNumberish(limb), `${name} must be a BigNumberish`);
11481
+ assert(!isNumber2(limb) || Number.isInteger(limb), `${name} must be an integer`);
11334
11482
  const bigInt = BigInt(limb);
11335
11483
  assert(bigInt >= 0n, `${name} must be non-negative`);
11336
11484
  assert(bigInt <= UINT_128_MAX, `${name} must fit in 128 bits`);
@@ -12473,9 +12621,9 @@ ${indent}}` : "}";
12473
12621
  */
12474
12622
  static abiSelector = Literal.ClassHash;
12475
12623
  /**
12476
- * Build from a number or a numeric string, refusing text and anything wider than 252 bits.
12477
- * @param {BigNumberish | boolean} data the hash to carry, within [0, 2^252 - 1]
12478
- * @throws {Error} when the value is text, is not a felt252 input, or is out of the ClassHash range
12624
+ * Build from a number or a numeric string, refusing text and anything outside the ClassHash range.
12625
+ * @param {BigNumberish | boolean} data the hash to carry, within [0, 2^251 - 1]
12626
+ * @throws {Error} when the value is text, a decimal number, not a felt252 input, or out of the ClassHash range
12479
12627
  * @example
12480
12628
  * ```typescript
12481
12629
  * const result = new CairoClassHash('0x1234').toApiRequest();
@@ -12530,21 +12678,34 @@ ${indent}}` : "}";
12530
12678
  /**
12531
12679
  * Throw unless the value can be carried by a ClassHash.
12532
12680
  *
12533
- * Text is refused first, then the value is read as a felt252 — which is what refuses a null, an
12534
- * object or an unsupported type, and what bounds the value. There is no narrower bound to add:
12535
- * a class hash is a hash output, so any field element is one.
12681
+ * Text is refused first, then a number with a decimal part. A number or a bigint is read as it
12682
+ * is, so that a negative one reaches the range check. Anything else is read as a felt252 — which
12683
+ * is what refuses a null, an object or an unsupported type. The value is finally checked against
12684
+ * the range of the Cairo `ClassHash` type, [0, 2^251), which is narrower than the field.
12536
12685
  * @param {BigNumberish | boolean} data the value to check
12537
- * @throws {Error} when the value is text, is not a felt252 input, or is outside the field
12686
+ * @throws {Error} when the value is text, a decimal number, not a felt252 input, or out of the ClassHash range
12538
12687
  * @example
12539
12688
  * ```typescript
12540
12689
  * CairoClassHash.validate('0x1234'); // passes
12541
12690
  * CairoClassHash.validate('abc');
12542
12691
  * // throws Error("Invalid input: a ClassHash cannot be built from text")
12692
+ * CairoClassHash.validate(2n ** 251n);
12693
+ * // throws Error("Value is out of ClassHash range [0, 3618502788666131106986593281521497120414687020801267626233049500247285301247]")
12694
+ * CairoClassHash.validate(-1);
12695
+ * // throws Error("Value is out of ClassHash range [0, 3618502788666131106986593281521497120414687020801267626233049500247285301247]")
12543
12696
  * ```
12544
12697
  */
12545
12698
  static validate(data) {
12546
12699
  assert(!isText(data), "Invalid input: a ClassHash cannot be built from text");
12547
- CairoFelt252.validate(data);
12700
+ assert(
12701
+ !isNumber2(data) || Number.isInteger(data),
12702
+ "Invalid input: decimal numbers are not supported, only integers"
12703
+ );
12704
+ const value = isNumber2(data) || isBigInt(data) ? BigInt(data) : new CairoFelt252(data).toBigInt();
12705
+ assert(
12706
+ value >= RANGE_CLASS_HASH.min && value <= RANGE_CLASS_HASH.max,
12707
+ `Value is out of ClassHash range [${RANGE_CLASS_HASH.min}, ${RANGE_CLASS_HASH.max}]`
12708
+ );
12548
12709
  }
12549
12710
  /**
12550
12711
  * Can this value be carried by a ClassHash?
@@ -12623,7 +12784,7 @@ ${indent}}` : "}";
12623
12784
  /**
12624
12785
  * Build from a number or a numeric string, refusing text and anything wider than 252 bits.
12625
12786
  * @param {BigNumberish | boolean} data the address to carry, within [0, 2^252 - 1]
12626
- * @throws {Error} when the value is text, is not a felt252 input, or is out of range
12787
+ * @throws {Error} when the value is text, a decimal number, not a felt252 input, or out of range
12627
12788
  * @example
12628
12789
  * ```typescript
12629
12790
  * const result = new CairoContractAddress('0x1234').toApiRequest();
@@ -12678,11 +12839,12 @@ ${indent}}` : "}";
12678
12839
  /**
12679
12840
  * Throw unless the value can be carried by a ContractAddress.
12680
12841
  *
12681
- * Text is refused first, then the value is read as a felt252 — which is what refuses a null, an
12682
- * object or an unsupported type — and finally checked against the bound an address has, which
12683
- * is narrower than the field.
12842
+ * Text is refused first, then a number with a decimal part. A number or a bigint is read as it
12843
+ * is, so that a negative one reaches the range check. Anything else is read as a felt252 — which
12844
+ * is what refuses a null, an object or an unsupported type. The value is finally checked against
12845
+ * the bound an address has, which is narrower than the field.
12684
12846
  * @param {BigNumberish | boolean} data the value to check
12685
- * @throws {Error} when the value is text, is not a felt252 input, or is out of range
12847
+ * @throws {Error} when the value is text, a decimal number, not a felt252 input, or out of range
12686
12848
  * @example
12687
12849
  * ```typescript
12688
12850
  * CairoContractAddress.validate('0x1234'); // passes
@@ -12690,11 +12852,17 @@ ${indent}}` : "}";
12690
12852
  * // throws Error("Invalid input: a ContractAddress cannot be built from text")
12691
12853
  * CairoContractAddress.validate(2n ** 251n);
12692
12854
  * // throws Error("Value is out of ContractAddress range [0, 3618502788666131106986593281521497120414687020801267626233049500247285300991]")
12855
+ * CairoContractAddress.validate(-1);
12856
+ * // throws Error("Value is out of ContractAddress range [0, 3618502788666131106986593281521497120414687020801267626233049500247285300991]")
12693
12857
  * ```
12694
12858
  */
12695
12859
  static validate(data) {
12696
12860
  assert(!isText(data), "Invalid input: a ContractAddress cannot be built from text");
12697
- const value = new CairoFelt252(data).toBigInt();
12861
+ assert(
12862
+ !isNumber2(data) || Number.isInteger(data),
12863
+ "Invalid input: decimal numbers are not supported, only integers"
12864
+ );
12865
+ const value = isNumber2(data) || isBigInt(data) ? BigInt(data) : new CairoFelt252(data).toBigInt();
12698
12866
  assert(
12699
12867
  value >= RANGE_CONTRACT_ADDRESS.min && value <= RANGE_CONTRACT_ADDRESS.max,
12700
12868
  `Value is out of ContractAddress range [${RANGE_CONTRACT_ADDRESS.min}, ${RANGE_CONTRACT_ADDRESS.max}]`
@@ -12779,7 +12947,7 @@ ${indent}}` : "}";
12779
12947
  /**
12780
12948
  * Build from a number or a numeric string, refusing text and anything wider than 160 bits.
12781
12949
  * @param {BigNumberish | boolean} data the address to carry, within [0, 2^160 - 1]
12782
- * @throws {Error} when the value is text, is not a felt252 input, or is out of the EthAddress range
12950
+ * @throws {Error} when the value is text, a decimal number, not a felt252 input, or out of the EthAddress range
12783
12951
  * @example
12784
12952
  * ```typescript
12785
12953
  * const result = new CairoEthAddress('0x1234').toApiRequest();
@@ -12835,10 +13003,12 @@ ${indent}}` : "}";
12835
13003
  * Throw unless the value can be carried by an EthAddress.
12836
13004
  *
12837
13005
  * Text is refused first, since an address spelled as words is a mistake rather than a value to
12838
- * encode. What remains is read as a felt252 — which is what refuses a null, an object or an
12839
- * unsupported type — then checked against the 160 bits an Ethereum address occupies.
13006
+ * encode, then a number with a decimal part. A number or a bigint is read as it is, so that a
13007
+ * negative one reaches the range check below. Anything else is read as a felt252 — which is what
13008
+ * refuses a null, an object or an unsupported type. The value is finally checked against the 160
13009
+ * bits an Ethereum address occupies.
12840
13010
  * @param {BigNumberish | boolean} data the value to check
12841
- * @throws {Error} when the value is text, is not a felt252 input, or is out of the EthAddress range
13011
+ * @throws {Error} when the value is text, a decimal number, not a felt252 input, or out of the EthAddress range
12842
13012
  * @example
12843
13013
  * ```typescript
12844
13014
  * CairoEthAddress.validate('0x1234'); // passes
@@ -12846,11 +13016,17 @@ ${indent}}` : "}";
12846
13016
  * // throws Error("Invalid input: an EthAddress cannot be built from text")
12847
13017
  * CairoEthAddress.validate(2n ** 160n);
12848
13018
  * // throws Error("Value is out of EthAddress range [0, 1461501637330902918203684832716283019655932542975]")
13019
+ * CairoEthAddress.validate(-1);
13020
+ * // throws Error("Value is out of EthAddress range [0, 1461501637330902918203684832716283019655932542975]")
12849
13021
  * ```
12850
13022
  */
12851
13023
  static validate(data) {
12852
13024
  assert(!isText(data), "Invalid input: an EthAddress cannot be built from text");
12853
- const value = new CairoFelt252(data).toBigInt();
13025
+ assert(
13026
+ !isNumber2(data) || Number.isInteger(data),
13027
+ "Invalid input: decimal numbers are not supported, only integers"
13028
+ );
13029
+ const value = isNumber2(data) || isBigInt(data) ? BigInt(data) : new CairoFelt252(data).toBigInt();
12854
13030
  assert(
12855
13031
  value >= RANGE_ETH_ADDRESS.min && value <= RANGE_ETH_ADDRESS.max,
12856
13032
  `Value is out of EthAddress range [${RANGE_ETH_ADDRESS.min}, ${RANGE_ETH_ADDRESS.max}]`
@@ -19097,7 +19273,7 @@ ${indent}}` : "}";
19097
19273
  return sanitizeHex(buf2hex(secp256k1.utils.randomSecretKey()));
19098
19274
  }
19099
19275
  function validateAndParseEthAddress(address) {
19100
- assertInRange(address, ZERO, 2n ** 160n - 1n, "Ethereum Address ");
19276
+ assertInRange(address, ZERO, 2n ** 160n - 1n, "Ethereum Address");
19101
19277
  const result = addHexPrefix(removeHexPrefix(toHex(address)).padStart(40, "0"));
19102
19278
  assert(Boolean(result.match(/^(0x)?[0-9a-f]{40}$/)), "Invalid Ethereum Address Format");
19103
19279
  return result;
@@ -23329,6 +23505,9 @@ ${indent}}` : "}";
23329
23505
  return addHexPrefix(padded);
23330
23506
  }
23331
23507
  function validateAndParseAddress(address) {
23508
+ if (isString(address) && !/^(0x)?[0-9a-f]+$/i.test(address)) {
23509
+ throw new Error("Invalid Address Format");
23510
+ }
23332
23511
  const result = addAddressPadding(address);
23333
23512
  if (!result.match(/^(0x)?[0-9a-fA-F]{64}$/)) {
23334
23513
  throw new Error("Invalid Address Format");