<!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Closes: #9966 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
145 lines
3.9 KiB
Go
145 lines
3.9 KiB
Go
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"
|
|
"github.com/cosmos/cosmos-sdk/x/authz"
|
|
)
|
|
|
|
var _ authz.QueryServer = Keeper{}
|
|
|
|
// Authorizations implements the Query/Grants gRPC method.
|
|
func (k Keeper) Grants(c context.Context, req *authz.QueryGrantsRequest) (*authz.QueryGrantsResponse, error) {
|
|
if req == nil {
|
|
return nil, status.Errorf(codes.InvalidArgument, "empty request")
|
|
}
|
|
|
|
granter, err := sdk.AccAddressFromBech32(req.Granter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
grantee, err := sdk.AccAddressFromBech32(req.Grantee)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
key := grantStoreKey(grantee, granter, "")
|
|
authStore := prefix.NewStore(store, key)
|
|
|
|
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
|
|
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
|
|
}
|
|
auth1 := auth.GetAuthorization()
|
|
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())
|
|
}
|
|
authorizations = append(authorizations, &authz.Grant{
|
|
Authorization: authorizationAny,
|
|
Expiration: auth.Expiration,
|
|
})
|
|
}
|
|
return true, nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &authz.QueryGrantsResponse{
|
|
Grants: authorizations,
|
|
Pagination: pageRes,
|
|
}, nil
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// unmarshal an authorization from a store value
|
|
func unmarshalAuthorization(cdc codec.BinaryCodec, value []byte) (v authz.Grant, err error) {
|
|
err = cdc.Unmarshal(value, &v)
|
|
return v, err
|
|
}
|