2020-10-15 22:31:52 +00:00
|
|
|
package accounts
|
2020-08-13 15:23:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-10-15 22:31:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/accounts/wallet"
|
2020-10-16 18:45:14 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager"
|
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager/imported"
|
2020-08-13 15:23:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDeleteAccounts_Noninteractive(t *testing.T) {
|
|
|
|
walletDir, _, passwordFilePath := setupWalletAndPasswordsDir(t)
|
|
|
|
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
|
|
|
|
require.NoError(t, err, "Could not generate random file path")
|
|
|
|
// Write a directory where we will import keys from.
|
|
|
|
keysDir := filepath.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath), "keysDir")
|
|
|
|
require.NoError(t, os.MkdirAll(keysDir, os.ModePerm))
|
|
|
|
|
2020-08-20 19:14:03 +00:00
|
|
|
// Create 3 keystore files in the keys directory we can then
|
2020-08-13 15:23:08 +00:00
|
|
|
// import from in our wallet.
|
|
|
|
k1, _ := createKeystore(t, keysDir)
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
k2, _ := createKeystore(t, keysDir)
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
k3, _ := createKeystore(t, keysDir)
|
|
|
|
generatedPubKeys := []string{k1.Pubkey, k2.Pubkey, k3.Pubkey}
|
|
|
|
// Only delete keys 0 and 1.
|
|
|
|
deletePublicKeys := strings.Join(generatedPubKeys[0:2], ",")
|
|
|
|
|
2020-10-16 18:45:14 +00:00
|
|
|
// We initialize a wallet with a imported keymanager.
|
2020-08-13 15:23:08 +00:00
|
|
|
cliCtx := setupWalletCtx(t, &testWalletConfig{
|
|
|
|
// Wallet configuration flags.
|
|
|
|
walletDir: walletDir,
|
2020-10-16 18:45:14 +00:00
|
|
|
keymanagerKind: keymanager.Imported,
|
2020-08-13 15:23:08 +00:00
|
|
|
walletPasswordFile: passwordFilePath,
|
|
|
|
accountPasswordFile: passwordFilePath,
|
|
|
|
// Flags required for ImportAccounts to work.
|
|
|
|
keysDir: keysDir,
|
|
|
|
// Flags required for DeleteAccounts to work.
|
|
|
|
deletePublicKeys: deletePublicKeys,
|
|
|
|
})
|
2020-09-17 01:34:42 +00:00
|
|
|
w, err := CreateWalletWithKeymanager(cliCtx.Context, &CreateWalletConfig{
|
|
|
|
WalletCfg: &wallet.Config{
|
2020-08-31 19:46:45 +00:00
|
|
|
WalletDir: walletDir,
|
2020-10-16 18:45:14 +00:00
|
|
|
KeymanagerKind: keymanager.Imported,
|
2020-08-31 19:46:45 +00:00
|
|
|
WalletPassword: password,
|
|
|
|
},
|
|
|
|
})
|
2020-08-13 15:23:08 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-10 02:07:28 +00:00
|
|
|
require.NoError(t, w.SaveHashedPassword(cliCtx.Context))
|
2020-08-13 15:23:08 +00:00
|
|
|
|
|
|
|
// We attempt to import accounts.
|
2020-08-31 19:46:45 +00:00
|
|
|
require.NoError(t, ImportAccountsCli(cliCtx))
|
2020-08-13 15:23:08 +00:00
|
|
|
|
|
|
|
// We attempt to delete the accounts specified.
|
2020-08-31 19:46:45 +00:00
|
|
|
require.NoError(t, DeleteAccountCli(cliCtx))
|
2020-08-13 15:23:08 +00:00
|
|
|
|
2020-10-16 18:45:14 +00:00
|
|
|
keymanager, err := imported.NewKeymanager(
|
2020-08-31 19:46:45 +00:00
|
|
|
cliCtx.Context,
|
2020-10-16 18:45:14 +00:00
|
|
|
&imported.SetupConfig{
|
2020-09-17 01:34:42 +00:00
|
|
|
Wallet: w,
|
2020-10-16 18:45:14 +00:00
|
|
|
Opts: imported.DefaultKeymanagerOpts(),
|
2020-08-31 19:46:45 +00:00
|
|
|
},
|
2020-08-13 15:23:08 +00:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
2020-08-31 19:46:45 +00:00
|
|
|
remainingAccounts, err := keymanager.FetchValidatingPublicKeys(cliCtx.Context)
|
2020-08-13 15:23:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(remainingAccounts), 1)
|
|
|
|
remainingPublicKey, err := hex.DecodeString(k3.Pubkey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.DeepEqual(t, remainingAccounts[0], bytesutil.ToBytes48(remainingPublicKey))
|
|
|
|
}
|