// SPDX-License-Identifier: MIT pragma solidity ^0.6.12; import "../common/libs/BytesLib.sol"; /** * @title Gateway recipient * * @notice Gateway target contract * * @author Stanisław Głogowski */ contract GatewayRecipient { using BytesLib for bytes; address public gateway; /** * @dev internal constructor */ constructor() internal {} // internal functions /** * @notice Initializes `GatewayRecipient` contract * @param gateway_ `Gateway` contract address */ function _initializeGatewayRecipient( address gateway_ ) internal { gateway = gateway_; } // internal functions (views) /** * @notice Gets gateway context account * @return context account address */ function _getContextAccount() internal view returns (address) { return _getContextAddress(40); } /** * @notice Gets gateway context sender * @return context sender address */ function _getContextSender() internal view returns (address) { return _getContextAddress(20); } /** * @notice Gets gateway context data * @return context data */ function _getContextData() internal view returns (bytes calldata) { bytes calldata result; if (_isGatewaySender()) { result = msg.data[:msg.data.length - 40]; } else { result = msg.data; } return result; } // private functions (views) function _getContextAddress( uint256 offset ) private view returns (address) { address result = address(0); if (_isGatewaySender()) { uint from = msg.data.length - offset; result = bytes(msg.data[from:from + 20]).toAddress(); } else { result = msg.sender; } return result; } function _isGatewaySender() private view returns (bool) { bool result; if (msg.sender == gateway) { require( msg.data.length >= 44, "GatewayRecipient: invalid msg.data" ); result = true; } return result; } }