Msg.sender
1 min read
Pronunciation
[mess-ayj sen-der]
Analogy
`msg.sender` is like the return address on an envelope—it tells you who sent the message.
Definition
Key Points Intro
`msg.sender` provides caller context through:
Key Points
Immediate caller: Could be EOA or contract.
Used for access control: e.g., `require(msg.sender == owner)`.
Immutable per call: Fixed for the transaction frame.
Different from `tx.origin`: Safer for authorization.
Example
```
function withdraw() external {
require(msg.sender == owner, "Not owner");
payable(owner).transfer(address(this).balance);
}
```
Technical Deep Dive
Security Warning
Avoid using `tx.origin` for auth checks; always use `msg.sender` to prevent phishing via intermediary contracts.
Caveat
`msg.sender` changes in internal calls; design auth logic accordingly.
Msg.sender - Related Articles
No related articles for this term.