|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity ^0.8.26; |
| 4 | + |
| 5 | +import {Math} from "./math/Math.sol"; |
| 6 | +import {SafeCast} from "./math/SafeCast.sol"; |
| 7 | +import {Bytes} from "./Bytes.sol"; |
| 8 | +import {Calldata} from "./Calldata.sol"; |
| 9 | + |
| 10 | +/** |
| 11 | + * @dev Helper library to format and parse https://ethereum-magicians.org/t/erc-7930-interoperable-addresses/23365[ERC-7930] interoperable |
| 12 | + * addresses. |
| 13 | + */ |
| 14 | +library InteroperableAddress { |
| 15 | + using SafeCast for uint256; |
| 16 | + using Bytes for bytes; |
| 17 | + |
| 18 | + error InteroperableAddressParsingError(bytes); |
| 19 | + error InteroperableAddressEmptyReferenceAndAddress(); |
| 20 | + |
| 21 | + /** |
| 22 | + * @dev Format an ERC-7930 interoperable address (version 1) from its components `chainType`, `chainReference` |
| 23 | + * and `addr`. This is a generic function that supports any chain type, chain reference and address supported by |
| 24 | + * ERC-7390, including interoperable addresses with empty chain reference or empty address. |
| 25 | + */ |
| 26 | + function formatV1( |
| 27 | + bytes2 chainType, |
| 28 | + bytes memory chainReference, |
| 29 | + bytes memory addr |
| 30 | + ) internal pure returns (bytes memory) { |
| 31 | + require(chainReference.length > 0 || addr.length > 0, InteroperableAddressEmptyReferenceAndAddress()); |
| 32 | + return |
| 33 | + abi.encodePacked( |
| 34 | + bytes2(0x0001), |
| 35 | + chainType, |
| 36 | + chainReference.length.toUint8(), |
| 37 | + chainReference, |
| 38 | + addr.length.toUint8(), |
| 39 | + addr |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @dev Variant of {formatV1-bytes2-bytes-bytes-} specific to EVM chains. Returns the ERC-7930 interoperable |
| 45 | + * address (version 1) for a given chainid and ethereum address. |
| 46 | + */ |
| 47 | + function formatEvmV1(uint256 chainid, address addr) internal pure returns (bytes memory) { |
| 48 | + bytes memory chainReference = _toChainReference(chainid); |
| 49 | + return abi.encodePacked(bytes4(0x00010000), uint8(chainReference.length), chainReference, uint8(20), addr); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @dev Variant of {formatV1-bytes2-bytes-bytes-} that specifies an EVM chain without an address. |
| 54 | + */ |
| 55 | + function formatEvmV1(uint256 chainid) internal pure returns (bytes memory) { |
| 56 | + bytes memory chainReference = _toChainReference(chainid); |
| 57 | + return abi.encodePacked(bytes4(0x00010000), uint8(chainReference.length), chainReference, uint8(0)); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @dev Variant of {formatV1-bytes2-bytes-bytes-} that specifies an EVM address without a chain reference. |
| 62 | + */ |
| 63 | + function formatEvmV1(address addr) internal pure returns (bytes memory) { |
| 64 | + return abi.encodePacked(bytes6(0x000100000014), addr); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @dev Parse a ERC-7930 interoperable address (version 1) into its different components. Reverts if the input is |
| 69 | + * not following a version 1 of ERC-7930 |
| 70 | + */ |
| 71 | + function parseV1( |
| 72 | + bytes memory self |
| 73 | + ) internal pure returns (bytes2 chainType, bytes memory chainReference, bytes memory addr) { |
| 74 | + bool success; |
| 75 | + (success, chainType, chainReference, addr) = tryParseV1(self); |
| 76 | + require(success, InteroperableAddressParsingError(self)); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @dev Variant of {parseV1} that handles calldata slices to reduce memory copy costs. |
| 81 | + */ |
| 82 | + function parseV1Calldata( |
| 83 | + bytes calldata self |
| 84 | + ) internal pure returns (bytes2 chainType, bytes calldata chainReference, bytes calldata addr) { |
| 85 | + bool success; |
| 86 | + (success, chainType, chainReference, addr) = tryParseV1Calldata(self); |
| 87 | + require(success, InteroperableAddressParsingError(self)); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @dev Variant of {parseV1} that does not revert on invalid input. Instead, it returns `false` as the first |
| 92 | + * return value to indicate parsing failure when the input does not follow version 1 of ERC-7930. |
| 93 | + */ |
| 94 | + function tryParseV1( |
| 95 | + bytes memory self |
| 96 | + ) internal pure returns (bool success, bytes2 chainType, bytes memory chainReference, bytes memory addr) { |
| 97 | + unchecked { |
| 98 | + success = true; |
| 99 | + if (self.length < 0x06) return (false, 0x0000, _emptyBytesMemory(), _emptyBytesMemory()); |
| 100 | + |
| 101 | + bytes2 version = _readBytes2(self, 0x00); |
| 102 | + if (version != bytes2(0x0001)) return (false, 0x0000, _emptyBytesMemory(), _emptyBytesMemory()); |
| 103 | + chainType = _readBytes2(self, 0x02); |
| 104 | + |
| 105 | + uint8 chainReferenceLength = uint8(self[0x04]); |
| 106 | + if (self.length < 0x06 + chainReferenceLength) |
| 107 | + return (false, 0x0000, _emptyBytesMemory(), _emptyBytesMemory()); |
| 108 | + chainReference = self.slice(0x05, 0x05 + chainReferenceLength); |
| 109 | + |
| 110 | + uint8 addrLength = uint8(self[0x05 + chainReferenceLength]); |
| 111 | + if (self.length < 0x06 + chainReferenceLength + addrLength) |
| 112 | + return (false, 0x0000, _emptyBytesMemory(), _emptyBytesMemory()); |
| 113 | + addr = self.slice(0x06 + chainReferenceLength, 0x06 + chainReferenceLength + addrLength); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @dev Variant of {tryParseV1} that handles calldata slices to reduce memory copy costs. |
| 119 | + */ |
| 120 | + function tryParseV1Calldata( |
| 121 | + bytes calldata self |
| 122 | + ) internal pure returns (bool success, bytes2 chainType, bytes calldata chainReference, bytes calldata addr) { |
| 123 | + unchecked { |
| 124 | + success = true; |
| 125 | + if (self.length < 0x06) return (false, 0x0000, Calldata.emptyBytes(), Calldata.emptyBytes()); |
| 126 | + |
| 127 | + bytes2 version = _readBytes2Calldata(self, 0x00); |
| 128 | + if (version != bytes2(0x0001)) return (false, 0x0000, Calldata.emptyBytes(), Calldata.emptyBytes()); |
| 129 | + chainType = _readBytes2Calldata(self, 0x02); |
| 130 | + |
| 131 | + uint8 chainReferenceLength = uint8(self[0x04]); |
| 132 | + if (self.length < 0x06 + chainReferenceLength) |
| 133 | + return (false, 0x0000, Calldata.emptyBytes(), Calldata.emptyBytes()); |
| 134 | + chainReference = self[0x05:0x05 + chainReferenceLength]; |
| 135 | + |
| 136 | + uint8 addrLength = uint8(self[0x05 + chainReferenceLength]); |
| 137 | + if (self.length < 0x06 + chainReferenceLength + addrLength) |
| 138 | + return (false, 0x0000, Calldata.emptyBytes(), Calldata.emptyBytes()); |
| 139 | + addr = self[0x06 + chainReferenceLength:0x06 + chainReferenceLength + addrLength]; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @dev Parse a ERC-7930 interoperable address (version 1) corresponding to an EIP-155 chain. The `chainId` and |
| 145 | + * `addr` return values will be zero if the input doesn't include a chainReference or an address, respectively. |
| 146 | + * |
| 147 | + * Requirements: |
| 148 | + * |
| 149 | + * * The input must be a valid ERC-7930 interoperable address (version 1) |
| 150 | + * * The underlying chainType must be "eip-155" |
| 151 | + */ |
| 152 | + function parseEvmV1(bytes memory self) internal pure returns (uint256 chainId, address addr) { |
| 153 | + bool success; |
| 154 | + (success, chainId, addr) = tryParseEvmV1(self); |
| 155 | + require(success, InteroperableAddressParsingError(self)); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * @dev Variant of {parseEvmV1} that handles calldata slices to reduce memory copy costs. |
| 160 | + */ |
| 161 | + function parseEvmV1Calldata(bytes calldata self) internal pure returns (uint256 chainId, address addr) { |
| 162 | + bool success; |
| 163 | + (success, chainId, addr) = tryParseEvmV1Calldata(self); |
| 164 | + require(success, InteroperableAddressParsingError(self)); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * @dev Variant of {parseEvmV1} that does not revert on invalid input. Instead, it returns `false` as the first |
| 169 | + * return value to indicate parsing failure when the input does not follow version 1 of ERC-7930. |
| 170 | + */ |
| 171 | + function tryParseEvmV1(bytes memory self) internal pure returns (bool success, uint256 chainId, address addr) { |
| 172 | + (bool success_, bytes2 chainType_, bytes memory chainReference_, bytes memory addr_) = tryParseV1(self); |
| 173 | + return |
| 174 | + (success_ && |
| 175 | + chainType_ == 0x0000 && |
| 176 | + chainReference_.length < 33 && |
| 177 | + (addr_.length == 0 || addr_.length == 20)) |
| 178 | + ? ( |
| 179 | + true, |
| 180 | + uint256(bytes32(chainReference_)) >> (256 - 8 * chainReference_.length), |
| 181 | + address(bytes20(addr_)) |
| 182 | + ) |
| 183 | + : (false, 0, address(0)); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * @dev Variant of {tryParseEvmV1} that handles calldata slices to reduce memory copy costs. |
| 188 | + */ |
| 189 | + function tryParseEvmV1Calldata( |
| 190 | + bytes calldata self |
| 191 | + ) internal pure returns (bool success, uint256 chainId, address addr) { |
| 192 | + (bool success_, bytes2 chainType_, bytes calldata chainReference_, bytes calldata addr_) = tryParseV1Calldata( |
| 193 | + self |
| 194 | + ); |
| 195 | + return |
| 196 | + (success_ && |
| 197 | + chainType_ == 0x0000 && |
| 198 | + chainReference_.length < 33 && |
| 199 | + (addr_.length == 0 || addr_.length == 20)) |
| 200 | + ? ( |
| 201 | + true, |
| 202 | + uint256(bytes32(chainReference_)) >> (256 - 8 * chainReference_.length), |
| 203 | + address(bytes20(addr_)) |
| 204 | + ) |
| 205 | + : (false, 0, address(0)); |
| 206 | + } |
| 207 | + |
| 208 | + function _toChainReference(uint256 chainid) private pure returns (bytes memory) { |
| 209 | + unchecked { |
| 210 | + // length fits in a uint8: log256(type(uint256).max) is 31 |
| 211 | + uint256 length = Math.log256(chainid) + 1; |
| 212 | + return abi.encodePacked(chainid).slice(32 - length); |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + function _readBytes2(bytes memory buffer, uint256 offset) private pure returns (bytes2 value) { |
| 217 | + // This is not memory safe in the general case, but all calls to this private function are within bounds. |
| 218 | + assembly ("memory-safe") { |
| 219 | + value := shl(240, shr(240, mload(add(add(buffer, 0x20), offset)))) |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + function _readBytes2Calldata(bytes calldata buffer, uint256 offset) private pure returns (bytes2 value) { |
| 224 | + assembly ("memory-safe") { |
| 225 | + value := shl(240, shr(240, calldataload(add(buffer.offset, offset)))) |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + function _emptyBytesMemory() private pure returns (bytes memory result) { |
| 230 | + assembly ("memory-safe") { |
| 231 | + result := 0x60 // mload(0x60) is always 0 |
| 232 | + } |
| 233 | + } |
| 234 | +} |
0 commit comments