|
| 1 | +/* |
| 2 | + * Copyright 2024 Circle Internet Group, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +pragma solidity 0.7.6; |
| 19 | + |
| 20 | +import {TypedMemView} from "@memview-sol/contracts/TypedMemView.sol"; |
| 21 | + |
| 22 | +/** |
| 23 | + * @title MessageV2 Library |
| 24 | + * @notice Library for formatted v2 messages used by Relayer and Receiver. |
| 25 | + * |
| 26 | + * @dev The message body is dynamically-sized to support custom message body |
| 27 | + * formats. Other fields must be fixed-size to avoid hash collisions. |
| 28 | + * Each other input value has an explicit type to guarantee fixed-size. |
| 29 | + * Padding: uintNN fields are left-padded, and bytesNN fields are right-padded. |
| 30 | + * |
| 31 | + * Field Bytes Type Index |
| 32 | + * version 4 uint32 0 |
| 33 | + * sourceDomain 4 uint32 4 |
| 34 | + * destinationDomain 4 uint32 8 |
| 35 | + * nonce 32 bytes32 12 |
| 36 | + * sender 32 bytes32 44 |
| 37 | + * recipient 32 bytes32 76 |
| 38 | + * destinationCaller 32 bytes32 108 |
| 39 | + * minFinalityThreshold 4 uint32 140 |
| 40 | + * finalityThresholdExecuted 4 uint32 144 |
| 41 | + * messageBody dynamic bytes 148 |
| 42 | + * @dev Differences from v1: |
| 43 | + * - Nonce is now bytes32 (vs. uint64) |
| 44 | + * - minFinalityThreshold added |
| 45 | + * - finalityThresholdExecuted added |
| 46 | + **/ |
| 47 | +library MessageV2 { |
| 48 | + using TypedMemView for bytes; |
| 49 | + using TypedMemView for bytes29; |
| 50 | + |
| 51 | + // Indices of each field in message |
| 52 | + uint8 private constant VERSION_INDEX = 0; |
| 53 | + uint8 private constant SOURCE_DOMAIN_INDEX = 4; |
| 54 | + uint8 private constant DESTINATION_DOMAIN_INDEX = 8; |
| 55 | + uint8 private constant NONCE_INDEX = 12; |
| 56 | + uint8 private constant SENDER_INDEX = 44; |
| 57 | + uint8 private constant RECIPIENT_INDEX = 76; |
| 58 | + uint8 private constant DESTINATION_CALLER_INDEX = 108; |
| 59 | + uint8 private constant MIN_FINALITY_THRESHOLD_INDEX = 140; |
| 60 | + uint8 private constant FINALITY_THRESHOLD_EXECUTED_INDEX = 144; |
| 61 | + uint8 private constant MESSAGE_BODY_INDEX = 148; |
| 62 | + |
| 63 | + bytes32 private constant EMPTY_NONCE = bytes32(0); |
| 64 | + uint32 private constant EMPTY_FINALITY_THRESHOLD_EXECUTED = 0; |
| 65 | + |
| 66 | + /** |
| 67 | + * @notice Returns formatted (packed) message with provided fields |
| 68 | + * @param _version the version of the message format |
| 69 | + * @param _sourceDomain Domain of home chain |
| 70 | + * @param _destinationDomain Domain of destination chain |
| 71 | + * @param _sender Address of sender on source chain as bytes32 |
| 72 | + * @param _recipient Address of recipient on destination chain as bytes32 |
| 73 | + * @param _destinationCaller Address of caller on destination chain as bytes32 |
| 74 | + * @param _minFinalityThreshold the minimum finality at which the message should be attested to |
| 75 | + * @param _messageBody Raw bytes of message body |
| 76 | + * @return Formatted message |
| 77 | + **/ |
| 78 | + function _formatMessageForRelay( |
| 79 | + uint32 _version, |
| 80 | + uint32 _sourceDomain, |
| 81 | + uint32 _destinationDomain, |
| 82 | + bytes32 _sender, |
| 83 | + bytes32 _recipient, |
| 84 | + bytes32 _destinationCaller, |
| 85 | + uint32 _minFinalityThreshold, |
| 86 | + bytes memory _messageBody |
| 87 | + ) internal pure returns (bytes memory) { |
| 88 | + return |
| 89 | + abi.encodePacked( |
| 90 | + _version, |
| 91 | + _sourceDomain, |
| 92 | + _destinationDomain, |
| 93 | + EMPTY_NONCE, |
| 94 | + _sender, |
| 95 | + _recipient, |
| 96 | + _destinationCaller, |
| 97 | + _minFinalityThreshold, |
| 98 | + EMPTY_FINALITY_THRESHOLD_EXECUTED, |
| 99 | + _messageBody |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + // @notice Returns _message's version field |
| 104 | + function _getVersion(bytes29 _message) internal pure returns (uint32) { |
| 105 | + return uint32(_message.indexUint(VERSION_INDEX, 4)); |
| 106 | + } |
| 107 | + |
| 108 | + // @notice Returns _message's sourceDomain field |
| 109 | + function _getSourceDomain(bytes29 _message) internal pure returns (uint32) { |
| 110 | + return uint32(_message.indexUint(SOURCE_DOMAIN_INDEX, 4)); |
| 111 | + } |
| 112 | + |
| 113 | + // @notice Returns _message's destinationDomain field |
| 114 | + function _getDestinationDomain( |
| 115 | + bytes29 _message |
| 116 | + ) internal pure returns (uint32) { |
| 117 | + return uint32(_message.indexUint(DESTINATION_DOMAIN_INDEX, 4)); |
| 118 | + } |
| 119 | + |
| 120 | + // @notice Returns _message's nonce field |
| 121 | + function _getNonce(bytes29 _message) internal pure returns (bytes32) { |
| 122 | + return _message.index(NONCE_INDEX, 32); |
| 123 | + } |
| 124 | + |
| 125 | + // @notice Returns _message's sender field |
| 126 | + function _getSender(bytes29 _message) internal pure returns (bytes32) { |
| 127 | + return _message.index(SENDER_INDEX, 32); |
| 128 | + } |
| 129 | + |
| 130 | + // @notice Returns _message's recipient field |
| 131 | + function _getRecipient(bytes29 _message) internal pure returns (bytes32) { |
| 132 | + return _message.index(RECIPIENT_INDEX, 32); |
| 133 | + } |
| 134 | + |
| 135 | + // @notice Returns _message's destinationCaller field |
| 136 | + function _getDestinationCaller( |
| 137 | + bytes29 _message |
| 138 | + ) internal pure returns (bytes32) { |
| 139 | + return _message.index(DESTINATION_CALLER_INDEX, 32); |
| 140 | + } |
| 141 | + |
| 142 | + // @notice Returns _message's minFinalityThreshold field |
| 143 | + function _getMinFinalityThreshold( |
| 144 | + bytes29 _message |
| 145 | + ) internal pure returns (uint32) { |
| 146 | + return uint32(_message.indexUint(MIN_FINALITY_THRESHOLD_INDEX, 4)); |
| 147 | + } |
| 148 | + |
| 149 | + // @notice Returns _message's finalityThresholdExecuted field |
| 150 | + function _getFinalityThresholdExecuted( |
| 151 | + bytes29 _message |
| 152 | + ) internal pure returns (uint32) { |
| 153 | + return uint32(_message.indexUint(FINALITY_THRESHOLD_EXECUTED_INDEX, 4)); |
| 154 | + } |
| 155 | + |
| 156 | + // @notice Returns _message's messageBody field |
| 157 | + function _getMessageBody(bytes29 _message) internal pure returns (bytes29) { |
| 158 | + return |
| 159 | + _message.slice( |
| 160 | + MESSAGE_BODY_INDEX, |
| 161 | + _message.len() - MESSAGE_BODY_INDEX, |
| 162 | + 0 |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * @notice Reverts if message is malformed or too short |
| 168 | + * @param _message The message as bytes29 |
| 169 | + */ |
| 170 | + function _validateMessageFormat(bytes29 _message) internal pure { |
| 171 | + require(_message.isValid(), "Malformed message"); |
| 172 | + require( |
| 173 | + _message.len() >= MESSAGE_BODY_INDEX, |
| 174 | + "Invalid message: too short" |
| 175 | + ); |
| 176 | + } |
| 177 | +} |
0 commit comments