forked from PAY-IT-FORWARD/contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeadMansSwitch.sol
95 lines (76 loc) · 2.68 KB
/
DeadMansSwitch.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// DeadMansSwitch contract, by Gavin Wood.
// Copyright Parity Technologies Ltd (UK), 2016.
// This code may be distributed under the terms of the Apache Licence, version 2
// or the MIT Licence, at your choice.
pragma solidity ^0.4;
/// This is intended to be used as a basic wallet. It provides the Received event
/// in order to track incoming transactions. It also has one piece of additional
/// functionality: to nominate a backup owner which can, after a timeout period,
/// claim ownership over the account.
contract DeadMansSwitch {
event ReclaimBegun();
event Reclaimed();
event Sent(address indexed to, uint value, bytes data);
event Received(address indexed from, uint value, bytes data);
event Reset();
event OwnerChanged(address indexed old, address indexed now);
event BackupChanged(address indexed old, address indexed now);
event ReclaimPeriodChanged(uint old, uint now);
function DeadMansSwitch(address _owner, address _backup, uint _reclaimPeriod) {
owner = _owner;
backup = _backup;
reclaimPeriod = _reclaimPeriod;
}
function() payable { Received(msg.sender, msg.value, msg.data); }
// Backup functions
function beginReclaim() only_backup when_no_timeout {
timeout = now + reclaimPeriod;
ReclaimBegun();
}
function finalizeReclaim() only_backup when_timed_out {
owner = backup;
timeout = 0;
Reclaimed();
}
function reset() only_owner_or_backup {
timeout = 0;
Reset();
}
// Owner functions
function send(address _to, uint _value, bytes _data) only_owner {
if (!_to.call.value(_value)(_data)) throw;
Sent(_to, _value, _data);
}
function setOwner(address _owner) only_owner {
OwnerChanged(owner, _owner);
owner = _owner;
}
function setBackup(address _backup) only_owner {
BackupChanged(backup, _backup);
backup = _backup;
}
function setReclaimPeriod(uint _period) only_owner {
ReclaimPeriodChanged(reclaimPeriod, _period);
reclaimPeriod = _period;
}
// Inspectors
function reclaimStarted() constant returns (bool) {
return timeout != 0;
}
function canFinalize() constant returns (bool) {
return timeout != 0 && now > timeout;
}
function timeLeft() constant only_when_timeout returns (uint) {
return now > timeout ? 0 : timeout - now;
}
modifier only_owner { if (msg.sender != owner) throw; _; }
modifier only_backup { if (msg.sender != backup) throw; _; }
modifier only_owner_or_backup { if (msg.sender != backup && msg.sender != owner) throw; _; }
modifier only_when_timeout { if (timeout == 0) throw; _; }
modifier when_no_timeout { if (timeout == 0) _; }
modifier when_timed_out { if (timeout != 0 && now > timeout) _; }
address public owner;
address public backup;
uint public reclaimPeriod;
uint public timeout;
}