You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
3.7 KiB
123 lines
3.7 KiB
4 years ago
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||
3 years ago
|
pragma solidity >=0.8.0;
|
||
4 years ago
|
|
||
3 years ago
|
import {TypeCasts} from "./TypeCasts.sol";
|
||
4 years ago
|
|
||
4 years ago
|
/**
|
||
|
* @title Message Library
|
||
|
* @author Celo Labs Inc.
|
||
3 years ago
|
* @notice Library for formatted messages used by Outbox and Replica.
|
||
4 years ago
|
**/
|
||
|
library Message {
|
||
3 years ago
|
using TypeCasts for bytes32;
|
||
4 years ago
|
|
||
|
/**
|
||
|
* @notice Returns formatted (packed) message with provided fields
|
||
3 years ago
|
* @dev This function should only be used in memory message construction.
|
||
3 years ago
|
* @param _originDomain Domain of home chain
|
||
4 years ago
|
* @param _sender Address of sender as bytes32
|
||
3 years ago
|
* @param _destinationDomain Domain of destination chain
|
||
4 years ago
|
* @param _recipient Address of recipient on destination chain as bytes32
|
||
3 years ago
|
* @param _messageBody Raw bytes of message body
|
||
4 years ago
|
* @return Formatted message
|
||
|
**/
|
||
|
function formatMessage(
|
||
3 years ago
|
uint32 _originDomain,
|
||
4 years ago
|
bytes32 _sender,
|
||
3 years ago
|
uint32 _destinationDomain,
|
||
4 years ago
|
bytes32 _recipient,
|
||
3 years ago
|
bytes calldata _messageBody
|
||
4 years ago
|
) internal pure returns (bytes memory) {
|
||
|
return
|
||
|
abi.encodePacked(
|
||
3 years ago
|
_originDomain,
|
||
4 years ago
|
_sender,
|
||
3 years ago
|
_destinationDomain,
|
||
4 years ago
|
_recipient,
|
||
3 years ago
|
_messageBody
|
||
4 years ago
|
);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @notice Returns leaf of formatted message with provided fields.
|
||
3 years ago
|
* @dev hash of abi packed message and leaf index.
|
||
|
* @param _message Raw bytes of message contents.
|
||
3 years ago
|
* @param _leafIndex Index of the message in the tree
|
||
4 years ago
|
* @return Leaf (hash) of formatted message
|
||
3 years ago
|
*/
|
||
|
function leaf(bytes calldata _message, uint256 _leafIndex)
|
||
4 years ago
|
internal
|
||
|
pure
|
||
3 years ago
|
returns (bytes32)
|
||
4 years ago
|
{
|
||
3 years ago
|
return keccak256(abi.encodePacked(_message, _leafIndex));
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* @notice Decode raw message bytes into structured message fields.
|
||
|
* @dev Efficiently slices calldata into structured message fields.
|
||
|
* @param _message Raw bytes of message contents.
|
||
|
* @return origin Domain of home chain
|
||
|
* @return sender Address of sender as bytes32
|
||
|
* @return destination Domain of destination chain
|
||
|
* @return recipient Address of recipient on destination chain as bytes32
|
||
|
* @return body Raw bytes of message body
|
||
|
*/
|
||
|
function destructure(bytes calldata _message)
|
||
|
internal
|
||
|
pure
|
||
|
returns (
|
||
|
uint32 origin,
|
||
|
bytes32 sender,
|
||
|
uint32 destination,
|
||
|
bytes32 recipient,
|
||
|
bytes calldata body
|
||
|
)
|
||
|
{
|
||
|
return (
|
||
|
uint32(bytes4(_message[0:4])),
|
||
|
bytes32(_message[4:36]),
|
||
|
uint32(bytes4(_message[36:40])),
|
||
|
bytes32(_message[40:72]),
|
||
|
bytes(_message[72:])
|
||
|
);
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
3 years ago
|
/**
|
||
|
* @notice Decode raw message bytes into structured message fields.
|
||
|
* @dev Efficiently slices calldata into structured message fields.
|
||
|
* @param _message Raw bytes of message contents.
|
||
|
* @return origin Domain of home chain
|
||
|
* @return sender Address of sender as address (bytes20)
|
||
|
* @return destination Domain of destination chain
|
||
|
* @return recipient Address of recipient on destination chain as address (bytes20)
|
||
|
* @return body Raw bytes of message body
|
||
|
*/
|
||
|
function destructureAddresses(bytes calldata _message)
|
||
3 years ago
|
internal
|
||
3 years ago
|
pure
|
||
|
returns (
|
||
|
uint32,
|
||
|
address,
|
||
|
uint32,
|
||
|
address,
|
||
|
bytes calldata
|
||
|
)
|
||
3 years ago
|
{
|
||
3 years ago
|
(
|
||
|
uint32 _origin,
|
||
|
bytes32 _sender,
|
||
|
uint32 destination,
|
||
|
bytes32 _recipient,
|
||
|
bytes calldata body
|
||
|
) = destructure(_message);
|
||
|
return (
|
||
|
_origin,
|
||
|
_sender.bytes32ToAddress(),
|
||
|
destination,
|
||
|
_recipient.bytes32ToAddress(),
|
||
|
body
|
||
|
);
|
||
4 years ago
|
}
|
||
4 years ago
|
}
|