Skip to content

Commit 2909098

Browse files
Amxxernestognw
andauthored
ERC-7930 (#5736)
Co-authored-by: Ernesto García <ernestognw@gmail.com>
1 parent 51da8ae commit 2909098

File tree

10 files changed

+597
-114
lines changed

10 files changed

+597
-114
lines changed

.changeset/rich-cows-repair.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openzeppelin-solidity': minor
3+
---
4+
5+
`InteroperableAddress`: Add a library for formatting and parsing ERC-7930 interoperable addresses.

contracts/mocks/Stateless.sol

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SPDX-License-Identifier: MIT
22

3-
pragma solidity ^0.8.24;
3+
pragma solidity ^0.8.26;
44

55
// We keep these imports and a dummy contract just to we can run the test suite after transpilation.
66

@@ -30,6 +30,7 @@ import {ERC1967Utils} from "../proxy/ERC1967/ERC1967Utils.sol";
3030
import {ERC4337Utils} from "../account/utils/draft-ERC4337Utils.sol";
3131
import {ERC7579Utils} from "../account/utils/draft-ERC7579Utils.sol";
3232
import {Heap} from "../utils/structs/Heap.sol";
33+
import {InteroperableAddress} from "../utils/draft-InteroperableAddress.sol";
3334
import {Math} from "../utils/math/Math.sol";
3435
import {MerkleProof} from "../utils/cryptography/MerkleProof.sol";
3536
import {MessageHashUtils} from "../utils/cryptography/MessageHashUtils.sol";

contracts/utils/README.adoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ Miscellaneous contracts and libraries containing utility functions you can use t
3838
* {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].
3939
* {Comparators}: A library that contains comparator functions to use with the {Heap} library.
4040
* {CAIP2}, {CAIP10}: Libraries for formatting and parsing CAIP-2 and CAIP-10 identifiers.
41+
* {InteroperableAddress}: Library for formatting and parsing ERC-7930 interoperable addresses.
4142
* {Blockhash}: A library for accessing historical block hashes beyond the standard 256 block limit utilizing EIP-2935's historical blockhash functionality.
4243
* {Time}: A library that provides helpers for manipulating time-related objects, including a `Delay` type.
43-
44+
4445
[NOTE]
4546
====
4647
Because Solidity does not support generic types, {EnumerableMap} and {EnumerableSet} are specialized to a limited number of key-value types.
@@ -134,6 +135,8 @@ Ethereum contracts have no native concept of an interface, so applications must
134135

135136
{{CAIP10}}
136137

138+
{{InteroperableAddress}}
139+
137140
{{Blockhash}}
138141

139142
{{Time}}
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
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+
}

package-lock.json

Lines changed: 23 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"hardhat-gas-reporter": "^2.1.0",
7676
"hardhat-ignore-warnings": "^0.2.11",
7777
"husky": "^9.1.7",
78+
"interoperable-addresses": "^0.1.3",
7879
"lint-staged": "^16.0.0",
7980
"lodash.startcase": "^4.4.0",
8081
"micromatch": "^4.0.2",

0 commit comments

Comments
 (0)