Skip to content
Lattice

ERC7786OpenBridgeLib

Git Source

Title: ERC7786OpenBridgeLib

Authors: David Dada <daveproxy80@gmail.com> (https://github.com/dadadave80), Adapted for EIP-2535 from OpenZeppelin community-contracts ERC7786OpenBridge (https://github.com/OpenZeppelin/openzeppelin-community-contracts, commit f7e5f08).

N-of-M ERC-7786 aggregator: sendMessage fans a wrapped message out across M gateways; receiveMessage counts independent gateway attestations and delivers to the recipient once N agree (retry-on-failure, executed-flag reentrancy guard). Both a source gateway and a recipient.

Functions

erc7786OpenBridgeStorage

function erc7786OpenBridgeStorage() internal pure returns (ERC7786OpenBridgeStorage storage $);

Returns

NameTypeDescription
$ERC7786OpenBridgeStorage

__ERC7786OpenBridge_init

Registers the shared IERC7786GatewaySource ERC-165 interface.

function __ERC7786OpenBridge_init() internal;

registerInterface

Writes true to the SHARED IERC7786GatewaySource ERC-165 map slot (0x11967553 → ...).

function registerInterface() internal;

getGateways

function getGateways() internal view returns (address[] memory);

Returns

NameTypeDescription
<none>address[]

getThreshold

function getThreshold() internal view returns (uint8);

Returns

NameTypeDescription
<none>uint8

getRemoteBridge

function getRemoteBridge(bytes memory chain) internal view returns (bytes memory);

Parameters

NameTypeDescription
chainbytes

Returns

NameTypeDescription
<none>bytes

minDirectCoverage

function minDirectCoverage() internal view returns (uint8);

Returns

NameTypeDescription
<none>uint8

supportsAttribute

function supportsAttribute(bytes4) internal pure returns (bool);

Parameters

NameTypeDescription
_bytes4

Returns

NameTypeDescription
<none>bool

addGateway

function addGateway(address gateway) internal;

Parameters

NameTypeDescription
gatewayaddress

removeGateway

function removeGateway(address gateway) internal;

Parameters

NameTypeDescription
gatewayaddress

setThreshold

function setThreshold(uint8 threshold) internal;

Parameters

NameTypeDescription
thresholduint8

setMinDirectCoverage

Sets the M-of-N coverage-awareness knob (issue #77 Q5): when non-zero, sendMessage refuses destinations whose DIRECT registry coverage is below it (set 2+ to hard-refuse M=1 routes; 0 — the default — disables the check entirely, so a diamond without a chain registry keeps working).

function setMinDirectCoverage(uint8 minDirectCoverage_) internal;

Parameters

NameTypeDescription
minDirectCoverage_uint8

registerRemoteBridge

Registers the matching remote bridge for its chain. Reverts with ThresholdViolation while the threshold is still 0, forcing the safe configuration order: gateways, then threshold, then remotes.

function registerRemoteBridge(bytes calldata bridge) internal;

Parameters

NameTypeDescription
bridgebytes

sendMessage

ERC-7786 source: wraps the message and fans it out across all gateways to the matching remote bridge. Rejects value + attributes. sendId = keccak of the per-gateway ids (or 0).

COVERAGE GATE (issue #77 Q5): when the admin has set _minDirectCoverage non-zero, the parsed destination chain must have at least that many DIRECT (non-hub-routed) gateways recorded in the chain registry — OpenBridgeInsufficientCoverage otherwise. The default 0 skips the registry read entirely, so a diamond without a chain registry record (or facet) behaves exactly as before.

function sendMessage(bytes calldata recipient, bytes calldata payload, bytes[] calldata attributes)
        internal
        returns (bytes32 sendId);

Parameters

NameTypeDescription
recipientbytes
payloadbytes
attributesbytes[]

Returns

NameTypeDescription
sendIdbytes32

_resolveRemoteBridge

Parses the destination chain out of recipient ONCE, enforces the coverage gate on it, and resolves the matching remote bridge (split into a helper to stay under the non-via-IR stack limit — see sendMessage for the gate semantics).

function _resolveRemoteBridge(ERC7786OpenBridgeStorage storage $, bytes calldata recipient)
        private
        view
        returns (bytes memory bridge);

Parameters

NameTypeDescription
$ERC7786OpenBridgeStorage
recipientbytes

Returns

NameTypeDescription
bridgebytes

receiveMessage

ERC-7786 recipient: count an attesting gateway and, once the N threshold is met, deliver the unwrapped message to the final recipient. Idempotent per gateway; retryable on failure.

Never executes while the threshold is 0 (an unconfigured bridge): 0 >= 0 would otherwise let any non-gateway caller deliver a forged payload. Gateway attestations are still recorded, so calling again after setThreshold executes the message. Skips rather than reverts, so adapters that cannot redeliver don't lose the attestation.

function receiveMessage(bytes32, bytes calldata sender, bytes calldata payload) internal returns (bytes4);

Parameters

NameTypeDescription
_bytes32
senderbytes
payloadbytes

Returns

NameTypeDescription
<none>bytes4

_fanOut

Sends wrapped to every gateway addressed to bridge; returns keccak of the non-zero ids.

function _fanOut(
        ERC7786OpenBridgeStorage storage $,
        bytes memory bridge,
        bytes memory wrapped,
        bytes[] calldata attributes
    ) private returns (bytes32);

Parameters

NameTypeDescription
$ERC7786OpenBridgeStorage
bridgebytes
wrappedbytes
attributesbytes[]

Returns

NameTypeDescription
<none>bytes32

_execute

Decodes the wrapped payload and delivers it. Sets executed BEFORE the call (reentrancy guard); a failing recipient call rolls that back and leaves the message retryable.

function _execute(OpenBridgeTracker storage t, bytes32 id, bytes calldata payload) private;

Parameters

NameTypeDescription
tOpenBridgeTracker
idbytes32
payloadbytes