mukan-sdk/x/authz/keeper/grpc_query.go
Mukan Erkin Törük 20afb5db80
Some checks failed
Build SimApp / build (amd64) (push) Waiting to run
Build SimApp / build (arm64) (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Build & Push / build (push) Waiting to run
Run Gosec / Gosec (push) Waiting to run
Lint / golangci-lint (push) Waiting to run
Checks dependencies and mocks generation / Check go mod tidy (push) Waiting to run
Checks dependencies and mocks generation / Check up to date mocks (push) Waiting to run
System Tests / setup (push) Waiting to run
System Tests / test-system (push) Blocked by required conditions
System Tests / test-system-legacy (push) Blocked by required conditions
Tests / Code Coverage / split-test-files (push) Waiting to run
Tests / Code Coverage / tests (00) (push) Blocked by required conditions
Tests / Code Coverage / tests (01) (push) Blocked by required conditions
Tests / Code Coverage / tests (02) (push) Blocked by required conditions
Tests / Code Coverage / tests (03) (push) Blocked by required conditions
Tests / Code Coverage / test-integration (push) Waiting to run
Tests / Code Coverage / test-e2e (push) Waiting to run
Tests / Code Coverage / repo-analysis (push) Blocked by required conditions
Tests / Code Coverage / test-sim-nondeterminism (push) Waiting to run
Tests / Code Coverage / test-clientv2 (push) Waiting to run
Tests / Code Coverage / test-core (push) Waiting to run
Tests / Code Coverage / test-depinject (push) Waiting to run
Tests / Code Coverage / test-errors (push) Waiting to run
Tests / Code Coverage / test-math (push) Waiting to run
Tests / Code Coverage / test-schema (push) Waiting to run
Tests / Code Coverage / test-collections (push) Waiting to run
Tests / Code Coverage / test-cosmovisor (push) Waiting to run
Tests / Code Coverage / test-confix (push) Waiting to run
Tests / Code Coverage / test-store (push) Waiting to run
Tests / Code Coverage / test-log (push) Waiting to run
Tests / Code Coverage / test-x-tx (push) Waiting to run
Tests / Code Coverage / test-x-nft (push) Waiting to run
Tests / Code Coverage / test-x-circuit (push) Waiting to run
Tests / Code Coverage / test-x-feegrant (push) Waiting to run
Tests / Code Coverage / test-x-evidence (push) Waiting to run
Tests / Code Coverage / test-x-upgrade (push) Waiting to run
Tests / Code Coverage / test-tools-benchmark (push) Waiting to run
Build & Push SDK Proto Builder / build (push) Has been cancelled
initial: sovereign Mukan Network fork
2026-05-11 03:18:24 +03:00

183 lines
5 KiB
Go

package keeper
import (
"bytes"
"context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"cosmossdk.io/errors"
"cosmossdk.io/store/prefix"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/authz"
)
var _ authz.QueryServer = Keeper{}
// Grants implements the Query/Grants gRPC method.
// It returns grants for a granter-grantee pair. If msg type URL is set, it returns grants only for that msg type.
func (k Keeper) Grants(ctx context.Context, req *authz.QueryGrantsRequest) (*authz.QueryGrantsResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
granter, err := k.authKeeper.AddressCodec().StringToBytes(req.Granter)
if err != nil {
return nil, err
}
grantee, err := k.authKeeper.AddressCodec().StringToBytes(req.Grantee)
if err != nil {
return nil, err
}
if req.MsgTypeUrl != "" {
grant, found := k.getGrant(ctx, grantStoreKey(grantee, granter, req.MsgTypeUrl))
if !found {
return nil, errors.Wrapf(authz.ErrNoAuthorizationFound, "authorization not found for %s type", req.MsgTypeUrl)
}
authorization, err := grant.GetAuthorization()
if err != nil {
return nil, err
}
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: grant.Expiration,
}},
}, nil
}
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
key := grantStoreKey(grantee, granter, "")
grantsStore := prefix.NewStore(store, key)
authorizations, pageRes, err := query.GenericFilteredPaginate(k.cdc, grantsStore, req.Pagination, func(key []byte, auth *authz.Grant) (*authz.Grant, error) {
auth1, err := auth.GetAuthorization()
if err != nil {
return nil, err
}
authorizationAny, err := codectypes.NewAnyWithValue(auth1)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
return &authz.Grant{
Authorization: authorizationAny,
Expiration: auth.Expiration,
}, nil
}, func() *authz.Grant {
return &authz.Grant{}
})
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(ctx context.Context, req *authz.QueryGranterGrantsRequest) (*authz.QueryGranterGrantsResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
granter, err := k.authKeeper.AddressCodec().StringToBytes(req.Granter)
if err != nil {
return nil, err
}
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
authzStore := prefix.NewStore(store, grantStoreKey(nil, granter, ""))
grants, pageRes, err := query.GenericFilteredPaginate(k.cdc, authzStore, req.Pagination, func(key []byte, auth *authz.Grant) (*authz.GrantAuthorization, error) {
auth1, err := auth.GetAuthorization()
if err != nil {
return nil, err
}
any, err := codectypes.NewAnyWithValue(auth1)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
grantee := firstAddressFromGrantStoreKey(key)
return &authz.GrantAuthorization{
Granter: req.Granter,
Grantee: grantee.String(),
Authorization: any,
Expiration: auth.Expiration,
}, nil
}, func() *authz.Grant {
return &authz.Grant{}
})
if err != nil {
return nil, err
}
return &authz.QueryGranterGrantsResponse{
Grants: grants,
Pagination: pageRes,
}, nil
}
// GranteeGrants implements the Query/GranteeGrants gRPC method.
func (k Keeper) GranteeGrants(ctx context.Context, req *authz.QueryGranteeGrantsRequest) (*authz.QueryGranteeGrantsResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
grantee, err := k.authKeeper.AddressCodec().StringToBytes(req.Grantee)
if err != nil {
return nil, err
}
store := prefix.NewStore(runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)), GrantKey)
authorizations, pageRes, err := query.GenericFilteredPaginate(k.cdc, store, req.Pagination, func(key []byte, auth *authz.Grant) (*authz.GrantAuthorization, error) {
auth1, err := auth.GetAuthorization()
if err != nil {
return nil, err
}
granter, g, _ := parseGrantStoreKey(append(GrantKey, key...))
if !bytes.Equal(g, grantee) {
return nil, nil
}
authorizationAny, err := codectypes.NewAnyWithValue(auth1)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
return &authz.GrantAuthorization{
Authorization: authorizationAny,
Expiration: auth.Expiration,
Granter: granter.String(),
Grantee: req.Grantee,
}, nil
}, func() *authz.Grant {
return &authz.Grant{}
})
if err != nil {
return nil, err
}
return &authz.QueryGranteeGrantsResponse{
Grants: authorizations,
Pagination: pageRes,
}, nil
}