-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDuosign.sol
227 lines (197 loc) · 6.83 KB
/
Duosign.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
The MIT License (MIT)
Copyright (c) 2017 DFINITY Stiftung
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/**
* @title: Duo-signing wallet.
* @author: Timo Hanke <timo.t.hanke@gmail.com> (twitter: @timothanke)
*
* This contract is a minimalistic and easy-to-use "two-sig" wallet.
* It has two or more owners and each action requires exactly two owners to
* sign.
*
* Actions are:
* - forward funds
* - add owner
* - remove owner
*
* Usage:
* - The proposal and subsequent confirmation by the second owner are two
* identical calls, just made from different sender addresses.
* - Each action is identified by a unique nonce that can be arbitrarily chosen
* by the users. The two signers have to use the same nonce.
*
* The contract is intentionally reduced to a minimal feature set:
* - The signature "threshold" is not configurable, it is always 2.
* - There is no handling of transaction payload (data), only pure value
* transfers.
*/
pragma solidity ^0.4.6;
contract Duosign {
enum TxType {
FWD, // forward eth
ADD, // add owner
DEL // remove owner
}
enum State {
EMPTY, // no tx initialized
OPEN, // tx proposed
DONE // tx executed
}
struct Transaction {
TxType txType;
address addr;
uint value;
}
mapping(address => bool) public isOwner;
mapping(uint32 => Transaction) public proposalByNonce;
mapping(uint32 => address) public proposerByNonce;
mapping(uint32 => State) public stateByNonce;
uint public nOwners;
address[] public ownerList; // all owners, former and current ones
uint32[] public nonceList; // all nonces that are in non-EMPTY state
/**
* Constructor
*
* The constructor initializes the set of owners.
*
* Note that the deployer is not by default an owner.
*/
function Duosign(address[] _owners) {
for (uint i = 0; i < _owners.length; ++i)
toggleOwner(_owners[i], true);
}
/**
* Default function
*
* By default do nothing but accepting the value.
*/
function() payable { }
/**
* Modifiers
*
* onlyowner - the sender must be an owner
* dedup - the nonce must be either new or related to an in-progress
* transaction that was opened by another owner
*/
modifier onlyowner {
if (!isOwner[msg.sender]) { throw; }
_;
}
modifier dedup(uint32 nonce) {
if (stateByNonce[nonce] == State.DONE) { throw; }
if (stateByNonce[nonce] == State.OPEN && proposerByNonce[nonce] == msg.sender) { throw; }
_;
}
/**
* Public functions
*
* Actions. These functions have to be called twice by different owners.
* forward - forward value out of the contract
* add - add an owner to the contract
* del - remove an owner of the contract
* ren - replace an owner by a different one
*
* Non-actions. These functions can be called by a single owner.
* cancel - cancel a proposed action before it is executed
*/
/**
* Actions.
*
* These functions build a Transaction struct of the corresponding type and
* pass it to the issue function along with the nonce. The issue function
* takes care of the rest.
*/
function forward(address to, uint value, uint32 nonce) onlyowner dedup(nonce) public {
issue(Transaction(TxType.FWD, to, value), nonce);
}
function add(address owner, uint32 nonce) onlyowner dedup(nonce) public {
issue(Transaction(TxType.ADD, owner, 0), nonce);
}
function del(address owner, uint32 nonce) onlyowner dedup(nonce) public {
issue(Transaction(TxType.DEL, owner, 0), nonce);
}
/**
* A proposer can cancel its own proposal before it got executed.
* The state of the nonce is simply advanced to DONE without executing it.
* The nonce can not be re-used anymore.
*/
function cancel(uint32 nonce) public {
if (proposerByNonce[nonce] != msg.sender) { throw; }
stateByNonce[nonce] = State.DONE;
}
/*
* Internal functions
*
* sameParameters - check if a new Transaction matches a previous proposal
* issue - opens a proposal (if Transaction is new) or calls execute
* execute - executes Transaction according to its type
* toggleOwner - add or remove an owner
*/
function sameParameters(Transaction a, Transaction b) internal constant returns (bool) {
return (a.txType == b.txType && a.addr == b.addr && a.value == b.value);
}
function issue(Transaction newTx, uint32 nonce) internal {
// first appearance of nonce?
if (stateByNonce[nonce] == State.EMPTY) {
// store Transaction as a proposal
proposerByNonce[nonce] = msg.sender;
proposalByNonce[nonce] = newTx;
stateByNonce[nonce] = State.OPEN;
nonceList.push(nonce);
return;
} else { // repeated appearance of nonce
// get the previous proposal
Transaction memory proposal = proposalByNonce[nonce];
// new transaction must match proposal
if (!sameParameters(newTx, proposal)) { throw; }
// adjust state & execute
stateByNonce[nonce] = State.DONE;
execute(proposal);
}
}
function execute(Transaction tx) internal {
// case "forward value"
if (tx.txType == TxType.FWD) {
bool success = tx.addr.call.value(tx.value)();
if (!success) { throw; }
}
// case "add owner"
else if (tx.txType == TxType.ADD) {
toggleOwner(tx.addr, true);
}
// case "remove owner"
else if (tx.txType == TxType.DEL) {
// require at least two remaining owners
if (nOwners <= 2) { throw; }
toggleOwner(tx.addr, false);
}
}
function toggleOwner(address addr, bool add) internal {
// throw if owner to be added already existed
// throw if owner to be removed does not existed
if (isOwner[addr] == add) { throw; }
// add new owner
if (add) {
nOwners++;
ownerList.push(addr);
} else {
nOwners--; }
isOwner[addr] = add;
}
}