github.com/Finschia/finschia-sdk@v0.48.1/x/authz/keeper/grpc_query.go (about)

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