/
handler_create.go
296 lines (252 loc) · 8.71 KB
/
handler_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package handler
import (
"archive/zip"
"context"
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
"strings"
"time"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/google/uuid"
"github.com/pkg/errors"
contracts "github.com/prysmaticlabs/prysm/contracts/deposit"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
"github.com/spf13/cobra"
eth2keymanager "github.com/bloxapp/eth2-key-manager"
"github.com/bloxapp/eth2-key-manager/cli/cmd/validator/flag"
"github.com/bloxapp/eth2-key-manager/core"
"github.com/bloxapp/eth2-key-manager/encryptor/keystorev4"
eth1deposit "github.com/bloxapp/eth2-key-manager/eth1_deposit"
"github.com/bloxapp/eth2-key-manager/stores/inmemory"
)
// ValidatorConfig represents the validator config data
type ValidatorConfig struct {
UUID string `json:"uuid"`
Crypto map[string]interface{} `json:"crypto"`
PubKey string `json:"pubkey"`
Path string `json:"path"`
Version uint `json:"version"`
}
// Create is the handler to create validator(s).
func (h *Handler) Create(cmd *cobra.Command, args []string) error {
// Get seeds count
seedsCount, err := flag.GetSeedsCountFlagValue(cmd)
if err != nil {
return errors.Wrap(err, "failed to get seeds count flag value")
}
// Get validators per seed number
validatorsPerSeed, err := flag.GetValidatorsPerSeedFlagValue(cmd)
if err != nil {
return errors.Wrap(err, "failed to get validators per seed flag value")
}
// Get wallet address
walletAddress, err := flag.GetWalletAddressFlagValue(cmd)
if err != nil {
return errors.Wrap(err, "failed to get wallet address flag value")
}
// Get wallet private key
walletPrivateKey, err := flag.GetWalletPrivateKeyFlagValue(cmd)
if err != nil {
return errors.Wrap(err, "failed to get wallet private key flag value")
}
// Get web3 address
web3Addr, err := flag.GetWeb3AddrFlagValue(cmd)
if err != nil {
return errors.Wrap(err, "failed to get web3 address flag value")
}
// Initialize connection with web3 API
rpcClient, err := rpc.Dial(web3Addr)
if err != nil {
return errors.Wrap(err, "failed to create connection with web3 API")
}
defer rpcClient.Close()
client := ethclient.NewClient(rpcClient)
// Fetch wallet balance
walletBalance, err := h.getWalletBalance(client, walletAddress)
if err != nil {
return errors.Wrap(err, "failed to get wallet balance")
}
store := inmemory.NewInMemStore(h.network)
options := ð2keymanager.KeyVaultOptions{}
options.SetStorage(store)
if _, err = eth2keymanager.NewKeyVault(options); err != nil {
return errors.Wrap(err, "failed to create key vault.")
}
wallet, err := store.OpenWallet()
if err != nil {
return errors.Wrap(err, "failed to open wallet")
}
// Check balance
minBalance := big.NewInt(0).
SetUint64(eth1deposit.MaxEffectiveBalanceInGwei * uint64(seedsCount) * uint64(validatorsPerSeed))
minBalance = minBalance.Mul(minBalance, big.NewInt(1e9))
if walletBalance.Cmp(minBalance) < 0 {
return errors.New("insufficient funds for transfer")
}
// Create deposit contract client
depositContract, err := contracts.NewDepositContract(common.HexToAddress(store.Network().DepositContractAddress()), client)
if err != nil {
return err
}
// Create transaction options
txOpts, err := buildTransactionOpts(walletPrivateKey)
if err != nil {
return errors.Wrap(err, "failed to build transaction options")
}
// Generate seed
encryptor := keystorev4.New()
seedToAccounts := make(map[string][]ValidatorConfig)
defer func() {
if err := h.writeResultToFiles(seedToAccounts); err != nil {
h.printer.Error(err)
if err := h.printer.JSON(seedToAccounts); err != nil {
h.printer.Error(err)
}
}
}()
for i := 0; i < seedsCount; i++ {
entropy, err := core.GenerateNewEntropy()
if err != nil {
return errors.Wrap(err, "failed to generate entropy")
}
mnemonic, err := core.EntropyToMnemonic(entropy)
if err != nil {
return errors.Wrap(err, "failed to generate mnemonic from entropy")
}
generatedSeed, err := core.SeedFromEntropy(entropy, "")
if err != nil {
return errors.Wrap(err, "failed to generate seed from entropy")
}
// Create accounts (validators)
for j := 0; j < validatorsPerSeed; j++ {
account, err := wallet.CreateValidatorAccount(generatedSeed, &j)
if err != nil {
return errors.Wrapf(err, "failed to create validator account")
}
cryptoFields, err := encryptor.Encrypt(generatedSeed, "")
if err != nil {
return errors.Wrap(err, "could not encrypt seed phrase into keystore")
}
// Make transaction
if err := h.makeTransaction(depositContract, txOpts, account); err != nil {
return errors.Wrap(err, "failed to make deposit")
}
if _, ok := seedToAccounts[mnemonic]; !ok {
seedToAccounts[mnemonic] = []ValidatorConfig{}
}
seedToAccounts[mnemonic] = append(seedToAccounts[mnemonic], ValidatorConfig{
UUID: uuid.New().String(),
PubKey: hex.EncodeToString(account.ValidatorPublicKey()),
Path: store.Network().FullPath(account.BasePath()),
Version: encryptor.Version(),
Crypto: cryptoFields,
})
}
}
return nil
}
func (h *Handler) getWalletBalance(client *ethclient.Client, walletAddr string) (*big.Int, error) {
address, err := hex.DecodeString(walletAddr)
if err != nil {
return nil, errors.Wrap(err, "failed to HEX decode the given wallet address")
}
res, err := client.BalanceAt(context.Background(), common.BytesToAddress(address), nil)
if err != nil {
return nil, errors.Wrap(err, "failed to get balance of the wallet")
}
return res, nil
}
func (h *Handler) makeTransaction(depositContract *contracts.DepositContract, txOpts *bind.TransactOpts, account core.ValidatorAccount) error {
// Get deposit data for account
depositData, err := account.GetDepositData()
if err != nil {
return errors.Wrap(err, "failed to get deposit data")
}
// Prepare public key
publicKey, err := hex.DecodeString(depositData["publicKey"].(string))
if err != nil {
return errors.Wrap(err, "failed to HEX decode account public key")
}
// Prepare withdrawal credentials
withdrawalCredentials, err := hex.DecodeString(depositData["withdrawalCredentials"].(string))
if err != nil {
return errors.Wrap(err, "failed to HEX decode account withdrawal credentials")
}
// Prepare signature
signature, err := hex.DecodeString(depositData["signature"].(string))
if err != nil {
return errors.Wrap(err, "failed to HEX decode account signature")
}
// Prepare data root
depositDataRoot, err := hex.DecodeString(depositData["depositDataRoot"].(string))
if err != nil {
return errors.Wrap(err, "failed to HEX decode account deposit data root")
}
// Sent deposit contract
tx, err := depositContract.Deposit(
txOpts,
publicKey,
withdrawalCredentials,
signature,
bytesutil.ToBytes32(depositDataRoot),
)
if err != nil {
return errors.Wrap(err, "unable to send transaction to contract")
}
h.printer.Text(fmt.Sprintf("Monitor your transaction on Etherscan here https://goerli.etherscan.io/tx/0x%x", tx.Hash()))
return nil
}
func (h *Handler) writeResultToFiles(results map[string][]ValidatorConfig) error {
if len(results) == 0 {
return errors.New("no results to store")
}
// Create zip file
fileName := fmt.Sprintf("validators_%d_%d", time.Now().Unix(), len(results))
out, cleanup, err := h.resultWriterFactory(fileName)
if err != nil {
return errors.Wrap(err, "failed to create result writer")
}
defer cleanup()
// Create a new zip archive.
w := zip.NewWriter(out)
// Put results into archive
for mnemonic, validators := range results {
for _, validator := range validators {
// Create file
f, err := w.Create(mnemonic + "/keystore-" + strings.ReplaceAll(validator.Path, "/", "_") + ".json")
if err != nil {
return errors.Wrap(err, "failed to create result file")
}
// Put result into file
if err := json.NewEncoder(f).Encode(validator); err != nil {
return errors.Wrap(err, "failed to write result into file")
}
}
}
// Close archive
if err := w.Close(); err != nil {
h.printer.Error(err)
}
return nil
}
func buildTransactionOpts(privateKey string) (*bind.TransactOpts, error) {
// User inputs private key, sign tx with private key
privKey, err := crypto.HexToECDSA(privateKey)
if err != nil {
return nil, errors.Wrap(err, "failed to decode private key")
}
txOps, err := bind.NewKeyedTransactorWithChainID(privKey, big.NewInt(1337))
if err != nil {
return nil, err
}
txOps.Value = new(big.Int).Mul(big.NewInt(int64(eth1deposit.MaxEffectiveBalanceInGwei)), big.NewInt(1e9))
txOps.GasLimit = 500000
txOps.Context = context.Background()
return txOps, nil
}