forked from protolambda/eth2-testnet-genesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validators.go
136 lines (120 loc) · 3.41 KB
/
validators.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
package main
import (
"crypto/sha256"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"sync/atomic"
"github.com/protolambda/zrnt/eth2/beacon/common"
"github.com/protolambda/zrnt/eth2/beacon/phase0"
"github.com/tyler-smith/go-bip39"
util "github.com/wealdtech/go-eth2-util"
"golang.org/x/sync/errgroup"
"gopkg.in/yaml.v3"
)
func loadValidatorKeys(spec *common.Spec, mnemonicsConfigPath string, tranchesDir string) ([]phase0.KickstartValidatorData, error) {
mnemonics, err := loadMnemonics(mnemonicsConfigPath)
if err != nil {
return nil, err
}
//var validators []phase0.KickstartValidatorData
var valCount uint64 = 0
for _, mnemonicSrc := range mnemonics {
valCount += mnemonicSrc.Count
}
validators := make([]phase0.KickstartValidatorData, valCount)
for m, mnemonicSrc := range mnemonics {
var g errgroup.Group
var prog int32
fmt.Printf("processing mnemonic %d, for %d validators\n", m, mnemonicSrc.Count)
seed, err := seedFromMnemonic(mnemonicSrc.Mnemonic)
if err != nil {
return nil, fmt.Errorf("mnemonic %d is bad", m)
}
pubs := make([]string, mnemonicSrc.Count)
for i := uint64(0); i < mnemonicSrc.Count; i++ {
mIdx := m
idx := i
g.Go(func() error {
signingKey, err := util.PrivateKeyFromSeedAndPath(seed, validatorKeyName(idx))
if err != nil {
return err
}
withdrawalKey, err := util.PrivateKeyFromSeedAndPath(seed, withdrawalKeyName(idx))
if err != nil {
return err
}
// BLS signing key
var data phase0.KickstartValidatorData
copy(data.Pubkey[:], signingKey.PublicKey().Marshal())
pubs[idx] = data.Pubkey.String()
// BLS withdrawal credentials
h := sha256.New()
h.Write(withdrawalKey.PublicKey().Marshal())
copy(data.WithdrawalCredentials[:], h.Sum(nil))
data.WithdrawalCredentials[0] = common.BLS_WITHDRAWAL_PREFIX
// Max effective balance by default for activation
data.Balance = spec.MAX_EFFECTIVE_BALANCE
validators[idx*uint64(mIdx+1)] = data
atomic.AddInt32(&prog, 1)
if prog%100 == 0 {
fmt.Printf("...validator %d/%d\n", prog, mnemonicSrc.Count)
}
return nil
})
}
if err := g.Wait(); err != nil {
return nil, err
}
fmt.Println("Writing pubkeys list file...")
if err := outputPubkeys(filepath.Join(tranchesDir, fmt.Sprintf("tranche_%04d.txt", m)), pubs); err != nil {
return nil, err
}
}
return validators, nil
}
func validatorKeyName(i uint64) string {
return fmt.Sprintf("m/12381/3600/%d/0/0", i)
}
func withdrawalKeyName(i uint64) string {
return fmt.Sprintf("m/12381/3600/%d/0", i)
}
func seedFromMnemonic(mnemonic string) (seed []byte, err error) {
mnemonic = strings.TrimSpace(mnemonic)
if !bip39.IsMnemonicValid(mnemonic) {
return nil, errors.New("mnemonic is not valid")
}
return bip39.NewSeed(mnemonic, ""), nil
}
func outputPubkeys(outPath string, data []string) error {
f, err := os.OpenFile(outPath, os.O_CREATE|os.O_WRONLY, 0777)
if err != nil {
return err
}
defer f.Close()
for _, p := range data {
if _, err := f.WriteString(p + "\n"); err != nil {
return err
}
}
return nil
}
type MnemonicSrc struct {
Mnemonic string `yaml:"mnemonic"`
Count uint64 `yaml:"count"`
}
func loadMnemonics(srcPath string) ([]MnemonicSrc, error) {
f, err := os.Open(srcPath)
if err != nil {
return nil, err
}
defer f.Close()
var data []MnemonicSrc
dec := yaml.NewDecoder(f)
if err := dec.Decode(&data); err != nil {
return nil, err
}
return data, nil
}