Skip to content

Commit e8b99de

Browse files
committed
fix: fix pagination key handling and Base64 decoding
1 parent 7f1de41 commit e8b99de

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

x/market/keeper/grpc_query.go

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package keeper
22

33
import (
44
"context"
5+
"encoding/base64"
56

67
"google.golang.org/grpc/codes"
78
"google.golang.org/grpc/status"
@@ -49,10 +50,21 @@ func (k Querier) Orders(c context.Context, req *types.QueryOrdersRequest) (*type
4950

5051
// setup for case 3 - cross-index search
5152
// nolint: gocritic
52-
if len(req.Pagination.Key) > 0 {
53+
hasPaginationKey := len(req.Pagination.Key) > 0
54+
if hasPaginationKey {
5355
var key []byte
5456
var err error
55-
states, searchPrefix, key, _, err = query.DecodePaginationKey(req.Pagination.Key)
57+
58+
// Handle Base64 encoded pagination keys
59+
paginationKeyBytes := req.Pagination.Key
60+
if isBase64String(req.Pagination.Key) {
61+
paginationKeyBytes, err = base64.StdEncoding.DecodeString(string(req.Pagination.Key))
62+
if err != nil {
63+
return nil, status.Error(codes.InvalidArgument, "invalid base64 pagination key")
64+
}
65+
}
66+
67+
states, searchPrefix, key, _, err = query.DecodePaginationKey(paginationKeyBytes)
5668
if err != nil {
5769
return nil, status.Error(codes.Internal, err.Error())
5870
}
@@ -84,7 +96,7 @@ func (k Querier) Orders(c context.Context, req *types.QueryOrdersRequest) (*type
8496
state := types.Order_State(states[idx])
8597
var err error
8698

87-
if idx > 0 {
99+
if idx > 0 && !hasPaginationKey {
88100
req.Pagination.Key = nil
89101
}
90102

@@ -125,13 +137,7 @@ func (k Querier) Orders(c context.Context, req *types.QueryOrdersRequest) (*type
125137
return nil, status.Error(codes.Internal, err.Error())
126138
}
127139

128-
if len(pageRes.NextKey) > 0 {
129-
nextKey := make([]byte, len(searchPrefix)+len(pageRes.NextKey))
130-
copy(nextKey, searchPrefix)
131-
copy(nextKey[len(searchPrefix):], pageRes.NextKey)
132-
133-
pageRes.NextKey = nextKey
134-
}
140+
// Keep raw Cosmos SDK nextKey; do not prepend searchPrefix
135141

136142
req.Pagination.Limit -= count
137143
total += count
@@ -278,13 +284,7 @@ func (k Querier) Bids(c context.Context, req *types.QueryBidsRequest) (*types.Qu
278284
return nil, status.Error(codes.Internal, err.Error())
279285
}
280286

281-
if len(pageRes.NextKey) > 0 {
282-
nextKey := make([]byte, len(searchPrefix)+len(pageRes.NextKey))
283-
copy(nextKey, searchPrefix)
284-
copy(nextKey[len(searchPrefix):], pageRes.NextKey)
285-
286-
pageRes.NextKey = nextKey
287-
}
287+
// Keep raw Cosmos SDK nextKey; do not prepend searchPrefix
288288

289289
req.Pagination.Limit -= count
290290
total += count
@@ -559,3 +559,15 @@ func (k Querier) Lease(c context.Context, req *types.QueryLeaseRequest) (*types.
559559
EscrowPayment: payment,
560560
}, nil
561561
}
562+
563+
// Helper function to check if bytes represent a Base64 string
564+
func isBase64String(data []byte) bool {
565+
// Check if all bytes are valid Base64 characters
566+
for _, b := range data {
567+
if !((b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z') || (b >= '0' && b <= '9') || b == '+' || b == '/' || b == '=') {
568+
return false
569+
}
570+
}
571+
// Additional check: Base64 strings should have length divisible by 4 (with padding)
572+
return len(data) > 0 && len(data)%4 == 0
573+
}

0 commit comments

Comments
 (0)