2021-05-06 11:23:48 -07:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2022-02-16 09:36:50 -08:00
|
|
|
"time"
|
|
|
|
|
2021-05-06 11:23:48 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/internal/conv"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/types/address"
|
2021-07-08 04:29:12 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/kv"
|
2021-05-06 11:23:48 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/authz"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Keys for store prefixes
|
2022-02-16 09:36:50 -08:00
|
|
|
// Items are stored with the following key: values
|
|
|
|
//
|
|
|
|
// - 0x01<grant_Bytes>: Grant
|
|
|
|
// - 0x02<grant_expiration_Bytes>: GrantQueueItem
|
|
|
|
//
|
2021-05-06 11:23:48 -07:00
|
|
|
var (
|
2022-02-16 09:36:50 -08:00
|
|
|
GrantKey = []byte{0x01} // prefix for each key
|
|
|
|
GrantQueuePrefix = []byte{0x02}
|
2021-05-06 11:23:48 -07:00
|
|
|
)
|
|
|
|
|
2022-02-16 09:36:50 -08:00
|
|
|
var lenTime = len(sdk.FormatTimeBytes(time.Now()))
|
|
|
|
|
2021-05-06 11:23:48 -07:00
|
|
|
// StoreKey is the store key string for authz
|
|
|
|
const StoreKey = authz.ModuleName
|
|
|
|
|
|
|
|
// grantStoreKey - return authorization store key
|
|
|
|
// Items are stored with the following key: values
|
|
|
|
//
|
|
|
|
// - 0x01<granterAddressLen (1 Byte)><granterAddress_Bytes><granteeAddressLen (1 Byte)><granteeAddress_Bytes><msgType_Bytes>: Grant
|
|
|
|
func grantStoreKey(grantee sdk.AccAddress, granter sdk.AccAddress, msgType string) []byte {
|
|
|
|
m := conv.UnsafeStrToBytes(msgType)
|
|
|
|
granter = address.MustLengthPrefix(granter)
|
|
|
|
grantee = address.MustLengthPrefix(grantee)
|
2022-04-21 11:52:31 -07:00
|
|
|
key := sdk.AppendLengthPrefixedBytes(GrantKey, granter, grantee, m)
|
2021-05-06 11:23:48 -07:00
|
|
|
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
2022-02-16 09:36:50 -08:00
|
|
|
// parseGrantStoreKey - split granter, grantee address and msg type from the authorization key
|
|
|
|
func parseGrantStoreKey(key []byte) (granterAddr, granteeAddr sdk.AccAddress, msgType string) {
|
2021-05-06 11:23:48 -07:00
|
|
|
// key is of format:
|
|
|
|
// 0x01<granterAddressLen (1 Byte)><granterAddress_Bytes><granteeAddressLen (1 Byte)><granteeAddress_Bytes><msgType_Bytes>
|
2022-04-21 11:52:31 -07:00
|
|
|
|
|
|
|
granterAddrLen, granterAddrLenEndIndex := sdk.ParseLengthPrefixedBytes(key, 1, 1) // ignore key[0] since it is a prefix key
|
|
|
|
granterAddr, granterAddrEndIndex := sdk.ParseLengthPrefixedBytes(key, granterAddrLenEndIndex+1, int(granterAddrLen[0]))
|
|
|
|
|
|
|
|
granteeAddrLen, granteeAddrLenEndIndex := sdk.ParseLengthPrefixedBytes(key, granterAddrEndIndex+1, 1)
|
|
|
|
granteeAddr, granteeAddrEndIndex := sdk.ParseLengthPrefixedBytes(key, granteeAddrLenEndIndex+1, int(granteeAddrLen[0]))
|
|
|
|
|
|
|
|
kv.AssertKeyAtLeastLength(key, granteeAddrEndIndex+1)
|
|
|
|
return granterAddr, granteeAddr, conv.UnsafeBytesToStr(key[(granteeAddrEndIndex + 1):])
|
2022-02-16 09:36:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseGrantQueueKey split expiration time, granter and grantee from the grant queue key
|
|
|
|
func parseGrantQueueKey(key []byte) (time.Time, sdk.AccAddress, sdk.AccAddress, error) {
|
|
|
|
// key is of format:
|
|
|
|
// 0x02<grant_expiration_Bytes><granterAddress_Bytes><granteeAddressLen (1 Byte)><granteeAddress_Bytes>
|
|
|
|
|
2022-04-21 11:52:31 -07:00
|
|
|
expBytes, expEndIndex := sdk.ParseLengthPrefixedBytes(key, 1, lenTime)
|
|
|
|
|
|
|
|
exp, err := sdk.ParseTimeBytes(expBytes)
|
2022-02-16 09:36:50 -08:00
|
|
|
if err != nil {
|
|
|
|
return exp, nil, nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:52:31 -07:00
|
|
|
granterAddrLen, granterAddrLenEndIndex := sdk.ParseLengthPrefixedBytes(key, expEndIndex+1, 1)
|
|
|
|
granter, granterEndIndex := sdk.ParseLengthPrefixedBytes(key, granterAddrLenEndIndex+1, int(granterAddrLen[0]))
|
2022-02-16 09:36:50 -08:00
|
|
|
|
2022-04-21 11:52:31 -07:00
|
|
|
granteeAddrLen, granteeAddrLenEndIndex := sdk.ParseLengthPrefixedBytes(key, granterEndIndex+1, 1)
|
|
|
|
grantee, _ := sdk.ParseLengthPrefixedBytes(key, granteeAddrLenEndIndex+1, int(granteeAddrLen[0]))
|
2022-02-16 09:36:50 -08:00
|
|
|
|
|
|
|
return exp, granter, grantee, nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 11:27:23 -07:00
|
|
|
// GrantQueueKey - return grant queue store key. If a given grant doesn't have a defined
|
|
|
|
// expiration, then it should not be used in the pruning queue.
|
|
|
|
// Key format is:
|
|
|
|
// 0x02<grant_expiration_Bytes>: GrantQueueItem
|
2022-02-16 09:36:50 -08:00
|
|
|
func GrantQueueKey(expiration time.Time, granter sdk.AccAddress, grantee sdk.AccAddress) []byte {
|
|
|
|
exp := sdk.FormatTimeBytes(expiration)
|
|
|
|
granter = address.MustLengthPrefix(granter)
|
|
|
|
grantee = address.MustLengthPrefix(grantee)
|
|
|
|
|
2022-04-21 11:52:31 -07:00
|
|
|
return sdk.AppendLengthPrefixedBytes(GrantQueuePrefix, exp, granter, grantee)
|
2022-02-16 09:36:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GrantQueueTimePrefix - return grant queue time prefix
|
|
|
|
func GrantQueueTimePrefix(expiration time.Time) []byte {
|
|
|
|
return append(GrantQueuePrefix, sdk.FormatTimeBytes(expiration)...)
|
2021-05-06 11:23:48 -07:00
|
|
|
}
|
2022-02-10 07:56:44 -08:00
|
|
|
|
|
|
|
// firstAddressFromGrantStoreKey parses the first address only
|
|
|
|
func firstAddressFromGrantStoreKey(key []byte) sdk.AccAddress {
|
|
|
|
addrLen := key[0]
|
|
|
|
return sdk.AccAddress(key[1 : 1+addrLen])
|
|
|
|
}
|