-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathWNS.sol
205 lines (153 loc) · 7.11 KB
/
WNS.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
pragma solidity 0.4.24;
/**
* @dev Worldwide OpenBazaar Resource Finder Naming Service(WorfNS)
* A simple naming service to register handles on FCFS basis
* It will map uniques handle to openbazaar addresses i.e. peerId
* handle=>peerId
*/
contract WNS {
event NewHandle(string handle, string peerId, address indexed owner);
event NewDisplayName(string handle, string displayName);
event NewImageLocation(string handle, string imageLocation);
event NewPeerId(string handle, string peerId);
event OwnershipTransferred(string handle, address indexed newOwner);
event HandleRemoved(string handle);
struct Handle{
address handleOwner;//Owner of the handle
string handleName;//This should be unqiue in nature
string displayName;//Need not to be unqiue
string imageLocation;//Can be an URI or IPNS address
string peerId;//OB peerId
}
mapping(bytes32=>Handle) handleNameHashVsHandle;//Unique handle hash versus Handle
mapping(address=>bool) public superUsers;//addresses who are allowed to handles on other user's behalf
modifier onlyHandleOwner(string handle){
require(handleNameHashVsHandle[keccak256(abi.encodePacked(handle))].handleOwner == msg.sender, "Unauthorized access to Handle");
_;
}
modifier handleExists(string handle){
require(handleNameHashVsHandle[keccak256(abi.encodePacked(handle))].handleOwner != address(0), "Handle does not exists");
_;
}
modifier handleAvailable(string handle){
require(handleNameHashVsHandle[keccak256(abi.encodePacked(handle))].handleOwner == address(0), "Handle is already taken");
_;
}
modifier nonZeroAddress(address _address){
require(_address != address(0), "0 address sent");
_;
}
modifier onlySuperUser(){
require(superUsers[msg.sender], "Not a super user");
_;
}
constructor(address[] _superUsers)public {
for(uint i = 0;i<_superUsers.length;i++){
superUsers[_superUsers[i]] = true;
}
}
/**
* @dev Allows super user to add handle on other user's behalf
* @param owner The address of the owner
* @param handle Unique Handle
* @param _displayName Display name of the entity
* @param _imageLocation URI or IPNS of the image
* @param _peerId OB peer id
*/
function addHandle(address owner, string handle, string _displayName, string _imageLocation, string _peerId)external
onlySuperUser nonZeroAddress(owner){
_createHandle(owner, handle, _displayName, _imageLocation, _peerId);
}
/**
* @dev Method to create new handle
* @param handle Unique Handle
* @param _displayName Display name of the entity
* @param _imageLocation URI or IPNS of the image
* @param _peerId OB peer id
*/
function createHandle(string handle, string _displayName, string _imageLocation, string _peerId)external {
_createHandle(msg.sender, handle, _displayName, _imageLocation, _peerId);
}
//helper method to add/create new handle in the contract
function _createHandle(address owner, string handle, string _displayName, string _imageLocation, string _peerId)private
handleAvailable(handle) {
require(bytes(handle).length>0, "Empty handle name provided");
bytes32 handleHash = keccak256(abi.encodePacked(handle));
handleNameHashVsHandle[handleHash] = Handle({
handleOwner:owner,
handleName:handle,
displayName:_displayName,
imageLocation:_imageLocation,
peerId:_peerId
});
emit NewHandle(handle, _peerId, owner);
}
/**
* @dev Transfer handle ownership to new address
* @param handle Handle whose ownership has to be changed
* @param newOwner Address of the new owner
*/
function transferOwnership(string handle, address newOwner)external
handleExists(handle) onlyHandleOwner(handle) nonZeroAddress(newOwner){
bytes32 handleHash = keccak256(abi.encodePacked(handle));
require(newOwner != handleNameHashVsHandle[handleHash].handleOwner, "New owner is same as previous owner");
handleNameHashVsHandle[handleHash].handleOwner = newOwner;
emit OwnershipTransferred(handle, newOwner);
}
/**
* @dev Method to change display name of the entity
* @param handle Handle whose display name has to be changed
* @param newName New Display Name
*/
function changeDisplayName(string handle, string newName)external handleExists(handle) onlyHandleOwner(handle){
require(bytes(newName).length>0, "Empyt names not allowed");
handleNameHashVsHandle[keccak256(abi.encodePacked(handle))].displayName = newName;
emit NewDisplayName(handle, newName);
}
/**
* @dev Method to change Location of Image
* @param handle Handle whose image location has to be changed
* @param newImageLocation New Image Location
*/
function changeImageLocation(string handle, string newImageLocation)external handleExists(handle) onlyHandleOwner(handle){
handleNameHashVsHandle[keccak256(abi.encodePacked(handle))].imageLocation = newImageLocation;
emit NewImageLocation(handle, newImageLocation);
}
/**
* @dev Method to extra data
* @param handle Handle whose extra data has to be changed
* @param peerId New Peer Id
*/
function changePeerId(string handle, string peerId)external handleExists(handle) onlyHandleOwner(handle){
handleNameHashVsHandle[keccak256(abi.encodePacked(handle))].peerId = peerId;
emit NewPeerId(handle, peerId);
}
/**
* @dev Method to get handle info about specific handle
* @param handleName The handle whose info has to be fetched
*/
function getHandleInfo(string handleName)external
view returns(address owner, string handle, string displayName, string imageLocation, string peerId){
bytes32 handleBytes = keccak256(abi.encodePacked(handleName));
owner = handleNameHashVsHandle[handleBytes].handleOwner;
handle = handleNameHashVsHandle[handleBytes].handleName;
displayName = handleNameHashVsHandle[handleBytes].displayName;
imageLocation = handleNameHashVsHandle[handleBytes].imageLocation;
peerId = handleNameHashVsHandle[handleBytes].peerId;
}
/**
* @dev Method to check availability of the handle
* @param handle Handle whose availability has to be checked
*/
function isHandleAvailable(string handle)external view returns(bool){
return handleNameHashVsHandle[keccak256(abi.encodePacked(handle))].handleOwner == address(0);
}
/**
* @dev Method to remove handleS
* @param handle Handle to be removed
*/
function removeHandle(string handle)external handleExists(handle) onlyHandleOwner(handle){
delete handleNameHashVsHandle[keccak256(abi.encodePacked(handle))];
emit HandleRemoved(handle);
}
}