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