From 65a991389f9ffb4e33eabdbdd1af0a6f754201a4 Mon Sep 17 00:00:00 2001 From: Likhita Polavarapu <78951027+likhita-809@users.noreply.github.com> Date: Thu, 1 Dec 2022 13:19:51 +0530 Subject: [PATCH] refactor: Add back removed empty checks in grpc from x/nft audit --- x/nft/keeper/grpc_query.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/x/nft/keeper/grpc_query.go b/x/nft/keeper/grpc_query.go index 0c4026e20f..e409a78c09 100644 --- a/x/nft/keeper/grpc_query.go +++ b/x/nft/keeper/grpc_query.go @@ -14,6 +14,10 @@ var _ nft.QueryServer = Keeper{} // Balance return the number of NFTs of a given class owned by the owner, same as balanceOf in ERC721 func (k Keeper) Balance(goCtx context.Context, r *nft.QueryBalanceRequest) (*nft.QueryBalanceResponse, error) { + if r == nil { + return nil, sdkerrors.ErrInvalidRequest.Wrap("empty request") + } + if len(r.ClassId) == 0 { return nil, nft.ErrEmptyClassID } @@ -30,6 +34,10 @@ func (k Keeper) Balance(goCtx context.Context, r *nft.QueryBalanceRequest) (*nft // Owner return the owner of the NFT based on its class and id, same as ownerOf in ERC721 func (k Keeper) Owner(goCtx context.Context, r *nft.QueryOwnerRequest) (*nft.QueryOwnerResponse, error) { + if r == nil { + return nil, sdkerrors.ErrInvalidRequest.Wrap("empty request") + } + if len(r.ClassId) == 0 { return nil, nft.ErrEmptyClassID } @@ -45,6 +53,10 @@ func (k Keeper) Owner(goCtx context.Context, r *nft.QueryOwnerRequest) (*nft.Que // Supply return the number of NFTs from the given class, same as totalSupply of ERC721. func (k Keeper) Supply(goCtx context.Context, r *nft.QuerySupplyRequest) (*nft.QuerySupplyResponse, error) { + if r == nil { + return nil, sdkerrors.ErrInvalidRequest.Wrap("empty request") + } + if len(r.ClassId) == 0 { return nil, nft.ErrEmptyClassID } @@ -55,6 +67,10 @@ func (k Keeper) Supply(goCtx context.Context, r *nft.QuerySupplyRequest) (*nft.Q // NFTs queries all NFTs of a given class or owner (at least one must be provided), similar to tokenByIndex in ERC721Enumerable func (k Keeper) NFTs(goCtx context.Context, r *nft.QueryNFTsRequest) (*nft.QueryNFTsResponse, error) { + if r == nil { + return nil, sdkerrors.ErrInvalidRequest.Wrap("empty request") + } + var err error var owner sdk.AccAddress @@ -113,6 +129,10 @@ func (k Keeper) NFTs(goCtx context.Context, r *nft.QueryNFTsRequest) (*nft.Query // NFT return an NFT based on its class and id. func (k Keeper) NFT(goCtx context.Context, r *nft.QueryNFTRequest) (*nft.QueryNFTResponse, error) { + if r == nil { + return nil, sdkerrors.ErrInvalidRequest.Wrap("empty request") + } + if len(r.ClassId) == 0 { return nil, nft.ErrEmptyClassID } @@ -130,6 +150,10 @@ func (k Keeper) NFT(goCtx context.Context, r *nft.QueryNFTRequest) (*nft.QueryNF // Class return an NFT class based on its id func (k Keeper) Class(goCtx context.Context, r *nft.QueryClassRequest) (*nft.QueryClassResponse, error) { + if r == nil { + return nil, sdkerrors.ErrInvalidRequest.Wrap("empty request") + } + if len(r.ClassId) == 0 { return nil, nft.ErrEmptyClassID } @@ -144,6 +168,10 @@ func (k Keeper) Class(goCtx context.Context, r *nft.QueryClassRequest) (*nft.Que // Classes return all NFT classes func (k Keeper) Classes(goCtx context.Context, r *nft.QueryClassesRequest) (*nft.QueryClassesResponse, error) { + if r == nil { + return nil, sdkerrors.ErrInvalidRequest.Wrap("empty request") + } + ctx := sdk.UnwrapSDKContext(goCtx) store := ctx.KVStore(k.storeKey) classStore := prefix.NewStore(store, ClassKey)