github.com/lino-network/lino@v0.6.11/types/common.go (about)

     1  package types
     2  
     3  import (
     4  	"regexp"
     5  
     6  	sdk "github.com/cosmos/cosmos-sdk/types"
     7  )
     8  
     9  // AccountKey key format in KVStore
    10  type AccountKey string
    11  
    12  // PoolName is the name of a lino pool.
    13  type PoolName string
    14  
    15  // Permlink key format in KVStore
    16  type Permlink string
    17  
    18  // ProposalKey key format in KVStore
    19  type ProposalKey string
    20  
    21  // user permission type to present different permission for different msg
    22  type Permission int
    23  
    24  // msg CapacityLevel, different level cost different user capacity
    25  type CapacityLevel int
    26  
    27  // indicates the current proposal status
    28  type ProposalResult int
    29  
    30  // indicates proposal type
    31  type ProposalType int
    32  
    33  // indicates donation type
    34  type DonationType int
    35  
    36  // indicates all possible balance behavior types
    37  type TransferDetailType int
    38  
    39  // indicates the type of punishment for oncall validators
    40  type PunishType int
    41  
    42  // AccOrAddr is either an address or an account key.
    43  type AccOrAddr struct {
    44  	AccountKey AccountKey     `json:"account_key,omitempty"`
    45  	Addr       sdk.AccAddress `json:"addr,omitempty"`
    46  	IsAddr     bool           `json:"is_addr,omitempty"`
    47  }
    48  
    49  func NewAccOrAddrFromAcc(acc AccountKey) AccOrAddr {
    50  	return AccOrAddr{
    51  		AccountKey: acc,
    52  	}
    53  }
    54  
    55  func NewAccOrAddrFromAddr(addr sdk.AccAddress) AccOrAddr {
    56  	return AccOrAddr{
    57  		Addr:   addr,
    58  		IsAddr: true,
    59  	}
    60  }
    61  
    62  func (a AccOrAddr) IsValid() bool {
    63  	if a.IsAddr {
    64  		return sdk.VerifyAddressFormat(a.Addr) == nil
    65  	}
    66  	return a.AccountKey.IsValid()
    67  }
    68  
    69  func (a AccOrAddr) String() string {
    70  	if a.IsAddr {
    71  		return a.Addr.String()
    72  	}
    73  	return string(a.AccountKey)
    74  }
    75  
    76  // GetPermlink try to generate Permlink from AccountKey and PostID
    77  func GetPermlink(author AccountKey, postID string) Permlink {
    78  	return Permlink(string(author) + PermlinkSeparator + postID)
    79  }
    80  
    81  // FindAccountInList - find AccountKey in given AccountKey list
    82  func FindAccountInList(me AccountKey, lst []AccountKey) int {
    83  	for index, user := range lst {
    84  		if user == me {
    85  			return index
    86  		}
    87  	}
    88  	return -1
    89  }
    90  
    91  func AccountListToSet(lst []AccountKey) map[AccountKey]bool {
    92  	rst := make(map[AccountKey]bool)
    93  	for _, acc := range lst {
    94  		rst[acc] = true
    95  	}
    96  	return rst
    97  }
    98  
    99  func (ak AccountKey) IsValid() bool {
   100  	if len(ak) < MinimumUsernameLength ||
   101  		len(ak) > MaximumUsernameLength {
   102  		return false
   103  	}
   104  
   105  	match, err := regexp.MatchString(UsernameReCheck, string(ak))
   106  	if !match || err != nil {
   107  		return false
   108  	}
   109  
   110  	match, err = regexp.MatchString(IllegalUsernameReCheck, string(ak))
   111  	if match || err != nil {
   112  		return false
   113  	}
   114  	return true
   115  }