OpenZeppelin에서 msg.sender를 사용하지 않는 이유

2022. 4. 1. 14:45개발 잡부/블록체인

728x90

개요

OpenZeppelin을 사용하다보면 (예를 들면 Ownable)
msg.sender대신 _msgSender()를 사용하는 경우가 있다.

import "./Context.sol";

...

modifier onlyOwner() {
    require(owner() == _msgSender(), "Ownable: caller is not the owner");
    _;
}

Context.sol

 

GitHub - OpenZeppelin/openzeppelin-contracts: OpenZeppelin Contracts is a library for secure smart contract development.

OpenZeppelin Contracts is a library for secure smart contract development. - GitHub - OpenZeppelin/openzeppelin-contracts: OpenZeppelin Contracts is a library for secure smart contract development.

github.com

이 코드에서는 msg.sender_msgSender로, msg.data_msgData로 사용할 수 있게 한다.

주석을 보면 그 이유를 알 수 있는데,

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.

meta-transaction을 처리 할 때 결제하는 계좌가 실제 보낸 계좌가 아닐 수 있다고 한다.

 

Sending gasless transactions - OpenZeppelin Docs

All Ethereum transactions use gas, and the sender of each transaction must have enough Ether to pay for the gas spent. Even though these gas costs are low for basic transactions (a couple of cents), getting Ether is no easy task: dApp users often need to g

docs.openzeppelin.com

메타 트랜젝션이란 제 3자를 끼고 하는 거래를 뜻한다고 한다...
나중에 좀 더 정확히 파악해 봐야겠다