@ -1,7 +1,5 @@
/ / SPDX - License - Identifier : MIT OR Apache - 2 . 0
pragma solidity >= 0 . 6 . 11 ;
import " @summa-tx/memview-sol/contracts/TypedMemView.sol " ;
pragma solidity >= 0 . 8 . 0 ;
import { TypeCasts } from " ./TypeCasts.sol " ;
@ -11,14 +9,11 @@ import {TypeCasts} from "./TypeCasts.sol";
* @ notice Library for formatted messages used by Outbox and Replica .
** /
library Message {
using TypedMemView for bytes ;
using TypedMemView for bytes29 ;
/ / Number of bytes in formatted message before ` body ` field
uint256 internal constant PREFIX_LENGTH = 72 ;
using TypeCasts for bytes32 ;
/**
* @ notice Returns formatted ( packed ) message with provided fields
* @ dev This function should only be used in memory message construction .
* @ param _originDomain Domain of home chain
* @ param _sender Address of sender as bytes32
* @ param _destinationDomain Domain of destination chain
@ -31,7 +26,7 @@ library Message {
bytes32 _sender ,
uint32 _destinationDomain ,
bytes32 _recipient ,
bytes memory _messageBody
bytes calldata _messageBody
) internal pure returns ( bytes memory ) {
return
abi . encodePacked (
@ -45,84 +40,83 @@ library Message {
/**
* @ notice Returns leaf of formatted message with provided fields .
* @ param _origin Domain of home chain
* @ param _sender Address of sender as bytes32
* @ param _destination Domain of destination chain
* @ param _recipient Address of recipient on destination chain as bytes32
* @ param _body Raw bytes of message body
* @ dev hash of abi packed message and leaf index .
* @ param _message Raw bytes of message contents .
* @ param _leafIndex Index of the message in the tree
* @ return Leaf ( hash ) of formatted message
** /
function messageHash (
uint32 _origin ,
bytes32 _sender ,
uint32 _destination ,
bytes32 _recipient ,
bytes memory _body ,
uint256 _leafIndex
) internal pure returns ( bytes32 ) {
return
keccak256 (
abi . encodePacked (
formatMessage (
_origin ,
_sender ,
_destination ,
_recipient ,
_body
) ,
_leafIndex
)
) ;
}
/ / / @ 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 ) ;
}
/ / / @ notice Returns message ' s destination field
function destination ( bytes29 _message ) internal pure returns ( uint32 ) {
return uint32 ( _message . indexUint ( 36 , 4 ) ) ;
}
/ / / @ notice Returns message ' s recipient field as bytes32
function recipient ( bytes29 _message ) internal pure returns ( bytes32 ) {
return _message . index ( 40 , 32 ) ;
}
/ / / @ notice Returns message ' s recipient field as an address
function recipientAddress ( bytes29 _message )
* /
function leaf ( bytes calldata _message , uint256 _leafIndex )
internal
pure
returns ( address )
returns ( bytes32 )
{
return TypeCasts . bytes32ToAddress ( recipient ( _message ) ) ;
return keccak256 ( abi . encodePacked ( _message , _leafIndex ) ) ;
}
/ / / @ 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 ) ;
/**
* @ 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 : ] )
) ;
}
function leaf ( bytes29 _message , uint256 _leafIndex )
/**
* @ 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 )
internal
view
returns ( bytes32 )
pure
returns (
uint32 ,
address ,
uint32 ,
address ,
bytes calldata
)
{
return
messageHash (
origin ( _message ) ,
sender ( _message ) ,
destination ( _message ) ,
recipient ( _message ) ,
TypedMemView . clone ( body ( _message ) ) ,
_leafIndex
) ;
(
uint32 _origin ,
bytes32 _sender ,
uint32 destination ,
bytes32 _recipient ,
bytes calldata body
) = destructure ( _message ) ;
return (
_origin ,
_sender . bytes32ToAddress ( ) ,
destination ,
_recipient . bytes32ToAddress ( ) ,
body
) ;
}
}