github.com/cosmos/cosmos-sdk@v0.50.10/x/authz/keeper/keys.go (about) 1 package keeper 2 3 import ( 4 "time" 5 6 "github.com/cosmos/cosmos-sdk/internal/conv" 7 sdk "github.com/cosmos/cosmos-sdk/types" 8 "github.com/cosmos/cosmos-sdk/types/address" 9 "github.com/cosmos/cosmos-sdk/types/kv" 10 "github.com/cosmos/cosmos-sdk/x/authz" 11 ) 12 13 // Keys for store prefixes 14 // Items are stored with the following key: values 15 // 16 // - 0x01<grant_Bytes>: Grant 17 // - 0x02<grant_expiration_Bytes>: GrantQueueItem 18 var ( 19 GrantKey = []byte{0x01} // prefix for each key 20 GrantQueuePrefix = []byte{0x02} 21 ) 22 23 var lenTime = len(sdk.FormatTimeBytes(time.Now())) 24 25 // StoreKey is the store key string for authz 26 const StoreKey = authz.ModuleName 27 28 // grantStoreKey - return authorization store key 29 // Items are stored with the following key: values 30 // 31 // - 0x01<granterAddressLen (1 Byte)><granterAddress_Bytes><granteeAddressLen (1 Byte)><granteeAddress_Bytes><msgType_Bytes>: Grant 32 func grantStoreKey(grantee, granter sdk.AccAddress, msgType string) []byte { 33 m := conv.UnsafeStrToBytes(msgType) 34 granter = address.MustLengthPrefix(granter) 35 grantee = address.MustLengthPrefix(grantee) 36 key := sdk.AppendLengthPrefixedBytes(GrantKey, granter, grantee, m) 37 38 return key 39 } 40 41 // parseGrantStoreKey - split granter, grantee address and msg type from the authorization key 42 func parseGrantStoreKey(key []byte) (granterAddr, granteeAddr sdk.AccAddress, msgType string) { 43 // key is of format: 44 // 0x01<granterAddressLen (1 Byte)><granterAddress_Bytes><granteeAddressLen (1 Byte)><granteeAddress_Bytes><msgType_Bytes> 45 46 granterAddrLen, granterAddrLenEndIndex := sdk.ParseLengthPrefixedBytes(key, 1, 1) // ignore key[0] since it is a prefix key 47 granterAddr, granterAddrEndIndex := sdk.ParseLengthPrefixedBytes(key, granterAddrLenEndIndex+1, int(granterAddrLen[0])) 48 49 granteeAddrLen, granteeAddrLenEndIndex := sdk.ParseLengthPrefixedBytes(key, granterAddrEndIndex+1, 1) 50 granteeAddr, granteeAddrEndIndex := sdk.ParseLengthPrefixedBytes(key, granteeAddrLenEndIndex+1, int(granteeAddrLen[0])) 51 52 kv.AssertKeyAtLeastLength(key, granteeAddrEndIndex+1) 53 return granterAddr, granteeAddr, conv.UnsafeBytesToStr(key[(granteeAddrEndIndex + 1):]) 54 } 55 56 // parseGrantQueueKey split expiration time, granter and grantee from the grant queue key 57 func parseGrantQueueKey(key []byte) (time.Time, sdk.AccAddress, sdk.AccAddress, error) { 58 // key is of format: 59 // 0x02<grant_expiration_Bytes><granterAddress_Bytes><granteeAddressLen (1 Byte)><granteeAddress_Bytes> 60 61 expBytes, expEndIndex := sdk.ParseLengthPrefixedBytes(key, 1, lenTime) 62 63 exp, err := sdk.ParseTimeBytes(expBytes) 64 if err != nil { 65 return exp, nil, nil, err 66 } 67 68 granterAddrLen, granterAddrLenEndIndex := sdk.ParseLengthPrefixedBytes(key, expEndIndex+1, 1) 69 granter, granterEndIndex := sdk.ParseLengthPrefixedBytes(key, granterAddrLenEndIndex+1, int(granterAddrLen[0])) 70 71 granteeAddrLen, granteeAddrLenEndIndex := sdk.ParseLengthPrefixedBytes(key, granterEndIndex+1, 1) 72 grantee, _ := sdk.ParseLengthPrefixedBytes(key, granteeAddrLenEndIndex+1, int(granteeAddrLen[0])) 73 74 return exp, granter, grantee, nil 75 } 76 77 // GrantQueueKey - return grant queue store key. If a given grant doesn't have a defined 78 // expiration, then it should not be used in the pruning queue. 79 // Key format is: 80 // 81 // 0x02<expiration><granterAddressLen (1 Byte)><granterAddressBytes><granteeAddressLen (1 Byte)><granteeAddressBytes>: GrantQueueItem 82 func GrantQueueKey(expiration time.Time, granter, grantee sdk.AccAddress) []byte { 83 exp := sdk.FormatTimeBytes(expiration) 84 granter = address.MustLengthPrefix(granter) 85 grantee = address.MustLengthPrefix(grantee) 86 87 return sdk.AppendLengthPrefixedBytes(GrantQueuePrefix, exp, granter, grantee) 88 } 89 90 // GrantQueueTimePrefix - return grant queue time prefix 91 func GrantQueueTimePrefix(expiration time.Time) []byte { 92 return append(GrantQueuePrefix, sdk.FormatTimeBytes(expiration)...) 93 } 94 95 // firstAddressFromGrantStoreKey parses the first address only 96 func firstAddressFromGrantStoreKey(key []byte) sdk.AccAddress { 97 addrLen := key[0] 98 return sdk.AccAddress(key[1 : 1+addrLen]) 99 }