Skip to content

Commit dcd3acd

Browse files
committed
chore: test more utils funcs
1 parent 24fa758 commit dcd3acd

File tree

4 files changed

+452
-58
lines changed

4 files changed

+452
-58
lines changed

types/operator.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"log/slog"
77
"math/big"
8+
"net/http"
89

910
"github.com/Layr-Labs/eigensdk-go/crypto/bls"
1011
"github.com/Layr-Labs/eigensdk-go/utils"
@@ -46,7 +47,7 @@ func (o Operator) Validate() error {
4647
return utils.WrapError(ErrInvalidMetadataUrl, err)
4748
}
4849

49-
body, err := utils.ReadPublicURL(o.MetadataUrl)
50+
body, err := utils.ReadPublicURL(o.MetadataUrl, http.DefaultClient)
5051
if err != nil {
5152
return utils.WrapError(ErrReadingMetadataUrlResponse, err)
5253
}

types/operator_metadata.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package types
22

33
import (
4+
"net/http"
5+
46
"github.com/Layr-Labs/eigensdk-go/utils"
57
)
68

@@ -45,7 +47,7 @@ func (om *OperatorMetadata) Validate() error {
4547
return ErrLogoRequired
4648
}
4749

48-
if err = utils.IsImageURL(om.Logo); err != nil {
50+
if err = utils.IsImageURL(om.Logo, http.DefaultClient); err != nil {
4951
return err
5052
}
5153

utils/utils.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func EcdsaPrivateKeyToAddress(privateKey *ecdsa.PrivateKey) (gethcommon.Address,
105105
return crypto.PubkeyToAddress(*publicKeyECDSA), nil
106106
}
107107

108+
// RoundUpDivideBig divides two positive big.Int numbers and rounds up the result.
108109
func RoundUpDivideBig(a, b *big.Int) *big.Int {
109110
one := new(big.Int).SetUint64(1)
110111
res := new(big.Int)
@@ -118,14 +119,13 @@ func IsValidEthereumAddress(address string) bool {
118119
return ethAddrPattern.MatchString(address)
119120
}
120121

121-
func ReadPublicURL(url string) ([]byte, error) {
122-
// allow no redirects
123-
httpClient := http.Client{
124-
CheckRedirect: func(req *http.Request, via []*http.Request) error {
125-
return http.ErrUseLastResponse
126-
},
127-
Timeout: 3 * time.Second,
122+
func ReadPublicURL(url string, httpClient *http.Client) ([]byte, error) {
123+
// Allow no redirects
124+
httpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
125+
return http.ErrUseLastResponse
128126
}
127+
httpClient.Timeout = 3 * time.Second
128+
129129
resp, err := httpClient.Get(url)
130130
if err != nil {
131131
return []byte{}, err
@@ -213,7 +213,7 @@ func CheckIfUrlIsValid(rawUrl string) error {
213213
return nil
214214
}
215215

216-
func IsImageURL(urlString string) error {
216+
func IsImageURL(urlString string, httpClient *http.Client) error {
217217
// Parse the URL
218218
parsedURL, err := url.Parse(urlString)
219219
if err != nil {
@@ -229,7 +229,7 @@ func IsImageURL(urlString string) error {
229229
// Check if the extension is in the list of image extensions
230230
for _, imgExt := range ImageExtensions {
231231
if strings.EqualFold(extension, imgExt) {
232-
imageBytes, err := ReadPublicURL(urlString)
232+
imageBytes, err := ReadPublicURL(urlString, httpClient)
233233
if err != nil {
234234
return err
235235
}

0 commit comments

Comments
 (0)