github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/p/demo/gnorkle/agent/whitelist.gno (about)

     1  package agent
     2  
     3  import "gno.land/p/demo/avl"
     4  
     5  // Whitelist manages whitelisted agent addresses.
     6  type 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.
    12  func (m *Whitelist) ClearAddresses() {
    13  	m.store = nil
    14  }
    15  
    16  // AddAddresses adds the given addresses to the whitelist.
    17  func (m *Whitelist) AddAddresses(addresses []string) {
    18  	if m.store == nil {
    19  		m.store = avl.NewTree()
    20  	}
    21  
    22  	for _, address := range addresses {
    23  		m.store.Set(address, struct{}{})
    24  	}
    25  }
    26  
    27  // RemoveAddress removes the given address from the whitelist if it exists.
    28  func (m *Whitelist) RemoveAddress(address string) {
    29  	if m.store == nil {
    30  		return
    31  	}
    32  
    33  	m.store.Remove(address)
    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.
    39  func (m Whitelist) HasDefinition() bool {
    40  	return m.store != nil
    41  }
    42  
    43  // HasAddress returns true if the given address is in the whitelist.
    44  func (m Whitelist) HasAddress(address string) bool {
    45  	if m.store == nil {
    46  		return false
    47  	}
    48  
    49  	return m.store.Has(address)
    50  }