27 | | - | |
---|
28 | | - | func key_Name_resolver (name) = key_entity("Name", name, "resolver") |
---|
29 | | - | |
---|
30 | | - | |
---|
31 | | - | func key_Name_expiresAt (name) = key_entity("Name", name, "expiresAt") |
---|
32 | | - | |
---|
33 | | - | |
---|
34 | | - | func key_Name_createdAt (name) = key_entity("Name", name, "createdAt") |
---|
35 | | - | |
---|
36 | | - | |
---|
37 | | - | let key_nameTTL = "nameTTL" |
---|
38 | | - | |
---|
39 | | - | let key_controllers = "controllers" |
---|
40 | | - | |
---|
41 | | - | func _setOwner (name,owner) = [StringEntry(key_Name_owner(name), owner)] |
---|
42 | | - | |
---|
43 | | - | |
---|
44 | | - | func _getOwner (name) = getString(this, key_Name_owner(name)) |
---|
45 | | - | |
---|
46 | | - | |
---|
47 | | - | func _setRegistrar (name,registrar) = [StringEntry(key_Name_registrar(name), registrar)] |
---|
48 | | - | |
---|
49 | | - | |
---|
50 | | - | func _getRegistrar (name) = getString(this, key_Name_registrar(name)) |
---|
51 | | - | |
---|
52 | | - | |
---|
53 | | - | func _setResolver (name,resolver) = [StringEntry(key_Name_resolver(name), resolver)] |
---|
54 | | - | |
---|
55 | | - | |
---|
56 | | - | func _getResolver (name) = getString(this, key_Name_resolver(name)) |
---|
57 | | - | |
---|
58 | | - | |
---|
59 | | - | func _getToken (name) = getString(this, key_Name_token(name)) |
---|
60 | | - | |
---|
61 | | - | |
---|
62 | | - | func _getCreatedAt (name) = getInteger(this, key_Name_createdAt(name)) |
---|
63 | | - | |
---|
64 | | - | |
---|
65 | | - | func _getExpiresAt (name) = getInteger(this, key_Name_expiresAt(name)) |
---|
66 | | - | |
---|
67 | | - | |
---|
68 | | - | func _getControllers () = getString(this, key_controllers) |
---|
69 | | - | |
---|
70 | | - | |
---|
71 | | - | let controllers = match _getControllers() { |
---|
72 | | - | case repr: String => |
---|
73 | | - | split(repr, ",") |
---|
74 | | - | case _ => |
---|
75 | | - | nil |
---|
76 | | - | } |
---|
77 | | - | |
---|
78 | | - | let nameTTL = getIntegerOrThrow(this, key_nameTTL) |
---|
79 | | - | |
---|
80 | | - | func isRegisteredName (name) = isDefined(getString(this, key_Name_token(name))) |
---|
81 | | - | |
---|
82 | | - | |
---|
83 | | - | func isCreatedName (name) = isDefined(_getCreatedAt(name)) |
---|
84 | | - | |
---|
85 | | - | |
---|
86 | | - | func isExpiredName (name) = match _getExpiresAt(name) { |
---|
87 | | - | case expiresAt: Int => |
---|
88 | | - | (lastBlock.timestamp > expiresAt) |
---|
89 | | - | case _ => |
---|
90 | | - | false |
---|
91 | | - | } |
---|
92 | | - | |
---|
93 | | - | |
---|
94 | | - | func isActiveName (name) = if (if (isRegisteredName(name)) |
---|
95 | | - | then isCreatedName(name) |
---|
96 | | - | else false) |
---|
97 | | - | then !(isExpiredName(name)) |
---|
98 | | - | else false |
---|
99 | | - | |
---|
100 | | - | |
---|
101 | | - | func isValidName (name) = { |
---|
102 | | - | let symbols = "abcdefghijklmnopqrstuvwxyz1234567890-" |
---|
103 | | - | func validateChars (isValid,char) = if (isValid) |
---|
104 | | - | then contains(symbols, char) |
---|
105 | | - | else false |
---|
106 | | - | |
---|
107 | | - | if (if (if (if (if ((size(name) > 0)) |
---|
108 | | - | then (63 >= size(name)) |
---|
109 | | - | else false) |
---|
110 | | - | then (indexOf(name, "--") != 2) |
---|
111 | | - | else false) |
---|
112 | | - | then (indexOf(name, "-") != 0) |
---|
113 | | - | else false) |
---|
114 | | - | then (lastIndexOf(name, "-") != (size(name) - 1)) |
---|
115 | | - | else false) |
---|
116 | | - | then { |
---|
117 | | - | let $l = split(name, "") |
---|
118 | | - | let $s = size($l) |
---|
119 | | - | let $acc0 = true |
---|
120 | | - | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
121 | | - | then $a |
---|
122 | | - | else validateChars($a, $l[$i]) |
---|
123 | | - | |
---|
124 | | - | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
125 | | - | then $a |
---|
126 | | - | else throw("List size exceeds 63") |
---|
127 | | - | |
---|
128 | | - | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 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) |
---|
129 | | - | } |
---|
130 | | - | else false |
---|
| 27 | + | func isOwner (inv,name) = if ((inv.caller == this)) |
---|
| 28 | + | then true |
---|
| 29 | + | else match invoke(registry, "owner", [name], nil) { |
---|
| 30 | + | case owner: String => |
---|
| 31 | + | (owner == toString(inv.caller)) |
---|
| 32 | + | case _ => |
---|
| 33 | + | false |
---|
134 | | - | func isAvailableName (name) = if (isValidName(name)) |
---|
135 | | - | then if (!(isRegisteredName(name))) |
---|
136 | | - | then true |
---|
137 | | - | else isExpiredName(name) |
---|
138 | | - | else false |
---|
139 | | - | |
---|
140 | | - | |
---|
141 | | - | func isValidAddress (maybeAddress) = isDefined(addressFromString(maybeAddress)) |
---|
142 | | - | |
---|
143 | | - | |
---|
144 | | - | func isAdmin (inv) = (inv.caller == this) |
---|
145 | | - | |
---|
146 | | - | |
---|
147 | | - | func isOwner (inv,name) = (toString(inv.caller) == valueOrElse(_getOwner(name), "")) |
---|
148 | | - | |
---|
149 | | - | |
---|
150 | | - | func isController (address) = containsElement(controllers, address) |
---|
151 | | - | |
---|
152 | | - | |
---|
153 | | - | func _splitLastLabel (name) = { |
---|
154 | | - | let labels = split(name, ".") |
---|
155 | | - | let lastIndex = (size(labels) - 1) |
---|
156 | | - | let lastLabel = labels[lastIndex] |
---|
157 | | - | let restLabels = makeString(removeByIndex(labels, lastIndex), ".") |
---|
158 | | - | $Tuple2(lastLabel, restLabels) |
---|
159 | | - | } |
---|
160 | | - | |
---|
161 | | - | |
---|
162 | | - | func _issueNameToken (name) = [Issue(if ((size(name) > 16)) |
---|
163 | | - | then (take(name, 15) + "~") |
---|
164 | | - | else take(name, 16), (name + ".waves"), 1, 0, false)] |
---|
165 | | - | |
---|
166 | | - | |
---|
167 | | - | func _registerNameWithToken (name,tokenId,createdAt) = [StringEntry(key_Name_token(name), tokenId), StringEntry(key_Token_name(tokenId), name), IntegerEntry(key_Name_createdAt(name), createdAt), IntegerEntry(key_Name_expiresAt(name), (createdAt + nameTTL))] |
---|
168 | | - | |
---|
169 | | - | |
---|
170 | | - | func _restoreNameWithToken (name,tokenId) = [StringEntry(key_Name_token(name), tokenId), StringEntry(key_Token_name(tokenId), name)] |
---|
171 | | - | |
---|
172 | | - | |
---|
173 | | - | func _transferTokenToOwner (tokenId,owner) = [ScriptTransfer(parseAddressOrThrow(owner), 1, fromBase58String(tokenId))] |
---|
174 | | - | |
---|
175 | | - | |
---|
176 | | - | func modifiers (validations) = unit |
---|
177 | | - | |
---|
178 | | - | |
---|
179 | | - | func onlyAdmin (inv) = if (isAdmin(inv)) |
---|
180 | | - | then unit |
---|
181 | | - | else throw("Permission denied") |
---|
182 | | - | |
---|
183 | | - | |
---|
184 | | - | func onlyController (inv) = if (if (isAdmin(inv)) |
---|
185 | | - | then true |
---|
186 | | - | else isController(toString(inv.caller))) |
---|
187 | | - | then unit |
---|
188 | | - | else throw("Permission denied") |
---|
189 | | - | |
---|
190 | | - | |
---|
191 | | - | func onlyOwner (inv,name) = if (if (isAdmin(inv)) |
---|
192 | | - | then true |
---|
193 | | - | else isOwner(inv, name)) |
---|
194 | | - | then unit |
---|
195 | | - | else throw("Permission denied") |
---|
196 | | - | |
---|
197 | | - | |
---|
198 | | - | func _callSupportedInterface (registrant,interfaceId,restLabels) = match invoke(registrant, "supportsInterface", [interfaceId], nil) { |
---|
199 | | - | case supportsInterface: Boolean => |
---|
200 | | - | if (!(supportsInterface)) |
---|
201 | | - | then unit |
---|
202 | | - | else match invoke(registrant, interfaceId, [restLabels], nil) { |
---|
203 | | - | case maybeString: String => |
---|
204 | | - | maybeString |
---|
205 | | - | case maybeInt: Int => |
---|
206 | | - | maybeInt |
---|
207 | | - | case _ => |
---|
208 | | - | unit |
---|
209 | | - | } |
---|
210 | | - | case _ => |
---|
211 | | - | unit |
---|
212 | | - | } |
---|
213 | | - | |
---|
214 | | - | |
---|
228 | | - | func addController (address) = valueOrElse(modifiers([onlyAdmin(inv), if (isValidAddress(address)) |
---|
229 | | - | then unit |
---|
230 | | - | else throw("Controller must be a valid address"), if (!(isController(address))) |
---|
231 | | - | then unit |
---|
232 | | - | else throw((("Controller `" + address) + "` is already registered"))]), [StringEntry(key_controllers, makeString((controllers :+ address), ","))]) |
---|
233 | | - | |
---|
234 | | - | |
---|
235 | | - | |
---|
236 | | - | @Callable(inv) |
---|
237 | | - | func removeController (address) = valueOrElse(modifiers([onlyAdmin(inv), if (isController(address)) |
---|
238 | | - | then unit |
---|
239 | | - | else throw((("Controller `" + address) + "` is not registered"))]), { |
---|
240 | | - | let controllerIndex = value(indexOf(controllers, address)) |
---|
241 | | - | [StringEntry(key_controllers, makeString(removeByIndex(controllers, controllerIndex), ","))] |
---|
242 | | - | }) |
---|
243 | | - | |
---|
244 | | - | |
---|
245 | | - | |
---|
246 | | - | @Callable(inv) |
---|
247 | | - | func validate (name) = $Tuple2(nil, isValidName(name)) |
---|
248 | | - | |
---|
249 | | - | |
---|
250 | | - | |
---|
251 | | - | @Callable(inv) |
---|
252 | | - | func available (name) = $Tuple2(nil, isAvailableName(name)) |
---|
253 | | - | |
---|
254 | | - | |
---|
255 | | - | |
---|
256 | | - | @Callable(inv) |
---|
257 | | - | func owner (name) = { |
---|
258 | | - | let $t079157962 = _splitLastLabel(name) |
---|
259 | | - | let label = $t079157962._1 |
---|
260 | | - | let restLabels = $t079157962._2 |
---|
261 | | - | let ownerAddress = _getOwner(label) |
---|
262 | | - | let registrarAddress = _getRegistrar(label) |
---|
263 | | - | let registrar = value(addressFromString(value(registrarAddress))) |
---|
264 | | - | $Tuple2(nil, if (if ((size(restLabels) == 0)) |
---|
265 | | - | then true |
---|
266 | | - | else !(isDefined(registrarAddress))) |
---|
267 | | - | then ownerAddress |
---|
268 | | - | else match _callSupportedInterface(registrar, "owner", restLabels) { |
---|
269 | | - | case maybeOwner: String => |
---|
270 | | - | if (isValidAddress(maybeOwner)) |
---|
271 | | - | then maybeOwner |
---|
272 | | - | else unit |
---|
273 | | - | case _ => |
---|
274 | | - | unit |
---|
275 | | - | }) |
---|
| 48 | + | func register (subName,owner) = { |
---|
| 49 | + | let currentOwner = _getOwner(subName) |
---|
| 50 | + | if (isDefined(currentOwner)) |
---|
| 51 | + | then throw((("`" + subName) + "` already registered")) |
---|
| 52 | + | else _setOwner(subName, owner) |
---|
281 | | - | func registrar (name) = { |
---|
282 | | - | let $t085918638 = _splitLastLabel(name) |
---|
283 | | - | let label = $t085918638._1 |
---|
284 | | - | let restLabels = $t085918638._2 |
---|
285 | | - | let registrarAddress = match _getRegistrar(label) { |
---|
286 | | - | case maybeRegistrar: String => |
---|
287 | | - | if (isValidAddress(maybeRegistrar)) |
---|
288 | | - | then maybeRegistrar |
---|
289 | | - | else unit |
---|
290 | | - | case _ => |
---|
291 | | - | unit |
---|
292 | | - | } |
---|
293 | | - | let registrar = value(addressFromString(value(registrarAddress))) |
---|
294 | | - | $Tuple2(nil, if (if ((size(restLabels) == 0)) |
---|
295 | | - | then true |
---|
296 | | - | else !(isDefined(registrarAddress))) |
---|
297 | | - | then registrarAddress |
---|
298 | | - | else match _callSupportedInterface(registrar, "registrar", restLabels) { |
---|
299 | | - | case maybeRegistrar: String => |
---|
300 | | - | if (isValidAddress(maybeRegistrar)) |
---|
301 | | - | then maybeRegistrar |
---|
302 | | - | else unit |
---|
303 | | - | case _ => |
---|
304 | | - | unit |
---|
305 | | - | }) |
---|
306 | | - | } |
---|
| 58 | + | func supportsInterface (interfaceId) = $Tuple2(nil, containsElement(["addr", "owner", "registrar"], interfaceId)) |
---|
320 | | - | func resolver (name) = $Tuple2(nil, _getResolver(_splitLastLabel(name)._1)) |
---|
321 | | - | |
---|
322 | | - | |
---|
323 | | - | |
---|
324 | | - | @Callable(inv) |
---|
325 | | - | func setResolver (name,resolver) = valueOrElse(modifiers([if (isActiveName(name)) |
---|
326 | | - | then unit |
---|
327 | | - | else throw("Name expired or not registered"), onlyOwner(inv, name), if (isValidAddress(resolver)) |
---|
328 | | - | then unit |
---|
329 | | - | else throw("Resolver must be a valid contract address")]), _setResolver(name, resolver)) |
---|
330 | | - | |
---|
331 | | - | |
---|
332 | | - | |
---|
333 | | - | @Callable(inv) |
---|
334 | | - | func nameCreated (name) = $Tuple2(nil, _getCreatedAt(_splitLastLabel(name)._1)) |
---|
335 | | - | |
---|
336 | | - | |
---|
337 | | - | |
---|
338 | | - | @Callable(inv) |
---|
339 | | - | func nameExpires (name) = $Tuple2(nil, _getExpiresAt(_splitLastLabel(name)._1)) |
---|
340 | | - | |
---|
341 | | - | |
---|
342 | | - | |
---|
343 | | - | @Callable(inv) |
---|
344 | | - | func register (name,owner,createdAt) = valueOrElse(modifiers([onlyController(inv), if (isAvailableName(name)) |
---|
345 | | - | then unit |
---|
346 | | - | else throw((("`" + name) + "` cannot be registered")), if (isValidAddress(owner)) |
---|
347 | | - | then unit |
---|
348 | | - | else throw("Owner must be a valid address"), if ((createdAt > 0)) |
---|
349 | | - | then unit |
---|
350 | | - | else throw("CreatedAt cannot be negative"), if ((lastBlock.timestamp >= createdAt)) |
---|
351 | | - | then unit |
---|
352 | | - | else throw("CreatedAt cannot be in the future")]), { |
---|
353 | | - | let issueNameToken = _issueNameToken(name) |
---|
354 | | - | let tokenId = calculateAssetId(issueNameToken[0]) |
---|
355 | | - | let tokenStr = toBase58String(tokenId) |
---|
356 | | - | let registerNameWithToken = _registerNameWithToken(name, tokenStr, createdAt) |
---|
357 | | - | let transferTokenToOwner = _transferTokenToOwner(tokenStr, owner) |
---|
358 | | - | let setOwnership = _setOwner(name, owner) |
---|
359 | | - | (((issueNameToken ++ registerNameWithToken) ++ transferTokenToOwner) ++ setOwnership) |
---|
360 | | - | }) |
---|
361 | | - | |
---|
362 | | - | |
---|
363 | | - | |
---|
364 | | - | @Callable(inv) |
---|
365 | | - | func reclaim (name) = valueOrElse(modifiers([if (isActiveName(name)) |
---|
366 | | - | then unit |
---|
367 | | - | else throw((("`" + name) + "` is not active"))]), { |
---|
368 | | - | let newOwner = inv.caller |
---|
369 | | - | let newOwnerAddress = toString(newOwner) |
---|
370 | | - | let tokenStr = valueOrErrorMessage(_getToken(name), (("No token is issued for `" + name) + "`")) |
---|
371 | | - | let tokenId = fromBase58String(tokenStr) |
---|
372 | | - | let hasToken = (assetBalance(newOwner, tokenId) == 1) |
---|
373 | | - | let isTokenOwner = isOwner(inv, name) |
---|
374 | | - | let isTokenExists = match assetInfo(tokenId) { |
---|
375 | | - | case token: Asset => |
---|
376 | | - | (token.quantity == 1) |
---|
377 | | - | case _ => |
---|
378 | | - | false |
---|
379 | | - | } |
---|
380 | | - | if (if (isTokenOwner) |
---|
381 | | - | then !(isTokenExists) |
---|
382 | | - | else false) |
---|
383 | | - | then { |
---|
384 | | - | let issueNewToken = _issueNameToken(name) |
---|
385 | | - | let newTokenId = calculateAssetId(issueNewToken[0]) |
---|
386 | | - | let newTokenStr = toBase58String(newTokenId) |
---|
387 | | - | let restoreNameWithNewToken = _restoreNameWithToken(name, newTokenStr) |
---|
388 | | - | let transferNewTokenToOwner = _transferTokenToOwner(newTokenStr, newOwnerAddress) |
---|
389 | | - | ((issueNewToken ++ restoreNameWithNewToken) ++ transferNewTokenToOwner) |
---|
390 | | - | } |
---|
391 | | - | else if (if (isTokenOwner) |
---|
392 | | - | then hasToken |
---|
393 | | - | else false) |
---|
394 | | - | then throw("You already own a name token") |
---|
395 | | - | else if (!(hasToken)) |
---|
396 | | - | then throw("You don't have a name token") |
---|
397 | | - | else _setOwner(name, newOwnerAddress) |
---|
398 | | - | }) |
---|
| 70 | + | func addr (name) = $Tuple2(nil, getString(this, key_address(name))) |
---|