Help understanding contract Context

Hi! I’m new learning solidity, I noticie a common contract call Context in lot of tokens. Here it is:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

/*
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}

I don’t understand how exactly this contract make a token more secure. When I call a function to one contract A, this contract may call another function in other contract B, so msg.sender will be this contract. But in this case, msg.sender = _msgSender() = contractA_address

So how this exactly provide more security? I don’t get, maybe if someone could give me an example I may understand it

2 Likes

This doesn’t by itself provide any more security.

When using a meta-transaction solution such as EIP-2771, msg.sender will be a forwarder account, and the “real” message sender has to be retrieved through other means:

Using Context and _msgSender allows you to correctly retrieve the “real” message sender in this context.

You don’t need to worry about it if you’re not planning to use meta transactions like this.

7 Likes

could you explain, how is it differ from msg.sender ?

You can use a variant of Context such as ERC2771Context that implements _msgSender with different logic: