-
Notifications
You must be signed in to change notification settings - Fork 24
/
LifChannels.sol
302 lines (258 loc) · 8.85 KB
/
LifChannels.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
pragma solidity ^0.4.18;
import "zeppelin-solidity/contracts/token/ERC20/ERC20.sol";
import "zeppelin-solidity/contracts/math/SafeMath.sol";
import "zeppelin-solidity/contracts/ECRecovery.sol";
/**
@title LifChannels, State channels for ERC20 Lif Token
Contract that provides holders of a ERC20 compatible token the creation of
channels between two users, once a channel is open the users can exchange
signatures offchain to agree on the final value of the transfer.
Uses OpenZeppelin ERC20 and SafeMath lib.
*/
contract LifChannels {
using SafeMath for uint256;
// Add recover method for bytes32 using ECRecovery lib from OpenZeppelin
using ECRecovery for bytes32;
// The ERC20 token to be used
ERC20 public token;
// The amount of time that a receiver has to challenge the sender
uint256 public challengeTime;
// The channels opened
mapping (bytes32 => Channel) public channels;
struct Channel {
uint256 deposit;
uint8 nonce;
}
// The requests from sender to close a channel
mapping (bytes32 => ClosingRequest) public closingRequests;
struct ClosingRequest {
uint256 closingBalance;
uint256 closeTime;
}
/*
* Events
*/
event ChannelCreated(
address indexed sender,
address indexed receiver,
uint8 indexed nonce,
uint256 deposit
);
event ChannelCloseRequested(
address indexed sender,
address indexed receiver,
uint8 indexed nonce,
uint256 balance
);
event ChannelClosed(
address indexed sender,
address indexed receiver,
uint8 indexed nonce,
uint256 balance
);
/**
* @dev Constructor
* @param tokenAddress address, the address of the ERC20 token that will be used
* @param _challengeTime uint256, the time that a channel has before ends
with and uncoopertive close
*/
function LifChannels(
address tokenAddress,
uint256 _challengeTime
) public {
require(tokenAddress != address(0));
require(_challengeTime >= 0);
token = ERC20(tokenAddress);
challengeTime = _challengeTime;
}
/**
* @dev Creates a channel between the msg.sender and the receiver
* @param receiver address, the receiver of the channel
* @param deposit uint256, the balance taht I want to load in the channel
* @param nonce uint8, the nonce number of the channel
*/
function openChannel(
address receiver,
uint256 deposit,
uint8 nonce
) external {
require(nonce > 0);
require(receiver != address(0));
require(deposit > 0);
// Create unique identifier from sender, receiver and current block timestamp
bytes32 channelId = getChannelId(msg.sender, receiver, nonce);
// Check taht teh channel not exist
require(channels[channelId].deposit == 0);
require(channels[channelId].nonce == 0);
require(closingRequests[channelId].closeTime == 0);
// Store channel information
channels[channelId] = Channel({deposit: deposit, nonce: nonce});
ChannelCreated(msg.sender, receiver, nonce, deposit);
// transferFrom deposit from sender to contract
// ! needs prior approval from user
require(token.transferFrom(msg.sender, address(this), deposit));
}
/**
* @dev Starts a close channel request form the sender
* @param receiver address, the receiver of the channel
* @param nonce uint8, the nonce number of the channel
* @param balance uint256, the final balance of teh receiver
*/
function uncooperativeClose(
address receiver,
uint8 nonce,
uint256 balance
) external {
bytes32 channelId = getChannelId(msg.sender, receiver, nonce);
// Check that the closing request dont exist
require(closingRequests[channelId].closeTime == 0);
// Check that the balance is less that the deposited
require(balance <= channels[channelId].deposit);
// Mark channel as closed and create closing request
closingRequests[channelId].closeTime = block.timestamp.add(challengeTime);
require(closingRequests[channelId].closeTime > block.timestamp);
closingRequests[channelId].closingBalance = balance;
ChannelCloseRequested(msg.sender, receiver, nonce, balance);
}
/**
* @dev Close a channel with the agreement of the sender and receiver
* @param receiver address, the receiver of the channel
* @param nonce uint8, the nonce number of the channel
* @param balanceMsgSig bytes, the signature of the sender
* @param closingSig bytes, the signature of the receiver
*/
function cooperativeClose(
address receiver,
uint8 nonce,
uint256 balance,
bytes balanceMsgSig,
bytes closingSig
) external {
// Derive receiver address from signature
bytes32 msgHash = keccak256(balanceMsgSig);
require(receiver == msgHash.recover(closingSig));
// Derive sender address from signed balance proof
address sender = getSignerOfBalanceHash(receiver, nonce, balance, balanceMsgSig);
close(sender, receiver, nonce, balance);
}
/**
* @dev Close a channel with an existing closing request
* @param receiver address, the receiver of the channel
* @param nonce uint8, the nonce number of the channel
*/
function closeChannel(address receiver, uint8 nonce) external {
bytes32 channelId = getChannelId(msg.sender, receiver, nonce);
// Check that the closing request was created
require(closingRequests[channelId].closeTime > 0);
// Make sure the challengeTime has ended
require(block.timestamp > closingRequests[channelId].closeTime);
close(msg.sender, receiver, nonce,
closingRequests[channelId].closingBalance
);
}
/**
* @dev Get the channel info
* @param sender address, the sender of the channel
* @param receiver address, the receiver of the channel
* @param nonce uint8, the nonce number of the channel
*/
function getChannelInfo(
address sender,
address receiver,
uint8 nonce
) external view returns (bytes32, uint256, uint256, uint256) {
bytes32 channelId = getChannelId(sender, receiver, nonce);
require(channels[channelId].nonce > 0);
return (
channelId,
channels[channelId].deposit,
closingRequests[channelId].closeTime,
closingRequests[channelId].closingBalance
);
}
/*
* Public functions
*/
/**
* @dev Get the signer of a balance hash signed with a generated hash on chain
* @param receiver address, the receiver to hash
* @param nonce uint8, the nonce number of the channel
* @param balance uint256, the balance to hash
* @param msgSigned bytes, the balance hash signed
*/
function getSignerOfBalanceHash(
address receiver,
uint8 nonce,
uint256 balance,
bytes msgSigned
) public view returns (address) {
bytes32 msgHash = generateBalanceHash(receiver, nonce, balance);
// Derive address from signature
address signer = msgHash.recover(msgSigned);
return signer;
}
/**
* @dev Generate a hash balance for an address
* @param receiver address, the receiver to hash
* @param nonce uint8, the nonce number of the channel
* @param balance uint256, the balance to hash
*/
function generateBalanceHash(
address receiver,
uint8 nonce,
uint256 balance
) public view returns (bytes32) {
return keccak256(receiver, nonce, balance, address(this));
}
/**
* @dev Generate a keccak256 hash
* @param message bytes, the mesage to hash
*/
function generateKeccak256(bytes message) public pure returns(bytes32) {
return keccak256(message);
}
/**
* @dev Generate a channel id
* @param sender address, the sender in the channel
* @param receiver address, the receiver in the channel
* @param nonce uint8, the nonce number of the channel
*/
function getChannelId(
address sender,
address receiver,
uint8 nonce
) public pure returns (bytes32 data) {
return keccak256(sender, receiver, nonce);
}
/*
* Internal functions
*/
/**
* @dev Close a channel
* @param sender address, the sender in the channel
* @param receiver address, the receiver in the channel
* @param nonce uint8, the nonce number of the channel
* @param receiverBalance uint256, the final balance of the receiver
*/
function close(
address sender,
address receiver,
uint8 nonce,
uint256 receiverBalance
) internal {
bytes32 channelId = getChannelId(sender, receiver, nonce);
Channel memory channel = channels[channelId];
require(channel.nonce > 0);
require(receiverBalance <= channel.deposit);
// Remove closed channel structures
// channel.nonce will become 0
// Change state before transfer call
delete channels[channelId];
delete closingRequests[channelId];
// Send balance to the receiver, as it is always <= deposit
require(token.transfer(receiver, receiverBalance));
// Send deposit - balance back to sender
require(token.transfer(sender, channel.deposit.sub(receiverBalance)));
ChannelClosed(sender, receiver, nonce, receiverBalance);
}
}