github.com/datachainlab/burrow@v0.25.0/permission/perm_flag.go (about)

     1  package permission
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  // Base permission references are like unix (the index is already bit shifted)
     9  const (
    10  	// Chain permissions.
    11  	// These permissions grant the ability for accounts to perform certain transition within the execution package
    12  	// Root is a reserved permission currently unused that may be used in the future to grant super-user privileges
    13  	// for instance to a governance contract
    14  	Root PermFlag = 1 << iota // 1
    15  	// Send permits an account to issue a SendTx to transfer value from one account to another. Note that value can
    16  	// still be transferred with a CallTx by specifying an Amount in the InputTx. Funding an account is the basic
    17  	// prerequisite for an account to act in the system so is often used as a surrogate for 'account creation' when
    18  	// sending to a unknown account - in order for this to be permitted the input account needs the CreateAccount
    19  	// permission in addition.
    20  	Send // 2
    21  	// Call permits and account to issue a CallTx, which can be used to call (run) the code of an existing
    22  	// account/contract (these are synonymous in Burrow/EVM). A CallTx can be used to create an account if it points to
    23  	// a nil address - in order for an account to be permitted to do this the input (calling) account needs the
    24  	// CreateContract permission in addition.
    25  	Call // 4
    26  	// CreateContract permits the input account of a CallTx to create a new contract/account when CallTx.Address is nil
    27  	// and permits an executing contract in the EVM to create a new contract programmatically.
    28  	CreateContract // 8
    29  	// CreateAccount permits an input account of a SendTx to add value to non-existing (unfunded) accounts
    30  	CreateAccount // 16
    31  	// Bond is a reserved permission for making changes to the validator set - currently unused
    32  	Bond // 32
    33  	// Name permits manipulation of the name registry by allowing an account to issue a NameTx
    34  	Name // 64
    35  	// Propose permits creating proposals and voting for them
    36  	Proposal // 128
    37  	// Input allows account to sign transactions
    38  	Input // 256
    39  	// Permission to execute batch transactins
    40  	Batch // 512
    41  
    42  	// Moderator permissions.
    43  	// These permissions concern the alteration of the chain permissions listed above. Each permission relates to a
    44  	// particular canonical permission mutation or query function. When an account is granted a moderation permission
    45  	// it is permitted to call that function. See snative.go for a marked-up description of what each function does.
    46  	HasBase
    47  	SetBase
    48  	UnsetBase
    49  	SetGlobal
    50  	HasRole
    51  	AddRole
    52  	RemoveRole
    53  
    54  	NumPermissions uint = 17 // NOTE Adjust this too. We can support upto 64
    55  
    56  	TopPermFlag      PermFlag = 1 << (NumPermissions - 1)
    57  	AllPermFlags     PermFlag = TopPermFlag | (TopPermFlag - 1)
    58  	DefaultPermFlags PermFlag = Send | Call | CreateContract | CreateAccount | Bond | Name | HasBase | HasRole | Proposal | Input | Batch
    59  
    60  	// Chain permissions strings
    61  	RootString           string = "root"
    62  	SendString                  = "send"
    63  	CallString                  = "call"
    64  	CreateContractString        = "createContract"
    65  	CreateAccountString         = "createAccount"
    66  	BondString                  = "bond"
    67  	NameString                  = "name"
    68  	ProposalString              = "proposal"
    69  	InputString                 = "input"
    70  	BatchString                 = "batch"
    71  
    72  	// Moderator permissions strings
    73  	HasBaseString    = "hasBase"
    74  	SetBaseString    = "setBase"
    75  	UnsetBaseString  = "unsetBase"
    76  	SetGlobalString  = "setGlobal"
    77  	HasRoleString    = "hasRole"
    78  	AddRoleString    = "addRole"
    79  	RemoveRoleString = "removeRole"
    80  	UnknownString    = "#-UNKNOWN-#"
    81  
    82  	AllString = "all"
    83  )
    84  
    85  // A particular permission
    86  type PermFlag uint64
    87  
    88  // Checks if a permission flag is valid (a known base chain or snative permission)
    89  func (pf PermFlag) IsValid() bool {
    90  	return pf <= AllPermFlags
    91  }
    92  
    93  // Returns the string name of a single bit non-composite PermFlag, or otherwise UnknownString
    94  // See BasePermissionsToStringList to generate a string representation of a composite PermFlag
    95  func (pf PermFlag) String() string {
    96  	switch pf {
    97  	case AllPermFlags:
    98  		return AllString
    99  	case Root:
   100  		return RootString
   101  	case Send:
   102  		return SendString
   103  	case Call:
   104  		return CallString
   105  	case CreateContract:
   106  		return CreateContractString
   107  	case CreateAccount:
   108  		return CreateAccountString
   109  	case Bond:
   110  		return BondString
   111  	case Name:
   112  		return NameString
   113  	case Proposal:
   114  		return ProposalString
   115  	case Input:
   116  		return InputString
   117  	case Batch:
   118  		return BatchString
   119  	case HasBase:
   120  		return HasBaseString
   121  	case SetBase:
   122  		return SetBaseString
   123  	case UnsetBase:
   124  		return UnsetBaseString
   125  	case SetGlobal:
   126  		return SetGlobalString
   127  	case HasRole:
   128  		return HasRoleString
   129  	case AddRole:
   130  		return AddRoleString
   131  	case RemoveRole:
   132  		return RemoveRoleString
   133  	default:
   134  		return UnknownString
   135  	}
   136  }
   137  
   138  // PermStringToFlag maps camel- and snake case strings to the
   139  // the corresponding permission flag.
   140  func PermStringToFlag(perm string) (PermFlag, error) {
   141  	switch strings.ToLower(perm) {
   142  	case AllString:
   143  		return AllPermFlags, nil
   144  	case RootString:
   145  		return Root, nil
   146  	case SendString:
   147  		return Send, nil
   148  	case CallString:
   149  		return Call, nil
   150  	case CreateContractString, "createcontract", "create_contract":
   151  		return CreateContract, nil
   152  	case CreateAccountString, "createaccount", "create_account":
   153  		return CreateAccount, nil
   154  	case BondString:
   155  		return Bond, nil
   156  	case NameString:
   157  		return Name, nil
   158  	case ProposalString:
   159  		return Proposal, nil
   160  	case InputString:
   161  		return Input, nil
   162  	case BatchString:
   163  		return Batch, nil
   164  	case HasBaseString, "hasbase", "has_base":
   165  		return HasBase, nil
   166  	case SetBaseString, "setbase", "set_base":
   167  		return SetBase, nil
   168  	case UnsetBaseString, "unsetbase", "unset_base":
   169  		return UnsetBase, nil
   170  	case SetGlobalString, "setglobal", "set_global":
   171  		return SetGlobal, nil
   172  	case HasRoleString, "hasrole", "has_role":
   173  		return HasRole, nil
   174  	case AddRoleString, "addrole", "add_role":
   175  		return AddRole, nil
   176  	case RemoveRoleString, "removerole", "rmrole", "rm_role":
   177  		return RemoveRole, nil
   178  	default:
   179  		return 0, fmt.Errorf("unknown permission %s", perm)
   180  	}
   181  }