SafeCast.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. const assert = require('assert');
  2. const format = require('../format-lines');
  3. const { range } = require('../../helpers');
  4. const LENGTHS = range(8, 256, 8).reverse(); // 248 → 8 (in steps of 8)
  5. // Returns the version of OpenZeppelin Contracts in which a particular function was introduced.
  6. // This is used in the docs for each function.
  7. const version = (selector, length) => {
  8. switch (selector) {
  9. case 'toUint(uint)': {
  10. switch (length) {
  11. case 8:
  12. case 16:
  13. case 32:
  14. case 64:
  15. case 128:
  16. return '2.5';
  17. case 96:
  18. case 224:
  19. return '4.2';
  20. default:
  21. assert(LENGTHS.includes(length));
  22. return '4.7';
  23. }
  24. }
  25. case 'toInt(int)': {
  26. switch (length) {
  27. case 8:
  28. case 16:
  29. case 32:
  30. case 64:
  31. case 128:
  32. return '3.1';
  33. default:
  34. assert(LENGTHS.includes(length));
  35. return '4.7';
  36. }
  37. }
  38. case 'toUint(int)': {
  39. switch (length) {
  40. case 256:
  41. return '3.0';
  42. default:
  43. assert(false);
  44. return;
  45. }
  46. }
  47. case 'toInt(uint)': {
  48. switch (length) {
  49. case 256:
  50. return '3.0';
  51. default:
  52. assert(false);
  53. return;
  54. }
  55. }
  56. default:
  57. assert(false);
  58. }
  59. };
  60. const header = `\
  61. pragma solidity ^0.8.19;
  62. /**
  63. * @dev Wrappers over Solidity's uintXX/intXX casting operators with added overflow
  64. * checks.
  65. *
  66. * Downcasting from uint256/int256 in Solidity does not revert on overflow. This can
  67. * easily result in undesired exploitation or bugs, since developers usually
  68. * assume that overflows raise errors. \`SafeCast\` restores this intuition by
  69. * reverting the transaction when such an operation overflows.
  70. *
  71. * Using this library instead of the unchecked operations eliminates an entire
  72. * class of bugs, so it's recommended to use it always.
  73. */
  74. `;
  75. const errors = `\
  76. /**
  77. * @dev Value doesn't fit in an uint of \`bits\` size.
  78. */
  79. error SafeCastOverflowedUintDowncast(uint8 bits, uint256 value);
  80. /**
  81. * @dev An int value doesn't fit in an uint of \`bits\` size.
  82. */
  83. error SafeCastOverflowedIntToUint(int256 value);
  84. /**
  85. * @dev Value doesn't fit in an int of \`bits\` size.
  86. */
  87. error SafeCastOverflowedIntDowncast(uint8 bits, int256 value);
  88. /**
  89. * @dev An uint value doesn't fit in an int of \`bits\` size.
  90. */
  91. error SafeCastOverflowedUintToInt(uint256 value);
  92. `;
  93. const toUintDownCast = length => `\
  94. /**
  95. * @dev Returns the downcasted uint${length} from uint256, reverting on
  96. * overflow (when the input is greater than largest uint${length}).
  97. *
  98. * Counterpart to Solidity's \`uint${length}\` operator.
  99. *
  100. * Requirements:
  101. *
  102. * - input must fit into ${length} bits
  103. *
  104. * _Available since v${version('toUint(uint)', length)}._
  105. */
  106. function toUint${length}(uint256 value) internal pure returns (uint${length}) {
  107. if (value > type(uint${length}).max) {
  108. revert SafeCastOverflowedUintDowncast(${length}, value);
  109. }
  110. return uint${length}(value);
  111. }
  112. `;
  113. /* eslint-disable max-len */
  114. const toIntDownCast = length => `\
  115. /**
  116. * @dev Returns the downcasted int${length} from int256, reverting on
  117. * overflow (when the input is less than smallest int${length} or
  118. * greater than largest int${length}).
  119. *
  120. * Counterpart to Solidity's \`int${length}\` operator.
  121. *
  122. * Requirements:
  123. *
  124. * - input must fit into ${length} bits
  125. *
  126. * _Available since v${version('toInt(int)', length)}._
  127. */
  128. function toInt${length}(int256 value) internal pure returns (int${length} downcasted) {
  129. downcasted = int${length}(value);
  130. if (downcasted != value) {
  131. revert SafeCastOverflowedIntDowncast(${length}, value);
  132. }
  133. }
  134. `;
  135. /* eslint-enable max-len */
  136. const toInt = length => `\
  137. /**
  138. * @dev Converts an unsigned uint${length} into a signed int${length}.
  139. *
  140. * Requirements:
  141. *
  142. * - input must be less than or equal to maxInt${length}.
  143. *
  144. * _Available since v${version('toInt(uint)', length)}._
  145. */
  146. function toInt${length}(uint${length} value) internal pure returns (int${length}) {
  147. // Note: Unsafe cast below is okay because \`type(int${length}).max\` is guaranteed to be positive
  148. if (value > uint${length}(type(int${length}).max)) {
  149. revert SafeCastOverflowedUintToInt(value);
  150. }
  151. return int${length}(value);
  152. }
  153. `;
  154. const toUint = length => `\
  155. /**
  156. * @dev Converts a signed int${length} into an unsigned uint${length}.
  157. *
  158. * Requirements:
  159. *
  160. * - input must be greater than or equal to 0.
  161. *
  162. * _Available since v${version('toUint(int)', length)}._
  163. */
  164. function toUint${length}(int${length} value) internal pure returns (uint${length}) {
  165. if (value < 0) {
  166. revert SafeCastOverflowedIntToUint(value);
  167. }
  168. return uint${length}(value);
  169. }
  170. `;
  171. // GENERATE
  172. module.exports = format(
  173. header.trimEnd(),
  174. 'library SafeCast {',
  175. errors,
  176. [...LENGTHS.map(toUintDownCast), toUint(256), ...LENGTHS.map(toIntDownCast), toInt(256)],
  177. '}',
  178. );