u64ToIntSafe function

int u64ToIntSafe(
  1. BigInt value,
  2. String fieldName
)

Narrows a u64 (BigInt) into an int with an explicit safety check.

Real-world expiry values are far below the unsafe range, so this check is effectively a guard against corrupted or malicious BCS data rather than a precision concern in normal operation. Throwing is correct behavior at the BCS/JSON boundary.

Implementation

int u64ToIntSafe(BigInt value, String fieldName) {
  if (value.isNegative) {
    throw RangeError(
        '$fieldName is negative ($value); expected an unsigned u64');
  }
  if (value > _maxSafeU64) {
    throw RangeError(
      '$fieldName ($value) exceeds the maximum safe integer ($_maxSafeU64); '
      'refusing to silently lose precision',
    );
  }
  return value.toInt();
}