Bytes library and CAIP2/CAIP10 helpers (#5252)
Co-authored-by: cairo <cairoeth@protonmail.com> Co-authored-by: Ernesto García <ernestognw@gmail.com> Co-authored-by: Arr00 <13561405+arr00@users.noreply.github.com>
This commit is contained in:
@ -9,6 +9,9 @@ import {Arrays} from "../utils/Arrays.sol";
|
||||
import {AuthorityUtils} from "../access/manager/AuthorityUtils.sol";
|
||||
import {Base64} from "../utils/Base64.sol";
|
||||
import {BitMaps} from "../utils/structs/BitMaps.sol";
|
||||
import {Bytes} from "../utils/Bytes.sol";
|
||||
import {CAIP2} from "../utils/CAIP2.sol";
|
||||
import {CAIP10} from "../utils/CAIP10.sol";
|
||||
import {Checkpoints} from "../utils/structs/Checkpoints.sol";
|
||||
import {CircularBuffer} from "../utils/structs/CircularBuffer.sol";
|
||||
import {Clones} from "../proxy/Clones.sol";
|
||||
|
||||
115
contracts/utils/Bytes.sol
Normal file
115
contracts/utils/Bytes.sol
Normal file
@ -0,0 +1,115 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {Math} from "./math/Math.sol";
|
||||
|
||||
/**
|
||||
* @dev Bytes operations.
|
||||
*/
|
||||
library Bytes {
|
||||
/**
|
||||
* @dev Forward search for `s` in `buffer`
|
||||
* * If `s` is present in the buffer, returns the index of the first instance
|
||||
* * If `s` is not present in the buffer, returns type(uint256).max
|
||||
*
|
||||
* NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf[Javascript's `Array.indexOf`]
|
||||
*/
|
||||
function indexOf(bytes memory buffer, bytes1 s) internal pure returns (uint256) {
|
||||
return indexOf(buffer, s, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Forward search for `s` in `buffer` starting at position `pos`
|
||||
* * If `s` is present in the buffer (at or after `pos`), returns the index of the next instance
|
||||
* * If `s` is not present in the buffer (at or after `pos`), returns type(uint256).max
|
||||
*
|
||||
* NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf[Javascript's `Array.indexOf`]
|
||||
*/
|
||||
function indexOf(bytes memory buffer, bytes1 s, uint256 pos) internal pure returns (uint256) {
|
||||
unchecked {
|
||||
uint256 length = buffer.length;
|
||||
for (uint256 i = pos; i < length; ++i) {
|
||||
if (bytes1(_unsafeReadBytesOffset(buffer, i)) == s) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return type(uint256).max;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Backward search for `s` in `buffer`
|
||||
* * If `s` is present in the buffer, returns the index of the last instance
|
||||
* * If `s` is not present in the buffer, returns type(uint256).max
|
||||
*
|
||||
* NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf[Javascript's `Array.lastIndexOf`]
|
||||
*/
|
||||
function lastIndexOf(bytes memory buffer, bytes1 s) internal pure returns (uint256) {
|
||||
return lastIndexOf(buffer, s, type(uint256).max);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Backward search for `s` in `buffer` starting at position `pos`
|
||||
* * If `s` is present in the buffer (at or before `pos`), returns the index of the previous instance
|
||||
* * If `s` is not present in the buffer (at or before `pos`), returns type(uint256).max
|
||||
*
|
||||
* NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf[Javascript's `Array.lastIndexOf`]
|
||||
*/
|
||||
function lastIndexOf(bytes memory buffer, bytes1 s, uint256 pos) internal pure returns (uint256) {
|
||||
unchecked {
|
||||
uint256 length = buffer.length;
|
||||
// NOTE here we cannot do `i = Math.min(pos + 1, length)` because `pos + 1` could overflow
|
||||
for (uint256 i = Math.min(pos, length - 1) + 1; i > 0; --i) {
|
||||
if (bytes1(_unsafeReadBytesOffset(buffer, i - 1)) == s) {
|
||||
return i - 1;
|
||||
}
|
||||
}
|
||||
return type(uint256).max;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Copies the content of `buffer`, from `start` (included) to the end of `buffer` into a new bytes object in
|
||||
* memory.
|
||||
*
|
||||
* NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice[Javascript's `Array.slice`]
|
||||
*/
|
||||
function slice(bytes memory buffer, uint256 start) internal pure returns (bytes memory) {
|
||||
return slice(buffer, start, buffer.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Copies the content of `buffer`, from `start` (included) to `end` (excluded) into a new bytes object in
|
||||
* memory.
|
||||
*
|
||||
* NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice[Javascript's `Array.slice`]
|
||||
*/
|
||||
function slice(bytes memory buffer, uint256 start, uint256 end) internal pure returns (bytes memory) {
|
||||
// sanitize
|
||||
uint256 length = buffer.length;
|
||||
end = Math.min(end, length);
|
||||
start = Math.min(start, end);
|
||||
|
||||
// allocate and copy
|
||||
bytes memory result = new bytes(end - start);
|
||||
assembly ("memory-safe") {
|
||||
mcopy(add(result, 0x20), add(buffer, add(start, 0x20)), sub(end, start))
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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)))
|
||||
}
|
||||
}
|
||||
}
|
||||
49
contracts/utils/CAIP10.sol
Normal file
49
contracts/utils/CAIP10.sol
Normal file
@ -0,0 +1,49 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {SafeCast} from "./math/SafeCast.sol";
|
||||
import {Bytes} from "./Bytes.sol";
|
||||
import {CAIP2} from "./CAIP2.sol";
|
||||
import {Strings} from "./Strings.sol";
|
||||
|
||||
/**
|
||||
* @dev Helper library to format and parse CAIP-10 identifiers
|
||||
*
|
||||
* https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-10.md[CAIP-10] defines account identifiers as:
|
||||
* account_id: chain_id + ":" + account_address
|
||||
* chain_id: [-a-z0-9]{3,8}:[-_a-zA-Z0-9]{1,32} (See {CAIP2})
|
||||
* account_address: [-.%a-zA-Z0-9]{1,128}
|
||||
*/
|
||||
library CAIP10 {
|
||||
using SafeCast for uint256;
|
||||
using Strings for address;
|
||||
using Bytes for bytes;
|
||||
|
||||
/// @dev Return the CAIP-10 identifier for an account on the current (local) chain.
|
||||
function local(address account) internal view returns (string memory) {
|
||||
return format(CAIP2.local(), account.toChecksumHexString());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Return the CAIP-10 identifier for a given caip2 chain and account.
|
||||
*
|
||||
* NOTE: This function does not verify that the inputs are properly formatted.
|
||||
*/
|
||||
function format(string memory caip2, string memory account) internal pure returns (string memory) {
|
||||
return string.concat(caip2, ":", account);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Parse a CAIP-10 identifier into its components.
|
||||
*
|
||||
* NOTE: This function does not verify that the CAIP-10 input is properly formatted. The `caip2` return can be
|
||||
* parsed using the {CAIP2} library.
|
||||
*/
|
||||
function parse(string memory caip10) internal pure returns (string memory caip2, string memory account) {
|
||||
bytes memory buffer = bytes(caip10);
|
||||
|
||||
uint256 pos = buffer.lastIndexOf(":");
|
||||
return (string(buffer.slice(0, pos)), string(buffer.slice(pos + 1)));
|
||||
}
|
||||
}
|
||||
47
contracts/utils/CAIP2.sol
Normal file
47
contracts/utils/CAIP2.sol
Normal file
@ -0,0 +1,47 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {SafeCast} from "./math/SafeCast.sol";
|
||||
import {Bytes} from "./Bytes.sol";
|
||||
import {Strings} from "./Strings.sol";
|
||||
|
||||
/**
|
||||
* @dev Helper library to format and parse CAIP-2 identifiers
|
||||
*
|
||||
* https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-2.md[CAIP-2] defines chain identifiers as:
|
||||
* chain_id: namespace + ":" + reference
|
||||
* namespace: [-a-z0-9]{3,8}
|
||||
* reference: [-_a-zA-Z0-9]{1,32}
|
||||
*/
|
||||
library CAIP2 {
|
||||
using SafeCast for uint256;
|
||||
using Strings for uint256;
|
||||
using Bytes for bytes;
|
||||
|
||||
/// @dev Return the CAIP-2 identifier for the current (local) chain.
|
||||
function local() internal view returns (string memory) {
|
||||
return format("eip155", block.chainid.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Return the CAIP-2 identifier for a given namespace and reference.
|
||||
*
|
||||
* NOTE: This function does not verify that the inputs are properly formatted.
|
||||
*/
|
||||
function format(string memory namespace, string memory ref) internal pure returns (string memory) {
|
||||
return string.concat(namespace, ":", ref);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Parse a CAIP-2 identifier into its components.
|
||||
*
|
||||
* NOTE: This function does not verify that the CAIP-2 input is properly formatted.
|
||||
*/
|
||||
function parse(string memory caip2) internal pure returns (string memory namespace, string memory ref) {
|
||||
bytes memory buffer = bytes(caip2);
|
||||
|
||||
uint256 pos = buffer.indexOf(":");
|
||||
return (string(buffer.slice(0, pos)), string(buffer.slice(pos + 1)));
|
||||
}
|
||||
}
|
||||
@ -31,6 +31,7 @@ Miscellaneous contracts and libraries containing utility functions you can use t
|
||||
* {Address}: Collection of functions for overloading Solidity's https://docs.soliditylang.org/en/latest/types.html#address[`address`] type.
|
||||
* {Arrays}: Collection of functions that operate on https://docs.soliditylang.org/en/latest/types.html#arrays[`arrays`].
|
||||
* {Base64}: On-chain base64 and base64URL encoding according to https://datatracker.ietf.org/doc/html/rfc4648[RFC-4648].
|
||||
* {Bytes}: Common operations on bytes objects.
|
||||
* {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.
|
||||
@ -41,6 +42,7 @@ Miscellaneous contracts and libraries containing utility functions you can use t
|
||||
* {Packing}: A library for packing and unpacking multiple values into bytes32
|
||||
* {Panic}: A library to revert with https://docs.soliditylang.org/en/v0.8.20/control-structures.html#panic-via-assert-and-error-via-require[Solidity panic codes].
|
||||
* {Comparators}: A library that contains comparator functions to use with with the {Heap} library.
|
||||
* {CAIP2}, {CAIP10}: Libraries for formatting and parsing CAIP-2 and CAIP-10 identifiers.
|
||||
|
||||
[NOTE]
|
||||
====
|
||||
|
||||
Reference in New Issue
Block a user