Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix message construction #2802

Merged
merged 15 commits into from
Jun 27, 2024
23 changes: 23 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ M-make:
M-docker:
- any: ['docker/**/*']

M-docs:
- any: ['docs/**/*']

M-integration:
- any: ['integration-tests/**/*']

Expand All @@ -31,5 +34,25 @@ M-charts:
C-Protocol-Critical:
- 'packages/contracts-core/**/*.sol'

M-vscode-settings:
- any: ['.vscode/**/*']


M-deps:
- 'package.json'
- 'yarn.lock'
- 'go.mod'
- 'go.sum'
- 'go.work'
- 'go.work.sum'

Sol:
- '**/*.ts'

Typescript:
- '**/*.ts'

Javascript:
- '**/*.js'
Go:
- '**/*.go'
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
"solidity.compileUsingRemoteVersion": "v0.8.17+commit.8df45f5f",
"solidity.formatter": "prettier",
"solidity.defaultCompiler": "remote"
}
}
29 changes: 17 additions & 12 deletions contrib/screener-api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"github.com/dubonzi/otelresty"
"github.com/go-resty/resty/v2"
"github.com/google/uuid"
"github.com/synapsecns/sanguine/core"
"github.com/synapsecns/sanguine/core/ginhelper"
"github.com/synapsecns/sanguine/core/metrics"
)
Expand Down Expand Up @@ -78,15 +79,19 @@

nonce := strings.ReplaceAll(uuid.New().String(), "-", "")[:32]
timestamp := fmt.Sprintf("%d", time.Now().Unix())
queryString := ""

bodyBz, err := json.Marshal(body)
if err != nil {
return "", fmt.Errorf("error marshaling body: %w", err)
}

message := fmt.Sprintf("%s%s%s%s%s%s%s",
appid, timestamp, nonce, "POST", "/api/data/sync", queryString, string(bodyBz))
bodyStr, err := core.BytesToJSONString(bodyBz)
if err != nil {
return "", fmt.Errorf("could not convert bytes to json: %w", err)
}

Check warning on line 91 in contrib/screener-api/client/client.go

View check run for this annotation

Codecov / codecov/patch

contrib/screener-api/client/client.go#L88-L91

Added lines #L88 - L91 were not covered by tests

message := fmt.Sprintf("%s;%s;%s;%s;%s;%s",
appid, timestamp, nonce, "POST", "/api/data/sync", bodyStr)

Check warning on line 94 in contrib/screener-api/client/client.go

View check run for this annotation

Codecov / codecov/patch

contrib/screener-api/client/client.go#L93-L94

Added lines #L93 - L94 were not covered by tests

signature := GenerateSignature(appsecret, message)

Expand Down Expand Up @@ -114,9 +119,13 @@

// BlackListBody is the json payload that represents a blacklisted address.
type BlackListBody struct {
Type string `json:"type"`
ID string `json:"id"`
Data string `json:"data"`
Type string `json:"type"`
ID string `json:"id"`
Data Data `json:"data"`
}

// Data is the data field in the BlackListBody.
type Data struct {
Address string `json:"address"`
Network string `json:"network"`
Tag string `json:"tag"`
Expand All @@ -129,12 +138,8 @@
}

// GenerateSignature generates a signature for the request.
func GenerateSignature(
secret,
message string,
) string {
key := []byte(secret)
h := hmac.New(sha256.New, key)
func GenerateSignature(secret, message string) string {
h := hmac.New(sha256.New, []byte(secret))

Check warning on line 142 in contrib/screener-api/client/client.go

View check run for this annotation

Codecov / codecov/patch

contrib/screener-api/client/client.go#L141-L142

Added lines #L141 - L142 were not covered by tests
h.Write([]byte(message))
return hex.EncodeToString(h.Sum(nil))
}
Expand Down
13 changes: 6 additions & 7 deletions contrib/screener-api/db/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ type BlacklistedAddress struct {
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`

Type string `gorm:"column:type" json:"type"`
ID string `gorm:"column:id;primary_key" json:"id"`
Data string `gorm:"column:data" json:"data"`
Address string `gorm:"column:address" json:"address"`
Network string `gorm:"column:network" json:"network"`
Tag string `gorm:"column:tag" json:"tag"`
Remark string `gorm:"column:remark" json:"remark"`
ID string `gorm:"column:id;primaryKey" json:"id"`
Type string `gorm:"column:type" json:"type"`
Address string `gorm:"column:address" json:"address"`
Network string `gorm:"column:network" json:"network"`
Tag string `gorm:"column:tag" json:"tag"`
Remark string `gorm:"column:remark" json:"remark"`
}
2 changes: 1 addition & 1 deletion contrib/screener-api/db/sql/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
{Name: idName},
},
DoUpdates: clause.AssignmentColumns([]string{
idName, typeName, dataName, addressName, networkName, tagName, remarkName},
idName, typeName, dataAddressName, dataRemarkName, dataNetworkName, dataTagName},

Check warning on line 59 in contrib/screener-api/db/sql/base/base.go

View check run for this annotation

Codecov / codecov/patch

contrib/screener-api/db/sql/base/base.go#L59

Added line #L59 was not covered by tests
),
}).Create(&body)
if dbTx.Error != nil {
Expand Down
22 changes: 10 additions & 12 deletions contrib/screener-api/db/sql/base/namer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@
func init() {
namer := dbcommon.NewNamer(GetAllModels())

addressName = namer.GetConsistentName("Address")
typeName = namer.GetConsistentName("Type")
idName = namer.GetConsistentName("ID")
dataName = namer.GetConsistentName("Data")
networkName = namer.GetConsistentName("Network")
tagName = namer.GetConsistentName("Tag")
remarkName = namer.GetConsistentName("Remark")
dataAddressName = namer.GetConsistentName("Address")
dataNetworkName = namer.GetConsistentName("Network")
dataTagName = namer.GetConsistentName("Tag")
dataRemarkName = namer.GetConsistentName("Remark")

Check warning on line 15 in contrib/screener-api/db/sql/base/namer.go

View check run for this annotation

Codecov / codecov/patch

contrib/screener-api/db/sql/base/namer.go#L12-L15

Added lines #L12 - L15 were not covered by tests
}

var (
addressName string
typeName string
idName string
dataName string
networkName string
tagName string
remarkName string
typeName string
idName string
dataAddressName string
dataNetworkName string
dataTagName string
dataRemarkName string
)
3 changes: 0 additions & 3 deletions contrib/screener-api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ const docTemplate = `{
"createdAt": {
"type": "string"
},
"data": {
"type": "string"
},
"id": {
"type": "string"
},
Expand Down
3 changes: 0 additions & 3 deletions contrib/screener-api/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,6 @@
"createdAt": {
"type": "string"
},
"data": {
"type": "string"
},
"id": {
"type": "string"
},
Expand Down
2 changes: 0 additions & 2 deletions contrib/screener-api/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ definitions:
type: string
createdAt:
type: string
data:
type: string
id:
type: string
network:
Expand Down
53 changes: 34 additions & 19 deletions contrib/screener-api/screener/screener.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,26 +277,24 @@
span.SetAttributes(
attribute.String("type", blacklistBody.Type),
(attribute.String("id", blacklistBody.ID)),
(attribute.String("data", blacklistBody.Data)),
(attribute.String("network", blacklistBody.Network)),
(attribute.String("tag", blacklistBody.Tag)),
(attribute.String("remark", blacklistBody.Remark)),
(attribute.String("address", blacklistBody.Address)),
(attribute.String("network", blacklistBody.Data.Network)),
(attribute.String("tag", blacklistBody.Data.Tag)),
(attribute.String("remark", blacklistBody.Data.Remark)),
(attribute.String("address", blacklistBody.Data.Address)),
)

blacklistedAddress := db.BlacklistedAddress{
Type: blacklistBody.Type,
ID: blacklistBody.ID,
Data: blacklistBody.Data,
Network: blacklistBody.Network,
Tag: blacklistBody.Tag,
Remark: blacklistBody.Remark,
Address: strings.ToLower(blacklistBody.Address),
Network: blacklistBody.Data.Network,
Tag: blacklistBody.Data.Tag,
Remark: blacklistBody.Data.Remark,
Address: strings.ToLower(blacklistBody.Data.Address),
Comment on lines +280 to +292
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improve test coverage and refine error handling in screener.go.

The newly added lines in blacklistAddress and authMiddleware functions lack test coverage. Additionally, the error handling in these functions can be improved to provide more detailed feedback and ensure robust operation.

Consider refining the error handling by:

  • Providing more specific error messages.
  • Logging errors at critical points to aid in debugging.

Would you like help in enhancing the error handling mechanisms or adding unit tests to cover these functionalities?

Also applies to: 366-369

}

s.blacklistCacheMux.Lock()
defer s.blacklistCacheMux.Unlock()
s.blacklistCache[blacklistBody.Address] = true
s.blacklistCache[blacklistBody.Data.Address] = true

switch blacklistBody.Type {
case "create":
Expand All @@ -306,7 +304,7 @@
return
}

span.AddEvent("blacklistedAddress", trace.WithAttributes(attribute.String("address", blacklistBody.Address)))
span.AddEvent("blacklistedAddress", trace.WithAttributes(attribute.String("address", blacklistBody.Data.Address)))
c.JSON(http.StatusOK, gin.H{"status": "success"})
return

Expand All @@ -317,7 +315,7 @@
return
}

span.AddEvent("blacklistedAddress", trace.WithAttributes(attribute.String("address", blacklistBody.Address)))
span.AddEvent("blacklistedAddress", trace.WithAttributes(attribute.String("address", blacklistBody.Data.Address)))
c.JSON(http.StatusOK, gin.H{"status": "success"})
return

Expand All @@ -328,7 +326,7 @@
return
}

span.AddEvent("blacklistedAddress", trace.WithAttributes(attribute.String("address", blacklistBody.Address)))
span.AddEvent("blacklistedAddress", trace.WithAttributes(attribute.String("address", blacklistBody.Data.Address)))
c.JSON(http.StatusOK, gin.H{"status": "success"})
return

Expand All @@ -352,17 +350,34 @@
signature := c.Request.Header.Get("X-Signature-signature")
queryString := c.Request.URL.RawQuery

bodyBytes, err := io.ReadAll(c.Request.Body)
bodyBz, err := io.ReadAll(c.Request.Body)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "could not read request body"})
c.Abort()
return
}
c.Request.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
bodyStr := string(bodyBytes)
// Put it back so we can read it again for DB operations.
c.Request.Body = io.NopCloser(bytes.NewBuffer(bodyBz))

message := fmt.Sprintf("%s%s%s%s%s%s%s",
appID, timestamp, nonce, "POST", "/api/data/sync", queryString, bodyStr)
bodyStr, err := core.BytesToJSONString(bodyBz)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "could not convert bytes to json"})
c.Abort()
return
}

Check warning on line 367 in contrib/screener-api/screener/screener.go

View check run for this annotation

Codecov / codecov/patch

contrib/screener-api/screener/screener.go#L364-L367

Added lines #L364 - L367 were not covered by tests

var message string
if len(queryString) > 0 {
message = fmt.Sprintf(
"%s;%s;%s;%s;%s;%s;%s",
appID, timestamp, nonce, "POST", "/api/data/sync", queryString, bodyStr,
)

Check warning on line 374 in contrib/screener-api/screener/screener.go

View check run for this annotation

Codecov / codecov/patch

contrib/screener-api/screener/screener.go#L371-L374

Added lines #L371 - L374 were not covered by tests
} else {
message = fmt.Sprintf(
"%s;%s;%s;%s;%s;%s",
appID, timestamp, nonce, "POST", "/api/data/sync", bodyStr,
)
}

expectedSignature := client.GenerateSignature(cfg.AppSecret, message)

Expand Down
22 changes: 8 additions & 14 deletions contrib/screener-api/screener/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package screener_test
import (
"context"
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -198,23 +197,18 @@ func blacklistTestWithOperation(t *testing.T, operation string, apiClient client
return statuses, fmt.Errorf("error generating random number: %w", err)
}

dataMap := map[string]string{"key": fmt.Sprintf("value-%d", randomNumber)}
dataStr, err := json.Marshal(dataMap)
if err != nil {
return statuses, fmt.Errorf("error marshaling data: %w", err)
}

var body client.BlackListBody

if operation == "create" || operation == "update" {
body = client.BlackListBody{
Type: operation,
ID: fmt.Sprintf("unique-id-%d", randomNumber),
Data: string(dataStr),
Address: fmt.Sprintf("address-%d", randomNumber),
Network: fmt.Sprintf("network-%d", randomNumber),
Tag: fmt.Sprintf("tag-%d", randomNumber),
Remark: "remark",
Type: operation,
ID: fmt.Sprintf("unique-id-%d", randomNumber),
Data: client.Data{
Address: fmt.Sprintf("address-%d", randomNumber),
Network: fmt.Sprintf("network-%d", randomNumber),
Tag: fmt.Sprintf("tag-%d", randomNumber),
Remark: "remark",
},
}
} else {
body = client.BlackListBody{
Expand Down
19 changes: 19 additions & 0 deletions core/bytes.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
package core

import (
"encoding/json"
"fmt"
)

// BytesToSlice converts a 32 bit array to a slice slice.
func BytesToSlice(bytes [32]byte) []byte {
rawBytes := make([]byte, len(bytes))
copy(rawBytes, bytes[:])
return rawBytes
}

// BytesToJSONString converts a 32 bit array to a JSON string without escapes, newlines, etc.
func BytesToJSONString(bz []byte) (string, error) {
var jsonData map[string]interface{}
if err := json.Unmarshal(bz, &jsonData); err != nil {
return "", fmt.Errorf("failed to unmarshal JSON: %w", err)
}
formattedJSON, err := json.Marshal(jsonData)
if err != nil {
return "", fmt.Errorf("failed to marshal JSON: %w", err)
}

Check warning on line 24 in core/bytes.go

View check run for this annotation

Codecov / codecov/patch

core/bytes.go#L23-L24

Added lines #L23 - L24 were not covered by tests

return string(formattedJSON), nil
}
Loading
Loading