Skip to content
Lattice

InteroperableAddress

Git Source

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

NameTypeDescription
chainTypebytes2
chainReferencebytes
addrbytes

Returns

NameTypeDescription
<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

NameTypeDescription
chainiduint256
addraddress

Returns

NameTypeDescription
<none>bytes

formatEvmV1

Variant of formatV1 that specifies an EVM chain without an address.

function formatEvmV1(uint256 chainid) internal pure returns (bytes memory);

Parameters

NameTypeDescription
chainiduint256

Returns

NameTypeDescription
<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

NameTypeDescription
addraddress

Returns

NameTypeDescription
<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

NameTypeDescription
selfbytes

Returns

NameTypeDescription
chainTypebytes2
chainReferencebytes
addrbytes

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

NameTypeDescription
selfbytes

Returns

NameTypeDescription
chainTypebytes2
chainReferencebytes
addrbytes

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

NameTypeDescription
selfbytes

Returns

NameTypeDescription
successbool
chainTypebytes2
chainReferencebytes
addrbytes

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

NameTypeDescription
selfbytes

Returns

NameTypeDescription
successbool
chainTypebytes2
chainReferencebytes
addrbytes

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

NameTypeDescription
selfbytes

Returns

NameTypeDescription
chainIduint256
addraddress

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

NameTypeDescription
selfbytes

Returns

NameTypeDescription
chainIduint256
addraddress

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

NameTypeDescription
selfbytes

Returns

NameTypeDescription
successbool
chainIduint256
addraddress

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

NameTypeDescription
selfbytes

Returns

NameTypeDescription
successbool
chainIduint256
addraddress

_toChainReference

function _toChainReference(uint256 chainid) private pure returns (bytes memory);

Parameters

NameTypeDescription
chainiduint256

Returns

NameTypeDescription
<none>bytes

_readBytes2

function _readBytes2(bytes memory buffer, uint256 offset) private pure returns (bytes2 value);

Parameters

NameTypeDescription
bufferbytes
offsetuint256

Returns

NameTypeDescription
valuebytes2

_readBytes2Calldata

function _readBytes2Calldata(bytes calldata buffer, uint256 offset) private pure returns (bytes2 value);

Parameters

NameTypeDescription
bufferbytes
offsetuint256

Returns

NameTypeDescription
valuebytes2

_emptyBytesMemory

function _emptyBytesMemory() private pure returns (bytes memory result);

Returns

NameTypeDescription
resultbytes

Errors

InteroperableAddressParsingError

error InteroperableAddressParsingError(bytes);

Parameters

NameTypeDescription
_bytes

InteroperableAddressEmptyReferenceAndAddress

error InteroperableAddressEmptyReferenceAndAddress();