users.gno
1.19 Kb ยท 52 lines
1package users
2
3import (
4 "regexp"
5 "std"
6
7 "gno.land/p/moul/fifo"
8 susers "gno.land/r/sys/users"
9)
10
11const (
12 reValidUsername = "^[a-z]{3}[_a-z0-9]{0,14}[0-9]{3}$"
13)
14
15var (
16 registerPrice = int64(1_000_000) // 1 GNOT
17 latestUsers = fifo.New(10) // Save the latest 10 users for rendering purposes
18 reUsername = regexp.MustCompile(reValidUsername)
19)
20
21// Register registers a new username for the caller.
22// A valid username must start with a minimum of 3 letters,
23// end with a minimum of 3 numbers, and be less than 20 chars long.
24// All letters must be lowercase, and the only valid special char is `_`.
25// Only calls from EOAs are supported.
26func Register(username string) {
27 crossing()
28
29 if !std.PreviousRealm().IsUser() {
30 panic(ErrNonUserCall)
31 }
32
33 if paused {
34 panic(ErrPaused)
35 }
36
37 if std.OriginSend().AmountOf("ugnot") != registerPrice {
38 panic(ErrInvalidPayment)
39 }
40
41 if matched := reUsername.MatchString(username); !matched {
42 panic(ErrInvalidUsername)
43 }
44
45 registrant := std.PreviousRealm().Address()
46 if err := cross(susers.RegisterUser)(username, registrant); err != nil {
47 panic(err)
48 }
49
50 latestUsers.Append(username)
51 std.Emit("Registeration", "address", registrant.String(), "name", username)
52}