Skip to content

Commit

Permalink
resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-babylonlabs committed Oct 26, 2024
1 parent c8c3354 commit aeefa68
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 206 deletions.
2 changes: 1 addition & 1 deletion config/config-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ assets:
host: "http://ord-poc.devnet.babylonchain.io"
port: 8888
timeout: 1000
terms_acceptance:
terms_acceptance_logging:
enabled: true
2 changes: 1 addition & 1 deletion config/config-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ assets:
host: "http://ord-poc.devnet.babylonchain.io"
port: 8888
timeout: 5000
terms_acceptance:
terms_acceptance_logging:
enabled: true
56 changes: 0 additions & 56 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,46 +38,6 @@ const docTemplate = `{
}
}
},
"/terms-acceptance": {
"post": {
"description": "Accept terms by the staker's BTC address (Taproot or Native Segwit)",
"produces": [
"application/json"
],
"parameters": [
{
"type": "string",
"description": "Staker BTC address in Taproot/Native Segwit format",
"name": "address",
"in": "query",
"required": true
},
{
"description": "Terms acceptance request",
"name": "terms_accepted",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/handlers.TermsAcceptanceRequest"
}
}
],
"responses": {
"200": {
"description": "Terms acceptance result",
"schema": {
"$ref": "#/definitions/handlers.TermsAcceptancePublic"
}
},
"400": {
"description": "Error: Bad Request",
"schema": {
"$ref": "#/definitions/github_com_babylonlabs-io_staking-api-service_internal_types.Error"
}
}
}
}
},
"/v1/delegation": {
"get": {
"description": "Retrieves a delegation by a given transaction hash",
Expand Down Expand Up @@ -514,22 +474,6 @@ const docTemplate = `{
}
}
},
"handlers.TermsAcceptancePublic": {
"type": "object",
"properties": {
"status": {
"type": "boolean"
}
}
},
"handlers.TermsAcceptanceRequest": {
"type": "object",
"properties": {
"terms_accepted": {
"type": "boolean"
}
}
},
"handlers.UnbondDelegationRequestPayload": {
"type": "object",
"properties": {
Expand Down
56 changes: 0 additions & 56 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,46 +30,6 @@
}
}
},
"/terms-acceptance": {
"post": {
"description": "Accept terms by the staker's BTC address (Taproot or Native Segwit)",
"produces": [
"application/json"
],
"parameters": [
{
"type": "string",
"description": "Staker BTC address in Taproot/Native Segwit format",
"name": "address",
"in": "query",
"required": true
},
{
"description": "Terms acceptance request",
"name": "terms_accepted",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/handlers.TermsAcceptanceRequest"
}
}
],
"responses": {
"200": {
"description": "Terms acceptance result",
"schema": {
"$ref": "#/definitions/handlers.TermsAcceptancePublic"
}
},
"400": {
"description": "Error: Bad Request",
"schema": {
"$ref": "#/definitions/github_com_babylonlabs-io_staking-api-service_internal_types.Error"
}
}
}
}
},
"/v1/delegation": {
"get": {
"description": "Retrieves a delegation by a given transaction hash",
Expand Down Expand Up @@ -506,22 +466,6 @@
}
}
},
"handlers.TermsAcceptancePublic": {
"type": "object",
"properties": {
"status": {
"type": "boolean"
}
}
},
"handlers.TermsAcceptanceRequest": {
"type": "object",
"properties": {
"terms_accepted": {
"type": "boolean"
}
}
},
"handlers.UnbondDelegationRequestPayload": {
"type": "object",
"properties": {
Expand Down
36 changes: 0 additions & 36 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,6 @@ definitions:
status:
type: integer
type: object
handlers.TermsAcceptancePublic:
properties:
status:
type: boolean
type: object
handlers.TermsAcceptanceRequest:
properties:
terms_accepted:
type: boolean
type: object
handlers.UnbondDelegationRequestPayload:
properties:
staker_signed_signature_hex:
Expand Down Expand Up @@ -267,32 +257,6 @@ paths:
schema:
type: string
summary: Health check endpoint
/terms-acceptance:
post:
description: Accept terms by the staker's BTC address (Taproot or Native Segwit)
parameters:
- description: Staker BTC address in Taproot/Native Segwit format
in: query
name: address
required: true
type: string
- description: Terms acceptance request
in: body
name: terms_accepted
required: true
schema:
$ref: '#/definitions/handlers.TermsAcceptanceRequest'
produces:
- application/json
responses:
"200":
description: Terms acceptance result
schema:
$ref: '#/definitions/handlers.TermsAcceptancePublic'
"400":
description: 'Error: Bad Request'
schema:
$ref: '#/definitions/github_com_babylonlabs-io_staking-api-service_internal_types.Error'
/v1/delegation:
get:
description: Retrieves a delegation by a given transaction hash
Expand Down
26 changes: 13 additions & 13 deletions internal/api/handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,26 +173,26 @@ func parseStateFilterQuery(
return stateEnum, nil
}

// parseTermsAcceptanceQuery parses the terms acceptance query and returns the address, public key, and terms accepted
func parseTermsAcceptanceQuery(request *http.Request, btcNetParam *chaincfg.Params) (string, string, bool, *types.Error) {
var req TermsAcceptanceRequest
// parseTermsAcceptanceLoggingRequest parses the terms acceptance request bdoy and returns the address and public key
func parseTermsAcceptanceLoggingRequest(request *http.Request, btcNetParam *chaincfg.Params) (string, string, *types.Error) {
var req TermsAcceptanceLoggingRequest
if err := json.NewDecoder(request.Body).Decode(&req); err != nil {
return "", "", false, types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, "Invalid request payload")
return "", "", types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, "Invalid request payload")
}

address, err := parseBtcAddressQuery(request, "address", btcNetParam)
if err != nil {
return "", "", false, err
// Validate the Bitcoin address
if _, err := utils.CheckBtcAddressType(req.Address, btcNetParam); err != nil {
return "", "", types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, "Invalid Bitcoin address")
}

publicKey, err := parsePublicKeyQuery(request, "public_key", false)
if err != nil {
return "", "", false, err
// Validate the public key
if _, err := utils.GetSchnorrPkFromHex(req.PublicKey); err != nil {
return "", "", types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, "Invalid public key")
}

if address == "" || publicKey == "" {
return "", "", false, types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, "Address and public key are required")
if req.Address == "" || req.PublicKey == "" {
return "", "", types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, "Address and public key are required")
}

return address, publicKey, req.TermsAccepted, nil
return req.Address, req.PublicKey, nil
}
13 changes: 6 additions & 7 deletions internal/api/handlers/terms.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@ import (
"github.com/babylonlabs-io/staking-api-service/internal/types"
)

type TermsAcceptanceRequest struct {
Address string `json:"address"`
TermsAccepted bool `json:"terms_accepted"`
PublicKey string `json:"public_key"`
type TermsAcceptanceLoggingRequest struct {
Address string `json:"address"`
PublicKey string `json:"public_key"`
}

type TermsAcceptancePublic struct {
Status bool `json:"status"`
}

func (h *Handler) AcceptTerms(request *http.Request) (*Result, *types.Error) {
address, publicKey, termsAccepted, err := parseTermsAcceptanceQuery(request, h.config.Server.BTCNetParam)
func (h *Handler) LogTermsAcceptance(request *http.Request) (*Result, *types.Error) {
address, publicKey, err := parseTermsAcceptanceLoggingRequest(request, h.config.Server.BTCNetParam)
if err != nil {
return nil, err
}

if err := h.services.AcceptTerms(request.Context(), address, publicKey, termsAccepted); err != nil {
if err := h.services.AcceptTerms(request.Context(), address, publicKey); err != nil {
return nil, err
}

Expand Down
4 changes: 2 additions & 2 deletions internal/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ func (a *Server) SetupRoutes(r *chi.Mux) {
handlers := a.handlers
r.Get("/healthcheck", registerHandler(handlers.HealthCheck))

if a.cfg.TermsAcceptance.Enabled {
r.Post("/terms-acceptance", registerHandler(handlers.AcceptTerms))
if a.cfg.TermsAcceptanceLogging.Enabled {
r.Post("/terms-acceptance", registerHandler(handlers.LogTermsAcceptance))
}

r.Get("/v1/staker/delegations", registerHandler(handlers.GetStakerDelegations))
Expand Down
16 changes: 6 additions & 10 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
)

type Config struct {
Server *ServerConfig `mapstructure:"server"`
Db *DbConfig `mapstructure:"db"`
Queue *queue.QueueConfig `mapstructure:"queue"`
Metrics *MetricsConfig `mapstructure:"metrics"`
Assets *AssetsConfig `mapstructure:"assets"`
TermsAcceptance *TermsAcceptanceConfig `mapstructure:"terms_acceptance"`
Server *ServerConfig `mapstructure:"server"`
Db *DbConfig `mapstructure:"db"`
Queue *queue.QueueConfig `mapstructure:"queue"`
Metrics *MetricsConfig `mapstructure:"metrics"`
Assets *AssetsConfig `mapstructure:"assets"`
TermsAcceptanceLogging *TermsAcceptanceConfig `mapstructure:"terms_acceptance_logging"`
}

func (cfg *Config) Validate() error {
Expand All @@ -42,10 +42,6 @@ func (cfg *Config) Validate() error {
}
}

if err := cfg.TermsAcceptance.Validate(); err != nil {
return err
}

return nil
}

Expand Down
6 changes: 0 additions & 6 deletions internal/config/terms.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,3 @@ package config
type TermsAcceptanceConfig struct {
Enabled bool `mapstructure:"enabled"`
}

func (cfg *TermsAcceptanceConfig) Validate() error {
// No validation needed for Enabled field as it can be either true or false

return nil
}
2 changes: 1 addition & 1 deletion internal/db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ type DBClient interface {
ctx context.Context,
paginationToken string,
) (*DbResultMap[model.DelegationDocument], error)
// Accept terms saves the acceptance of the terms of service of the public key
// SaveTermsAcceptance saves the acceptance of the terms of service of the public key
SaveTermsAcceptance(ctx context.Context, termsAcceptance *model.TermsAcceptance) error
}

Expand Down
12 changes: 6 additions & 6 deletions internal/db/model/terms.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package model

import "time"
import (
"go.mongodb.org/mongo-driver/bson/primitive"
)

type TermsAcceptance struct {
Id string `bson:"_id"`
Address string `bson:"address"`
PublicKey string `bson:"public_key"`
TermsAccepted bool `bson:"terms_accepted"`
UpdatedAt time.Time `bson:"updated_at"`
Id primitive.ObjectID `bson:"_id,omitempty"`
Address string `bson:"address"`
PublicKey string `bson:"public_key"`
}
7 changes: 3 additions & 4 deletions internal/services/terms.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import (
"github.com/babylonlabs-io/staking-api-service/internal/types"
)

func (s *Services) AcceptTerms(ctx context.Context, address, publicKey string, termsAccepted bool) *types.Error {
func (s *Services) AcceptTerms(ctx context.Context, address, publicKey string) *types.Error {
termsAcceptance := &model.TermsAcceptance{
Address: address,
PublicKey: publicKey,
TermsAccepted: termsAccepted,
Address: address,
PublicKey: publicKey,
}

if err := s.DbClient.SaveTermsAcceptance(ctx, termsAcceptance); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion tests/config/config-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ assets:
host: "http://ord-poc.devnet.babylonchain.io"
port: 8888
timeout: 100
terms_acceptance:
terms_acceptance_logging:
enabled: true
Loading

0 comments on commit aeefa68

Please sign in to comment.