Heritable.sol 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. pragma solidity ^0.4.24;
  2. import "./Ownable.sol";
  3. /**
  4. * @title Heritable
  5. * @dev The Heritable 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 Heritable is Ownable {
  10. address private heir_;
  11. // Time window the owner has to notify they are alive.
  12. uint256 private heartbeatTimeout_;
  13. // Timestamp of the owner's death, as pronounced by the heir.
  14. uint256 private timeOfDeath_;
  15. event HeirChanged(address indexed owner, address indexed newHeir);
  16. event OwnerHeartbeated(address indexed owner);
  17. event OwnerProclaimedDead(
  18. address indexed owner,
  19. address indexed heir,
  20. uint256 timeOfDeath
  21. );
  22. event HeirOwnershipClaimed(
  23. address indexed previousOwner,
  24. address indexed newOwner
  25. );
  26. /**
  27. * @dev Throw an exception if called by any account other than the heir's.
  28. */
  29. modifier onlyHeir() {
  30. require(msg.sender == heir_);
  31. _;
  32. }
  33. /**
  34. * @notice Create a new Heritable Contract with heir address 0x0.
  35. * @param _heartbeatTimeout time available for the owner to notify they are alive,
  36. * before the heir can take ownership.
  37. */
  38. constructor(uint256 _heartbeatTimeout) public {
  39. heartbeatTimeout_ = _heartbeatTimeout;
  40. }
  41. function setHeir(address _newHeir) public onlyOwner {
  42. require(_newHeir != owner);
  43. heartbeat();
  44. emit HeirChanged(owner, _newHeir);
  45. heir_ = _newHeir;
  46. }
  47. /**
  48. * @dev Use these getter functions to access the internal variables in
  49. * an inherited contract.
  50. */
  51. function heir() public view returns(address) {
  52. return heir_;
  53. }
  54. function heartbeatTimeout() public view returns(uint256) {
  55. return heartbeatTimeout_;
  56. }
  57. function timeOfDeath() public view returns(uint256) {
  58. return timeOfDeath_;
  59. }
  60. /**
  61. * @dev set heir = 0x0
  62. */
  63. function removeHeir() public onlyOwner {
  64. heartbeat();
  65. heir_ = address(0);
  66. }
  67. /**
  68. * @dev Heir can pronounce the owners death. To claim the ownership, they will
  69. * have to wait for `heartbeatTimeout` seconds.
  70. */
  71. function proclaimDeath() public onlyHeir {
  72. require(_ownerLives());
  73. emit OwnerProclaimedDead(owner, heir_, timeOfDeath_);
  74. // solium-disable-next-line security/no-block-members
  75. timeOfDeath_ = block.timestamp;
  76. }
  77. /**
  78. * @dev Owner can send a heartbeat if they were mistakenly pronounced dead.
  79. */
  80. function heartbeat() public onlyOwner {
  81. emit OwnerHeartbeated(owner);
  82. timeOfDeath_ = 0;
  83. }
  84. /**
  85. * @dev Allows heir to transfer ownership only if heartbeat has timed out.
  86. */
  87. function claimHeirOwnership() public onlyHeir {
  88. require(!_ownerLives());
  89. // solium-disable-next-line security/no-block-members
  90. require(block.timestamp >= timeOfDeath_ + heartbeatTimeout_);
  91. emit OwnershipTransferred(owner, heir_);
  92. emit HeirOwnershipClaimed(owner, heir_);
  93. owner = heir_;
  94. timeOfDeath_ = 0;
  95. }
  96. function _ownerLives() internal view returns (bool) {
  97. return timeOfDeath_ == 0;
  98. }
  99. }