forked from PAY-IT-FORWARD/contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleRegistry.sol
170 lines (139 loc) · 5.63 KB
/
SimpleRegistry.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
//! Registry contract.
//! By Gav Wood (Ethcore), 2016.
//! Released under the Apache Licence 2.
pragma solidity ^0.4.0;
// From Owned.sol
contract Owned {
event NewOwner(address indexed old, address indexed current);
function setOwner(address _new) only_owner { NewOwner(owner, _new); owner = _new; }
modifier only_owner { if (msg.sender != owner) return; _; }
address public owner = msg.sender;
}
// From Registry.sol
contract MetadataRegistry {
event DataChanged(bytes32 indexed name, string indexed key, string plainKey);
function getData(bytes32 _name, string _key) constant returns (bytes32);
function getAddress(bytes32 _name, string _key) constant returns (address);
function getUint(bytes32 _name, string _key) constant returns (uint);
}
contract OwnerRegistry {
event Reserved(bytes32 indexed name, address indexed owner);
event Transferred(bytes32 indexed name, address indexed oldOwner, address indexed newOwner);
event Dropped(bytes32 indexed name, address indexed owner);
function getOwner(bytes32 _name) constant returns (address);
}
contract ReverseRegistry {
event ReverseConfirmed(string indexed name, address indexed reverse);
event ReverseRemoved(string indexed name, address indexed reverse);
function hasReverse(bytes32 _name) constant returns (bool);
function getReverse(bytes32 _name) constant returns (address);
function canReverse(address _data) constant returns (bool);
function reverse(address _data) constant returns (string);
}
contract SimpleRegistry is Owned, MetadataRegistry, OwnerRegistry, ReverseRegistry {
struct Entry {
address owner;
address reverse;
mapping (string => bytes32) data;
}
event Drained(uint amount);
event FeeChanged(uint amount);
event ReverseProposed(string indexed name, address indexed reverse);
// Registry functions.
function getData(bytes32 _name, string _key) constant returns (bytes32) {
return entries[_name].data[_key];
}
function getAddress(bytes32 _name, string _key) constant returns (address) {
return address(entries[_name].data[_key]);
}
function getUint(bytes32 _name, string _key) constant returns (uint) {
return uint(entries[_name].data[_key]);
}
// OwnerRegistry function.
function getOwner(bytes32 _name) constant returns (address) { return entries[_name].owner; }
// ReversibleRegistry functions.
function hasReverse(bytes32 _name) constant returns (bool) { return entries[_name].reverse != 0; }
function getReverse(bytes32 _name) constant returns (address) { return entries[_name].reverse; }
function canReverse(address _data) constant returns (bool) { return bytes(reverses[_data]).length != 0; }
function reverse(address _data) constant returns (string) { return reverses[_data]; }
// Reservation functions.
function reserve(bytes32 _name) when_unreserved(_name) when_fee_paid payable returns (bool success) {
entries[_name].owner = msg.sender;
Reserved(_name, msg.sender);
return true;
}
function reserved(bytes32 _name) constant returns (bool reserved) {
return entries[_name].owner != 0;
}
function transfer(bytes32 _name, address _to) only_owner_of(_name) returns (bool success) {
entries[_name].owner = _to;
Transferred(_name, msg.sender, _to);
return true;
}
function drop(bytes32 _name) only_owner_of(_name) returns (bool success) {
delete reverses[entries[_name].reverse];
delete entries[_name];
Dropped(_name, msg.sender);
return true;
}
// Data admin functions.
function setData(bytes32 _name, string _key, bytes32 _value) only_owner_of(_name) returns (bool success) {
entries[_name].data[_key] = _value;
DataChanged(_name, _key, _key);
return true;
}
function setAddress(bytes32 _name, string _key, address _value) only_owner_of(_name) returns (bool success) {
entries[_name].data[_key] = bytes32(_value);
DataChanged(_name, _key, _key);
return true;
}
function setUint(bytes32 _name, string _key, uint _value) only_owner_of(_name) returns (bool success) {
entries[_name].data[_key] = bytes32(_value);
DataChanged(_name, _key, _key);
return true;
}
// Reverse registration.
function proposeReverse(string _name, address _who) only_owner_of(sha3(_name)) returns (bool success) {
var sha3Name = sha3(_name);
if (entries[sha3Name].reverse != 0 && sha3(reverses[entries[sha3Name].reverse]) == sha3Name) {
delete reverses[entries[sha3Name].reverse];
ReverseRemoved(_name, entries[sha3Name].reverse);
}
entries[sha3Name].reverse = _who;
ReverseProposed(_name, _who);
return true;
}
function confirmReverse(string _name) when_proposed(_name) returns (bool success) {
reverses[msg.sender] = _name;
ReverseConfirmed(_name, msg.sender);
return true;
}
function confirmReverseAs(string _name, address _who) only_owner returns (bool success) {
reverses[_who] = _name;
ReverseConfirmed(_name, _who);
return true;
}
function removeReverse() {
ReverseRemoved(reverses[msg.sender], msg.sender);
delete entries[sha3(reverses[msg.sender])].reverse;
delete reverses[msg.sender];
}
// Admin functions for the owner.
function setFee(uint _amount) only_owner returns (bool) {
fee = _amount;
FeeChanged(_amount);
return true;
}
function drain() only_owner returns (bool) {
Drained(this.balance);
if (!msg.sender.send(this.balance)) throw;
return true;
}
modifier when_unreserved(bytes32 _name) { if (entries[_name].owner != 0) return; _; }
modifier only_owner_of(bytes32 _name) { if (entries[_name].owner != msg.sender) return; _; }
modifier when_proposed(string _name) { if (entries[sha3(_name)].reverse != msg.sender) return; _; }
modifier when_fee_paid { if (msg.value < fee) return; _; }
mapping (bytes32 => Entry) entries;
mapping (address => string) reverses;
uint public fee = 1 ether;
}