Skip to content

Commit

Permalink
feat: Allow operator not need password for ecdsa key (#119)
Browse files Browse the repository at this point in the history
* feat: allow not use password for ecdsa

* add operator_ecdsa_address config
  • Loading branch information
fyInALT authored Apr 9, 2024
1 parent 941118c commit 538b23d
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 46 deletions.
2 changes: 2 additions & 0 deletions aggregator/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ func (agg *AggregatorService) CreateTask(req *message.CreateTaskRequest) (*messa
agg.logger.Error("send new task failed", "err", err)
return nil, err
}
} else {
agg.logger.Info("the task had created", "task", task)
}

return &message.CreateTaskResponse{Info: *task}, nil
Expand Down
2 changes: 1 addition & 1 deletion cli/actions/deposit_into_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func DepositIntoStrategy(ctx *cli.Context) error {
log.Println("Config:", string(configJson))
}

operator, err := operator.NewOperatorFromConfig(nodeConfig)
operator, err := operator.NewOperatorFromConfig(nodeConfig, true)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/actions/deregister_operator_with_avs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func DeregisterOperatorWithAvs(ctx *cli.Context) error {
log.Println("Config:", string(configJson))
}

operator, err := operator.NewOperatorFromConfig(nodeConfig)
operator, err := operator.NewOperatorFromConfig(nodeConfig, true)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/actions/print_operator_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func PrintOperatorStatus(ctx *cli.Context) error {
log.Println("Config:", string(configJson))
}

operator, err := operator.NewOperatorFromConfig(nodeConfig)
operator, err := operator.NewOperatorFromConfig(nodeConfig, true)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/actions/register_operator_with_avs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func RegisterOperatorWithAvs(ctx *cli.Context) error {
log.Println("Config:", string(configJson))
}

operator, err := operator.NewOperatorFromConfig(nodeConfig)
operator, err := operator.NewOperatorFromConfig(nodeConfig, true)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/actions/register_operator_with_eigenlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func RegisterOperatorWithEigenlayer(ctx *cli.Context) error {
log.Println("Config:", string(configJson))
}

operator, err := operator.NewOperatorFromConfig(nodeConfig)
operator, err := operator.NewOperatorFromConfig(nodeConfig, true)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions core/config/avs_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type NodeConfig struct {
EthWsUrl string `yaml:"eth_ws_url"`
BlsPrivateKeyStorePath string `yaml:"bls_private_key_store_path"`
EcdsaPrivateKeyStorePath string `yaml:"ecdsa_private_key_store_path"`
OperatorEcdsaAddress string `yaml:"operator_ecdsa_address"`
AggregatorServerIpPortAddress string `yaml:"aggregator_server_ip_port_address"`
AggregatorGRPCServerIpPortAddress string `yaml:"aggregator_grpc_server_ip_port_address"`
AggregatorJSONRPCServerIpPortAddr string `yaml:"aggregator_jsonrpc_server_ip_port_address"`
Expand Down
2 changes: 1 addition & 1 deletion operator/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func operatorMain(ctx *cli.Context) error {
}

log.Println("initializing operator")
operator, err := operator.NewOperatorFromConfig(nodeConfig)
operator, err := operator.NewOperatorFromConfig(nodeConfig, false)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion operator/grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (c *AggregatorGRpcClient) SendSignedTaskResponseToAggregator(signedTaskResp
if err != nil {
resChan <- alert.AlertResponse{
Err: err,
Msg: "call CreateAlertTask failed",
Msg: fmt.Sprintf("call CreateAlertTask failed by %v", err),
}
return
}
Expand Down
113 changes: 74 additions & 39 deletions operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package operator

import (
"context"
"crypto/ecdsa"
"encoding/json"
"fmt"
"log"
Expand All @@ -11,6 +12,7 @@ import (

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/prometheus/client_golang/prometheus"

"github.com/alt-research/avs/core"
Expand Down Expand Up @@ -188,6 +190,11 @@ func withEnvConfig(c config.NodeConfig) config.NodeConfig {
c.Layer2ChainId = uint32(layer2ChainId)
}

operatorEcdsaAddress, ok := os.LookupEnv("OPERATOR_ECDSA_ADDRESS")
if ok && operatorEcdsaAddress != "" {
c.OperatorEcdsaAddress = operatorEcdsaAddress
}

configJson, err := json.MarshalIndent(c, "", " ")
if err != nil {
panic(err)
Expand All @@ -201,7 +208,7 @@ func withEnvConfig(c config.NodeConfig) config.NodeConfig {
// TODO(samlaf): config is a mess right now, since the chainio client constructors
//
// take the config in core (which is shared with aggregator and challenger)
func NewOperatorFromConfig(cfg config.NodeConfig) (*Operator, error) {
func NewOperatorFromConfig(cfg config.NodeConfig, isUseEcdsaKey bool) (*Operator, error) {
var logLevel sdklogging.LogLevel
if cfg.Production {
logLevel = sdklogging.Production
Expand Down Expand Up @@ -256,24 +263,74 @@ func NewOperatorFromConfig(cfg config.NodeConfig) (*Operator, error) {
return nil, err
}

ecdsaKeyPassword, ok := os.LookupEnv("OPERATOR_ECDSA_KEY_PASSWORD")
if !ok {
logger.Warnf("OPERATOR_ECDSA_KEY_PASSWORD env var not set. using empty string")
}
var operatorAddress common.Address
var avsWriter *chainio.AvsWriter
var privateKey *ecdsa.PrivateKey
if isUseEcdsaKey {
var err error
operatorAddress, err = sdkEcdsa.GetAddressFromKeyStoreFile(c.EcdsaPrivateKeyStorePath)
if err != nil {
panic(err)
}

signerConfig := signerv2.Config{
KeystorePath: c.EcdsaPrivateKeyStorePath,
Password: ecdsaKeyPassword,
}
ecdsaKeyPassword, ok := os.LookupEnv("OPERATOR_ECDSA_KEY_PASSWORD")
if !ok {
logger.Warnf("OPERATOR_ECDSA_KEY_PASSWORD env var not set. using empty string")
}

signerV2, _, err := signerv2.SignerFromConfig(signerConfig, chainId)
if err != nil {
panic(err)
}
signerConfig := signerv2.Config{
KeystorePath: c.EcdsaPrivateKeyStorePath,
Password: ecdsaKeyPassword,
}
signerV2, _, err := signerv2.SignerFromConfig(signerConfig, chainId)
if err != nil {
panic(err)
}

privateKey, err := sdkEcdsa.ReadKey(signerConfig.KeystorePath, signerConfig.Password)
if err != nil {
return nil, err
privateKey, err = sdkEcdsa.ReadKey(signerConfig.KeystorePath, signerConfig.Password)
if err != nil {
return nil, err
}

txSender, err := wallet.NewPrivateKeyWallet(ethRpcClient, signerV2, operatorAddress, logger)
if err != nil {
return nil, err
}
txMgr := txmgr.NewSimpleTxManager(txSender, ethRpcClient, logger, operatorAddress)

avsWriter, err = chainio.BuildAvsWriter(
txMgr, common.HexToAddress(c.AVSRegistryCoordinatorAddress),
common.HexToAddress(c.OperatorStateRetrieverAddress), ethRpcClient, logger,
)
if err != nil {
logger.Error("Cannot create AvsWriter", "err", err)
return nil, err
}
} else {
// just use a default value, it just to avoid build client panic
// will never use this value to generate tx.
ecdsaPrivateKey, err := crypto.HexToECDSA("ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80")
if err != nil {
panic(err)
}
privateKey = ecdsaPrivateKey

if c.EcdsaPrivateKeyStorePath != "" {
operatorAddress, err = sdkEcdsa.GetAddressFromKeyStoreFile(c.EcdsaPrivateKeyStorePath)
if err != nil {
panic(err)
}
} else {
if c.OperatorEcdsaAddress == "" {
return nil, fmt.Errorf("If not use EcdsaPrivateKeyStorePath, must use operator_ecdsa_address or `OPERATOR_ECDSA_ADDRESS` env to select ecdsa address!")
}

if !common.IsHexAddress(c.OperatorEcdsaAddress) {
return nil, fmt.Errorf("the operator_ecdsa_address format is not hex address!")
}

operatorAddress = common.HexToAddress(c.OperatorEcdsaAddress)
}
}

chainioConfig := clients.BuildAllConfig{
Expand All @@ -289,28 +346,6 @@ func NewOperatorFromConfig(cfg config.NodeConfig) (*Operator, error) {
panic(err)
}

operatorAddress, err := sdkEcdsa.GetAddressFromKeyStoreFile(signerConfig.KeystorePath)
if err != nil {
panic(err)
}

addr := operatorAddress

txSender, err := wallet.NewPrivateKeyWallet(ethRpcClient, signerV2, addr, logger)
if err != nil {
return nil, err
}
txMgr := txmgr.NewSimpleTxManager(txSender, ethRpcClient, logger, addr)

avsWriter, err := chainio.BuildAvsWriter(
txMgr, common.HexToAddress(c.AVSRegistryCoordinatorAddress),
common.HexToAddress(c.OperatorStateRetrieverAddress), ethRpcClient, logger,
)
if err != nil {
logger.Error("Cannot create AvsWriter", "err", err)
return nil, err
}

avsReader, err := chainio.BuildAvsReader(
common.HexToAddress(c.AVSRegistryCoordinatorAddress),
common.HexToAddress(c.OperatorStateRetrieverAddress),
Expand Down Expand Up @@ -513,7 +548,7 @@ func (o *Operator) Start(ctx context.Context) error {
// https://eigen.nethermind.io/docs/spec/api/
o.logger.Fatal("Error in metrics server", "err", err)
case newTaskCreatedLog := <-o.newTaskCreatedChan:
o.logger.Info("newTaskCreatedLog", "new", newTaskCreatedLog)
o.logger.Info("newTaskCreatedLog", "new", newTaskCreatedLog.Alert)
o.metrics.IncNumTasksReceived()
taskResponse, err := o.ProcessNewTaskCreatedLog(newTaskCreatedLog.Alert)
if err != nil {
Expand Down

0 comments on commit 538b23d

Please sign in to comment.