2020-08-13 20:27:42 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-03 15:11:17 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-08-13 20:27:42 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/validator/accounts/v2"
|
2020-09-03 15:11:17 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/event"
|
2020-09-22 14:49:07 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/fileutil"
|
2020-09-03 15:11:17 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2020-08-13 20:27:42 +00:00
|
|
|
"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"
|
|
|
|
"github.com/prysmaticlabs/prysm/validator/accounts/wallet"
|
2020-08-13 20:27:42 +00:00
|
|
|
dbtest "github.com/prysmaticlabs/prysm/validator/db/testing"
|
2020-10-15 22:31:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager"
|
2020-08-13 20:27:42 +00:00
|
|
|
)
|
|
|
|
|
2020-09-03 15:11:17 +00:00
|
|
|
func setupWalletDir(t testing.TB) string {
|
|
|
|
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
|
|
|
|
require.NoError(t, err, "Could not generate random file path")
|
|
|
|
walletDir := filepath.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath), "wallet")
|
2020-09-22 14:49:07 +00:00
|
|
|
require.NoError(t, os.MkdirAll(walletDir, os.ModePerm))
|
2020-09-03 15:11:17 +00:00
|
|
|
t.Cleanup(func() {
|
|
|
|
require.NoError(t, os.RemoveAll(walletDir), "Failed to remove directory")
|
|
|
|
})
|
|
|
|
return walletDir
|
|
|
|
}
|
|
|
|
|
2020-08-13 20:27:42 +00:00
|
|
|
func TestServer_SignupAndLogin_RoundTrip(t *testing.T) {
|
|
|
|
valDB := dbtest.SetupDB(t, [][48]byte{})
|
|
|
|
ctx := context.Background()
|
2020-09-03 15:11:17 +00:00
|
|
|
|
|
|
|
localWalletDir := setupWalletDir(t)
|
|
|
|
defaultWalletPath = localWalletDir
|
|
|
|
strongPass := "29384283xasjasd32%%&*@*#*"
|
|
|
|
|
2020-08-13 20:27:42 +00:00
|
|
|
ss := &Server{
|
2020-09-03 15:11:17 +00:00
|
|
|
valDB: valDB,
|
|
|
|
walletInitializedFeed: new(event.Feed),
|
2020-10-10 02:07:28 +00:00
|
|
|
walletDir: defaultWalletPath,
|
2020-08-13 20:27:42 +00:00
|
|
|
}
|
|
|
|
weakPass := "password"
|
2020-09-22 14:49:07 +00:00
|
|
|
_, err := ss.Signup(ctx, &pb.AuthRequest{
|
2020-08-13 20:27:42 +00:00
|
|
|
Password: weakPass,
|
|
|
|
})
|
2020-10-10 02:07:28 +00:00
|
|
|
require.ErrorContains(t, "Could not validate wallet password input", err)
|
2020-08-13 20:27:42 +00:00
|
|
|
|
|
|
|
// We assert we are able to signup with a strong password.
|
|
|
|
_, err = ss.Signup(ctx, &pb.AuthRequest{
|
|
|
|
Password: strongPass,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Assert we stored the hashed password.
|
2020-09-22 14:49:07 +00:00
|
|
|
passwordHashExists := fileutil.FileExists(filepath.Join(defaultWalletPath, wallet.HashedPasswordFileName))
|
|
|
|
assert.Equal(t, true, passwordHashExists)
|
|
|
|
|
|
|
|
// We attempt to create the wallet.
|
2020-10-15 22:31:52 +00:00
|
|
|
_, err = accounts.CreateWalletWithKeymanager(ctx, &accounts.CreateWalletConfig{
|
2020-09-22 14:49:07 +00:00
|
|
|
WalletCfg: &wallet.Config{
|
|
|
|
WalletDir: defaultWalletPath,
|
2020-10-15 22:31:52 +00:00
|
|
|
KeymanagerKind: keymanager.Derived,
|
2020-09-22 14:49:07 +00:00
|
|
|
WalletPassword: strongPass,
|
|
|
|
},
|
|
|
|
SkipMnemonicConfirm: true,
|
|
|
|
})
|
2020-08-13 20:27:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// We assert we are able to login.
|
|
|
|
_, err = ss.Login(ctx, &pb.AuthRequest{
|
|
|
|
Password: strongPass,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|