2020-07-13 21:37:18 +00:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
2020-07-23 02:12:51 +00:00
|
|
|
"context"
|
2020-07-13 21:37:18 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/logrusorgru/aurora"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/prysmaticlabs/prysm/validator/flags"
|
2020-07-23 02:12:51 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager/v2/direct"
|
2020-07-13 21:37:18 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ImportAccount uses the archived account made from ExportAccount to import an account and
|
|
|
|
// asks the users for account passwords.
|
|
|
|
func ImportAccount(cliCtx *cli.Context) error {
|
2020-07-22 04:49:04 +00:00
|
|
|
wallet, err := OpenWallet(cliCtx)
|
2020-07-20 21:12:46 +00:00
|
|
|
if err != nil {
|
2020-07-22 04:49:04 +00:00
|
|
|
return errors.Wrap(err, "could not open wallet")
|
2020-07-13 21:37:18 +00:00
|
|
|
}
|
2020-07-23 02:12:51 +00:00
|
|
|
keymanager, err := wallet.InitializeKeymanager(context.Background(), true /* skip mnemonic confirm */)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not initialize keymanager")
|
|
|
|
}
|
|
|
|
km, ok := keymanager.(*direct.Keymanager)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("can only export accounts for a non-HD wallet")
|
|
|
|
}
|
2020-07-13 21:37:18 +00:00
|
|
|
|
2020-07-23 03:10:23 +00:00
|
|
|
backupDir, err := inputDirectory(cliCtx, importDirPromptText, flags.BackupDirFlag)
|
2020-07-13 21:37:18 +00:00
|
|
|
if err != nil {
|
2020-07-22 02:04:08 +00:00
|
|
|
return errors.Wrap(err, "could not parse output directory")
|
2020-07-13 21:37:18 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 04:49:04 +00:00
|
|
|
accountsImported, err := unzipArchiveToTarget(backupDir, wallet.AccountsDir())
|
2020-07-13 21:37:18 +00:00
|
|
|
if err != nil {
|
2020-07-22 02:04:08 +00:00
|
|
|
return errors.Wrap(err, "could not unzip archive")
|
2020-07-13 21:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
au := aurora.NewAurora(true)
|
|
|
|
var loggedAccounts []string
|
|
|
|
for _, accountName := range accountsImported {
|
|
|
|
loggedAccounts = append(loggedAccounts, fmt.Sprintf("%s", au.BrightGreen(accountName).Bold()))
|
|
|
|
}
|
|
|
|
fmt.Printf("Importing accounts: %s\n", strings.Join(loggedAccounts, ", "))
|
|
|
|
|
|
|
|
for _, accountName := range accountsImported {
|
2020-07-23 02:12:51 +00:00
|
|
|
if err := km.EnterPasswordForAccount(cliCtx, accountName); err != nil {
|
2020-07-22 02:04:08 +00:00
|
|
|
return errors.Wrap(err, "could not set account password")
|
2020-07-13 21:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-23 02:12:51 +00:00
|
|
|
if err := logAccountsImported(wallet, km, accountsImported); err != nil {
|
2020-07-22 02:04:08 +00:00
|
|
|
return errors.Wrap(err, "could not log accounts imported")
|
2020-07-13 21:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func unzipArchiveToTarget(archiveDir string, target string) ([]string, error) {
|
|
|
|
archiveFile := filepath.Join(archiveDir, archiveFilename)
|
|
|
|
reader, err := zip.OpenReader(archiveFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not open reader for archive")
|
|
|
|
}
|
|
|
|
|
|
|
|
perms := os.FileMode(0700)
|
|
|
|
if err := os.MkdirAll(target, perms); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not parent path for folder")
|
|
|
|
}
|
|
|
|
|
|
|
|
var accounts []string
|
|
|
|
for _, file := range reader.File {
|
|
|
|
path := filepath.Join(target, file.Name)
|
|
|
|
parentFolder := filepath.Dir(path)
|
|
|
|
if file.FileInfo().IsDir() {
|
|
|
|
accounts = append(accounts, file.FileInfo().Name())
|
|
|
|
if err := os.MkdirAll(path, perms); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not make path for file")
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
if err := os.MkdirAll(parentFolder, perms); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not make path for file")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := copyFileFromZipToPath(file, path); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return accounts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyFileFromZipToPath(file *zip.File, path string) error {
|
|
|
|
fileReader, err := file.Open()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := fileReader.Close(); err != nil {
|
|
|
|
log.WithError(err).Error("Could not close file")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
targetFile, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not open file")
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := targetFile.Close(); err != nil {
|
|
|
|
log.WithError(err).Error("Could not close target")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if _, err := io.Copy(targetFile, fileReader); err != nil {
|
|
|
|
return errors.Wrap(err, "could not copy file")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-23 02:12:51 +00:00
|
|
|
func logAccountsImported(wallet *Wallet, keymanager *direct.Keymanager, accountNames []string) error {
|
2020-07-13 21:37:18 +00:00
|
|
|
au := aurora.NewAurora(true)
|
|
|
|
|
|
|
|
numAccounts := au.BrightYellow(len(accountNames))
|
|
|
|
fmt.Println("")
|
|
|
|
if len(accountNames) == 1 {
|
|
|
|
fmt.Printf("Imported %d validator account\n", numAccounts)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Imported %d validator accounts\n", numAccounts)
|
|
|
|
}
|
|
|
|
for _, accountName := range accountNames {
|
|
|
|
fmt.Println("")
|
|
|
|
fmt.Printf("%s\n", au.BrightGreen(accountName).Bold())
|
|
|
|
|
2020-07-23 02:12:51 +00:00
|
|
|
publicKey, err := keymanager.PublicKeyForAccount(accountName)
|
2020-07-13 21:37:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not get public key")
|
|
|
|
}
|
|
|
|
fmt.Printf("%s %#x\n", au.BrightMagenta("[public key]").Bold(), publicKey)
|
|
|
|
|
|
|
|
dirPath := au.BrightCyan("(wallet dir)")
|
|
|
|
fmt.Printf("%s %s\n", dirPath, filepath.Join(wallet.AccountsDir(), accountName))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|