Skip to content

Commit

Permalink
chore: trim 0x from private key (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
shrimalmadhur authored Aug 21, 2024
1 parent 327df84 commit e140239
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
include .env

GO_LINES_IGNORED_DIRS=
GO_PACKAGES=./pkg/... ./cmd/...
GO_PACKAGES=./pkg/... ./cmd/... ./internal/...
GO_FOLDERS=$(shell echo ${GO_PACKAGES} | sed -e "s/\.\///g" | sed -e "s/\/\.\.\.//g")

help:
Expand Down
4 changes: 3 additions & 1 deletion internal/versionupdate/check_version_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ func Check(currentVersion string) {
fmt.Println()
fmt.Printf("There is a new version (%s) for this library available.\n", greenVersion)
fmt.Printf("Your current running verison is (%s).\n", yellowOldVersion)
fmt.Println("Please update (https://github.com/Layr-Labs/eigenlayer-cli#install-eigenlayer-cli-using-a-binary) to get latest features and bug fixes.")
fmt.Println(
"Please update (https://github.com/Layr-Labs/eigenlayer-cli#install-eigenlayer-cli-using-a-binary) to get latest features and bug fixes.",
)
fmt.Println()
}
}
6 changes: 5 additions & 1 deletion pkg/internal/common/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func GetSignerConfig(cCtx *cli.Context, logger eigensdkLogger.Logger) (*types.Si
ecdsaPrivateKeyString := cCtx.String(flags.EcdsaPrivateKeyFlag.Name)
if !IsEmptyString(ecdsaPrivateKeyString) {
logger.Debug("Using private key signer")
pk, err := crypto.HexToECDSA(ecdsaPrivateKeyString)
pk, err := crypto.HexToECDSA(Trim0x(ecdsaPrivateKeyString))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -438,3 +438,7 @@ func GetNoSendTxOpts(from common.Address) *bind.TransactOpts {
NoSend: true,
}
}

func Trim0x(s string) string {
return strings.TrimPrefix(s, "0x")
}
8 changes: 4 additions & 4 deletions pkg/keys/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"fmt"
"math/big"
"regexp"
"strings"

"github.com/Layr-Labs/eigenlayer-cli/pkg/internal/common"
"github.com/Layr-Labs/eigenlayer-cli/pkg/telemetry"

"github.com/Layr-Labs/eigenlayer-cli/pkg/utils"

"github.com/Layr-Labs/eigensdk-go/crypto/bls"
"github.com/ethereum/go-ethereum/crypto"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -65,7 +65,7 @@ This command will import keys in $HOME/.eigenlayer/operator_keys/ location

switch keyType {
case KeyTypeECDSA:
privateKey = strings.TrimPrefix(privateKey, "0x")
privateKey = common.Trim0x(privateKey)
privateKeyPair, err := crypto.HexToECDSA(privateKey)
if err != nil {
return err
Expand All @@ -86,7 +86,7 @@ This command will import keys in $HOME/.eigenlayer/operator_keys/ location
// Try to parse as hex
fmt.Println("Importing from hex")
z := new(big.Int)
privateKey = strings.TrimPrefix(privateKey, "0x")
privateKey = common.Trim0x(privateKey)
_, ok := z.SetString(privateKey, 16)
if !ok {
return ErrInvalidHexPrivateKey
Expand Down
8 changes: 4 additions & 4 deletions pkg/keys/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"github.com/urfave/cli/v2"

"github.com/Layr-Labs/eigensdk-go/crypto/bls"

"github.com/Layr-Labs/eigenlayer-cli/pkg/internal/common"
prompterMock "github.com/Layr-Labs/eigenlayer-cli/pkg/utils/mocks"

"github.com/stretchr/testify/assert"
"github.com/urfave/cli/v2"
"go.uber.org/mock/gomock"
)

Expand Down Expand Up @@ -202,7 +202,7 @@ func TestImportCmd(t *testing.T) {
if tt.args[1] == KeyTypeECDSA {
key, err := GetECDSAPrivateKey(tt.keyPath, "")
assert.NoError(t, err)
assert.Equal(t, strings.Trim(tt.args[3], "0x"), hex.EncodeToString(key.D.Bytes()))
assert.Equal(t, common.Trim0x(tt.args[3]), hex.EncodeToString(key.D.Bytes()))
} else if tt.args[1] == KeyTypeBLS {
key, err := bls.ReadPrivateKeyFromFile(tt.keyPath, "")
assert.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions pkg/operator/keys/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"fmt"
"math/big"
"regexp"
"strings"

"github.com/Layr-Labs/eigenlayer-cli/pkg/internal/common"
"github.com/Layr-Labs/eigenlayer-cli/pkg/telemetry"

"github.com/Layr-Labs/eigenlayer-cli/pkg/utils"

"github.com/Layr-Labs/eigensdk-go/crypto/bls"
"github.com/ethereum/go-ethereum/crypto"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -65,7 +65,7 @@ This command will import keys in $HOME/.eigenlayer/operator_keys/ location

switch keyType {
case KeyTypeECDSA:
privateKey = strings.TrimPrefix(privateKey, "0x")
privateKey = common.Trim0x(privateKey)
privateKeyPair, err := crypto.HexToECDSA(privateKey)
if err != nil {
return err
Expand All @@ -86,7 +86,7 @@ This command will import keys in $HOME/.eigenlayer/operator_keys/ location
// Try to parse as hex
fmt.Println("Importing from hex")
z := new(big.Int)
privateKey = strings.TrimPrefix(privateKey, "0x")
privateKey = common.Trim0x(privateKey)
_, ok := z.SetString(privateKey, 16)
if !ok {
return ErrInvalidHexPrivateKey
Expand Down
8 changes: 4 additions & 4 deletions pkg/operator/keys/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"github.com/urfave/cli/v2"

"github.com/Layr-Labs/eigensdk-go/crypto/bls"

"github.com/Layr-Labs/eigenlayer-cli/pkg/internal/common"
prompterMock "github.com/Layr-Labs/eigenlayer-cli/pkg/utils/mocks"

"github.com/stretchr/testify/assert"
"github.com/urfave/cli/v2"
"go.uber.org/mock/gomock"
)

Expand Down Expand Up @@ -203,7 +203,7 @@ func TestImportCmd(t *testing.T) {
if tt.args[1] == KeyTypeECDSA {
key, err := GetECDSAPrivateKey(tt.keyPath, "")
assert.NoError(t, err)
assert.Equal(t, strings.Trim(tt.args[3], "0x"), hex.EncodeToString(key.D.Bytes()))
assert.Equal(t, common.Trim0x(tt.args[3]), hex.EncodeToString(key.D.Bytes()))
} else if tt.args[1] == KeyTypeBLS {
key, err := bls.ReadPrivateKeyFromFile(tt.keyPath, "")
assert.NoError(t, err)
Expand Down

0 comments on commit e140239

Please sign in to comment.