-
Notifications
You must be signed in to change notification settings - Fork 56
/
channel.sol
51 lines (37 loc) · 1.09 KB
/
channel.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
pragma solidity ^0.4.0;
contract Channel {
address public channelSender;
address public channelRecipient;
uint public startDate;
uint public channelTimeout;
mapping (bytes32 => address) signatures;
function Channel(address to, uint timeout) payable {
channelRecipient = to;
channelSender = msg.sender;
startDate = now;
channelTimeout = timeout;
}
function CloseChannel(bytes32 h, uint8 v, bytes32 r, bytes32 s, uint value){
address signer;
bytes32 proof;
// get signer from signature
signer = ecrecover(h, v, r, s);
// signature is invalid, throw
if (signer != channelSender && signer != channelRecipient) throw;
proof = sha3(this, value);
// signature is valid but doesn't match the data provided
if (proof != h) throw;
if (signatures[proof] == 0)
signatures[proof] = signer;
else if (signatures[proof] != signer){
// channel completed, both signatures provided
if (!channelRecipient.send(value)) throw;
selfdestruct(channelSender);
}
}
function ChannelTimeout(){
if (startDate + channelTimeout > now)
throw;
selfdestruct(channelSender);
}
}