Search Apps Documentation Source Content File Folder Download Copy Actions Download

verifier.gno

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