Inheritable.sol 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 Inheritable is Ownable {
  10. address public heir;
  11. // Time window the owner has to notify they are alive.
  12. uint public heartbeatTimeout;
  13. // Timestamp of the owner's death, as pronounced by the heir.
  14. uint public timeOfDeath;
  15. event HeirChanged(address indexed owner, address indexed newHeir);
  16. event OwnerHeartbeated(address indexed owner);
  17. event OwnerProclaimedDead(address indexed owner, address indexed heir, uint timeOfDeath);
  18. /**
  19. * @dev Throw an exception if called by any account other than the heir's.
  20. */
  21. modifier onlyHeir() {
  22. require(msg.sender == heir);
  23. _;
  24. }
  25. /**
  26. * @notice Create a new Inheritable Contract with heir address 0x0.
  27. * @param _heartbeatTimeout time available for the owner to notify they are alive,
  28. * before the heir can take ownership.
  29. */
  30. function Inheritable(uint _heartbeatTimeout) public {
  31. setHeartbeatTimeout(_heartbeatTimeout);
  32. }
  33. function setHeir(address newHeir) public onlyOwner {
  34. require(newHeir != owner);
  35. heartbeat();
  36. HeirChanged(owner, newHeir);
  37. heir = newHeir;
  38. }
  39. /**
  40. * @dev set heir = 0x0
  41. */
  42. function removeHeir() public onlyOwner {
  43. heartbeat();
  44. heir = 0;
  45. }
  46. /**
  47. * @dev Heir can pronounce the owners death. To inherit the ownership, they will
  48. * have to wait for `heartbeatTimeout` seconds.
  49. */
  50. function proclaimDeath() public onlyHeir {
  51. require(ownerLives());
  52. OwnerProclaimedDead(owner, heir, timeOfDeath);
  53. timeOfDeath = now;
  54. }
  55. /**
  56. * @dev Owner can send a heartbeat if they were mistakenly pronounced dead.
  57. */
  58. function heartbeat() public onlyOwner {
  59. OwnerHeartbeated(owner);
  60. timeOfDeath = 0;
  61. }
  62. /**
  63. * @dev Allows heir to transfer ownership only if heartbeat has timed out.
  64. */
  65. function inherit() public onlyHeir {
  66. require(!ownerLives());
  67. require(now >= timeOfDeath + heartbeatTimeout);
  68. OwnershipTransferred(owner, heir);
  69. owner = heir;
  70. timeOfDeath = 0;
  71. }
  72. function setHeartbeatTimeout(uint newHeartbeatTimeout) internal onlyOwner {
  73. require(ownerLives());
  74. heartbeatTimeout = newHeartbeatTimeout;
  75. }
  76. function ownerLives() internal constant returns (bool) {
  77. return timeOfDeath == 0;
  78. }
  79. }