|
| 1 | +// SPDX-License-Identifier: Apache 2 |
| 2 | +pragma solidity ^0.8.19; |
| 3 | + |
| 4 | +import {BytesParsing} from "wormhole-sdk/libraries/BytesParsing.sol"; |
| 5 | + |
| 6 | +// ╭───────────────────────────────────────────────────────────────────────╮ |
| 7 | +// │ Library for encoding and decoding WormholeCctpTokenMessenger messages │ |
| 8 | +// ╰───────────────────────────────────────────────────────────────────────╯ |
| 9 | + |
| 10 | +library WormholeCctpMessageLib { |
| 11 | + using BytesParsing for bytes; |
| 12 | + using {BytesParsing.checkLength} for uint; |
| 13 | + |
| 14 | + uint8 internal constant PAYLOAD_ID_DEPOSIT = 1; |
| 15 | + |
| 16 | + // uint private constant _DEPOSIT_META_SIZE = |
| 17 | + // 32 /*universalTokenAddress*/ + |
| 18 | + // 32 /*amount*/ + |
| 19 | + // 4 /*sourceCctpDomain*/ + |
| 20 | + // 4 /*targetCctpDomain*/ + |
| 21 | + // 8 /*cctpNonce*/ + |
| 22 | + // 32 /*burnSource*/ + |
| 23 | + // 32 /*mintRecipient*/; |
| 24 | + |
| 25 | + error PayloadTooLarge(uint256); |
| 26 | + error InvalidPayloadId(uint8); |
| 27 | + |
| 28 | + function encodeDeposit( |
| 29 | + bytes32 universalTokenAddress, |
| 30 | + uint256 amount, |
| 31 | + uint32 sourceCctpDomain, |
| 32 | + uint32 targetCctpDomain, |
| 33 | + uint64 cctpNonce, |
| 34 | + bytes32 burnSource, |
| 35 | + bytes32 mintRecipient, |
| 36 | + bytes memory payload |
| 37 | + ) internal pure returns (bytes memory) { |
| 38 | + uint payloadLen = payload.length; |
| 39 | + if (payloadLen > type(uint16).max) |
| 40 | + revert PayloadTooLarge(payloadLen); |
| 41 | + |
| 42 | + return abi.encodePacked( |
| 43 | + PAYLOAD_ID_DEPOSIT, |
| 44 | + universalTokenAddress, |
| 45 | + amount, |
| 46 | + sourceCctpDomain, |
| 47 | + targetCctpDomain, |
| 48 | + cctpNonce, |
| 49 | + burnSource, |
| 50 | + mintRecipient, |
| 51 | + uint16(payloadLen), |
| 52 | + payload |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + function decodeDepositCd(bytes calldata vaaPayload) internal pure returns ( |
| 57 | + bytes32 token, |
| 58 | + uint256 amount, |
| 59 | + uint32 sourceCctpDomain, |
| 60 | + uint32 targetCctpDomain, |
| 61 | + uint64 cctpNonce, |
| 62 | + bytes32 burnSource, |
| 63 | + bytes32 mintRecipient, |
| 64 | + bytes calldata payload |
| 65 | + ) { |
| 66 | + uint offset = 0; |
| 67 | + ( token, |
| 68 | + amount, |
| 69 | + sourceCctpDomain, |
| 70 | + targetCctpDomain, |
| 71 | + cctpNonce, |
| 72 | + burnSource, |
| 73 | + mintRecipient, |
| 74 | + offset |
| 75 | + ) = decodeDepositHeaderCdUnchecked(vaaPayload, offset); |
| 76 | + |
| 77 | + (payload, offset) = decodeDepositPayloadCdUnchecked(vaaPayload, offset); |
| 78 | + vaaPayload.length.checkLength(offset); |
| 79 | + } |
| 80 | + |
| 81 | + function decodeDepositHeaderCdUnchecked( |
| 82 | + bytes calldata encoded, |
| 83 | + uint offset |
| 84 | + ) internal pure returns ( |
| 85 | + bytes32 token, |
| 86 | + uint256 amount, |
| 87 | + uint32 sourceCctpDomain, |
| 88 | + uint32 targetCctpDomain, |
| 89 | + uint64 cctpNonce, |
| 90 | + bytes32 burnSource, |
| 91 | + bytes32 mintRecipient, |
| 92 | + uint payloadOffset |
| 93 | + ) { |
| 94 | + uint8 payloadId; |
| 95 | + (payloadId, offset) = encoded.asUint8CdUnchecked(offset); |
| 96 | + checkPayloadId(payloadId, PAYLOAD_ID_DEPOSIT); |
| 97 | + (token, offset) = encoded.asBytes32CdUnchecked(offset); |
| 98 | + (amount, offset) = encoded.asUint256CdUnchecked(offset); |
| 99 | + (sourceCctpDomain, offset) = encoded.asUint32CdUnchecked(offset); |
| 100 | + (targetCctpDomain, offset) = encoded.asUint32CdUnchecked(offset); |
| 101 | + (cctpNonce, offset) = encoded.asUint64CdUnchecked(offset); |
| 102 | + (burnSource, offset) = encoded.asBytes32CdUnchecked(offset); |
| 103 | + (mintRecipient, offset) = encoded.asBytes32CdUnchecked(offset); |
| 104 | + payloadOffset = offset; |
| 105 | + } |
| 106 | + |
| 107 | + function decodeDepositPayloadCdUnchecked( |
| 108 | + bytes calldata encoded, |
| 109 | + uint offset |
| 110 | + ) internal pure returns (bytes calldata payload, uint newOffset) { |
| 111 | + (payload, newOffset) = encoded.sliceUint16PrefixedCdUnchecked(offset); |
| 112 | + } |
| 113 | + |
| 114 | + function decodeDepositMem(bytes memory vaaPayload) internal pure returns ( |
| 115 | + bytes32 token, |
| 116 | + uint256 amount, |
| 117 | + uint32 sourceCctpDomain, |
| 118 | + uint32 targetCctpDomain, |
| 119 | + uint64 cctpNonce, |
| 120 | + bytes32 burnSource, |
| 121 | + bytes32 mintRecipient, |
| 122 | + bytes memory payload |
| 123 | + ) { |
| 124 | + uint offset = 0; |
| 125 | + ( token, |
| 126 | + amount, |
| 127 | + sourceCctpDomain, |
| 128 | + targetCctpDomain, |
| 129 | + cctpNonce, |
| 130 | + burnSource, |
| 131 | + mintRecipient, |
| 132 | + offset |
| 133 | + ) = decodeDepositHeaderMemUnchecked(vaaPayload, 0); |
| 134 | + |
| 135 | + (payload, offset) = decodeDepositPayloadMemUnchecked(vaaPayload, offset); |
| 136 | + vaaPayload.length.checkLength(offset); |
| 137 | + } |
| 138 | + |
| 139 | + function decodeDepositHeaderMemUnchecked( |
| 140 | + bytes memory encoded, |
| 141 | + uint offset |
| 142 | + ) internal pure returns ( |
| 143 | + bytes32 token, |
| 144 | + uint256 amount, |
| 145 | + uint32 sourceCctpDomain, |
| 146 | + uint32 targetCctpDomain, |
| 147 | + uint64 cctpNonce, |
| 148 | + bytes32 burnSource, |
| 149 | + bytes32 mintRecipient, |
| 150 | + uint payloadOffset |
| 151 | + ) { |
| 152 | + uint8 payloadId; |
| 153 | + (payloadId, offset) = encoded.asUint8MemUnchecked(offset); |
| 154 | + checkPayloadId(payloadId, PAYLOAD_ID_DEPOSIT); |
| 155 | + (token, offset) = encoded.asBytes32MemUnchecked(offset); |
| 156 | + (amount, offset) = encoded.asUint256MemUnchecked(offset); |
| 157 | + (sourceCctpDomain, offset) = encoded.asUint32MemUnchecked(offset); |
| 158 | + (targetCctpDomain, offset) = encoded.asUint32MemUnchecked(offset); |
| 159 | + (cctpNonce, offset) = encoded.asUint64MemUnchecked(offset); |
| 160 | + (burnSource, offset) = encoded.asBytes32MemUnchecked(offset); |
| 161 | + (mintRecipient, offset) = encoded.asBytes32MemUnchecked(offset); |
| 162 | + payloadOffset = offset; |
| 163 | + } |
| 164 | + |
| 165 | + function decodeDepositPayloadMemUnchecked( |
| 166 | + bytes memory encoded, |
| 167 | + uint offset |
| 168 | + ) internal pure returns (bytes memory payload, uint newOffset) { |
| 169 | + (payload, newOffset) = encoded.sliceUint16PrefixedMemUnchecked(offset); |
| 170 | + } |
| 171 | + |
| 172 | + function checkPayloadId(uint8 encoded, uint8 expected) internal pure { |
| 173 | + if (encoded != expected) |
| 174 | + revert InvalidPayloadId(encoded); |
| 175 | + } |
| 176 | +} |
0 commit comments