|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity ^0.8.26; |
| 4 | + |
| 5 | +import {IERC7786GatewaySource} from "../interfaces/draft-IERC7786.sol"; |
| 6 | +import {InteroperableAddress} from "../utils/draft-InteroperableAddress.sol"; |
| 7 | +import {Bytes} from "../utils/Bytes.sol"; |
| 8 | +import {ERC7786Recipient} from "./ERC7786Recipient.sol"; |
| 9 | + |
| 10 | +/** |
| 11 | + * @dev Core bridging mechanism. |
| 12 | + * |
| 13 | + * This contract contains the logic to register and send messages to counterparts on remote chains using ERC-7786 |
| 14 | + * gateways. It ensure received messages originate from a counterpart. This is the base of token bridges such as |
| 15 | + * {BridgeERC20Core}. |
| 16 | + * |
| 17 | + * Contracts that inherit from this contract can use the internal {_sendMessageToCounterpart} to send messages to their |
| 18 | + * counterpart on a foreign chain. They must override the {_processMessage} function to handle messages that have |
| 19 | + * been verified. |
| 20 | + */ |
| 21 | +abstract contract CrosschainLinked is ERC7786Recipient { |
| 22 | + using Bytes for bytes; |
| 23 | + using InteroperableAddress for bytes; |
| 24 | + |
| 25 | + struct Link { |
| 26 | + address gateway; |
| 27 | + bytes counterpart; // Full InteroperableAddress (chain ref + address) |
| 28 | + } |
| 29 | + mapping(bytes chain => Link) private _links; |
| 30 | + |
| 31 | + /** |
| 32 | + * @dev Emitted when a new link is registered. |
| 33 | + * |
| 34 | + * Note: the `counterpart` argument is a full InteroperableAddress (chain ref + address). |
| 35 | + */ |
| 36 | + event LinkRegistered(address gateway, bytes counterpart); |
| 37 | + |
| 38 | + /** |
| 39 | + * @dev Reverted when trying to register a link for a chain that is already registered. |
| 40 | + * |
| 41 | + * Note: the `chain` argument is a "chain-only" InteroperableAddress (empty address). |
| 42 | + */ |
| 43 | + error LinkAlreadyRegistered(bytes chain); |
| 44 | + |
| 45 | + constructor(Link[] memory links) { |
| 46 | + for (uint256 i = 0; i < links.length; ++i) { |
| 47 | + _setLink(links[i].gateway, links[i].counterpart, false); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @dev Returns the ERC-7786 gateway used for sending and receiving cross-chain messages to a given chain. |
| 53 | + * |
| 54 | + * Note: The `chain` parameter is a "chain-only" InteroperableAddress (empty address) and the `counterpart` returns |
| 55 | + * the full InteroperableAddress (chain ref + address) that is on `chain`. |
| 56 | + */ |
| 57 | + function getLink(bytes memory chain) public view virtual returns (address gateway, bytes memory counterpart) { |
| 58 | + Link storage self = _links[chain]; |
| 59 | + return (self.gateway, self.counterpart); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @dev Internal setter to change the ERC-7786 gateway and counterpart for a given chain. Called at construction. |
| 64 | + * |
| 65 | + * Note: The `counterpart` parameter is the full InteroperableAddress (chain ref + address). |
| 66 | + */ |
| 67 | + function _setLink(address gateway, bytes memory counterpart, bool allowOverride) internal virtual { |
| 68 | + // Sanity check, this should revert if gateway is not an ERC-7786 implementation. Note that since |
| 69 | + // supportsAttribute returns data, an EOA would fail that test (nothing returned). |
| 70 | + IERC7786GatewaySource(gateway).supportsAttribute(bytes4(0)); |
| 71 | + |
| 72 | + bytes memory chain = _extractChain(counterpart); |
| 73 | + if (allowOverride || _links[chain].gateway == address(0)) { |
| 74 | + _links[chain] = Link(gateway, counterpart); |
| 75 | + emit LinkRegistered(gateway, counterpart); |
| 76 | + } else { |
| 77 | + revert LinkAlreadyRegistered(chain); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @dev Internal messaging function |
| 83 | + * |
| 84 | + * Note: The `chain` parameter is a "chain-only" InteroperableAddress (empty address). |
| 85 | + */ |
| 86 | + function _sendMessageToCounterpart( |
| 87 | + bytes memory chain, |
| 88 | + bytes memory payload, |
| 89 | + bytes[] memory attributes |
| 90 | + ) internal virtual returns (bytes32) { |
| 91 | + (address gateway, bytes memory counterpart) = getLink(chain); |
| 92 | + return IERC7786GatewaySource(gateway).sendMessage(counterpart, payload, attributes); |
| 93 | + } |
| 94 | + |
| 95 | + /// @inheritdoc ERC7786Recipient |
| 96 | + function _isAuthorizedGateway( |
| 97 | + address instance, |
| 98 | + bytes calldata sender |
| 99 | + ) internal view virtual override returns (bool) { |
| 100 | + (address gateway, bytes memory router) = getLink(_extractChain(sender)); |
| 101 | + return instance == gateway && sender.equal(router); |
| 102 | + } |
| 103 | + |
| 104 | + function _extractChain(bytes memory self) private pure returns (bytes memory) { |
| 105 | + (bytes2 chainType, bytes memory chainReference, ) = self.parseV1(); |
| 106 | + return InteroperableAddress.formatV1(chainType, chainReference, hex""); |
| 107 | + } |
| 108 | +} |
0 commit comments