verifier.gno
1.44 Kb · 46 lines
1// Package names enforces namespace permissions for package deployment.
2// Only address-prefix (PA) namespaces are allowed.
3package names
4
5import "gno.land/p/nt/ownable/v0"
6
7var (
8 Ownable = ownable.NewWithAddressByPrevious("g1edq4dugw0sgat4zxcw9xardvuydqf6cgleuc8p") // genesis deployer — dropped in genesis via Enable.
9 enabled = false
10)
11
12// IsAuthorizedAddressForNamespace checks if the given address can deploy to the given namespace.
13// Only the address's own PA namespace is permitted.
14func IsAuthorizedAddressForNamespace(address_XXX address, namespace string) bool {
15 return verifier(enabled, address_XXX, namespace)
16}
17
18// Enable enables the namespace check and drops centralized ownership of this realm.
19// The namespace check is disabled initially to ease txtar and other testing contexts,
20// but this function is meant to be called in the genesis of a chain.
21func Enable(cur realm) {
22 if err := Ownable.DropOwnership(); err != nil {
23 panic(err)
24 }
25 enabled = true
26}
27
28func IsEnabled() bool {
29 return enabled
30}
31
32// verifier checks namespace deployment permissions.
33// An address matching the namespace is the only allowed case.
34func verifier(isEnabled bool, address_XXX address, namespace string) bool {
35 if !isEnabled {
36 return true // only in pre-genesis cases
37 }
38
39 if namespace == "" || !address_XXX.IsValid() {
40 return false
41 }
42
43 // Allow user with their own address as namespace
44 // ie gno.land/{p,r}/{ADDRESS}/**
45 return address_XXX.String() == namespace
46}