Accounts V2: Allow for user to specify single file rather than directory for keystore import (#6813)

* Fix public key formatting
* Accounts V2: Allow for user to specify single file rather than directory
* Merge branch 'master' into fix-pubkey-formatting
* Merge refs/heads/master into fix-pubkey-formatting
This commit is contained in:
Preston Van Loon 2020-07-31 13:17:39 -07:00 committed by GitHub
parent 7a1e5b1dd9
commit 3f983e2a96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 10 deletions

View File

@ -55,18 +55,33 @@ func ImportAccount(cliCtx *cli.Context) error {
}
accountsImported := make([]string, 0)
pubKeysImported := make([][]byte, 0)
files, err := ioutil.ReadDir(keysDir)
isDir, err := hasDir(keysDir)
if err != nil {
return errors.Wrap(err, "could not read dir")
return errors.Wrap(err, "could not determine if path is a directory")
}
for i := 0; i < len(files); i++ {
if files[i].IsDir() {
continue
// Consider that the keysDir might be a path to a specific file and handle accordingly.
if isDir {
files, err := ioutil.ReadDir(keysDir)
if err != nil {
return errors.Wrap(err, "could not read dir")
}
if !strings.HasPrefix(files[i].Name(), "keystore") {
continue
for i := 0; i < len(files); i++ {
if files[i].IsDir() {
continue
}
if !strings.HasPrefix(files[i].Name(), "keystore") {
continue
}
accountName, pubKey, err := wallet.importKeystore(ctx, filepath.Join(keysDir, files[i].Name()))
if err != nil {
return errors.Wrap(err, "could not import keystore")
}
accountsImported = append(accountsImported, accountName)
pubKeysImported = append(pubKeysImported, pubKey)
}
accountName, pubKey, err := wallet.importKeystore(ctx, filepath.Join(keysDir, files[i].Name()))
} else {
accountName, pubKey, err := wallet.importKeystore(ctx, keysDir)
if err != nil {
return errors.Wrap(err, "could not import keystore")
}

View File

@ -79,7 +79,59 @@ func TestImport_Noninteractive(t *testing.T) {
assert.Equal(t, 2, len(keys))
}
func createKeystore(t *testing.T, path string) {
func TestImport_Noninteractive_Filepath(t *testing.T) {
walletDir, passwordsDir, passwordFilePath := setupWalletAndPasswordsDir(t)
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
require.NoError(t, err, "Could not generate random file path")
keysDir := filepath.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath), "keysDir")
require.NoError(t, os.MkdirAll(keysDir, os.ModePerm))
t.Cleanup(func() {
require.NoError(t, os.RemoveAll(keysDir), "Failed to remove directory")
})
cliCtx := setupWalletCtx(t, &testWalletConfig{
walletDir: walletDir,
passwordsDir: passwordsDir,
keysDir: createKeystore(t, keysDir), // Using direct filepath to the new keystore.
keymanagerKind: v2keymanager.Direct,
walletPasswordFile: passwordFilePath,
accountPasswordFile: passwordFilePath,
})
wallet, err := NewWallet(cliCtx, v2keymanager.Direct)
require.NoError(t, err)
require.NoError(t, wallet.SaveWallet())
ctx := context.Background()
keymanagerCfg := direct.DefaultConfig()
keymanagerCfg.AccountPasswordsDirectory = passwordsDir
encodedCfg, err := direct.MarshalConfigFile(ctx, keymanagerCfg)
require.NoError(t, err)
require.NoError(t, wallet.WriteKeymanagerConfigToDisk(ctx, encodedCfg))
keymanager, err := direct.NewKeymanager(
ctx,
wallet,
keymanagerCfg,
)
require.NoError(t, err)
// Make sure there are no accounts at the start.
accounts, err := keymanager.ValidatingAccountNames()
require.NoError(t, err)
assert.Equal(t, len(accounts), 0)
require.NoError(t, ImportAccount(cliCtx))
wallet, err = OpenWallet(cliCtx)
require.NoError(t, err)
km, err := wallet.InitializeKeymanager(ctx, true)
require.NoError(t, err)
keys, err := km.FetchValidatingPublicKeys(ctx)
require.NoError(t, err)
assert.Equal(t, 1, len(keys))
}
// Returns the fullPath to the newly created keystore file.
func createKeystore(t *testing.T, path string) string {
validatingKey := bls.RandKey()
encryptor := keystorev4.New()
cryptoFields, err := encryptor.Encrypt(validatingKey.Marshal(), password)
@ -99,4 +151,5 @@ func createKeystore(t *testing.T, path string) {
createdAt := roughtime.Now().Unix()
fullPath := filepath.Join(path, fmt.Sprintf(direct.KeystoreFileNameFormat, createdAt))
require.NoError(t, ioutil.WriteFile(fullPath, encoded, os.ModePerm))
return fullPath
}

View File

@ -19,7 +19,7 @@ import (
)
const (
importKeysDirPromptText = "Enter the directory where your keystores to import are located"
importKeysDirPromptText = "Enter the directory or filepath where your keystores to import are located"
exportDirPromptText = "Enter a file location to write the exported account(s) to"
walletDirPromptText = "Enter a wallet directory"
passwordsDirPromptText = "Directory where passwords will be stored"