2020-08-02 20:02:04 +00:00
|
|
|
package direct
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/k0kubun/go-ansi"
|
|
|
|
"github.com/pkg/errors"
|
2020-08-06 21:56:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bls"
|
2020-08-02 20:02:04 +00:00
|
|
|
v2keymanager "github.com/prysmaticlabs/prysm/validator/keymanager/v2"
|
|
|
|
"github.com/schollz/progressbar/v3"
|
|
|
|
keystorev4 "github.com/wealdtech/go-eth2-wallet-encryptor-keystorev4"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ImportKeystores into the direct keymanager from an external source.
|
2020-08-11 16:32:05 +00:00
|
|
|
func (dr *Keymanager) ImportKeystores(
|
2020-08-31 19:46:45 +00:00
|
|
|
ctx context.Context,
|
2020-08-11 16:32:05 +00:00
|
|
|
keystores []*v2keymanager.Keystore,
|
2020-08-31 19:46:45 +00:00
|
|
|
importsPassword string,
|
2020-08-11 16:32:05 +00:00
|
|
|
) error {
|
2020-08-02 20:02:04 +00:00
|
|
|
decryptor := keystorev4.New()
|
|
|
|
privKeys := make([][]byte, len(keystores))
|
|
|
|
pubKeys := make([][]byte, len(keystores))
|
|
|
|
bar := initializeProgressBar(len(keystores), "Importing accounts...")
|
|
|
|
var err error
|
2020-08-03 17:05:30 +00:00
|
|
|
var privKeyBytes []byte
|
|
|
|
var pubKeyBytes []byte
|
2020-08-02 20:02:04 +00:00
|
|
|
for i := 0; i < len(keystores); i++ {
|
2020-08-31 19:46:45 +00:00
|
|
|
privKeyBytes, pubKeyBytes, importsPassword, err = dr.attemptDecryptKeystore(decryptor, keystores[i], importsPassword)
|
2020-08-02 20:02:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
privKeys[i] = privKeyBytes
|
|
|
|
pubKeys[i] = pubKeyBytes
|
|
|
|
if err := bar.Add(1); err != nil {
|
|
|
|
return errors.Wrap(err, "could not add to progress bar")
|
|
|
|
}
|
|
|
|
}
|
2020-10-10 01:45:26 +00:00
|
|
|
foundKey := map[string]bool{}
|
|
|
|
for i := range pubKeys {
|
|
|
|
strKey := string(pubKeys[i])
|
|
|
|
if foundKey[strKey] {
|
|
|
|
return fmt.Errorf("duplicated key found: %#x", pubKeys[i])
|
|
|
|
}
|
|
|
|
foundKey[strKey] = true
|
|
|
|
}
|
2020-08-02 20:02:04 +00:00
|
|
|
// Write the accounts to disk into a single keystore.
|
|
|
|
accountsKeystore, err := dr.createAccountsKeystore(ctx, privKeys, pubKeys)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
encodedAccounts, err := json.MarshalIndent(accountsKeystore, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-03 18:58:04 +00:00
|
|
|
return dr.wallet.WriteFileAtPath(ctx, AccountsPath, accountsKeystoreFileName, encodedAccounts)
|
2020-08-02 20:02:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 (dr *Keymanager) attemptDecryptKeystore(
|
|
|
|
enc *keystorev4.Encryptor, keystore *v2keymanager.Keystore, password string,
|
2020-08-03 17:05:30 +00:00
|
|
|
) ([]byte, []byte, string, error) {
|
2020-08-02 20:02:04 +00:00
|
|
|
// Attempt to decrypt the keystore with the specifies password.
|
|
|
|
var privKeyBytes []byte
|
|
|
|
var err error
|
|
|
|
privKeyBytes, err = enc.Decrypt(keystore.Crypto, password)
|
|
|
|
if err != nil && strings.Contains(err.Error(), "invalid checksum") {
|
2020-09-03 15:11:17 +00:00
|
|
|
return nil, nil, "", fmt.Errorf("wrong password for keystore with pubkey %s", keystore.Pubkey)
|
2020-08-02 20:02:04 +00:00
|
|
|
} else if err != nil {
|
2020-08-03 17:05:30 +00:00
|
|
|
return nil, nil, "", errors.Wrap(err, "could not decrypt keystore")
|
2020-08-02 20:02:04 +00:00
|
|
|
}
|
2020-08-06 21:56:25 +00:00
|
|
|
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()
|
2020-08-02 20:02:04 +00:00
|
|
|
}
|
2020-08-03 17:05:30 +00:00
|
|
|
return privKeyBytes, pubKeyBytes, password, nil
|
2020-08-02 20:02:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
)
|
|
|
|
}
|