Procedural SafeCast.sol generation (#3245)
This commit is contained in:
16
scripts/generate/format-lines.js
Normal file
16
scripts/generate/format-lines.js
Normal file
@ -0,0 +1,16 @@
|
||||
function formatLines (...lines) {
|
||||
return [...indentEach(0, lines)].join('\n') + '\n';
|
||||
}
|
||||
|
||||
function *indentEach (indent, lines) {
|
||||
for (const line of lines) {
|
||||
if (Array.isArray(line)) {
|
||||
yield * indentEach(indent + 1, line);
|
||||
} else {
|
||||
const padding = ' '.repeat(indent);
|
||||
yield * line.split('\n').map(subline => subline === '' ? '' : padding + subline);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = formatLines;
|
||||
29
scripts/generate/run.js
Executable file
29
scripts/generate/run.js
Executable file
@ -0,0 +1,29 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const fs = require('fs');
|
||||
const format = require('./format-lines');
|
||||
|
||||
function getVersion (path) {
|
||||
try {
|
||||
return fs
|
||||
.readFileSync(path, 'utf8')
|
||||
.match(/\/\/ OpenZeppelin Contracts \(last updated v\d+\.\d+\.\d+\)/)[0];
|
||||
} catch (err) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
for (const [ file, template ] of Object.entries({
|
||||
'utils/math/SafeCast.sol': './templates/SafeCast',
|
||||
'mocks/SafeCastMock.sol': './templates/SafeCastMock',
|
||||
})) {
|
||||
const path = `./contracts/${file}`;
|
||||
const version = getVersion(path);
|
||||
const content = format(
|
||||
'// SPDX-License-Identifier: MIT',
|
||||
(version ? version + ` (${file})\n` : ''),
|
||||
require(template).trimEnd(),
|
||||
);
|
||||
|
||||
fs.writeFileSync(path, content);
|
||||
}
|
||||
168
scripts/generate/templates/SafeCast.js
Executable file
168
scripts/generate/templates/SafeCast.js
Executable file
@ -0,0 +1,168 @@
|
||||
const assert = require('assert');
|
||||
const format = require('../format-lines');
|
||||
const { range } = require('../../helpers');
|
||||
|
||||
const LENGTHS = range(8, 256, 8).reverse(); // 248 → 8 (in steps of 8)
|
||||
|
||||
// Returns the version of OpenZeppelin Contracts in which a particular function was introduced.
|
||||
// This is used in the docs for each function.
|
||||
const version = (selector, length) => {
|
||||
switch (selector) {
|
||||
case 'toUint(uint)': {
|
||||
switch (length) {
|
||||
case 8:
|
||||
case 16:
|
||||
case 32:
|
||||
case 64:
|
||||
case 128:
|
||||
return '2.5';
|
||||
case 96:
|
||||
case 224:
|
||||
return '4.2';
|
||||
default:
|
||||
assert(LENGTHS.includes(length));
|
||||
return '4.7';
|
||||
}
|
||||
}
|
||||
case 'toInt(int)': {
|
||||
switch (length) {
|
||||
case 8:
|
||||
case 16:
|
||||
case 32:
|
||||
case 64:
|
||||
case 128:
|
||||
return '3.1';
|
||||
default:
|
||||
assert(LENGTHS.includes(length));
|
||||
return '4.7';
|
||||
}
|
||||
}
|
||||
case 'toUint(int)': {
|
||||
switch (length) {
|
||||
case 256:
|
||||
return '3.0';
|
||||
default:
|
||||
assert(false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
case 'toInt(uint)': {
|
||||
switch (length) {
|
||||
case 256:
|
||||
return '3.0';
|
||||
default:
|
||||
assert(false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
};
|
||||
|
||||
const header = `\
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
/**
|
||||
* @dev Wrappers over Solidity's uintXX/intXX casting operators with added overflow
|
||||
* checks.
|
||||
*
|
||||
* Downcasting from uint256/int256 in Solidity does not revert on overflow. This can
|
||||
* easily result in undesired exploitation or bugs, since developers usually
|
||||
* assume that overflows raise errors. \`SafeCast\` restores this intuition by
|
||||
* reverting the transaction when such an operation overflows.
|
||||
*
|
||||
* Using this library instead of the unchecked operations eliminates an entire
|
||||
* class of bugs, so it's recommended to use it always.
|
||||
*
|
||||
* Can be combined with {SafeMath} and {SignedSafeMath} to extend it to smaller types, by performing
|
||||
* all math on \`uint256\` and \`int256\` and then downcasting.
|
||||
*/
|
||||
`;
|
||||
|
||||
const toUintDownCast = length => `\
|
||||
/**
|
||||
* @dev Returns the downcasted uint${length} from uint256, reverting on
|
||||
* overflow (when the input is greater than largest uint${length}).
|
||||
*
|
||||
* Counterpart to Solidity's \`uint${length}\` operator.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - input must fit into ${length} bits
|
||||
*
|
||||
* _Available since v${version('toUint(uint)', length)}._
|
||||
*/
|
||||
function toUint${length}(uint256 value) internal pure returns (uint${length}) {
|
||||
require(value <= type(uint${length}).max, "SafeCast: value doesn't fit in ${length} bits");
|
||||
return uint${length}(value);
|
||||
}
|
||||
`;
|
||||
|
||||
/* eslint-disable max-len */
|
||||
const toIntDownCast = length => `\
|
||||
/**
|
||||
* @dev Returns the downcasted int${length} from int256, reverting on
|
||||
* overflow (when the input is less than smallest int${length} or
|
||||
* greater than largest int${length}).
|
||||
*
|
||||
* Counterpart to Solidity's \`int${length}\` operator.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - input must fit into ${length} bits
|
||||
*
|
||||
* _Available since v${version('toInt(int)', length)}._
|
||||
*/
|
||||
function toInt${length}(int256 value) internal pure returns (int${length}) {
|
||||
require(value >= type(int${length}).min && value <= type(int${length}).max, "SafeCast: value doesn't fit in ${length} bits");
|
||||
return int${length}(value);
|
||||
}
|
||||
`;
|
||||
/* eslint-enable max-len */
|
||||
|
||||
const toInt = length => `\
|
||||
/**
|
||||
* @dev Converts an unsigned uint${length} into a signed int${length}.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - input must be less than or equal to maxInt${length}.
|
||||
*
|
||||
* _Available since v${version('toInt(uint)', length)}._
|
||||
*/
|
||||
function toInt${length}(uint${length} value) internal pure returns (int${length}) {
|
||||
// Note: Unsafe cast below is okay because \`type(int${length}).max\` is guaranteed to be positive
|
||||
require(value <= uint${length}(type(int${length}).max), "SafeCast: value doesn't fit in an int${length}");
|
||||
return int${length}(value);
|
||||
}
|
||||
`;
|
||||
|
||||
const toUint = length => `\
|
||||
/**
|
||||
* @dev Converts a signed int${length} into an unsigned uint${length}.
|
||||
*
|
||||
* Requirements:
|
||||
*
|
||||
* - input must be greater than or equal to 0.
|
||||
*
|
||||
* _Available since v${version('toUint(int)', length)}._
|
||||
*/
|
||||
function toUint${length}(int${length} value) internal pure returns (uint${length}) {
|
||||
require(value >= 0, "SafeCast: value must be positive");
|
||||
return uint${length}(value);
|
||||
}
|
||||
`;
|
||||
|
||||
// GENERATE
|
||||
module.exports = format(
|
||||
header.trimEnd(),
|
||||
'library SafeCast {',
|
||||
[
|
||||
...LENGTHS.map(size => toUintDownCast(size)),
|
||||
toUint(256),
|
||||
...LENGTHS.map(size => toIntDownCast(size)),
|
||||
toInt(256).trimEnd(),
|
||||
],
|
||||
'}',
|
||||
);
|
||||
50
scripts/generate/templates/SafeCastMock.js
Executable file
50
scripts/generate/templates/SafeCastMock.js
Executable file
@ -0,0 +1,50 @@
|
||||
const format = require('../format-lines');
|
||||
const { range } = require('../../helpers');
|
||||
|
||||
const LENGTHS = range(8, 256, 8).reverse(); // 248 → 8 (in steps of 8)
|
||||
|
||||
const header = `\
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "../utils/math/SafeCast.sol";
|
||||
`;
|
||||
|
||||
const toInt = length => `\
|
||||
function toInt${length}(uint${length} a) public pure returns (int${length}) {
|
||||
return a.toInt${length}();
|
||||
}
|
||||
`;
|
||||
|
||||
const toUint = length => `\
|
||||
function toUint${length}(int${length} a) public pure returns (uint${length}) {
|
||||
return a.toUint${length}();
|
||||
}
|
||||
`;
|
||||
|
||||
const toIntDownCast = length => `\
|
||||
function toInt${length}(int256 a) public pure returns (int${length}) {
|
||||
return a.toInt${length}();
|
||||
}
|
||||
`;
|
||||
|
||||
const toUintDownCast = length => `\
|
||||
function toUint${length}(uint256 a) public pure returns (uint${length}) {
|
||||
return a.toUint${length}();
|
||||
}
|
||||
`;
|
||||
|
||||
// GENERATE
|
||||
module.exports = format(
|
||||
header,
|
||||
'contract SafeCastMock {',
|
||||
[
|
||||
'using SafeCast for uint256;',
|
||||
'using SafeCast for int256;',
|
||||
'',
|
||||
toUint(256),
|
||||
...LENGTHS.map(size => toUintDownCast(size)),
|
||||
toInt(256),
|
||||
...LENGTHS.map(size => toIntDownCast(size)),
|
||||
].flatMap(fn => fn.split('\n')).slice(0, -1),
|
||||
'}',
|
||||
);
|
||||
Reference in New Issue
Block a user