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