forked from PAY-IT-FORWARD/contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeadMansSwitch.sol
109 lines (90 loc) · 3.34 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//! The DeadMansSwitch contract.
//!
//! Copyright 2016 Gavin Wood, Parity Technologies Ltd.
//!
//! Licensed under the Apache License, Version 2.0 (the "License");
//! you may not use this file except in compliance with the License.
//! You may obtain a copy of the License at
//!
//! http://www.apache.org/licenses/LICENSE-2.0
//!
//! Unless required by applicable law or agreed to in writing, software
//! distributed under the License is distributed on an "AS IS" BASIS,
//! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//! See the License for the specific language governing permissions and
//! limitations under the License.
//!
//! This code may be distributed under the terms of the Apache Licence
//! version 2.0 (see above) or the MIT-license, 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 current);
event BackupChanged(address indexed old, address indexed current);
event ReclaimPeriodChanged(uint old, uint current);
function DeadMansSwitch(address _owner, address _backup, uint _reclaimPeriod) public {
owner = _owner;
backup = _backup;
reclaimPeriod = _reclaimPeriod;
}
function() payable public { Received(msg.sender, msg.value, msg.data); }
// Backup functions
function beginReclaim() only_backup when_no_timeout public {
timeout = now + reclaimPeriod;
ReclaimBegun();
}
function finalizeReclaim() only_backup when_timed_out public {
owner = backup;
timeout = 0;
Reclaimed();
}
function reset() only_owner_or_backup public {
timeout = 0;
Reset();
}
// Owner functions
function send(address _to, uint _value, bytes _data) only_owner public {
require(_to.call.value(_value)(_data));
Sent(_to, _value, _data);
}
function setOwner(address _owner) only_owner public {
OwnerChanged(owner, _owner);
owner = _owner;
}
function setBackup(address _backup) only_owner public {
BackupChanged(backup, _backup);
backup = _backup;
}
function setReclaimPeriod(uint _period) only_owner public {
ReclaimPeriodChanged(reclaimPeriod, _period);
reclaimPeriod = _period;
}
// Inspectors
function reclaimStarted() constant public returns (bool) {
return timeout != 0;
}
function canFinalize() constant public returns (bool) {
return timeout != 0 && now > timeout;
}
function timeLeft() constant only_when_timeout public returns (uint) {
return now > timeout ? 0 : timeout - now;
}
modifier only_owner { require (msg.sender == owner); _; }
modifier only_backup { require (msg.sender == backup); _; }
modifier only_owner_or_backup { require (msg.sender == backup || msg.sender == owner); _; }
modifier only_when_timeout { require (timeout != 0); _; }
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;
}