starknet 11.0.1 → 11.0.3

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/dist/index.mjs CHANGED
@@ -39,6 +39,7 @@ __export(constants_exports, {
39
39
  OutsideExecutionCallerAny: () => OutsideExecutionCallerAny,
40
40
  PAYMASTER_RPC_NODES: () => PAYMASTER_RPC_NODES,
41
41
  PRIME: () => PRIME,
42
+ RANGE_CLASS_HASH: () => RANGE_CLASS_HASH,
42
43
  RANGE_CONTRACT_ADDRESS: () => RANGE_CONTRACT_ADDRESS,
43
44
  RANGE_ETH_ADDRESS: () => RANGE_ETH_ADDRESS,
44
45
  RANGE_FELT: () => RANGE_FELT,
@@ -278,6 +279,7 @@ var RANGE_I64 = range(-(2n ** 63n), 2n ** 63n - 1n);
278
279
  var RANGE_I128 = range(-(2n ** 127n), 2n ** 127n - 1n);
279
280
  var RANGE_ETH_ADDRESS = range(ZERO, 2n ** 160n - 1n);
280
281
  var RANGE_CONTRACT_ADDRESS = range(ZERO, ADDR_BOUND - 1n);
282
+ var RANGE_CLASS_HASH = range(ZERO, 2n ** 251n - 1n);
281
283
  var LegacyUDC = {
282
284
  ADDRESS: "0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf",
283
285
  ENTRYPOINT: "deployContract"
@@ -755,6 +757,7 @@ __export(num_exports, {
755
757
  hexToBytes: () => hexToBytes,
756
758
  hexToDecimalString: () => hexToDecimalString,
757
759
  isBigNumberish: () => isBigNumberish,
760
+ isEmptyHex: () => isEmptyHex,
758
761
  isHex: () => isHex,
759
762
  isHexString: () => isHexString2,
760
763
  isStringWholeNumber: () => isStringWholeNumber,
@@ -810,6 +813,7 @@ function isHex(hex) {
810
813
  }
811
814
  var isHexString2 = isHex;
812
815
  function toBigInt(value) {
816
+ assert(!isEmptyHex(value), "Invalid input: '0x' holds no hexadecimal digit");
813
817
  return BigInt(value);
814
818
  }
815
819
  function tryToBigInt(value) {
@@ -834,13 +838,13 @@ function hexToDecimalString(hex) {
834
838
  return BigInt(addHexPrefix(hex)).toString(10);
835
839
  }
836
840
  function assertInRange(input, lowerBound, upperBound, inputName = "") {
837
- const messageSuffix = inputName === "" ? "invalid length" : `invalid ${inputName} length`;
838
841
  const inputBigInt = BigInt(input);
839
842
  const lowerBoundBigInt = BigInt(lowerBound);
840
843
  const upperBoundBigInt = BigInt(upperBound);
844
+ const rangeName = inputName === "" ? "range" : `${inputName} range`;
841
845
  assert(
842
846
  inputBigInt >= lowerBoundBigInt && inputBigInt <= upperBoundBigInt,
843
- `Message not signable, ${messageSuffix}.`
847
+ `Value is out of ${rangeName} [${lowerBoundBigInt}, ${upperBoundBigInt}]`
844
848
  );
845
849
  }
846
850
  function bigNumberishArrayToDecimalStringArray(data) {
@@ -893,8 +897,11 @@ function stringToSha256ToArrayBuff4(str) {
893
897
  const result = int31(BigInt(addHexPrefix(buf2hex(sha256(utf8ToBytes(str))))));
894
898
  return hexToBytes(toHex(result));
895
899
  }
900
+ function isEmptyHex(value) {
901
+ return isString(value) && /^0x$/i.test(value);
902
+ }
896
903
  function isBigNumberish(input) {
897
- return isNumber(input) || isBigInt(input) || isString(input) && (isHex(input) || isStringWholeNumber(input));
904
+ return isNumber(input) || isBigInt(input) || isString(input) && (isHex(input) && !isEmptyHex(input) || isStringWholeNumber(input));
898
905
  }
899
906
  function getNext(iterator) {
900
907
  const it = iterator.next();
@@ -1478,16 +1485,18 @@ var CairoFelt252 = class _CairoFelt252 {
1478
1485
  /**
1479
1486
  * Throw unless the value can be carried by a felt252.
1480
1487
  *
1481
- * Null, undefined and any type this class does not read are refused first, each with its own
1482
- * message, then the value is converted and its range checked. A negative number does not reach
1483
- * that range check : converting it fails earlier, and the message comes from the conversion.
1488
+ * Null, undefined, any type this class does not read, and `'0x'` are refused first, each with its
1489
+ * own message, then the value is converted and its range checked. A negative number does not
1490
+ * reach that range check : converting it fails earlier, and the message comes from the conversion.
1484
1491
  * @param {BigNumberish | boolean} data the value to check
1485
- * @throws {Error} when the value is null, undefined, of an unread type, or outside [0, P)
1492
+ * @throws {Error} when the value is null, undefined, of an unread type, `'0x'`, or outside [0, P)
1486
1493
  * @example
1487
1494
  * ```typescript
1488
1495
  * CairoFelt252.validate('Hello'); // passes
1489
1496
  * CairoFelt252.validate({});
1490
1497
  * // throws Error("Unsupported data type 'object' for felt252. Expected string, number, bigint, or boolean")
1498
+ * CairoFelt252.validate('0x');
1499
+ * // throws Error("Invalid input: '0x' holds no hexadecimal digit")
1491
1500
  * CairoFelt252.validate(-1);
1492
1501
  * // throws Error("Cannot convert negative bigint -1 to Uint8Array")
1493
1502
  * ```
@@ -1499,6 +1508,7 @@ var CairoFelt252 = class _CairoFelt252 {
1499
1508
  isString(data) || isNumber(data) || isBigInt(data) || isBoolean(data),
1500
1509
  `Unsupported data type '${typeof data}' for felt252. Expected string, number, bigint, or boolean`
1501
1510
  );
1511
+ assert(!isEmptyHex(data), "Invalid input: '0x' holds no hexadecimal digit");
1502
1512
  const value = _CairoFelt252.__processData(data);
1503
1513
  const bn = uint8ArrayToBigInt(value);
1504
1514
  _CairoFelt252.assertRange(bn);
@@ -2315,7 +2325,7 @@ var CairoUint32 = class _CairoUint32 {
2315
2325
  * otherwise. Text therefore only fits here up to 4 ASCII characters : one more already
2316
2326
  * makes a number past 4294967295.
2317
2327
  * @param {BigNumberish | boolean} data the value to carry, within [0, 4294967295]
2318
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
2328
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
2319
2329
  * @example
2320
2330
  * ```typescript
2321
2331
  * const result = new CairoUint32('abcd').toApiRequest();
@@ -2403,11 +2413,11 @@ var CairoUint32 = class _CairoUint32 {
2403
2413
  /**
2404
2414
  * Throw unless the value can be carried by a u32.
2405
2415
  *
2406
- * Four things are refused, each with its own message : a null or undefined value, an object or
2407
- * an array, a number with a decimal part, and a value outside [0, 4294967295]. A text string reaches
2416
+ * Five things are refused, each with its own message : a null or undefined value, an object or
2417
+ * an array, a number with a decimal part, `'0x'`, and a value outside [0, 4294967295]. A text string reaches
2408
2418
  * that last check as the number its bytes spell, so `'abcde'` is refused for being out of range.
2409
2419
  * @param {BigNumberish | boolean} data the value to check
2410
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
2420
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
2411
2421
  * @example
2412
2422
  * ```typescript
2413
2423
  * CairoUint32.validate(70000); // passes
@@ -2422,6 +2432,7 @@ var CairoUint32 = class _CairoUint32 {
2422
2432
  !isNumber(data) || Number.isInteger(data),
2423
2433
  "Invalid input: decimal numbers are not supported, only integers"
2424
2434
  );
2435
+ assert(!isEmptyHex(data), "Invalid input: '0x' holds no hexadecimal digit");
2425
2436
  const value = _CairoUint32.__processData(data);
2426
2437
  assert(
2427
2438
  value >= RANGE_U32.min && value <= RANGE_U32.max,
@@ -3009,7 +3020,6 @@ var CairoUint256 = class _CairoUint256 {
3009
3020
  * ```
3010
3021
  */
3011
3022
  low;
3012
- // TODO should be u128
3013
3023
  /**
3014
3024
  * The top 128 bits of the value.
3015
3025
  * @example
@@ -3019,7 +3029,6 @@ var CairoUint256 = class _CairoUint256 {
3019
3029
  * ```
3020
3030
  */
3021
3031
  high;
3022
- // TODO should be u128
3023
3032
  /**
3024
3033
  * The abi type this class serializes.
3025
3034
  * @example
@@ -3053,25 +3062,37 @@ var CairoUint256 = class _CairoUint256 {
3053
3062
  * Throw unless a whole value can be represented as a u256, and give back its number.
3054
3063
  *
3055
3064
  * A string is only accepted while it spells a number, in base 10 or 16 : one that does not is
3056
- * refused for its type.
3065
+ * refused as text, and `'0x'`, which holds no digit, is refused too. So is an object, a `Uint256`
3066
+ * one included : its two halves are checked by {@link CairoUint256.validateProps}, which is what
3067
+ * the constructor calls for it.
3057
3068
  * @param {BigNumberish} bigNumberish the value to check
3058
3069
  * @returns {bigint} the value as a number, once accepted
3059
- * @throws {Error} when the value is null, undefined, of an unread type, or out of range
3070
+ * @throws {Error} when the value is null, undefined, text, `'0x'`, of an unread type, a decimal number, or out of range
3060
3071
  * @example
3061
3072
  * ```typescript
3062
3073
  * const result = CairoUint256.validate(255);
3063
3074
  * // result = 255n
3064
3075
  * CairoUint256.validate(-1);
3065
3076
  * // throws Error("bigNumberish is smaller than UINT_256_MIN")
3077
+ * CairoUint256.validate('abc');
3078
+ * // throws Error("Invalid input: a u256 cannot be built from text")
3079
+ * CairoUint256.validate(1.5);
3080
+ * // throws Error("Invalid input: decimal numbers are not supported, only integers")
3066
3081
  * ```
3067
3082
  */
3068
3083
  static validate(bigNumberish) {
3069
3084
  assert(bigNumberish !== null, "null value is not allowed for u256");
3070
3085
  assert(bigNumberish !== void 0, "undefined value is not allowed for u256");
3086
+ assert(!isText(bigNumberish), "Invalid input: a u256 cannot be built from text");
3087
+ assert(!isEmptyHex(bigNumberish), "Invalid input: '0x' holds no hexadecimal digit");
3071
3088
  assert(
3072
- isBigNumberish(bigNumberish) || isObject(bigNumberish),
3089
+ isBigNumberish(bigNumberish),
3073
3090
  `Unsupported data type '${typeof bigNumberish}' for u256. Expected a numeric string (decimal or hexadecimal), number, bigint, or Uint256 object`
3074
3091
  );
3092
+ assert(
3093
+ !isNumber(bigNumberish) || Number.isInteger(bigNumberish),
3094
+ "Invalid input: decimal numbers are not supported, only integers"
3095
+ );
3075
3096
  const bigInt = BigInt(bigNumberish);
3076
3097
  assert(bigInt >= UINT_256_MIN, "bigNumberish is smaller than UINT_256_MIN");
3077
3098
  assert(bigInt <= UINT_256_MAX, "bigNumberish is bigger than UINT_256_MAX");
@@ -3085,18 +3106,31 @@ var CairoUint256 = class _CairoUint256 {
3085
3106
  * @param {BigNumberish} low the bottom 128 bits
3086
3107
  * @param {BigNumberish} high the top 128 bits
3087
3108
  * @returns {{low: bigint, high: bigint}} the two halves as numbers
3088
- * @throws {Error} when either half is outside the u128 range
3109
+ * @throws {Error} when either half is null, undefined, text, `'0x'`, not a number, a decimal number, or outside the u128 range
3089
3110
  * @example
3090
3111
  * ```typescript
3091
3112
  * const result = CairoUint256.validateProps(5, 4);
3092
3113
  * // result = { low: 5n, high: 4n }
3093
3114
  * CairoUint256.validateProps(-1, 4);
3094
3115
  * // throws Error("low is out of range UINT_256_LOW_MIN - UINT_256_LOW_MAX")
3116
+ * CairoUint256.validateProps(1.5, 4);
3117
+ * // throws Error("low must be an integer")
3118
+ * CairoUint256.validateProps('abc', 4);
3119
+ * // throws Error("low cannot be built from text")
3095
3120
  * ```
3096
3121
  */
3097
3122
  static validateProps(low, high) {
3098
- const bigIntLow = BigInt(low);
3099
- const bigIntHigh = BigInt(high);
3123
+ const validateHalf = (half, name) => {
3124
+ assert(half !== null, `${name} cannot be null`);
3125
+ assert(half !== void 0, `${name} cannot be undefined`);
3126
+ assert(!isText(half), `${name} cannot be built from text`);
3127
+ assert(!isEmptyHex(half), `${name} cannot be '0x', which holds no hexadecimal digit`);
3128
+ assert(isBigNumberish(half), `${name} must be a BigNumberish`);
3129
+ assert(!isNumber(half) || Number.isInteger(half), `${name} must be an integer`);
3130
+ return BigInt(half);
3131
+ };
3132
+ const bigIntLow = validateHalf(low, "low");
3133
+ const bigIntHigh = validateHalf(high, "high");
3100
3134
  assert(
3101
3135
  bigIntLow >= UINT_256_LOW_MIN && bigIntLow <= UINT_256_LOW_MAX,
3102
3136
  "low is out of range UINT_256_LOW_MIN - UINT_256_LOW_MAX"
@@ -3233,7 +3267,6 @@ var CairoUint512 = class _CairoUint512 {
3233
3267
  * ```
3234
3268
  */
3235
3269
  limb0;
3236
- // TODO should be u128
3237
3270
  /**
3238
3271
  * Bits 128 to 255 of the value.
3239
3272
  * @example
@@ -3243,7 +3276,6 @@ var CairoUint512 = class _CairoUint512 {
3243
3276
  * ```
3244
3277
  */
3245
3278
  limb1;
3246
- // TODO should be u128
3247
3279
  /**
3248
3280
  * Bits 256 to 383 of the value.
3249
3281
  * @example
@@ -3253,7 +3285,6 @@ var CairoUint512 = class _CairoUint512 {
3253
3285
  * ```
3254
3286
  */
3255
3287
  limb2;
3256
- // TODO should be u128
3257
3288
  /**
3258
3289
  * Bits 384 to 511 of the value.
3259
3290
  * @example
@@ -3263,7 +3294,6 @@ var CairoUint512 = class _CairoUint512 {
3263
3294
  * ```
3264
3295
  */
3265
3296
  limb3;
3266
- // TODO should be u128
3267
3297
  /**
3268
3298
  * The abi type this class serializes.
3269
3299
  * @example
@@ -3305,25 +3335,37 @@ var CairoUint512 = class _CairoUint512 {
3305
3335
  * Throw unless a whole value can be represented as a u512, and give back its number.
3306
3336
  *
3307
3337
  * A string is only accepted while it spells a number, in base 10 or 16 : one that does not is
3308
- * refused for its type.
3338
+ * refused as text, and `'0x'`, which holds no digit, is refused too. So is an object, a `Uint512`
3339
+ * one included : its four limbs are checked by {@link CairoUint512.validateProps}, which is what
3340
+ * the constructor calls for it.
3309
3341
  * @param {BigNumberish} bigNumberish the value to check
3310
3342
  * @returns {bigint} the value as a number, once accepted
3311
- * @throws {Error} when the value is null, undefined, of an unread type, or out of range
3343
+ * @throws {Error} when the value is null, undefined, text, `'0x'`, of an unread type, a decimal number, or out of range
3312
3344
  * @example
3313
3345
  * ```typescript
3314
3346
  * const result = CairoUint512.validate(255);
3315
3347
  * // result = 255n
3316
3348
  * CairoUint512.validate(-1);
3317
3349
  * // throws Error("bigNumberish is smaller than UINT_512_MIN.")
3350
+ * CairoUint512.validate('abc');
3351
+ * // throws Error("Invalid input: a u512 cannot be built from text")
3352
+ * CairoUint512.validate(1.5);
3353
+ * // throws Error("Invalid input: decimal numbers are not supported, only integers")
3318
3354
  * ```
3319
3355
  */
3320
3356
  static validate(bigNumberish) {
3321
3357
  assert(bigNumberish !== null, "null value is not allowed for u512");
3322
3358
  assert(bigNumberish !== void 0, "undefined value is not allowed for u512");
3359
+ assert(!isText(bigNumberish), "Invalid input: a u512 cannot be built from text");
3360
+ assert(!isEmptyHex(bigNumberish), "Invalid input: '0x' holds no hexadecimal digit");
3323
3361
  assert(
3324
- isBigNumberish(bigNumberish) || isObject(bigNumberish),
3362
+ isBigNumberish(bigNumberish),
3325
3363
  `Unsupported data type '${typeof bigNumberish}' for u512. Expected a numeric string (decimal or hexadecimal), number, bigint, or Uint512 object`
3326
3364
  );
3365
+ assert(
3366
+ !isNumber(bigNumberish) || Number.isInteger(bigNumberish),
3367
+ "Invalid input: decimal numbers are not supported, only integers"
3368
+ );
3327
3369
  const bigInt = BigInt(bigNumberish);
3328
3370
  assert(bigInt >= UINT_512_MIN, "bigNumberish is smaller than UINT_512_MIN.");
3329
3371
  assert(bigInt <= UINT_512_MAX, "bigNumberish is bigger than UINT_512_MAX.");
@@ -3339,20 +3381,33 @@ var CairoUint512 = class _CairoUint512 {
3339
3381
  * @param {BigNumberish} limb2 bits 256 to 383
3340
3382
  * @param {BigNumberish} limb3 bits 384 to 511
3341
3383
  * @returns {{limb0: bigint, limb1: bigint, limb2: bigint, limb3: bigint}} the four limbs as numbers
3342
- * @throws {Error} when any limb is outside the u128 range
3384
+ * @throws {Error} when any limb is null, undefined, text, `'0x'`, not a number, a decimal number, or outside the u128 range
3343
3385
  * @example
3344
3386
  * ```typescript
3345
3387
  * const result = CairoUint512.validateProps(7, 0, 0, 64);
3346
3388
  * // result = { limb0: 7n, limb1: 0n, limb2: 0n, limb3: 64n }
3347
3389
  * CairoUint512.validateProps(-1, 0, 0, 0);
3348
3390
  * // throws Error("limb0 is not in the range of a u128 number")
3391
+ * CairoUint512.validateProps(1.5, 0, 0, 0);
3392
+ * // throws Error("limb0 must be an integer")
3393
+ * CairoUint512.validateProps('abc', 0, 0, 0);
3394
+ * // throws Error("limb0 cannot be built from text")
3349
3395
  * ```
3350
3396
  */
3351
3397
  static validateProps(limb0, limb1, limb2, limb3) {
3352
- const l0 = BigInt(limb0);
3353
- const l1 = BigInt(limb1);
3354
- const l2 = BigInt(limb2);
3355
- const l3 = BigInt(limb3);
3398
+ const validateLimb = (limb, name) => {
3399
+ assert(limb !== null, `${name} cannot be null`);
3400
+ assert(limb !== void 0, `${name} cannot be undefined`);
3401
+ assert(!isText(limb), `${name} cannot be built from text`);
3402
+ assert(!isEmptyHex(limb), `${name} cannot be '0x', which holds no hexadecimal digit`);
3403
+ assert(isBigNumberish(limb), `${name} must be a BigNumberish`);
3404
+ assert(!isNumber(limb) || Number.isInteger(limb), `${name} must be an integer`);
3405
+ return BigInt(limb);
3406
+ };
3407
+ const l0 = validateLimb(limb0, "limb0");
3408
+ const l1 = validateLimb(limb1, "limb1");
3409
+ const l2 = validateLimb(limb2, "limb2");
3410
+ const l3 = validateLimb(limb3, "limb3");
3356
3411
  [l0, l1, l2, l3].forEach((value, index) => {
3357
3412
  assert(
3358
3413
  value >= UINT_128_MIN && value <= UINT_128_MAX,
@@ -3524,7 +3579,7 @@ var CairoUint96 = class _CairoUint96 {
3524
3579
  * otherwise. Text therefore only fits here up to 12 ASCII characters : one more already
3525
3580
  * makes a number past 79228162514264337593543950335.
3526
3581
  * @param {BigNumberish | boolean} data the value to carry, within [0, 79228162514264337593543950335]
3527
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
3582
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
3528
3583
  * @example
3529
3584
  * ```typescript
3530
3585
  * const result = new CairoUint96('abcdefghijkl').toApiRequest();
@@ -3612,11 +3667,11 @@ var CairoUint96 = class _CairoUint96 {
3612
3667
  /**
3613
3668
  * Throw unless the value can be carried by a u96.
3614
3669
  *
3615
- * Four things are refused, each with its own message : a null or undefined value, an object or
3616
- * an array, a number with a decimal part, and a value outside [0, 79228162514264337593543950335]. A text string reaches
3670
+ * Five things are refused, each with its own message : a null or undefined value, an object or
3671
+ * an array, a number with a decimal part, `'0x'`, and a value outside [0, 79228162514264337593543950335]. A text string reaches
3617
3672
  * that last check as the number its bytes spell, so `'abcdefghijklm'` is refused for being out of range.
3618
3673
  * @param {BigNumberish | boolean} data the value to check
3619
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
3674
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
3620
3675
  * @example
3621
3676
  * ```typescript
3622
3677
  * CairoUint96.validate(5000000000); // passes
@@ -3631,6 +3686,7 @@ var CairoUint96 = class _CairoUint96 {
3631
3686
  !isNumber(data) || Number.isInteger(data),
3632
3687
  "Invalid input: decimal numbers are not supported, only integers"
3633
3688
  );
3689
+ assert(!isEmptyHex(data), "Invalid input: '0x' holds no hexadecimal digit");
3634
3690
  const value = _CairoUint96.__processData(data);
3635
3691
  assert(
3636
3692
  value >= RANGE_U96.min && value <= RANGE_U96.max,
@@ -4017,7 +4073,7 @@ var CairoUint8 = class _CairoUint8 {
4017
4073
  * otherwise. Text therefore only fits here while it is a single ASCII character : two characters
4018
4074
  * already make a number past 255.
4019
4075
  * @param {BigNumberish | boolean} data the value to carry, within [0, 255]
4020
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
4076
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
4021
4077
  * @example
4022
4078
  * ```typescript
4023
4079
  * const result = new CairoUint8('a').toApiRequest();
@@ -4107,11 +4163,11 @@ var CairoUint8 = class _CairoUint8 {
4107
4163
  /**
4108
4164
  * Throw unless the value can be carried by a u8.
4109
4165
  *
4110
- * Four things are refused, each with its own message : a null or undefined value, an object or
4111
- * an array, a number with a decimal part, and a value outside [0, 255]. A text string reaches
4166
+ * Five things are refused, each with its own message : a null or undefined value, an object or
4167
+ * an array, a number with a decimal part, `'0x'`, and a value outside [0, 255]. A text string reaches
4112
4168
  * that last check as the number its bytes spell, so `'ab'` is refused for being out of range.
4113
4169
  * @param {BigNumberish | boolean} data the value to check
4114
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
4170
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
4115
4171
  * @example
4116
4172
  * ```typescript
4117
4173
  * CairoUint8.validate(44); // passes
@@ -4126,6 +4182,7 @@ var CairoUint8 = class _CairoUint8 {
4126
4182
  !isNumber(data) || Number.isInteger(data),
4127
4183
  "Invalid input: decimal numbers are not supported, only integers"
4128
4184
  );
4185
+ assert(!isEmptyHex(data), "Invalid input: '0x' holds no hexadecimal digit");
4129
4186
  const value = _CairoUint8.__processData(data);
4130
4187
  assert(
4131
4188
  value >= RANGE_U8.min && value <= RANGE_U8.max,
@@ -4216,7 +4273,7 @@ var CairoUint16 = class _CairoUint16 {
4216
4273
  * otherwise. Text therefore only fits here up to 2 ASCII characters : one more already
4217
4274
  * makes a number past 65535.
4218
4275
  * @param {BigNumberish | boolean} data the value to carry, within [0, 65535]
4219
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
4276
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
4220
4277
  * @example
4221
4278
  * ```typescript
4222
4279
  * const result = new CairoUint16('ab').toApiRequest();
@@ -4304,11 +4361,11 @@ var CairoUint16 = class _CairoUint16 {
4304
4361
  /**
4305
4362
  * Throw unless the value can be carried by a u16.
4306
4363
  *
4307
- * Four things are refused, each with its own message : a null or undefined value, an object or
4308
- * an array, a number with a decimal part, and a value outside [0, 65535]. A text string reaches
4364
+ * Five things are refused, each with its own message : a null or undefined value, an object or
4365
+ * an array, a number with a decimal part, `'0x'`, and a value outside [0, 65535]. A text string reaches
4309
4366
  * that last check as the number its bytes spell, so `'abc'` is refused for being out of range.
4310
4367
  * @param {BigNumberish | boolean} data the value to check
4311
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
4368
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
4312
4369
  * @example
4313
4370
  * ```typescript
4314
4371
  * CairoUint16.validate(300); // passes
@@ -4323,6 +4380,7 @@ var CairoUint16 = class _CairoUint16 {
4323
4380
  !isNumber(data) || Number.isInteger(data),
4324
4381
  "Invalid input: decimal numbers are not supported, only integers"
4325
4382
  );
4383
+ assert(!isEmptyHex(data), "Invalid input: '0x' holds no hexadecimal digit");
4326
4384
  const value = _CairoUint16.__processData(data);
4327
4385
  assert(
4328
4386
  value >= RANGE_U16.min && value <= RANGE_U16.max,
@@ -4413,7 +4471,7 @@ var CairoUint64 = class _CairoUint64 {
4413
4471
  * otherwise. Text therefore only fits here up to 8 ASCII characters : one more already
4414
4472
  * makes a number past 18446744073709551615.
4415
4473
  * @param {BigNumberish | boolean} data the value to carry, within [0, 18446744073709551615]
4416
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
4474
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
4417
4475
  * @example
4418
4476
  * ```typescript
4419
4477
  * const result = new CairoUint64('abcdefgh').toApiRequest();
@@ -4501,11 +4559,11 @@ var CairoUint64 = class _CairoUint64 {
4501
4559
  /**
4502
4560
  * Throw unless the value can be carried by a u64.
4503
4561
  *
4504
- * Four things are refused, each with its own message : a null or undefined value, an object or
4505
- * an array, a number with a decimal part, and a value outside [0, 18446744073709551615]. A text string reaches
4562
+ * Five things are refused, each with its own message : a null or undefined value, an object or
4563
+ * an array, a number with a decimal part, `'0x'`, and a value outside [0, 18446744073709551615]. A text string reaches
4506
4564
  * that last check as the number its bytes spell, so `'abcdefghi'` is refused for being out of range.
4507
4565
  * @param {BigNumberish | boolean} data the value to check
4508
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
4566
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
4509
4567
  * @example
4510
4568
  * ```typescript
4511
4569
  * CairoUint64.validate(5000000000); // passes
@@ -4520,6 +4578,7 @@ var CairoUint64 = class _CairoUint64 {
4520
4578
  !isNumber(data) || Number.isInteger(data),
4521
4579
  "Invalid input: decimal numbers are not supported, only integers"
4522
4580
  );
4581
+ assert(!isEmptyHex(data), "Invalid input: '0x' holds no hexadecimal digit");
4523
4582
  const value = _CairoUint64.__processData(data);
4524
4583
  assert(
4525
4584
  value >= RANGE_U64.min && value <= RANGE_U64.max,
@@ -4610,7 +4669,7 @@ var CairoUint128 = class _CairoUint128 {
4610
4669
  * otherwise. Text therefore only fits here up to 16 ASCII characters : one more already
4611
4670
  * makes a number past 340282366920938463463374607431768211455.
4612
4671
  * @param {BigNumberish | boolean} data the value to carry, within [0, 340282366920938463463374607431768211455]
4613
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
4672
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
4614
4673
  * @example
4615
4674
  * ```typescript
4616
4675
  * const result = new CairoUint128('abcdefghijklmnop').toApiRequest();
@@ -4698,11 +4757,11 @@ var CairoUint128 = class _CairoUint128 {
4698
4757
  /**
4699
4758
  * Throw unless the value can be carried by a u128.
4700
4759
  *
4701
- * Four things are refused, each with its own message : a null or undefined value, an object or
4702
- * an array, a number with a decimal part, and a value outside [0, 340282366920938463463374607431768211455]. A text string reaches
4760
+ * Five things are refused, each with its own message : a null or undefined value, an object or
4761
+ * an array, a number with a decimal part, `'0x'`, and a value outside [0, 340282366920938463463374607431768211455]. A text string reaches
4703
4762
  * that last check as the number its bytes spell, so `'abcdefghijklmnopq'` is refused for being out of range.
4704
4763
  * @param {BigNumberish | boolean} data the value to check
4705
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
4764
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
4706
4765
  * @example
4707
4766
  * ```typescript
4708
4767
  * CairoUint128.validate(5000000000); // passes
@@ -4717,6 +4776,7 @@ var CairoUint128 = class _CairoUint128 {
4717
4776
  !isNumber(data) || Number.isInteger(data),
4718
4777
  "Invalid input: decimal numbers are not supported, only integers"
4719
4778
  );
4779
+ assert(!isEmptyHex(data), "Invalid input: '0x' holds no hexadecimal digit");
4720
4780
  const value = _CairoUint128.__processData(data);
4721
4781
  assert(
4722
4782
  value >= RANGE_U128.min && value <= RANGE_U128.max,
@@ -4803,15 +4863,17 @@ var CairoInt8 = class _CairoInt8 {
4803
4863
  /**
4804
4864
  * Build from a number, a string or a boolean, refusing anything out of the i8 range.
4805
4865
  *
4806
- * A string is read as a number when it spells one — decimal or hexadecimal — and as UTF-8 text
4807
- * otherwise. Text therefore only fits here up to a single ASCII character : one more already
4808
- * makes a number past 127.
4866
+ * A string is read as a number when it spells one — decimal or hexadecimal, with or without a
4867
+ * leading minus sign — and as UTF-8 text otherwise. Text therefore only fits here up to a single
4868
+ * ASCII character : one more already makes a number past 127.
4809
4869
  * @param {BigNumberish | boolean} data the value to carry, within [-128, 127]
4810
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
4870
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
4811
4871
  * @example
4812
4872
  * ```typescript
4813
4873
  * const result = new CairoInt8('a').toApiRequest();
4814
4874
  * // result = ["97"]
4875
+ * const result2 = new CairoInt8('-5').toApiRequest();
4876
+ * // result2 = ["3618502788666131213697322783095070105623107215331596699973092056135872020476"]
4815
4877
  * ```
4816
4878
  */
4817
4879
  constructor(data) {
@@ -4823,6 +4885,9 @@ var CairoInt8 = class _CairoInt8 {
4823
4885
  *
4824
4886
  * Nothing here refuses a value : `validate` is what reads this number and decides. So an input
4825
4887
  * far outside the i8 range comes back untouched rather than raising.
4888
+ *
4889
+ * A minus sign in front of a number makes it negative rather than text : `'-5'` and `'-0x5'` are
4890
+ * both -5. This holds for the signed integers only — for any other type, such a string is text.
4826
4891
  * @param {BigNumberish | boolean} data the value to convert
4827
4892
  * @returns {bigint} the number the input spells, of whatever size
4828
4893
  * @example
@@ -4831,9 +4896,14 @@ var CairoInt8 = class _CairoInt8 {
4831
4896
  * // result = 97n
4832
4897
  * const result2 = CairoInt8.__processData('ab');
4833
4898
  * // result2 = 24930n (past the i8 range, and returned all the same)
4899
+ * const result3 = CairoInt8.__processData('-0x5');
4900
+ * // result3 = -5n (a negative number, not the text "-0x5")
4834
4901
  * ```
4835
4902
  */
4836
4903
  static __processData(data) {
4904
+ if (isString(data) && data.startsWith("-") && isBigNumberish(data.slice(1))) {
4905
+ return -BigInt(data.slice(1));
4906
+ }
4837
4907
  if (isString(data) && isText(data)) {
4838
4908
  return utf8ToBigInt(data);
4839
4909
  }
@@ -4912,11 +4982,12 @@ var CairoInt8 = class _CairoInt8 {
4912
4982
  /**
4913
4983
  * Throw unless the value can be carried by an i8.
4914
4984
  *
4915
- * Four things are refused, each with its own message : a null or undefined value, an object or
4916
- * an array, a number with a decimal part, and a value outside [-128, 127]. A text string reaches
4917
- * that last check as the number its bytes spell, so `'ab'` is refused for being out of range.
4985
+ * Five things are refused, each with its own message : a null or undefined value, an object or
4986
+ * an array, a number with a decimal part, `'0x'`, and a value outside [-128, 127]. A text string
4987
+ * reaches that last check as the number its bytes spell, so `'ab'` is refused for being out of
4988
+ * range.
4918
4989
  * @param {BigNumberish | boolean} data the value to check
4919
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
4990
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
4920
4991
  * @example
4921
4992
  * ```typescript
4922
4993
  * CairoInt8.validate(5); // passes
@@ -4931,6 +5002,7 @@ var CairoInt8 = class _CairoInt8 {
4931
5002
  !isNumber(data) || Number.isInteger(data),
4932
5003
  "Invalid input: decimal numbers are not supported, only integers"
4933
5004
  );
5005
+ assert(!isEmptyHex(data), "Invalid input: '0x' holds no hexadecimal digit");
4934
5006
  const value = _CairoInt8.__processData(data);
4935
5007
  assert(
4936
5008
  value >= RANGE_I8.min && value <= RANGE_I8.max,
@@ -5021,15 +5093,17 @@ var CairoInt16 = class _CairoInt16 {
5021
5093
  /**
5022
5094
  * Build from a number, a string or a boolean, refusing anything out of the i16 range.
5023
5095
  *
5024
- * A string is read as a number when it spells one — decimal or hexadecimal — and as UTF-8 text
5025
- * otherwise. Text therefore only fits here up to 2 ASCII characters : one more already
5026
- * makes a number past 32767.
5096
+ * A string is read as a number when it spells one — decimal or hexadecimal, with or without a
5097
+ * leading minus sign — and as UTF-8 text otherwise. Text therefore only fits here up to 2 ASCII
5098
+ * characters : one more already makes a number past 32767.
5027
5099
  * @param {BigNumberish | boolean} data the value to carry, within [-32768, 32767]
5028
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
5100
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
5029
5101
  * @example
5030
5102
  * ```typescript
5031
5103
  * const result = new CairoInt16('ab').toApiRequest();
5032
5104
  * // result = ["24930"]
5105
+ * const result2 = new CairoInt16('-5').toApiRequest();
5106
+ * // result2 = ["3618502788666131213697322783095070105623107215331596699973092056135872020476"]
5033
5107
  * ```
5034
5108
  */
5035
5109
  constructor(data) {
@@ -5041,6 +5115,9 @@ var CairoInt16 = class _CairoInt16 {
5041
5115
  *
5042
5116
  * Nothing here refuses a value : `validate` is what reads this number and decides. So an input
5043
5117
  * far outside the i16 range comes back untouched rather than raising.
5118
+ *
5119
+ * A minus sign in front of a number makes it negative rather than text : `'-5'` and `'-0x5'` are
5120
+ * both -5. This holds for the signed integers only — for any other type, such a string is text.
5044
5121
  * @param {BigNumberish | boolean} data the value to convert
5045
5122
  * @returns {bigint} the number the input spells, of whatever size
5046
5123
  * @example
@@ -5049,9 +5126,14 @@ var CairoInt16 = class _CairoInt16 {
5049
5126
  * // result = 24930n
5050
5127
  * const result2 = CairoInt16.__processData('abc');
5051
5128
  * // result2 = 6382179n (past the i16 range, and returned all the same)
5129
+ * const result3 = CairoInt16.__processData('-0x5');
5130
+ * // result3 = -5n (a negative number, not the text "-0x5")
5052
5131
  * ```
5053
5132
  */
5054
5133
  static __processData(data) {
5134
+ if (isString(data) && data.startsWith("-") && isBigNumberish(data.slice(1))) {
5135
+ return -BigInt(data.slice(1));
5136
+ }
5055
5137
  if (isString(data) && isText(data)) {
5056
5138
  return utf8ToBigInt(data);
5057
5139
  }
@@ -5130,11 +5212,11 @@ var CairoInt16 = class _CairoInt16 {
5130
5212
  /**
5131
5213
  * Throw unless the value can be carried by an i16.
5132
5214
  *
5133
- * Four things are refused, each with its own message : a null or undefined value, an object or
5134
- * an array, a number with a decimal part, and a value outside [-32768, 32767]. A text string reaches
5215
+ * Five things are refused, each with its own message : a null or undefined value, an object or
5216
+ * an array, a number with a decimal part, `'0x'`, and a value outside [-32768, 32767]. A text string reaches
5135
5217
  * that last check as the number its bytes spell, so `'abc'` is refused for being out of range.
5136
5218
  * @param {BigNumberish | boolean} data the value to check
5137
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
5219
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
5138
5220
  * @example
5139
5221
  * ```typescript
5140
5222
  * CairoInt16.validate(300); // passes
@@ -5149,6 +5231,7 @@ var CairoInt16 = class _CairoInt16 {
5149
5231
  !isNumber(data) || Number.isInteger(data),
5150
5232
  "Invalid input: decimal numbers are not supported, only integers"
5151
5233
  );
5234
+ assert(!isEmptyHex(data), "Invalid input: '0x' holds no hexadecimal digit");
5152
5235
  const value = _CairoInt16.__processData(data);
5153
5236
  assert(
5154
5237
  value >= RANGE_I16.min && value <= RANGE_I16.max,
@@ -5239,15 +5322,17 @@ var CairoInt32 = class _CairoInt32 {
5239
5322
  /**
5240
5323
  * Build from a number, a string or a boolean, refusing anything out of the i32 range.
5241
5324
  *
5242
- * A string is read as a number when it spells one — decimal or hexadecimal — and as UTF-8 text
5243
- * otherwise. Text therefore only fits here up to 4 ASCII characters : one more already
5244
- * makes a number past 2147483647.
5325
+ * A string is read as a number when it spells one — decimal or hexadecimal, with or without a
5326
+ * leading minus sign — and as UTF-8 text otherwise. Text therefore only fits here up to 4 ASCII
5327
+ * characters : one more already makes a number past 2147483647.
5245
5328
  * @param {BigNumberish | boolean} data the value to carry, within [-2147483648, 2147483647]
5246
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
5329
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
5247
5330
  * @example
5248
5331
  * ```typescript
5249
5332
  * const result = new CairoInt32('abcd').toApiRequest();
5250
5333
  * // result = ["1633837924"]
5334
+ * const result2 = new CairoInt32('-5').toApiRequest();
5335
+ * // result2 = ["3618502788666131213697322783095070105623107215331596699973092056135872020476"]
5251
5336
  * ```
5252
5337
  */
5253
5338
  constructor(data) {
@@ -5259,6 +5344,9 @@ var CairoInt32 = class _CairoInt32 {
5259
5344
  *
5260
5345
  * Nothing here refuses a value : `validate` is what reads this number and decides. So an input
5261
5346
  * far outside the i32 range comes back untouched rather than raising.
5347
+ *
5348
+ * A minus sign in front of a number makes it negative rather than text : `'-5'` and `'-0x5'` are
5349
+ * both -5. This holds for the signed integers only — for any other type, such a string is text.
5262
5350
  * @param {BigNumberish | boolean} data the value to convert
5263
5351
  * @returns {bigint} the number the input spells, of whatever size
5264
5352
  * @example
@@ -5267,9 +5355,14 @@ var CairoInt32 = class _CairoInt32 {
5267
5355
  * // result = 1633837924n
5268
5356
  * const result2 = CairoInt32.__processData('abcde');
5269
5357
  * // result2 = 418262508645n (past the i32 range, and returned all the same)
5358
+ * const result3 = CairoInt32.__processData('-0x5');
5359
+ * // result3 = -5n (a negative number, not the text "-0x5")
5270
5360
  * ```
5271
5361
  */
5272
5362
  static __processData(data) {
5363
+ if (isString(data) && data.startsWith("-") && isBigNumberish(data.slice(1))) {
5364
+ return -BigInt(data.slice(1));
5365
+ }
5273
5366
  if (isString(data) && isText(data)) {
5274
5367
  return utf8ToBigInt(data);
5275
5368
  }
@@ -5348,11 +5441,11 @@ var CairoInt32 = class _CairoInt32 {
5348
5441
  /**
5349
5442
  * Throw unless the value can be carried by an i32.
5350
5443
  *
5351
- * Four things are refused, each with its own message : a null or undefined value, an object or
5352
- * an array, a number with a decimal part, and a value outside [-2147483648, 2147483647]. A text string reaches
5444
+ * Five things are refused, each with its own message : a null or undefined value, an object or
5445
+ * an array, a number with a decimal part, `'0x'`, and a value outside [-2147483648, 2147483647]. A text string reaches
5353
5446
  * that last check as the number its bytes spell, so `'abcde'` is refused for being out of range.
5354
5447
  * @param {BigNumberish | boolean} data the value to check
5355
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
5448
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
5356
5449
  * @example
5357
5450
  * ```typescript
5358
5451
  * CairoInt32.validate(70000); // passes
@@ -5367,6 +5460,7 @@ var CairoInt32 = class _CairoInt32 {
5367
5460
  !isNumber(data) || Number.isInteger(data),
5368
5461
  "Invalid input: decimal numbers are not supported, only integers"
5369
5462
  );
5463
+ assert(!isEmptyHex(data), "Invalid input: '0x' holds no hexadecimal digit");
5370
5464
  const value = _CairoInt32.__processData(data);
5371
5465
  assert(
5372
5466
  value >= RANGE_I32.min && value <= RANGE_I32.max,
@@ -5457,15 +5551,17 @@ var CairoInt64 = class _CairoInt64 {
5457
5551
  /**
5458
5552
  * Build from a number, a string or a boolean, refusing anything out of the i64 range.
5459
5553
  *
5460
- * A string is read as a number when it spells one — decimal or hexadecimal — and as UTF-8 text
5461
- * otherwise. Text therefore only fits here up to 8 ASCII characters : one more already
5462
- * makes a number past 9223372036854775807.
5554
+ * A string is read as a number when it spells one — decimal or hexadecimal, with or without a
5555
+ * leading minus sign — and as UTF-8 text otherwise. Text therefore only fits here up to 8 ASCII
5556
+ * characters : one more already makes a number past 9223372036854775807.
5463
5557
  * @param {BigNumberish | boolean} data the value to carry, within [-9223372036854775808, 9223372036854775807]
5464
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
5558
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
5465
5559
  * @example
5466
5560
  * ```typescript
5467
5561
  * const result = new CairoInt64('abcdefgh').toApiRequest();
5468
5562
  * // result = ["7017280452245743464"]
5563
+ * const result2 = new CairoInt64('-5').toApiRequest();
5564
+ * // result2 = ["3618502788666131213697322783095070105623107215331596699973092056135872020476"]
5469
5565
  * ```
5470
5566
  */
5471
5567
  constructor(data) {
@@ -5477,6 +5573,9 @@ var CairoInt64 = class _CairoInt64 {
5477
5573
  *
5478
5574
  * Nothing here refuses a value : `validate` is what reads this number and decides. So an input
5479
5575
  * far outside the i64 range comes back untouched rather than raising.
5576
+ *
5577
+ * A minus sign in front of a number makes it negative rather than text : `'-5'` and `'-0x5'` are
5578
+ * both -5. This holds for the signed integers only — for any other type, such a string is text.
5480
5579
  * @param {BigNumberish | boolean} data the value to convert
5481
5580
  * @returns {bigint} the number the input spells, of whatever size
5482
5581
  * @example
@@ -5485,9 +5584,14 @@ var CairoInt64 = class _CairoInt64 {
5485
5584
  * // result = 7017280452245743464n
5486
5585
  * const result2 = CairoInt64.__processData('abcdefghi');
5487
5586
  * // result2 = 1796423795774910326889n (past the i64 range, and returned all the same)
5587
+ * const result3 = CairoInt64.__processData('-0x5');
5588
+ * // result3 = -5n (a negative number, not the text "-0x5")
5488
5589
  * ```
5489
5590
  */
5490
5591
  static __processData(data) {
5592
+ if (isString(data) && data.startsWith("-") && isBigNumberish(data.slice(1))) {
5593
+ return -BigInt(data.slice(1));
5594
+ }
5491
5595
  if (isString(data) && isText(data)) {
5492
5596
  return utf8ToBigInt(data);
5493
5597
  }
@@ -5566,11 +5670,11 @@ var CairoInt64 = class _CairoInt64 {
5566
5670
  /**
5567
5671
  * Throw unless the value can be carried by an i64.
5568
5672
  *
5569
- * Four things are refused, each with its own message : a null or undefined value, an object or
5570
- * an array, a number with a decimal part, and a value outside [-9223372036854775808, 9223372036854775807]. A text string reaches
5673
+ * Five things are refused, each with its own message : a null or undefined value, an object or
5674
+ * an array, a number with a decimal part, `'0x'`, and a value outside [-9223372036854775808, 9223372036854775807]. A text string reaches
5571
5675
  * that last check as the number its bytes spell, so `'abcdefghi'` is refused for being out of range.
5572
5676
  * @param {BigNumberish | boolean} data the value to check
5573
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
5677
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
5574
5678
  * @example
5575
5679
  * ```typescript
5576
5680
  * CairoInt64.validate(5000000000); // passes
@@ -5585,6 +5689,7 @@ var CairoInt64 = class _CairoInt64 {
5585
5689
  !isNumber(data) || Number.isInteger(data),
5586
5690
  "Invalid input: decimal numbers are not supported, only integers"
5587
5691
  );
5692
+ assert(!isEmptyHex(data), "Invalid input: '0x' holds no hexadecimal digit");
5588
5693
  const value = _CairoInt64.__processData(data);
5589
5694
  assert(
5590
5695
  value >= RANGE_I64.min && value <= RANGE_I64.max,
@@ -5675,15 +5780,17 @@ var CairoInt128 = class _CairoInt128 {
5675
5780
  /**
5676
5781
  * Build from a number, a string or a boolean, refusing anything out of the i128 range.
5677
5782
  *
5678
- * A string is read as a number when it spells one — decimal or hexadecimal — and as UTF-8 text
5679
- * otherwise. Text therefore only fits here up to 16 ASCII characters : one more already
5680
- * makes a number past 170141183460469231731687303715884105727.
5783
+ * A string is read as a number when it spells one — decimal or hexadecimal, with or without a
5784
+ * leading minus sign — and as UTF-8 text otherwise. Text therefore only fits here up to 16 ASCII
5785
+ * characters : one more already makes a number past 170141183460469231731687303715884105727.
5681
5786
  * @param {BigNumberish | boolean} data the value to carry, within [-170141183460469231731687303715884105728, 170141183460469231731687303715884105727]
5682
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
5787
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
5683
5788
  * @example
5684
5789
  * ```typescript
5685
5790
  * const result = new CairoInt128('abcdefghijklmnop').toApiRequest();
5686
5791
  * // result = ["129445976596022050476432668810952994672"]
5792
+ * const result2 = new CairoInt128('-5').toApiRequest();
5793
+ * // result2 = ["3618502788666131213697322783095070105623107215331596699973092056135872020476"]
5687
5794
  * ```
5688
5795
  */
5689
5796
  constructor(data) {
@@ -5695,6 +5802,9 @@ var CairoInt128 = class _CairoInt128 {
5695
5802
  *
5696
5803
  * Nothing here refuses a value : `validate` is what reads this number and decides. So an input
5697
5804
  * far outside the i128 range comes back untouched rather than raising.
5805
+ *
5806
+ * A minus sign in front of a number makes it negative rather than text : `'-5'` and `'-0x5'` are
5807
+ * both -5. This holds for the signed integers only — for any other type, such a string is text.
5698
5808
  * @param {BigNumberish | boolean} data the value to convert
5699
5809
  * @returns {bigint} the number the input spells, of whatever size
5700
5810
  * @example
@@ -5703,9 +5813,14 @@ var CairoInt128 = class _CairoInt128 {
5703
5813
  * // result = 129445976596022050476432668810952994672n
5704
5814
  * const result2 = CairoInt128.__processData('abcdefghijklmnopq');
5705
5815
  * // result2 = 33138170008581644921966763215603966636145n (past the i128 range, and returned all the same)
5816
+ * const result3 = CairoInt128.__processData('-0x5');
5817
+ * // result3 = -5n (a negative number, not the text "-0x5")
5706
5818
  * ```
5707
5819
  */
5708
5820
  static __processData(data) {
5821
+ if (isString(data) && data.startsWith("-") && isBigNumberish(data.slice(1))) {
5822
+ return -BigInt(data.slice(1));
5823
+ }
5709
5824
  if (isString(data) && isText(data)) {
5710
5825
  return utf8ToBigInt(data);
5711
5826
  }
@@ -5784,11 +5899,11 @@ var CairoInt128 = class _CairoInt128 {
5784
5899
  /**
5785
5900
  * Throw unless the value can be carried by an i128.
5786
5901
  *
5787
- * Four things are refused, each with its own message : a null or undefined value, an object or
5788
- * an array, a number with a decimal part, and a value outside [-170141183460469231731687303715884105728, 170141183460469231731687303715884105727]. A text string reaches
5902
+ * Five things are refused, each with its own message : a null or undefined value, an object or
5903
+ * an array, a number with a decimal part, `'0x'`, and a value outside [-170141183460469231731687303715884105728, 170141183460469231731687303715884105727]. A text string reaches
5789
5904
  * that last check as the number its bytes spell, so `'abcdefghijklmnopq'` is refused for being out of range.
5790
5905
  * @param {BigNumberish | boolean} data the value to check
5791
- * @throws {Error} when the value is null, undefined, an object, a decimal number, or out of range
5906
+ * @throws {Error} when the value is null, undefined, an object, a decimal number, `'0x'`, or out of range
5792
5907
  * @example
5793
5908
  * ```typescript
5794
5909
  * CairoInt128.validate(5000000000); // passes
@@ -5803,6 +5918,7 @@ var CairoInt128 = class _CairoInt128 {
5803
5918
  !isNumber(data) || Number.isInteger(data),
5804
5919
  "Invalid input: decimal numbers are not supported, only integers"
5805
5920
  );
5921
+ assert(!isEmptyHex(data), "Invalid input: '0x' holds no hexadecimal digit");
5806
5922
  const value = _CairoInt128.__processData(data);
5807
5923
  assert(
5808
5924
  value >= RANGE_I128.min && value <= RANGE_I128.max,
@@ -5893,7 +6009,7 @@ var CairoBool = class _CairoBool {
5893
6009
  /**
5894
6010
  * Build from a boolean, or from the numbers 0 and 1.
5895
6011
  * @param {BigNumberish | boolean} data the value to carry : a boolean, 0 or 1
5896
- * @throws {Error} when the value is text, is not a felt252 input, or is a number other than 0 or 1
6012
+ * @throws {Error} when the value is text, a decimal number, not a felt252 input, or a number other than 0 or 1
5897
6013
  * @example
5898
6014
  * ```typescript
5899
6015
  * const result = new CairoBool(false).toApiRequest();
@@ -5966,21 +6082,28 @@ var CairoBool = class _CairoBool {
5966
6082
  /**
5967
6083
  * Throw unless the value can be carried by a bool.
5968
6084
  *
5969
- * Text is refused first, then the value is read as a felt252 — which is what refuses a null, an
5970
- * object or an unsupported type — and finally checked to be one of the only two numbers a bool
5971
- * can hold.
6085
+ * Text is refused first, then a number with a decimal part. A number or a bigint is read as it
6086
+ * is, so that a negative one reaches the last check. Anything else is read as a felt252 — which
6087
+ * is what refuses a null, an object or an unsupported type. The value is finally checked to be
6088
+ * one of the only two numbers a bool can hold.
5972
6089
  * @param {BigNumberish | boolean} data the value to check
5973
- * @throws {Error} when the value is text, is not a felt252 input, or is a number other than 0 or 1
6090
+ * @throws {Error} when the value is text, a decimal number, not a felt252 input, or a number other than 0 or 1
5974
6091
  * @example
5975
6092
  * ```typescript
5976
6093
  * CairoBool.validate(true); // passes
5977
6094
  * CairoBool.validate(2);
5978
6095
  * // throws Error("Only values 0 or 1 are possible in a core::bool, received 2")
6096
+ * CairoBool.validate(-1);
6097
+ * // throws Error("Only values 0 or 1 are possible in a core::bool, received -1")
5979
6098
  * ```
5980
6099
  */
5981
6100
  static validate(data) {
5982
6101
  assert(!isText(data), "Invalid input: a core::bool cannot be built from text");
5983
- const value = new CairoFelt252(data).toBigInt();
6102
+ assert(
6103
+ !isNumber(data) || Number.isInteger(data),
6104
+ "Invalid input: decimal numbers are not supported, only integers"
6105
+ );
6106
+ const value = isNumber(data) || isBigInt(data) ? BigInt(data) : new CairoFelt252(data).toBigInt();
5984
6107
  assert(
5985
6108
  value === 0n || value === 1n,
5986
6109
  `Only values 0 or 1 are possible in a core::bool, received ${value}`
@@ -6095,7 +6218,9 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6095
6218
  /**
6096
6219
  * @param {any[]} arr either one value — a 512-bit number or a {@link Secp256k1PointStruct} — or
6097
6220
  * the four limbs
6098
- * @throws {Error} when a value is out of range, or when the argument count is neither 1 nor 4
6221
+ * @throws {Error} when a value is null, undefined, of an unread type, text, a decimal number, or out
6222
+ * of range — see {@link CairoSecp256k1Point.validate} and {@link CairoSecp256k1Point.validateProps}
6223
+ * — or when the argument count is neither 1 nor 4
6099
6224
  * @example
6100
6225
  * ```typescript
6101
6226
  * const result = new CairoSecp256k1Point(1n).toApiRequest();
@@ -6142,22 +6267,32 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6142
6267
  * the constructor needs it, and computing it twice would mean splitting a 512-bit value twice.
6143
6268
  * @param {BigNumberish} input the 512-bit value to check
6144
6269
  * @returns {bigint} the value, once checked
6145
- * @throws {Error} when the value is null, undefined, of an unread type, or outside [0, 2^512 - 1]
6270
+ * @throws {Error} when the value is null, undefined, text, `'0x'`, of an unread type, a decimal number, or outside [0, 2^512 - 1]
6146
6271
  * @example
6147
6272
  * ```typescript
6148
6273
  * const result = CairoSecp256k1Point.validate('0x1234');
6149
6274
  * // result = 4660n
6150
6275
  * CairoSecp256k1Point.validate(SECP256K1_POINT_MAX + 1n);
6151
6276
  * // throws Error("input is bigger than SECP256K1_POINT_MAX")
6277
+ * CairoSecp256k1Point.validate('abc');
6278
+ * // throws Error("Invalid input: a Secp256k1Point cannot be built from text")
6279
+ * CairoSecp256k1Point.validate(1.5);
6280
+ * // throws Error("Invalid input: decimal numbers are not supported, only integers")
6152
6281
  * ```
6153
6282
  */
6154
6283
  static validate(input) {
6155
6284
  assert(input !== null, "null value is not allowed for Secp256k1Point");
6156
6285
  assert(input !== void 0, "undefined value is not allowed for Secp256k1Point");
6286
+ assert(!isText(input), "Invalid input: a Secp256k1Point cannot be built from text");
6287
+ assert(!isEmptyHex(input), "Invalid input: '0x' holds no hexadecimal digit");
6157
6288
  assert(
6158
6289
  isBigNumberish(input),
6159
6290
  `Unsupported input for Secp256k1Point. Expected a number, a bigint, or a string spelling one, received '${typeof input}'`
6160
6291
  );
6292
+ assert(
6293
+ !isNumber(input) || Number.isInteger(input),
6294
+ "Invalid input: decimal numbers are not supported, only integers"
6295
+ );
6161
6296
  const bigInt = BigInt(input);
6162
6297
  assert(bigInt >= SECP256K1_POINT_MIN, "input is smaller than SECP256K1_POINT_MIN");
6163
6298
  assert(bigInt <= SECP256K1_POINT_MAX, "input is bigger than SECP256K1_POINT_MAX");
@@ -6170,20 +6305,27 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6170
6305
  * @param {BigNumberish} yLow the low 128 bits of y
6171
6306
  * @param {BigNumberish} yHigh the high 128 bits of y
6172
6307
  * @returns {{xLow: bigint, xHigh: bigint, yLow: bigint, yHigh: bigint}} the four limbs, checked
6173
- * @throws {Error} when a limb is null, undefined, not a number, negative, or wider than 128 bits
6308
+ * @throws {Error} when a limb is null, undefined, text, `'0x'`, not a number, a decimal number, negative, or wider than 128 bits
6174
6309
  * @example
6175
6310
  * ```typescript
6176
6311
  * const result = CairoSecp256k1Point.validateProps(1, 2, 3, 4);
6177
6312
  * // result = { xLow: 1n, xHigh: 2n, yLow: 3n, yHigh: 4n }
6178
6313
  * CairoSecp256k1Point.validateProps(1, 2, 3, 2n ** 128n);
6179
6314
  * // throws Error("yHigh must fit in 128 bits")
6315
+ * CairoSecp256k1Point.validateProps(1.5, 2, 3, 4);
6316
+ * // throws Error("xLow must be an integer")
6317
+ * CairoSecp256k1Point.validateProps('abc', 2, 3, 4);
6318
+ * // throws Error("xLow cannot be built from text")
6180
6319
  * ```
6181
6320
  */
6182
6321
  static validateProps(xLow, xHigh, yLow, yHigh) {
6183
6322
  const validateLimb = (limb, name) => {
6184
6323
  assert(limb !== null, `${name} cannot be null`);
6185
6324
  assert(limb !== void 0, `${name} cannot be undefined`);
6325
+ assert(!isText(limb), `${name} cannot be built from text`);
6326
+ assert(!isEmptyHex(limb), `${name} cannot be '0x', which holds no hexadecimal digit`);
6186
6327
  assert(isBigNumberish(limb), `${name} must be a BigNumberish`);
6328
+ assert(!isNumber(limb) || Number.isInteger(limb), `${name} must be an integer`);
6187
6329
  const bigInt = BigInt(limb);
6188
6330
  assert(bigInt >= 0n, `${name} must be non-negative`);
6189
6331
  assert(bigInt <= UINT_128_MAX, `${name} must fit in 128 bits`);
@@ -7326,9 +7468,9 @@ var CairoClassHash = class _CairoClassHash {
7326
7468
  */
7327
7469
  static abiSelector = Literal.ClassHash;
7328
7470
  /**
7329
- * Build from a number or a numeric string, refusing text and anything wider than 252 bits.
7330
- * @param {BigNumberish | boolean} data the hash to carry, within [0, 2^252 - 1]
7331
- * @throws {Error} when the value is text, is not a felt252 input, or is out of the ClassHash range
7471
+ * Build from a number or a numeric string, refusing text and anything outside the ClassHash range.
7472
+ * @param {BigNumberish | boolean} data the hash to carry, within [0, 2^251 - 1]
7473
+ * @throws {Error} when the value is text, a decimal number, not a felt252 input, or out of the ClassHash range
7332
7474
  * @example
7333
7475
  * ```typescript
7334
7476
  * const result = new CairoClassHash('0x1234').toApiRequest();
@@ -7383,21 +7525,34 @@ var CairoClassHash = class _CairoClassHash {
7383
7525
  /**
7384
7526
  * Throw unless the value can be carried by a ClassHash.
7385
7527
  *
7386
- * Text is refused first, then the value is read as a felt252 — which is what refuses a null, an
7387
- * object or an unsupported type, and what bounds the value. There is no narrower bound to add:
7388
- * a class hash is a hash output, so any field element is one.
7528
+ * Text is refused first, then a number with a decimal part. A number or a bigint is read as it
7529
+ * is, so that a negative one reaches the range check. Anything else is read as a felt252 — which
7530
+ * is what refuses a null, an object or an unsupported type. The value is finally checked against
7531
+ * the range of the Cairo `ClassHash` type, [0, 2^251), which is narrower than the field.
7389
7532
  * @param {BigNumberish | boolean} data the value to check
7390
- * @throws {Error} when the value is text, is not a felt252 input, or is outside the field
7533
+ * @throws {Error} when the value is text, a decimal number, not a felt252 input, or out of the ClassHash range
7391
7534
  * @example
7392
7535
  * ```typescript
7393
7536
  * CairoClassHash.validate('0x1234'); // passes
7394
7537
  * CairoClassHash.validate('abc');
7395
7538
  * // throws Error("Invalid input: a ClassHash cannot be built from text")
7539
+ * CairoClassHash.validate(2n ** 251n);
7540
+ * // throws Error("Value is out of ClassHash range [0, 3618502788666131106986593281521497120414687020801267626233049500247285301247]")
7541
+ * CairoClassHash.validate(-1);
7542
+ * // throws Error("Value is out of ClassHash range [0, 3618502788666131106986593281521497120414687020801267626233049500247285301247]")
7396
7543
  * ```
7397
7544
  */
7398
7545
  static validate(data) {
7399
7546
  assert(!isText(data), "Invalid input: a ClassHash cannot be built from text");
7400
- CairoFelt252.validate(data);
7547
+ assert(
7548
+ !isNumber(data) || Number.isInteger(data),
7549
+ "Invalid input: decimal numbers are not supported, only integers"
7550
+ );
7551
+ const value = isNumber(data) || isBigInt(data) ? BigInt(data) : new CairoFelt252(data).toBigInt();
7552
+ assert(
7553
+ value >= RANGE_CLASS_HASH.min && value <= RANGE_CLASS_HASH.max,
7554
+ `Value is out of ClassHash range [${RANGE_CLASS_HASH.min}, ${RANGE_CLASS_HASH.max}]`
7555
+ );
7401
7556
  }
7402
7557
  /**
7403
7558
  * Can this value be carried by a ClassHash?
@@ -7476,7 +7631,7 @@ var CairoContractAddress = class _CairoContractAddress {
7476
7631
  /**
7477
7632
  * Build from a number or a numeric string, refusing text and anything wider than 252 bits.
7478
7633
  * @param {BigNumberish | boolean} data the address to carry, within [0, 2^252 - 1]
7479
- * @throws {Error} when the value is text, is not a felt252 input, or is out of range
7634
+ * @throws {Error} when the value is text, a decimal number, not a felt252 input, or out of range
7480
7635
  * @example
7481
7636
  * ```typescript
7482
7637
  * const result = new CairoContractAddress('0x1234').toApiRequest();
@@ -7531,11 +7686,12 @@ var CairoContractAddress = class _CairoContractAddress {
7531
7686
  /**
7532
7687
  * Throw unless the value can be carried by a ContractAddress.
7533
7688
  *
7534
- * Text is refused first, then the value is read as a felt252 — which is what refuses a null, an
7535
- * object or an unsupported type — and finally checked against the bound an address has, which
7536
- * is narrower than the field.
7689
+ * Text is refused first, then a number with a decimal part. A number or a bigint is read as it
7690
+ * is, so that a negative one reaches the range check. Anything else is read as a felt252 — which
7691
+ * is what refuses a null, an object or an unsupported type. The value is finally checked against
7692
+ * the bound an address has, which is narrower than the field.
7537
7693
  * @param {BigNumberish | boolean} data the value to check
7538
- * @throws {Error} when the value is text, is not a felt252 input, or is out of range
7694
+ * @throws {Error} when the value is text, a decimal number, not a felt252 input, or out of range
7539
7695
  * @example
7540
7696
  * ```typescript
7541
7697
  * CairoContractAddress.validate('0x1234'); // passes
@@ -7543,11 +7699,17 @@ var CairoContractAddress = class _CairoContractAddress {
7543
7699
  * // throws Error("Invalid input: a ContractAddress cannot be built from text")
7544
7700
  * CairoContractAddress.validate(2n ** 251n);
7545
7701
  * // throws Error("Value is out of ContractAddress range [0, 3618502788666131106986593281521497120414687020801267626233049500247285300991]")
7702
+ * CairoContractAddress.validate(-1);
7703
+ * // throws Error("Value is out of ContractAddress range [0, 3618502788666131106986593281521497120414687020801267626233049500247285300991]")
7546
7704
  * ```
7547
7705
  */
7548
7706
  static validate(data) {
7549
7707
  assert(!isText(data), "Invalid input: a ContractAddress cannot be built from text");
7550
- const value = new CairoFelt252(data).toBigInt();
7708
+ assert(
7709
+ !isNumber(data) || Number.isInteger(data),
7710
+ "Invalid input: decimal numbers are not supported, only integers"
7711
+ );
7712
+ const value = isNumber(data) || isBigInt(data) ? BigInt(data) : new CairoFelt252(data).toBigInt();
7551
7713
  assert(
7552
7714
  value >= RANGE_CONTRACT_ADDRESS.min && value <= RANGE_CONTRACT_ADDRESS.max,
7553
7715
  `Value is out of ContractAddress range [${RANGE_CONTRACT_ADDRESS.min}, ${RANGE_CONTRACT_ADDRESS.max}]`
@@ -7632,7 +7794,7 @@ var CairoEthAddress = class _CairoEthAddress {
7632
7794
  /**
7633
7795
  * Build from a number or a numeric string, refusing text and anything wider than 160 bits.
7634
7796
  * @param {BigNumberish | boolean} data the address to carry, within [0, 2^160 - 1]
7635
- * @throws {Error} when the value is text, is not a felt252 input, or is out of the EthAddress range
7797
+ * @throws {Error} when the value is text, a decimal number, not a felt252 input, or out of the EthAddress range
7636
7798
  * @example
7637
7799
  * ```typescript
7638
7800
  * const result = new CairoEthAddress('0x1234').toApiRequest();
@@ -7688,10 +7850,12 @@ var CairoEthAddress = class _CairoEthAddress {
7688
7850
  * Throw unless the value can be carried by an EthAddress.
7689
7851
  *
7690
7852
  * Text is refused first, since an address spelled as words is a mistake rather than a value to
7691
- * encode. What remains is read as a felt252 — which is what refuses a null, an object or an
7692
- * unsupported type — then checked against the 160 bits an Ethereum address occupies.
7853
+ * encode, then a number with a decimal part. A number or a bigint is read as it is, so that a
7854
+ * negative one reaches the range check below. Anything else is read as a felt252 — which is what
7855
+ * refuses a null, an object or an unsupported type. The value is finally checked against the 160
7856
+ * bits an Ethereum address occupies.
7693
7857
  * @param {BigNumberish | boolean} data the value to check
7694
- * @throws {Error} when the value is text, is not a felt252 input, or is out of the EthAddress range
7858
+ * @throws {Error} when the value is text, a decimal number, not a felt252 input, or out of the EthAddress range
7695
7859
  * @example
7696
7860
  * ```typescript
7697
7861
  * CairoEthAddress.validate('0x1234'); // passes
@@ -7699,11 +7863,17 @@ var CairoEthAddress = class _CairoEthAddress {
7699
7863
  * // throws Error("Invalid input: an EthAddress cannot be built from text")
7700
7864
  * CairoEthAddress.validate(2n ** 160n);
7701
7865
  * // throws Error("Value is out of EthAddress range [0, 1461501637330902918203684832716283019655932542975]")
7866
+ * CairoEthAddress.validate(-1);
7867
+ * // throws Error("Value is out of EthAddress range [0, 1461501637330902918203684832716283019655932542975]")
7702
7868
  * ```
7703
7869
  */
7704
7870
  static validate(data) {
7705
7871
  assert(!isText(data), "Invalid input: an EthAddress cannot be built from text");
7706
- const value = new CairoFelt252(data).toBigInt();
7872
+ assert(
7873
+ !isNumber(data) || Number.isInteger(data),
7874
+ "Invalid input: decimal numbers are not supported, only integers"
7875
+ );
7876
+ const value = isNumber(data) || isBigInt(data) ? BigInt(data) : new CairoFelt252(data).toBigInt();
7707
7877
  assert(
7708
7878
  value >= RANGE_ETH_ADDRESS.min && value <= RANGE_ETH_ADDRESS.max,
7709
7879
  `Value is out of EthAddress range [${RANGE_ETH_ADDRESS.min}, ${RANGE_ETH_ADDRESS.max}]`
@@ -8378,7 +8548,13 @@ var CairoTypeOption = class _CairoTypeOption {
8378
8548
  * Build an option, from a value a caller passed or from the felts of a response.
8379
8549
  *
8380
8550
  * A {@link CairoOption} says its own branch, so `variant` is not given with one — nor with the
8381
- * response iterator, which reads the branch off the wire. Anywhere else it is required.
8551
+ * response iterator, which reads the branch off the wire, nor with a `CairoTypeOption`, which
8552
+ * is copied as it is. Anywhere else it is required.
8553
+ *
8554
+ * For the same reason, a `CairoOption` that holds a `CairoTypeOption` is refused: once it is
8555
+ * unwrapped, the variant comes with the `CairoTypeOption`. Accepted, it would lose one level:
8556
+ * `Some(Some(1))` would be sent as `Some(None)`. Write a nested value with `CairoOption`
8557
+ * only, or build one `CairoTypeOption` for the whole type.
8382
8558
  *
8383
8559
  * `subType` is what makes an option of options work. Handed a `CairoOption`, this constructor
8384
8560
  * unwraps it and calls itself with what was inside, which for a nested option is another
@@ -8389,11 +8565,11 @@ var CairoTypeOption = class _CairoTypeOption {
8389
8565
  * @param {string} optionCairoType the abi type, `core::option::Option::<T>`
8390
8566
  * @param {AllowArray<CairoTypeStrategy>} parsingStrategy how to build the value
8391
8567
  * @param {CairoOptionVariant | number} [variant] which branch, when the content does not say.
8392
- * Must be omitted when `content` is the response iterator.
8568
+ * Must be omitted when `content` is the response iterator or a `CairoTypeOption`.
8393
8569
  * @param {boolean} [subType=false] true when called from the unwrapping of a nested
8394
8570
  * `CairoOption`, which is the only caller that should set it
8395
- * @throws {Error} when the type is not an option, when the variant is missing, absurd, or
8396
- * contradicts the content
8571
+ * @throws {Error} when the type is not an option, or when the variant is missing, absurd,
8572
+ * contradicts the content, or is given with a `CairoTypeOption`
8397
8573
  * @example
8398
8574
  * ```typescript
8399
8575
  * const type = 'core::option::Option::<core::integer::u8>';
@@ -8438,6 +8614,10 @@ var CairoTypeOption = class _CairoTypeOption {
8438
8614
  return;
8439
8615
  }
8440
8616
  if (content instanceof _CairoTypeOption) {
8617
+ assert(
8618
+ isUndefined(variant),
8619
+ 'when "content" parameter is a CairoTypeOption, do not define "variant" parameter.'
8620
+ );
8441
8621
  this.content = content.content;
8442
8622
  this.isVariantSome = content.isVariantSome;
8443
8623
  this.optionCairoType = content.optionCairoType;
@@ -10899,11 +11079,17 @@ function errorU512(key) {
10899
11079
  `Your object includes the property : ${key}, containing an Uint512 object without the 'limb0' to 'limb3' keys.`
10900
11080
  );
10901
11081
  }
11082
+ function getVariantType(abiEnum, variantName) {
11083
+ return abiEnum.variants?.find((abiVariant) => abiVariant.name === variantName)?.type ?? "";
11084
+ }
10902
11085
  function orderPropsByAbi(unorderedObject, abiOfObject, structs, enums) {
10903
11086
  const orderInput = (unorderedItem, abiType) => {
10904
11087
  if (CairoFixedArray.isTypeFixedArray(abiType)) {
10905
11088
  return orderFixedArray(unorderedItem, abiType);
10906
11089
  }
11090
+ if (isCairoType(unorderedItem)) {
11091
+ return unorderedItem;
11092
+ }
10907
11093
  if (isTypeArray(abiType)) {
10908
11094
  return orderArray(unorderedItem, abiType);
10909
11095
  }
@@ -11016,14 +11202,8 @@ function orderPropsByAbi(unorderedObject, abiOfObject, structs, enums) {
11016
11202
  const orderEnum = (unorderedObject2, abiObject) => {
11017
11203
  if (isTypeResult(abiObject.name)) {
11018
11204
  const unorderedResult = unorderedObject2;
11019
- const resultOkType = abiObject.name.substring(
11020
- abiObject.name.indexOf("<") + 1,
11021
- abiObject.name.lastIndexOf(",")
11022
- );
11023
- const resultErrType = abiObject.name.substring(
11024
- abiObject.name.indexOf(",") + 1,
11025
- abiObject.name.lastIndexOf(">")
11026
- );
11205
+ const resultOkType = getVariantType(abiObject, "Ok");
11206
+ const resultErrType = getVariantType(abiObject, "Err");
11027
11207
  if (unorderedResult.isOk()) {
11028
11208
  return new CairoResult(
11029
11209
  CairoResultVariant.Ok,
@@ -11037,10 +11217,7 @@ function orderPropsByAbi(unorderedObject, abiOfObject, structs, enums) {
11037
11217
  }
11038
11218
  if (isTypeOption(abiObject.name)) {
11039
11219
  const unorderedOption = unorderedObject2;
11040
- const resultSomeType = abiObject.name.substring(
11041
- abiObject.name.indexOf("<") + 1,
11042
- abiObject.name.lastIndexOf(">")
11043
- );
11220
+ const resultSomeType = getVariantType(abiObject, "Some");
11044
11221
  if (unorderedOption.isSome()) {
11045
11222
  return new CairoOption(
11046
11223
  CairoOptionVariant.Some,
@@ -11055,10 +11232,7 @@ function orderPropsByAbi(unorderedObject, abiOfObject, structs, enums) {
11055
11232
  if (isUndefined(variant[1])) {
11056
11233
  return variant;
11057
11234
  }
11058
- const variantType = abiObject.type.substring(
11059
- abiObject.type.lastIndexOf("<") + 1,
11060
- abiObject.type.lastIndexOf(">")
11061
- );
11235
+ const variantType = getVariantType(abiObject, variant[0]);
11062
11236
  if (variantType === "()") {
11063
11237
  return variant;
11064
11238
  }
@@ -13420,7 +13594,7 @@ function ethRandomPrivateKey() {
13420
13594
  return sanitizeHex(buf2hex(secp256k1.utils.randomSecretKey()));
13421
13595
  }
13422
13596
  function validateAndParseEthAddress(address) {
13423
- assertInRange(address, ZERO, 2n ** 160n - 1n, "Ethereum Address ");
13597
+ assertInRange(address, ZERO, 2n ** 160n - 1n, "Ethereum Address");
13424
13598
  const result = addHexPrefix(removeHexPrefix(toHex(address)).padStart(40, "0"));
13425
13599
  assert(Boolean(result.match(/^(0x)?[0-9a-f]{40}$/)), "Invalid Ethereum Address Format");
13426
13600
  return result;
@@ -17574,6 +17748,9 @@ function addAddressPadding(address) {
17574
17748
  return addHexPrefix(padded);
17575
17749
  }
17576
17750
  function validateAndParseAddress(address) {
17751
+ if (isString(address) && !/^(0x)?[0-9a-f]+$/i.test(address)) {
17752
+ throw new Error("Invalid Address Format");
17753
+ }
17577
17754
  const result = addAddressPadding(address);
17578
17755
  if (!result.match(/^(0x)?[0-9a-fA-F]{64}$/)) {
17579
17756
  throw new Error("Invalid Address Format");