Skip to content

Commit

Permalink
evm: add custom payload ID support
Browse files Browse the repository at this point in the history
  • Loading branch information
a5-pickle committed Jan 11, 2024
1 parent a55e233 commit 1709060
Showing 1 changed file with 88 additions and 4 deletions.
92 changes: 88 additions & 4 deletions evm/src/libraries/WormholeCctpMessages.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ library WormholeCctpMessages {
error InvalidMessage();
error UnexpectedMessageLength(uint256, uint256);

/**
* @dev NOTE: This method encodes the Wormhole message payload assuming the payload ID == 1.
*/
function encodeDeposit(
address token,
uint256 amount,
Expand All @@ -42,6 +45,7 @@ library WormholeCctpMessages {
) internal pure returns (bytes memory encoded) {
encoded = encodeDeposit(
token.toUniversalAddress(),
DEPOSIT,
amount,
sourceCctpDomain,
targetCctpDomain,
Expand All @@ -52,6 +56,9 @@ library WormholeCctpMessages {
);
}

/**
* @dev NOTE: This method encodes the Wormhole message payload assuming the payload ID == 1.
*/
function encodeDeposit(
bytes32 universalTokenAddress,
uint256 amount,
Expand All @@ -61,6 +68,54 @@ library WormholeCctpMessages {
bytes32 burnSource,
bytes32 mintRecipient,
bytes memory payload
) internal pure returns (bytes memory encoded) {
encoded = encodeDeposit(
universalTokenAddress,
DEPOSIT,
amount,
sourceCctpDomain,
targetCctpDomain,
cctpNonce,
burnSource,
mintRecipient,
payload
);
}

function encodeDeposit(
address token,
uint8 payloadId,
uint256 amount,
uint32 sourceCctpDomain,
uint32 targetCctpDomain,
uint64 cctpNonce,
bytes32 burnSource,
bytes32 mintRecipient,
bytes memory payload
) internal pure returns (bytes memory encoded) {
encoded = encodeDeposit(
token.toUniversalAddress(),
payloadId,
amount,
sourceCctpDomain,
targetCctpDomain,
cctpNonce,
burnSource,
mintRecipient,
payload
);
}

function encodeDeposit(
bytes32 universalTokenAddress,
uint8 payloadId,
uint256 amount,
uint32 sourceCctpDomain,
uint32 targetCctpDomain,
uint64 cctpNonce,
bytes32 burnSource,
bytes32 mintRecipient,
bytes memory payload
) internal pure returns (bytes memory encoded) {
uint256 payloadLen = payload.length;
if (payloadLen == 0) {
Expand All @@ -70,7 +125,7 @@ library WormholeCctpMessages {
}

encoded = abi.encodePacked(
DEPOSIT,
payloadId,
universalTokenAddress,
amount,
sourceCctpDomain,
Expand All @@ -83,6 +138,9 @@ library WormholeCctpMessages {
);
}

/**
* @dev NOTE: This method decodes the VAA payload assuming the payload ID == 1.
*/
function decodeDeposit(IWormhole.VM memory vaa)
internal
pure
Expand All @@ -106,10 +164,36 @@ library WormholeCctpMessages {
burnSource,
mintRecipient,
payload
) = decodeDeposit(vaa, true);
) = decodeDeposit(vaa, DEPOSIT, true);
}

function decodeDeposit(IWormhole.VM memory vaa, uint8 payloadId)
internal
pure
returns (
bytes32 token,
uint256 amount,
uint32 sourceCctpDomain,
uint32 targetCctpDomain,
uint64 cctpNonce,
bytes32 burnSource,
bytes32 mintRecipient,
bytes memory payload
)
{
(
token,
amount,
sourceCctpDomain,
targetCctpDomain,
cctpNonce,
burnSource,
mintRecipient,
payload
) = decodeDeposit(vaa, payloadId, true);
}

function decodeDeposit(IWormhole.VM memory vaa, bool revertCustomErrors)
function decodeDeposit(IWormhole.VM memory vaa, uint8 payloadId, bool revertCustomErrors)
internal
pure
returns (
Expand All @@ -124,7 +208,7 @@ library WormholeCctpMessages {
)
{
bytes memory encoded = vaa.payload;
uint256 offset = _checkPayloadId(encoded, 0, DEPOSIT, revertCustomErrors);
uint256 offset = _checkPayloadId(encoded, 0, payloadId, revertCustomErrors);

(token, offset) = encoded.asBytes32Unchecked(offset);
(amount, offset) = encoded.asUint256Unchecked(offset);
Expand Down

0 comments on commit 1709060

Please sign in to comment.