github.com/cosmos/cosmos-sdk@v0.50.10/x/authz/keeper/grpc_query.go (about) 1 package keeper 2 3 import ( 4 "bytes" 5 "context" 6 7 "google.golang.org/grpc/codes" 8 "google.golang.org/grpc/status" 9 10 "cosmossdk.io/errors" 11 "cosmossdk.io/store/prefix" 12 13 codectypes "github.com/cosmos/cosmos-sdk/codec/types" 14 "github.com/cosmos/cosmos-sdk/runtime" 15 "github.com/cosmos/cosmos-sdk/types/query" 16 "github.com/cosmos/cosmos-sdk/x/authz" 17 ) 18 19 var _ authz.QueryServer = Keeper{} 20 21 // Grants implements the Query/Grants gRPC method. 22 // It returns grants for a granter-grantee pair. If msg type URL is set, it returns grants only for that msg type. 23 func (k Keeper) Grants(ctx context.Context, req *authz.QueryGrantsRequest) (*authz.QueryGrantsResponse, error) { 24 if req == nil { 25 return nil, status.Errorf(codes.InvalidArgument, "empty request") 26 } 27 28 granter, err := k.authKeeper.AddressCodec().StringToBytes(req.Granter) 29 if err != nil { 30 return nil, err 31 } 32 33 grantee, err := k.authKeeper.AddressCodec().StringToBytes(req.Grantee) 34 if err != nil { 35 return nil, err 36 } 37 38 if req.MsgTypeUrl != "" { 39 grant, found := k.getGrant(ctx, grantStoreKey(grantee, granter, req.MsgTypeUrl)) 40 if !found { 41 return nil, errors.Wrapf(authz.ErrNoAuthorizationFound, "authorization not found for %s type", req.MsgTypeUrl) 42 } 43 44 authorization, err := grant.GetAuthorization() 45 if err != nil { 46 return nil, err 47 } 48 49 authorizationAny, err := codectypes.NewAnyWithValue(authorization) 50 if err != nil { 51 return nil, status.Errorf(codes.Internal, err.Error()) 52 } 53 return &authz.QueryGrantsResponse{ 54 Grants: []*authz.Grant{{ 55 Authorization: authorizationAny, 56 Expiration: grant.Expiration, 57 }}, 58 }, nil 59 } 60 61 store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) 62 key := grantStoreKey(grantee, granter, "") 63 grantsStore := prefix.NewStore(store, key) 64 65 authorizations, pageRes, err := query.GenericFilteredPaginate(k.cdc, grantsStore, req.Pagination, func(key []byte, auth *authz.Grant) (*authz.Grant, error) { 66 auth1, err := auth.GetAuthorization() 67 if err != nil { 68 return nil, err 69 } 70 71 authorizationAny, err := codectypes.NewAnyWithValue(auth1) 72 if err != nil { 73 return nil, status.Errorf(codes.Internal, err.Error()) 74 } 75 return &authz.Grant{ 76 Authorization: authorizationAny, 77 Expiration: auth.Expiration, 78 }, nil 79 }, func() *authz.Grant { 80 return &authz.Grant{} 81 }) 82 if err != nil { 83 return nil, err 84 } 85 86 return &authz.QueryGrantsResponse{ 87 Grants: authorizations, 88 Pagination: pageRes, 89 }, nil 90 } 91 92 // GranterGrants implements the Query/GranterGrants gRPC method. 93 func (k Keeper) GranterGrants(ctx context.Context, req *authz.QueryGranterGrantsRequest) (*authz.QueryGranterGrantsResponse, error) { 94 if req == nil { 95 return nil, status.Errorf(codes.InvalidArgument, "empty request") 96 } 97 98 granter, err := k.authKeeper.AddressCodec().StringToBytes(req.Granter) 99 if err != nil { 100 return nil, err 101 } 102 103 store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) 104 authzStore := prefix.NewStore(store, grantStoreKey(nil, granter, "")) 105 106 grants, pageRes, err := query.GenericFilteredPaginate(k.cdc, authzStore, req.Pagination, func(key []byte, auth *authz.Grant) (*authz.GrantAuthorization, error) { 107 auth1, err := auth.GetAuthorization() 108 if err != nil { 109 return nil, err 110 } 111 112 any, err := codectypes.NewAnyWithValue(auth1) 113 if err != nil { 114 return nil, status.Errorf(codes.Internal, err.Error()) 115 } 116 117 grantee := firstAddressFromGrantStoreKey(key) 118 return &authz.GrantAuthorization{ 119 Granter: req.Granter, 120 Grantee: grantee.String(), 121 Authorization: any, 122 Expiration: auth.Expiration, 123 }, nil 124 }, func() *authz.Grant { 125 return &authz.Grant{} 126 }) 127 if err != nil { 128 return nil, err 129 } 130 131 return &authz.QueryGranterGrantsResponse{ 132 Grants: grants, 133 Pagination: pageRes, 134 }, nil 135 } 136 137 // GranteeGrants implements the Query/GranteeGrants gRPC method. 138 func (k Keeper) GranteeGrants(ctx context.Context, req *authz.QueryGranteeGrantsRequest) (*authz.QueryGranteeGrantsResponse, error) { 139 if req == nil { 140 return nil, status.Errorf(codes.InvalidArgument, "empty request") 141 } 142 143 grantee, err := k.authKeeper.AddressCodec().StringToBytes(req.Grantee) 144 if err != nil { 145 return nil, err 146 } 147 148 store := prefix.NewStore(runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)), GrantKey) 149 150 authorizations, pageRes, err := query.GenericFilteredPaginate(k.cdc, store, req.Pagination, func(key []byte, auth *authz.Grant) (*authz.GrantAuthorization, error) { 151 auth1, err := auth.GetAuthorization() 152 if err != nil { 153 return nil, err 154 } 155 156 granter, g, _ := parseGrantStoreKey(append(GrantKey, key...)) 157 if !bytes.Equal(g, grantee) { 158 return nil, nil 159 } 160 161 authorizationAny, err := codectypes.NewAnyWithValue(auth1) 162 if err != nil { 163 return nil, status.Errorf(codes.Internal, err.Error()) 164 } 165 166 return &authz.GrantAuthorization{ 167 Authorization: authorizationAny, 168 Expiration: auth.Expiration, 169 Granter: granter.String(), 170 Grantee: req.Grantee, 171 }, nil 172 }, func() *authz.Grant { 173 return &authz.Grant{} 174 }) 175 if err != nil { 176 return nil, err 177 } 178 179 return &authz.QueryGranteeGrantsResponse{ 180 Grants: authorizations, 181 Pagination: pageRes, 182 }, nil 183 }