forked from yep/eth-tweet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TweetRegistry.sol
125 lines (104 loc) · 3.92 KB
/
TweetRegistry.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
/*
decentralized microblogging
Copyright (C) 2015 Jahn Bertsch
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation in version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// "class" TweetRegistry
contract TweetRegistry {
// mappings to look up account names, account ids and addresses
mapping (address => string) _addressToAccountName;
mapping (uint => address) _accountIdToAccountAddress;
mapping (string => address) _accountNameToAddress;
// might be interesting to see how many people use the system
uint _numberOfAccounts;
// owner
address _registryAdmin;
// allowed to administrate accounts only, not everything
address _accountAdmin;
// if a newer version of this registry is available, force users to use it
bool _registrationDisabled;
function TweetRegistry() {
_registryAdmin = msg.sender;
_accountAdmin = msg.sender; // can be changed later
_numberOfAccounts = 0;
_registrationDisabled = false;
}
function register(string name, address accountAddress) returns (int result) {
if (_accountNameToAddress[name] != address(0)) {
// name already taken
result = -1;
} else if (bytes(_addressToAccountName[accountAddress]).length != 0) {
// account address is already registered
result = -2;
} else if (bytes(name).length >= 64) {
// name too long
result = -3;
} else if (_registrationDisabled){
// registry is disabled because a newer version is available
result = -4;
} else {
_addressToAccountName[accountAddress] = name;
_accountNameToAddress[name] = accountAddress;
_accountIdToAccountAddress[_numberOfAccounts] = accountAddress;
_numberOfAccounts++;
result = 0; // success
}
}
function getNumberOfAccounts() constant returns (uint numberOfAccounts) {
numberOfAccounts = _numberOfAccounts;
}
function getAddressOfName(string name) constant returns (address addr) {
addr = _accountNameToAddress[name];
}
function getNameOfAddress(address addr) constant returns (string name) {
name = _addressToAccountName[addr];
}
function getAddressOfId(uint id) constant returns (address addr) {
addr = _accountIdToAccountAddress[id];
}
function unregister() returns (string unregisteredAccountName) {
unregisteredAccountName = _addressToAccountName[msg.sender];
_addressToAccountName[msg.sender] = "";
_accountNameToAddress[unregisteredAccountName] = address(0);
// _accountIdToAccountAddress is never deleted on purpose
}
function adminUnregister(string name) {
if (msg.sender == _registryAdmin || msg.sender == _accountAdmin) {
address addr = _accountNameToAddress[name];
_addressToAccountName[addr] = "";
_accountNameToAddress[name] = address(0);
// _accountIdToAccountAddress is never deleted on purpose
}
}
function adminSetRegistrationDisabled(bool registrationDisabled) {
// currently, the code of the registry can not be updated once it is
// deployed. if a newer version of the registry is available, account
// registration can be disabled
if (msg.sender == _registryAdmin) {
_registrationDisabled = registrationDisabled;
}
}
function adminSetAccountAdministrator(address accountAdmin) {
if (msg.sender == _registryAdmin) {
_accountAdmin = accountAdmin;
}
}
function adminRetrieveDonations() {
if (msg.sender == _registryAdmin) {
_registryAdmin.send(this.balance);
}
}
function adminDeleteRegistry() {
if (msg.sender == _registryAdmin) {
suicide(_registryAdmin); // this is a predefined function, it deletes the contract and returns all funds to the admin's address
}
}
}