mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-24 12:27:18 +00:00
14e3e80df3
* keymanager-api * import updates to standards * adding in more unit tests and code fixes to improve api experience * deleting accidently generated files * deleting accidently generated files * adding in more test coverage * fixing linter issue * removing incorrect unit tests * improving logic for accounts import * linter fix * addressing review comments * fixing based on comments * fixing nonzero root * fixing protos * regen protos * adjusting protos again * updating api specs * fixing code and unit tests after specs changed * fixing imports * adding in required changes for api middleware also adding unit tests to catch changes for protos * fixing deepsource issues * fixing linting * seeing if using pointers helps * addressing comments * updating bazel build Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
155 lines
5.2 KiB
Go
155 lines
5.2 KiB
Go
package local
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/k0kubun/go-ansi"
|
|
"github.com/pkg/errors"
|
|
"github.com/prysmaticlabs/prysm/crypto/bls"
|
|
ethpbservice "github.com/prysmaticlabs/prysm/proto/eth/service"
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager"
|
|
"github.com/schollz/progressbar/v3"
|
|
keystorev4 "github.com/wealdtech/go-eth2-wallet-encryptor-keystorev4"
|
|
)
|
|
|
|
// ImportKeystores into the local keymanager from an external source.
|
|
func (km *Keymanager) ImportKeystores(
|
|
ctx context.Context,
|
|
keystores []*keymanager.Keystore,
|
|
passwords []string,
|
|
) ([]*ethpbservice.ImportedKeystoreStatus, error) {
|
|
if len(passwords) == 0 {
|
|
return nil, ErrNoPasswords
|
|
}
|
|
if len(passwords) != len(keystores) {
|
|
return nil, ErrMismatchedNumPasswords
|
|
}
|
|
decryptor := keystorev4.New()
|
|
bar := initializeProgressBar(len(keystores), "Importing accounts...")
|
|
keys := map[string]string{}
|
|
statuses := make([]*ethpbservice.ImportedKeystoreStatus, len(keystores))
|
|
var err error
|
|
|
|
for i := 0; i < len(keystores); i++ {
|
|
var privKeyBytes []byte
|
|
var pubKeyBytes []byte
|
|
privKeyBytes, pubKeyBytes, _, err = km.attemptDecryptKeystore(decryptor, keystores[i], passwords[i])
|
|
if err != nil {
|
|
statuses[i] = ðpbservice.ImportedKeystoreStatus{
|
|
Status: ethpbservice.ImportedKeystoreStatus_ERROR,
|
|
Message: err.Error(),
|
|
}
|
|
continue
|
|
}
|
|
if err := bar.Add(1); err != nil {
|
|
log.Error(err)
|
|
}
|
|
// if key exists prior to being added then output log that duplicate key was found
|
|
if _, ok := keys[string(pubKeyBytes)]; ok {
|
|
log.Warnf("Duplicate key in import will be ignored: %#x", pubKeyBytes)
|
|
statuses[i] = ðpbservice.ImportedKeystoreStatus{
|
|
Status: ethpbservice.ImportedKeystoreStatus_DUPLICATE,
|
|
}
|
|
continue
|
|
}
|
|
keys[string(pubKeyBytes)] = string(privKeyBytes)
|
|
statuses[i] = ðpbservice.ImportedKeystoreStatus{
|
|
Status: ethpbservice.ImportedKeystoreStatus_IMPORTED,
|
|
}
|
|
}
|
|
privKeys := make([][]byte, 0)
|
|
pubKeys := make([][]byte, 0)
|
|
for pubKey, privKey := range keys {
|
|
pubKeys = append(pubKeys, []byte(pubKey))
|
|
privKeys = append(privKeys, []byte(privKey))
|
|
}
|
|
|
|
// Write the accounts to disk into a single keystore.
|
|
accountsKeystore, err := km.CreateAccountsKeystore(ctx, privKeys, pubKeys)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
encodedAccounts, err := json.MarshalIndent(accountsKeystore, "", "\t")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := km.wallet.WriteFileAtPath(ctx, AccountsPath, AccountsKeystoreFileName, encodedAccounts); err != nil {
|
|
return nil, err
|
|
}
|
|
return statuses, nil
|
|
}
|
|
|
|
// ImportKeypairs directly into the keymanager.
|
|
func (km *Keymanager) ImportKeypairs(ctx context.Context, privKeys, pubKeys [][]byte) error {
|
|
// Write the accounts to disk into a single keystore.
|
|
accountsKeystore, err := km.CreateAccountsKeystore(ctx, privKeys, pubKeys)
|
|
if err != nil {
|
|
return errors.Wrap(err, "could not import account keypairs")
|
|
}
|
|
encodedAccounts, err := json.MarshalIndent(accountsKeystore, "", "\t")
|
|
if err != nil {
|
|
return errors.Wrap(err, "could not marshal accounts keystore into JSON")
|
|
}
|
|
return km.wallet.WriteFileAtPath(ctx, AccountsPath, AccountsKeystoreFileName, encodedAccounts)
|
|
}
|
|
|
|
// Retrieves the private key and public key from an EIP-2335 keystore file
|
|
// by decrypting using a specified password. If the password fails,
|
|
// it prompts the user for the correct password until it confirms.
|
|
func (_ *Keymanager) attemptDecryptKeystore(
|
|
enc *keystorev4.Encryptor, keystore *keymanager.Keystore, password string,
|
|
) ([]byte, []byte, string, error) {
|
|
// Attempt to decrypt the keystore with the specifies password.
|
|
var privKeyBytes []byte
|
|
var err error
|
|
privKeyBytes, err = enc.Decrypt(keystore.Crypto, password)
|
|
doesNotDecrypt := err != nil && strings.Contains(err.Error(), keymanager.IncorrectPasswordErrMsg)
|
|
if doesNotDecrypt {
|
|
return nil, nil, "", fmt.Errorf(
|
|
"incorrect password for key 0x%s",
|
|
keystore.Pubkey,
|
|
)
|
|
}
|
|
if err != nil && !strings.Contains(err.Error(), keymanager.IncorrectPasswordErrMsg) {
|
|
return nil, nil, "", errors.Wrap(err, "could not decrypt keystore")
|
|
}
|
|
var pubKeyBytes []byte
|
|
// Attempt to use the pubkey present in the keystore itself as a field. If unavailable,
|
|
// then utilize the public key directly from the private key.
|
|
if keystore.Pubkey != "" {
|
|
pubKeyBytes, err = hex.DecodeString(keystore.Pubkey)
|
|
if err != nil {
|
|
return nil, nil, "", errors.Wrap(err, "could not decode pubkey from keystore")
|
|
}
|
|
} else {
|
|
privKey, err := bls.SecretKeyFromBytes(privKeyBytes)
|
|
if err != nil {
|
|
return nil, nil, "", errors.Wrap(err, "could not initialize private key from bytes")
|
|
}
|
|
pubKeyBytes = privKey.PublicKey().Marshal()
|
|
}
|
|
return privKeyBytes, pubKeyBytes, password, nil
|
|
}
|
|
|
|
func initializeProgressBar(numItems int, msg string) *progressbar.ProgressBar {
|
|
return progressbar.NewOptions(
|
|
numItems,
|
|
progressbar.OptionFullWidth(),
|
|
progressbar.OptionSetWriter(ansi.NewAnsiStdout()),
|
|
progressbar.OptionEnableColorCodes(true),
|
|
progressbar.OptionSetTheme(progressbar.Theme{
|
|
Saucer: "[green]=[reset]",
|
|
SaucerHead: "[green]>[reset]",
|
|
SaucerPadding: " ",
|
|
BarStart: "[",
|
|
BarEnd: "]",
|
|
}),
|
|
progressbar.OptionOnCompletion(func() { fmt.Println() }),
|
|
progressbar.OptionSetDescription(msg),
|
|
)
|
|
}
|