|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity ^0.8.26; |
| 4 | + |
| 5 | +// Interfaces |
| 6 | +import {IERC20} from "@openzeppelin/contracts/interfaces/IERC20.sol"; |
| 7 | +import {IERC7802} from "@openzeppelin/contracts/interfaces/draft-IERC7802.sol"; |
| 8 | +import {IERC7786GatewaySource, IERC7786Receiver} from "../interfaces/IERC7786.sol"; |
| 9 | + |
| 10 | +// Utilities |
| 11 | +import {Arrays} from "@openzeppelin/contracts/utils/Arrays.sol"; |
| 12 | +import {BitMaps} from "@openzeppelin/contracts/utils/structs/BitMaps.sol"; |
| 13 | +import {Bytes} from "@openzeppelin/contracts/utils/Bytes.sol"; |
| 14 | +import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol"; |
| 15 | +import {InteroperableAddress} from "@openzeppelin/contracts/utils/draft-InteroperableAddress.sol"; |
| 16 | +import {IndirectCall} from "../utils/IndirectCall.sol"; |
| 17 | + |
| 18 | +contract ERC7802Bridge is IERC7786Receiver { |
| 19 | + using BitMaps for BitMaps.BitMap; |
| 20 | + using InteroperableAddress for bytes; |
| 21 | + |
| 22 | + struct BridgeMetadata { |
| 23 | + address token; |
| 24 | + bool isCustodial; |
| 25 | + mapping(bytes chain => address) gateway; |
| 26 | + mapping(bytes chain => bytes) remote; |
| 27 | + } |
| 28 | + |
| 29 | + mapping(bytes32 bridgeId => BridgeMetadata) private _bridges; |
| 30 | + BitMaps.BitMap private _processed; |
| 31 | + |
| 32 | + event Sent(address token, address from, bytes to, uint256 amount); |
| 33 | + event Received(address token, bytes from, address to, uint256 amount); |
| 34 | + |
| 35 | + // event GatewayRegistered(address indexed gateway, bytes chain); |
| 36 | + // event RemoteBridgeRegistered(address indexed token, bytes chain); |
| 37 | + |
| 38 | + error ERC7802BridgeInvalidBidgeId(bytes32 bridgeId); |
| 39 | + error ERC7802BridgeMissingGateway(bytes32 bridgeId, bytes chain); |
| 40 | + error ERC7802BridgeMissingRemote(bytes32 bridgeId, bytes chain); |
| 41 | + // error ERC7802BridgeImproperChainIdentifier(bytes chain); |
| 42 | + error ERC7802BridgeDuplicate(); |
| 43 | + error ERC7802BridgeInvalidGateway(); |
| 44 | + error ERC7802BridgeInvalidSender(); |
| 45 | + |
| 46 | + function getBridgeEndpoint(bytes32 bridgeId) public returns (address) { |
| 47 | + return IndirectCall.getRelayer(bridgeId); |
| 48 | + } |
| 49 | + |
| 50 | + function getBridgeToken(bytes32 bridgeId) public view returns (address token, bool isCustodial) { |
| 51 | + token = _bridges[bridgeId].token; |
| 52 | + isCustodial = _bridges[bridgeId].isCustodial; |
| 53 | + if (token == address(0)) revert ERC7802BridgeInvalidBidgeId(bridgeId); |
| 54 | + } |
| 55 | + |
| 56 | + function getBridgeGateway(bytes32 bridgeId, bytes memory chain) public view returns (address) { |
| 57 | + address result = _bridges[bridgeId].gateway[chain]; |
| 58 | + if (result == address(0)) revert ERC7802BridgeMissingGateway(bridgeId, chain); |
| 59 | + return result; |
| 60 | + } |
| 61 | + |
| 62 | + function getBridgeRemote(bytes32 bridgeId, bytes memory chain) public view returns (bytes memory) { |
| 63 | + bytes memory result = _bridges[bridgeId].remote[chain]; |
| 64 | + if (result.length == 0) revert ERC7802BridgeMissingRemote(bridgeId, chain); |
| 65 | + return result; |
| 66 | + } |
| 67 | + |
| 68 | + struct Foreign { |
| 69 | + bytes32 id; |
| 70 | + address gateway; |
| 71 | + bytes remote; |
| 72 | + } |
| 73 | + function createBridge(address token, bool isCustodial, Foreign[] calldata foreign) public returns (bytes32) { |
| 74 | + bytes32[] memory ids = new bytes32[](foreign.length + 1); |
| 75 | + bytes32[] memory links = new bytes32[](foreign.length); |
| 76 | + for (uint256 i = 0; i < foreign.length; ++i) { |
| 77 | + ids[i] = foreign[i].id; |
| 78 | + links[i] = keccak256( |
| 79 | + abi.encode(InteroperableAddress.formatEvmV1(block.chainid, foreign[i].gateway), foreign[i].remote) |
| 80 | + ); |
| 81 | + } |
| 82 | + ids[foreign.length] = keccak256( |
| 83 | + abi.encode( |
| 84 | + InteroperableAddress.formatEvmV1(block.chainid, token), // bytes token |
| 85 | + bytes32(SafeCast.toUint(isCustodial)), // bytes32 tokenOptions |
| 86 | + Arrays.sort(links) |
| 87 | + ) |
| 88 | + ); |
| 89 | + |
| 90 | + bytes32 bridgeId = keccak256(abi.encode(Arrays.sort(ids))); |
| 91 | + |
| 92 | + // Should we check for collision. I don't think that is necessary |
| 93 | + BridgeMetadata storage details = _bridges[bridgeId]; |
| 94 | + details.token = token; |
| 95 | + details.isCustodial = isCustodial; |
| 96 | + |
| 97 | + for (uint256 i = 0; i < foreign.length; ++i) { |
| 98 | + (bytes2 chainType, bytes memory chainReference, ) = foreign[i].remote.parseV1(); |
| 99 | + bytes memory chain = InteroperableAddress.formatV1(chainType, chainReference, ""); |
| 100 | + details.gateway[chain] = foreign[i].gateway; |
| 101 | + details.remote[chain] = foreign[i].remote; |
| 102 | + } |
| 103 | + |
| 104 | + return bridgeId; |
| 105 | + } |
| 106 | + |
| 107 | + function send( |
| 108 | + bytes32 bridgeId, |
| 109 | + bytes memory to, |
| 110 | + uint256 amount, |
| 111 | + bytes[] memory attributes |
| 112 | + ) public payable virtual returns (bytes32) { |
| 113 | + address token = _fetchTokens(bridgeId, msg.sender, amount); |
| 114 | + |
| 115 | + // identify destination chain |
| 116 | + (bytes2 chainType, bytes memory chainReference, bytes memory recipient) = to.parseV1(); |
| 117 | + bytes memory destChain = InteroperableAddress.formatV1(chainType, chainReference, ""); |
| 118 | + |
| 119 | + // get details for that bridge: gateway, remote bridge, remote token |
| 120 | + address gateway = getBridgeGateway(bridgeId, destChain); |
| 121 | + bytes memory bridge = getBridgeRemote(bridgeId, destChain); |
| 122 | + |
| 123 | + // prepare payload |
| 124 | + bytes memory payload = abi.encode( |
| 125 | + bridgeId, |
| 126 | + InteroperableAddress.formatEvmV1(block.chainid, msg.sender), |
| 127 | + recipient, |
| 128 | + amount |
| 129 | + ); |
| 130 | + |
| 131 | + // send crosschain signal |
| 132 | + bytes32 sendId = IERC7786GatewaySource(gateway).sendMessage{value: msg.value}(bridge, payload, attributes); |
| 133 | + emit Sent(token, msg.sender, to, amount); |
| 134 | + |
| 135 | + return sendId; |
| 136 | + } |
| 137 | + |
| 138 | + function executeMessage( |
| 139 | + bytes32 receiveId, |
| 140 | + bytes memory sender, |
| 141 | + bytes memory payload, |
| 142 | + bytes[] memory /*attributes*/ |
| 143 | + ) public payable virtual returns (bytes4) { |
| 144 | + // prevent duplicate |
| 145 | + require(!_processed.get(uint256(receiveId)), ERC7802BridgeDuplicate()); |
| 146 | + _processed.set(uint256(receiveId)); |
| 147 | + |
| 148 | + // parse payload |
| 149 | + (bytes32 bridgeId, bytes memory from, bytes memory recipient, uint256 amount) = abi.decode( |
| 150 | + payload, |
| 151 | + (bytes32, bytes, bytes, uint256) |
| 152 | + ); |
| 153 | + |
| 154 | + // identify source chain and validate corresponding gateway |
| 155 | + (bytes2 chainType, bytes memory chainReference, ) = from.parseV1(); |
| 156 | + bytes memory srcChain = InteroperableAddress.formatV1(chainType, chainReference, ""); |
| 157 | + |
| 158 | + require(msg.sender == getBridgeGateway(bridgeId, srcChain), ERC7802BridgeInvalidGateway()); |
| 159 | + require(Bytes.equal(sender, getBridgeRemote(bridgeId, srcChain)), ERC7802BridgeInvalidSender()); |
| 160 | + |
| 161 | + // get recipient |
| 162 | + address to = address(bytes20(recipient)); |
| 163 | + |
| 164 | + // distribute bridged tokens |
| 165 | + address token = _distributeTokens(bridgeId, to, amount); |
| 166 | + emit Received(token, from, to, amount); |
| 167 | + |
| 168 | + return IERC7786Receiver.executeMessage.selector; |
| 169 | + } |
| 170 | + |
| 171 | + function _fetchTokens(bytes32 bridgeId, address from, uint256 amount) internal virtual returns (address) { |
| 172 | + (address token, bool isCustodial) = getBridgeToken(bridgeId); |
| 173 | + if (isCustodial) { |
| 174 | + (bool success, bytes memory returndata) = IndirectCall.indirectCall( |
| 175 | + token, |
| 176 | + abi.encodeCall(IERC20.transferFrom, (from, getBridgeEndpoint(bridgeId), amount)), |
| 177 | + bridgeId |
| 178 | + ); |
| 179 | + require(success && (returndata.length == 0 ? token.code.length == 0 : uint256(bytes32(returndata)) == 1)); |
| 180 | + } else { |
| 181 | + (bool success, ) = IndirectCall.indirectCall( |
| 182 | + token, |
| 183 | + abi.encodeCall(IERC7802.crosschainBurn, (from, amount)), |
| 184 | + bridgeId |
| 185 | + ); |
| 186 | + require(success); |
| 187 | + } |
| 188 | + return token; |
| 189 | + } |
| 190 | + |
| 191 | + function _distributeTokens(bytes32 bridgeId, address to, uint256 amount) internal virtual returns (address) { |
| 192 | + (address token, bool isCustodial) = getBridgeToken(bridgeId); |
| 193 | + if (isCustodial) { |
| 194 | + (bool success, bytes memory returndata) = IndirectCall.indirectCall( |
| 195 | + token, |
| 196 | + abi.encodeCall(IERC20.transfer, (to, amount)), |
| 197 | + bridgeId |
| 198 | + ); |
| 199 | + require(success && (returndata.length == 0 ? token.code.length == 0 : uint256(bytes32(returndata)) == 1)); |
| 200 | + } else { |
| 201 | + (bool success, ) = IndirectCall.indirectCall( |
| 202 | + token, |
| 203 | + abi.encodeCall(IERC7802.crosschainMint, (to, amount)), |
| 204 | + bridgeId |
| 205 | + ); |
| 206 | + require(success); |
| 207 | + } |
| 208 | + return token; |
| 209 | + } |
| 210 | +} |
0 commit comments