2021-01-25 08:41:30 -08:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
|
|
|
proto "github.com/gogo/protobuf/proto"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/store/prefix"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/types/query"
|
2021-05-06 11:23:48 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/authz"
|
2021-01-25 08:41:30 -08:00
|
|
|
)
|
|
|
|
|
2021-05-06 11:23:48 -07:00
|
|
|
var _ authz.QueryServer = Keeper{}
|
2021-01-25 08:41:30 -08:00
|
|
|
|
2021-05-06 11:23:48 -07:00
|
|
|
// Authorizations implements the Query/Grants gRPC method.
|
|
|
|
func (k Keeper) Grants(c context.Context, req *authz.QueryGrantsRequest) (*authz.QueryGrantsResponse, error) {
|
2021-01-25 08:41:30 -08:00
|
|
|
if req == nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
|
|
|
granter, err := sdk.AccAddressFromBech32(req.Granter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-06 11:23:48 -07:00
|
|
|
grantee, err := sdk.AccAddressFromBech32(req.Grantee)
|
2021-01-25 08:41:30 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2021-05-06 11:23:48 -07:00
|
|
|
key := grantStoreKey(grantee, granter, "")
|
2021-01-25 08:41:30 -08:00
|
|
|
authStore := prefix.NewStore(store, key)
|
2021-05-06 11:23:48 -07:00
|
|
|
|
|
|
|
if req.MsgTypeUrl != "" {
|
|
|
|
authorization, expiration := k.GetCleanAuthorization(ctx, grantee, granter, req.MsgTypeUrl)
|
|
|
|
if authorization == nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "no authorization found for %s type", req.MsgTypeUrl)
|
|
|
|
}
|
|
|
|
authorizationAny, err := codectypes.NewAnyWithValue(authorization)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
return &authz.QueryGrantsResponse{
|
|
|
|
Grants: []*authz.Grant{{
|
|
|
|
Authorization: authorizationAny,
|
|
|
|
Expiration: expiration,
|
|
|
|
}},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var authorizations []*authz.Grant
|
2021-01-25 08:41:30 -08:00
|
|
|
pageRes, err := query.FilteredPaginate(authStore, req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) {
|
|
|
|
auth, err := unmarshalAuthorization(k.cdc, value)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2021-05-06 11:23:48 -07:00
|
|
|
auth1 := auth.GetAuthorization()
|
2021-01-25 08:41:30 -08:00
|
|
|
if accumulate {
|
|
|
|
msg, ok := auth1.(proto.Message)
|
|
|
|
if !ok {
|
|
|
|
return false, status.Errorf(codes.Internal, "can't protomarshal %T", msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
authorizationAny, err := codectypes.NewAnyWithValue(msg)
|
|
|
|
if err != nil {
|
|
|
|
return false, status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
2021-05-06 11:23:48 -07:00
|
|
|
authorizations = append(authorizations, &authz.Grant{
|
2021-01-25 08:41:30 -08:00
|
|
|
Authorization: authorizationAny,
|
|
|
|
Expiration: auth.Expiration,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-06 11:23:48 -07:00
|
|
|
return &authz.QueryGrantsResponse{
|
|
|
|
Grants: authorizations,
|
|
|
|
Pagination: pageRes,
|
2021-01-25 08:41:30 -08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-10-11 06:30:12 -07:00
|
|
|
// GranterGrants implements the Query/GranterGrants gRPC method.
|
|
|
|
func (k Keeper) GranterGrants(c context.Context, req *authz.QueryGranterGrantsRequest) (*authz.QueryGranterGrantsResponse, error) {
|
|
|
|
if req == nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
|
|
|
granter, err := sdk.AccAddressFromBech32(req.Granter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
|
|
authzStore := prefix.NewStore(store, grantStoreKey(nil, granter, ""))
|
|
|
|
|
|
|
|
var authorizations []*authz.Grant
|
|
|
|
pageRes, err := query.FilteredPaginate(authzStore, req.Pagination, func(key []byte, value []byte,
|
|
|
|
accumulate bool) (bool, error) {
|
|
|
|
auth, err := unmarshalAuthorization(k.cdc, value)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
auth1 := auth.GetAuthorization()
|
|
|
|
if accumulate {
|
|
|
|
any, err := codectypes.NewAnyWithValue(auth1)
|
|
|
|
if err != nil {
|
|
|
|
return false, status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
authorizations = append(authorizations, &authz.Grant{
|
|
|
|
Authorization: any,
|
|
|
|
Expiration: auth.Expiration,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &authz.QueryGranterGrantsResponse{
|
|
|
|
Grants: authorizations,
|
|
|
|
Pagination: pageRes,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-25 08:41:30 -08:00
|
|
|
// unmarshal an authorization from a store value
|
2021-05-06 11:23:48 -07:00
|
|
|
func unmarshalAuthorization(cdc codec.BinaryCodec, value []byte) (v authz.Grant, err error) {
|
2021-04-29 03:46:22 -07:00
|
|
|
err = cdc.Unmarshal(value, &v)
|
2021-01-25 08:41:30 -08:00
|
|
|
return v, err
|
|
|
|
}
|