-
Notifications
You must be signed in to change notification settings - Fork 978
/
cli_manager.go
98 lines (90 loc) · 3.13 KB
/
cli_manager.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
package accounts
import (
"context"
"io"
"net/http"
"os"
"time"
"github.com/pkg/errors"
grpcutil "github.com/prysmaticlabs/prysm/v5/api/grpc"
"github.com/prysmaticlabs/prysm/v5/crypto/bls"
"github.com/prysmaticlabs/prysm/v5/validator/accounts/wallet"
beaconApi "github.com/prysmaticlabs/prysm/v5/validator/client/beacon-api"
iface "github.com/prysmaticlabs/prysm/v5/validator/client/iface"
nodeClientFactory "github.com/prysmaticlabs/prysm/v5/validator/client/node-client-factory"
validatorClientFactory "github.com/prysmaticlabs/prysm/v5/validator/client/validator-client-factory"
validatorHelpers "github.com/prysmaticlabs/prysm/v5/validator/helpers"
"github.com/prysmaticlabs/prysm/v5/validator/keymanager"
"github.com/prysmaticlabs/prysm/v5/validator/keymanager/derived"
"google.golang.org/grpc"
)
// NewCLIManager allows for managing validator accounts via CLI commands.
func NewCLIManager(opts ...Option) (*CLIManager, error) {
acc := &CLIManager{
mnemonicLanguage: derived.DefaultMnemonicLanguage,
inputReader: os.Stdin,
}
for _, opt := range opts {
if err := opt(acc); err != nil {
return nil, err
}
}
return acc, nil
}
// CLIManager defines a struct capable of performing various validator
// wallet & account operations via the command line.
type CLIManager struct {
wallet *wallet.Wallet
keymanager keymanager.IKeymanager
keymanagerKind keymanager.Kind
showPrivateKeys bool
listValidatorIndices bool
deletePublicKeys bool
importPrivateKeys bool
readPasswordFile bool
skipMnemonicConfirm bool
dialOpts []grpc.DialOption
grpcHeaders []string
beaconRPCProvider string
walletKeyCount int
privateKeyFile string
passwordFilePath string
keysDir string
mnemonicLanguage string
backupsDir string
backupsPassword string
filteredPubKeys []bls.PublicKey
rawPubKeys [][]byte
formattedPubKeys []string
exitJSONOutputPath string
walletDir string
walletPassword string
mnemonic string
numAccounts int
mnemonic25thWord string
beaconApiEndpoint string
beaconApiTimeout time.Duration
inputReader io.Reader
}
func (acm *CLIManager) prepareBeaconClients(ctx context.Context) (*iface.ValidatorClient, *iface.NodeClient, error) {
if acm.dialOpts == nil {
return nil, nil, errors.New("failed to construct dial options for beacon clients")
}
ctx = grpcutil.AppendHeaders(ctx, acm.grpcHeaders)
grpcConn, err := grpc.DialContext(ctx, acm.beaconRPCProvider, acm.dialOpts...)
if err != nil {
return nil, nil, errors.Wrapf(err, "could not dial endpoint %s", acm.beaconRPCProvider)
}
conn := validatorHelpers.NewNodeConnection(
grpcConn,
acm.beaconApiEndpoint,
acm.beaconApiTimeout,
)
restHandler := &beaconApi.BeaconApiJsonRestHandler{
HttpClient: http.Client{Timeout: acm.beaconApiTimeout},
Host: acm.beaconApiEndpoint,
}
validatorClient := validatorClientFactory.NewValidatorClient(conn, restHandler)
nodeClient := nodeClientFactory.NewNodeClient(conn, restHandler)
return &validatorClient, &nodeClient, nil
}