Inheritable.sol 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. pragma solidity ^0.4.11;
  2. import './Ownable.sol';
  3. /**
  4. * @title Inheritable
  5. * @dev The Inheritable contract provides ownership transfer capabilities, in the
  6. * case that the current owner stops "heartbeating". Only the heir can pronounce the
  7. * owner's death.
  8. */
  9. contract Inheritable2 is Ownable {
  10. address public heir;
  11. // Time window the owner has to notify she is alive.
  12. uint public heartbeatTimeout;
  13. // Timestamp of the owner's death, as pronounced by the heir.
  14. uint public timeOfDeath;
  15. event OwnerPronouncedDead(address indexed owner, address indexed heir, uint indexed timeOfDeath);
  16. /**
  17. * @dev Throw an exception if called by any account other than the heir's.
  18. */
  19. modifier onlyHeir() {
  20. require(msg.sender == heir);
  21. _;
  22. }
  23. /**
  24. * @notice Create a new Inheritable Contract with heir address 0x0.
  25. * @param _heartbeatTimeout time available for the owner to notify she's alive,
  26. * before the heir can take ownership.
  27. */
  28. function Inheritable(uint _heartbeatTimeout) public {
  29. heartbeatTimeout = _heartbeatTimeout;
  30. }
  31. function setHeir(address newHeir) public onlyOwner {
  32. heir = newHeir;
  33. }
  34. /**
  35. * @dev set heir = 0x0
  36. */
  37. function removeHeir() public onlyOwner {
  38. delete(heir);
  39. }
  40. function setHeartbeatTimeout(uint newHeartbeatTimeout) public onlyOwner {
  41. require(ownerLives());
  42. heartbeatTimeout = newHeartbeatTimeout;
  43. }
  44. /**
  45. * @dev Heir can pronounce the owners death. To inherit the ownership, he will
  46. * have to wait for `heartbeatTimeout` seconds.
  47. */
  48. function pronounceDeath() public onlyHeir {
  49. require(ownerLives());
  50. timeOfDeath = now;
  51. OwnerPronouncedDead(owner, heir, timeOfDeath);
  52. }
  53. /**
  54. * @dev Owner can send a heartbeat if she was mistakenly pronounced dead.
  55. */
  56. function heartbeat() public onlyOwner {
  57. delete(timeOfDeath);
  58. }
  59. /**
  60. * @dev Allows heir to transfer ownership only if heartbeat has timed out.
  61. */
  62. function inherit() public onlyHeir {
  63. require(!ownerLives());
  64. require(now >= timeOfDeath + heartbeatTimeout);
  65. OwnershipTransferred(owner, heir);
  66. owner = heir;
  67. delete(timeOfDeath);
  68. }
  69. function ownerLives() internal returns (bool) {
  70. return timeOfDeath == 0;
  71. }
  72. }