InteroperableAddress
Author: Modified from OpenZeppelin (https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/draft-InteroperableAddress.sol)
Helper library to format and parse https://ethereum-magicians.org/t/erc-7930-interoperable-addresses/23365[ERC-7930] interoperable addresses.
Functions
formatV1
Format an ERC-7930 interoperable address (version 1) from its components chainType, chainReference
and addr. This is a generic function that supports any chain type, chain reference and address supported by
ERC-7930, including interoperable addresses with empty chain reference or empty address.
function formatV1(bytes2 chainType, bytes memory chainReference, bytes memory addr)
internal
pure
returns (bytes memory);Parameters
| Name | Type | Description |
|---|---|---|
| chainType | bytes2 | |
| chainReference | bytes | |
| addr | bytes |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | bytes |
formatEvmV1
Variant of formatV1 specific to EVM chains. Returns the ERC-7930 interoperable
address (version 1) for a given chainid and ethereum address.
function formatEvmV1(uint256 chainid, address addr) internal pure returns (bytes memory);Parameters
| Name | Type | Description |
|---|---|---|
| chainid | uint256 | |
| addr | address |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | bytes |
formatEvmV1
Variant of formatV1 that specifies an EVM chain without an address.
function formatEvmV1(uint256 chainid) internal pure returns (bytes memory);Parameters
| Name | Type | Description |
|---|---|---|
| chainid | uint256 |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | bytes |
formatEvmV1
Variant of formatV1 that specifies an EVM address without a chain reference.
function formatEvmV1(address addr) internal pure returns (bytes memory);Parameters
| Name | Type | Description |
|---|---|---|
| addr | address |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | bytes |
parseV1
Parse a ERC-7930 interoperable address (version 1) into its different components. Reverts if the input is not following a version 1 of ERC-7930.
NOTE: Trailing bytes after a valid v1 encoding are ignored. The same decoded address may therefore correspond to multiple distinct input byte strings.
function parseV1(bytes memory self)
internal
pure
returns (bytes2 chainType, bytes memory chainReference, bytes memory addr);Parameters
| Name | Type | Description |
|---|---|---|
| self | bytes |
Returns
| Name | Type | Description |
|---|---|---|
| chainType | bytes2 | |
| chainReference | bytes | |
| addr | bytes |
parseV1Calldata
Variant of parseV1 that handles calldata slices to reduce memory copy costs.
function parseV1Calldata(bytes calldata self)
internal
pure
returns (bytes2 chainType, bytes calldata chainReference, bytes calldata addr);Parameters
| Name | Type | Description |
|---|---|---|
| self | bytes |
Returns
| Name | Type | Description |
|---|---|---|
| chainType | bytes2 | |
| chainReference | bytes | |
| addr | bytes |
tryParseV1
Variant of parseV1 that does not revert on invalid input. Instead, it returns false as the first
return value to indicate parsing failure when the input does not follow version 1 of ERC-7930.
function tryParseV1(bytes memory self)
internal
pure
returns (bool success, bytes2 chainType, bytes memory chainReference, bytes memory addr);Parameters
| Name | Type | Description |
|---|---|---|
| self | bytes |
Returns
| Name | Type | Description |
|---|---|---|
| success | bool | |
| chainType | bytes2 | |
| chainReference | bytes | |
| addr | bytes |
tryParseV1Calldata
Variant of tryParseV1 that handles calldata slices to reduce memory copy costs.
function tryParseV1Calldata(bytes calldata self)
internal
pure
returns (bool success, bytes2 chainType, bytes calldata chainReference, bytes calldata addr);Parameters
| Name | Type | Description |
|---|---|---|
| self | bytes |
Returns
| Name | Type | Description |
|---|---|---|
| success | bool | |
| chainType | bytes2 | |
| chainReference | bytes | |
| addr | bytes |
parseEvmV1
Parse a ERC-7930 interoperable address (version 1) corresponding to an EIP-155 chain. The chainId and
addr return values will be zero if the input doesn't include a chainReference or an address, respectively.
NOTE: Trailing bytes after a valid v1 encoding are ignored. The same decoded (chainId, addr) may therefore correspond to multiple distinct input byte strings.
Requirements:
- The input must be a valid ERC-7930 interoperable address (version 1)
- The underlying chainType must be "eip-155"
function parseEvmV1(bytes memory self) internal pure returns (uint256 chainId, address addr);Parameters
| Name | Type | Description |
|---|---|---|
| self | bytes |
Returns
| Name | Type | Description |
|---|---|---|
| chainId | uint256 | |
| addr | address |
parseEvmV1Calldata
Variant of parseEvmV1 that handles calldata slices to reduce memory copy costs.
function parseEvmV1Calldata(bytes calldata self) internal pure returns (uint256 chainId, address addr);Parameters
| Name | Type | Description |
|---|---|---|
| self | bytes |
Returns
| Name | Type | Description |
|---|---|---|
| chainId | uint256 | |
| addr | address |
tryParseEvmV1
Variant of parseEvmV1 that does not revert on invalid input. Instead, it returns false as the first
return value to indicate parsing failure when the input does not follow version 1 of ERC-7930.
function tryParseEvmV1(bytes memory self) internal pure returns (bool success, uint256 chainId, address addr);Parameters
| Name | Type | Description |
|---|---|---|
| self | bytes |
Returns
| Name | Type | Description |
|---|---|---|
| success | bool | |
| chainId | uint256 | |
| addr | address |
tryParseEvmV1Calldata
Variant of tryParseEvmV1 that handles calldata slices to reduce memory copy costs.
function tryParseEvmV1Calldata(bytes calldata self)
internal
pure
returns (bool success, uint256 chainId, address addr);Parameters
| Name | Type | Description |
|---|---|---|
| self | bytes |
Returns
| Name | Type | Description |
|---|---|---|
| success | bool | |
| chainId | uint256 | |
| addr | address |
_toChainReference
function _toChainReference(uint256 chainid) private pure returns (bytes memory);Parameters
| Name | Type | Description |
|---|---|---|
| chainid | uint256 |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | bytes |
_readBytes2
function _readBytes2(bytes memory buffer, uint256 offset) private pure returns (bytes2 value);Parameters
| Name | Type | Description |
|---|---|---|
| buffer | bytes | |
| offset | uint256 |
Returns
| Name | Type | Description |
|---|---|---|
| value | bytes2 |
_readBytes2Calldata
function _readBytes2Calldata(bytes calldata buffer, uint256 offset) private pure returns (bytes2 value);Parameters
| Name | Type | Description |
|---|---|---|
| buffer | bytes | |
| offset | uint256 |
Returns
| Name | Type | Description |
|---|---|---|
| value | bytes2 |
_emptyBytesMemory
function _emptyBytesMemory() private pure returns (bytes memory result);Returns
| Name | Type | Description |
|---|---|---|
| result | bytes |
Errors
InteroperableAddressParsingError
error InteroperableAddressParsingError(bytes);Parameters
| Name | Type | Description |
|---|---|---|
| _ | bytes |
InteroperableAddressEmptyReferenceAndAddress
error InteroperableAddressEmptyReferenceAndAddress();