2022-04-28 14:46:46 +00:00
|
|
|
package accounts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-10-31 16:33:54 +00:00
|
|
|
"io"
|
2024-01-11 16:03:35 +00:00
|
|
|
"net/http"
|
2023-10-31 16:33:54 +00:00
|
|
|
"os"
|
2022-11-11 17:33:48 +00:00
|
|
|
"time"
|
2022-04-28 14:46:46 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2024-02-15 05:46:47 +00:00
|
|
|
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"
|
2022-04-28 14:46:46 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewCLIManager allows for managing validator accounts via CLI commands.
|
2023-10-20 16:45:33 +00:00
|
|
|
func NewCLIManager(opts ...Option) (*CLIManager, error) {
|
|
|
|
acc := &CLIManager{
|
2023-01-26 23:44:38 +00:00
|
|
|
mnemonicLanguage: derived.DefaultMnemonicLanguage,
|
2023-10-31 16:33:54 +00:00
|
|
|
inputReader: os.Stdin,
|
2023-01-26 23:44:38 +00:00
|
|
|
}
|
2022-04-28 14:46:46 +00:00
|
|
|
for _, opt := range opts {
|
|
|
|
if err := opt(acc); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return acc, nil
|
|
|
|
}
|
|
|
|
|
2023-10-20 16:45:33 +00:00
|
|
|
// CLIManager defines a struct capable of performing various validator
|
2022-08-24 16:57:03 +00:00
|
|
|
// wallet & account operations via the command line.
|
2023-10-20 16:45:33 +00:00
|
|
|
type CLIManager struct {
|
2022-04-28 14:46:46 +00:00
|
|
|
wallet *wallet.Wallet
|
|
|
|
keymanager keymanager.IKeymanager
|
2022-09-02 14:56:47 +00:00
|
|
|
keymanagerKind keymanager.Kind
|
2022-04-28 14:46:46 +00:00
|
|
|
showPrivateKeys bool
|
|
|
|
listValidatorIndices bool
|
2022-05-17 23:13:36 +00:00
|
|
|
deletePublicKeys bool
|
2022-06-16 14:14:03 +00:00
|
|
|
importPrivateKeys bool
|
|
|
|
readPasswordFile bool
|
2022-09-02 14:56:47 +00:00
|
|
|
skipMnemonicConfirm bool
|
2022-04-28 14:46:46 +00:00
|
|
|
dialOpts []grpc.DialOption
|
|
|
|
grpcHeaders []string
|
|
|
|
beaconRPCProvider string
|
2022-05-17 23:13:36 +00:00
|
|
|
walletKeyCount int
|
2022-06-16 14:14:03 +00:00
|
|
|
privateKeyFile string
|
|
|
|
passwordFilePath string
|
|
|
|
keysDir string
|
2022-10-26 21:04:00 +00:00
|
|
|
mnemonicLanguage string
|
2022-06-07 15:19:12 +00:00
|
|
|
backupsDir string
|
|
|
|
backupsPassword string
|
2022-06-13 15:17:46 +00:00
|
|
|
filteredPubKeys []bls.PublicKey
|
|
|
|
rawPubKeys [][]byte
|
|
|
|
formattedPubKeys []string
|
2023-04-17 18:01:13 +00:00
|
|
|
exitJSONOutputPath string
|
2022-08-24 16:57:03 +00:00
|
|
|
walletDir string
|
|
|
|
walletPassword string
|
|
|
|
mnemonic string
|
|
|
|
numAccounts int
|
|
|
|
mnemonic25thWord string
|
2022-11-11 17:33:48 +00:00
|
|
|
beaconApiEndpoint string
|
|
|
|
beaconApiTimeout time.Duration
|
2023-10-31 16:33:54 +00:00
|
|
|
inputReader io.Reader
|
2022-04-28 14:46:46 +00:00
|
|
|
}
|
|
|
|
|
2023-10-20 16:45:33 +00:00
|
|
|
func (acm *CLIManager) prepareBeaconClients(ctx context.Context) (*iface.ValidatorClient, *iface.NodeClient, error) {
|
2022-04-28 14:46:46 +00:00
|
|
|
if acm.dialOpts == nil {
|
|
|
|
return nil, nil, errors.New("failed to construct dial options for beacon clients")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = grpcutil.AppendHeaders(ctx, acm.grpcHeaders)
|
2022-11-11 17:33:48 +00:00
|
|
|
grpcConn, err := grpc.DialContext(ctx, acm.beaconRPCProvider, acm.dialOpts...)
|
2022-04-28 14:46:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrapf(err, "could not dial endpoint %s", acm.beaconRPCProvider)
|
|
|
|
}
|
2022-11-11 17:33:48 +00:00
|
|
|
conn := validatorHelpers.NewNodeConnection(
|
|
|
|
grpcConn,
|
|
|
|
acm.beaconApiEndpoint,
|
|
|
|
acm.beaconApiTimeout,
|
|
|
|
)
|
|
|
|
|
2024-03-13 13:01:05 +00:00
|
|
|
restHandler := beaconApi.NewBeaconApiJsonRestHandler(http.Client{Timeout: acm.beaconApiTimeout}, acm.beaconApiEndpoint)
|
2024-01-11 16:03:35 +00:00
|
|
|
validatorClient := validatorClientFactory.NewValidatorClient(conn, restHandler)
|
|
|
|
nodeClient := nodeClientFactory.NewNodeClient(conn, restHandler)
|
|
|
|
|
2022-04-28 14:46:46 +00:00
|
|
|
return &validatorClient, &nodeClient, nil
|
|
|
|
}
|