forked from PAY-IT-FORWARD/contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyServerSet.sol
88 lines (74 loc) · 3.06 KB
/
KeyServerSet.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
pragma solidity ^0.4.6;
contract Owned {
modifier only_owner { if (msg.sender != owner) return; _; }
event NewOwner(address indexed old, address indexed current);
function setOwner(address _new) only_owner { NewOwner(owner, _new); owner = _new; }
address public owner = msg.sender;
}
contract KeyServerSet is Owned {
struct KeyServer {
// Index in the keyServersList.
uint index;
// Public key of key server.
bytes publicKey;
// IP address of key server.
string ip;
}
// Public keys of all active key servers.
address[] public keyServersList;
// Mapping public key => server IP address.
mapping(address => KeyServer) keyServers;
// When new server is added to set.
event KeyServerAdded(address keyServer);
// When existing server is removed from set.
event KeyServerRemoved(address keyServer);
// Only if valid public is passed
modifier valid_public(bytes keyServerPublic) { require(keyServerPublic.length == 64); _; }
// Only run if server is not currently in the set.
modifier new_key_server(address keyServer) { require(sha3(keyServers[keyServer].ip) == sha3("")); _; }
// Only run if server is currently in the set.
modifier old_key_server(address keyServer) { require(sha3(keyServers[keyServer].ip) != sha3("")); _; }
// Get all active key servers public keys.
function getKeyServers() constant returns (address[]) {
return keyServersList;
}
// Get key server public key.
function getKeyServerPublic(address keyServer) old_key_server(keyServer) constant returns (bytes) {
return keyServers[keyServer].publicKey;
}
// Get key server address.
function getKeyServerAddress(address keyServer) old_key_server(keyServer) constant returns (string) {
return keyServers[keyServer].ip;
}
// Add new key server to set.
function addKeyServer(bytes keyServerPublic, string keyServerIp) public only_owner valid_public(keyServerPublic) new_key_server(computeAddress(keyServerPublic)) {
// compute address from public
address keyServer = computeAddress(keyServerPublic);
// fire event
KeyServerAdded(keyServer);
// append to the list and to the map
keyServers[keyServer].index = keyServersList.length;
keyServers[keyServer].publicKey = keyServerPublic;
keyServers[keyServer].ip = keyServerIp;
keyServersList.push(keyServer);
}
// Remove key server from set.
function removeKeyServer(address keyServer) public only_owner old_key_server(keyServer) {
// fire event
KeyServerRemoved(keyServer);
// swap list elements (removedIndex, lastIndex)
uint removedIndex = keyServers[keyServer].index;
uint lastIndex = keyServersList.length - 1;
address lastKeyServer = keyServersList[lastIndex];
keyServersList[removedIndex] = lastKeyServer;
keyServers[lastKeyServer].index = removedIndex;
// remove element from list and map
delete keyServersList[lastIndex];
delete keyServers[keyServer];
keyServersList.length--;
}
// Compute address from public key.
function computeAddress(bytes keyServerPublic) constant private returns (address) {
return address(uint(keccak256(keyServerPublic)) & 0x00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
}
}