Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nonergodic committed Dec 19, 2024
1 parent a126d90 commit c6f5ca7
Show file tree
Hide file tree
Showing 15 changed files with 1,099 additions and 314 deletions.
4 changes: 0 additions & 4 deletions docs/Testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ The `WormholeCctpSimulator` contract can be deployed to simulate a virtual `Worm

Forge's `deal` cheat code does not work for USDC. `UsdcDealer` is another override library that implements a `deal` function that allows minting of USDC.

### CctpMessages

Library to parse CCTP messages composed/emitted by Circle's `TokenMessenger` and `MessageTransmitter` contracts. Used in `CctpOverride` and `WormholeCctpSimulator`.

### ERC20Mock

Copy of SolMate's ERC20 Mock token that uses the overrideable `IERC20` interface of this SDK to guarantee compatibility.
Expand Down
67 changes: 23 additions & 44 deletions src/WormholeCctpTokenMessenger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {IMessageTransmitter} from "wormhole-sdk/interfaces/cctp/IMessageTransmit
import {ITokenMessenger} from "wormhole-sdk/interfaces/cctp/ITokenMessenger.sol";
import {ITokenMinter} from "wormhole-sdk/interfaces/cctp/ITokenMinter.sol";

import {toUniversalAddress} from "wormhole-sdk/Utils.sol";
import {toUniversalAddress,
eagerAnd,
eagerOr} from "wormhole-sdk/Utils.sol";
import {WormholeCctpMessages} from "wormhole-sdk/libraries/WormholeCctpMessages.sol";
import {CONSISTENCY_LEVEL_FINALIZED} from "wormhole-sdk/constants/ConsistencyLevel.sol";

Expand Down Expand Up @@ -37,7 +39,7 @@ abstract contract WormholeCctpTokenMessenger {
/// @dev The emitter of the VAA must match the expected emitter.
error UnexpectedEmitter(bytes32, bytes32);

/// @dev Wormhole Core Bridge contract address.
/// @dev Wormhole Core Bridge contract address.
IWormhole immutable _wormhole;

/// @dev Wormhole Chain ID. NOTE: This is NOT the EVM chain ID.
Expand Down Expand Up @@ -114,7 +116,7 @@ abstract contract WormholeCctpTokenMessenger {
mintRecipient,
payload
),
CONSISTENCY_LEVEL_FINALIZED
CONSISTENCY_LEVEL_FINALIZED
);
}

Expand All @@ -138,7 +140,7 @@ abstract contract WormholeCctpTokenMessenger {
bytes memory payload
) {
// First parse and verify VAA.
vaa = _parseAndVerifyVaa( encodedVaa, true /*revertCustomErrors*/);
vaa = _parseAndVerifyVaa( encodedVaa);

// Decode the deposit message so we can match the Wormhole message with the CCTP message.
uint32 sourceCctpDomain;
Expand All @@ -162,8 +164,7 @@ abstract contract WormholeCctpTokenMessenger {
sourceCctpDomain,
destinationCctpDomain,
cctpNonce,
token,
true // revertCustomErrors
token
);
}

Expand Down Expand Up @@ -191,7 +192,7 @@ abstract contract WormholeCctpTokenMessenger {
bytes memory payload
) {
// First parse and verify VAA.
vaa = _parseAndVerifyVaa(encodedVaa, false /*revertCustomErrors*/);
vaa = _parseAndVerifyVaa(encodedVaa);

// Decode the deposit message so we can match the Wormhole message with the CCTP message.
(
Expand All @@ -212,8 +213,7 @@ abstract contract WormholeCctpTokenMessenger {
sourceCctpDomain,
destinationCctpDomain,
cctpNonce,
token,
false // revertCustomErrors
token
);
}

Expand All @@ -238,37 +238,20 @@ abstract contract WormholeCctpTokenMessenger {
* NOTE: Reverts with `UnexpectedEmitter(bytes32, bytes32)`.
*/
function requireEmitter(IWormhole.VM memory vaa, bytes32 expectedEmitter) internal pure {
if (expectedEmitter != 0 && vaa.emitterAddress != expectedEmitter)
if (eagerAnd(expectedEmitter != 0, vaa.emitterAddress != expectedEmitter))
revert UnexpectedEmitter(vaa.emitterAddress, expectedEmitter);
}

/**
* @dev We encourage an integrator to use this method to make sure the VAA is emitted from one
* that his contract trusts. Usually foreign emitters are stored in a mapping keyed off by
* Wormhole Chain ID (uint16).
*
* NOTE: Reverts with built-in Error(string).
*/
function requireEmitterLegacy(IWormhole.VM memory vaa, bytes32 expectedEmitter) internal pure {
require(expectedEmitter != 0 && vaa.emitterAddress == expectedEmitter, "unknown emitter");
}

// private
// ----- private methods -----

function _parseAndVerifyVaa(
bytes calldata encodedVaa,
bool revertCustomErrors
bytes calldata encodedVaa
) private view returns (IWormhole.VM memory vaa) {
bool valid;
string memory reason;
(vaa, valid, reason) = _wormhole.parseAndVerifyVM(encodedVaa);

if (!valid) {
if (revertCustomErrors)
revert InvalidVaa();
else
require(false, reason);
}
(vaa, valid, ) = _wormhole.parseAndVerifyVM(encodedVaa);

if (!valid)
revert InvalidVaa();
}

function _matchMessagesAndMint(
Expand All @@ -277,8 +260,7 @@ abstract contract WormholeCctpTokenMessenger {
uint32 vaaSourceCctpDomain,
uint32 vaaDestinationCctpDomain,
uint64 vaaCctpNonce,
bytes32 burnToken,
bool revertCustomErrors
bytes32 burnToken
) private returns (bytes32 mintToken) {
// Confirm that the caller passed the correct message pair.
{
Expand All @@ -301,16 +283,13 @@ abstract contract WormholeCctpTokenMessenger {
nonce := shr(96, ptr)
}

if (
vaaSourceCctpDomain != sourceDomain ||
vaaDestinationCctpDomain != destinationDomain||
//avoid short circuiting (more gas and bytecode efficient)
if (eagerOr(eagerOr(
vaaSourceCctpDomain != sourceDomain,
vaaDestinationCctpDomain != destinationDomain,
vaaCctpNonce != nonce
) {
if (revertCustomErrors)
revert CctpVaaMismatch(sourceDomain, destinationDomain, nonce);
else
require(false, "invalid message pair");
}
)))
revert CctpVaaMismatch(sourceDomain, destinationDomain, nonce);
}

// Call the circle bridge to mint tokens to the recipient.
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/BytesParsing.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache 2
pragma solidity ^0.8.4;

import "../constants/Common.sol";
import "wormhole-sdk/constants/Common.sol";

//This file appears comically large, but all unused functions are removed by the compiler.
library BytesParsing {
Expand Down
Loading

0 comments on commit c6f5ca7

Please sign in to comment.