|
| 1 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | +/* |
| 3 | + This file is part of The Colony Network. |
| 4 | +
|
| 5 | + The Colony Network is free software: you can redistribute it and/or modify |
| 6 | + it under the terms of the GNU General Public License as published by |
| 7 | + the Free Software Foundation, either version 3 of the License, or |
| 8 | + (at your option) any later version. |
| 9 | +
|
| 10 | + The Colony Network is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + GNU General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU General Public License |
| 16 | + along with The Colony Network. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +*/ |
| 18 | + |
| 19 | +pragma solidity 0.8.23; |
| 20 | + |
| 21 | +import { IWormhole } from "../../lib/wormhole/ethereum/contracts/interfaces/IWormhole.sol"; |
| 22 | +import { IColonyNetwork } from "../colonyNetwork/IColonyNetwork.sol"; |
| 23 | +import { IColonyBridge } from "./IColonyBridge.sol"; |
| 24 | +import { DSAuth } from "../../lib/dappsys/auth.sol"; |
| 25 | + |
| 26 | +contract WormholeBridgeForColony is DSAuth, IColonyBridge { |
| 27 | + address colonyNetwork; |
| 28 | + // ChainId => colonyBridge |
| 29 | + mapping(uint256 => address) colonyBridges; |
| 30 | + |
| 31 | + IWormhole public wormhole; |
| 32 | + |
| 33 | + function evmChainIdToWormholeChainId(uint256 _evmChainId) public pure returns (uint16) { |
| 34 | + if (_evmChainId == 1) { |
| 35 | + return 2; |
| 36 | + } else if (_evmChainId == 137 || _evmChainId == 80001) { |
| 37 | + return 5; // Polygon |
| 38 | + } else if (_evmChainId == 10 || _evmChainId == 420) { |
| 39 | + return 24; // Optimism |
| 40 | + } else if (_evmChainId == 100) { |
| 41 | + return 25; // xDai |
| 42 | + } else { |
| 43 | + return uint16(_evmChainId % 265669); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + function supportedEvmChainId(uint256 _evmChainId) public pure returns (bool) { |
| 48 | + return |
| 49 | + _evmChainId == 1 || |
| 50 | + _evmChainId == 137 || |
| 51 | + _evmChainId == 80001 || |
| 52 | + _evmChainId == 10 || |
| 53 | + _evmChainId == 420 || |
| 54 | + _evmChainId == 100 || |
| 55 | + _evmChainId == 265669100 || |
| 56 | + _evmChainId == 265669101; |
| 57 | + } |
| 58 | + |
| 59 | + function setWormholeAddress(address _wormhole) public auth { |
| 60 | + wormhole = IWormhole(_wormhole); |
| 61 | + } |
| 62 | + |
| 63 | + function setColonyNetworkAddress(address _colonyNetwork) public auth { |
| 64 | + colonyNetwork = _colonyNetwork; |
| 65 | + } |
| 66 | + |
| 67 | + function getColonyNetworkAddress() public view returns (address) { |
| 68 | + return colonyNetwork; |
| 69 | + } |
| 70 | + |
| 71 | + function setColonyBridgeAddress(uint256 evmChainId, address _colonyNetwork) public auth { |
| 72 | + require(evmChainId <= type(uint128).max, "colony-bridge-chainid-too-large"); |
| 73 | + uint16 requestedWormholeChainId = evmChainIdToWormholeChainId(evmChainId); |
| 74 | + colonyBridges[requestedWormholeChainId] = _colonyNetwork; |
| 75 | + } |
| 76 | + |
| 77 | + function getColonyBridgeAddress(uint256 evmChainId) public view returns (address) { |
| 78 | + uint16 requestedWormholeChainId = evmChainIdToWormholeChainId(evmChainId); |
| 79 | + return colonyBridges[requestedWormholeChainId]; |
| 80 | + } |
| 81 | + |
| 82 | + function wormholeFormatAddressToEthereumAddress( |
| 83 | + bytes32 _wormholeFormatAddress |
| 84 | + ) public pure returns (address) { |
| 85 | + return address(uint160(uint256(_wormholeFormatAddress))); |
| 86 | + } |
| 87 | + |
| 88 | + function receiveMessage(bytes memory _vaa) public { |
| 89 | + (IWormhole.VM memory wormholeMessage, bool valid, string memory reason) = wormhole |
| 90 | + .parseAndVerifyVM(_vaa); |
| 91 | + |
| 92 | + // Check the vaa was valid |
| 93 | + require(valid, reason); |
| 94 | + |
| 95 | + // Check came from a known colony bridge |
| 96 | + require( |
| 97 | + wormholeFormatAddressToEthereumAddress(wormholeMessage.emitterAddress) == |
| 98 | + colonyBridges[wormholeMessage.emitterChainId], |
| 99 | + "colony-bridge-bridged-tx-only-from-colony-bridge" |
| 100 | + ); |
| 101 | + |
| 102 | + // We ignore sequence numbers - bridging out of order is okay, because we have our own way of handling that |
| 103 | + |
| 104 | + // Do the thing |
| 105 | + |
| 106 | + (bool success, bytes memory returndata) = address(colonyNetwork).call(wormholeMessage.payload); |
| 107 | + if (!success) { |
| 108 | + // Stolen shamelessly from |
| 109 | + // https://ethereum.stackexchange.com/questions/83528/how-can-i-get-the-revert-reason-of-a-call-in-solidity-so-that-i-can-use-it-in-th |
| 110 | + // If the _res length is less than 68, then the transaction failed silently (without a revert message) |
| 111 | + if (returndata.length >= 68) { |
| 112 | + assembly { |
| 113 | + // Slice the sighash. |
| 114 | + returndata := add(returndata, 0x04) |
| 115 | + } |
| 116 | + require(false, abi.decode(returndata, (string))); // All that remains is the revert string |
| 117 | + } |
| 118 | + require(false, "require-execute-call-reverted-with-no-error"); |
| 119 | + } |
| 120 | + |
| 121 | + require(success, "wormhole-bridge-receive-message-failed"); |
| 122 | + } |
| 123 | + |
| 124 | + function sendMessage( |
| 125 | + uint256 evmChainId, |
| 126 | + bytes memory payload |
| 127 | + ) public onlyColonyNetwork returns (bool) { |
| 128 | + if (!supportedEvmChainId(evmChainId)) { |
| 129 | + revert("colony-bridge-not-known-chain"); |
| 130 | + } |
| 131 | + try wormhole.publishMessage(0, payload, 0) { |
| 132 | + return true; |
| 133 | + } catch { |
| 134 | + return false; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + modifier onlyColonyNetwork() { |
| 139 | + require(msg.sender == colonyNetwork, "wormhole-bridge-only-colony-network"); |
| 140 | + _; |
| 141 | + } |
| 142 | +} |
0 commit comments