tendermint/permission/types/snatives.go

176 lines
4.0 KiB
Go
Raw Normal View History

2015-06-19 23:53:21 -07:00
package types
2015-07-20 11:24:57 -07:00
import (
"fmt"
"github.com/tendermint/tendermint/binary"
)
//---------------------------------------------------------------------------------------------------
// snative permissions
2015-06-19 23:53:21 -07:00
const (
// first 32 bits of BasePermission are for chain, second 32 are for snative
FirstSNativePermFlag PermFlag = 1 << 32
2015-06-19 23:53:21 -07:00
)
2015-07-21 10:05:08 -07:00
// we need to reset iota with new const block
2015-06-19 23:53:21 -07:00
const (
// each snative has an associated permission flag
2015-07-20 11:24:57 -07:00
HasBase PermFlag = FirstSNativePermFlag << iota
SetBase
UnsetBase
SetGlobal
2015-06-19 23:53:21 -07:00
HasRole
AddRole
RmRole
2015-07-21 10:05:08 -07:00
NumSNativePermissions uint = 7 // NOTE adjust this too
2015-06-19 23:53:21 -07:00
TopSNativePermFlag PermFlag = FirstSNativePermFlag << (NumSNativePermissions - 1)
AllSNativePermFlags PermFlag = (TopSNativePermFlag | (TopSNativePermFlag - 1)) &^ (FirstSNativePermFlag - 1)
2015-06-19 23:53:21 -07:00
)
2015-07-20 11:24:57 -07:00
//---------------------------------------------------------------------------------------------------
// snative tx interface and argument encoding
2015-07-21 10:05:08 -07:00
// SNativesArgs are a registered interface in the SNativeTx,
// so binary handles the arguments and each snative gets a type-byte
// PermFlag() maps the type-byte to the permission
// The account sending the SNativeTx must have this PermFlag set
2015-07-20 11:24:57 -07:00
type SNativeArgs interface {
PermFlag() PermFlag
}
const (
SNativeArgsTypeHasBase = byte(0x01)
SNativeArgsTypeSetBase = byte(0x02)
SNativeArgsTypeUnsetBase = byte(0x03)
SNativeArgsTypeSetGlobal = byte(0x04)
2015-07-21 10:05:08 -07:00
SNativeArgsTypeHasRole = byte(0x05)
SNativeArgsTypeAddRole = byte(0x06)
SNativeArgsTypeRmRole = byte(0x07)
2015-07-20 11:24:57 -07:00
)
// for binary.readReflect
var _ = binary.RegisterInterface(
struct{ SNativeArgs }{},
binary.ConcreteType{&HasBaseArgs{}, SNativeArgsTypeHasBase},
binary.ConcreteType{&SetBaseArgs{}, SNativeArgsTypeSetBase},
binary.ConcreteType{&UnsetBaseArgs{}, SNativeArgsTypeUnsetBase},
binary.ConcreteType{&SetGlobalArgs{}, SNativeArgsTypeSetGlobal},
binary.ConcreteType{&HasRoleArgs{}, SNativeArgsTypeHasRole},
binary.ConcreteType{&AddRoleArgs{}, SNativeArgsTypeAddRole},
binary.ConcreteType{&RmRoleArgs{}, SNativeArgsTypeRmRole},
)
type HasBaseArgs struct {
2015-07-21 11:29:31 -07:00
Address []byte `json:"address"`
Permission PermFlag `json:"permission"`
2015-07-20 11:24:57 -07:00
}
func (*HasBaseArgs) PermFlag() PermFlag {
return HasBase
}
type SetBaseArgs struct {
2015-07-21 11:29:31 -07:00
Address []byte `json:"address"`
Permission PermFlag `json:"permission"`
Value bool `json:"value"`
2015-07-20 11:24:57 -07:00
}
func (*SetBaseArgs) PermFlag() PermFlag {
return SetBase
}
type UnsetBaseArgs struct {
2015-07-21 11:29:31 -07:00
Address []byte `json:"address"`
Permission PermFlag `json:"permission"`
2015-07-20 11:24:57 -07:00
}
func (*UnsetBaseArgs) PermFlag() PermFlag {
return UnsetBase
}
type SetGlobalArgs struct {
2015-07-21 11:29:31 -07:00
Permission PermFlag `json:"permission"`
Value bool `json:"value"`
2015-07-20 11:24:57 -07:00
}
func (*SetGlobalArgs) PermFlag() PermFlag {
return SetGlobal
}
type HasRoleArgs struct {
2015-07-21 11:29:31 -07:00
Address []byte `json:"address"`
Role string `json:"role"`
2015-07-20 11:24:57 -07:00
}
func (*HasRoleArgs) PermFlag() PermFlag {
return HasRole
}
type AddRoleArgs struct {
2015-07-21 11:29:31 -07:00
Address []byte `json:"address"`
Role string `json:"role"`
2015-07-20 11:24:57 -07:00
}
func (*AddRoleArgs) PermFlag() PermFlag {
return AddRole
}
type RmRoleArgs struct {
2015-07-21 11:29:31 -07:00
Address []byte `json:"address"`
Role string `json:"role"`
2015-07-20 11:24:57 -07:00
}
func (*RmRoleArgs) PermFlag() PermFlag {
return RmRole
}
//------------------------------------------------------------
// string utilities
func SNativePermFlagToString(pF PermFlag) (perm string) {
switch pF {
case HasBase:
perm = "HasBase"
case SetBase:
perm = "SetBase"
case UnsetBase:
perm = "UnsetBase"
case SetGlobal:
perm = "SetGlobal"
case HasRole:
perm = "HasRole"
case AddRole:
perm = "AddRole"
case RmRole:
perm = "RmRole"
default:
perm = "#-UNKNOWN-#"
}
return
}
func SNativeStringToPermFlag(perm string) (pF PermFlag, err error) {
switch perm {
case "HasBase":
pF = HasBase
case "SetBase":
pF = SetBase
case "UnsetBase":
pF = UnsetBase
case "SetGlobal":
pF = SetGlobal
case "HasRole":
pF = HasRole
case "AddRole":
pF = AddRole
case "RmRole":
pF = RmRole
default:
err = fmt.Errorf("Unknown permission %s", perm)
}
return
}