From 538b23df49dcdca4a55f6c93edd37f045a08e644 Mon Sep 17 00:00:00 2001 From: fyInALT <97101459+fyInALT@users.noreply.github.com> Date: Tue, 9 Apr 2024 19:57:17 +0800 Subject: [PATCH] feat: Allow operator not need password for ecdsa key (#119) * feat: allow not use password for ecdsa * add operator_ecdsa_address config --- aggregator/service.go | 2 + cli/actions/deposit_into_strategy.go | 2 +- cli/actions/deregister_operator_with_avs.go | 2 +- cli/actions/print_operator_status.go | 2 +- cli/actions/register_operator_with_avs.go | 2 +- .../register_operator_with_eigenlayer.go | 2 +- core/config/avs_config.go | 1 + operator/cmd/main.go | 2 +- operator/grpc_client.go | 2 +- operator/operator.go | 113 ++++++++++++------ 10 files changed, 84 insertions(+), 46 deletions(-) diff --git a/aggregator/service.go b/aggregator/service.go index cf31cf5..d210e82 100644 --- a/aggregator/service.go +++ b/aggregator/service.go @@ -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 diff --git a/cli/actions/deposit_into_strategy.go b/cli/actions/deposit_into_strategy.go index 47a448a..7905fed 100644 --- a/cli/actions/deposit_into_strategy.go +++ b/cli/actions/deposit_into_strategy.go @@ -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 } diff --git a/cli/actions/deregister_operator_with_avs.go b/cli/actions/deregister_operator_with_avs.go index 36e7e40..26d3160 100644 --- a/cli/actions/deregister_operator_with_avs.go +++ b/cli/actions/deregister_operator_with_avs.go @@ -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 } diff --git a/cli/actions/print_operator_status.go b/cli/actions/print_operator_status.go index 13c131c..f12e189 100644 --- a/cli/actions/print_operator_status.go +++ b/cli/actions/print_operator_status.go @@ -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 } diff --git a/cli/actions/register_operator_with_avs.go b/cli/actions/register_operator_with_avs.go index 0b57d74..e8ee202 100644 --- a/cli/actions/register_operator_with_avs.go +++ b/cli/actions/register_operator_with_avs.go @@ -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 } diff --git a/cli/actions/register_operator_with_eigenlayer.go b/cli/actions/register_operator_with_eigenlayer.go index d9f6e88..20f17b7 100644 --- a/cli/actions/register_operator_with_eigenlayer.go +++ b/cli/actions/register_operator_with_eigenlayer.go @@ -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 } diff --git a/core/config/avs_config.go b/core/config/avs_config.go index fdfb602..f743fcd 100644 --- a/core/config/avs_config.go +++ b/core/config/avs_config.go @@ -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"` diff --git a/operator/cmd/main.go b/operator/cmd/main.go index 714e6bd..f0f0caa 100644 --- a/operator/cmd/main.go +++ b/operator/cmd/main.go @@ -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 } diff --git a/operator/grpc_client.go b/operator/grpc_client.go index 66d9cdc..810e26f 100644 --- a/operator/grpc_client.go +++ b/operator/grpc_client.go @@ -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 } diff --git a/operator/operator.go b/operator/operator.go index b818428..8c1d6bf 100644 --- a/operator/operator.go +++ b/operator/operator.go @@ -2,6 +2,7 @@ package operator import ( "context" + "crypto/ecdsa" "encoding/json" "fmt" "log" @@ -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" @@ -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) @@ -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 @@ -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{ @@ -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), @@ -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 {