Inheritable.sol 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 OwnerPronouncedDead(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. heartbeatTimeout = _heartbeatTimeout;
  32. }
  33. function setHeir(address newHeir) public onlyOwner {
  34. heartbeat();
  35. HeirChanged(owner, newHeir);
  36. heir = newHeir;
  37. }
  38. /**
  39. * @dev set heir = 0x0
  40. */
  41. function removeHeir() public onlyOwner {
  42. heartbeat();
  43. heir = 0;
  44. }
  45. function setHeartbeatTimeout(uint newHeartbeatTimeout) public onlyOwner {
  46. require(ownerLives());
  47. heartbeatTimeout = newHeartbeatTimeout;
  48. }
  49. /**
  50. * @dev Heir can pronounce the owners death. To inherit the ownership, they will
  51. * have to wait for `heartbeatTimeout` seconds.
  52. */
  53. function pronounceDeath() public onlyHeir {
  54. require(ownerLives());
  55. OwnerPronouncedDead(owner, heir, timeOfDeath);
  56. timeOfDeath = now;
  57. }
  58. /**
  59. * @dev Owner can send a heartbeat if they were mistakenly pronounced dead.
  60. */
  61. function heartbeat() public onlyOwner {
  62. OwnerHeartbeated(owner);
  63. timeOfDeath = 0;
  64. }
  65. /**
  66. * @dev Allows heir to transfer ownership only if heartbeat has timed out.
  67. */
  68. function inherit() public onlyHeir {
  69. require(!ownerLives());
  70. require(now >= timeOfDeath + heartbeatTimeout);
  71. OwnershipTransferred(owner, heir);
  72. owner = heir;
  73. timeOfDeath = 0;
  74. }
  75. function ownerLives() internal returns (bool) {
  76. return timeOfDeath == 0;
  77. }
  78. }