draft-InteroperableAddress.sol 9.8 KB

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