Skip to content

Commit 1709060

Browse files
committed
evm: add custom payload ID support
1 parent a55e233 commit 1709060

File tree

1 file changed

+88
-4
lines changed

1 file changed

+88
-4
lines changed

evm/src/libraries/WormholeCctpMessages.sol

+88-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ library WormholeCctpMessages {
3030
error InvalidMessage();
3131
error UnexpectedMessageLength(uint256, uint256);
3232

33+
/**
34+
* @dev NOTE: This method encodes the Wormhole message payload assuming the payload ID == 1.
35+
*/
3336
function encodeDeposit(
3437
address token,
3538
uint256 amount,
@@ -42,6 +45,7 @@ library WormholeCctpMessages {
4245
) internal pure returns (bytes memory encoded) {
4346
encoded = encodeDeposit(
4447
token.toUniversalAddress(),
48+
DEPOSIT,
4549
amount,
4650
sourceCctpDomain,
4751
targetCctpDomain,
@@ -52,6 +56,9 @@ library WormholeCctpMessages {
5256
);
5357
}
5458

59+
/**
60+
* @dev NOTE: This method encodes the Wormhole message payload assuming the payload ID == 1.
61+
*/
5562
function encodeDeposit(
5663
bytes32 universalTokenAddress,
5764
uint256 amount,
@@ -61,6 +68,54 @@ library WormholeCctpMessages {
6168
bytes32 burnSource,
6269
bytes32 mintRecipient,
6370
bytes memory payload
71+
) internal pure returns (bytes memory encoded) {
72+
encoded = encodeDeposit(
73+
universalTokenAddress,
74+
DEPOSIT,
75+
amount,
76+
sourceCctpDomain,
77+
targetCctpDomain,
78+
cctpNonce,
79+
burnSource,
80+
mintRecipient,
81+
payload
82+
);
83+
}
84+
85+
function encodeDeposit(
86+
address token,
87+
uint8 payloadId,
88+
uint256 amount,
89+
uint32 sourceCctpDomain,
90+
uint32 targetCctpDomain,
91+
uint64 cctpNonce,
92+
bytes32 burnSource,
93+
bytes32 mintRecipient,
94+
bytes memory payload
95+
) internal pure returns (bytes memory encoded) {
96+
encoded = encodeDeposit(
97+
token.toUniversalAddress(),
98+
payloadId,
99+
amount,
100+
sourceCctpDomain,
101+
targetCctpDomain,
102+
cctpNonce,
103+
burnSource,
104+
mintRecipient,
105+
payload
106+
);
107+
}
108+
109+
function encodeDeposit(
110+
bytes32 universalTokenAddress,
111+
uint8 payloadId,
112+
uint256 amount,
113+
uint32 sourceCctpDomain,
114+
uint32 targetCctpDomain,
115+
uint64 cctpNonce,
116+
bytes32 burnSource,
117+
bytes32 mintRecipient,
118+
bytes memory payload
64119
) internal pure returns (bytes memory encoded) {
65120
uint256 payloadLen = payload.length;
66121
if (payloadLen == 0) {
@@ -70,7 +125,7 @@ library WormholeCctpMessages {
70125
}
71126

72127
encoded = abi.encodePacked(
73-
DEPOSIT,
128+
payloadId,
74129
universalTokenAddress,
75130
amount,
76131
sourceCctpDomain,
@@ -83,6 +138,9 @@ library WormholeCctpMessages {
83138
);
84139
}
85140

141+
/**
142+
* @dev NOTE: This method decodes the VAA payload assuming the payload ID == 1.
143+
*/
86144
function decodeDeposit(IWormhole.VM memory vaa)
87145
internal
88146
pure
@@ -106,10 +164,36 @@ library WormholeCctpMessages {
106164
burnSource,
107165
mintRecipient,
108166
payload
109-
) = decodeDeposit(vaa, true);
167+
) = decodeDeposit(vaa, DEPOSIT, true);
168+
}
169+
170+
function decodeDeposit(IWormhole.VM memory vaa, uint8 payloadId)
171+
internal
172+
pure
173+
returns (
174+
bytes32 token,
175+
uint256 amount,
176+
uint32 sourceCctpDomain,
177+
uint32 targetCctpDomain,
178+
uint64 cctpNonce,
179+
bytes32 burnSource,
180+
bytes32 mintRecipient,
181+
bytes memory payload
182+
)
183+
{
184+
(
185+
token,
186+
amount,
187+
sourceCctpDomain,
188+
targetCctpDomain,
189+
cctpNonce,
190+
burnSource,
191+
mintRecipient,
192+
payload
193+
) = decodeDeposit(vaa, payloadId, true);
110194
}
111195

112-
function decodeDeposit(IWormhole.VM memory vaa, bool revertCustomErrors)
196+
function decodeDeposit(IWormhole.VM memory vaa, uint8 payloadId, bool revertCustomErrors)
113197
internal
114198
pure
115199
returns (
@@ -124,7 +208,7 @@ library WormholeCctpMessages {
124208
)
125209
{
126210
bytes memory encoded = vaa.payload;
127-
uint256 offset = _checkPayloadId(encoded, 0, DEPOSIT, revertCustomErrors);
211+
uint256 offset = _checkPayloadId(encoded, 0, payloadId, revertCustomErrors);
128212

129213
(token, offset) = encoded.asBytes32Unchecked(offset);
130214
(amount, offset) = encoded.asUint256Unchecked(offset);

0 commit comments

Comments
 (0)