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.
131 lines
4.1 KiB
131 lines
4.1 KiB
4 years ago
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||
|
pragma solidity >=0.6.11;
|
||
|
|
||
|
import "@summa-tx/memview-sol/contracts/TypedMemView.sol";
|
||
|
|
||
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 {
|
||
|
using TypedMemView for bytes;
|
||
|
using TypedMemView for bytes29;
|
||
|
|
||
|
// Number of bytes in formatted message before `body` field
|
||
|
uint256 internal constant PREFIX_LENGTH = 76;
|
||
|
|
||
|
/**
|
||
|
* @notice Returns formatted (packed) message with provided fields
|
||
3 years ago
|
* @param _originDomain Domain of home chain
|
||
4 years ago
|
* @param _sender Address of sender as bytes32
|
||
3 years ago
|
* @param _nonce Destination-specific nonce
|
||
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 _nonce,
|
||
3 years ago
|
uint32 _destinationDomain,
|
||
4 years ago
|
bytes32 _recipient,
|
||
3 years ago
|
bytes memory _messageBody
|
||
4 years ago
|
) internal pure returns (bytes memory) {
|
||
|
return
|
||
|
abi.encodePacked(
|
||
3 years ago
|
_originDomain,
|
||
4 years ago
|
_sender,
|
||
3 years ago
|
_nonce,
|
||
3 years ago
|
_destinationDomain,
|
||
4 years ago
|
_recipient,
|
||
3 years ago
|
_messageBody
|
||
4 years ago
|
);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @notice Returns leaf of formatted message with provided fields.
|
||
4 years ago
|
* @param _origin Domain of home chain
|
||
4 years ago
|
* @param _sender Address of sender as bytes32
|
||
3 years ago
|
* @param _nonce Destination-specific nonce number
|
||
4 years ago
|
* @param _destination Domain of destination chain
|
||
|
* @param _recipient Address of recipient on destination chain as bytes32
|
||
|
* @param _body Raw bytes of message body
|
||
|
* @return Leaf (hash) of formatted message
|
||
|
**/
|
||
|
function messageHash(
|
||
4 years ago
|
uint32 _origin,
|
||
4 years ago
|
bytes32 _sender,
|
||
3 years ago
|
uint32 _nonce,
|
||
4 years ago
|
uint32 _destination,
|
||
|
bytes32 _recipient,
|
||
|
bytes memory _body
|
||
|
) internal pure returns (bytes32) {
|
||
|
return
|
||
|
keccak256(
|
||
|
formatMessage(
|
||
4 years ago
|
_origin,
|
||
4 years ago
|
_sender,
|
||
3 years ago
|
_nonce,
|
||
4 years ago
|
_destination,
|
||
|
_recipient,
|
||
|
_body
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
/// @notice Returns message's origin field
|
||
|
function origin(bytes29 _message) internal pure returns (uint32) {
|
||
|
return uint32(_message.indexUint(0, 4));
|
||
|
}
|
||
|
|
||
|
/// @notice Returns message's sender field
|
||
|
function sender(bytes29 _message) internal pure returns (bytes32) {
|
||
|
return _message.index(4, 32);
|
||
|
}
|
||
|
|
||
3 years ago
|
/// @notice Returns message's nonce field
|
||
|
function nonce(bytes29 _message) internal pure returns (uint32) {
|
||
4 years ago
|
return uint32(_message.indexUint(36, 4));
|
||
|
}
|
||
|
|
||
|
/// @notice Returns message's destination field
|
||
|
function destination(bytes29 _message) internal pure returns (uint32) {
|
||
|
return uint32(_message.indexUint(40, 4));
|
||
|
}
|
||
|
|
||
|
/// @notice Returns message's recipient field as bytes32
|
||
|
function recipient(bytes29 _message) internal pure returns (bytes32) {
|
||
|
return _message.index(44, 32);
|
||
|
}
|
||
|
|
||
|
/// @notice Returns message's recipient field as an address
|
||
|
function recipientAddress(bytes29 _message)
|
||
|
internal
|
||
|
pure
|
||
|
returns (address)
|
||
|
{
|
||
4 years ago
|
return TypeCasts.bytes32ToAddress(recipient(_message));
|
||
4 years ago
|
}
|
||
|
|
||
|
/// @notice Returns message's body field as bytes29 (refer to TypedMemView library for details on bytes29 type)
|
||
|
function body(bytes29 _message) internal pure returns (bytes29) {
|
||
|
return _message.slice(PREFIX_LENGTH, _message.len() - PREFIX_LENGTH, 0);
|
||
|
}
|
||
4 years ago
|
|
||
|
function leaf(bytes29 _message) internal view returns (bytes32) {
|
||
3 years ago
|
return
|
||
|
messageHash(
|
||
|
origin(_message),
|
||
|
sender(_message),
|
||
|
nonce(_message),
|
||
|
destination(_message),
|
||
|
recipient(_message),
|
||
|
TypedMemView.clone(body(_message))
|
||
|
);
|
||
4 years ago
|
}
|
||
4 years ago
|
}
|