Consider two contracts: A calls B. What will be the output of msg.sender and tx.origin inside contract B when A calls B?
contract B {
function check() public view returns (address, address) {
return (msg.sender, tx.origin);
}
}
contract A {
B b;
constructor(address _b) {
b = B(_b);
}
function callB() public view returns (address, address) {
return b.check();
}
}Remember that msg.sender is the immediate caller, while tx.origin is the original external account that started the transaction.
Inside contract B, msg.sender is the address of contract A because A called B. The tx.origin remains the original external account that initiated the transaction, not the intermediate contract.
What will be the output of the following Solidity function if called by an external user directly?
function isOwner() public view returns (bool) {
return tx.origin == owner;
}address owner = 0x1234567890123456789012345678901234567890; function isOwner() public view returns (bool) { return tx.origin == owner; }
tx.origin is the original external account that started the transaction.
The function compares tx.origin (the external account that started the transaction) to the stored owner address. If they match, it returns true.
Consider this contract snippet:
function withdraw() public {
require(tx.origin == owner, "Not owner");
payable(msg.sender).transfer(address(this).balance);
}What is the security risk here?
Think about how tx.origin can be manipulated by intermediate contracts.
Using tx.origin for authorization is dangerous because an attacker can create a malicious contract that the owner calls. The tx.origin remains the owner, so the check passes, but the attacker controls the call flow, allowing unauthorized withdrawals.
Which option correctly fixes the syntax error in this function?
function checkOrigin() public view returns (bool) {
if tx.origin == owner {
return true;
} else {
return false;
}
}Check the syntax for if statements in Solidity.
In Solidity, the condition in an if statement must be enclosed in parentheses. The original code misses these parentheses, causing a syntax error.
You want to restrict a function so only the contract owner can call it. Which approach is the most secure?
Consider how intermediate contracts affect msg.sender and tx.origin.
Using msg.sender ensures the immediate caller is the owner, preventing phishing attacks that exploit tx.origin. Checking tx.origin is unsafe because it can be manipulated by malicious contracts.