Skip to content

Commit ceb9b19

Browse files
committed
fix: address encoding to string
1 parent d3ffcac commit ceb9b19

File tree

9 files changed

+30
-16
lines changed

9 files changed

+30
-16
lines changed

api/info/handlers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"github.com/sentinel-official/dvpn-node/context"
1212
)
1313

14-
// HandlerGetInfo returns a handler function to retrieve node information.
15-
func HandlerGetInfo(c *context.Context) gin.HandlerFunc {
14+
// handlerGetInfo returns a handler function to retrieve node information.
15+
func handlerGetInfo(c *context.Context) gin.HandlerFunc {
1616
return func(ctx *gin.Context) {
1717
downLink, upLink := c.SpeedtestResults()
1818
loc := c.Location()

api/info/results.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ type ResultGetInfo struct {
1919
Version *version.Info `json:"version"`
2020
}
2121

22-
func (i *ResultGetInfo) GetType() types.ServiceType {
23-
return types.ServiceTypeFromString(i.Type)
22+
func (r *ResultGetInfo) GetType() types.ServiceType {
23+
return types.ServiceTypeFromString(r.Type)
2424
}

api/info/routes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ import (
77
)
88

99
func RegisterRoutes(ctx *context.Context, r gin.IRouter) {
10-
r.GET("/", HandlerGetInfo(ctx))
10+
r.GET("/", handlerGetInfo(ctx))
1111
}

api/session/handlers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package session
22

33
import (
4-
"bytes"
54
"encoding/json"
65
"errors"
76
"fmt"
@@ -20,6 +19,7 @@ import (
2019
"github.com/sentinel-official/dvpn-node/database/operations"
2120
)
2221

22+
// handlerAddSession returns a handler function to process the request for adding a session.
2323
func handlerAddSession(c *context.Context) gin.HandlerFunc {
2424
return func(ctx *gin.Context) {
2525
// TODO: validate current peer count
@@ -113,8 +113,8 @@ func handlerAddSession(c *context.Context) gin.HandlerFunc {
113113
ctx.JSON(http.StatusInternalServerError, types.NewResponseError(7, err))
114114
return
115115
}
116-
if !bytes.Equal(req.PubKey.Address(), accAddr) {
117-
err = fmt.Errorf("account address mismatch; got %s, expected %s", req.PubKey.Address(), accAddr)
116+
if !req.AccAddr().Equals(accAddr) {
117+
err = fmt.Errorf("account address mismatch; got %s, expected %s", req.AccAddr(), accAddr)
118118
ctx.JSON(http.StatusUnauthorized, types.NewResponseError(7, err))
119119
return
120120
}

api/session/requests.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import (
1010
"github.com/sentinel-official/sentinel-go-sdk/utils"
1111
)
1212

13+
// RequestAddSession represents the request for adding a session.
1314
type RequestAddSession struct {
1415
Data []byte
1516
PubKey types.PubKey
1617
Signature []byte
1718

19+
// Inline struct for the request body
1820
Body struct {
1921
Data string `json:"data" binding:"required,base64,gt=0"`
2022
ID uint64 `json:"id" binding:"required,gt=0"`
@@ -23,29 +25,39 @@ type RequestAddSession struct {
2325
}
2426
}
2527

28+
// Msg generates the message from the session request.
2629
func (r *RequestAddSession) Msg() []byte {
27-
return append(cosmossdk.Uint64ToBigEndian(r.Body.ID), r.Body.Data...)
30+
return append(cosmossdk.Uint64ToBigEndian(r.Body.ID), r.Data...)
2831
}
2932

33+
// AccAddr returns the account address derived from the public key.
34+
func (r *RequestAddSession) AccAddr() cosmossdk.AccAddress {
35+
return r.PubKey.Address().Bytes()
36+
}
37+
38+
// newRequestAddSession binds and decodes the incoming request.
3039
func newRequestAddSession(c *gin.Context) (req *RequestAddSession, err error) {
3140
req = &RequestAddSession{}
3241
if err = c.ShouldBindJSON(&req.Body); err != nil {
3342
return nil, fmt.Errorf("failed to bind json body: %w", err)
3443
}
3544

45+
// Decode base64 encoded data
3646
req.Data, err = base64.StdEncoding.DecodeString(req.Body.Data)
3747
if err != nil {
38-
return nil, fmt.Errorf("failed to decode data: %s", err)
48+
return nil, fmt.Errorf("failed to decode data: %w", err)
3949
}
4050

51+
// Decode base64 encoded signature
4152
req.Signature, err = base64.StdEncoding.DecodeString(req.Body.Signature)
4253
if err != nil {
43-
return nil, fmt.Errorf("failed to decode signature: %s", err)
54+
return nil, fmt.Errorf("failed to decode signature: %w", err)
4455
}
4556

57+
// Decode public key
4658
req.PubKey, err = utils.DecodePubKey(req.Body.PubKey)
4759
if err != nil {
48-
return nil, fmt.Errorf("failed to decode pub_key: %s", err)
60+
return nil, fmt.Errorf("failed to decode pub_key: %w", err)
4961
}
5062

5163
return req, nil

api/session/results.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package session
22

3+
// ResultAddSession represents the response for adding a session.
34
type ResultAddSession struct {
45
Addrs []string `json:"addrs"`
56
Data interface{} `json:"data"`

api/session/routes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/sentinel-official/dvpn-node/context"
77
)
88

9+
// RegisterRoutes registers the routes for the session API.
910
func RegisterRoutes(ctx *context.Context, r gin.IRouter) {
1011
r.POST("/sessions", handlerAddSession(ctx))
1112
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ require (
1212
github.com/gin-contrib/cors v1.7.3
1313
github.com/gin-gonic/gin v1.10.0
1414
github.com/sentinel-official/hub/v12 v12.0.0-rc9
15-
github.com/sentinel-official/sentinel-go-sdk v1.0.0-rc6.0.20250126190423-afc17d6deb5c
15+
github.com/sentinel-official/sentinel-go-sdk v1.0.0-rc6.0.20250127172651-2198709506a2
1616
github.com/soheilhy/cmux v0.1.5
1717
github.com/spf13/cobra v1.8.1
18+
github.com/spf13/pflag v1.0.5
1819
github.com/spf13/viper v1.19.0
1920
gorm.io/driver/sqlite v1.5.7
2021
gorm.io/gorm v1.25.12
@@ -141,7 +142,6 @@ require (
141142
github.com/sourcegraph/conc v0.3.0 // indirect
142143
github.com/spf13/afero v1.11.0 // indirect
143144
github.com/spf13/cast v1.6.0 // indirect
144-
github.com/spf13/pflag v1.0.5 // indirect
145145
github.com/stretchr/testify v1.10.0 // indirect
146146
github.com/subosito/gotenv v1.6.0 // indirect
147147
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,8 @@ github.com/seiflotfy/cuckoofilter v0.0.0-20220411075957-e3b120b3f5fb h1:XfLJSPIO
520520
github.com/seiflotfy/cuckoofilter v0.0.0-20220411075957-e3b120b3f5fb/go.mod h1:bR6DqgcAl1zTcOX8/pE2Qkj9XO00eCNqmKb7lXP8EAg=
521521
github.com/sentinel-official/hub/v12 v12.0.0-rc9 h1:ZLCOUyFCvu8D3S8HNX6sWKm5wE7IS+2mjVjI9Iv1ark=
522522
github.com/sentinel-official/hub/v12 v12.0.0-rc9/go.mod h1:XouLQPAOkYh5PEcForT+YfoPOf3AfNS9dV6moyVb68k=
523-
github.com/sentinel-official/sentinel-go-sdk v1.0.0-rc6.0.20250126190423-afc17d6deb5c h1:CO0qVGT3DgBVyGmmZY4u7OVRhEw+AJfYoJT/pvCCXp4=
524-
github.com/sentinel-official/sentinel-go-sdk v1.0.0-rc6.0.20250126190423-afc17d6deb5c/go.mod h1:h2yuV3Ioe3WFFMl/QWyHKr3MOsr95BtpzRldl9yCl7I=
523+
github.com/sentinel-official/sentinel-go-sdk v1.0.0-rc6.0.20250127172651-2198709506a2 h1:pIfJZMOyUTl0rj2HBA6RA//Lmeuo2w895HjtsyFv2qc=
524+
github.com/sentinel-official/sentinel-go-sdk v1.0.0-rc6.0.20250127172651-2198709506a2/go.mod h1:ce13epMkD/MVQd+hdQrARi6s6jv0GiPLCxdKc1Y0HKU=
525525
github.com/shirou/gopsutil/v4 v4.24.11 h1:WaU9xqGFKvFfsUv94SXcUPD7rCkU0vr/asVdQOBZNj8=
526526
github.com/shirou/gopsutil/v4 v4.24.11/go.mod h1:s4D/wg+ag4rG0WO7AiTj2BeYCRhym0vM7DHbZRxnIT8=
527527
github.com/showwin/speedtest-go v1.7.9 h1:5b3T3U3WSppVXFqsIqF1zdHRYKKVuPNpzFU71HnYNEY=

0 commit comments

Comments
 (0)