Created
January 7, 2018 13:18
-
-
Save kidwai/7eae0db1f47d398d89b5112cbc1356e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.18; | |
/** | |
* @title DeathWallet | |
* @dev Wallet with a dead man switch. | |
* @author Mohammad Kidwai | |
*/ | |
contract DeathWallet { | |
address public owner; // The owner of the wallet. | |
uint public last; // The latest block in which a transaction to this | |
// contract was sent from `owner`. | |
uint public period; // The number of blocks within which `owner` must | |
// send a transaction to this contract to preserve | |
// ownership. | |
address public beneficiary; // The address to which ownership may be transferred | |
// after `period` blocks of inactivity by `owner`. | |
/** @dev Initializes a DeathWallet contract. | |
* | |
* @param _owner The owner of the wallet, provided as a parameter to support multisignature contract addresses. | |
* @param _beneficiary The beneficiary address, described above. | |
* @param _period The period, described above. | |
*/ | |
function DeathWallet( | |
address _owner, | |
address _beneficiary, | |
uint _period | |
) public { | |
owner = _owner; | |
beneficiary = _beneficiary; | |
last = block.number; | |
period = _period; | |
} | |
/** | |
* @dev Returns true if `owner` has been seen in at least `period` blocks. | |
* | |
* @return _result True if 'dead' | |
*/ | |
function isDead() public constant returns (bool) { | |
return last + period < block.number; | |
} | |
/** | |
* @dev Ping this contract to update `last`. | |
* | |
*/ | |
function ping () public onlyBy(owner) { | |
require (!isDead()); // can't ping if you're a dead man! | |
last = block.number; | |
Ping(block.number + period); | |
} | |
/** @dev Claims ownership of this contract if `owner` has not been seen | |
* for at least `period` blocks. | |
* | |
* Note that blocks are used in favour of timestamps as | |
* timestamps are not guaranteed correct, despite usually | |
* being so. | |
* | |
*/ | |
function claim () onlyBy(beneficiary) public { | |
// Ensure the owner has not been seen for at least `period` blocks. | |
require (isDead()); | |
// Upate state variables. | |
owner = beneficiary; | |
last = block.number; | |
Ping(block.number + period); | |
} | |
/** | |
* @dev Withdraw funds to the specified address. | |
* | |
* @param _to The target address. | |
* @param _amount The quantity to withdraw. | |
* | |
*/ | |
function withdraw ( | |
address _to, | |
uint _amount | |
) public onlyBy(owner) | |
{ | |
require (!isDead()); | |
// Attempt transfer | |
_to.transfer(_amount); | |
last = block.number; | |
Ping(block.number + period); | |
Withdrawal(_to, _amount); | |
} | |
/** | |
* @dev Set the period. | |
* | |
* @param _period The new period. | |
* | |
*/ | |
function setPeriod( | |
uint _period | |
) public onlyBy(owner) | |
{ | |
period = _period; | |
last = block.number; | |
Ping(block.number + period); | |
} | |
/** | |
* @dev Set the beneficiary address. | |
* | |
* @param _beneficiary The new beneficiary. | |
*/ | |
function setBeneficiary( | |
address _beneficiary | |
) public onlyBy(owner) { | |
beneficiary = _beneficiary; | |
last = block.number; | |
Ping(block.number + period); | |
} | |
/** | |
* @dev Fallback function allows deposits. | |
*/ | |
function () payable public { | |
Deposit(msg.sender, msg.value); | |
} | |
/** | |
* @dev Check that the sender is the specified address. | |
* | |
* @param _addr The address that must be matched. | |
*/ | |
modifier onlyBy (address _addr) { | |
require (msg.sender == _addr); | |
_; | |
} | |
event Deposit (address indexed _from, uint _amount); | |
event Withdrawal(address indexed _to, uint _amount); | |
event Ping(uint _blocknum); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment