verifier.gno
2.01 Kb ยท 68 lines
1// Package names provides functionality for checking of package deployments
2// by users registered in r/sys/users are done to proper namespaces.
3package names
4
5import (
6 "std"
7 "strings"
8
9 "gno.land/p/demo/ownable"
10
11 "gno.land/r/sys/users"
12)
13
14var (
15 Ownable = ownable.NewWithAddress("g1manfred47kzduec920z88wfr64ylksmdcedlf5") // dropped in genesis via Enable. XXX We should switch to something better once the GovDAO situation is stabilized.
16
17 enabled = false
18)
19
20// IsAuthorizedAddressForNamespace ensures that the given address has ownership of the given name.
21// A user's name found in r/sys/users is equivalent to their namespace.
22func IsAuthorizedAddressForNamespace(address_XXX std.Address, namespace string) bool {
23 return verifier(enabled, address_XXX, namespace)
24}
25
26// Enable enables the namespace check and drops centralized ownership of this realm.
27// The namespace check is disabled initially to ease txtar and other testing contexts,
28// but this function is meant to be called in the genesis of a chain.
29func Enable(cur realm) {
30 if err := Ownable.DropOwnershipByPrevious(); err != nil {
31 panic(err)
32 }
33 enabled = true
34}
35
36func IsEnabled() bool {
37 return enabled
38}
39
40// verifier checks the store to see that the
41// user has properly registered a given name/namespace.
42// This function considers as valid an `address` that matches the `namespace` (PA namespaces)
43func verifier(enabled bool, address_XXX std.Address, namespace string) bool {
44 if !enabled {
45 return true // only in pre-genesis cases
46 }
47
48 if strings.TrimSpace(address_XXX.String()) == "" || strings.TrimSpace(namespace) == "" {
49 return false
50 }
51
52 // Allow user with their own address as namespace
53 // This enables pseudo-anon namespaces
54 // ie gno.land/{p,r}/{ADDRESS}/**
55 if address_XXX.String() == namespace {
56 return true
57 }
58
59 // Can be a registered namespace or an alias
60 userData, _ := users.ResolveName(namespace)
61 if userData == nil || userData.IsDeleted() {
62 return false
63 }
64
65 /// XXX: add check for r/sys/teams down the line
66
67 return userData.Addr() == address_XXX
68}