whitelist.gno

1.24 Kb ยท 50 lines
 1package agent
 2
 3import "gno.land/p/demo/avl"
 4
 5// Whitelist manages whitelisted agent addresses.
 6type Whitelist struct {
 7	store *avl.Tree
 8}
 9
10// ClearAddresses removes all addresses from the whitelist and puts into a state
11// that indicates it is moot and has no whitelist defined.
12func (m *Whitelist) ClearAddresses() {
13	m.store = nil
14}
15
16// AddAddresses adds the given addresses to the whitelist.
17func (m *Whitelist) AddAddresses(addresses []string) {
18	if m.store == nil {
19		m.store = avl.NewTree()
20	}
21
22	for _, address_XXX := range addresses {
23		m.store.Set(address_XXX, struct{}{})
24	}
25}
26
27// RemoveAddress removes the given address from the whitelist if it exists.
28func (m *Whitelist) RemoveAddress(address_XXX string) {
29	if m.store == nil {
30		return
31	}
32
33	m.store.Remove(address_XXX)
34}
35
36// HasDefinition returns true if the whitelist has a definition. It retuns false if
37// `ClearAddresses` has been called without any subsequent `AddAddresses` calls, or
38// if `AddAddresses` has never been called.
39func (m Whitelist) HasDefinition() bool {
40	return m.store != nil
41}
42
43// HasAddress returns true if the given address is in the whitelist.
44func (m Whitelist) HasAddress(address_XXX string) bool {
45	if m.store == nil {
46		return false
47	}
48
49	return m.store.Has(address_XXX)
50}