This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcreate.go
186 lines (158 loc) · 4.96 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package config
import (
"encoding/json"
"errors"
"fmt"
"math/big"
"os"
eigensdkTypes "github.com/Layr-Labs/eigensdk-go/types"
eigenSdkUtils "github.com/Layr-Labs/eigensdk-go/utils"
"github.com/NethermindEth/eigenlayer/cli/prompter"
"github.com/NethermindEth/eigenlayer/internal/types"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
func CreateCmd(p prompter.Prompter) *cobra.Command {
cmd := cobra.Command{
Use: "create",
Short: "Used to create operator config and metadata json sample file",
Long: `
This command is used to create a sample empty operator config file
and also an empty metadata json file which you need to upload for
operator metadata
Both of these are needed for operator registration
`,
RunE: func(cmd *cobra.Command, args []string) error {
op := types.OperatorConfig{}
// Prompt user to generate empty or non-empty files
populate, err := p.Confirm("Would you like to populate the operator config file?")
if err != nil {
return err
}
if populate {
op, err = promptOperatorInfo(&op, p)
if err != nil {
return err
}
}
yamlData, err := yaml.Marshal(&op)
if err != nil {
return err
}
operatorFile := "operator.yaml"
err = os.WriteFile(operatorFile, yamlData, 0o644)
if err != nil {
return err
}
metadata := eigensdkTypes.OperatorMetadata{}
jsonData, err := json.MarshalIndent(metadata, "", " ")
if err != nil {
return err
}
metadataFile := "metadata.json"
err = os.WriteFile(metadataFile, jsonData, 0o644)
if err != nil {
return err
}
fmt.Println("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. ")
fmt.Println("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).")
fmt.Println("Once you have filled in the operator.yaml file, you can register your operator using the configuration file.")
return nil
},
}
return &cmd
}
func promptOperatorInfo(config *types.OperatorConfig, p prompter.Prompter) (types.OperatorConfig, error) {
// Prompt and set operator address
operatorAddress, err := p.InputString("Enter your operator address:", "", "",
func(s string) error {
return validateAddressIsNonZeroAndValid(s)
},
)
if err != nil {
return types.OperatorConfig{}, err
}
config.Operator.Address = operatorAddress
// Prompt to gate stakers approval
gateApproval, err := p.Confirm("Do you want to gate stakers approval?")
if err != nil {
return types.OperatorConfig{}, err
}
// Prompt for address if operator wants to gate approvals
if gateApproval {
delegationApprover, err := p.InputString("Enter your staker approver address:", "", "",
func(s string) error {
isValidAddress := eigenSdkUtils.IsValidEthereumAddress(s)
if !isValidAddress {
return errors.New("address is invalid")
}
return nil
},
)
if err != nil {
return types.OperatorConfig{}, err
}
config.Operator.DelegationApproverAddress = delegationApprover
} else {
config.Operator.DelegationApproverAddress = eigensdkTypes.ZeroAddress
}
// Prompt and set earnings address
earningsAddress, err := p.InputString("Enter your earnings address (default to your operator address):", config.Operator.Address, "",
func(s string) error {
return validateAddressIsNonZeroAndValid(s)
},
)
if err != nil {
return types.OperatorConfig{}, err
}
config.Operator.EarningsReceiverAddress = earningsAddress
// Prompt for eth node
rpcUrl, err := p.InputString("Enter your ETH rpc url:", "http://localhost:8545", "",
func(s string) error { return nil },
)
if err != nil {
return types.OperatorConfig{}, err
}
config.EthRPCUrl = rpcUrl
// Prompt for ecdsa key path
ecdsaKeyPath, err := p.InputString("Enter your ecdsa key path:", "", "",
func(s string) error { return nil },
)
if err != nil {
return types.OperatorConfig{}, err
}
config.PrivateKeyStorePath = ecdsaKeyPath
// Prompt for bls key path
blsKeyPath, err := p.InputString("Enter your bls key path:", "", "",
func(s string) error { return nil },
)
if err != nil {
return types.OperatorConfig{}, err
}
config.BlsPrivateKeyStorePath = blsKeyPath
// Prompt for network & set chainId
chainId, err := p.Select("Select your network:", []string{"mainnet", "goerli", "local"})
if err != nil {
return types.OperatorConfig{}, err
}
switch chainId {
case "mainnet":
config.ChainId = *big.NewInt(1)
case "goerli":
config.ChainId = *big.NewInt(5)
case "local":
config.ChainId = *big.NewInt(31337)
}
config.SignerType = types.LocalKeystoreSigner
return *config, nil
}
func validateAddressIsNonZeroAndValid(address string) error {
if address == eigensdkTypes.ZeroAddress {
return errors.New("address is 0")
}
addressIsValid := eigenSdkUtils.IsValidEthereumAddress(address)
if !addressIsValid {
return errors.New("address is invalid")
}
return nil
}