Merge pull request #170 from Recmo/feature/no-owner
Prevent contracts from owning Ether, Tokens and other contracts.
This commit is contained in:
18
contracts/ownership/HasNoContracts.sol
Normal file
18
contracts/ownership/HasNoContracts.sol
Normal file
@ -0,0 +1,18 @@
|
||||
pragma solidity ^0.4.8;
|
||||
|
||||
import "./Ownable.sol";
|
||||
|
||||
/// @title Contracts that should not own Contracts
|
||||
/// @author Remco Bloemen <remco@2π.com>
|
||||
///
|
||||
/// Should contracts (anything Ownable) end up being owned by
|
||||
/// this contract, it allows the owner of this contract to
|
||||
/// reclaim ownership of the contracts.
|
||||
contract HasNoContracts is Ownable {
|
||||
|
||||
/// Reclaim ownership of Ownable contracts
|
||||
function reclaimContract(address contractAddr) external onlyOwner {
|
||||
Ownable contractInst = Ownable(contractAddr);
|
||||
contractInst.transferOwnership(owner);
|
||||
}
|
||||
}
|
||||
42
contracts/ownership/HasNoEther.sol
Normal file
42
contracts/ownership/HasNoEther.sol
Normal file
@ -0,0 +1,42 @@
|
||||
pragma solidity ^0.4.8;
|
||||
|
||||
import "./Ownable.sol";
|
||||
|
||||
/// @title Contracts that should not own Ether
|
||||
/// @author Remco Bloemen <remco@2π.com>
|
||||
///
|
||||
/// This tries to block incoming ether to prevent accidental
|
||||
/// loss of Ether. Should Ether end up in the contrat, it will
|
||||
/// allow the owner to reclaim this ether.
|
||||
///
|
||||
/// @notice Ether can still be send to this contract by:
|
||||
/// * calling functions labeled `payable`
|
||||
/// * `selfdestruct(contract_address)`
|
||||
/// * mining directly to the contract address
|
||||
contract HasNoEther is Ownable {
|
||||
|
||||
/// Constructor that rejects incoming Ether
|
||||
/// @dev The flag `payable` is added so we can access `msg.value`
|
||||
/// without compiler warning. If we leave out payable, then
|
||||
/// Solidity will allow inheriting contracts to implement a
|
||||
/// payable constructor. By doing it this way we prevent a
|
||||
/// payable constructor from working.
|
||||
/// Alternatively we could use assembly to access msg.value.
|
||||
function HasNoEther() payable {
|
||||
if(msg.value > 0) {
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// Disallow direct send by settings a default function without `payable`
|
||||
function() external {
|
||||
}
|
||||
|
||||
/// Transfer all Ether owned by the contract to the owner
|
||||
/// @dev What if owner is itself a contract marked HasNoEther?
|
||||
function reclaimEther() external onlyOwner {
|
||||
if(!owner.send(this.balance)) {
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
26
contracts/ownership/HasNoTokens.sol
Normal file
26
contracts/ownership/HasNoTokens.sol
Normal file
@ -0,0 +1,26 @@
|
||||
pragma solidity ^0.4.8;
|
||||
|
||||
import "./Ownable.sol";
|
||||
import "../token/ERC20Basic.sol";
|
||||
|
||||
/// @title Contracts that should not own Tokens
|
||||
/// @author Remco Bloemen <remco@2π.com>
|
||||
///
|
||||
/// This blocks incoming ERC23 tokens to prevent accidental
|
||||
/// loss of tokens. Should tokens (any ERC20Basic compatible)
|
||||
/// end up in the contract, it allows the owner to reclaim
|
||||
/// the tokens.
|
||||
contract HasNoTokens is Ownable {
|
||||
|
||||
/// Reject all ERC23 compatible tokens
|
||||
function tokenFallback(address from_, uint value_, bytes data_) external {
|
||||
throw;
|
||||
}
|
||||
|
||||
/// Reclaim all ERC20Basic compatible tokens
|
||||
function reclaimToken(address tokenAddr) external onlyOwner {
|
||||
ERC20Basic tokenInst = ERC20Basic(tokenAddr);
|
||||
uint256 balance = tokenInst.balanceOf(this);
|
||||
tokenInst.transfer(owner, balance);
|
||||
}
|
||||
}
|
||||
14
contracts/ownership/NoOwner.sol
Normal file
14
contracts/ownership/NoOwner.sol
Normal file
@ -0,0 +1,14 @@
|
||||
pragma solidity ^0.4.8;
|
||||
|
||||
import "./HasNoEther.sol";
|
||||
import "./HasNoTokens.sol";
|
||||
import "./HasNoContracts.sol";
|
||||
|
||||
/// @title Base contract for contracts that should not own things.
|
||||
/// @author Remco Bloemen <remco@2π.com>
|
||||
///
|
||||
/// Solves a class of errors where a contract accidentally
|
||||
/// becomes owner of Ether, Tokens or Owned contracts. See
|
||||
/// respective base contracts for details.
|
||||
contract NoOwner is HasNoEther, HasNoTokens, HasNoContracts {
|
||||
}
|
||||
Reference in New Issue
Block a user