users.gno

1.17 Kb ยท 50 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	if !std.PreviousRealm().IsUser() {
28		panic(ErrNonUserCall)
29	}
30
31	if paused {
32		panic(ErrPaused)
33	}
34
35	if std.OriginSend().AmountOf("ugnot") != registerPrice {
36		panic(ErrInvalidPayment)
37	}
38
39	if matched := reUsername.MatchString(username); !matched {
40		panic(ErrInvalidUsername)
41	}
42
43	registrant := std.PreviousRealm().Address()
44	if err := susers.RegisterUser(username, registrant); err != nil {
45		panic(err)
46	}
47
48	latestUsers.Append(username)
49	std.Emit("Registeration", "address", registrant.String(), "name", username)
50}