-
Notifications
You must be signed in to change notification settings - Fork 44
/
keys.go
140 lines (114 loc) · 3.77 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package types
import (
"fmt"
"math/big"
"time"
"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"
FormatTimeBytesLength = 29 // len(sdk.FormatTimeBytes(time.Now()))
)
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}
PolicyQueueKeyPrefix = []byte{0x51}
)
func PolicyForAccountPrefix(resourceID math.Uint, resourceType resource.ResourceType, useV2 bool) []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()))
}
if useV2 {
key = append(key, LengthPrefix(resourceID)...)
} else {
key = append(key, resourceID.Bytes()...)
}
return key
}
func GetPolicyForAccountKey(resourceID math.Uint, resourceType resource.ResourceType, addr sdk.AccAddress, useV2 bool) []byte {
var key []byte
key = PolicyForAccountPrefix(resourceID, resourceType, useV2)
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, LengthPrefix(groupID)...)
}
func GetGroupMemberKey(groupID math.Uint, member sdk.AccAddress) []byte {
return append(GroupMemberPrefix, append(LengthPrefix(groupID), member.Bytes()...)...)
}
func GetGroupMemberByIDKey(memberID math.Uint) []byte {
return append(GroupMemberByIDPrefix, memberID.Bytes()...)
}
// PolicyPrefixQueue is the canonical key to store policy key.
//
// Key format:
// - <key_prefix><exp_bytes><policy_id_bytes>
func PolicyPrefixQueue(exp *time.Time, key []byte) []byte {
policyByExpTimeKey := PolicyByExpTimeKey(exp)
return append(policyByExpTimeKey, key...)
}
// PolicyByExpTimeKey returns a key with key prefix, expiry
//
// Key format:
// - <key_prefix><exp_bytes>
func PolicyByExpTimeKey(exp *time.Time) []byte {
// no need of appending len(exp_bytes) here, `FormatTimeBytes` gives const length everytime.
return append(PolicyQueueKeyPrefix, sdk.FormatTimeBytes(*exp)...)
}
func ParsePolicyIdFromQueueKey(key []byte) math.Uint {
// key is of format:
// <key_prefix><expiration_bytes(fixed length)><policy_id_bytes>
bz := key[FormatTimeBytesLength+1:]
return math.NewUintFromBigInt(new(big.Int).SetBytes(bz))
}
func LengthPrefix(id math.Uint) []byte {
bz := id.Bytes()
bzLen := len(bz)
if bzLen == 0 {
return bz
}
return append([]byte{byte(bzLen)}, bz...)
}