From bacbe82e6ae3f7f5ec7cdcd7d480f1e528471bbb Mon Sep 17 00:00:00 2001 From: derpy-duck <115193320+derpy-duck@users.noreply.github.com> Date: Fri, 3 Nov 2023 10:36:31 -0400 Subject: [PATCH] CCTP-support + Remove Replay Protection (#18) * CCTP support * remove replay protection --------- Co-authored-by: Joe Howarth --- README.md | 22 +- foundry.toml | 1 + src/Base.sol | 54 ++ src/CCTPAndTokenBase.sol | 456 +++++++++++++++ src/CCTPBase.sol | 237 ++++++++ src/TokenBase.sol | 250 ++++++++ src/Utils.sol | 2 +- src/WormholeRelayerSDK.sol | 223 +------- .../CCTPInterfaces/IMessageTransmitter.sol | 27 + src/interfaces/CCTPInterfaces/IReceiver.sol | 33 ++ src/interfaces/CCTPInterfaces/IRelayer.sol | 71 +++ .../CCTPInterfaces/ITokenMessenger.sol | 34 ++ src/interfaces/IWormholeReceiver.sol | 28 +- src/interfaces/IWormholeRelayer.sol | 195 ++++++- src/testing/ERC20Mock.sol | 6 +- src/testing/WormholeRelayerTest.sol | 534 +++++++++++++++--- src/testing/helpers/BytesParsing.sol | 2 +- src/testing/helpers/CircleCCTPSimulator.sol | 157 +++++ .../helpers/DeliveryInstructionDecoder.sol | 170 ++++-- src/testing/helpers/MockOffchainRelayer.sol | 229 ++++++-- src/testing/helpers/WormholeSimulator.sol | 154 +++-- test/CCTPAndTokenBridgeBase.t.sol | 247 ++++++++ test/CCTPBase.t.sol | 162 ++++++ test/ChooseChains.t.sol | 2 +- test/ExtraChains.t.sol | 2 +- test/Fork.t.sol | 158 ++++-- 26 files changed, 2888 insertions(+), 568 deletions(-) create mode 100644 src/Base.sol create mode 100644 src/CCTPAndTokenBase.sol create mode 100644 src/CCTPBase.sol create mode 100644 src/TokenBase.sol create mode 100644 src/interfaces/CCTPInterfaces/IMessageTransmitter.sol create mode 100644 src/interfaces/CCTPInterfaces/IReceiver.sol create mode 100644 src/interfaces/CCTPInterfaces/IRelayer.sol create mode 100644 src/interfaces/CCTPInterfaces/ITokenMessenger.sol create mode 100644 src/testing/helpers/CircleCCTPSimulator.sol create mode 100644 test/CCTPAndTokenBridgeBase.t.sol create mode 100644 test/CCTPBase.t.sol diff --git a/README.md b/README.md index 3e8a2bb..ebc9230 100644 --- a/README.md +++ b/README.md @@ -14,18 +14,24 @@ forge install wormhole-foundation/wormhole-solidity-sdk [HelloWormhole - Simple cross-chain message sending application](https://github.com/wormhole-foundation/hello-wormhole) -[HelloToken - Simple cross-chain token sending application](https://github.com/wormhole-foundation/hello-token) +[HelloToken - Simple cross-chain token sending application](https://github.com/wormhole-foundation/hello-tokens) + +[HelloUSDC - Simple cross-chain USDC sending application using CCTP](https://github.com/wormhole-foundation/hello-usdc) ### SDK Summary - Includes interfaces to interact with contracts in the Wormhole ecosystem ([src/interfaces](https://github.com/wormhole-foundation/wormhole-solidity-sdk/tree/main/src/interfaces)) - Includes the base class ‘Base’ with helpers for common actions that will typically need to be done within ‘receiveWormholeMessages’: - - [`onlyWormholeRelayer()`](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/WormholeRelayerSDK.sol#L26): Checking that msg.sender is the wormhole relayer contract - - [`replayProtect(bytes32 deliveryHash)`](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/WormholeRelayerSDK.sol#L31): Checking that the current delivery has not already been processed (via the hash) + - [`onlyWormholeRelayer()`](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/Base.sol#L24): Checking that msg.sender is the wormhole relayer contract Sometimes, Cross-chain applications may be set up such that there is one ‘spoke’ contract on every chain, which sends messages to the ‘hub’ contract. If so, we’d ideally only want to allow messages to be sent from these spoke contracts. Included are helpers for this: - - [`setRegisteredSender(uint16 sourceChain, bytes32 sourceAddress)`](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/WormholeRelayerSDK.sol#L49): Setting the specified sender for ‘sourceChain’ to be ‘sourceAddress’ - - [`isRegisteredSender(uint16 sourceChain, bytes32 sourceAddress)`](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/WormholeRelayerSDK.sol#L37): Checking that the sender who requested the delivery is the registered address for that chain - Look at [test/Fork.t.sol](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/test/Fork.t.sol#L16) for an example usage of Base -- Included are also the ‘[TokenSender](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/WormholeRelayerSDK.sol#L79)’ and ‘[TokenReceiver](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/WormholeRelayerSDK.sol#L186)’ base classes with helpers for smart contracts that wish to send and receive tokens using Wormhole’s TokenBridge. See ‘[HelloToken](https://github.com/wormhole-foundation/hello-token)’ for example usage. + + - [`setRegisteredSender(uint16 sourceChain, bytes32 sourceAddress)`](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/Base.sol#L47): Setting the specified sender for ‘sourceChain’ to be ‘sourceAddress’ + - [`isRegisteredSender(uint16 sourceChain, bytes32 sourceAddress)`](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/Base.sol#L35): Checking that the sender who requested the delivery is the registered address for that chain + + Look at test/Counter.t.sol for an example usage of Base + +- Included are also the ‘[TokenSender](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/TokenBase#L36)’ and ‘[TokenReceiver](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/TokenBase.sol#L126)’ base classes with helpers for smart contracts that wish to send and receive tokens using Wormhole’s TokenBridge. See ‘[HelloToken](https://github.com/wormhole-foundation/hello-token)’ for example usage. +- Included are also the ‘[CCTPSender](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/CCTPBase#L70)’ and ‘[CCTPReceiver](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/CCTPBase.sol#L134)’ base classes with helpers for smart contracts that wish to send and receive both tokens using Wormhole’s TokenBridge as well as USDC using CCTP. See ‘[HelloUSDC](https://github.com/wormhole-foundation/hello-usdc)’ for example usage. - Included are helpers that help set up a local forge testing environment. See ‘[HelloWormhole](https://github.com/wormhole-foundation/hello-wormhole)’ for example usage. -**Note: This code is meant to be used as starter / reference code. Feel free to modify for use in your contracts, and also make sure to audit any code used from here as part of your contracts before deploying to mainnet.** + +**Note: This code is meant to be used as starter / reference code. Feel free to modify for use in your contracts, and also make sure to audit any code used from here as part of your contracts before deploying to mainnet.** \ No newline at end of file diff --git a/foundry.toml b/foundry.toml index 40ad0d1..dd87c33 100644 --- a/foundry.toml +++ b/foundry.toml @@ -3,5 +3,6 @@ solc_version = "0.8.13" src = "src" out = "out" libs = ["lib"] +via_ir = true # See more config options https://github.com/foundry-rs/foundry/tree/master/config \ No newline at end of file diff --git a/src/Base.sol b/src/Base.sol new file mode 100644 index 0000000..d54481a --- /dev/null +++ b/src/Base.sol @@ -0,0 +1,54 @@ +pragma solidity ^0.8.13; + +import "./interfaces/IWormholeReceiver.sol"; +import "./interfaces/IWormholeRelayer.sol"; +import "./interfaces/IWormhole.sol"; +import "./Utils.sol"; + +abstract contract Base { + IWormholeRelayer public immutable wormholeRelayer; + IWormhole public immutable wormhole; + + address registrationOwner; + mapping(uint16 => bytes32) registeredSenders; + + constructor(address _wormholeRelayer, address _wormhole) { + wormholeRelayer = IWormholeRelayer(_wormholeRelayer); + wormhole = IWormhole(_wormhole); + registrationOwner = msg.sender; + } + + modifier onlyWormholeRelayer() { + require( + msg.sender == address(wormholeRelayer), + "Msg.sender is not Wormhole Relayer" + ); + _; + } + + modifier isRegisteredSender(uint16 sourceChain, bytes32 sourceAddress) { + require( + registeredSenders[sourceChain] == sourceAddress, + "Not registered sender" + ); + _; + } + + /** + * Sets the registered address for 'sourceChain' to 'sourceAddress' + * So that for messages from 'sourceChain', only ones from 'sourceAddress' are valid + * + * Assumes only one sender per chain is valid + * Sender is the address that called 'send' on the Wormhole Relayer contract on the source chain) + */ + function setRegisteredSender( + uint16 sourceChain, + bytes32 sourceAddress + ) public { + require( + msg.sender == registrationOwner, + "Not allowed to set registered sender" + ); + registeredSenders[sourceChain] = sourceAddress; + } +} diff --git a/src/CCTPAndTokenBase.sol b/src/CCTPAndTokenBase.sol new file mode 100644 index 0000000..9b51f76 --- /dev/null +++ b/src/CCTPAndTokenBase.sol @@ -0,0 +1,456 @@ +pragma solidity ^0.8.13; + +import "./interfaces/IWormholeReceiver.sol"; +import "./interfaces/IWormholeRelayer.sol"; +import "./interfaces/ITokenBridge.sol"; +import {IERC20} from "./interfaces/IERC20.sol"; +import "./interfaces/CCTPInterfaces/ITokenMessenger.sol"; +import "./interfaces/CCTPInterfaces/IMessageTransmitter.sol"; + +import "./Utils.sol"; +import "./TokenBase.sol"; +import "./CCTPBase.sol"; + +abstract contract CCTPAndTokenBase is CCTPBase { + ITokenBridge public immutable tokenBridge; + + enum Transfer { + TOKEN_BRIDGE, + CCTP + } + + constructor( + address _wormholeRelayer, + address _tokenBridge, + address _wormhole, + address _circleMessageTransmitter, + address _circleTokenMessenger, + address _USDC + ) + CCTPBase( + _wormholeRelayer, + _wormhole, + _circleMessageTransmitter, + _circleTokenMessenger, + _USDC + ) + { + tokenBridge = ITokenBridge(_tokenBridge); + } +} + +abstract contract CCTPAndTokenSender is CCTPAndTokenBase { + // CCTP Sender functions, taken from "./CCTPBase.sol" + + uint8 internal constant CONSISTENCY_LEVEL_FINALIZED = 15; + + using CCTPMessageLib for *; + + mapping(uint16 => uint32) public chainIdToCCTPDomain; + + /** + * Sets the CCTP Domain corresponding to chain 'chain' to be 'cctpDomain' + * So that transfers of USDC to chain 'chain' use the target CCTP domain 'cctpDomain' + * + * This action can only be performed by 'cctpConfigurationOwner', who is set to be the deployer + * + * Currently, cctp domains are: + * Ethereum: Wormhole chain id 2, cctp domain 0 + * Avalanche: Wormhole chain id 6, cctp domain 1 + * Optimism: Wormhole chain id 24, cctp domain 2 + * Arbitrum: Wormhole chain id 23, cctp domain 3 + * Base: Wormhole chain id 30, cctp domain 6 + * + * These can be set via: + * setCCTPDomain(2, 0); + * setCCTPDomain(6, 1); + * setCCTPDomain(24, 2); + * setCCTPDomain(23, 3); + * setCCTPDomain(30, 6); + */ + function setCCTPDomain(uint16 chain, uint32 cctpDomain) public { + require( + msg.sender == cctpConfigurationOwner, + "Not allowed to set CCTP Domain" + ); + chainIdToCCTPDomain[chain] = cctpDomain; + } + + function getCCTPDomain(uint16 chain) internal view returns (uint32) { + return chainIdToCCTPDomain[chain]; + } + + /** + * transferUSDC wraps common boilerplate for sending tokens to another chain using IWormholeRelayer + * - approves the Circle TokenMessenger contract to spend 'amount' of USDC + * - calls Circle's 'depositForBurnWithCaller' + * - returns key for inclusion in WormholeRelayer `additionalVaas` argument + * + * Note: this requires that only the targetAddress can redeem transfers. + * + */ + + function transferUSDC( + uint256 amount, + uint16 targetChain, + address targetAddress + ) internal returns (MessageKey memory) { + IERC20(USDC).approve(address(circleTokenMessenger), amount); + bytes32 targetAddressBytes32 = addressToBytes32CCTP(targetAddress); + uint64 nonce = circleTokenMessenger.depositForBurnWithCaller( + amount, + getCCTPDomain(targetChain), + targetAddressBytes32, + USDC, + targetAddressBytes32 + ); + return + MessageKey( + CCTPMessageLib.CCTP_KEY_TYPE, + abi.encodePacked(getCCTPDomain(wormhole.chainId()), nonce) + ); + } + + // Publishes a CCTP transfer of 'amount' of USDC + // and requests a delivery of the transfer along with 'payload' to 'targetAddress' on 'targetChain' + // + // The second step is done by publishing a wormhole message representing a request + // to call 'receiveWormholeMessages' on the address 'targetAddress' on chain 'targetChain' + // with the payload 'abi.encode(Transfer.CCTP, amount, payload)' + // (we encode a Transfer enum to distinguish this from a TokenBridge transfer) + // (and we encode the amount so it can be checked on the target chain) + function sendUSDCWithPayloadToEvm( + uint16 targetChain, + address targetAddress, + bytes memory payload, + uint256 receiverValue, + uint256 gasLimit, + uint256 amount + ) internal returns (uint64 sequence) { + MessageKey[] memory messageKeys = new MessageKey[](1); + messageKeys[0] = transferUSDC(amount, targetChain, targetAddress); + + bytes memory userPayload = abi.encode(Transfer.CCTP, amount, payload); + address defaultDeliveryProvider = wormholeRelayer + .getDefaultDeliveryProvider(); + + (uint256 cost, ) = wormholeRelayer.quoteEVMDeliveryPrice( + targetChain, + receiverValue, + gasLimit + ); + + sequence = wormholeRelayer.sendToEvm{value: cost}( + targetChain, + targetAddress, + userPayload, + receiverValue, + 0, + gasLimit, + targetChain, + address(0x0), + defaultDeliveryProvider, + messageKeys, + CONSISTENCY_LEVEL_FINALIZED + ); + } + + function addressToBytes32CCTP(address addr) private pure returns (bytes32) { + return bytes32(uint256(uint160(addr))); + } + + // TokenBridge Sender functions, taken from "./TokenBase.sol" + + /** + * transferTokens wraps common boilerplate for sending tokens to another chain using IWormholeRelayer + * - approves tokenBridge to spend 'amount' of 'token' + * - emits token transfer VAA + * - returns VAA key for inclusion in WormholeRelayer `additionalVaas` argument + * + * Note: this function uses transferTokensWithPayload instead of transferTokens since the former requires that only the targetAddress + * can redeem transfers. Otherwise it's possible for another address to redeem the transfer before the targetContract is invoked by + * the offchain relayer and the target contract would have to be hardened against this. + * + */ + function transferTokens( + address token, + uint256 amount, + uint16 targetChain, + address targetAddress + ) internal returns (VaaKey memory) { + return + transferTokens( + token, + amount, + targetChain, + targetAddress, + bytes("") + ); + } + + /** + * transferTokens wraps common boilerplate for sending tokens to another chain using IWormholeRelayer. + * A payload can be included in the transfer vaa. By including a payload here instead of the deliveryVaa, + * fewer trust assumptions are placed on the WormholeRelayer contract. + * + * - approves tokenBridge to spend 'amount' of 'token' + * - emits token transfer VAA + * - returns VAA key for inclusion in WormholeRelayer `additionalVaas` argument + * + * Note: this function uses transferTokensWithPayload instead of transferTokens since the former requires that only the targetAddress + * can redeem transfers. Otherwise it's possible for another address to redeem the transfer before the targetContract is invoked by + * the offchain relayer and the target contract would have to be hardened against this. + */ + function transferTokens( + address token, + uint256 amount, + uint16 targetChain, + address targetAddress, + bytes memory payload + ) internal returns (VaaKey memory) { + IERC20(token).approve(address(tokenBridge), amount); + uint64 sequence = tokenBridge.transferTokensWithPayload{ + value: wormhole.messageFee() + }( + token, + amount, + targetChain, + toWormholeFormat(targetAddress), + 0, + payload + ); + return + VaaKey({ + emitterAddress: toWormholeFormat(address(tokenBridge)), + chainId: wormhole.chainId(), + sequence: sequence + }); + } + + // Publishes a wormhole message representing a 'TokenBridge' transfer of 'amount' of 'token' + // and requests a delivery of the transfer along with 'payload' to 'targetAddress' on 'targetChain' + // + // The second step is done by publishing a wormhole message representing a request + // to call 'receiveWormholeMessages' on the address 'targetAddress' on chain 'targetChain' + // with the payload 'abi.encode(Transfer.TOKEN_BRIDGE, payload)' + // (we encode a Transfer enum to distinguish this from a CCTP transfer) + function sendTokenWithPayloadToEvm( + uint16 targetChain, + address targetAddress, + bytes memory payload, + uint256 receiverValue, + uint256 gasLimit, + address token, + uint256 amount + ) internal returns (uint64) { + VaaKey[] memory vaaKeys = new VaaKey[](1); + vaaKeys[0] = transferTokens(token, amount, targetChain, targetAddress); + + (uint256 cost, ) = wormholeRelayer.quoteEVMDeliveryPrice( + targetChain, + receiverValue, + gasLimit + ); + return + wormholeRelayer.sendVaasToEvm{value: cost}( + targetChain, + targetAddress, + abi.encode(Transfer.TOKEN_BRIDGE, payload), + receiverValue, + gasLimit, + vaaKeys + ); + } + + function sendTokenWithPayloadToEvm( + uint16 targetChain, + address targetAddress, + bytes memory payload, + uint256 receiverValue, + uint256 gasLimit, + address token, + uint256 amount, + uint16 refundChain, + address refundAddress + ) internal returns (uint64) { + VaaKey[] memory vaaKeys = new VaaKey[](1); + vaaKeys[0] = transferTokens(token, amount, targetChain, targetAddress); + + (uint256 cost, ) = wormholeRelayer.quoteEVMDeliveryPrice( + targetChain, + receiverValue, + gasLimit + ); + return + wormholeRelayer.sendVaasToEvm{value: cost}( + targetChain, + targetAddress, + abi.encode(Transfer.TOKEN_BRIDGE, payload), + receiverValue, + gasLimit, + vaaKeys, + refundChain, + refundAddress + ); + } +} + +abstract contract CCTPAndTokenReceiver is CCTPAndTokenBase { + function redeemUSDC( + bytes memory cctpMessage + ) internal returns (uint256 amount) { + (bytes memory message, bytes memory signature) = abi.decode( + cctpMessage, + (bytes, bytes) + ); + uint256 beforeBalance = IERC20(USDC).balanceOf(address(this)); + circleMessageTransmitter.receiveMessage(message, signature); + return IERC20(USDC).balanceOf(address(this)) - beforeBalance; + } + + struct TokenReceived { + bytes32 tokenHomeAddress; + uint16 tokenHomeChain; + address tokenAddress; // wrapped address if tokenHomeChain !== this chain, else tokenHomeAddress (in evm address format) + uint256 amount; + uint256 amountNormalized; // if decimals > 8, normalized to 8 decimal places + } + + function getDecimals( + address tokenAddress + ) internal view returns (uint8 decimals) { + // query decimals + (, bytes memory queriedDecimals) = address(tokenAddress).staticcall( + abi.encodeWithSignature("decimals()") + ); + decimals = abi.decode(queriedDecimals, (uint8)); + } + + function getTokenAddressOnThisChain( + uint16 tokenHomeChain, + bytes32 tokenHomeAddress + ) internal view returns (address tokenAddressOnThisChain) { + return + tokenHomeChain == wormhole.chainId() + ? fromWormholeFormat(tokenHomeAddress) + : tokenBridge.wrappedAsset(tokenHomeChain, tokenHomeAddress); + } + + function receiveWormholeMessages( + bytes memory payload, + bytes[] memory additionalMessages, + bytes32 sourceAddress, + uint16 sourceChain, + bytes32 deliveryHash + ) external payable { + Transfer transferType = abi.decode(payload, (Transfer)); + if (transferType == Transfer.TOKEN_BRIDGE) { + TokenReceived[] memory receivedTokens = new TokenReceived[]( + additionalMessages.length + ); + + for (uint256 i = 0; i < additionalMessages.length; ++i) { + IWormhole.VM memory parsed = wormhole.parseVM( + additionalMessages[i] + ); + require( + parsed.emitterAddress == + tokenBridge.bridgeContracts(parsed.emitterChainId), + "Not a Token Bridge VAA" + ); + ITokenBridge.TransferWithPayload memory transfer = tokenBridge + .parseTransferWithPayload(parsed.payload); + require( + transfer.to == toWormholeFormat(address(this)) && + transfer.toChain == wormhole.chainId(), + "Token was not sent to this address" + ); + + tokenBridge.completeTransferWithPayload(additionalMessages[i]); + + address thisChainTokenAddress = getTokenAddressOnThisChain( + transfer.tokenChain, + transfer.tokenAddress + ); + uint8 decimals = getDecimals(thisChainTokenAddress); + uint256 denormalizedAmount = transfer.amount; + if (decimals > 8) + denormalizedAmount *= uint256(10) ** (decimals - 8); + + receivedTokens[i] = TokenReceived({ + tokenHomeAddress: transfer.tokenAddress, + tokenHomeChain: transfer.tokenChain, + tokenAddress: thisChainTokenAddress, + amount: denormalizedAmount, + amountNormalized: transfer.amount + }); + } + + (, bytes memory userPayload) = abi.decode( + payload, + (Transfer, bytes) + ); + + // call into overriden method + receivePayloadAndTokens( + userPayload, + receivedTokens, + sourceAddress, + sourceChain, + deliveryHash + ); + } else if (transferType == Transfer.CCTP) { + // Currently, 'sendUSDCWithPayloadToEVM' only sends one CCTP transfer + // That can be modified if the integrator desires to send multiple CCTP transfers + // in which case the following code would have to be modified to support + // redeeming these multiple transfers and checking that their 'amount's are accurate + require( + additionalMessages.length <= 1, + "CCTP: At most one Message is supported" + ); + + uint256 amountUSDCReceived; + if (additionalMessages.length == 1) { + amountUSDCReceived = redeemUSDC(additionalMessages[0]); + } + + (, uint256 amount, bytes memory userPayload) = abi.decode( + payload, + (Transfer, uint256, bytes) + ); + + // Check that the correct amount was received + // It is important to verify that the 'USDC' sent in by the relayer is the same amount + // that the sender sent in on the source chain + require(amount == amountUSDCReceived, "Wrong amount received"); + + receivePayloadAndUSDC( + userPayload, + amountUSDCReceived, + sourceAddress, + sourceChain, + deliveryHash + ); + } else { + revert("Invalid transfer type"); + } + } + + // Implement this function to handle in-bound deliveries that include a CCTP transfer + function receivePayloadAndUSDC( + bytes memory payload, + uint256 amountUSDCReceived, + bytes32 sourceAddress, + uint16 sourceChain, + bytes32 deliveryHash + ) internal virtual {} + + // Implement this function to handle in-bound deliveries that include a TokenBridge transfer + function receivePayloadAndTokens( + bytes memory payload, + TokenReceived[] memory receivedTokens, + bytes32 sourceAddress, + uint16 sourceChain, + bytes32 deliveryHash + ) internal virtual {} +} diff --git a/src/CCTPBase.sol b/src/CCTPBase.sol new file mode 100644 index 0000000..19f8b9a --- /dev/null +++ b/src/CCTPBase.sol @@ -0,0 +1,237 @@ +pragma solidity ^0.8.13; + +import "./interfaces/IWormholeReceiver.sol"; +import "./interfaces/IWormholeRelayer.sol"; +import {IERC20} from "./interfaces/IERC20.sol"; +import "./interfaces/CCTPInterfaces/ITokenMessenger.sol"; +import "./interfaces/CCTPInterfaces/IMessageTransmitter.sol"; + +import "./Utils.sol"; +import "./Base.sol"; + +library CCTPMessageLib { + // The second standardized key type is a CCTP Key + // representing a CCTP transfer of USDC + // (on the IWormholeRelayer interface) + + // Note - the default delivery provider only will relay CCTP transfers that were sent + // in the same transaction that this message was emitted! + // (This will always be the case if 'CCTPSender' is used) + + uint8 constant CCTP_KEY_TYPE = 2; + + // encoded using abi.encodePacked(domain, nonce) + struct CCTPKey { + uint32 domain; + uint64 nonce; + } + + // encoded using abi.encode(message, signature) + struct CCTPMessage { + bytes message; + bytes signature; + } +} + +abstract contract CCTPBase is Base { + ITokenMessenger immutable circleTokenMessenger; + IMessageTransmitter immutable circleMessageTransmitter; + address immutable USDC; + address cctpConfigurationOwner; + + constructor( + address _wormholeRelayer, + address _wormhole, + address _circleMessageTransmitter, + address _circleTokenMessenger, + address _USDC + ) Base(_wormholeRelayer, _wormhole) { + circleTokenMessenger = ITokenMessenger(_circleTokenMessenger); + circleMessageTransmitter = IMessageTransmitter( + _circleMessageTransmitter + ); + USDC = _USDC; + cctpConfigurationOwner = msg.sender; + } +} + +abstract contract CCTPSender is CCTPBase { + uint8 internal constant CONSISTENCY_LEVEL_FINALIZED = 15; + + using CCTPMessageLib for *; + + mapping(uint16 => uint32) public chainIdToCCTPDomain; + + /** + * Sets the CCTP Domain corresponding to chain 'chain' to be 'cctpDomain' + * So that transfers of USDC to chain 'chain' use the target CCTP domain 'cctpDomain' + * + * This action can only be performed by 'cctpConfigurationOwner', who is set to be the deployer + * + * Currently, cctp domains are: + * Ethereum: Wormhole chain id 2, cctp domain 0 + * Avalanche: Wormhole chain id 6, cctp domain 1 + * Optimism: Wormhole chain id 24, cctp domain 2 + * Arbitrum: Wormhole chain id 23, cctp domain 3 + * Base: Wormhole chain id 30, cctp domain 6 + * + * These can be set via: + * setCCTPDomain(2, 0); + * setCCTPDomain(6, 1); + * setCCTPDomain(24, 2); + * setCCTPDomain(23, 3); + * setCCTPDomain(30, 6); + */ + function setCCTPDomain(uint16 chain, uint32 cctpDomain) public { + require( + msg.sender == cctpConfigurationOwner, + "Not allowed to set CCTP Domain" + ); + chainIdToCCTPDomain[chain] = cctpDomain; + } + + function getCCTPDomain(uint16 chain) internal view returns (uint32) { + return chainIdToCCTPDomain[chain]; + } + + /** + * transferUSDC wraps common boilerplate for sending tokens to another chain using IWormholeRelayer + * - approves the Circle TokenMessenger contract to spend 'amount' of USDC + * - calls Circle's 'depositForBurnWithCaller' + * - returns key for inclusion in WormholeRelayer `additionalVaas` argument + * + * Note: this requires that only the targetAddress can redeem transfers. + * + */ + + function transferUSDC( + uint256 amount, + uint16 targetChain, + address targetAddress + ) internal returns (MessageKey memory) { + IERC20(USDC).approve(address(circleTokenMessenger), amount); + bytes32 targetAddressBytes32 = addressToBytes32CCTP(targetAddress); + uint64 nonce = circleTokenMessenger.depositForBurnWithCaller( + amount, + getCCTPDomain(targetChain), + targetAddressBytes32, + USDC, + targetAddressBytes32 + ); + return + MessageKey( + CCTPMessageLib.CCTP_KEY_TYPE, + abi.encodePacked(getCCTPDomain(wormhole.chainId()), nonce) + ); + } + + // Publishes a CCTP transfer of 'amount' of USDC + // and requests a delivery of the transfer along with 'payload' to 'targetAddress' on 'targetChain' + // + // The second step is done by publishing a wormhole message representing a request + // to call 'receiveWormholeMessages' on the address 'targetAddress' on chain 'targetChain' + // with the payload 'abi.encode(amount, payload)' + // (and we encode the amount so it can be checked on the target chain) + function sendUSDCWithPayloadToEvm( + uint16 targetChain, + address targetAddress, + bytes memory payload, + uint256 receiverValue, + uint256 gasLimit, + uint256 amount + ) internal returns (uint64 sequence) { + MessageKey[] memory messageKeys = new MessageKey[](1); + messageKeys[0] = transferUSDC(amount, targetChain, targetAddress); + + bytes memory userPayload = abi.encode(amount, payload); + address defaultDeliveryProvider = wormholeRelayer + .getDefaultDeliveryProvider(); + + (uint256 cost, ) = wormholeRelayer.quoteEVMDeliveryPrice( + targetChain, + receiverValue, + gasLimit + ); + + sequence = wormholeRelayer.sendToEvm{value: cost}( + targetChain, + targetAddress, + userPayload, + receiverValue, + 0, + gasLimit, + targetChain, + address(0x0), + defaultDeliveryProvider, + messageKeys, + CONSISTENCY_LEVEL_FINALIZED + ); + } + + function addressToBytes32CCTP(address addr) private pure returns (bytes32) { + return bytes32(uint256(uint160(addr))); + } +} + +abstract contract CCTPReceiver is CCTPBase { + function redeemUSDC( + bytes memory cctpMessage + ) internal returns (uint256 amount) { + (bytes memory message, bytes memory signature) = abi.decode( + cctpMessage, + (bytes, bytes) + ); + uint256 beforeBalance = IERC20(USDC).balanceOf(address(this)); + circleMessageTransmitter.receiveMessage(message, signature); + return IERC20(USDC).balanceOf(address(this)) - beforeBalance; + } + + function receiveWormholeMessages( + bytes memory payload, + bytes[] memory additionalMessages, + bytes32 sourceAddress, + uint16 sourceChain, + bytes32 deliveryHash + ) external payable { + // Currently, 'sendUSDCWithPayloadToEVM' only sends one CCTP transfer + // That can be modified if the integrator desires to send multiple CCTP transfers + // in which case the following code would have to be modified to support + // redeeming these multiple transfers and checking that their 'amount's are accurate + require( + additionalMessages.length <= 1, + "CCTP: At most one Message is supported" + ); + + uint256 amountUSDCReceived; + if (additionalMessages.length == 1) { + amountUSDCReceived = redeemUSDC(additionalMessages[0]); + } + + (uint256 amount, bytes memory userPayload) = abi.decode( + payload, + (uint256, bytes) + ); + + // Check that the correct amount was received + // It is important to verify that the 'USDC' sent in by the relayer is the same amount + // that the sender sent in on the source chain + require(amount == amountUSDCReceived, "Wrong amount received"); + + receivePayloadAndUSDC( + userPayload, + amountUSDCReceived, + sourceAddress, + sourceChain, + deliveryHash + ); + } + + // Implement this function to handle in-bound deliveries that include a CCTP transfer + function receivePayloadAndUSDC( + bytes memory payload, + uint256 amountUSDCReceived, + bytes32 sourceAddress, + uint16 sourceChain, + bytes32 deliveryHash + ) internal virtual {} +} diff --git a/src/TokenBase.sol b/src/TokenBase.sol new file mode 100644 index 0000000..0e20e64 --- /dev/null +++ b/src/TokenBase.sol @@ -0,0 +1,250 @@ +pragma solidity ^0.8.13; + +import "./interfaces/IWormholeReceiver.sol"; +import "./interfaces/IWormholeRelayer.sol"; +import "./interfaces/ITokenBridge.sol"; +import {IERC20} from "./interfaces/IERC20.sol"; +import {Base} from "./WormholeRelayerSDK.sol"; + +import "./Utils.sol"; + +abstract contract TokenBase is Base { + ITokenBridge public immutable tokenBridge; + + constructor( + address _wormholeRelayer, + address _tokenBridge, + address _wormhole + ) Base(_wormholeRelayer, _wormhole) { + tokenBridge = ITokenBridge(_tokenBridge); + } +} + +abstract contract TokenSender is TokenBase { + /** + * transferTokens wraps common boilerplate for sending tokens to another chain using IWormholeRelayer + * - approves tokenBridge to spend 'amount' of 'token' + * - emits token transfer VAA + * - returns VAA key for inclusion in WormholeRelayer `additionalVaas` argument + * + * Note: this function uses transferTokensWithPayload instead of transferTokens since the former requires that only the targetAddress + * can redeem transfers. Otherwise it's possible for another address to redeem the transfer before the targetContract is invoked by + * the offchain relayer and the target contract would have to be hardened against this. + * + */ + function transferTokens( + address token, + uint256 amount, + uint16 targetChain, + address targetAddress + ) internal returns (VaaKey memory) { + return + transferTokens( + token, + amount, + targetChain, + targetAddress, + bytes("") + ); + } + + /** + * transferTokens wraps common boilerplate for sending tokens to another chain using IWormholeRelayer. + * A payload can be included in the transfer vaa. By including a payload here instead of the deliveryVaa, + * fewer trust assumptions are placed on the WormholeRelayer contract. + * + * - approves tokenBridge to spend 'amount' of 'token' + * - emits token transfer VAA + * - returns VAA key for inclusion in WormholeRelayer `additionalVaas` argument + * + * Note: this function uses transferTokensWithPayload instead of transferTokens since the former requires that only the targetAddress + * can redeem transfers. Otherwise it's possible for another address to redeem the transfer before the targetContract is invoked by + * the offchain relayer and the target contract would have to be hardened against this. + */ + function transferTokens( + address token, + uint256 amount, + uint16 targetChain, + address targetAddress, + bytes memory payload + ) internal returns (VaaKey memory) { + IERC20(token).approve(address(tokenBridge), amount); + uint64 sequence = tokenBridge.transferTokensWithPayload{ + value: wormhole.messageFee() + }( + token, + amount, + targetChain, + toWormholeFormat(targetAddress), + 0, + payload + ); + return + VaaKey({ + emitterAddress: toWormholeFormat(address(tokenBridge)), + chainId: wormhole.chainId(), + sequence: sequence + }); + } + + // Publishes a wormhole message representing a 'TokenBridge' transfer of 'amount' of 'token' + // and requests a delivery of the transfer along with 'payload' to 'targetAddress' on 'targetChain' + // + // The second step is done by publishing a wormhole message representing a request + // to call 'receiveWormholeMessages' on the address 'targetAddress' on chain 'targetChain' + // with the payload 'payload' + function sendTokenWithPayloadToEvm( + uint16 targetChain, + address targetAddress, + bytes memory payload, + uint256 receiverValue, + uint256 gasLimit, + address token, + uint256 amount + ) internal returns (uint64) { + VaaKey[] memory vaaKeys = new VaaKey[](1); + vaaKeys[0] = transferTokens(token, amount, targetChain, targetAddress); + + (uint256 cost, ) = wormholeRelayer.quoteEVMDeliveryPrice( + targetChain, + receiverValue, + gasLimit + ); + return + wormholeRelayer.sendVaasToEvm{value: cost}( + targetChain, + targetAddress, + payload, + receiverValue, + gasLimit, + vaaKeys + ); + } + + function sendTokenWithPayloadToEvm( + uint16 targetChain, + address targetAddress, + bytes memory payload, + uint256 receiverValue, + uint256 gasLimit, + address token, + uint256 amount, + uint16 refundChain, + address refundAddress + ) internal returns (uint64) { + VaaKey[] memory vaaKeys = new VaaKey[](1); + vaaKeys[0] = transferTokens(token, amount, targetChain, targetAddress); + + (uint256 cost, ) = wormholeRelayer.quoteEVMDeliveryPrice( + targetChain, + receiverValue, + gasLimit + ); + return + wormholeRelayer.sendVaasToEvm{value: cost}( + targetChain, + targetAddress, + payload, + receiverValue, + gasLimit, + vaaKeys, + refundChain, + refundAddress + ); + } +} + +abstract contract TokenReceiver is TokenBase { + struct TokenReceived { + bytes32 tokenHomeAddress; + uint16 tokenHomeChain; + address tokenAddress; // wrapped address if tokenHomeChain !== this chain, else tokenHomeAddress (in evm address format) + uint256 amount; + uint256 amountNormalized; // if decimals > 8, normalized to 8 decimal places + } + + function getDecimals( + address tokenAddress + ) internal view returns (uint8 decimals) { + // query decimals + (, bytes memory queriedDecimals) = address(tokenAddress).staticcall( + abi.encodeWithSignature("decimals()") + ); + decimals = abi.decode(queriedDecimals, (uint8)); + } + + function getTokenAddressOnThisChain( + uint16 tokenHomeChain, + bytes32 tokenHomeAddress + ) internal view returns (address tokenAddressOnThisChain) { + return + tokenHomeChain == wormhole.chainId() + ? fromWormholeFormat(tokenHomeAddress) + : tokenBridge.wrappedAsset(tokenHomeChain, tokenHomeAddress); + } + + function receiveWormholeMessages( + bytes memory payload, + bytes[] memory additionalVaas, + bytes32 sourceAddress, + uint16 sourceChain, + bytes32 deliveryHash + ) external payable { + TokenReceived[] memory receivedTokens = new TokenReceived[]( + additionalVaas.length + ); + + for (uint256 i = 0; i < additionalVaas.length; ++i) { + IWormhole.VM memory parsed = wormhole.parseVM(additionalVaas[i]); + require( + parsed.emitterAddress == + tokenBridge.bridgeContracts(parsed.emitterChainId), + "Not a Token Bridge VAA" + ); + ITokenBridge.TransferWithPayload memory transfer = tokenBridge + .parseTransferWithPayload(parsed.payload); + require( + transfer.to == toWormholeFormat(address(this)) && + transfer.toChain == wormhole.chainId(), + "Token was not sent to this address" + ); + + tokenBridge.completeTransferWithPayload(additionalVaas[i]); + + address thisChainTokenAddress = getTokenAddressOnThisChain( + transfer.tokenChain, + transfer.tokenAddress + ); + uint8 decimals = getDecimals(thisChainTokenAddress); + uint256 denormalizedAmount = transfer.amount; + if (decimals > 8) + denormalizedAmount *= uint256(10) ** (decimals - 8); + + receivedTokens[i] = TokenReceived({ + tokenHomeAddress: transfer.tokenAddress, + tokenHomeChain: transfer.tokenChain, + tokenAddress: thisChainTokenAddress, + amount: denormalizedAmount, + amountNormalized: transfer.amount + }); + } + + // call into overriden method + receivePayloadAndTokens( + payload, + receivedTokens, + sourceAddress, + sourceChain, + deliveryHash + ); + } + + // Implement this function to handle in-bound deliveries that include a TokenBridge transfer + function receivePayloadAndTokens( + bytes memory payload, + TokenReceived[] memory receivedTokens, + bytes32 sourceAddress, + uint16 sourceChain, + bytes32 deliveryHash + ) internal virtual {} +} diff --git a/src/Utils.sol b/src/Utils.sol index 6c51823..4106e78 100644 --- a/src/Utils.sol +++ b/src/Utils.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: UNLICENSED + pragma solidity ^0.8.13; import "./interfaces/IWormholeRelayer.sol"; diff --git a/src/WormholeRelayerSDK.sol b/src/WormholeRelayerSDK.sol index 269ef31..511cf01 100644 --- a/src/WormholeRelayerSDK.sol +++ b/src/WormholeRelayerSDK.sol @@ -1,224 +1,9 @@ -// SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.13; import "./interfaces/IWormholeReceiver.sol"; import "./interfaces/IWormholeRelayer.sol"; -import "./interfaces/ITokenBridge.sol"; -import {IERC20} from "./interfaces/IERC20.sol"; - import "./Utils.sol"; - -abstract contract Base { - IWormholeRelayer public immutable wormholeRelayer; - IWormhole public immutable wormhole; - - mapping(bytes32 => bool) public seenDeliveryVaaHashes; - - address registrationOwner; - mapping(uint16 => bytes32) registeredSenders; - - constructor(address _wormholeRelayer, address _wormhole) { - wormholeRelayer = IWormholeRelayer(_wormholeRelayer); - wormhole = IWormhole(_wormhole); - registrationOwner = msg.sender; - } - - modifier onlyWormholeRelayer() { - require(msg.sender == address(wormholeRelayer), "Msg.sender is not Wormhole Relayer"); - _; - } - - modifier replayProtect(bytes32 deliveryHash) { - require(!seenDeliveryVaaHashes[deliveryHash], "Message already processed"); - seenDeliveryVaaHashes[deliveryHash] = true; - _; - } - - modifier isRegisteredSender(uint16 sourceChain, bytes32 sourceAddress) { - require(registeredSenders[sourceChain] == sourceAddress, "Not registered sender"); - _; - } - - /** - * Sets the registered address for 'sourceChain' to 'sourceAddress' - * So that for messages from 'sourceChain', only ones from 'sourceAddress' are valid - * - * Assumes only one sender per chain is valid - * Sender is the address that called 'send' on the Wormhole Relayer contract on the source chain) - */ - function setRegisteredSender(uint16 sourceChain, bytes32 sourceAddress) public { - require(msg.sender == registrationOwner, "Not allowed to set registered sender"); - registeredSenders[sourceChain] = sourceAddress; - } -} - -abstract contract TokenBase is Base { - ITokenBridge public immutable tokenBridge; - - constructor(address _wormholeRelayer, address _tokenBridge, address _wormhole) Base(_wormholeRelayer, _wormhole) { - tokenBridge = ITokenBridge(_tokenBridge); - } - - function getDecimals(address tokenAddress) internal view returns (uint8 decimals) { - // query decimals - (, bytes memory queriedDecimals) = address(tokenAddress).staticcall(abi.encodeWithSignature("decimals()")); - decimals = abi.decode(queriedDecimals, (uint8)); - } - - function getTokenAddressOnThisChain(uint16 tokenHomeChain, bytes32 tokenHomeAddress) - internal - view - returns (address tokenAddressOnThisChain) - { - return tokenHomeChain == wormhole.chainId() - ? fromWormholeFormat(tokenHomeAddress) - : tokenBridge.wrappedAsset(tokenHomeChain, tokenHomeAddress); - } -} - -abstract contract TokenSender is TokenBase { - /** - * transferTokens wraps common boilerplate for sending tokens to another chain using IWormholeRelayer - * - approves tokenBridge to spend 'amount' of 'token' - * - emits token transfer VAA - * - returns VAA key for inclusion in WormholeRelayer `additionalVaas` argument - * - * Note: this function uses transferTokensWithPayload instead of transferTokens since the former requires that only the targetAddress - * can redeem transfers. Otherwise it's possible for another address to redeem the transfer before the targetContract is invoked by - * the offchain relayer and the target contract would have to be hardened against this. - * - */ - function transferTokens(address token, uint256 amount, uint16 targetChain, address targetAddress) - internal - returns (VaaKey memory) - { - return transferTokens(token, amount, targetChain, targetAddress, bytes("")); - } - - /** - * transferTokens wraps common boilerplate for sending tokens to another chain using IWormholeRelayer. - * A payload can be included in the transfer vaa. By including a payload here instead of the deliveryVaa, - * fewer trust assumptions are placed on the WormholeRelayer contract. - * - * - approves tokenBridge to spend 'amount' of 'token' - * - emits token transfer VAA - * - returns VAA key for inclusion in WormholeRelayer `additionalVaas` argument - * - * Note: this function uses transferTokensWithPayload instead of transferTokens since the former requires that only the targetAddress - * can redeem transfers. Otherwise it's possible for another address to redeem the transfer before the targetContract is invoked by - * the offchain relayer and the target contract would have to be hardened against this. - */ - function transferTokens( - address token, - uint256 amount, - uint16 targetChain, - address targetAddress, - bytes memory payload - ) internal returns (VaaKey memory) { - IERC20(token).approve(address(tokenBridge), amount); - uint64 sequence = tokenBridge.transferTokensWithPayload{value: wormhole.messageFee()}( - token, amount, targetChain, toWormholeFormat(targetAddress), 0, payload - ); - return VaaKey({ - emitterAddress: toWormholeFormat(address(tokenBridge)), - chainId: wormhole.chainId(), - sequence: sequence - }); - } - - function sendTokenWithPayloadToEvm( - uint16 targetChain, - address targetAddress, - bytes memory payload, - uint256 receiverValue, - uint256 gasLimit, - address token, - uint256 amount - ) internal returns (uint64) { - VaaKey[] memory vaaKeys = new VaaKey[](1); - vaaKeys[0] = transferTokens(token, amount, targetChain, targetAddress); - - (uint256 cost,) = wormholeRelayer.quoteEVMDeliveryPrice(targetChain, receiverValue, gasLimit); - return wormholeRelayer.sendVaasToEvm{value: cost}( - targetChain, targetAddress, payload, receiverValue, gasLimit, vaaKeys - ); - } - - function sendTokenWithPayloadToEvm( - uint16 targetChain, - address targetAddress, - bytes memory payload, - uint256 receiverValue, - uint256 gasLimit, - address token, - uint256 amount, - uint16 refundChain, - address refundAddress - ) internal returns (uint64) { - VaaKey[] memory vaaKeys = new VaaKey[](1); - vaaKeys[0] = transferTokens(token, amount, targetChain, targetAddress); - - (uint256 cost,) = wormholeRelayer.quoteEVMDeliveryPrice(targetChain, receiverValue, gasLimit); - return wormholeRelayer.sendVaasToEvm{value: cost}( - targetChain, targetAddress, payload, receiverValue, gasLimit, vaaKeys, refundChain, refundAddress - ); - } -} - -abstract contract TokenReceiver is TokenBase { - struct TokenReceived { - bytes32 tokenHomeAddress; - uint16 tokenHomeChain; - address tokenAddress; // wrapped address if tokenHomeChain !== this chain, else tokenHomeAddress (in evm address format) - uint256 amount; - uint256 amountNormalized; // if decimals > 8, normalized to 8 decimal places - } - - function receiveWormholeMessages( - bytes memory payload, - bytes[] memory additionalVaas, - bytes32 sourceAddress, - uint16 sourceChain, - bytes32 deliveryHash - ) external payable { - TokenReceived[] memory receivedTokens = new TokenReceived[](additionalVaas.length); - - for (uint256 i = 0; i < additionalVaas.length; ++i) { - IWormhole.VM memory parsed = wormhole.parseVM(additionalVaas[i]); - require( - parsed.emitterAddress == tokenBridge.bridgeContracts(parsed.emitterChainId), "Not a Token Bridge VAA" - ); - ITokenBridge.TransferWithPayload memory transfer = tokenBridge.parseTransferWithPayload(parsed.payload); - require( - transfer.to == toWormholeFormat(address(this)) && transfer.toChain == wormhole.chainId(), - "Token was not sent to this address" - ); - - tokenBridge.completeTransferWithPayload(additionalVaas[i]); - - address thisChainTokenAddress = getTokenAddressOnThisChain(transfer.tokenChain, transfer.tokenAddress); - uint8 decimals = getDecimals(thisChainTokenAddress); - uint256 denormalizedAmount = transfer.amount; - if (decimals > 8) denormalizedAmount *= uint256(10) ** (decimals - 8); - - receivedTokens[i] = TokenReceived({ - tokenHomeAddress: transfer.tokenAddress, - tokenHomeChain: transfer.tokenChain, - tokenAddress: thisChainTokenAddress, - amount: denormalizedAmount, - amountNormalized: transfer.amount - }); - } - - // call into overriden method - receivePayloadAndTokens(payload, receivedTokens, sourceAddress, sourceChain, deliveryHash); - } - - function receivePayloadAndTokens( - bytes memory payload, - TokenReceived[] memory receivedTokens, - bytes32 sourceAddress, - uint16 sourceChain, - bytes32 deliveryHash - ) internal virtual {} -} +import {Base} from "./Base.sol"; +import {TokenBase, TokenReceiver, TokenSender} from "./TokenBase.sol"; +import {CCTPBase, CCTPReceiver, CCTPSender} from "./CCTPBase.sol"; +import {CCTPAndTokenBase, CCTPAndTokenReceiver, CCTPAndTokenSender} from "./CCTPAndTokenBase.sol"; diff --git a/src/interfaces/CCTPInterfaces/IMessageTransmitter.sol b/src/interfaces/CCTPInterfaces/IMessageTransmitter.sol new file mode 100644 index 0000000..c1a12b3 --- /dev/null +++ b/src/interfaces/CCTPInterfaces/IMessageTransmitter.sol @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2022, Circle Internet Financial Limited. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +pragma solidity ^0.8.0; + +import "./IRelayer.sol"; +import "./IReceiver.sol"; + +/** + * @title IMessageTransmitter + * @notice Interface for message transmitters, which both relay and receive messages. + */ +interface IMessageTransmitter is IRelayer, IReceiver { + +} diff --git a/src/interfaces/CCTPInterfaces/IReceiver.sol b/src/interfaces/CCTPInterfaces/IReceiver.sol new file mode 100644 index 0000000..ff8821d --- /dev/null +++ b/src/interfaces/CCTPInterfaces/IReceiver.sol @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022, Circle Internet Financial Limited. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +pragma solidity ^0.8.0; + +/** + * @title IReceiver + * @notice Receives messages on destination chain and forwards them to IMessageDestinationHandler + */ +interface IReceiver { + /** + * @notice Receives an incoming message, validating the header and passing + * the body to application-specific handler. + * @param message The message raw bytes + * @param signature The message signature + * @return success bool, true if successful + */ + function receiveMessage(bytes calldata message, bytes calldata signature) + external + returns (bool success); +} diff --git a/src/interfaces/CCTPInterfaces/IRelayer.sol b/src/interfaces/CCTPInterfaces/IRelayer.sol new file mode 100644 index 0000000..a7f50d5 --- /dev/null +++ b/src/interfaces/CCTPInterfaces/IRelayer.sol @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2022, Circle Internet Financial Limited. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +pragma solidity ^0.8.0; + +/** + * @title IRelayer + * @notice Sends messages from source domain to destination domain + */ +interface IRelayer { + /** + * @notice Sends an outgoing message from the source domain. + * @dev Increment nonce, format the message, and emit `MessageSent` event with message information. + * @param destinationDomain Domain of destination chain + * @param recipient Address of message recipient on destination domain as bytes32 + * @param messageBody Raw bytes content of message + * @return nonce reserved by message + */ + function sendMessage( + uint32 destinationDomain, + bytes32 recipient, + bytes calldata messageBody + ) external returns (uint64); + + /** + * @notice Sends an outgoing message from the source domain, with a specified caller on the + * destination domain. + * @dev Increment nonce, format the message, and emit `MessageSent` event with message information. + * WARNING: if the `destinationCaller` does not represent a valid address as bytes32, then it will not be possible + * to broadcast the message on the destination domain. This is an advanced feature, and the standard + * sendMessage() should be preferred for use cases where a specific destination caller is not required. + * @param destinationDomain Domain of destination chain + * @param recipient Address of message recipient on destination domain as bytes32 + * @param destinationCaller caller on the destination domain, as bytes32 + * @param messageBody Raw bytes content of message + * @return nonce reserved by message + */ + function sendMessageWithCaller( + uint32 destinationDomain, + bytes32 recipient, + bytes32 destinationCaller, + bytes calldata messageBody + ) external returns (uint64); + + /** + * @notice Replace a message with a new message body and/or destination caller. + * @dev The `originalAttestation` must be a valid attestation of `originalMessage`. + * @param originalMessage original message to replace + * @param originalAttestation attestation of `originalMessage` + * @param newMessageBody new message body of replaced message + * @param newDestinationCaller the new destination caller + */ + function replaceMessage( + bytes calldata originalMessage, + bytes calldata originalAttestation, + bytes calldata newMessageBody, + bytes32 newDestinationCaller + ) external; +} diff --git a/src/interfaces/CCTPInterfaces/ITokenMessenger.sol b/src/interfaces/CCTPInterfaces/ITokenMessenger.sol new file mode 100644 index 0000000..29e700e --- /dev/null +++ b/src/interfaces/CCTPInterfaces/ITokenMessenger.sol @@ -0,0 +1,34 @@ + +pragma solidity ^0.8.0; + +interface ITokenMessenger { + /** + * @notice Deposits and burns tokens from sender to be minted on destination domain. The mint + * on the destination domain must be called by `destinationCaller`. + * WARNING: if the `destinationCaller` does not represent a valid address as bytes32, then it will not be possible + * to broadcast the message on the destination domain. This is an advanced feature, and the standard + * depositForBurn() should be preferred for use cases where a specific destination caller is not required. + * Emits a `DepositForBurn` event. + * @dev reverts if: + * - given destinationCaller is zero address + * - given burnToken is not supported + * - given destinationDomain has no TokenMessenger registered + * - transferFrom() reverts. For example, if sender's burnToken balance or approved allowance + * to this contract is less than `amount`. + * - burn() reverts. For example, if `amount` is 0. + * - MessageTransmitter returns false or reverts. + * @param amount amount of tokens to burn + * @param destinationDomain destination domain + * @param mintRecipient address of mint recipient on destination domain + * @param burnToken address of contract to burn deposited tokens, on local domain + * @param destinationCaller caller on the destination domain, as bytes32 + * @return nonce unique nonce reserved by message + */ + function depositForBurnWithCaller( + uint256 amount, + uint32 destinationDomain, + bytes32 mintRecipient, + address burnToken, + bytes32 destinationCaller + ) external returns (uint64 nonce); +} diff --git a/src/interfaces/IWormholeReceiver.sol b/src/interfaces/IWormholeReceiver.sol index 6ef7d89..fa5e594 100644 --- a/src/interfaces/IWormholeReceiver.sol +++ b/src/interfaces/IWormholeReceiver.sol @@ -12,11 +12,7 @@ interface IWormholeReceiver { * * NOTE: This function should be restricted such that only the Wormhole Relayer contract can call it. * - * We also recommend that this function: - * - Stores all received `deliveryHash`s in a mapping `(bytes32 => bool)`, and - * on every call, checks that deliveryHash has not already been stored in the - * map (This is to prevent other users maliciously trying to relay the same message) - * - Checks that `sourceChain` and `sourceAddress` are indeed who + * We also recommend that this function checks that `sourceChain` and `sourceAddress` are indeed who * you expect to have requested the calling of `send` on the source chain * * The invocation of this function corresponding to the `send` request will have msg.value equal @@ -26,22 +22,28 @@ interface IWormholeReceiver { * specified by the send requester, this delivery will result in a `ReceiverFailure`. * * @param payload - an arbitrary message which was included in the delivery by the - * requester. - * @param additionalVaas - Additional VAAs which were requested to be included in this delivery. - * They are guaranteed to all be included and in the same order as was specified in the - * delivery request. + * requester. This message's signature will already have been verified (as long as msg.sender is the Wormhole Relayer contract) + * @param additionalMessages - Additional messages which were requested to be included in this delivery. + * Note: There are no contract-level guarantees that the messages in this array are what was requested + * so **you should verify any sensitive information given here!** + * + * For example, if a 'VaaKey' was specified on the source chain, then MAKE SURE the corresponding message here + * has valid signatures (by calling `parseAndVerifyVM(message)` on the Wormhole core contract) + * + * This field can be used to perform and relay TokenBridge or CCTP transfers, and there are example + * usages of this at + * https://github.com/wormhole-foundation/hello-token + * https://github.com/wormhole-foundation/hello-cctp + * * @param sourceAddress - the (wormhole format) address on the sending chain which requested * this delivery. * @param sourceChain - the wormhole chain ID where this delivery was requested. * @param deliveryHash - the VAA hash of the deliveryVAA. * - * NOTE: These signedVaas are NOT verified by the Wormhole core contract prior to being provided - * to this call. Always make sure `parseAndVerify()` is called on the Wormhole core contract - * before trusting the content of a raw VAA, otherwise the VAA may be invalid or malicious. */ function receiveWormholeMessages( bytes memory payload, - bytes[] memory additionalVaas, + bytes[] memory additionalMessages, bytes32 sourceAddress, uint16 sourceChain, bytes32 deliveryHash diff --git a/src/interfaces/IWormholeRelayer.sol b/src/interfaces/IWormholeRelayer.sol index a316758..520a1cc 100644 --- a/src/interfaces/IWormholeRelayer.sol +++ b/src/interfaces/IWormholeRelayer.sol @@ -8,7 +8,7 @@ pragma solidity ^0.8.0; * @notice This project allows developers to build cross-chain applications powered by Wormhole without needing to * write and run their own relaying infrastructure * - * We implement the IWormholeRelayer interface that allows users to request a delivery provider to relay a payload (and/or additional VAAs) + * We implement the IWormholeRelayer interface that allows users to request a delivery provider to relay a payload (and/or additional messages) * to a chain and address of their choice. */ @@ -25,10 +25,46 @@ struct VaaKey { uint64 sequence; } +// 0-127 are reserved for standardized KeyTypes, 128-255 are for custom use +uint8 constant VAA_KEY_TYPE = 1; + +struct MessageKey { + uint8 keyType; // 0-127 are reserved for standardized KeyTypes, 128-255 are for custom use + bytes encodedKey; +} + interface IWormholeRelayerBase { - event SendEvent(uint64 indexed sequence, uint256 deliveryQuote, uint256 paymentForExtraReceiverValue); + event SendEvent( + uint64 indexed sequence, + uint256 deliveryQuote, + uint256 paymentForExtraReceiverValue + ); + + function getRegisteredWormholeRelayerContract( + uint16 chainId + ) external view returns (bytes32); + + /** + * @notice Returns true if a delivery has been attempted for the given deliveryHash + * Note: invalid deliveries where the tx reverts are not considered attempted + */ + function deliveryAttempted( + bytes32 deliveryHash + ) external view returns (bool attempted); - function getRegisteredWormholeRelayerContract(uint16 chainId) external view returns (bytes32); + /** + * @notice block number at which a delivery was successfully executed + */ + function deliverySuccessBlock( + bytes32 deliveryHash + ) external view returns (uint256 blockNumber); + + /** + * @notice block number of the latest attempt to execute a delivery that failed + */ + function deliveryFailureBlock( + bytes32 deliveryHash + ) external view returns (uint256 blockNumber); } /** @@ -196,6 +232,51 @@ interface IWormholeRelayerSend is IWormholeRelayerBase { uint8 consistencyLevel ) external payable returns (uint64 sequence); + /** + * @notice Publishes an instruction for the delivery provider at `deliveryProviderAddress` + * to relay a payload and external messages specified by `messageKeys` to the address `targetAddress` on chain `targetChain` + * with gas limit `gasLimit` and `msg.value` equal to + * receiverValue + (arbitrary amount that is paid for by paymentForExtraReceiverValue of this chain's wei) in targetChain wei. + * + * Any refunds (from leftover gas) will be sent to `refundAddress` on chain `refundChain` + * `targetAddress` must implement the IWormholeReceiver interface + * + * This function must be called with `msg.value` equal to + * quoteEVMDeliveryPrice(targetChain, receiverValue, gasLimit, deliveryProviderAddress) + paymentForExtraReceiverValue + * + * Note: MessageKeys can specify wormhole messages (VaaKeys) or other types of messages (ex. USDC CCTP attestations). Ensure the selected + * DeliveryProvider supports all the MessageKey.keyType values specified or it will not be delivered! + * + * @param targetChain in Wormhole Chain ID format + * @param targetAddress address to call on targetChain (that implements IWormholeReceiver) + * @param payload arbitrary bytes to pass in as parameter in call to `targetAddress` + * @param receiverValue msg.value that delivery provider should pass in for call to `targetAddress` (in targetChain currency units) + * @param paymentForExtraReceiverValue amount (in current chain currency units) to spend on extra receiverValue + * (in addition to the `receiverValue` specified) + * @param gasLimit gas limit with which to call `targetAddress`. Any units of gas unused will be refunded according to the + * `targetChainRefundPerGasUnused` rate quoted by the delivery provider + * @param refundChain The chain to deliver any refund to, in Wormhole Chain ID format + * @param refundAddress The address on `refundChain` to deliver any refund to + * @param deliveryProviderAddress The address of the desired delivery provider's implementation of IDeliveryProvider + * @param messageKeys Additional messagess to pass in as parameter in call to `targetAddress` + * @param consistencyLevel Consistency level with which to publish the delivery instructions - see + * https://book.wormhole.com/wormhole/3_coreLayerContracts.html?highlight=consistency#consistency-levels + * @return sequence sequence number of published VAA containing delivery instructions + */ + function sendToEvm( + uint16 targetChain, + address targetAddress, + bytes memory payload, + uint256 receiverValue, + uint256 paymentForExtraReceiverValue, + uint256 gasLimit, + uint16 refundChain, + address refundAddress, + address deliveryProviderAddress, + MessageKey[] memory messageKeys, + uint8 consistencyLevel + ) external payable returns (uint64 sequence); + /** * @notice Publishes an instruction for the delivery provider at `deliveryProviderAddress` * to relay a payload and VAAs specified by `vaaKeys` to the address `targetAddress` on chain `targetChain` @@ -238,6 +319,51 @@ interface IWormholeRelayerSend is IWormholeRelayerBase { uint8 consistencyLevel ) external payable returns (uint64 sequence); + /** + * @notice Publishes an instruction for the delivery provider at `deliveryProviderAddress` + * to relay a payload and VAAs specified by `vaaKeys` to the address `targetAddress` on chain `targetChain` + * with `msg.value` equal to + * receiverValue + (arbitrary amount that is paid for by paymentForExtraReceiverValue of this chain's wei) in targetChain wei. + * + * Any refunds (from leftover gas) will be sent to `refundAddress` on chain `refundChain` + * `targetAddress` must implement the IWormholeReceiver interface + * + * This function must be called with `msg.value` equal to + * quoteDeliveryPrice(targetChain, receiverValue, encodedExecutionParameters, deliveryProviderAddress) + paymentForExtraReceiverValue + * + * Note: MessageKeys can specify wormhole messages (VaaKeys) or other types of messages (ex. USDC CCTP attestations). Ensure the selected + * DeliveryProvider supports all the MessageKey.keyType values specified or it will not be delivered! + * + * @param targetChain in Wormhole Chain ID format + * @param targetAddress address to call on targetChain (that implements IWormholeReceiver), in Wormhole bytes32 format + * @param payload arbitrary bytes to pass in as parameter in call to `targetAddress` + * @param receiverValue msg.value that delivery provider should pass in for call to `targetAddress` (in targetChain currency units) + * @param paymentForExtraReceiverValue amount (in current chain currency units) to spend on extra receiverValue + * (in addition to the `receiverValue` specified) + * @param encodedExecutionParameters encoded information on how to execute delivery that may impact pricing + * e.g. for version EVM_V1, this is a struct that encodes the `gasLimit` with which to call `targetAddress` + * @param refundChain The chain to deliver any refund to, in Wormhole Chain ID format + * @param refundAddress The address on `refundChain` to deliver any refund to, in Wormhole bytes32 format + * @param deliveryProviderAddress The address of the desired delivery provider's implementation of IDeliveryProvider + * @param messageKeys Additional messagess to pass in as parameter in call to `targetAddress` + * @param consistencyLevel Consistency level with which to publish the delivery instructions - see + * https://book.wormhole.com/wormhole/3_coreLayerContracts.html?highlight=consistency#consistency-levels + * @return sequence sequence number of published VAA containing delivery instructions + */ + function send( + uint16 targetChain, + bytes32 targetAddress, + bytes memory payload, + uint256 receiverValue, + uint256 paymentForExtraReceiverValue, + bytes memory encodedExecutionParameters, + uint16 refundChain, + bytes32 refundAddress, + address deliveryProviderAddress, + MessageKey[] memory messageKeys, + uint8 consistencyLevel + ) external payable returns (uint64 sequence); + /** * @notice Requests a previously published delivery instruction to be redelivered * (e.g. with a different delivery provider) @@ -262,7 +388,6 @@ interface IWormholeRelayerSend is IWormholeRelayerBase { * @notice *** This will only be able to succeed if the following is true ** * - newGasLimit >= gas limit of the old instruction * - newReceiverValue >= receiver value of the old instruction - * - newDeliveryProvider's `targetChainRefundPerGasUnused` >= old relay provider's `targetChainRefundPerGasUnused` */ function resendToEvm( VaaKey memory deliveryVaaKey, @@ -309,12 +434,22 @@ interface IWormholeRelayerSend is IWormholeRelayerBase { * @param gasLimit gas limit with which to call `targetAddress`. * @return nativePriceQuote Price, in units of current chain currency, that the delivery provider charges to perform the relay * @return targetChainRefundPerGasUnused amount of target chain currency that will be refunded per unit of gas unused, - * if a refundAddress is specified + * if a refundAddress is specified. + * Note: This value can be overridden by the delivery provider on the target chain. The returned value here should be considered to be a + * promise by the delivery provider of the amount of refund per gas unused that will be returned to the refundAddress at the target chain. + * If a delivery provider decides to override, this will be visible as part of the emitted Delivery event on the target chain. */ - function quoteEVMDeliveryPrice(uint16 targetChain, uint256 receiverValue, uint256 gasLimit) + function quoteEVMDeliveryPrice( + uint16 targetChain, + uint256 receiverValue, + uint256 gasLimit + ) external view - returns (uint256 nativePriceQuote, uint256 targetChainRefundPerGasUnused); + returns ( + uint256 nativePriceQuote, + uint256 targetChainRefundPerGasUnused + ); /** * @notice Returns the price to request a relay to chain `targetChain`, using delivery provider `deliveryProviderAddress` @@ -326,13 +461,22 @@ interface IWormholeRelayerSend is IWormholeRelayerBase { * @return nativePriceQuote Price, in units of current chain currency, that the delivery provider charges to perform the relay * @return targetChainRefundPerGasUnused amount of target chain currency that will be refunded per unit of gas unused, * if a refundAddress is specified + * Note: This value can be overridden by the delivery provider on the target chain. The returned value here should be considered to be a + * promise by the delivery provider of the amount of refund per gas unused that will be returned to the refundAddress at the target chain. + * If a delivery provider decides to override, this will be visible as part of the emitted Delivery event on the target chain. */ function quoteEVMDeliveryPrice( uint16 targetChain, uint256 receiverValue, uint256 gasLimit, address deliveryProviderAddress - ) external view returns (uint256 nativePriceQuote, uint256 targetChainRefundPerGasUnused); + ) + external + view + returns ( + uint256 nativePriceQuote, + uint256 targetChainRefundPerGasUnused + ); /** * @notice Returns the price to request a relay to chain `targetChain`, using delivery provider `deliveryProviderAddress` @@ -353,7 +497,10 @@ interface IWormholeRelayerSend is IWormholeRelayerBase { uint256 receiverValue, bytes memory encodedExecutionParameters, address deliveryProviderAddress - ) external view returns (uint256 nativePriceQuote, bytes memory encodedExecutionInfo); + ) + external + view + returns (uint256 nativePriceQuote, bytes memory encodedExecutionInfo); /** * @notice Returns the (extra) amount of target chain currency that `targetAddress` @@ -365,17 +512,21 @@ interface IWormholeRelayerSend is IWormholeRelayerBase { * @return targetChainAmount The amount such that if `targetAddress` will be called with `msg.value` equal to * receiverValue + targetChainAmount */ - function quoteNativeForChain(uint16 targetChain, uint256 currentChainAmount, address deliveryProviderAddress) - external - view - returns (uint256 targetChainAmount); + function quoteNativeForChain( + uint16 targetChain, + uint256 currentChainAmount, + address deliveryProviderAddress + ) external view returns (uint256 targetChainAmount); /** * @notice Returns the address of the current default delivery provider * @return deliveryProvider The address of (the default delivery provider)'s contract on this source * chain. This must be a contract that implements IDeliveryProvider. */ - function getDefaultDeliveryProvider() external view returns (address deliveryProvider); + function getDefaultDeliveryProvider() + external + view + returns (address deliveryProvider); } /** @@ -393,7 +544,8 @@ interface IWormholeRelayerDelivery is IWormholeRelayerBase { REFUND_FAIL, CROSS_CHAIN_REFUND_SENT, CROSS_CHAIN_REFUND_FAIL_PROVIDER_NOT_SUPPORTED, - CROSS_CHAIN_REFUND_FAIL_NOT_ENOUGH + CROSS_CHAIN_REFUND_FAIL_NOT_ENOUGH, + NO_REFUND_REQUESTED } /** @@ -407,14 +559,14 @@ interface IWormholeRelayerDelivery is IWormholeRelayerBase { * @custom:member gasUsed - The amount of gas that was used to call your target contract * @custom:member status: * - RECEIVER_FAILURE, if the target contract reverts - * - SUCCESS, if the target contract doesn't revert + * - SUCCESS, if the target contract doesn't revert * @custom:member additionalStatusInfo: * - If status is SUCCESS, then this is empty. * - If status is RECEIVER_FAILURE, this is `RETURNDATA_TRUNCATION_THRESHOLD` bytes of the * return data (i.e. potentially truncated revert reason information). * @custom:member refundStatus - Result of the refund. REFUND_SUCCESS or REFUND_FAIL are for * refunds where targetChain=refundChain; the others are for targetChain!=refundChain, - * where a cross chain refund is necessary + * where a cross chain refund is necessary, or if the default code path is used where no refund is requested (NO_REFUND_REQUESTED) * @custom:member overridesInfo: * - If not an override: empty bytes array * - Otherwise: An encoded `DeliveryOverride` @@ -477,8 +629,12 @@ error InvalidMsgValue(uint256 msgValue, uint256 totalFee); error RequestedGasLimitTooLow(); -error DeliveryProviderDoesNotSupportTargetChain(address relayer, uint16 chainId); +error DeliveryProviderDoesNotSupportTargetChain( + address relayer, + uint16 chainId +); error DeliveryProviderCannotReceivePayment(); +error DeliveryProviderDoesNotSupportMessageKeyType(uint8 keyType); //When calling `delivery()` a second time even though a delivery is already in progress error ReentrantDelivery(address msgSender, address lockedBy); @@ -486,12 +642,13 @@ error ReentrantDelivery(address msgSender, address lockedBy); error InvalidPayloadId(uint8 parsed, uint8 expected); error InvalidPayloadLength(uint256 received, uint256 expected); error InvalidVaaKeyType(uint8 parsed); +error TooManyMessageKeys(uint256 numMessageKeys); error InvalidDeliveryVaa(string reason); //When the delivery VAA (signed wormhole message with delivery instructions) was not emitted by the // registered WormholeRelayer contract error InvalidEmitter(bytes32 emitter, bytes32 registered, uint16 chainId); -error VaaKeysLengthDoesNotMatchVaasLength(uint256 keys, uint256 vaas); +error MessageKeysLengthDoesNotMatchMessagesLength(uint256 keys, uint256 vaas); error VaaKeysDoNotMatchVaas(uint8 index); //When someone tries to call an external function of the WormholeRelayer that is only intended to be // called by the WormholeRelayer itself (to allow retroactive reverts for atomicity) diff --git a/src/testing/ERC20Mock.sol b/src/testing/ERC20Mock.sol index 3fdee7a..848ec99 100644 --- a/src/testing/ERC20Mock.sol +++ b/src/testing/ERC20Mock.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: UNLICENSED + pragma solidity ^0.8.13; import "../interfaces/IERC20.sol"; @@ -199,4 +199,8 @@ contract ERC20Mock is ERC20 { function mint(address to, uint256 amount) public { _mint(to, amount); } + + function burn(address from, uint256 amount) public { + _burn(from, amount); + } } diff --git a/src/testing/WormholeRelayerTest.sol b/src/testing/WormholeRelayerTest.sol index a116f00..b6851d5 100644 --- a/src/testing/WormholeRelayerTest.sol +++ b/src/testing/WormholeRelayerTest.sol @@ -1,12 +1,14 @@ -// SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.13; import "../../src/interfaces/IWormholeRelayer.sol"; import "../../src/interfaces/IWormhole.sol"; import "../../src/interfaces/ITokenBridge.sol"; +import "../../src/interfaces/CCTPInterfaces/IMessageTransmitter.sol"; +import "../../src/interfaces/CCTPInterfaces/ITokenMessenger.sol"; import "../../src/Utils.sol"; import "./helpers/WormholeSimulator.sol"; +import "./helpers/CircleCCTPSimulator.sol"; import "./ERC20Mock.sol"; import "./helpers/DeliveryInstructionDecoder.sol"; import "./helpers/ExecutionParameters.sol"; @@ -22,6 +24,20 @@ struct ChainInfo { IWormholeRelayer relayer; ITokenBridge tokenBridge; IWormhole wormhole; + IMessageTransmitter circleMessageTransmitter; + ITokenMessenger circleTokenMessenger; + IERC20 USDC; +} + +interface USDCMinter { + function mint(address _to, uint256 _amount) external returns (bool); + + function masterMinter() external returns (address); + + function configureMinter( + address minter, + uint256 minterAllowedAmount + ) external returns (bool); } struct ActiveFork { @@ -33,6 +49,11 @@ struct ActiveFork { ITokenBridge tokenBridge; IWormhole wormhole; WormholeSimulator guardian; + // USDC parameters - only non-empty for Ethereum, Avalanche, Optimism, Arbitrum mainnets/testnets + IERC20 USDC; + ITokenMessenger circleTokenMessenger; + IMessageTransmitter circleMessageTransmitter; + CircleMessageTransmitterSimulator circleAttester; } abstract contract WormholeRelayerTest is Test { @@ -46,7 +67,10 @@ abstract contract WormholeRelayerTest is Test { */ function setUpGeneral() public virtual {} - uint256 constant DEVNET_GUARDIAN_PK = 0xcfb12303a19cde580bb4dd771639b0d26bc68353645571a8cff516ab2ee113a0; + uint256 constant DEVNET_GUARDIAN_PK = + 0xcfb12303a19cde580bb4dd771639b0d26bc68353645571a8cff516ab2ee113a0; + uint256 constant CIRCLE_ATTESTER_PK = + 0xcfb12303a19cde580bb4dd771639b0d26bc68353645571a8cff516ab2ee113a0; // conveneince information to set up tests against testnet/mainnet forks mapping(uint16 => ChainInfo) public chainInfosTestnet; @@ -85,7 +109,12 @@ abstract contract WormholeRelayerTest is Test { wormhole: chainInfos[i].wormhole, // patch these in setUp() once we have the fork fork: 0, - guardian: WormholeSimulator(address(0)) + guardian: WormholeSimulator(address(0)), + circleMessageTransmitter: chainInfos[i] + .circleMessageTransmitter, + circleTokenMessenger: chainInfos[i].circleTokenMessenger, + USDC: chainInfos[i].USDC, + circleAttester: CircleMessageTransmitterSimulator(address(0)) }); } } @@ -108,6 +137,10 @@ abstract contract WormholeRelayerTest is Test { address(fork.wormhole), DEVNET_GUARDIAN_PK ); + fork.circleAttester = new CircleMessageTransmitterSimulator( + address(fork.circleMessageTransmitter), + CIRCLE_ATTESTER_PK + ); } // run setUp virtual functions for each fork @@ -119,11 +152,19 @@ abstract contract WormholeRelayerTest is Test { ActiveFork memory firstFork = activeForks[activeForksList[0]]; vm.selectFork(firstFork.fork); - mockOffchainRelayer = new MockOffchainRelayer(address(firstFork.wormhole), address(firstFork.guardian), vm); + mockOffchainRelayer = new MockOffchainRelayer( + address(firstFork.wormhole), + address(firstFork.guardian), + address(firstFork.circleAttester) + ); // register all active forks with the 'offchain' relayer for (uint256 i = 0; i < activeForksList.length; ++i) { ActiveFork storage fork = activeForks[activeForksList[i]]; - mockOffchainRelayer.registerChain(fork.chainId, address(fork.relayer), fork.fork); + mockOffchainRelayer.registerChain( + fork.chainId, + address(fork.relayer), + fork.fork + ); } // Allow the offchain relayer to work on all forks @@ -139,6 +180,10 @@ abstract contract WormholeRelayerTest is Test { performDelivery(vm.getRecordedLogs()); } + function performDelivery(bool debugLogging) public { + performDelivery(vm.getRecordedLogs(), debugLogging); + } + function performDelivery(Vm.Log[] memory logs, bool debugLogging) public { require(logs.length > 0, "no events recorded"); mockOffchainRelayer.relay(logs, debugLogging); @@ -149,7 +194,9 @@ abstract contract WormholeRelayerTest is Test { mockOffchainRelayer.relay(logs); } - function createAndAttestToken(uint16 homeChain) public returns (ERC20Mock token) { + function createAndAttestToken( + uint16 homeChain + ) public returns (ERC20Mock token) { uint256 originalFork = vm.activeFork(); ActiveFork memory home = activeForks[homeChain]; vm.selectFork(home.fork); @@ -159,11 +206,16 @@ abstract contract WormholeRelayerTest is Test { vm.recordLogs(); home.tokenBridge.attestToken(address(token), 0); - Vm.Log memory log = home.guardian.fetchWormholeMessageFromLog(vm.getRecordedLogs())[0]; - bytes memory attestation = home.guardian.fetchSignedMessageFromLogs(log, home.chainId); + Vm.Log memory log = home.guardian.fetchWormholeMessageFromLog( + vm.getRecordedLogs() + )[0]; + bytes memory attestation = home.guardian.fetchSignedMessageFromLogs( + log, + home.chainId + ); for (uint256 i = 0; i < activeForksList.length; ++i) { - if(activeForksList[i] == home.chainId) { + if (activeForksList[i] == home.chainId) { continue; } ActiveFork memory fork = activeForks[activeForksList[i]]; @@ -174,145 +226,441 @@ abstract contract WormholeRelayerTest is Test { vm.selectFork(originalFork); } + function mintUSDC(uint16 chain, address addr, uint256 amount) public { + uint256 originalFork = vm.activeFork(); + ActiveFork memory current = activeForks[chain]; + vm.selectFork(current.fork); + + USDCMinter tokenMinter = USDCMinter(address(current.USDC)); + vm.prank(tokenMinter.masterMinter()); + tokenMinter.configureMinter(address(this), amount); + tokenMinter.mint(addr, amount); + + vm.selectFork(originalFork); + } + function logFork() public view { uint256 fork = vm.activeFork(); for (uint256 i = 0; i < activeForksList.length; ++i) { if (fork == activeForks[activeForksList[i]].fork) { - console.log("%s fork active", activeForks[activeForksList[i]].name); + console.log( + "%s fork active", + activeForks[activeForksList[i]].name + ); return; } } } function initChainInfo() private { + chainInfosTestnet[2] = ChainInfo({ + chainId: 2, + name: "goerli - ethereum", + url: "https://ethereum-goerli.publicnode.com", + relayer: IWormholeRelayer( + 0x28D8F1Be96f97C1387e94A53e00eCcFb4E75175a + ), + tokenBridge: ITokenBridge( + 0xF890982f9310df57d00f659cf4fd87e65adEd8d7 + ), + wormhole: IWormhole(0x706abc4E45D419950511e474C7B9Ed348A4a716c), + circleMessageTransmitter: IMessageTransmitter( + 0x26413e8157CD32011E726065a5462e97dD4d03D9 + ), + circleTokenMessenger: ITokenMessenger( + 0xD0C3da58f55358142b8d3e06C1C30c5C6114EFE8 + ), + USDC: IERC20(0x07865c6E87B9F70255377e024ace6630C1Eaa37F) + }); chainInfosTestnet[6] = ChainInfo({ chainId: 6, name: "fuji - avalanche", - url: vm.envOr("AVALANCHE_FUJI_RPC_URL", string("https://api.avax-test.network/ext/bc/C/rpc")), - relayer: IWormholeRelayer(0xA3cF45939bD6260bcFe3D66bc73d60f19e49a8BB), - tokenBridge: ITokenBridge(0x61E44E506Ca5659E6c0bba9b678586fA2d729756), - wormhole: IWormhole(0x7bbcE28e64B3F8b84d876Ab298393c38ad7aac4C) + url: vm.envOr( + "AVALANCHE_FUJI_RPC_URL", + string("https://api.avax-test.network/ext/bc/C/rpc") + ), + relayer: IWormholeRelayer( + 0xA3cF45939bD6260bcFe3D66bc73d60f19e49a8BB + ), + tokenBridge: ITokenBridge( + 0x61E44E506Ca5659E6c0bba9b678586fA2d729756 + ), + wormhole: IWormhole(0x7bbcE28e64B3F8b84d876Ab298393c38ad7aac4C), + circleMessageTransmitter: IMessageTransmitter( + 0xa9fB1b3009DCb79E2fe346c16a604B8Fa8aE0a79 + ), + circleTokenMessenger: ITokenMessenger( + 0xeb08f243E5d3FCFF26A9E38Ae5520A669f4019d0 + ), + USDC: IERC20(0x5425890298aed601595a70AB815c96711a31Bc65) }); chainInfosTestnet[14] = ChainInfo({ chainId: 14, name: "alfajores - celo", - url: vm.envOr("CELO_TESTNET_RPC_URL", string("https://alfajores-forno.celo-testnet.org")), - relayer: IWormholeRelayer(0x306B68267Deb7c5DfCDa3619E22E9Ca39C374f84), - tokenBridge: ITokenBridge(0x05ca6037eC51F8b712eD2E6Fa72219FEaE74E153), - wormhole: IWormhole(0x88505117CA88e7dd2eC6EA1E13f0948db2D50D56) + url: vm.envOr( + "CELO_TESTNET_RPC_URL", + string("https://alfajores-forno.celo-testnet.org") + ), + relayer: IWormholeRelayer( + 0x306B68267Deb7c5DfCDa3619E22E9Ca39C374f84 + ), + tokenBridge: ITokenBridge( + 0x05ca6037eC51F8b712eD2E6Fa72219FEaE74E153 + ), + wormhole: IWormhole(0x88505117CA88e7dd2eC6EA1E13f0948db2D50D56), + circleMessageTransmitter: IMessageTransmitter(address(0)), + circleTokenMessenger: ITokenMessenger(address(0)), + USDC: IERC20(address(0)) }); chainInfosTestnet[4] = ChainInfo({ chainId: 4, name: "bsc testnet", - url: vm.envOr("BSC_TESTNET_RPC_URL", string("https://bsc-testnet.public.blastapi.io")), - relayer: IWormholeRelayer(0x80aC94316391752A193C1c47E27D382b507c93F3), - tokenBridge: ITokenBridge(0x9dcF9D205C9De35334D646BeE44b2D2859712A09), - wormhole: IWormhole(0x68605AD7b15c732a30b1BbC62BE8F2A509D74b4D) + url: vm.envOr( + "BSC_TESTNET_RPC_URL", + string("https://bsc-testnet.public.blastapi.io") + ), + relayer: IWormholeRelayer( + 0x80aC94316391752A193C1c47E27D382b507c93F3 + ), + tokenBridge: ITokenBridge( + 0x9dcF9D205C9De35334D646BeE44b2D2859712A09 + ), + wormhole: IWormhole(0x68605AD7b15c732a30b1BbC62BE8F2A509D74b4D), + circleMessageTransmitter: IMessageTransmitter(address(0)), + circleTokenMessenger: ITokenMessenger(address(0)), + USDC: IERC20(address(0)) }); chainInfosTestnet[5] = ChainInfo({ chainId: 5, name: "polygon mumbai", - url: vm.envOr("POLYGON_MUMBAI_RPC_URL", string("https://rpc.ankr.com/polygon_mumbai")), - relayer: IWormholeRelayer(0x0591C25ebd0580E0d4F27A82Fc2e24E7489CB5e0), - tokenBridge: ITokenBridge(0x377D55a7928c046E18eEbb61977e714d2a76472a), - wormhole: IWormhole(0x0CBE91CF822c73C2315FB05100C2F714765d5c20) + url: vm.envOr( + "POLYGON_MUMBAI_RPC_URL", + string("https://rpc.ankr.com/polygon_mumbai") + ), + relayer: IWormholeRelayer( + 0x0591C25ebd0580E0d4F27A82Fc2e24E7489CB5e0 + ), + tokenBridge: ITokenBridge( + 0x377D55a7928c046E18eEbb61977e714d2a76472a + ), + wormhole: IWormhole(0x0CBE91CF822c73C2315FB05100C2F714765d5c20), + circleMessageTransmitter: IMessageTransmitter(address(0)), + circleTokenMessenger: ITokenMessenger(address(0)), + USDC: IERC20(address(0)) }); chainInfosTestnet[16] = ChainInfo({ chainId: 16, name: "moonbase alpha - moonbeam", - url: vm.envOr("MOONBASE_ALPHA_RPC_URL", string("https://rpc.testnet.moonbeam.network")), - relayer: IWormholeRelayer(0x0591C25ebd0580E0d4F27A82Fc2e24E7489CB5e0), - tokenBridge: ITokenBridge(0xbc976D4b9D57E57c3cA52e1Fd136C45FF7955A96), - wormhole: IWormhole(0xa5B7D85a8f27dd7907dc8FdC21FA5657D5E2F901) + url: vm.envOr( + "MOONBASE_ALPHA_RPC_URL", + string("https://rpc.testnet.moonbeam.network") + ), + relayer: IWormholeRelayer( + 0x0591C25ebd0580E0d4F27A82Fc2e24E7489CB5e0 + ), + tokenBridge: ITokenBridge( + 0xbc976D4b9D57E57c3cA52e1Fd136C45FF7955A96 + ), + wormhole: IWormhole(0xa5B7D85a8f27dd7907dc8FdC21FA5657D5E2F901), + circleMessageTransmitter: IMessageTransmitter(address(0)), + circleTokenMessenger: ITokenMessenger(address(0)), + USDC: IERC20(address(0)) + }); + chainInfosTestnet[23] = ChainInfo({ + chainId: 23, + name: "goerli - arbitrum", + url: vm.envOr( + "ARBITRUM_TESTNET_RPC_URL", + string("https://arbitrum-goerli.publicnode.com") + ), + relayer: IWormholeRelayer( + 0xAd753479354283eEE1b86c9470c84D42f229FF43 + ), + tokenBridge: ITokenBridge( + 0x23908A62110e21C04F3A4e011d24F901F911744A + ), + wormhole: IWormhole(0xC7A204bDBFe983FCD8d8E61D02b475D4073fF97e), + circleMessageTransmitter: IMessageTransmitter( + 0x109bC137cb64eAb7c0B1ddDd1Edf341467dC2D35 + ), + circleTokenMessenger: ITokenMessenger( + 0x12Dcfd3Fe2E9EAc2859fd1Ed86d2ab8C5a2f9352 + ), + USDC: IERC20(0xfd064A18f3BF249cf1f87FC203E90D8f650f2d63) + }); + chainInfosTestnet[24] = ChainInfo({ + chainId: 24, + name: "goerli - optimism", + url: vm.envOr( + "OPTIMISM_TESTNET_RPC_URL", + string("https://optimism-goerli.publicnode.com") + ), + relayer: IWormholeRelayer( + 0x01A957A525a5b7A72808bA9D10c389674E459891 + ), + tokenBridge: ITokenBridge( + 0xC7A204bDBFe983FCD8d8E61D02b475D4073fF97e + ), + wormhole: IWormhole(0x6b9C8671cdDC8dEab9c719bB87cBd3e782bA6a35), + circleMessageTransmitter: IMessageTransmitter( + 0x9ff9a4da6f2157A9c82CE756f8fD7E0d75be8895 + ), + circleTokenMessenger: ITokenMessenger( + 0x23A04D5935ed8bC8E3eB78dB3541f0ABfB001c6e + ), + USDC: IERC20(0xe05606174bac4A6364B31bd0eCA4bf4dD368f8C6) + }); + chainInfosTestnet[30] = ChainInfo({ + chainId: 30, + name: "goerli - base", + url: vm.envOr( + "BASE_TESTNET_RPC_URL", + string("https://goerli.base.org") + ), + relayer: IWormholeRelayer( + 0xea8029CD7FCAEFFcD1F53686430Db0Fc8ed384E1 + ), + tokenBridge: ITokenBridge( + 0xA31aa3FDb7aF7Db93d18DDA4e19F811342EDF780 + ), + wormhole: IWormhole(0x23908A62110e21C04F3A4e011d24F901F911744A), + circleMessageTransmitter: IMessageTransmitter( + address(0x9ff9a4da6f2157A9c82CE756f8fD7E0d75be8895) + ), + circleTokenMessenger: ITokenMessenger( + address(0x877b8e8c9e2383077809787ED6F279ce01CB4cc8) + ), + USDC: IERC20(address(0xF175520C52418dfE19C8098071a252da48Cd1C19)) }); - chainInfosMainnet[2] = ChainInfo({ chainId: 2, name: "ethereum", - url: vm.envOr("ETHEREUM_RPC_URL", string("https://rpc.ankr.com/eth")), - relayer: IWormholeRelayer(0x27428DD2d3DD32A4D7f7C497eAaa23130d894911), - tokenBridge: ITokenBridge(0x3ee18B2214AFF97000D974cf647E7C347E8fa585), - wormhole: IWormhole(0x98f3c9e6E3fAce36bAAd05FE09d375Ef1464288B) + url: vm.envOr( + "ETHEREUM_RPC_URL", + string("https://rpc.ankr.com/eth") + ), + relayer: IWormholeRelayer( + 0x27428DD2d3DD32A4D7f7C497eAaa23130d894911 + ), + tokenBridge: ITokenBridge( + 0x3ee18B2214AFF97000D974cf647E7C347E8fa585 + ), + wormhole: IWormhole(0x98f3c9e6E3fAce36bAAd05FE09d375Ef1464288B), + circleMessageTransmitter: IMessageTransmitter( + 0x0a992d191DEeC32aFe36203Ad87D7d289a738F81 + ), + circleTokenMessenger: ITokenMessenger( + 0xBd3fa81B58Ba92a82136038B25aDec7066af3155 + ), + USDC: IERC20(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48) }); chainInfosMainnet[4] = ChainInfo({ chainId: 4, name: "bsc", - url: vm.envOr("BSC_RPC_URL", string("https://bsc-dataseed2.defibit.io")), - relayer: IWormholeRelayer(0x27428DD2d3DD32A4D7f7C497eAaa23130d894911), - tokenBridge: ITokenBridge(0xB6F6D86a8f9879A9c87f643768d9efc38c1Da6E7), - wormhole: IWormhole(0x98f3c9e6E3fAce36bAAd05FE09d375Ef1464288B) + url: vm.envOr( + "BSC_RPC_URL", + string("https://bsc-dataseed2.defibit.io") + ), + relayer: IWormholeRelayer( + 0x27428DD2d3DD32A4D7f7C497eAaa23130d894911 + ), + tokenBridge: ITokenBridge( + 0xB6F6D86a8f9879A9c87f643768d9efc38c1Da6E7 + ), + wormhole: IWormhole(0x98f3c9e6E3fAce36bAAd05FE09d375Ef1464288B), + circleMessageTransmitter: IMessageTransmitter(address(0)), + circleTokenMessenger: ITokenMessenger(address(0)), + USDC: IERC20(address(0)) }); chainInfosMainnet[6] = ChainInfo({ chainId: 6, name: "avalanche", - url: vm.envOr("AVALANCHE_RPC_URL", string("https://rpc.ankr.com/avalanche")), - relayer: IWormholeRelayer(0x27428DD2d3DD32A4D7f7C497eAaa23130d894911), - tokenBridge: ITokenBridge(0x0e082F06FF657D94310cB8cE8B0D9a04541d8052), - wormhole: IWormhole(0x54a8e5f9c4CbA08F9943965859F6c34eAF03E26c) + url: vm.envOr( + "AVALANCHE_RPC_URL", + string("https://rpc.ankr.com/avalanche") + ), + relayer: IWormholeRelayer( + 0x27428DD2d3DD32A4D7f7C497eAaa23130d894911 + ), + tokenBridge: ITokenBridge( + 0x0e082F06FF657D94310cB8cE8B0D9a04541d8052 + ), + wormhole: IWormhole(0x54a8e5f9c4CbA08F9943965859F6c34eAF03E26c), + circleMessageTransmitter: IMessageTransmitter( + 0x8186359aF5F57FbB40c6b14A588d2A59C0C29880 + ), + circleTokenMessenger: ITokenMessenger( + 0x6B25532e1060CE10cc3B0A99e5683b91BFDe6982 + ), + USDC: IERC20(0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E) }); chainInfosMainnet[10] = ChainInfo({ chainId: 10, name: "fantom", - url: vm.envOr("FANTOM_RPC_URL", string("https://rpc.ankr.com/fantom")), - relayer: IWormholeRelayer(0x27428DD2d3DD32A4D7f7C497eAaa23130d894911), - tokenBridge: ITokenBridge(0x7C9Fc5741288cDFdD83CeB07f3ea7e22618D79D2), - wormhole: IWormhole(0x126783A6Cb203a3E35344528B26ca3a0489a1485) + url: vm.envOr( + "FANTOM_RPC_URL", + string("https://rpc.ankr.com/fantom") + ), + relayer: IWormholeRelayer( + 0x27428DD2d3DD32A4D7f7C497eAaa23130d894911 + ), + tokenBridge: ITokenBridge( + 0x7C9Fc5741288cDFdD83CeB07f3ea7e22618D79D2 + ), + wormhole: IWormhole(0x126783A6Cb203a3E35344528B26ca3a0489a1485), + circleMessageTransmitter: IMessageTransmitter(address(0)), + circleTokenMessenger: ITokenMessenger(address(0)), + USDC: IERC20(address(0)) }); chainInfosMainnet[13] = ChainInfo({ chainId: 13, name: "klaytn", - url: vm.envOr("KLAYTN_RPC_URL", string("https://klaytn-mainnet-rpc.allthatnode.com:8551")), - relayer: IWormholeRelayer(0x27428DD2d3DD32A4D7f7C497eAaa23130d894911), - tokenBridge: ITokenBridge(0x5b08ac39EAED75c0439FC750d9FE7E1F9dD0193F), - wormhole: IWormhole(0x0C21603c4f3a6387e241c0091A7EA39E43E90bb7) + url: vm.envOr( + "KLAYTN_RPC_URL", + string("https://klaytn-mainnet-rpc.allthatnode.com:8551") + ), + relayer: IWormholeRelayer( + 0x27428DD2d3DD32A4D7f7C497eAaa23130d894911 + ), + tokenBridge: ITokenBridge( + 0x5b08ac39EAED75c0439FC750d9FE7E1F9dD0193F + ), + wormhole: IWormhole(0x0C21603c4f3a6387e241c0091A7EA39E43E90bb7), + circleMessageTransmitter: IMessageTransmitter(address(0)), + circleTokenMessenger: ITokenMessenger(address(0)), + USDC: IERC20(address(0)) }); chainInfosMainnet[14] = ChainInfo({ chainId: 14, name: "celo", url: vm.envOr("CELO_RPC_URL", string("https://forno.celo.org")), - relayer: IWormholeRelayer(0x27428DD2d3DD32A4D7f7C497eAaa23130d894911), - tokenBridge: ITokenBridge(0x796Dff6D74F3E27060B71255Fe517BFb23C93eed), - wormhole: IWormhole(0xa321448d90d4e5b0A732867c18eA198e75CAC48E) + relayer: IWormholeRelayer( + 0x27428DD2d3DD32A4D7f7C497eAaa23130d894911 + ), + tokenBridge: ITokenBridge( + 0x796Dff6D74F3E27060B71255Fe517BFb23C93eed + ), + wormhole: IWormhole(0xa321448d90d4e5b0A732867c18eA198e75CAC48E), + circleMessageTransmitter: IMessageTransmitter(address(0)), + circleTokenMessenger: ITokenMessenger(address(0)), + USDC: IERC20(address(0)) }); chainInfosMainnet[12] = ChainInfo({ chainId: 12, name: "acala", - url: vm.envOr("ACALA_RPC_URL", string("https://eth-rpc-acala.aca-api.network")), - relayer: IWormholeRelayer(0x27428DD2d3DD32A4D7f7C497eAaa23130d894911), - tokenBridge: ITokenBridge(0xae9d7fe007b3327AA64A32824Aaac52C42a6E624), - wormhole: IWormhole(0xa321448d90d4e5b0A732867c18eA198e75CAC48E) + url: vm.envOr( + "ACALA_RPC_URL", + string("https://eth-rpc-acala.aca-api.network") + ), + relayer: IWormholeRelayer( + 0x27428DD2d3DD32A4D7f7C497eAaa23130d894911 + ), + tokenBridge: ITokenBridge( + 0xae9d7fe007b3327AA64A32824Aaac52C42a6E624 + ), + wormhole: IWormhole(0xa321448d90d4e5b0A732867c18eA198e75CAC48E), + circleMessageTransmitter: IMessageTransmitter(address(0)), + circleTokenMessenger: ITokenMessenger(address(0)), + USDC: IERC20(address(0)) }); chainInfosMainnet[11] = ChainInfo({ chainId: 11, name: "karura", - url: vm.envOr("KARURA_RPC_URL", string("https://eth-rpc-karura.aca-api.network")), - relayer: IWormholeRelayer(0x27428DD2d3DD32A4D7f7C497eAaa23130d894911), - tokenBridge: ITokenBridge(0xae9d7fe007b3327AA64A32824Aaac52C42a6E624), - wormhole: IWormhole(0xa321448d90d4e5b0A732867c18eA198e75CAC48E) + url: vm.envOr( + "KARURA_RPC_URL", + string("https://eth-rpc-karura.aca-api.network") + ), + relayer: IWormholeRelayer( + 0x27428DD2d3DD32A4D7f7C497eAaa23130d894911 + ), + tokenBridge: ITokenBridge( + 0xae9d7fe007b3327AA64A32824Aaac52C42a6E624 + ), + wormhole: IWormhole(0xa321448d90d4e5b0A732867c18eA198e75CAC48E), + circleMessageTransmitter: IMessageTransmitter(address(0)), + circleTokenMessenger: ITokenMessenger(address(0)), + USDC: IERC20(address(0)) }); chainInfosMainnet[16] = ChainInfo({ chainId: 16, name: "moombeam", - url: vm.envOr("MOOMBEAM_RPC_URL", string("https://rpc.ankr.com/moonbeam")), - relayer: IWormholeRelayer(0x27428DD2d3DD32A4D7f7C497eAaa23130d894911), - tokenBridge: ITokenBridge(0xB1731c586ca89a23809861c6103F0b96B3F57D92), - wormhole: IWormhole(0xC8e2b0cD52Cf01b0Ce87d389Daa3d414d4cE29f3) + url: vm.envOr( + "MOOMBEAM_RPC_URL", + string("https://rpc.ankr.com/moonbeam") + ), + relayer: IWormholeRelayer( + 0x27428DD2d3DD32A4D7f7C497eAaa23130d894911 + ), + tokenBridge: ITokenBridge( + 0xB1731c586ca89a23809861c6103F0b96B3F57D92 + ), + wormhole: IWormhole(0xC8e2b0cD52Cf01b0Ce87d389Daa3d414d4cE29f3), + circleMessageTransmitter: IMessageTransmitter(address(0)), + circleTokenMessenger: ITokenMessenger(address(0)), + USDC: IERC20(address(0)) }); chainInfosMainnet[23] = ChainInfo({ chainId: 23, name: "arbitrum", - url: vm.envOr("ARBITRUM_RPC_URL", string("https://rpc.ankr.com/arbitrum")), - relayer: IWormholeRelayer(0x27428DD2d3DD32A4D7f7C497eAaa23130d894911), - tokenBridge: ITokenBridge(0x0b2402144Bb366A632D14B83F244D2e0e21bD39c), - wormhole: IWormhole(0xa5f208e072434bC67592E4C49C1B991BA79BCA46) + url: vm.envOr( + "ARBITRUM_RPC_URL", + string("https://rpc.ankr.com/arbitrum") + ), + relayer: IWormholeRelayer( + 0x27428DD2d3DD32A4D7f7C497eAaa23130d894911 + ), + tokenBridge: ITokenBridge( + 0x0b2402144Bb366A632D14B83F244D2e0e21bD39c + ), + wormhole: IWormhole(0xa5f208e072434bC67592E4C49C1B991BA79BCA46), + circleMessageTransmitter: IMessageTransmitter( + 0xC30362313FBBA5cf9163F0bb16a0e01f01A896ca + ), + circleTokenMessenger: ITokenMessenger( + 0x19330d10D9Cc8751218eaf51E8885D058642E08A + ), + USDC: IERC20(0xaf88d065e77c8cC2239327C5EDb3A432268e5831) }); chainInfosMainnet[24] = ChainInfo({ chainId: 24, name: "optimism", - url: vm.envOr("OPTIMISM_RPC_URL", string("https://rpc.ankr.com/optimism")), - relayer: IWormholeRelayer(0x27428DD2d3DD32A4D7f7C497eAaa23130d894911), - tokenBridge: ITokenBridge(0x1D68124e65faFC907325e3EDbF8c4d84499DAa8b), - wormhole: IWormhole(0xEe91C335eab126dF5fDB3797EA9d6aD93aeC9722) + url: vm.envOr( + "OPTIMISM_RPC_URL", + string("https://rpc.ankr.com/optimism") + ), + relayer: IWormholeRelayer( + 0x27428DD2d3DD32A4D7f7C497eAaa23130d894911 + ), + tokenBridge: ITokenBridge( + 0x1D68124e65faFC907325e3EDbF8c4d84499DAa8b + ), + wormhole: IWormhole(0xEe91C335eab126dF5fDB3797EA9d6aD93aeC9722), + circleMessageTransmitter: IMessageTransmitter( + 0x4D41f22c5a0e5c74090899E5a8Fb597a8842b3e8 + ), + circleTokenMessenger: ITokenMessenger( + 0x2B4069517957735bE00ceE0fadAE88a26365528f + ), + USDC: IERC20(0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85) + }); + chainInfosMainnet[30] = ChainInfo({ + chainId: 30, + name: "base", + url: vm.envOr("BASE_RPC_URL", string("https://mainnet.base.org")), + relayer: IWormholeRelayer( + 0x706F82e9bb5b0813501714Ab5974216704980e31 + ), + tokenBridge: ITokenBridge( + 0x8d2de8d2f73F1F4cAB472AC9A881C9b123C79627 + ), + wormhole: IWormhole(0xbebdb6C8ddC678FfA9f8748f85C815C556Dd8ac6), + circleMessageTransmitter: IMessageTransmitter( + address(0xAD09780d193884d503182aD4588450C416D6F9D4) + ), + circleTokenMessenger: ITokenMessenger( + address(0x1682Ae6375C4E4A97e4B583BC394c861A46D8962) + ), + USDC: IERC20(address(0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913)) }); } @@ -336,7 +684,7 @@ abstract contract WormholeRelayerBasicTest is WormholeRelayerTest { */ function setUpOther(ActiveFork memory fork) public virtual {} - /* + /* * aliases for activeForks */ @@ -360,8 +708,11 @@ abstract contract WormholeRelayerBasicTest is WormholeRelayerTest { WormholeSimulator public guardianSource; WormholeSimulator public guardianTarget; - /* - * end activeForks aliases + CircleMessageTransmitterSimulator public circleAttesterSource; + CircleMessageTransmitterSimulator public circleAttesterTarget; + + /* + * end activeForks aliases */ constructor() WormholeRelayerTest() { @@ -375,6 +726,8 @@ abstract contract WormholeRelayerBasicTest is WormholeRelayerTest { // aliases can't be set until after setUp guardianSource = activeForks[activeForksList[0]].guardian; guardianTarget = activeForks[activeForksList[1]].guardian; + circleAttesterSource = activeForks[activeForksList[0]].circleAttester; + circleAttesterTarget = activeForks[activeForksList[1]].circleAttester; sourceFork = activeForks[activeForksList[0]].fork; targetFork = activeForks[activeForksList[1]].fork; } @@ -405,26 +758,35 @@ abstract contract WormholeRelayerBasicTest is WormholeRelayerTest { wormholeTarget = targetChainInfo.wormhole; } - function setTestnetForkChains(uint16 _sourceChain, uint16 _targetChain) public { + function setTestnetForkChains( + uint16 _sourceChain, + uint16 _targetChain + ) public { ChainInfo[] memory forks = new ChainInfo[](2); forks[0] = chainInfosTestnet[_sourceChain]; forks[1] = chainInfosTestnet[_targetChain]; setActiveForks(forks); } - function setMainnetForkChains(uint16 _sourceChain, uint16 _targetChain) public { + function setMainnetForkChains( + uint16 _sourceChain, + uint16 _targetChain + ) public { ChainInfo[] memory forks = new ChainInfo[](2); forks[0] = chainInfosMainnet[_sourceChain]; forks[1] = chainInfosMainnet[_targetChain]; setActiveForks(forks); } - function setForkChains(bool testnet, uint16 _sourceChain, uint16 _targetChain) public { - if (testnet) { - setTestnetForkChains(_sourceChain, _targetChain); - return; - } - setMainnetForkChains(_sourceChain, _targetChain); + function setForkChains( + bool testnet, + uint16 _sourceChain, + uint16 _targetChain + ) public { + if (testnet) { + setTestnetForkChains(_sourceChain, _targetChain); + return; + } + setMainnetForkChains(_sourceChain, _targetChain); } - } diff --git a/src/testing/helpers/BytesParsing.sol b/src/testing/helpers/BytesParsing.sol index 56968a1..945da6e 100644 --- a/src/testing/helpers/BytesParsing.sol +++ b/src/testing/helpers/BytesParsing.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: UNLICENSED + pragma solidity ^0.8.13; library BytesParsing { diff --git a/src/testing/helpers/CircleCCTPSimulator.sol b/src/testing/helpers/CircleCCTPSimulator.sol new file mode 100644 index 0000000..df5669c --- /dev/null +++ b/src/testing/helpers/CircleCCTPSimulator.sol @@ -0,0 +1,157 @@ +// SPDX-License-Identifier: Apache 2 +pragma solidity ^0.8.13; + +import {IMessageTransmitter} from "../../../src/interfaces/CCTPInterfaces/IMessageTransmitter.sol"; +import "./BytesLib.sol"; + +import "forge-std/Vm.sol"; +import "forge-std/console.sol"; + +import {CCTPMessageLib} from "../../CCTPBase.sol"; + +interface MessageTransmitterViewAttesterManager { + function attesterManager() external view returns (address); + + function enableAttester(address newAttester) external; + + function setSignatureThreshold(uint256 newSignatureThreshold) external; +} + +/** + * @title A Circle MessageTransmitter Simulator + * @notice This contract simulates attesting Circle messages emitted in a forge test. + * It overrides the Circle 'attester' set to allow for signing messages with a single + * private key on any EVM where the MessageTransmitter contract is deployed. + * @dev This contract is meant to be used when testing against a testnet or mainnet fork. + */ +contract CircleMessageTransmitterSimulator { + using BytesLib for bytes; + + bool public valid; + + // Taken from forge-std/Script.sol + address private constant VM_ADDRESS = + address(bytes20(uint160(uint256(keccak256("hevm cheat code"))))); + Vm public constant vm = Vm(VM_ADDRESS); + + // Allow access to MessageTransmitter + IMessageTransmitter public messageTransmitter; + + // Save the private key to sign messages with + uint256 private attesterPrivateKey; + + /** + * @param messageTransmitter_ address of the MessageTransmitter for the chain being forked + * @param attesterPrivateKey_ private key of the (single) attester - to override the MessageTransmitter contract with + */ + constructor(address messageTransmitter_, uint256 attesterPrivateKey_) { + messageTransmitter = IMessageTransmitter(messageTransmitter_); + attesterPrivateKey = attesterPrivateKey_; + valid = messageTransmitter_ != address(0); + if(valid) overrideAttester(vm.addr(attesterPrivateKey)); + } + + function overrideAttester(address attesterPublicKey) internal { + { + MessageTransmitterViewAttesterManager attesterManagerInterface = MessageTransmitterViewAttesterManager( + address(messageTransmitter) + ); + address attesterManager = attesterManagerInterface + .attesterManager(); + vm.prank(attesterManager); + attesterManagerInterface.enableAttester(attesterPublicKey); + + vm.prank(attesterManager); + attesterManagerInterface.setSignatureThreshold(1); + } + } + + + function parseMessageFromMessageTransmitterLog( + Vm.Log memory log + ) internal pure returns (bytes memory message) { + uint256 index = 32; + + // length of payload + uint256 payloadLen = log.data.toUint256(index); + index += 32; + + message = log.data.slice(index, payloadLen); + index += payloadLen; + + // trailing bytes (due to 32 byte slot overlap) + require(log.data.length - index < 32, "Too many extra bytes"); + index += log.data.length - index; + require( + index == log.data.length, + "failed to parse MessageTransmitter message" + ); + } + + /** + * @notice Finds published messageTransmitter events in forge logs + * @param logs The forge Vm.log captured when recording events during test execution + */ + function fetchMessageTransmitterLogsFromLogs( + Vm.Log[] memory logs + ) public pure returns (Vm.Log[] memory) { + uint256 count = 0; + for (uint256 i = 0; i < logs.length; i++) { + if ( + logs[i].topics[0] == + keccak256("MessageSent(bytes)") + ) { + count += 1; + } + } + + // create log array to save published messages + Vm.Log[] memory published = new Vm.Log[](count); + + uint256 publishedIndex = 0; + for (uint256 i = 0; i < logs.length; i++) { + if ( + logs[i].topics[0] == + keccak256("MessageSent(bytes)") + ) { + published[publishedIndex] = logs[i]; + publishedIndex += 1; + } + } + + return published; + } + + /** + * @notice attests a simulated MessageTransmitter message using the emitted log from MessageTransmitter + * @param log The forge Vm.log captured when recording events during test execution + * @return attestation attested message + */ + + function fetchSignedMessageFromLog( + Vm.Log memory log + ) public view returns (CCTPMessageLib.CCTPMessage memory) { + // Parse messageTransmitter message from ethereum logs + bytes memory message = parseMessageFromMessageTransmitterLog(log); + + return CCTPMessageLib.CCTPMessage(message, signMessage(message)); + } + + /** + * @notice Signs a simulated messageTransmitter message + * @param message The messageTransmitter message + * @return signedMessage signed messageTransmitter message + */ + + function signMessage( + bytes memory message + ) public view returns (bytes memory signedMessage) { + + bytes32 messageHash = keccak256(message); + + // Sign the hash with the attester private key + (uint8 v, bytes32 r, bytes32 s) = vm.sign(attesterPrivateKey, messageHash); + return abi.encodePacked(r, s, v); + } + +} diff --git a/src/testing/helpers/DeliveryInstructionDecoder.sol b/src/testing/helpers/DeliveryInstructionDecoder.sol index 7ca1492..8158317 100644 --- a/src/testing/helpers/DeliveryInstructionDecoder.sol +++ b/src/testing/helpers/DeliveryInstructionDecoder.sol @@ -1,10 +1,9 @@ -// SPDX-License-Identifier: UNLICENSED + pragma solidity ^0.8.13; import "../../../src/interfaces/IWormholeRelayer.sol"; import "./BytesParsing.sol"; - -import "forge-std/console.sol"; +import {CCTPMessageLib} from "../../CCTPBase.sol"; uint8 constant VERSION_VAAKEY = 1; uint8 constant VERSION_DELIVERY_OVERRIDE = 1; @@ -25,7 +24,7 @@ struct DeliveryInstruction { bytes32 refundDeliveryProvider; bytes32 sourceDeliveryProvider; bytes32 senderAddress; - VaaKey[] vaaKeys; + MessageKey[] messageKeys; } struct RedeliveryInstruction { @@ -43,7 +42,9 @@ struct DeliveryOverride { bytes32 redeliveryHash; } -function decodeDeliveryInstruction(bytes memory encoded) pure returns (DeliveryInstruction memory strct) { +function decodeDeliveryInstruction( + bytes memory encoded +) pure returns (DeliveryInstruction memory strct) { uint256 offset = checkUint8(encoded, 0, PAYLOAD_ID_DELIVERY_INSTRUCTION); uint256 requestedReceiverValue; @@ -60,7 +61,7 @@ function decodeDeliveryInstruction(bytes memory encoded) pure returns (DeliveryI (strct.refundDeliveryProvider, offset) = encoded.asBytes32Unchecked(offset); (strct.sourceDeliveryProvider, offset) = encoded.asBytes32Unchecked(offset); (strct.senderAddress, offset) = encoded.asBytes32Unchecked(offset); - (strct.vaaKeys, offset) = decodeVaaKeyArray(encoded, offset); + (strct.messageKeys, offset) = decodeMessageKeyArray(encoded, offset); strct.requestedReceiverValue = requestedReceiverValue; strct.extraReceiverValue = extraReceiverValue; @@ -68,16 +69,20 @@ function decodeDeliveryInstruction(bytes memory encoded) pure returns (DeliveryI checkLength(encoded, offset); } -function decodeRedeliveryInstruction(bytes memory encoded) pure returns (RedeliveryInstruction memory strct) { +function decodeRedeliveryInstruction( + bytes memory encoded +) pure returns (RedeliveryInstruction memory strct) { uint256 offset = checkUint8(encoded, 0, PAYLOAD_ID_REDELIVERY_INSTRUCTION); uint256 newRequestedReceiverValue; - + offset = checkUint8(encoded, offset, VAA_KEY_TYPE); (strct.deliveryVaaKey, offset) = decodeVaaKey(encoded, offset); (strct.targetChain, offset) = encoded.asUint16Unchecked(offset); (newRequestedReceiverValue, offset) = encoded.asUint256Unchecked(offset); (strct.newEncodedExecutionInfo, offset) = decodeBytes(encoded, offset); - (strct.newSourceDeliveryProvider, offset) = encoded.asBytes32Unchecked(offset); + (strct.newSourceDeliveryProvider, offset) = encoded.asBytes32Unchecked( + offset + ); (strct.newSenderAddress, offset) = encoded.asBytes32Unchecked(offset); strct.newRequestedReceiverValue = newRequestedReceiverValue; @@ -85,56 +90,132 @@ function decodeRedeliveryInstruction(bytes memory encoded) pure returns (Redeliv checkLength(encoded, offset); } -function encodeVaaKeyArray(VaaKey[] memory vaaKeys) pure returns (bytes memory encoded) { - assert(vaaKeys.length < type(uint8).max); - encoded = abi.encodePacked(uint8(vaaKeys.length)); - for (uint256 i = 0; i < vaaKeys.length;) { - encoded = abi.encodePacked(encoded, encodeVaaKey(vaaKeys[i])); +function vaaKeyArrayToMessageKeyArray( + VaaKey[] memory vaaKeys +) pure returns (MessageKey[] memory msgKeys) { + msgKeys = new MessageKey[](vaaKeys.length); + uint256 len = vaaKeys.length; + for (uint256 i = 0; i < len; ) { + msgKeys[i] = MessageKey(VAA_KEY_TYPE, encodeVaaKey(vaaKeys[i])); unchecked { ++i; } } } -function decodeVaaKeyArray(bytes memory encoded, uint256 startOffset) - pure - returns (VaaKey[] memory vaaKeys, uint256 offset) -{ - uint8 vaaKeysLength; - (vaaKeysLength, offset) = encoded.asUint8Unchecked(startOffset); - vaaKeys = new VaaKey[](vaaKeysLength); - for (uint256 i = 0; i < vaaKeys.length;) { - (vaaKeys[i], offset) = decodeVaaKey(encoded, offset); - unchecked { - ++i; - } +function encodeMessageKey( + MessageKey memory msgKey +) pure returns (bytes memory encoded) { + if (msgKey.keyType == VAA_KEY_TYPE) { + // known length + encoded = abi.encodePacked(msgKey.keyType, msgKey.encodedKey); + } else { + encoded = abi.encodePacked( + msgKey.keyType, + encodeBytes(msgKey.encodedKey) + ); + } +} + +uint256 constant VAA_KEY_TYPE_LENGTH = 2 + 32 + 8; + +function decodeMessageKey( + bytes memory encoded, + uint256 startOffset +) pure returns (MessageKey memory msgKey, uint256 offset) { + (msgKey.keyType, offset) = encoded.asUint8Unchecked(startOffset); + if (msgKey.keyType == VAA_KEY_TYPE) { + (msgKey.encodedKey, offset) = encoded.sliceUnchecked( + offset, + VAA_KEY_TYPE_LENGTH + ); + } else { + (msgKey.encodedKey, offset) = decodeBytes(encoded, offset); } } -function encodeVaaKey(VaaKey memory vaaKey) pure returns (bytes memory encoded) { - encoded = abi.encodePacked(encoded, VERSION_VAAKEY, vaaKey.chainId, vaaKey.emitterAddress, vaaKey.sequence); +function encodeVaaKey( + VaaKey memory vaaKey +) pure returns (bytes memory encoded) { + encoded = abi.encodePacked( + vaaKey.chainId, + vaaKey.emitterAddress, + vaaKey.sequence + ); } -function decodeVaaKey(bytes memory encoded, uint256 startOffset) pure returns (VaaKey memory vaaKey, uint256 offset) { - offset = checkUint8(encoded, startOffset, VERSION_VAAKEY); +function decodeVaaKey( + bytes memory encoded, + uint256 startOffset +) pure returns (VaaKey memory vaaKey, uint256 offset) { + offset = startOffset; (vaaKey.chainId, offset) = encoded.asUint16Unchecked(offset); (vaaKey.emitterAddress, offset) = encoded.asBytes32Unchecked(offset); (vaaKey.sequence, offset) = encoded.asUint64Unchecked(offset); } +function encodeMessageKeyArray( + MessageKey[] memory msgKeys +) pure returns (bytes memory encoded) { + uint256 len = msgKeys.length; + if (len > type(uint8).max) { + revert TooManyMessageKeys(len); + } + encoded = abi.encodePacked(uint8(msgKeys.length)); + for (uint256 i = 0; i < len; ) { + encoded = abi.encodePacked(encoded, encodeMessageKey(msgKeys[i])); + unchecked { + ++i; + } + } +} + +function decodeMessageKeyArray( + bytes memory encoded, + uint256 startOffset +) pure returns (MessageKey[] memory msgKeys, uint256 offset) { + uint8 msgKeysLength; + (msgKeysLength, offset) = encoded.asUint8Unchecked(startOffset); + msgKeys = new MessageKey[](msgKeysLength); + for (uint256 i = 0; i < msgKeysLength; ) { + (msgKeys[i], offset) = decodeMessageKey(encoded, offset); + unchecked { + ++i; + } + } +} + +function decodeCCTPKey( + bytes memory encoded, + uint256 startOffset +) pure returns (CCTPMessageLib.CCTPKey memory cctpKey, uint256 offset) { + offset = startOffset; + (cctpKey.domain, offset) = encoded.asUint32Unchecked(offset); + (cctpKey.nonce, offset) = encoded.asUint64Unchecked(offset); +} + +// ------------------------------------------ -------------------------------------------- + function encodeBytes(bytes memory payload) pure returns (bytes memory encoded) { //casting payload.length to uint32 is safe because you'll be hard-pressed to allocate 4 GB of // EVM memory in a single transaction encoded = abi.encodePacked(uint32(payload.length), payload); } -function decodeBytes(bytes memory encoded, uint256 startOffset) pure returns (bytes memory payload, uint256 offset) { +function decodeBytes( + bytes memory encoded, + uint256 startOffset +) pure returns (bytes memory payload, uint256 offset) { uint32 payloadLength; (payloadLength, offset) = encoded.asUint32Unchecked(startOffset); (payload, offset) = encoded.sliceUnchecked(offset, payloadLength); } -function checkUint8(bytes memory encoded, uint256 startOffset, uint8 expectedPayloadId) pure returns (uint256 offset) { +function checkUint8( + bytes memory encoded, + uint256 startOffset, + uint8 expectedPayloadId +) pure returns (uint256 offset) { uint8 parsedPayloadId; (parsedPayloadId, offset) = encoded.asUint8Unchecked(startOffset); if (parsedPayloadId != expectedPayloadId) { @@ -148,8 +229,29 @@ function checkLength(bytes memory encoded, uint256 expected) pure { } } -function encode(DeliveryOverride memory strct) pure returns (bytes memory encoded) { +function encode( + DeliveryOverride memory strct +) pure returns (bytes memory encoded) { encoded = abi.encodePacked( - VERSION_DELIVERY_OVERRIDE, strct.newReceiverValue, encodeBytes(strct.newExecutionInfo), strct.redeliveryHash + VERSION_DELIVERY_OVERRIDE, + strct.newReceiverValue, + encodeBytes(strct.newExecutionInfo), + strct.redeliveryHash ); } + +function decodeDeliveryOverride( + bytes memory encoded +) pure returns (DeliveryOverride memory strct) { + uint256 offset = checkUint8(encoded, 0, VERSION_DELIVERY_OVERRIDE); + + uint256 receiverValue; + + (receiverValue, offset) = encoded.asUint256Unchecked(offset); + (strct.newExecutionInfo, offset) = decodeBytes(encoded, offset); + (strct.redeliveryHash, offset) = encoded.asBytes32Unchecked(offset); + + strct.newReceiverValue = receiverValue; + + checkLength(encoded, offset); +} diff --git a/src/testing/helpers/MockOffchainRelayer.sol b/src/testing/helpers/MockOffchainRelayer.sol index 839bfc8..6d6777e 100644 --- a/src/testing/helpers/MockOffchainRelayer.sol +++ b/src/testing/helpers/MockOffchainRelayer.sol @@ -3,22 +3,31 @@ pragma solidity ^0.8.0; import {WormholeSimulator} from "./WormholeSimulator.sol"; +import {CircleMessageTransmitterSimulator} from "./CircleCCTPSimulator.sol"; import {toWormholeFormat, fromWormholeFormat} from "../../Utils.sol"; import "../../interfaces/IWormholeRelayer.sol"; import "../../interfaces/IWormhole.sol"; import "forge-std/Vm.sol"; import "./DeliveryInstructionDecoder.sol"; +import {CCTPMessageLib} from "../../CCTPBase.sol"; import "./ExecutionParameters.sol"; import "./BytesParsing.sol"; +import "forge-std/console.sol"; using BytesParsing for bytes; contract MockOffchainRelayer { + uint16 chainIdOfWormholeAndGuardianUtilities; IWormhole relayerWormhole; WormholeSimulator relayerWormholeSimulator; + CircleMessageTransmitterSimulator relayerCircleSimulator; + - Vm public vm; + // Taken from forge-std/Script.sol + address private constant VM_ADDRESS = + address(bytes20(uint160(uint256(keccak256("hevm cheat code"))))); + Vm public constant vm = Vm(VM_ADDRESS); mapping(uint16 => address) wormholeRelayerContracts; @@ -26,26 +35,42 @@ contract MockOffchainRelayer { mapping(uint256 => uint16) chainIdFromFork; - mapping(bytes32 => bytes[]) pastEncodedVMs; + mapping(bytes32 => bytes[]) pastEncodedSignedVaas; mapping(bytes32 => bytes) pastEncodedDeliveryVAA; - constructor(address _wormhole, address _wormholeSimulator, Vm vm_) { + constructor(address _wormhole, address _wormholeSimulator, address _circleSimulator) { relayerWormhole = IWormhole(_wormhole); relayerWormholeSimulator = WormholeSimulator(_wormholeSimulator); - vm = vm_; + relayerCircleSimulator = CircleMessageTransmitterSimulator(_circleSimulator); chainIdOfWormholeAndGuardianUtilities = relayerWormhole.chainId(); } - function getPastEncodedVMs(uint16 chainId, uint64 deliveryVAASequence) public view returns (bytes[] memory) { - return pastEncodedVMs[keccak256(abi.encodePacked(chainId, deliveryVAASequence))]; + function getPastEncodedSignedVaas( + uint16 chainId, + uint64 deliveryVAASequence + ) public view returns (bytes[] memory) { + return + pastEncodedSignedVaas[ + keccak256(abi.encodePacked(chainId, deliveryVAASequence)) + ]; } - function getPastDeliveryVAA(uint16 chainId, uint64 deliveryVAASequence) public view returns (bytes memory) { - return pastEncodedDeliveryVAA[keccak256(abi.encodePacked(chainId, deliveryVAASequence))]; + function getPastDeliveryVAA( + uint16 chainId, + uint64 deliveryVAASequence + ) public view returns (bytes memory) { + return + pastEncodedDeliveryVAA[ + keccak256(abi.encodePacked(chainId, deliveryVAASequence)) + ]; } - function registerChain(uint16 chainId, address wormholeRelayerContractAddress, uint256 fork) public { + function registerChain( + uint16 chainId, + address wormholeRelayerContractAddress, + uint256 fork + ) public { wormholeRelayerContracts[chainId] = wormholeRelayerContractAddress; forks[chainId] = fork; chainIdFromFork[fork] = chainId; @@ -63,33 +88,78 @@ contract MockOffchainRelayer { relay(logs, bytes(""), false); } - function vaaKeyMatchesVAA(VaaKey memory vaaKey, bytes memory signedVaa) internal view returns (bool) { + function vaaKeyMatchesVAA( + VaaKey memory vaaKey, + bytes memory signedVaa + ) internal view returns (bool) { IWormhole.VM memory parsedVaa = relayerWormhole.parseVM(signedVaa); - return (vaaKey.chainId == parsedVaa.emitterChainId) && (vaaKey.emitterAddress == parsedVaa.emitterAddress) - && (vaaKey.sequence == parsedVaa.sequence); + return + (vaaKey.chainId == parsedVaa.emitterChainId) && + (vaaKey.emitterAddress == parsedVaa.emitterAddress) && + (vaaKey.sequence == parsedVaa.sequence); + } + + function cctpKeyMatchesCCTPMessage( + CCTPMessageLib.CCTPKey memory cctpKey, + CCTPMessageLib.CCTPMessage memory cctpMessage + ) internal pure returns (bool) { + (uint64 nonce,) = cctpMessage.message.asUint64(12); + (uint32 domain,) = cctpMessage.message.asUint32(4); + return + nonce == cctpKey.nonce && domain == cctpKey.domain; } - function relay(Vm.Log[] memory logs, bytes memory deliveryOverrides, bool debugLogging) public { + function relay( + Vm.Log[] memory logs, + bytes memory deliveryOverrides, + bool debugLogging + ) public { uint16 chainId = chainIdFromFork[vm.activeFork()]; - require(wormholeRelayerContracts[chainId] != address(0), "Chain not registered with MockOffchainRelayer"); + require( + wormholeRelayerContracts[chainId] != address(0), + "Chain not registered with MockOffchainRelayer" + ); vm.selectFork(forks[chainIdOfWormholeAndGuardianUtilities]); - Vm.Log[] memory entries = relayerWormholeSimulator.fetchWormholeMessageFromLog(logs); + Vm.Log[] memory entries = relayerWormholeSimulator + .fetchWormholeMessageFromLog(logs); if (debugLogging) { console.log("Found %s wormhole messages in logs", entries.length); } - bytes[] memory encodedVMs = new bytes[](entries.length); - for (uint256 i = 0; i < encodedVMs.length; i++) { - encodedVMs[i] = relayerWormholeSimulator.fetchSignedMessageFromLogs(entries[i], chainId); + bytes[] memory encodedSignedVaas = new bytes[](entries.length); + for (uint256 i = 0; i < encodedSignedVaas.length; i++) { + encodedSignedVaas[i] = relayerWormholeSimulator.fetchSignedMessageFromLogs( + entries[i], + chainId + ); + } + + bool checkCCTP = relayerCircleSimulator.valid(); + Vm.Log[] memory cctpEntries = new Vm.Log[](0); + if(checkCCTP) { + cctpEntries = relayerCircleSimulator + .fetchMessageTransmitterLogsFromLogs(logs); + } + + if (debugLogging) { + console.log("Found %s circle messages in logs", cctpEntries.length); + } + + CCTPMessageLib.CCTPMessage[] memory circleSignedMessages = new CCTPMessageLib.CCTPMessage[](cctpEntries.length); + for (uint256 i = 0; i < cctpEntries.length; i++) { + circleSignedMessages[i] = relayerCircleSimulator.fetchSignedMessageFromLog( + cctpEntries[i] + ); } - IWormhole.VM[] memory parsed = new IWormhole.VM[](encodedVMs.length); - for (uint16 i = 0; i < encodedVMs.length; i++) { - parsed[i] = relayerWormhole.parseVM(encodedVMs[i]); + + IWormhole.VM[] memory parsed = new IWormhole.VM[](encodedSignedVaas.length); + for (uint16 i = 0; i < encodedSignedVaas.length; i++) { + parsed[i] = relayerWormhole.parseVM(encodedSignedVaas[i]); } - for (uint16 i = 0; i < encodedVMs.length; i++) { + for (uint16 i = 0; i < encodedSignedVaas.length; i++) { if (debugLogging) { console.log( "Found VAA from chain %s emitted from %s", @@ -99,14 +169,21 @@ contract MockOffchainRelayer { } if ( - parsed[i].emitterAddress == toWormholeFormat(wormholeRelayerContracts[chainId]) - && (parsed[i].emitterChainId == chainId) + parsed[i].emitterAddress == + toWormholeFormat(wormholeRelayerContracts[chainId]) && + (parsed[i].emitterChainId == chainId) ) { if (debugLogging) { console.log("Relaying VAA to chain %s", chainId); } vm.selectFork(forks[chainIdOfWormholeAndGuardianUtilities]); - genericRelay(encodedVMs[i], encodedVMs, parsed[i], deliveryOverrides); + genericRelay( + encodedSignedVaas[i], + encodedSignedVaas, + circleSignedMessages, + parsed[i], + deliveryOverrides + ); } } @@ -120,39 +197,64 @@ contract MockOffchainRelayer { function setInfo( uint16 chainId, uint64 deliveryVAASequence, - bytes[] memory encodedVMs, + bytes[] memory encodedSignedVaas, bytes memory encodedDeliveryVAA ) internal { bytes32 key = keccak256(abi.encodePacked(chainId, deliveryVAASequence)); - pastEncodedVMs[key] = encodedVMs; + pastEncodedSignedVaas[key] = encodedSignedVaas; pastEncodedDeliveryVAA[key] = encodedDeliveryVAA; } function genericRelay( bytes memory encodedDeliveryVAA, - bytes[] memory encodedVMs, + bytes[] memory encodedSignedVaas, + CCTPMessageLib.CCTPMessage[] memory cctpMessages, IWormhole.VM memory parsedDeliveryVAA, bytes memory deliveryOverrides ) internal { - (uint8 payloadId,) = parsedDeliveryVAA.payload.asUint8Unchecked(0); + (uint8 payloadId, ) = parsedDeliveryVAA.payload.asUint8Unchecked(0); if (payloadId == 1) { - DeliveryInstruction memory instruction = decodeDeliveryInstruction(parsedDeliveryVAA.payload); + DeliveryInstruction memory instruction = decodeDeliveryInstruction( + parsedDeliveryVAA.payload + ); - bytes[] memory encodedVMsToBeDelivered = new bytes[](instruction.vaaKeys.length); + bytes[] memory encodedSignedVaasToBeDelivered = new bytes[]( + instruction.messageKeys.length + ); - for (uint8 i = 0; i < instruction.vaaKeys.length; i++) { - for (uint8 j = 0; j < encodedVMs.length; j++) { - if (vaaKeyMatchesVAA(instruction.vaaKeys[i], encodedVMs[j])) { - encodedVMsToBeDelivered[i] = encodedVMs[j]; - break; + for (uint8 i = 0; i < instruction.messageKeys.length; i++) { + if (instruction.messageKeys[i].keyType == 1) { + // VaaKey + (VaaKey memory vaaKey, ) = decodeVaaKey( + instruction.messageKeys[i].encodedKey, + 0 + ); + for (uint8 j = 0; j < encodedSignedVaas.length; j++) { + if (vaaKeyMatchesVAA(vaaKey, encodedSignedVaas[j])) { + encodedSignedVaasToBeDelivered[i] = encodedSignedVaas[j]; + break; + } + } + } else if (instruction.messageKeys[i].keyType == 2) { + // CCTP Key + (CCTPMessageLib.CCTPKey memory key,) = decodeCCTPKey(instruction.messageKeys[i].encodedKey, 0); + for (uint8 j = 0; j < cctpMessages.length; j++) { + if (cctpKeyMatchesCCTPMessage(key, cctpMessages[j])) { + encodedSignedVaasToBeDelivered[i] = abi.encode(cctpMessages[j].message, cctpMessages[j].signature); + break; + } } } } - EvmExecutionInfoV1 memory executionInfo = decodeEvmExecutionInfoV1(instruction.encodedExecutionInfo); + EvmExecutionInfoV1 memory executionInfo = decodeEvmExecutionInfoV1( + instruction.encodedExecutionInfo + ); - uint256 budget = executionInfo.gasLimit * executionInfo.targetChainRefundPerGasUnused - + instruction.requestedReceiverValue + instruction.extraReceiverValue; + uint256 budget = executionInfo.gasLimit * + executionInfo.targetChainRefundPerGasUnused + + instruction.requestedReceiverValue + + instruction.extraReceiverValue; uint16 targetChain = instruction.targetChain; @@ -161,18 +263,25 @@ contract MockOffchainRelayer { vm.deal(address(this), budget); vm.recordLogs(); - IWormholeRelayerDelivery(wormholeRelayerContracts[targetChain]).deliver{value: budget}( - encodedVMsToBeDelivered, encodedDeliveryVAA, payable(address(this)), deliveryOverrides + IWormholeRelayerDelivery(wormholeRelayerContracts[targetChain]) + .deliver{value: budget}( + encodedSignedVaasToBeDelivered, + encodedDeliveryVAA, + payable(address(this)), + deliveryOverrides ); setInfo( parsedDeliveryVAA.emitterChainId, parsedDeliveryVAA.sequence, - encodedVMsToBeDelivered, + encodedSignedVaasToBeDelivered, encodedDeliveryVAA ); } else if (payloadId == 2) { - RedeliveryInstruction memory instruction = decodeRedeliveryInstruction(parsedDeliveryVAA.payload); + RedeliveryInstruction + memory instruction = decodeRedeliveryInstruction( + parsedDeliveryVAA.payload + ); DeliveryOverride memory deliveryOverride = DeliveryOverride({ newExecutionInfo: instruction.newEncodedExecutionInfo, @@ -180,21 +289,33 @@ contract MockOffchainRelayer { redeliveryHash: parsedDeliveryVAA.hash }); - EvmExecutionInfoV1 memory executionInfo = decodeEvmExecutionInfoV1(instruction.newEncodedExecutionInfo); - uint256 budget = executionInfo.gasLimit * executionInfo.targetChainRefundPerGasUnused - + instruction.newRequestedReceiverValue; + EvmExecutionInfoV1 memory executionInfo = decodeEvmExecutionInfoV1( + instruction.newEncodedExecutionInfo + ); + uint256 budget = executionInfo.gasLimit * + executionInfo.targetChainRefundPerGasUnused + + instruction.newRequestedReceiverValue; - bytes memory oldEncodedDeliveryVAA = - getPastDeliveryVAA(instruction.deliveryVaaKey.chainId, instruction.deliveryVaaKey.sequence); - bytes[] memory oldEncodedVMs = - getPastEncodedVMs(instruction.deliveryVaaKey.chainId, instruction.deliveryVaaKey.sequence); + bytes memory oldEncodedDeliveryVAA = getPastDeliveryVAA( + instruction.deliveryVaaKey.chainId, + instruction.deliveryVaaKey.sequence + ); + bytes[] memory oldEncodedSignedVaas = getPastEncodedSignedVaas( + instruction.deliveryVaaKey.chainId, + instruction.deliveryVaaKey.sequence + ); - uint16 targetChain = - decodeDeliveryInstruction(relayerWormhole.parseVM(oldEncodedDeliveryVAA).payload).targetChain; + uint16 targetChain = decodeDeliveryInstruction( + relayerWormhole.parseVM(oldEncodedDeliveryVAA).payload + ).targetChain; vm.selectFork(forks[targetChain]); - IWormholeRelayerDelivery(wormholeRelayerContracts[targetChain]).deliver{value: budget}( - oldEncodedVMs, oldEncodedDeliveryVAA, payable(address(this)), encode(deliveryOverride) + IWormholeRelayerDelivery(wormholeRelayerContracts[targetChain]) + .deliver{value: budget}( + oldEncodedSignedVaas, + oldEncodedDeliveryVAA, + payable(address(this)), + encode(deliveryOverride) ); } } diff --git a/src/testing/helpers/WormholeSimulator.sol b/src/testing/helpers/WormholeSimulator.sol index f2bc000..a73f6ca 100644 --- a/src/testing/helpers/WormholeSimulator.sol +++ b/src/testing/helpers/WormholeSimulator.sol @@ -12,13 +12,14 @@ import "forge-std/console.sol"; * @notice This contract simulates signing Wormhole messages emitted in a forge test. * It overrides the Wormhole guardian set to allow for signing messages with a single * private key on any EVM where Wormhole core contracts are deployed. - * @dev This contract is meant to be used when testing against a mainnet fork. + * @dev This contract is meant to be used when testing against a testnet or mainnet fork. */ contract WormholeSimulator { using BytesLib for bytes; // Taken from forge-std/Script.sol - address private constant VM_ADDRESS = address(bytes20(uint160(uint256(keccak256("hevm cheat code"))))); + address private constant VM_ADDRESS = + address(bytes20(uint160(uint256(keccak256("hevm cheat code"))))); Vm public constant vm = Vm(VM_ADDRESS); // Allow access to Wormhole @@ -44,15 +45,24 @@ contract WormholeSimulator { // Get slot for Guardian Set at the current index uint32 guardianSetIndex = wormhole.getCurrentGuardianSetIndex(); - bytes32 guardianSetSlot = keccak256(abi.encode(guardianSetIndex, 2)); + bytes32 guardianSetSlot = keccak256( + abi.encode(guardianSetIndex, 2) + ); // Overwrite all but first guardian set to zero address. This isn't // necessary, but just in case we inadvertently access these slots // for any reason. - uint256 numGuardians = uint256(vm.load(address(wormhole), guardianSetSlot)); - for (uint256 i = 1; i < numGuardians;) { + uint256 numGuardians = uint256( + vm.load(address(wormhole), guardianSetSlot) + ); + for (uint256 i = 1; i < numGuardians; ) { vm.store( - address(wormhole), bytes32(uint256(keccak256(abi.encodePacked(guardianSetSlot))) + i), bytes32(0) + address(wormhole), + bytes32( + uint256(keccak256(abi.encodePacked(guardianSetSlot))) + + i + ), + bytes32(0) ); unchecked { i += 1; @@ -63,7 +73,9 @@ contract WormholeSimulator { // in the function argument. vm.store( address(wormhole), - bytes32(uint256(keccak256(abi.encodePacked(guardianSetSlot))) + 0), // just explicit w/ index 0 + bytes32( + uint256(keccak256(abi.encodePacked(guardianSetSlot))) + 0 + ), // just explicit w/ index 0 bytes32(uint256(uint160(devnetGuardian))) ); @@ -75,17 +87,26 @@ contract WormholeSimulator { ); // Confirm guardian set override - address[] memory guardians = wormhole.getGuardianSet(guardianSetIndex).keys; + address[] memory guardians = wormhole + .getGuardianSet(guardianSetIndex) + .keys; require(guardians.length == 1, "guardians.length != 1"); - require(guardians[0] == devnetGuardian, "incorrect guardian set override"); + require( + guardians[0] == devnetGuardian, + "incorrect guardian set override" + ); } } - function doubleKeccak256(bytes memory body) internal pure returns (bytes32) { + function doubleKeccak256( + bytes memory body + ) internal pure returns (bytes32) { return keccak256(abi.encodePacked(keccak256(body))); } - function parseVMFromLogs(Vm.Log memory log) internal pure returns (IWormhole.VM memory vm_) { + function parseVMFromLogs( + Vm.Log memory log + ) internal pure returns (IWormhole.VM memory vm_) { uint256 index = 0; // emitterAddress @@ -123,10 +144,17 @@ contract WormholeSimulator { * @notice Finds published Wormhole events in forge logs * @param logs The forge Vm.log captured when recording events during test execution */ - function fetchWormholeMessageFromLog(Vm.Log[] memory logs) public pure returns (Vm.Log[] memory) { + function fetchWormholeMessageFromLog( + Vm.Log[] memory logs + ) public pure returns (Vm.Log[] memory) { uint256 count = 0; for (uint256 i = 0; i < logs.length; i++) { - if (logs[i].topics[0] == keccak256("LogMessagePublished(address,uint64,uint32,bytes,uint8)")) { + if ( + logs[i].topics[0] == + keccak256( + "LogMessagePublished(address,uint64,uint32,bytes,uint8)" + ) + ) { count += 1; } } @@ -136,7 +164,12 @@ contract WormholeSimulator { uint256 publishedIndex = 0; for (uint256 i = 0; i < logs.length; i++) { - if (logs[i].topics[0] == keccak256("LogMessagePublished(address,uint64,uint32,bytes,uint8)")) { + if ( + logs[i].topics[0] == + keccak256( + "LogMessagePublished(address,uint64,uint32,bytes,uint8)" + ) + ) { published[publishedIndex] = logs[i]; publishedIndex += 1; } @@ -150,7 +183,9 @@ contract WormholeSimulator { * @param vm_ Wormhole VM struct * @return encodedObservation Wormhole message body encoded into bytes */ - function encodeObservation(IWormhole.VM memory vm_) public pure returns (bytes memory encodedObservation) { + function encodeObservation( + IWormhole.VM memory vm_ + ) public pure returns (bytes memory encodedObservation) { encodedObservation = abi.encodePacked( vm_.timestamp, vm_.nonce, @@ -167,11 +202,10 @@ contract WormholeSimulator { * @param log The forge Vm.log captured when recording events during test execution * @return signedMessage Formatted and signed Wormhole message */ - function fetchSignedMessageFromLogs(Vm.Log memory log, uint16 emitterChainId) - public - view - returns (bytes memory signedMessage) - { + function fetchSignedMessageFromLogs( + Vm.Log memory log, + uint16 emitterChainId + ) public view returns (bytes memory signedMessage) { // Create message instance IWormhole.VM memory vm_; @@ -186,81 +220,14 @@ contract WormholeSimulator { return encodeAndSignMessage(vm_); } - /** - * @notice Formats and signs a simulated Wormhole batch VAA given an array of Wormhole log entries - * @param logs The forge Vm.log entries captured when recording events during test execution - * @param nonce The nonce of the messages to be accumulated into the batch VAA - * @return signedMessage Formatted and signed Wormhole message - */ - function fetchSignedBatchVAAFromLogs( - Vm.Log[] memory logs, - uint32 nonce, - uint16 emitterChainId, - address emitterAddress - ) public view returns (bytes memory signedMessage) { - uint8 numObservations = 0; - IWormhole.VM[] memory vm_ = new IWormhole.VM[](logs.length); - - for (uint256 i = 0; i < logs.length; i++) { - vm_[i] = parseVMFromLogs(logs[i]); - vm_[i].timestamp = uint32(block.timestamp); - vm_[i].emitterChainId = emitterChainId; - vm_[i].emitterAddress = bytes32(uint256(uint160(emitterAddress))); - if (vm_[i].nonce == nonce) { - numObservations += 1; - } - } - - bytes memory packedObservations; - bytes32[] memory hashes = new bytes32[](numObservations); - - uint8 counter = 0; - for (uint256 i = 0; i < logs.length; i++) { - if (vm_[i].nonce == nonce) { - bytes memory observation = abi.encodePacked( - vm_[i].timestamp, - vm_[i].nonce, - vm_[i].emitterChainId, - vm_[i].emitterAddress, - vm_[i].sequence, - vm_[i].consistencyLevel, - vm_[i].payload - ); - hashes[counter] = doubleKeccak256(observation); - packedObservations = - abi.encodePacked(packedObservations, uint8(counter), uint32(observation.length), observation); - counter++; - } - } - - bytes32 batchHash = doubleKeccak256(abi.encodePacked(uint8(2), keccak256(abi.encodePacked(hashes)))); - - IWormhole.Signature[] memory sigs = new IWormhole.Signature[](1); - (sigs[0].v, sigs[0].r, sigs[0].s) = vm.sign(devnetGuardianPK, batchHash); - - sigs[0].guardianIndex = 0; - - signedMessage = abi.encodePacked( - uint8(2), - wormhole.getCurrentGuardianSetIndex(), - uint8(1), - sigs[0].guardianIndex, - sigs[0].r, - sigs[0].s, - uint8(sigs[0].v - 27), - numObservations, - hashes, - numObservations, - packedObservations - ); - } - /** * @notice Signs and preformatted simulated Wormhole message * @param vm_ The preformatted Wormhole message * @return signedMessage Formatted and signed Wormhole message */ - function encodeAndSignMessage(IWormhole.VM memory vm_) public view returns (bytes memory signedMessage) { + function encodeAndSignMessage( + IWormhole.VM memory vm_ + ) public view returns (bytes memory signedMessage) { // Compute the hash of the body bytes memory body = encodeObservation(vm_); vm_.hash = doubleKeccak256(body); @@ -288,7 +255,12 @@ contract WormholeSimulator { */ function setMessageFee(uint256 newFee) public { bytes32 coreModule = 0x00000000000000000000000000000000000000000000000000000000436f7265; - bytes memory message = abi.encodePacked(coreModule, uint8(3), uint16(wormhole.chainId()), newFee); + bytes memory message = abi.encodePacked( + coreModule, + uint8(3), + uint16(wormhole.chainId()), + newFee + ); IWormhole.VM memory preSignedMessage = IWormhole.VM({ version: 1, timestamp: uint32(block.timestamp), diff --git a/test/CCTPAndTokenBridgeBase.t.sol b/test/CCTPAndTokenBridgeBase.t.sol new file mode 100644 index 0000000..2f32bb5 --- /dev/null +++ b/test/CCTPAndTokenBridgeBase.t.sol @@ -0,0 +1,247 @@ +pragma solidity ^0.8.13; + +import "../src/WormholeRelayerSDK.sol"; +import "../src/interfaces/IWormholeReceiver.sol"; +import "../src/interfaces/IWormholeRelayer.sol"; +import "../src/interfaces/IERC20.sol"; + +import "../src/testing/WormholeRelayerTest.sol"; + +import "../src/WormholeRelayerSDK.sol"; +import "../src/Utils.sol"; + +contract CCTPAndTokenBridgeToy is CCTPAndTokenSender, CCTPAndTokenReceiver { + uint256 constant GAS_LIMIT = 250_000; + + constructor( + address _wormholeRelayer, + address _tokenBridge, + address _wormhole, + address _circleMessageTransmitter, + address _circleTokenMessenger, + address _USDC + ) + CCTPAndTokenBase( + _wormholeRelayer, + _tokenBridge, + _wormhole, + _circleMessageTransmitter, + _circleTokenMessenger, + _USDC + ) + { + setCCTPDomain(2, 0); + setCCTPDomain(6, 1); + } + + function quoteCrossChainDeposit( + uint16 targetChain + ) public view returns (uint256 cost) { + // Cost of delivering token and payload to targetChain + (cost, ) = wormholeRelayer.quoteEVMDeliveryPrice( + targetChain, + 0, + GAS_LIMIT + ); + } + + function sendCrossChainDeposit( + uint16 targetChain, + address recipient, + uint256 amount, + address token + ) public payable { + uint256 cost = quoteCrossChainDeposit(targetChain); + require( + msg.value == cost, + "msg.value must be quoteCrossChainDeposit(targetChain)" + ); + + IERC20(token).transferFrom(msg.sender, address(this), amount); + + bytes memory payload = abi.encode(recipient, amount); + sendTokenWithPayloadToEvm( + targetChain, + fromWormholeFormat(registeredSenders[targetChain]), // address (on targetChain) to send token and payload to + payload, + 0, // receiver value + GAS_LIMIT, + token, + amount + ); + } + + function sendCrossChainUSDCDeposit( + uint16 targetChain, + address recipient, + uint256 amount + ) public payable { + uint256 cost = quoteCrossChainDeposit(targetChain); + require( + msg.value == cost, + "msg.value must be quoteCrossChainDeposit(targetChain)" + ); + + IERC20(USDC).transferFrom(msg.sender, address(this), amount); + + bytes memory payload = abi.encode(recipient, amount); + sendUSDCWithPayloadToEvm( + targetChain, + fromWormholeFormat(registeredSenders[targetChain]), // address (on targetChain) to send token and payload to + payload, + 0, // receiver value + GAS_LIMIT, + amount + ); + } + + function receivePayloadAndUSDC( + bytes memory payload, + uint256 amount, + bytes32 sourceAddress, + uint16 sourceChain, + bytes32 // deliveryHash + ) + internal + override + onlyWormholeRelayer + isRegisteredSender(sourceChain, sourceAddress) + { + (address recipient, uint256 expectedAmount) = abi.decode( + payload, + (address, uint256) + ); + require(amount == expectedAmount, "amount != payload.expectedAmount"); + IERC20(USDC).transfer(recipient, amount); + } + + function receivePayloadAndTokens( + bytes memory payload, + TokenReceived[] memory receivedTokens, + bytes32 sourceAddress, + uint16 sourceChain, + bytes32 // deliveryHash + ) + internal + override + onlyWormholeRelayer + isRegisteredSender(sourceChain, sourceAddress) + { + require(receivedTokens.length == 1, "Expected 1 token transfers"); + address recipient = abi.decode(payload, (address)); + IERC20(receivedTokens[0].tokenAddress).transfer( + recipient, + receivedTokens[0].amount + ); + } +} + +contract WormholeSDKTest is WormholeRelayerBasicTest { + CCTPAndTokenBridgeToy CCTPAndTokenBridgeToySource; + CCTPAndTokenBridgeToy CCTPAndTokenBridgeToyTarget; + ERC20Mock USDCSource; + ERC20Mock USDCTarget; + ERC20Mock public token; + + constructor() { + setTestnetForkChains(2, 6); + } + + function setUpSource() public override { + USDCSource = ERC20Mock(address(sourceChainInfo.USDC)); + mintUSDC(sourceChain, address(this), 5000e18); + CCTPAndTokenBridgeToySource = new CCTPAndTokenBridgeToy( + address(relayerSource), + address(tokenBridgeSource), + address(wormholeSource), + address(sourceChainInfo.circleMessageTransmitter), + address(sourceChainInfo.circleTokenMessenger), + address(USDCSource) + ); + token = createAndAttestToken(sourceChain); + } + + function setUpTarget() public override { + USDCTarget = ERC20Mock(address(targetChainInfo.USDC)); + mintUSDC(targetChain, address(this), 5000e18); + CCTPAndTokenBridgeToyTarget = new CCTPAndTokenBridgeToy( + address(relayerTarget), + address(tokenBridgeTarget), + address(wormholeTarget), + address(targetChainInfo.circleMessageTransmitter), + address(targetChainInfo.circleTokenMessenger), + address(USDCTarget) + ); + } + + function setUpGeneral() public override { + vm.selectFork(sourceFork); + CCTPAndTokenBridgeToySource.setRegisteredSender( + targetChain, + toWormholeFormat(address(CCTPAndTokenBridgeToyTarget)) + ); + + vm.selectFork(targetFork); + CCTPAndTokenBridgeToyTarget.setRegisteredSender( + sourceChain, + toWormholeFormat(address(CCTPAndTokenBridgeToySource)) + ); + } + + function testSendUSDC() public { + vm.selectFork(sourceFork); + + uint256 amount = 100e6; + USDCSource.approve(address(CCTPAndTokenBridgeToySource), amount); + + vm.selectFork(targetFork); + address recipient = 0x1234567890123456789012345678901234567890; + + vm.selectFork(sourceFork); + uint256 cost = CCTPAndTokenBridgeToySource.quoteCrossChainDeposit( + targetChain + ); + + vm.recordLogs(); + CCTPAndTokenBridgeToySource.sendCrossChainUSDCDeposit{value: cost}( + targetChain, + recipient, + amount + ); + performDelivery(true); + + vm.selectFork(targetFork); + assertEq(IERC20(USDCTarget).balanceOf(recipient), amount); + } + + function testSendToken() public { + vm.selectFork(sourceFork); + + uint256 amount = 19e17; + token.approve(address(CCTPAndTokenBridgeToySource), amount); + + vm.selectFork(targetFork); + address recipient = 0x1234567890123456789012345678901234567890; + + vm.selectFork(sourceFork); + uint256 cost = CCTPAndTokenBridgeToySource.quoteCrossChainDeposit( + targetChain + ); + + vm.recordLogs(); + CCTPAndTokenBridgeToySource.sendCrossChainDeposit{value: cost}( + targetChain, + recipient, + amount, + address(token) + ); + performDelivery(); + + vm.selectFork(targetFork); + address wormholeWrappedToken = tokenBridgeTarget.wrappedAsset( + sourceChain, + toWormholeFormat(address(token)) + ); + assertEq(IERC20(wormholeWrappedToken).balanceOf(recipient), amount); + } +} diff --git a/test/CCTPBase.t.sol b/test/CCTPBase.t.sol new file mode 100644 index 0000000..9f75fb6 --- /dev/null +++ b/test/CCTPBase.t.sol @@ -0,0 +1,162 @@ +pragma solidity ^0.8.13; + +import "../src/WormholeRelayerSDK.sol"; +import "../src/interfaces/IWormholeReceiver.sol"; +import "../src/interfaces/IWormholeRelayer.sol"; +import "../src/interfaces/IERC20.sol"; + +import "../src/testing/WormholeRelayerTest.sol"; + +import "../src/WormholeRelayerSDK.sol"; +import "../src/Utils.sol"; + +contract CCTPToy is CCTPSender, CCTPReceiver { + uint256 constant GAS_LIMIT = 250_000; + + constructor( + address _wormholeRelayer, + address _wormhole, + address _circleMessageTransmitter, + address _circleTokenMessenger, + address _USDC + ) + CCTPBase( + _wormholeRelayer, + _wormhole, + _circleMessageTransmitter, + _circleTokenMessenger, + _USDC + ) + { + setCCTPDomain(2, 0); + setCCTPDomain(6, 1); + } + + function quoteCrossChainDeposit( + uint16 targetChain + ) public view returns (uint256 cost) { + // Cost of delivering token and payload to targetChain + (cost, ) = wormholeRelayer.quoteEVMDeliveryPrice( + targetChain, + 0, + GAS_LIMIT + ); + } + + function sendCrossChainDeposit( + uint16 targetChain, + address recipient, + uint256 amount + ) public payable { + uint256 cost = quoteCrossChainDeposit(targetChain); + require( + msg.value == cost, + "msg.value must be quoteCrossChainDeposit(targetChain)" + ); + + IERC20(USDC).transferFrom(msg.sender, address(this), amount); + + bytes memory payload = abi.encode(recipient, amount); + sendUSDCWithPayloadToEvm( + targetChain, + fromWormholeFormat(registeredSenders[targetChain]), // address (on targetChain) to send token and payload to + payload, + 0, // receiver value + GAS_LIMIT, + amount + ); + } + + function receivePayloadAndUSDC( + bytes memory payload, + uint256 amount, + bytes32 sourceAddress, + uint16 sourceChain, + bytes32 // deliveryHash + ) + internal + override + onlyWormholeRelayer + isRegisteredSender(sourceChain, sourceAddress) + { + (address recipient, uint256 expectedAmount) = abi.decode( + payload, + (address, uint256) + ); + require(amount == expectedAmount, "amount != payload.expectedAmount"); + IERC20(USDC).transfer(recipient, amount); + } +} + +contract WormholeSDKTest is WormholeRelayerBasicTest { + CCTPToy CCTPToySource; + CCTPToy CCTPToyTarget; + ERC20Mock USDCSource; + ERC20Mock USDCTarget; + + constructor() { + setTestnetForkChains(2, 6); + } + + function setUpSource() public override { + USDCSource = ERC20Mock(address(sourceChainInfo.USDC)); + mintUSDC(sourceChain, address(this), 5000e18); + CCTPToySource = new CCTPToy( + address(relayerSource), + address(wormholeSource), + address(sourceChainInfo.circleMessageTransmitter), + address(sourceChainInfo.circleTokenMessenger), + address(USDCSource) + ); + } + + function setUpTarget() public override { + USDCTarget = ERC20Mock(address(targetChainInfo.USDC)); + mintUSDC(targetChain, address(this), 5000e18); + CCTPToyTarget = new CCTPToy( + address(relayerTarget), + address(wormholeTarget), + address(targetChainInfo.circleMessageTransmitter), + address(targetChainInfo.circleTokenMessenger), + address(USDCTarget) + ); + } + + function setUpGeneral() public override { + vm.selectFork(sourceFork); + CCTPToySource.setRegisteredSender( + targetChain, + toWormholeFormat(address(CCTPToyTarget)) + ); + + vm.selectFork(targetFork); + CCTPToyTarget.setRegisteredSender( + sourceChain, + toWormholeFormat(address(CCTPToySource)) + ); + } + + function testSendToken() public { + vm.selectFork(sourceFork); + + uint256 amount = 100e6; + USDCSource.approve(address(CCTPToySource), amount); + + vm.selectFork(targetFork); + address recipient = 0x1234567890123456789012345678901234567890; + + vm.selectFork(sourceFork); + uint256 cost = CCTPToySource.quoteCrossChainDeposit(targetChain); + + vm.recordLogs(); + CCTPToySource.sendCrossChainDeposit{value: cost}( + targetChain, + recipient, + amount + ); + performDelivery(true); + + vm.selectFork(targetFork); + assertEq(IERC20(USDCTarget).balanceOf(recipient), amount); + } +} diff --git a/test/ChooseChains.t.sol b/test/ChooseChains.t.sol index a358e25..6ea50c3 100644 --- a/test/ChooseChains.t.sol +++ b/test/ChooseChains.t.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: UNLICENSED + pragma solidity ^0.8.13; import "../src/WormholeRelayerSDK.sol"; diff --git a/test/ExtraChains.t.sol b/test/ExtraChains.t.sol index cb708c4..e38066a 100644 --- a/test/ExtraChains.t.sol +++ b/test/ExtraChains.t.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: UNLICENSED + pragma solidity ^0.8.13; import "../src/WormholeRelayerSDK.sol"; diff --git a/test/Fork.t.sol b/test/Fork.t.sol index 698ea83..c20b56e 100644 --- a/test/Fork.t.sol +++ b/test/Fork.t.sol @@ -1,4 +1,3 @@ -// SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.13; import "../src/WormholeRelayerSDK.sol"; @@ -18,15 +17,23 @@ contract Toy is Base { uint256 public payloadReceived; - constructor(address _wormholeRelayer, address _wormhole) Base(_wormholeRelayer, _wormhole) {} + constructor( + address _wormholeRelayer, + address _wormhole + ) Base(_wormholeRelayer, _wormhole) {} function receiveWormholeMessages( bytes memory payload, bytes[] memory, bytes32 sourceAddress, uint16 sourceChain, - bytes32 deliveryHash - ) public payable onlyWormholeRelayer replayProtect(deliveryHash) isRegisteredSender(sourceChain, sourceAddress) { + bytes32 // deliveryHash + ) + public + payable + onlyWormholeRelayer + isRegisteredSender(sourceChain, sourceAddress) + { payloadReceived = abi.decode(payload, (uint256)); console.log("Toy received message"); @@ -36,19 +43,29 @@ contract Toy is Base { } contract TokenToy is TokenSender, TokenReceiver { - constructor (address _wormholeRelayer, address _bridge, address _wormhole) TokenBase(_wormholeRelayer, _bridge, _wormhole) {} - + constructor( + address _wormholeRelayer, + address _bridge, + address _wormhole + ) TokenBase(_wormholeRelayer, _bridge, _wormhole) {} + uint256 constant GAS_LIMIT = 250_000; - - function quoteCrossChainDeposit(uint16 targetChain) public view returns (uint256 cost) { + + function quoteCrossChainDeposit( + uint16 targetChain + ) public view returns (uint256 cost) { // Cost of delivering token and payload to targetChain uint256 deliveryCost; - (deliveryCost,) = wormholeRelayer.quoteEVMDeliveryPrice(targetChain, 0, GAS_LIMIT); + (deliveryCost, ) = wormholeRelayer.quoteEVMDeliveryPrice( + targetChain, + 0, + GAS_LIMIT + ); // Total cost: delivery cost + cost of publishing the 'sending token' wormhole message cost = deliveryCost + wormhole.messageFee(); } - + function sendCrossChainDeposit( uint16 targetChain, address recipient, @@ -56,17 +73,20 @@ contract TokenToy is TokenSender, TokenReceiver { address token ) public payable { uint256 cost = quoteCrossChainDeposit(targetChain); - require(msg.value == cost, "msg.value must be quoteCrossChainDeposit(targetChain)"); + require( + msg.value == cost, + "msg.value must be quoteCrossChainDeposit(targetChain)" + ); IERC20(token).transferFrom(msg.sender, address(this), amount); bytes memory payload = abi.encode(recipient); sendTokenWithPayloadToEvm( - targetChain, + targetChain, fromWormholeFormat(registeredSenders[targetChain]), // address (on targetChain) to send token and payload to - payload, + payload, 0, // receiver value - GAS_LIMIT, + GAS_LIMIT, token, // address of IERC20 token contract amount ); @@ -81,17 +101,20 @@ contract TokenToy is TokenSender, TokenReceiver { address refundAddress ) public payable { uint256 cost = quoteCrossChainDeposit(targetChain); - require(msg.value == cost, "msg.value must be quoteCrossChainDeposit(targetChain)"); + require( + msg.value == cost, + "msg.value must be quoteCrossChainDeposit(targetChain)" + ); IERC20(token).transferFrom(msg.sender, address(this), amount); bytes memory payload = abi.encode(recipient); sendTokenWithPayloadToEvm( - targetChain, + targetChain, fromWormholeFormat(registeredSenders[targetChain]), // address (on targetChain) to send token and payload to - payload, + payload, 0, // receiver value - GAS_LIMIT, + GAS_LIMIT, token, // address of IERC20 token contract amount, refundChain, @@ -105,10 +128,18 @@ contract TokenToy is TokenSender, TokenReceiver { bytes32 sourceAddress, uint16 sourceChain, bytes32 // deliveryHash - ) internal override onlyWormholeRelayer isRegisteredSender(sourceChain, sourceAddress) { + ) + internal + override + onlyWormholeRelayer + isRegisteredSender(sourceChain, sourceAddress) + { require(receivedTokens.length == 1, "Expected 1 token transfers"); address recipient = abi.decode(payload, (address)); - IERC20(receivedTokens[0].tokenAddress).transfer(recipient, receivedTokens[0].amount); + IERC20(receivedTokens[0].tokenAddress).transfer( + recipient, + receivedTokens[0].amount + ); } } @@ -121,35 +152,62 @@ contract WormholeSDKTest is WormholeRelayerBasicTest { function setUpSource() public override { toySource = new Toy(address(relayerSource), address(wormholeSource)); - toySource.setRegisteredSender(targetChain, toWormholeFormat(address(this))); + toySource.setRegisteredSender( + targetChain, + toWormholeFormat(address(this)) + ); - tokenToySource = new TokenToy(address(relayerSource), address(tokenBridgeSource), address(wormholeSource)); + tokenToySource = new TokenToy( + address(relayerSource), + address(tokenBridgeSource), + address(wormholeSource) + ); token = createAndAttestToken(sourceChain); } function setUpTarget() public override { toyTarget = new Toy(address(relayerTarget), address(wormholeTarget)); - toyTarget.setRegisteredSender(sourceChain, toWormholeFormat(address(this))); + toyTarget.setRegisteredSender( + sourceChain, + toWormholeFormat(address(this)) + ); - tokenToyTarget = new TokenToy(address(relayerTarget), address(tokenBridgeTarget), address(wormholeTarget)); + tokenToyTarget = new TokenToy( + address(relayerTarget), + address(tokenBridgeTarget), + address(wormholeTarget) + ); } function setUpGeneral() public override { vm.selectFork(sourceFork); - tokenToySource.setRegisteredSender(targetChain, toWormholeFormat(address(tokenToyTarget))); - + tokenToySource.setRegisteredSender( + targetChain, + toWormholeFormat(address(tokenToyTarget)) + ); vm.selectFork(targetFork); - tokenToyTarget.setRegisteredSender(sourceChain, toWormholeFormat(address(tokenToySource))); - + tokenToyTarget.setRegisteredSender( + sourceChain, + toWormholeFormat(address(tokenToySource)) + ); } function testSendMessage() public { - vm.recordLogs(); - (uint256 cost,) = relayerSource.quoteEVMDeliveryPrice(targetChain, 1e17, 100_000); - relayerSource.sendPayloadToEvm{value: cost}(targetChain, address(toyTarget), abi.encode(55), 1e17, 100_000); + (uint256 cost, ) = relayerSource.quoteEVMDeliveryPrice( + targetChain, + 1e17, + 100_000 + ); + relayerSource.sendPayloadToEvm{value: cost}( + targetChain, + address(toyTarget), + abi.encode(55), + 1e17, + 100_000 + ); performDelivery(); vm.selectFork(targetFork); @@ -160,8 +218,18 @@ contract WormholeSDKTest is WormholeRelayerBasicTest { vm.selectFork(targetFork); vm.recordLogs(); - (uint256 cost,) = relayerTarget.quoteEVMDeliveryPrice(sourceChain, 1e17, 100_000); - relayerTarget.sendPayloadToEvm{value: cost}(sourceChain, address(toySource), abi.encode(56), 1e17, 100_000); + (uint256 cost, ) = relayerTarget.quoteEVMDeliveryPrice( + sourceChain, + 1e17, + 100_000 + ); + relayerTarget.sendPayloadToEvm{value: cost}( + sourceChain, + address(toySource), + abi.encode(56), + 1e17, + 100_000 + ); performDelivery(); vm.selectFork(sourceFork); @@ -169,7 +237,6 @@ contract WormholeSDKTest is WormholeRelayerBasicTest { } function testSendToken() public { - vm.selectFork(sourceFork); uint256 amount = 19e17; @@ -183,17 +250,22 @@ contract WormholeSDKTest is WormholeRelayerBasicTest { vm.recordLogs(); tokenToySource.sendCrossChainDeposit{value: cost}( - targetChain, recipient, amount, address(token) + targetChain, + recipient, + amount, + address(token) ); performDelivery(); vm.selectFork(targetFork); - address wormholeWrappedToken = tokenBridgeTarget.wrappedAsset(sourceChain, toWormholeFormat(address(token))); + address wormholeWrappedToken = tokenBridgeTarget.wrappedAsset( + sourceChain, + toWormholeFormat(address(token)) + ); assertEq(IERC20(wormholeWrappedToken).balanceOf(recipient), amount); } function testSendTokenWithRefund() public { - vm.selectFork(sourceFork); uint256 amount = 19e17; @@ -207,12 +279,20 @@ contract WormholeSDKTest is WormholeRelayerBasicTest { vm.recordLogs(); tokenToySource.sendCrossChainDeposit{value: cost}( - targetChain, recipient, amount, address(token), targetChain, refundAddress + targetChain, + recipient, + amount, + address(token), + targetChain, + refundAddress ); performDelivery(); vm.selectFork(targetFork); - address wormholeWrappedToken = tokenBridgeTarget.wrappedAsset(sourceChain, toWormholeFormat(address(token))); + address wormholeWrappedToken = tokenBridgeTarget.wrappedAsset( + sourceChain, + toWormholeFormat(address(token)) + ); assertEq(IERC20(wormholeWrappedToken).balanceOf(recipient), amount); assertTrue(refundAddress.balance > 0); }