-
Notifications
You must be signed in to change notification settings - Fork 44
/
keys.go
94 lines (75 loc) · 2.54 KB
/
keys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package types
import (
"fmt"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/bnb-chain/greenfield/types/resource"
)
const (
// ModuleName defines the module name
ModuleName = "permission"
// StoreKey defines the primary module store key
StoreKey = ModuleName
// RouterKey defines the module's message routing key
RouterKey = ModuleName
// MemStoreKey defines the in-memory store key
MemStoreKey = "mem_permission"
)
var (
ParamsKey = []byte{0x01}
BucketPolicyForAccountPrefix = []byte{0x11}
ObjectPolicyForAccountPrefix = []byte{0x12}
GroupPolicyForAccountPrefix = []byte{0x13}
GroupMemberPrefix = []byte{0x14}
BucketPolicyForGroupPrefix = []byte{0x21}
ObjectPolicyForGroupPrefix = []byte{0x22}
PolicyByIDPrefix = []byte{0x31}
GroupMemberByIDPrefix = []byte{0x32}
PolicySequencePrefix = []byte{0x41}
GroupMemberSequencePrefix = []byte{0x42}
)
func PolicyForAccountPrefix(resourceID math.Uint, resourceType resource.ResourceType) []byte {
var key []byte
switch resourceType {
case resource.RESOURCE_TYPE_BUCKET:
key = BucketPolicyForAccountPrefix
case resource.RESOURCE_TYPE_OBJECT:
key = ObjectPolicyForAccountPrefix
case resource.RESOURCE_TYPE_GROUP:
key = GroupPolicyForAccountPrefix
default:
panic(fmt.Sprintf("GetPolicyForAccountKey Invalid Resource Type, %s", resourceType.String()))
}
key = append(key, resourceID.Bytes()...)
return key
}
func GetPolicyForAccountKey(resourceID math.Uint, resourceType resource.ResourceType, addr sdk.AccAddress) []byte {
key := PolicyForAccountPrefix(resourceID, resourceType)
key = append(key, addr.Bytes()...)
return key
}
func GetPolicyForGroupKey(resourceID math.Uint, resourceType resource.ResourceType) []byte {
var key []byte
switch resourceType {
case resource.RESOURCE_TYPE_BUCKET:
key = BucketPolicyForGroupPrefix
case resource.RESOURCE_TYPE_OBJECT:
key = ObjectPolicyForGroupPrefix
default:
panic(fmt.Sprintf("GetPolicyForGroupKey Invalid Resource Type, %s", resourceType.String()))
}
key = append(key, resourceID.Bytes()...)
return key
}
func GetPolicyByIDKey(policyID math.Uint) []byte {
return append(PolicyByIDPrefix, policyID.Bytes()...)
}
func GroupMembersPrefix(groupID math.Uint) []byte {
return append(GroupMemberPrefix, groupID.Bytes()...)
}
func GetGroupMemberKey(groupID math.Uint, member sdk.AccAddress) []byte {
return append(GroupMemberPrefix, append(groupID.Bytes(), member.Bytes()...)...)
}
func GetGroupMemberByIDKey(memberID math.Uint) []byte {
return append(GroupMemberByIDPrefix, memberID.Bytes()...)
}