Skip to content

Commit

Permalink
Check address is valid in x/affiliates ReferredBy query
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyding committed Nov 12, 2024
1 parent 62fdf68 commit 6bf1852
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions protocol/x/affiliates/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ func (k Keeper) ReferredBy(ctx context.Context,
req *types.ReferredByRequest) (*types.ReferredByResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)

// Check req.Address is a valid bech32 address
_, err := sdk.AccAddressFromBech32(req.GetAddress())
if err != nil {
return nil, errorsmod.Wrapf(types.ErrInvalidAddress, "address: %s, error: %s",
req.GetAddress(), err.Error())
}

affiliateAddr, exists := k.GetReferredBy(sdkCtx, req.GetAddress())
if !exists {
return &types.ReferredByResponse{}, nil
Expand Down
8 changes: 8 additions & 0 deletions protocol/x/affiliates/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ func TestReferredBy(t *testing.T) {
expected: nil,
expectError: nil,
},
"Invalid bech32 address": {
req: &types.ReferredByRequest{
Address: "Foo",
},
setup: func(ctx sdk.Context, k keeper.Keeper) {},
expected: nil,
expectError: types.ErrInvalidAddress,
},
}

for name, tc := range testCases {
Expand Down

0 comments on commit 6bf1852

Please sign in to comment.