Skip to content

Instantly share code, notes, and snippets.

@kidwai
Last active September 13, 2017 04:08
Show Gist options
  • Save kidwai/20a8d1988dacb7670223d39d39c48444 to your computer and use it in GitHub Desktop.
Save kidwai/20a8d1988dacb7670223d39d39c48444 to your computer and use it in GitHub Desktop.
A wallet whose owner can be changed after a fixed period of absence.
pragma solidity ^0.4.11;
// A contract with an owner property
contract Ownable {
address public owner;
modifier onlyOwner {
require(msg.sender == owner);
_;
}
}
// An ownable contract that can send and receive ether
contract Wallet is Ownable {
function Wallet () {
owner = msg.sender;
}
function withdraw (address _to, uint _value) onlyOwner {
_to.transfer(_value);
}
function () payable {}
}
// A wallet with a pre-specified "timeout" and "beneficiary".
// These are given by 'death_interval' and 'beneficiary', respectively.
contract DeathWallet is Wallet {
uint256 public death_block; // the block after which absence means death
uint256 public death_interval; // the number of blocks of absence to interpret as death
address public beneficiary; // the beneficiary in case of death
function DeathWallet (address _beneficiary, uint256 _death_interval) {
owner = msg.sender;
beneficiary = _beneficiary;
death_interval = _death_interval;
death_block = block.number + death_interval;
}
function () payable {
// update death block if owner checks in
if (msg.sender == owner) {
update_death();
}
// update owner if beneficiary calls after death
else if (msg.sender == beneficiary && isDead()) {
owner = beneficiary;
}
}
function setBeneficiary (address _to) onlyOwner {
beneficiary = _to;
}
function isDead () constant internal returns (bool) {
return block.number >= death_block;
}
function update_death () internal {
death_block = block.number + death_interval;
}
}
@kidwai
Copy link
Author

kidwai commented Sep 13, 2017

This would be used as follows.

  1. Create a DeathWallet contract, supplying the beneficiary address and the number of blocks to interpret as your death. That is, if you believe that being unable to send a transaction to your Death Wallet within 1 million blocks is sufficient to indicate your death, enter 1000000.

  2. Send a transaction to the Death Wallet to 'check in'. This transaction may or may not contain funds. Your death block will be reset to the initial death interval specified into the future.

  3. If you die, you will likely be unable to transact with your funds. Rather than forfeiting these, your beneficiary can send an empty transaction to your contract after your 'death block', and if you have not recently checked in, the contract's owner will be updated to the beneficiary address, allowing them to make withdrawal's and set subsequent beneficiaries.

Lots of improvements and generalizations to work on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment