middleware.go

Overview

The [middleware.go](/projects/291/69263) file defines HTTP middleware functions intended for validating public keys (`pubkey`) within incoming HTTP requests in a Cosmos blockchain-related API service. These middleware functions intercept requests and ensure that the `pubkey` parameter is present and valid before allowing the request to proceed to the next handler. This validation step helps enforce data integrity and prevents unnecessary processing of invalid requests.

Specifically, the file provides two middleware functions:

Both middleware functions leverage validation helper functions (assumed to be implemented elsewhere in the codebase) to check the format and correctness of the `pubkey`.

Detailed Descriptions

Functions


ValidatePubkey

func ValidatePubkey(next http.Handler) http.Handler

**Description:** Middleware function that validates the presence and correctness of a general public key (`pubkey`) extracted from the request URL variables. If validation passes, the middleware forwards the request to the next handler; otherwise, it returns an HTTP 400 Bad Request error with an appropriate message.

**Parameters:**

**Returns:**

**Behavior:**

  1. Extracts the pubkey parameter from the URL path variables using the mux router.

  2. Checks if pubkey is present and not empty.

  3. Validates the pubkey format using the helper function IsValidAddress(pubkey).

  4. If validation fails, responds with HTTP 400 and an error message using api.HandleError.

  5. If validation succeeds, calls the next handler.

**Usage Example:**

router := mux.NewRouter()
router.Handle("/accounts/{pubkey}", ValidatePubkey(accountHandler))

In this example, the middleware ensures that requests to `/accounts/{pubkey}` contain a valid public key before `accountHandler` executes.


ValidateValidatorPubkey

func ValidateValidatorPubkey(next http.Handler) http.Handler

**Description:** Middleware function that validates the presence and correctness of a validator-specific public key (`pubkey`) extracted from the request URL variables. Similar to `ValidatePubkey`, but the validation is specific to validator addresses.

**Parameters:**

**Returns:**

**Behavior:**

  1. Extracts the pubkey parameter from the URL path variables using the mux router.

  2. Checks if pubkey is present and not empty.

  3. Validates the pubkey format using the helper function IsValidValidatorAddress(pubkey).

  4. If validation fails, responds with HTTP 400 and an error message via api.HandleError.

  5. If validation succeeds, calls the next handler.

**Usage Example:**

router := mux.NewRouter()
router.Handle("/validators/{pubkey}", ValidateValidatorPubkey(validatorHandler))

Here, requests to `/validators/{pubkey}` require a valid validator public key before `validatorHandler` processes them.


Important Implementation Details

Interaction with Other Parts of the System


Visual Diagram: Middleware Flowchart

This flowchart represents the validation workflow of each middleware function as it handles incoming HTTP requests.

flowchart TD
    A[Incoming HTTP Request] --> B{Extract "pubkey" from URL}
    B -->|No "pubkey" found| E[Respond 400 Bad Request: "pubkey required"]
    B -->|"pubkey" found| C{Validate "pubkey" format}
    C -->|Invalid pubkey| F[Respond 400 Bad Request: "invalid pubkey"]
    C -->|Valid pubkey| D[Call next handler]

For `ValidateValidatorPubkey`, the "Validate `pubkey` format" step uses `IsValidValidatorAddress` instead of `IsValidAddress`.


Summary

The [middleware.go](/projects/291/69263) file provides essential middleware utilities for validating public keys in HTTP request paths within a Cosmos blockchain API context. These middleware functions help ensure that only requests with valid `pubkey` parameters are processed further, improving API robustness and reducing potential errors downstream. The modular middleware design integrates seamlessly with Gorilla Mux and enforces consistent validation and error handling practices across the API service.