Skip to content
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions x/market/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"context"
"encoding/base64"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -49,10 +50,21 @@

// setup for case 3 - cross-index search
// nolint: gocritic
if len(req.Pagination.Key) > 0 {
hasPaginationKey := len(req.Pagination.Key) > 0
if hasPaginationKey {

Check failure on line 54 in x/market/keeper/grpc_query.go

View workflow job for this annotation

GitHub Actions / lint

ifElseChain: rewrite if-else to switch statement (gocritic)

Check failure on line 54 in x/market/keeper/grpc_query.go

View workflow job for this annotation

GitHub Actions / lint

ifElseChain: rewrite if-else to switch statement (gocritic)
var key []byte
var err error
states, searchPrefix, key, _, err = query.DecodePaginationKey(req.Pagination.Key)

// Handle Base64 encoded pagination keys
paginationKeyBytes := req.Pagination.Key
if isBase64String(req.Pagination.Key) {
paginationKeyBytes, err = base64.StdEncoding.DecodeString(string(req.Pagination.Key))
if err != nil {
return nil, status.Error(codes.InvalidArgument, "invalid base64 pagination key")
}
}

states, searchPrefix, key, _, err = query.DecodePaginationKey(paginationKeyBytes)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
Expand Down Expand Up @@ -84,7 +96,7 @@
state := types.Order_State(states[idx])
var err error

if idx > 0 {
if idx > 0 && !hasPaginationKey {
req.Pagination.Key = nil
}

Expand Down Expand Up @@ -125,13 +137,7 @@
return nil, status.Error(codes.Internal, err.Error())
}

if len(pageRes.NextKey) > 0 {
nextKey := make([]byte, len(searchPrefix)+len(pageRes.NextKey))
copy(nextKey, searchPrefix)
copy(nextKey[len(searchPrefix):], pageRes.NextKey)

pageRes.NextKey = nextKey
}
// Keep raw Cosmos SDK nextKey; do not prepend searchPrefix

req.Pagination.Limit -= count
total += count
Expand Down Expand Up @@ -278,13 +284,7 @@
return nil, status.Error(codes.Internal, err.Error())
}

if len(pageRes.NextKey) > 0 {
nextKey := make([]byte, len(searchPrefix)+len(pageRes.NextKey))
copy(nextKey, searchPrefix)
copy(nextKey[len(searchPrefix):], pageRes.NextKey)

pageRes.NextKey = nextKey
}
// Keep raw Cosmos SDK nextKey; do not prepend searchPrefix

req.Pagination.Limit -= count
total += count
Expand Down Expand Up @@ -559,3 +559,15 @@
EscrowPayment: payment,
}, nil
}

// Helper function to check if bytes represent a Base64 string
func isBase64String(data []byte) bool {
// Check if all bytes are valid Base64 characters
for _, b := range data {
if !((b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z') || (b >= '0' && b <= '9') || b == '+' || b == '/' || b == '=') {
return false
}
}
// Additional check: Base64 strings should have length divisible by 4 (with padding)
return len(data) > 0 && len(data)%4 == 0
}
Loading