Skip to content

Commit

Permalink
add validations
Browse files Browse the repository at this point in the history
  • Loading branch information
TymKh committed Nov 1, 2024
1 parent 6d6c922 commit eec040f
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 21 deletions.
2 changes: 1 addition & 1 deletion adapters/database/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func toDomainBuilderWithCredentials(builder BuilderWithCredentials) (*domain.Bui
for _, cred := range builder.Credentials {
s.Services = append(s.Services, domain.BuilderServices{
TLSCert: cred.TLSCert.String,
ECDSAPubKey: cred.ECDSAPubKey,
ECDSAPubKey: domain.Bytes2Address(cred.ECDSAPubKey),
Service: cred.Service,
})
}
Expand Down
12 changes: 11 additions & 1 deletion domain/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package domain
import (
"errors"
"net"

"github.com/ethereum/go-ethereum/common"
)

var (
Expand Down Expand Up @@ -43,6 +45,14 @@ type BuilderWithServices struct {

type BuilderServices struct {
TLSCert string
ECDSAPubKey []byte
ECDSAPubKey *common.Address
Service string
}

func Bytes2Address(b []byte) *common.Address {
if len(b) == 0 {
return nil
}
addr := common.BytesToAddress(b)
return &addr
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ require (
github.com/buger/jsonparser v1.1.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/ethereum/go-ethereum v1.14.11 // indirect
github.com/go-gorp/gorp/v3 v3.1.0 // indirect
github.com/holiman/uint256 v1.3.1 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgtype v1.14.3 // indirect
github.com/jackc/pgx/v5 v5.7.1 // indirect
Expand All @@ -31,6 +33,7 @@ require (
github.com/valyala/fastrand v1.1.0 // indirect
github.com/valyala/histogram v1.2.0 // indirect
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/sys v0.25.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ethereum/go-ethereum v1.14.11 h1:8nFDCUUE67rPc6AKxFj7JKaOa2W/W1Rse3oS6LvvxEY=
github.com/ethereum/go-ethereum v1.14.11/go.mod h1:+l/fr42Mma+xBnhefL/+z11/hcmJ2egl+ScIVPjhc7E=
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/httplog/v2 v2.1.1 h1:ojojiu4PIaoeJ/qAO4GWUxJqvYUTobeo7zmuHQJAxRk=
Expand All @@ -32,6 +34,8 @@ github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRx
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/holiman/uint256 v1.3.1 h1:JfTzmih28bittyHM8z360dCjIA9dbPIBlcTI6lmctQs=
github.com/holiman/uint256 v1.3.1/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E=
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
github.com/jackc/chunkreader/v2 v2.0.1/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
Expand Down Expand Up @@ -175,6 +179,8 @@ golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ=
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
Expand Down
19 changes: 4 additions & 15 deletions ports/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ports

import (
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -231,14 +230,13 @@ func (bhs *BuilderHubHandler) RegisterCredentials(w http.ResponseWriter, r *http
w.WriteHeader(http.StatusBadRequest)
return
}

ecdsaBytes, err := fromHex(sc.ECDSAPubkey)
if err != nil {
bhs.log.Error("Failed to decode ecdsa public key", "err", err)
if sc.TLSCert == "" && sc.ECDSAPubkey == nil {
bhs.log.Error("No credentials provided")
w.WriteHeader(http.StatusBadRequest)
return
}
err = bhs.builderHubService.RegisterCredentialsForBuilder(r.Context(), builder.Name, service, sc.TLSCert, ecdsaBytes, measurementName, authData.AttestationType)

err = bhs.builderHubService.RegisterCredentialsForBuilder(r.Context(), builder.Name, service, sc.TLSCert, sc.ECDSAPubkey.Bytes(), measurementName, authData.AttestationType)
if err != nil {
bhs.log.Error("Failed to register credentials", "err", err)
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -247,12 +245,3 @@ func (bhs *BuilderHubHandler) RegisterCredentials(w http.ResponseWriter, r *http

w.WriteHeader(http.StatusOK)
}

func fromHex(s string) ([]byte, error) {
s = strings.TrimPrefix(s, "0x")
b, err := hex.DecodeString(s)
if err != nil {
return nil, err
}
return b, nil
}
8 changes: 4 additions & 4 deletions ports/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
package ports

import (
"encoding/hex"
"encoding/json"
"errors"

"github.com/ethereum/go-ethereum/common"
"github.com/flashbots/builder-hub/domain"
)

Expand All @@ -24,8 +24,8 @@ type BuilderWithServiceCreds struct {
}

type ServiceCred struct {
TLSCert string `json:"tls_cert,omitempty"`
ECDSAPubkey string `json:"ecdsa_pubkey_address,omitempty"`
TLSCert string `json:"tls_cert,omitempty"`
ECDSAPubkey *common.Address `json:"ecdsa_pubkey_address,omitempty"`
}

// MarshalJSON is a custom json marshaller. Unfortunately, there seems to be no way to inline map[string]Service when marshalling
Expand Down Expand Up @@ -56,7 +56,7 @@ func fromDomainBuilderWithServices(builder domain.BuilderWithServices) BuilderWi
for _, v := range builder.Services {
b.ServiceCreds[v.Service] = ServiceCred{
TLSCert: v.TLSCert,
ECDSAPubkey: "0x" + hex.EncodeToString(v.ECDSAPubKey),
ECDSAPubkey: v.ECDSAPubKey,
}
}

Expand Down
36 changes: 36 additions & 0 deletions ports/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ports

import (
"encoding/json"
"testing"
)

func TestServiceCreds(t *testing.T) {
str := `{"tls_cert":"something", "ecdsa_pubkey_address":"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"}`
var sc ServiceCred
err := json.Unmarshal([]byte(str), &sc)
if err != nil {
t.Error("Failed to unmarshal ServiceCred", err)
}
if sc.TLSCert != "something" {
t.Error("Failed to unmarshal TLS cert")
}
if sc.ECDSAPubkey.Hex() != "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" {
t.Error("Failed to unmarshal ECDSA pubkey")
}
}

func TestServiceCredsNil(t *testing.T) {
str := `{"tls_cert":"something"}`
var sc ServiceCred
err := json.Unmarshal([]byte(str), &sc)
if err != nil {
t.Error("Failed to unmarshal ServiceCred", err)
}
if sc.TLSCert != "something" {
t.Error("Failed to unmarshal TLS cert")
}
if sc.ECDSAPubkey != nil {
t.Error("Failed to unmarshal ECDSA pubkey")
}
}

0 comments on commit eec040f

Please sign in to comment.