diff --git a/docs/docs.go b/docs/docs.go index a0e3c65..f44ec4a 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -143,9 +143,9 @@ const docTemplate = `{ ], "responses": { "200": { - "description": "Result", + "description": "Delegation check result", "schema": { - "$ref": "#/definitions/handlers.Result" + "$ref": "#/definitions/handlers.DelegationCheckPublicResponse" } }, "400": { @@ -379,6 +379,17 @@ const docTemplate = `{ } } }, + "handlers.DelegationCheckPublicResponse": { + "type": "object", + "properties": { + "code": { + "type": "integer" + }, + "data": { + "type": "boolean" + } + } + }, "handlers.PublicResponse-array_services_DelegationPublic": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index dc34a4f..b903030 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -135,9 +135,9 @@ ], "responses": { "200": { - "description": "Result", + "description": "Delegation check result", "schema": { - "$ref": "#/definitions/handlers.Result" + "$ref": "#/definitions/handlers.DelegationCheckPublicResponse" } }, "400": { @@ -371,6 +371,17 @@ } } }, + "handlers.DelegationCheckPublicResponse": { + "type": "object", + "properties": { + "code": { + "type": "integer" + }, + "data": { + "type": "boolean" + } + } + }, "handlers.PublicResponse-array_services_DelegationPublic": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 6cf6525..8ec56d2 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -7,6 +7,13 @@ definitions: statusCode: type: integer type: object + handlers.DelegationCheckPublicResponse: + properties: + code: + type: integer + data: + type: boolean + type: object handlers.PublicResponse-array_services_DelegationPublic: properties: data: @@ -326,9 +333,9 @@ paths: - application/json responses: "200": - description: Result + description: Delegation check result schema: - $ref: '#/definitions/handlers.Result' + $ref: '#/definitions/handlers.DelegationCheckPublicResponse' "400": description: 'Error: Bad Request' schema: diff --git a/internal/api/handlers/handler.go b/internal/api/handlers/handler.go index 288e33a..b0eb5f9 100644 --- a/internal/api/handlers/handler.go +++ b/internal/api/handlers/handler.go @@ -17,6 +17,10 @@ type Handler struct { services *services.Services } +type ResultOptions struct { + Code int +} + type paginationResponse struct { NextKey string `json:"next_key"` } diff --git a/internal/api/handlers/staker.go b/internal/api/handlers/staker.go index cd408d4..e1fce9b 100644 --- a/internal/api/handlers/staker.go +++ b/internal/api/handlers/staker.go @@ -7,6 +7,11 @@ import ( "github.com/babylonlabs-io/staking-api-service/internal/utils" ) +type DelegationCheckPublicResponse struct { + Data bool `json:"data"` + Code int `json:"code"` +} + // GetStakerDelegations @Summary Get staker delegations // @Description Retrieves delegations for a given staker // @Produce json @@ -46,7 +51,7 @@ func (h *Handler) GetStakerDelegations(request *http.Request) (*Result, *types.E // @Produce json // @Param address query string true "Staker BTC address in Taproot/Native Segwit format" // @Param timeframe query string false "Check if the delegation is active within the provided timeframe" Enums(today) -// @Success 200 {object} Result "Result" +// @Success 200 {object} DelegationCheckPublicResponse "Delegation check result" // @Failure 400 {object} types.Error "Error: Bad Request" // @Router /v1/staker/delegation/check [get] func (h *Handler) CheckStakerDelegationExist(request *http.Request) (*Result, *types.Error) { @@ -65,7 +70,7 @@ func (h *Handler) CheckStakerDelegationExist(request *http.Request) (*Result, *t return nil, err } if _, exist := addressToPkMapping[address]; !exist { - return NewResult(false), nil + return buildDelegationCheckResponse(false), nil } exist, err := h.services.CheckStakerHasActiveDelegationByPk( @@ -75,7 +80,16 @@ func (h *Handler) CheckStakerDelegationExist(request *http.Request) (*Result, *t return nil, err } - return NewResult(exist), nil + return buildDelegationCheckResponse(exist), nil +} + +func buildDelegationCheckResponse(exist bool) *Result { + return &Result{ + Data: &DelegationCheckPublicResponse{ + Data: exist, Code: 0, + }, + Status: http.StatusOK, + } } func parseTimeframeToAfterTimestamp(timeframe string) (int64, *types.Error) { diff --git a/internal/api/middlewares/cors.go b/internal/api/middlewares/cors.go index d522b05..eb01b6f 100644 --- a/internal/api/middlewares/cors.go +++ b/internal/api/middlewares/cors.go @@ -10,7 +10,7 @@ import ( const ( maxAge = 300 stakerDelegationCheckPath = "/v1/staker/delegation/check" - galxeOrigin = "https://app.galxe.com" + dashboardGalxeOrigin = "https://dashboard.galxe.com" ) func CorsMiddleware(cfg *config.Config) func(http.Handler) http.Handler { @@ -21,7 +21,7 @@ func CorsMiddleware(cfg *config.Config) func(http.Handler) http.Handler { if r.URL.Path == stakerDelegationCheckPath { // Return CORS options specific to this route return cors.Options{ - AllowedOrigins: []string{galxeOrigin}, + AllowedOrigins: []string{dashboardGalxeOrigin}, AllowedMethods: []string{"GET", "OPTIONS", "POST"}, MaxAge: maxAge, // Below is a workaround to allow the custom CORS header to be set. @@ -44,9 +44,10 @@ func CorsMiddleware(cfg *config.Config) func(http.Handler) http.Handler { cors := cors.New(options) corsHandler := cors.Handler(next) - // Set the custom cors header for the special route for GET requests - if r.URL.Path == stakerDelegationCheckPath { - w.Header().Set("Access-Control-Allow-Origin", galxeOrigin) + origin := r.Header.Get("Origin") + // Set the custom cors header for the special route for GET requests from Galxe + if r.URL.Path == stakerDelegationCheckPath && origin == dashboardGalxeOrigin { + w.Header().Set("Access-Control-Allow-Origin", origin) w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST") if r.Method == http.MethodOptions { // This is a preflight request, respond with 204 immediately diff --git a/tests/integration_test/staker_test.go b/tests/integration_test/staker_test.go index 2913db5..02db97f 100644 --- a/tests/integration_test/staker_test.go +++ b/tests/integration_test/staker_test.go @@ -135,7 +135,7 @@ func TestActiveStakingFetchedByStakerPkWithInvalidPaginationKey(t *testing.T) { assert.Equal(t, "invalid pagination key format", response.Message) } -func TestCheckStakerDelegationAllowOptionRequest(t *testing.T) { +func TestCheckStakerDelegationAllowOptionRequestForGalxe(t *testing.T) { testServer := setupTestServer(t, nil) defer testServer.Close() @@ -144,7 +144,7 @@ func TestCheckStakerDelegationAllowOptionRequest(t *testing.T) { req, err := http.NewRequest("OPTIONS", url, nil) assert.NoError(t, err) req.Header.Add("Access-Control-Request-Method", "GET") - req.Header.Add("Origin", "https://app.galxe.com") + req.Header.Add("Origin", "https://dashboard.galxe.com") req.Header.Add("Access-Control-Request-Headers", "Content-Type") // Send the request @@ -154,8 +154,15 @@ func TestCheckStakerDelegationAllowOptionRequest(t *testing.T) { // Check that the status code is HTTP 204 assert.Equal(t, http.StatusNoContent, resp.StatusCode, "expected HTTP 204") - assert.Equal(t, "https://app.galxe.com", resp.Header.Get("Access-Control-Allow-Origin"), "expected Access-Control-Allow-Origin to be https://app.galxe.com") + assert.Equal(t, "https://dashboard.galxe.com", resp.Header.Get("Access-Control-Allow-Origin"), "expected Access-Control-Allow-Origin to be https://dashboard.galxe.com") assert.Equal(t, "GET, OPTIONS, POST", resp.Header.Get("Access-Control-Allow-Methods"), "expected Access-Control-Allow-Methods to be GET and OPTIONS") + + // Try with a different origin + req.Header.Add("Origin", "https://dashboard.galxe.com") + resp, err = client.Do(req) + assert.NoError(t, err) + defer resp.Body.Close() + assert.Equal(t, http.StatusNoContent, resp.StatusCode, "expected HTTP 204") } func FuzzCheckStakerActiveDelegations(f *testing.F) { @@ -384,10 +391,12 @@ func fetchCheckStakerActiveDelegations( bodyBytes, err := io.ReadAll(resp.Body) assert.NoError(t, err, "reading response body should not fail") - var response handlers.PublicResponse[bool] + var response handlers.DelegationCheckPublicResponse err = json.Unmarshal(bodyBytes, &response) assert.NoError(t, err, "unmarshalling response body should not fail") + assert.Equal(t, response.Code, 0, "expected response code to be 0") + return response.Data }