Add toUint, toInt and hexToUint to Strings (#5166)
Co-authored-by: cairo <cairoeth@protonmail.com> Co-authored-by: Ernesto García <ernestognw@gmail.com>
This commit is contained in:
5
.changeset/eighty-hounds-promise.md
Normal file
5
.changeset/eighty-hounds-promise.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
'openzeppelin-solidity': minor
|
||||
---
|
||||
|
||||
`Strings`: Add `parseUint`, `parseInt`, `parseHexUint` and `parseAddress` to parse strings into numbers and addresses. Also provide variants of these functions that parse substrings, and `tryXxx` variants that do not revert on invalid input.
|
||||
@ -13,6 +13,7 @@ import {DoubleEndedQueue} from "../utils/structs/DoubleEndedQueue.sol";
|
||||
import {Address} from "../utils/Address.sol";
|
||||
import {Context} from "../utils/Context.sol";
|
||||
import {Nonces} from "../utils/Nonces.sol";
|
||||
import {Strings} from "../utils/Strings.sol";
|
||||
import {IGovernor, IERC6372} from "./IGovernor.sol";
|
||||
|
||||
/**
|
||||
@ -760,67 +761,25 @@ abstract contract Governor is Context, ERC165, EIP712, Nonces, IGovernor, IERC72
|
||||
address proposer,
|
||||
string memory description
|
||||
) internal view virtual returns (bool) {
|
||||
uint256 len = bytes(description).length;
|
||||
unchecked {
|
||||
uint256 length = bytes(description).length;
|
||||
|
||||
// Length is too short to contain a valid proposer suffix
|
||||
if (len < 52) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Extract what would be the `#proposer=0x` marker beginning the suffix
|
||||
bytes12 marker;
|
||||
assembly ("memory-safe") {
|
||||
// - Start of the string contents in memory = description + 32
|
||||
// - First character of the marker = len - 52
|
||||
// - Length of "#proposer=0x0000000000000000000000000000000000000000" = 52
|
||||
// - We read the memory word starting at the first character of the marker:
|
||||
// - (description + 32) + (len - 52) = description + (len - 20)
|
||||
// - Note: Solidity will ignore anything past the first 12 bytes
|
||||
marker := mload(add(description, sub(len, 20)))
|
||||
}
|
||||
|
||||
// If the marker is not found, there is no proposer suffix to check
|
||||
if (marker != bytes12("#proposer=0x")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Parse the 40 characters following the marker as uint160
|
||||
uint160 recovered = 0;
|
||||
for (uint256 i = len - 40; i < len; ++i) {
|
||||
(bool isHex, uint8 value) = _tryHexToUint(bytes(description)[i]);
|
||||
// If any of the characters is not a hex digit, ignore the suffix entirely
|
||||
if (!isHex) {
|
||||
// Length is too short to contain a valid proposer suffix
|
||||
if (length < 52) {
|
||||
return true;
|
||||
}
|
||||
recovered = (recovered << 4) | value;
|
||||
}
|
||||
|
||||
return recovered == uint160(proposer);
|
||||
}
|
||||
// Extract what would be the `#proposer=` marker beginning the suffix
|
||||
bytes10 marker = bytes10(_unsafeReadBytesOffset(bytes(description), length - 52));
|
||||
|
||||
/**
|
||||
* @dev Try to parse a character from a string as a hex value. Returns `(true, value)` if the char is in
|
||||
* `[0-9a-fA-F]` and `(false, 0)` otherwise. Value is guaranteed to be in the range `0 <= value < 16`
|
||||
*/
|
||||
function _tryHexToUint(bytes1 char) private pure returns (bool isHex, uint8 value) {
|
||||
uint8 c = uint8(char);
|
||||
unchecked {
|
||||
// Case 0-9
|
||||
if (47 < c && c < 58) {
|
||||
return (true, c - 48);
|
||||
}
|
||||
// Case A-F
|
||||
else if (64 < c && c < 71) {
|
||||
return (true, c - 55);
|
||||
}
|
||||
// Case a-f
|
||||
else if (96 < c && c < 103) {
|
||||
return (true, c - 87);
|
||||
}
|
||||
// Else: not a hex char
|
||||
else {
|
||||
return (false, 0);
|
||||
// If the marker is not found, there is no proposer suffix to check
|
||||
if (marker != bytes10("#proposer=")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check that the last 42 characters (after the marker) are a properly formatted address.
|
||||
(bool success, address recovered) = Strings.tryParseAddress(description, length - 42, length);
|
||||
return !success || recovered == proposer;
|
||||
}
|
||||
}
|
||||
|
||||
@ -849,4 +808,17 @@ abstract contract Governor is Context, ERC165, EIP712, Nonces, IGovernor, IERC72
|
||||
* @inheritdoc IGovernor
|
||||
*/
|
||||
function quorum(uint256 timepoint) public view virtual returns (uint256);
|
||||
|
||||
/**
|
||||
* @dev Reads a bytes32 from a bytes array without bounds checking.
|
||||
*
|
||||
* NOTE: making this function internal would mean it could be used with memory unsafe offset, and marking the
|
||||
* assembly block as such would prevent some optimizations.
|
||||
*/
|
||||
function _unsafeReadBytesOffset(bytes memory buffer, uint256 offset) private pure returns (bytes32 value) {
|
||||
// This is not memory safe in the general case, but all calls to this private function are within bounds.
|
||||
assembly ("memory-safe") {
|
||||
value := mload(add(buffer, add(0x20, offset)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -34,7 +34,7 @@ Miscellaneous contracts and libraries containing utility functions you can use t
|
||||
* {Strings}: Common operations for strings formatting.
|
||||
* {ShortString}: Library to encode (and decode) short strings into (or from) a single bytes32 slot for optimizing costs. Short strings are limited to 31 characters.
|
||||
* {SlotDerivation}: Methods for deriving storage slot from ERC-7201 namespaces as well as from constructions such as mapping and arrays.
|
||||
* {StorageSlot}: Methods for accessing specific storage slots formatted as common primitive types.
|
||||
* {StorageSlot}: Methods for accessing specific storage slots formatted as common primitive types.
|
||||
* {TransientSlot}: Primitives for reading from and writing to transient storage (only value types are currently supported).
|
||||
* {Multicall}: Abstract contract with a utility to allow batching together multiple calls in a single transaction. Useful for allowing EOAs to perform multiple operations at once.
|
||||
* {Context}: A utility for abstracting the sender and calldata in the current execution context.
|
||||
|
||||
@ -4,12 +4,15 @@
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {Math} from "./math/Math.sol";
|
||||
import {SafeCast} from "./math/SafeCast.sol";
|
||||
import {SignedMath} from "./math/SignedMath.sol";
|
||||
|
||||
/**
|
||||
* @dev String operations.
|
||||
*/
|
||||
library Strings {
|
||||
using SafeCast for *;
|
||||
|
||||
bytes16 private constant HEX_DIGITS = "0123456789abcdef";
|
||||
uint8 private constant ADDRESS_LENGTH = 20;
|
||||
|
||||
@ -18,6 +21,16 @@ library Strings {
|
||||
*/
|
||||
error StringsInsufficientHexLength(uint256 value, uint256 length);
|
||||
|
||||
/**
|
||||
* @dev The string being parsed contains characters that are not in scope of the given base.
|
||||
*/
|
||||
error StringsInvalidChar();
|
||||
|
||||
/**
|
||||
* @dev The string being parsed is not a properly formatted address.
|
||||
*/
|
||||
error StringsInvalidAddressFormat();
|
||||
|
||||
/**
|
||||
* @dev Converts a `uint256` to its ASCII `string` decimal representation.
|
||||
*/
|
||||
@ -113,4 +126,275 @@ library Strings {
|
||||
function equal(string memory a, string memory b) internal pure returns (bool) {
|
||||
return bytes(a).length == bytes(b).length && keccak256(bytes(a)) == keccak256(bytes(b));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Parse a decimal string and returns the value as a `uint256`.
|
||||
*
|
||||
* Requirements:
|
||||
* - The string must be formatted as `[0-9]*`
|
||||
* - The result must fit into an `uint256` type
|
||||
*/
|
||||
function parseUint(string memory input) internal pure returns (uint256) {
|
||||
return parseUint(input, 0, bytes(input).length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Variant of {parseUint} that parses a substring of `input` located between position `begin` (included) and
|
||||
* `end` (excluded).
|
||||
*
|
||||
* Requirements:
|
||||
* - The substring must be formatted as `[0-9]*`
|
||||
* - The result must fit into an `uint256` type
|
||||
*/
|
||||
function parseUint(string memory input, uint256 begin, uint256 end) internal pure returns (uint256) {
|
||||
(bool success, uint256 value) = tryParseUint(input, begin, end);
|
||||
if (!success) revert StringsInvalidChar();
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Variant of {parseUint-string} that returns false if the parsing fails because of an invalid character.
|
||||
*
|
||||
* NOTE: This function will revert if the result does not fit in a `uint256`.
|
||||
*/
|
||||
function tryParseUint(string memory input) internal pure returns (bool success, uint256 value) {
|
||||
return tryParseUint(input, 0, bytes(input).length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Variant of {parseUint-string-uint256-uint256} that returns false if the parsing fails because of an invalid
|
||||
* character.
|
||||
*
|
||||
* NOTE: This function will revert if the result does not fit in a `uint256`.
|
||||
*/
|
||||
function tryParseUint(
|
||||
string memory input,
|
||||
uint256 begin,
|
||||
uint256 end
|
||||
) internal pure returns (bool success, uint256 value) {
|
||||
bytes memory buffer = bytes(input);
|
||||
|
||||
uint256 result = 0;
|
||||
for (uint256 i = begin; i < end; ++i) {
|
||||
uint8 chr = _tryParseChr(bytes1(_unsafeReadBytesOffset(buffer, i)));
|
||||
if (chr > 9) return (false, 0);
|
||||
result *= 10;
|
||||
result += chr;
|
||||
}
|
||||
return (true, result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Parse a decimal string and returns the value as a `int256`.
|
||||
*
|
||||
* Requirements:
|
||||
* - The string must be formatted as `[-+]?[0-9]*`
|
||||
* - The result must fit in an `int256` type.
|
||||
*/
|
||||
function parseInt(string memory input) internal pure returns (int256) {
|
||||
return parseInt(input, 0, bytes(input).length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Variant of {parseInt-string} that parses a substring of `input` located between position `begin` (included) and
|
||||
* `end` (excluded).
|
||||
*
|
||||
* Requirements:
|
||||
* - The substring must be formatted as `[-+]?[0-9]*`
|
||||
* - The result must fit in an `int256` type.
|
||||
*/
|
||||
function parseInt(string memory input, uint256 begin, uint256 end) internal pure returns (int256) {
|
||||
(bool success, int256 value) = tryParseInt(input, begin, end);
|
||||
if (!success) revert StringsInvalidChar();
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Variant of {parseInt-string} that returns false if the parsing fails because of an invalid character or if
|
||||
* the result does not fit in a `int256`.
|
||||
*
|
||||
* NOTE: This function will revert if the absolute value of the result does not fit in a `uint256`.
|
||||
*/
|
||||
function tryParseInt(string memory input) internal pure returns (bool success, int256 value) {
|
||||
return tryParseInt(input, 0, bytes(input).length);
|
||||
}
|
||||
|
||||
uint256 private constant ABS_MIN_INT256 = 2 ** 255;
|
||||
|
||||
/**
|
||||
* @dev Variant of {parseInt-string-uint256-uint256} that returns false if the parsing fails because of an invalid
|
||||
* character or if the result does not fit in a `int256`.
|
||||
*
|
||||
* NOTE: This function will revert if the absolute value of the result does not fit in a `uint256`.
|
||||
*/
|
||||
function tryParseInt(
|
||||
string memory input,
|
||||
uint256 begin,
|
||||
uint256 end
|
||||
) internal pure returns (bool success, int256 value) {
|
||||
bytes memory buffer = bytes(input);
|
||||
|
||||
// Check presence of a negative sign.
|
||||
bytes1 sign = bytes1(_unsafeReadBytesOffset(buffer, begin));
|
||||
bool positiveSign = sign == bytes1("+");
|
||||
bool negativeSign = sign == bytes1("-");
|
||||
uint256 offset = (positiveSign || negativeSign).toUint();
|
||||
|
||||
(bool absSuccess, uint256 absValue) = tryParseUint(input, begin + offset, end);
|
||||
|
||||
if (absSuccess && absValue < ABS_MIN_INT256) {
|
||||
return (true, negativeSign ? -int256(absValue) : int256(absValue));
|
||||
} else if (absSuccess && negativeSign && absValue == ABS_MIN_INT256) {
|
||||
return (true, type(int256).min);
|
||||
} else return (false, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Parse a hexadecimal string (with or without "0x" prefix), and returns the value as a `uint256`.
|
||||
*
|
||||
* Requirements:
|
||||
* - The string must be formatted as `(0x)?[0-9a-fA-F]*`
|
||||
* - The result must fit in an `uint256` type.
|
||||
*/
|
||||
function parseHexUint(string memory input) internal pure returns (uint256) {
|
||||
return parseHexUint(input, 0, bytes(input).length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Variant of {parseHexUint} that parses a substring of `input` located between position `begin` (included) and
|
||||
* `end` (excluded).
|
||||
*
|
||||
* Requirements:
|
||||
* - The substring must be formatted as `(0x)?[0-9a-fA-F]*`
|
||||
* - The result must fit in an `uint256` type.
|
||||
*/
|
||||
function parseHexUint(string memory input, uint256 begin, uint256 end) internal pure returns (uint256) {
|
||||
(bool success, uint256 value) = tryParseHexUint(input, begin, end);
|
||||
if (!success) revert StringsInvalidChar();
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Variant of {parseHexUint-string} that returns false if the parsing fails because of an invalid character.
|
||||
*
|
||||
* NOTE: This function will revert if the result does not fit in a `uint256`.
|
||||
*/
|
||||
function tryParseHexUint(string memory input) internal pure returns (bool success, uint256 value) {
|
||||
return tryParseHexUint(input, 0, bytes(input).length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Variant of {parseHexUint-string-uint256-uint256} that returns false if the parsing fails because of an
|
||||
* invalid character.
|
||||
*
|
||||
* NOTE: This function will revert if the result does not fit in a `uint256`.
|
||||
*/
|
||||
function tryParseHexUint(
|
||||
string memory input,
|
||||
uint256 begin,
|
||||
uint256 end
|
||||
) internal pure returns (bool success, uint256 value) {
|
||||
bytes memory buffer = bytes(input);
|
||||
|
||||
// skip 0x prefix if present
|
||||
bool hasPrefix = bytes2(_unsafeReadBytesOffset(buffer, begin)) == bytes2("0x");
|
||||
uint256 offset = hasPrefix.toUint() * 2;
|
||||
|
||||
uint256 result = 0;
|
||||
for (uint256 i = begin + offset; i < end; ++i) {
|
||||
uint8 chr = _tryParseChr(bytes1(_unsafeReadBytesOffset(buffer, i)));
|
||||
if (chr > 15) return (false, 0);
|
||||
result *= 16;
|
||||
unchecked {
|
||||
// Multiplying by 16 is equivalent to a shift of 4 bits (with additional overflow check).
|
||||
// This guaratees that adding a value < 16 will not cause an overflow, hence the unchecked.
|
||||
result += chr;
|
||||
}
|
||||
}
|
||||
return (true, result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Parse a hexadecimal string (with or without "0x" prefix), and returns the value as an `address`.
|
||||
*
|
||||
* Requirements:
|
||||
* - The string must be formatted as `(0x)?[0-9a-fA-F]{40}`
|
||||
*/
|
||||
function parseAddress(string memory input) internal pure returns (address) {
|
||||
return parseAddress(input, 0, bytes(input).length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Variant of {parseAddress} that parses a substring of `input` located between position `begin` (included) and
|
||||
* `end` (excluded).
|
||||
*
|
||||
* Requirements:
|
||||
* - The substring must be formatted as `(0x)?[0-9a-fA-F]{40}`
|
||||
*/
|
||||
function parseAddress(string memory input, uint256 begin, uint256 end) internal pure returns (address) {
|
||||
(bool success, address value) = tryParseAddress(input, begin, end);
|
||||
if (!success) revert StringsInvalidAddressFormat();
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Variant of {parseAddress-string} that returns false if the parsing fails because the input is not a properly
|
||||
* formatted address. See {parseAddress} requirements.
|
||||
*/
|
||||
function tryParseAddress(string memory input) internal pure returns (bool success, address value) {
|
||||
return tryParseAddress(input, 0, bytes(input).length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Variant of {parseAddress-string-uint256-uint256} that returns false if the parsing fails because input is not a properly
|
||||
* formatted address. See {parseAddress} requirements.
|
||||
*/
|
||||
function tryParseAddress(
|
||||
string memory input,
|
||||
uint256 begin,
|
||||
uint256 end
|
||||
) internal pure returns (bool success, address value) {
|
||||
// check that input is the correct length
|
||||
bool hasPrefix = bytes2(_unsafeReadBytesOffset(bytes(input), begin)) == bytes2("0x");
|
||||
uint256 expectedLength = 40 + hasPrefix.toUint() * 2;
|
||||
|
||||
if (end - begin == expectedLength) {
|
||||
// length guarantees that this does not overflow, and value is at most type(uint160).max
|
||||
(bool s, uint256 v) = tryParseHexUint(input, begin, end);
|
||||
return (s, address(uint160(v)));
|
||||
} else {
|
||||
return (false, address(0));
|
||||
}
|
||||
}
|
||||
|
||||
function _tryParseChr(bytes1 chr) private pure returns (uint8) {
|
||||
uint8 value = uint8(chr);
|
||||
|
||||
// Try to parse `chr`:
|
||||
// - Case 1: [0-9]
|
||||
// - Case 2: [a-f]
|
||||
// - Case 3: [A-F]
|
||||
// - otherwise not supported
|
||||
unchecked {
|
||||
if (value > 47 && value < 58) value -= 48;
|
||||
else if (value > 96 && value < 103) value -= 87;
|
||||
else if (value > 64 && value < 71) value -= 55;
|
||||
else return type(uint8).max;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Reads a bytes32 from a bytes array without bounds checking.
|
||||
*
|
||||
* NOTE: making this function internal would mean it could be used with memory unsafe offset, and marking the
|
||||
* assembly block as such would prevent some optimizations.
|
||||
*/
|
||||
function _unsafeReadBytesOffset(bytes memory buffer, uint256 offset) private pure returns (bytes32 value) {
|
||||
// This is not memory safe in the general case, but all calls to this private function are within bounds.
|
||||
assembly ("memory-safe") {
|
||||
value := mload(add(buffer, add(0x20, offset)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
27
test/utils/Strings.t.sol
Normal file
27
test/utils/Strings.t.sol
Normal file
@ -0,0 +1,27 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {Test} from "forge-std/Test.sol";
|
||||
|
||||
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
|
||||
|
||||
contract StringsTest is Test {
|
||||
using Strings for *;
|
||||
|
||||
function testParse(uint256 value) external {
|
||||
assertEq(value, value.toString().parseUint());
|
||||
}
|
||||
|
||||
function testParseSigned(int256 value) external {
|
||||
assertEq(value, value.toStringSigned().parseInt());
|
||||
}
|
||||
|
||||
function testParseHex(uint256 value) external {
|
||||
assertEq(value, value.toHexString().parseHexUint());
|
||||
}
|
||||
|
||||
function testParseChecksumHex(address value) external {
|
||||
assertEq(value, value.toChecksumHexString().parseAddress());
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
const { PANIC_CODES } = require('@nomicfoundation/hardhat-chai-matchers/panic');
|
||||
|
||||
async function fixture() {
|
||||
const mock = await ethers.deployContract('$Strings');
|
||||
@ -38,11 +39,15 @@ describe('Strings', function () {
|
||||
it('converts MAX_UINT256', async function () {
|
||||
const value = ethers.MaxUint256;
|
||||
expect(await this.mock.$toString(value)).to.equal(value.toString(10));
|
||||
expect(await this.mock.$parseUint(value.toString(10))).to.equal(value);
|
||||
expect(await this.mock.$tryParseUint(value.toString(10))).to.deep.equal([true, value]);
|
||||
});
|
||||
|
||||
for (const value of values) {
|
||||
it(`converts ${value}`, async function () {
|
||||
expect(await this.mock.$toString(value)).to.equal(value);
|
||||
expect(await this.mock.$toString(value)).to.equal(value.toString(10));
|
||||
expect(await this.mock.$parseUint(value.toString(10))).to.equal(value);
|
||||
expect(await this.mock.$tryParseUint(value.toString(10))).to.deep.equal([true, value]);
|
||||
});
|
||||
}
|
||||
});
|
||||
@ -51,21 +56,29 @@ describe('Strings', function () {
|
||||
it('converts MAX_INT256', async function () {
|
||||
const value = ethers.MaxInt256;
|
||||
expect(await this.mock.$toStringSigned(value)).to.equal(value.toString(10));
|
||||
expect(await this.mock.$parseInt(value.toString(10))).to.equal(value);
|
||||
expect(await this.mock.$tryParseInt(value.toString(10))).to.deep.equal([true, value]);
|
||||
});
|
||||
|
||||
it('converts MIN_INT256', async function () {
|
||||
const value = ethers.MinInt256;
|
||||
expect(await this.mock.$toStringSigned(value)).to.equal(value.toString(10));
|
||||
expect(await this.mock.$parseInt(value.toString(10))).to.equal(value);
|
||||
expect(await this.mock.$tryParseInt(value.toString(10))).to.deep.equal([true, value]);
|
||||
});
|
||||
|
||||
for (const value of values) {
|
||||
it(`convert ${value}`, async function () {
|
||||
expect(await this.mock.$toStringSigned(value)).to.equal(value);
|
||||
expect(await this.mock.$toStringSigned(value)).to.equal(value.toString(10));
|
||||
expect(await this.mock.$parseInt(value.toString(10))).to.equal(value);
|
||||
expect(await this.mock.$tryParseInt(value.toString(10))).to.deep.equal([true, value]);
|
||||
});
|
||||
|
||||
it(`convert negative ${value}`, async function () {
|
||||
const negated = -value;
|
||||
expect(await this.mock.$toStringSigned(negated)).to.equal(negated.toString(10));
|
||||
expect(await this.mock.$parseInt(negated.toString(10))).to.equal(negated);
|
||||
expect(await this.mock.$tryParseInt(negated.toString(10))).to.deep.equal([true, negated]);
|
||||
});
|
||||
}
|
||||
});
|
||||
@ -73,17 +86,36 @@ describe('Strings', function () {
|
||||
|
||||
describe('toHexString', function () {
|
||||
it('converts 0', async function () {
|
||||
expect(await this.mock.getFunction('$toHexString(uint256)')(0n)).to.equal('0x00');
|
||||
const value = 0n;
|
||||
const string = ethers.toBeHex(value); // 0x00
|
||||
|
||||
expect(await this.mock.getFunction('$toHexString(uint256)')(value)).to.equal(string);
|
||||
expect(await this.mock.$parseHexUint(string)).to.equal(value);
|
||||
expect(await this.mock.$parseHexUint(string.replace(/0x/, ''))).to.equal(value);
|
||||
expect(await this.mock.$tryParseHexUint(string)).to.deep.equal([true, value]);
|
||||
expect(await this.mock.$tryParseHexUint(string.replace(/0x/, ''))).to.deep.equal([true, value]);
|
||||
});
|
||||
|
||||
it('converts a positive number', async function () {
|
||||
expect(await this.mock.getFunction('$toHexString(uint256)')(0x4132n)).to.equal('0x4132');
|
||||
const value = 0x4132n;
|
||||
const string = ethers.toBeHex(value);
|
||||
|
||||
expect(await this.mock.getFunction('$toHexString(uint256)')(value)).to.equal(string);
|
||||
expect(await this.mock.$parseHexUint(string)).to.equal(value);
|
||||
expect(await this.mock.$parseHexUint(string.replace(/0x/, ''))).to.equal(value);
|
||||
expect(await this.mock.$tryParseHexUint(string)).to.deep.equal([true, value]);
|
||||
expect(await this.mock.$tryParseHexUint(string.replace(/0x/, ''))).to.deep.equal([true, value]);
|
||||
});
|
||||
|
||||
it('converts MAX_UINT256', async function () {
|
||||
expect(await this.mock.getFunction('$toHexString(uint256)')(ethers.MaxUint256)).to.equal(
|
||||
`0x${ethers.MaxUint256.toString(16)}`,
|
||||
);
|
||||
const value = ethers.MaxUint256;
|
||||
const string = ethers.toBeHex(value);
|
||||
|
||||
expect(await this.mock.getFunction('$toHexString(uint256)')(value)).to.equal(string);
|
||||
expect(await this.mock.$parseHexUint(string)).to.equal(value);
|
||||
expect(await this.mock.$parseHexUint(string.replace(/0x/, ''))).to.equal(value);
|
||||
expect(await this.mock.$tryParseHexUint(string)).to.deep.equal([true, value]);
|
||||
expect(await this.mock.$tryParseHexUint(string.replace(/0x/, ''))).to.deep.equal([true, value]);
|
||||
});
|
||||
});
|
||||
|
||||
@ -97,13 +129,13 @@ describe('Strings', function () {
|
||||
it('converts a positive number (short)', async function () {
|
||||
const length = 1n;
|
||||
await expect(this.mock.getFunction('$toHexString(uint256,uint256)')(0x4132n, length))
|
||||
.to.be.revertedWithCustomError(this.mock, `StringsInsufficientHexLength`)
|
||||
.to.be.revertedWithCustomError(this.mock, 'StringsInsufficientHexLength')
|
||||
.withArgs(0x4132, length);
|
||||
});
|
||||
|
||||
it('converts MAX_UINT256', async function () {
|
||||
expect(await this.mock.getFunction('$toHexString(uint256,uint256)')(ethers.MaxUint256, 32n)).to.equal(
|
||||
`0x${ethers.MaxUint256.toString(16)}`,
|
||||
ethers.toBeHex(ethers.MaxUint256),
|
||||
);
|
||||
});
|
||||
});
|
||||
@ -139,9 +171,16 @@ describe('Strings', function () {
|
||||
describe('toChecksumHexString', function () {
|
||||
for (const addr of addresses) {
|
||||
it(`converts ${addr}`, async function () {
|
||||
expect(await this.mock.getFunction('$toChecksumHexString(address)')(addr)).to.equal(
|
||||
ethers.getAddress(addr.toLowerCase()),
|
||||
);
|
||||
expect(await this.mock.$toChecksumHexString(addr)).to.equal(ethers.getAddress(addr));
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe('parseAddress', function () {
|
||||
for (const addr of addresses) {
|
||||
it(`converts ${addr}`, async function () {
|
||||
expect(await this.mock.$parseAddress(addr)).to.equal(ethers.getAddress(addr));
|
||||
expect(await this.mock.$tryParseAddress(addr)).to.deep.equal([true, ethers.getAddress(addr)]);
|
||||
});
|
||||
}
|
||||
});
|
||||
@ -177,4 +216,112 @@ describe('Strings', function () {
|
||||
expect(await this.mock.$equal(str1, str2)).to.be.true;
|
||||
});
|
||||
});
|
||||
|
||||
describe('Edge cases: invalid parsing', function () {
|
||||
it('parseUint overflow', async function () {
|
||||
await expect(this.mock.$parseUint((ethers.MaxUint256 + 1n).toString(10))).to.be.revertedWithPanic(
|
||||
PANIC_CODES.ARITHMETIC_OVERFLOW,
|
||||
);
|
||||
await expect(this.mock.$tryParseUint((ethers.MaxUint256 + 1n).toString(10))).to.be.revertedWithPanic(
|
||||
PANIC_CODES.ARITHMETIC_OVERFLOW,
|
||||
);
|
||||
});
|
||||
|
||||
it('parseUint invalid character', async function () {
|
||||
await expect(this.mock.$parseUint('0x1')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
|
||||
await expect(this.mock.$parseUint('1f')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
|
||||
await expect(this.mock.$parseUint('-10')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
|
||||
await expect(this.mock.$parseUint('1.0')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
|
||||
await expect(this.mock.$parseUint('1 000')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
|
||||
expect(await this.mock.$tryParseUint('0x1')).to.deep.equal([false, 0n]);
|
||||
expect(await this.mock.$tryParseUint('1f')).to.deep.equal([false, 0n]);
|
||||
expect(await this.mock.$tryParseUint('-10')).to.deep.equal([false, 0n]);
|
||||
expect(await this.mock.$tryParseUint('1.0')).deep.equal([false, 0n]);
|
||||
expect(await this.mock.$tryParseUint('1 000')).deep.equal([false, 0n]);
|
||||
});
|
||||
|
||||
it('parseInt overflow', async function () {
|
||||
await expect(this.mock.$parseInt((ethers.MaxUint256 + 1n).toString(10))).to.be.revertedWithPanic(
|
||||
PANIC_CODES.ARITHMETIC_OVERFLOW,
|
||||
);
|
||||
await expect(this.mock.$parseInt((-ethers.MaxUint256 - 1n).toString(10))).to.be.revertedWithPanic(
|
||||
PANIC_CODES.ARITHMETIC_OVERFLOW,
|
||||
);
|
||||
await expect(this.mock.$tryParseInt((ethers.MaxUint256 + 1n).toString(10))).to.be.revertedWithPanic(
|
||||
PANIC_CODES.ARITHMETIC_OVERFLOW,
|
||||
);
|
||||
await expect(this.mock.$tryParseInt((-ethers.MaxUint256 - 1n).toString(10))).to.be.revertedWithPanic(
|
||||
PANIC_CODES.ARITHMETIC_OVERFLOW,
|
||||
);
|
||||
await expect(this.mock.$parseInt((ethers.MaxInt256 + 1n).toString(10))).to.be.revertedWithCustomError(
|
||||
this.mock,
|
||||
'StringsInvalidChar',
|
||||
);
|
||||
await expect(this.mock.$parseInt((ethers.MinInt256 - 1n).toString(10))).to.be.revertedWithCustomError(
|
||||
this.mock,
|
||||
'StringsInvalidChar',
|
||||
);
|
||||
expect(await this.mock.$tryParseInt((ethers.MaxInt256 + 1n).toString(10))).to.deep.equal([false, 0n]);
|
||||
expect(await this.mock.$tryParseInt((ethers.MinInt256 - 1n).toString(10))).to.deep.equal([false, 0n]);
|
||||
});
|
||||
|
||||
it('parseInt invalid character', async function () {
|
||||
await expect(this.mock.$parseInt('0x1')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
|
||||
await expect(this.mock.$parseInt('1f')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
|
||||
await expect(this.mock.$parseInt('1.0')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
|
||||
await expect(this.mock.$parseInt('1 000')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
|
||||
expect(await this.mock.$tryParseInt('0x1')).to.deep.equal([false, 0n]);
|
||||
expect(await this.mock.$tryParseInt('1f')).to.deep.equal([false, 0n]);
|
||||
expect(await this.mock.$tryParseInt('1.0')).to.deep.equal([false, 0n]);
|
||||
expect(await this.mock.$tryParseInt('1 000')).to.deep.equal([false, 0n]);
|
||||
});
|
||||
|
||||
it('parseHexUint overflow', async function () {
|
||||
await expect(this.mock.$parseHexUint((ethers.MaxUint256 + 1n).toString(16))).to.be.revertedWithPanic(
|
||||
PANIC_CODES.ARITHMETIC_OVERFLOW,
|
||||
);
|
||||
await expect(this.mock.$tryParseHexUint((ethers.MaxUint256 + 1n).toString(16))).to.be.revertedWithPanic(
|
||||
PANIC_CODES.ARITHMETIC_OVERFLOW,
|
||||
);
|
||||
});
|
||||
|
||||
it('parseHexUint invalid character', async function () {
|
||||
await expect(this.mock.$parseHexUint('0123456789abcdefg')).to.be.revertedWithCustomError(
|
||||
this.mock,
|
||||
'StringsInvalidChar',
|
||||
);
|
||||
await expect(this.mock.$parseHexUint('-1')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
|
||||
await expect(this.mock.$parseHexUint('-f')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
|
||||
await expect(this.mock.$parseHexUint('-0xf')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
|
||||
await expect(this.mock.$parseHexUint('1.0')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
|
||||
await expect(this.mock.$parseHexUint('1 000')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
|
||||
expect(await this.mock.$tryParseHexUint('0123456789abcdefg')).to.deep.equal([false, 0n]);
|
||||
expect(await this.mock.$tryParseHexUint('-1')).to.deep.equal([false, 0n]);
|
||||
expect(await this.mock.$tryParseHexUint('-f')).to.deep.equal([false, 0n]);
|
||||
expect(await this.mock.$tryParseHexUint('-0xf')).to.deep.equal([false, 0n]);
|
||||
expect(await this.mock.$tryParseHexUint('1.0')).to.deep.equal([false, 0n]);
|
||||
expect(await this.mock.$tryParseHexUint('1 000')).to.deep.equal([false, 0n]);
|
||||
});
|
||||
|
||||
it('parseAddress invalid format', async function () {
|
||||
for (const addr of [
|
||||
'0x736a507fB2881d6bB62dcA54673CF5295dC07833', // valid
|
||||
'0x736a507fB2881d6-B62dcA54673CF5295dC07833', // invalid char
|
||||
'0x0736a507fB2881d6bB62dcA54673CF5295dC07833', // tooLong
|
||||
'0x36a507fB2881d6bB62dcA54673CF5295dC07833', // tooShort
|
||||
'736a507fB2881d6bB62dcA54673CF5295dC07833', // missingPrefix - supported
|
||||
]) {
|
||||
if (ethers.isAddress(addr)) {
|
||||
expect(await this.mock.$parseAddress(addr)).to.equal(ethers.getAddress(addr));
|
||||
expect(await this.mock.$tryParseAddress(addr)).to.deep.equal([true, ethers.getAddress(addr)]);
|
||||
} else {
|
||||
await expect(this.mock.$parseAddress(addr)).to.be.revertedWithCustomError(
|
||||
this.mock,
|
||||
'StringsInvalidAddressFormat',
|
||||
);
|
||||
expect(await this.mock.$tryParseAddress(addr)).to.deep.equal([false, ethers.ZeroAddress]);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user