verifier.gno

2.00 Kb ยท 70 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 std.Address, namespace string) bool {
23	return verifier(enabled, address, 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() {
30	crossing()
31	if err := Ownable.DropOwnershipByPrevious(); err != nil {
32		panic(err)
33	}
34	enabled = true
35}
36
37func IsEnabled() bool {
38	crossing()
39	return enabled
40}
41
42// verifier checks the store to see that the
43// user has properly registered a given name/namespace.
44// This function considers as valid an `address` that matches the `namespace` (PA namespaces)
45func verifier(enabled bool, address std.Address, namespace string) bool {
46	if !enabled {
47		return true // only in pre-genesis cases
48	}
49
50	if strings.TrimSpace(address.String()) == "" || strings.TrimSpace(namespace) == "" {
51		return false
52	}
53
54	// Allow user with their own address as namespace
55	// This enables pseudo-anon namespaces
56	// ie gno.land/{p,r}/{ADDRESS}/**
57	if address.String() == namespace {
58		return true
59	}
60
61	// Can be a registered namespace or an alias
62	userData, _ := users.ResolveName(namespace)
63	if userData == nil || userData.IsDeleted() {
64		return false
65	}
66
67	/// XXX: add check for r/sys/teams down the line
68
69	return userData.Addr() == address
70}