github.com/aergoio/aergo@v1.3.1/contract/enterprise/admin.go (about)

     1  package enterprise
     2  
     3  import (
     4  	"bytes"
     5  
     6  	"github.com/aergoio/aergo/state"
     7  	"github.com/aergoio/aergo/types"
     8  )
     9  
    10  const AdminsKey = "ADMINS"
    11  
    12  func GetAdmin(r AccountStateReader) (*types.EnterpriseConfig, error) {
    13  	scs, err := r.GetEnterpriseAccountState()
    14  	if err != nil {
    15  		return nil, err
    16  	}
    17  	admins, err := getAdmins(scs)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  	ret := &types.EnterpriseConfig{Key: AdminsKey, On: false}
    22  	if admins != nil {
    23  		ret.On = true
    24  		for _, admin := range admins {
    25  			ret.Values = append(ret.Values, types.EncodeAddress(admin))
    26  		}
    27  	}
    28  	return ret, nil
    29  }
    30  func setAdmins(scs *state.ContractState, addresses [][]byte) error {
    31  	return scs.SetData([]byte(AdminsKey), bytes.Join(addresses, []byte("")))
    32  }
    33  
    34  func getAdmins(scs *state.ContractState) ([][]byte, error) {
    35  	data, err := scs.GetData([]byte(AdminsKey))
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	var results [][]byte
    40  	for i := 0; i < len(data); i += types.AddressLength {
    41  		results = append(results, data[i:i+types.AddressLength])
    42  	}
    43  	return results, nil
    44  }