|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "math/big" |
| 8 | + "os" |
| 9 | + |
| 10 | + "github.com/Layr-Labs/eigenlayer-cli/pkg/types" |
| 11 | + "github.com/Layr-Labs/eigenlayer-cli/pkg/utils" |
| 12 | + eigensdkTypes "github.com/Layr-Labs/eigensdk-go/types" |
| 13 | + eigenSdkUtils "github.com/Layr-Labs/eigensdk-go/utils" |
| 14 | + "github.com/urfave/cli/v2" |
| 15 | + "gopkg.in/yaml.v3" |
| 16 | +) |
| 17 | + |
| 18 | +func CreateCmd(p utils.Prompter) *cli.Command { |
| 19 | + createCmd := &cli.Command{ |
| 20 | + Name: "create", |
| 21 | + Usage: "Used to create operator config and metadata json sample file", |
| 22 | + UsageText: "create", |
| 23 | + Description: ` |
| 24 | + This command is used to create a sample empty operator config file |
| 25 | + and also an empty metadata json file which you need to upload for |
| 26 | + operator metadata |
| 27 | +
|
| 28 | + Both of these are needed for operator registration |
| 29 | + `, |
| 30 | + Action: func(ctx *cli.Context) error { |
| 31 | + op := types.OperatorConfig{} |
| 32 | + |
| 33 | + // Prompt user to generate empty or non-empty files |
| 34 | + populate, err := p.Confirm("Would you like to populate the operator config file?") |
| 35 | + if err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + |
| 39 | + if populate { |
| 40 | + op, err = promptOperatorInfo(&op, p) |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + yamlData, err := yaml.Marshal(&op) |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + operatorFile := "operator.yaml" |
| 51 | + err = os.WriteFile(operatorFile, yamlData, 0o644) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + metadata := eigensdkTypes.OperatorMetadata{} |
| 57 | + jsonData, err := json.MarshalIndent(metadata, "", " ") |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + metadataFile := "metadata.json" |
| 63 | + err = os.WriteFile(metadataFile, jsonData, 0o644) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + fmt.Println( |
| 69 | + "Created operator.yaml and metadata.json files. Please fill in the smart contract configuration details(el_slasher_address and bls_public_key_compendium_address) provided by EigenLayer team. ", |
| 70 | + ) |
| 71 | + fmt.Println( |
| 72 | + "Please fill in the metadata.json file and upload it to a public url. Then update the operator.yaml file with the url (metadata_url).", |
| 73 | + ) |
| 74 | + fmt.Println( |
| 75 | + "Once you have filled in the operator.yaml file, you can register your operator using the configuration file.", |
| 76 | + ) |
| 77 | + return nil |
| 78 | + }, |
| 79 | + } |
| 80 | + |
| 81 | + return createCmd |
| 82 | +} |
| 83 | + |
| 84 | +func promptOperatorInfo(config *types.OperatorConfig, p utils.Prompter) (types.OperatorConfig, error) { |
| 85 | + // Prompt and set operator address |
| 86 | + operatorAddress, err := p.InputString("Enter your operator address:", "", "", |
| 87 | + func(s string) error { |
| 88 | + return validateAddressIsNonZeroAndValid(s) |
| 89 | + }, |
| 90 | + ) |
| 91 | + if err != nil { |
| 92 | + return types.OperatorConfig{}, err |
| 93 | + } |
| 94 | + config.Operator.Address = operatorAddress |
| 95 | + |
| 96 | + // TODO(madhur): Disabling this for now as the feature doesn't work but |
| 97 | + // we need to keep the code around for future |
| 98 | + // Prompt to gate stakers approval |
| 99 | + //gateApproval, err := p.Confirm("Do you want to gate stakers approval?") |
| 100 | + //if err != nil { |
| 101 | + // return types.OperatorConfig{}, err |
| 102 | + //} |
| 103 | + // Prompt for address if operator wants to gate approvals |
| 104 | + //if gateApproval { |
| 105 | + // delegationApprover, err := p.InputString("Enter your staker approver address:", "", "", |
| 106 | + // func(s string) error { |
| 107 | + // isValidAddress := eigenSdkUtils.IsValidEthereumAddress(s) |
| 108 | + // |
| 109 | + // if !isValidAddress { |
| 110 | + // return errors.New("address is invalid") |
| 111 | + // } |
| 112 | + // |
| 113 | + // return nil |
| 114 | + // }, |
| 115 | + // ) |
| 116 | + // if err != nil { |
| 117 | + // return types.OperatorConfig{}, err |
| 118 | + // } |
| 119 | + // config.Operator.DelegationApproverAddress = delegationApprover |
| 120 | + //} else { |
| 121 | + // config.Operator.DelegationApproverAddress = eigensdkTypes.ZeroAddress |
| 122 | + //} |
| 123 | + |
| 124 | + // TODO(madhur): Remove this once we have the feature working and want to prompt users for this address |
| 125 | + config.Operator.DelegationApproverAddress = eigensdkTypes.ZeroAddress |
| 126 | + |
| 127 | + // Prompt and set earnings address |
| 128 | + earningsAddress, err := p.InputString( |
| 129 | + "Enter your earnings address (default to your operator address):", |
| 130 | + config.Operator.Address, |
| 131 | + "", |
| 132 | + func(s string) error { |
| 133 | + return validateAddressIsNonZeroAndValid(s) |
| 134 | + }, |
| 135 | + ) |
| 136 | + if err != nil { |
| 137 | + return types.OperatorConfig{}, err |
| 138 | + } |
| 139 | + config.Operator.EarningsReceiverAddress = earningsAddress |
| 140 | + |
| 141 | + // Prompt for eth node |
| 142 | + rpcUrl, err := p.InputString("Enter your ETH rpc url:", "http://localhost:8545", "", |
| 143 | + func(s string) error { return nil }, |
| 144 | + ) |
| 145 | + if err != nil { |
| 146 | + return types.OperatorConfig{}, err |
| 147 | + } |
| 148 | + config.EthRPCUrl = rpcUrl |
| 149 | + |
| 150 | + // Prompt for ecdsa key path |
| 151 | + ecdsaKeyPath, err := p.InputString("Enter your ecdsa key path:", "", "", |
| 152 | + func(s string) error { return nil }, |
| 153 | + ) |
| 154 | + if err != nil { |
| 155 | + return types.OperatorConfig{}, err |
| 156 | + } |
| 157 | + config.PrivateKeyStorePath = ecdsaKeyPath |
| 158 | + |
| 159 | + // Prompt for bls key path |
| 160 | + blsKeyPath, err := p.InputString("Enter your bls key path:", "", "", |
| 161 | + func(s string) error { return nil }, |
| 162 | + ) |
| 163 | + if err != nil { |
| 164 | + return types.OperatorConfig{}, err |
| 165 | + } |
| 166 | + config.BlsPrivateKeyStorePath = blsKeyPath |
| 167 | + |
| 168 | + // Prompt for network & set chainId |
| 169 | + chainId, err := p.Select("Select your network:", []string{"mainnet", "goerli", "holesky", "local"}) |
| 170 | + if err != nil { |
| 171 | + return types.OperatorConfig{}, err |
| 172 | + } |
| 173 | + |
| 174 | + switch chainId { |
| 175 | + case "mainnet": |
| 176 | + config.ChainId = *big.NewInt(1) |
| 177 | + case "goerli": |
| 178 | + config.ChainId = *big.NewInt(5) |
| 179 | + case "holesky": |
| 180 | + config.ChainId = *big.NewInt(17000) |
| 181 | + case "local": |
| 182 | + config.ChainId = *big.NewInt(31337) |
| 183 | + } |
| 184 | + |
| 185 | + config.SignerType = types.LocalKeystoreSigner |
| 186 | + |
| 187 | + return *config, nil |
| 188 | +} |
| 189 | + |
| 190 | +func validateAddressIsNonZeroAndValid(address string) error { |
| 191 | + if address == eigensdkTypes.ZeroAddress { |
| 192 | + return errors.New("address is 0") |
| 193 | + } |
| 194 | + |
| 195 | + addressIsValid := eigenSdkUtils.IsValidEthereumAddress(address) |
| 196 | + |
| 197 | + if !addressIsValid { |
| 198 | + return errors.New("address is invalid") |
| 199 | + } |
| 200 | + |
| 201 | + return nil |
| 202 | +} |
0 commit comments