2017-10-11 14:20:51 +00:00
|
|
|
package accounts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/account"
|
2018-02-08 12:52:47 +00:00
|
|
|
e2e "github.com/status-im/status-go/t/e2e"
|
|
|
|
. "github.com/status-im/status-go/t/utils"
|
2017-10-11 14:20:51 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccountsTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(AccountsTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type AccountsTestSuite struct {
|
|
|
|
e2e.BackendTestSuite
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AccountsTestSuite) TestAccountsList() {
|
2017-10-23 16:03:07 +00:00
|
|
|
s.StartTestBackend()
|
2017-10-11 14:20:51 +00:00
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
|
|
|
accounts, err := s.Backend.AccountManager().Accounts()
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// make sure that we start with empty accounts list (nobody has logged in yet)
|
|
|
|
s.Zero(len(accounts), "accounts returned, while there should be none (we haven't logged in yet)")
|
|
|
|
|
|
|
|
// create an account
|
|
|
|
address, _, _, err := s.Backend.AccountManager().CreateAccount(TestConfig.Account1.Password)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// ensure that there is still no accounts returned
|
|
|
|
accounts, err = s.Backend.AccountManager().Accounts()
|
|
|
|
s.NoError(err)
|
|
|
|
s.Zero(len(accounts), "accounts returned, while there should be none (we haven't logged in yet)")
|
|
|
|
|
|
|
|
// select account (sub-accounts will be created for this key)
|
2018-03-22 12:31:12 +00:00
|
|
|
err = s.Backend.SelectAccount(address, TestConfig.Account1.Password)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err, "account selection failed")
|
|
|
|
|
|
|
|
// at this point main account should show up
|
|
|
|
accounts, err = s.Backend.AccountManager().Accounts()
|
|
|
|
s.NoError(err)
|
|
|
|
s.Equal(1, len(accounts), "exactly single account is expected (main account)")
|
2018-06-12 16:50:25 +00:00
|
|
|
s.Equal(accounts[0].Hex(), address,
|
2017-10-11 14:20:51 +00:00
|
|
|
fmt.Sprintf("main account is not retured as the first key: got %s, expected %s", accounts[0].Hex(), "0x"+address))
|
|
|
|
|
|
|
|
// create sub-account 1
|
|
|
|
subAccount1, subPubKey1, err := s.Backend.AccountManager().CreateChildAccount("", TestConfig.Account1.Password)
|
|
|
|
s.NoError(err, "cannot create sub-account")
|
|
|
|
|
|
|
|
// now we expect to see both main account and sub-account 1
|
|
|
|
accounts, err = s.Backend.AccountManager().Accounts()
|
|
|
|
s.NoError(err)
|
|
|
|
s.Equal(2, len(accounts), "exactly 2 accounts are expected (main + sub-account 1)")
|
2018-06-12 16:50:25 +00:00
|
|
|
s.Equal(accounts[0].Hex(), address, "main account is not retured as the first key")
|
|
|
|
s.Equal(accounts[1].Hex(), subAccount1, "subAcount1 not returned")
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
// create sub-account 2, index automatically progresses
|
|
|
|
subAccount2, subPubKey2, err := s.Backend.AccountManager().CreateChildAccount("", TestConfig.Account1.Password)
|
|
|
|
s.NoError(err, "cannot create sub-account")
|
|
|
|
s.False(subAccount1 == subAccount2 || subPubKey1 == subPubKey2, "sub-account index auto-increament failed")
|
|
|
|
|
|
|
|
// finally, all 3 accounts should show up (main account, sub-accounts 1 and 2)
|
|
|
|
accounts, err = s.Backend.AccountManager().Accounts()
|
|
|
|
s.NoError(err)
|
|
|
|
s.Equal(3, len(accounts), "unexpected number of accounts")
|
2018-06-12 16:50:25 +00:00
|
|
|
s.Equal(accounts[0].Hex(), address, "main account is not retured as the first key")
|
2017-10-11 14:20:51 +00:00
|
|
|
|
2018-06-12 16:50:25 +00:00
|
|
|
subAccount1MatchesKey1 := accounts[1].Hex() != "0x"+subAccount1
|
|
|
|
subAccount1MatchesKey2 := accounts[2].Hex() != "0x"+subAccount1
|
2017-10-11 14:20:51 +00:00
|
|
|
s.False(!subAccount1MatchesKey1 && !subAccount1MatchesKey2, "subAcount1 not returned")
|
|
|
|
|
2018-06-12 16:50:25 +00:00
|
|
|
subAccount2MatchesKey1 := accounts[1].Hex() != "0x"+subAccount2
|
|
|
|
subAccount2MatchesKey2 := accounts[2].Hex() != "0x"+subAccount2
|
2017-10-11 14:20:51 +00:00
|
|
|
s.False(!subAccount2MatchesKey1 && !subAccount2MatchesKey2, "subAcount2 not returned")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AccountsTestSuite) TestCreateChildAccount() {
|
2017-10-23 16:03:07 +00:00
|
|
|
s.StartTestBackend()
|
2017-10-11 14:20:51 +00:00
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
keyStore, err := s.Backend.StatusNode().AccountKeyStore()
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
|
|
|
s.NotNil(keyStore)
|
|
|
|
|
|
|
|
// create an account
|
|
|
|
address, pubKey, mnemonic, err := s.Backend.AccountManager().CreateAccount(TestConfig.Account1.Password)
|
|
|
|
s.NoError(err)
|
|
|
|
s.T().Logf("Account created: {address: %s, key: %s, mnemonic:%s}", address, pubKey, mnemonic)
|
|
|
|
|
2018-03-22 12:31:12 +00:00
|
|
|
acct, err := account.ParseAccountString(address)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err, "can not get account from address")
|
|
|
|
|
|
|
|
// obtain decrypted key, and make sure that extended key (which will be used as root for sub-accounts) is present
|
|
|
|
_, key, err := keyStore.AccountDecryptedKey(acct, TestConfig.Account1.Password)
|
|
|
|
s.NoError(err, "can not obtain decrypted account key")
|
|
|
|
s.NotNil(key.ExtendedKey, "CKD#2 has not been generated for new account")
|
|
|
|
|
|
|
|
// try creating sub-account, w/o selecting main account i.e. w/o login to main account
|
|
|
|
_, _, err = s.Backend.AccountManager().CreateChildAccount("", TestConfig.Account1.Password)
|
|
|
|
s.EqualError(account.ErrNoAccountSelected, err.Error(), "expected error is not returned (tried to create sub-account w/o login)")
|
|
|
|
|
2018-03-22 12:31:12 +00:00
|
|
|
err = s.Backend.SelectAccount(address, TestConfig.Account1.Password)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err, "cannot select account")
|
|
|
|
|
|
|
|
// try to create sub-account with wrong password
|
|
|
|
_, _, err = s.Backend.AccountManager().CreateChildAccount("", "wrong password")
|
|
|
|
expectedErr := errors.New("cannot retrieve a valid key for a given account: could not decrypt key with given passphrase")
|
|
|
|
s.EqualError(expectedErr, err.Error(), "create sub-account with wrong password")
|
|
|
|
|
|
|
|
// create sub-account (from implicit parent)
|
|
|
|
subAccount1, subPubKey1, err := s.Backend.AccountManager().CreateChildAccount("", TestConfig.Account1.Password)
|
|
|
|
s.NoError(err, "cannot create sub-account")
|
|
|
|
|
|
|
|
// make sure that sub-account index automatically progresses
|
|
|
|
subAccount2, subPubKey2, err := s.Backend.AccountManager().CreateChildAccount("", TestConfig.Account1.Password)
|
|
|
|
s.NoError(err)
|
|
|
|
s.False(subAccount1 == subAccount2 || subPubKey1 == subPubKey2, "sub-account index auto-increament failed")
|
|
|
|
|
|
|
|
// create sub-account (from explicit parent)
|
|
|
|
subAccount3, subPubKey3, err := s.Backend.AccountManager().CreateChildAccount(subAccount2, TestConfig.Account1.Password)
|
|
|
|
s.NoError(err)
|
|
|
|
s.False(subAccount1 == subAccount3 || subPubKey1 == subPubKey3 || subAccount2 == subAccount3 || subPubKey2 == subPubKey3)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AccountsTestSuite) TestRecoverAccount() {
|
2017-10-23 16:03:07 +00:00
|
|
|
s.StartTestBackend()
|
2017-10-11 14:20:51 +00:00
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
keyStore, err := s.Backend.StatusNode().AccountKeyStore()
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
2017-10-24 16:48:31 +00:00
|
|
|
// create an acc
|
2017-10-11 14:20:51 +00:00
|
|
|
address, pubKey, mnemonic, err := s.Backend.AccountManager().CreateAccount(TestConfig.Account1.Password)
|
|
|
|
s.NoError(err)
|
|
|
|
s.T().Logf("Account created: {address: %s, key: %s, mnemonic:%s}", address, pubKey, mnemonic)
|
|
|
|
|
|
|
|
// try recovering using password + mnemonic
|
|
|
|
addressCheck, pubKeyCheck, err := s.Backend.AccountManager().RecoverAccount(TestConfig.Account1.Password, mnemonic)
|
2017-10-24 16:48:31 +00:00
|
|
|
s.NoError(err, "recover acc failed")
|
2017-10-11 14:20:51 +00:00
|
|
|
s.False(address != addressCheck || pubKey != pubKeyCheck, "incorrect accound details recovered")
|
|
|
|
|
2017-10-24 16:48:31 +00:00
|
|
|
// now test recovering, but make sure that acc/key file is removed i.e. simulate recovering on a new device
|
2018-03-22 12:31:12 +00:00
|
|
|
acc, err := account.ParseAccountString(address)
|
2017-10-24 16:48:31 +00:00
|
|
|
s.NoError(err, "can not get acc from address")
|
2017-10-11 14:20:51 +00:00
|
|
|
|
2017-10-24 16:48:31 +00:00
|
|
|
acc, key, err := keyStore.AccountDecryptedKey(acc, TestConfig.Account1.Password)
|
|
|
|
s.NoError(err, "can not obtain decrypted acc key")
|
2017-10-11 14:20:51 +00:00
|
|
|
extChild2String := key.ExtendedKey.String()
|
|
|
|
|
2017-10-24 16:48:31 +00:00
|
|
|
s.NoError(keyStore.Delete(acc, TestConfig.Account1.Password), "cannot remove acc")
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
addressCheck, pubKeyCheck, err = s.Backend.AccountManager().RecoverAccount(TestConfig.Account1.Password, mnemonic)
|
2017-10-24 16:48:31 +00:00
|
|
|
s.NoError(err, "recover acc failed (for non-cached acc)")
|
2017-10-11 14:20:51 +00:00
|
|
|
s.False(address != addressCheck || pubKey != pubKeyCheck,
|
2017-10-24 16:48:31 +00:00
|
|
|
"incorrect acc details recovered (for non-cached acc)")
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
// make sure that extended key exists and is imported ok too
|
2017-10-24 16:48:31 +00:00
|
|
|
_, key, err = keyStore.AccountDecryptedKey(acc, TestConfig.Account1.Password)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
|
|
|
s.Equal(extChild2String, key.ExtendedKey.String(), "CKD#2 key mismatch")
|
|
|
|
|
|
|
|
// make sure that calling import several times, just returns from cache (no error is expected)
|
|
|
|
addressCheck, pubKeyCheck, err = s.Backend.AccountManager().RecoverAccount(TestConfig.Account1.Password, mnemonic)
|
2017-10-24 16:48:31 +00:00
|
|
|
s.NoError(err, "recover acc failed (for non-cached acc)")
|
2017-10-11 14:20:51 +00:00
|
|
|
s.False(address != addressCheck || pubKey != pubKeyCheck,
|
2017-10-24 16:48:31 +00:00
|
|
|
"incorrect acc details recovered (for non-cached acc)")
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AccountsTestSuite) TestSelectAccount() {
|
2017-10-23 16:03:07 +00:00
|
|
|
s.StartTestBackend()
|
2017-10-11 14:20:51 +00:00
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
|
|
|
// create an account
|
|
|
|
address1, pubKey1, _, err := s.Backend.AccountManager().CreateAccount(TestConfig.Account1.Password)
|
|
|
|
s.NoError(err)
|
|
|
|
s.T().Logf("Account created: {address: %s, key: %s}", address1, pubKey1)
|
|
|
|
|
|
|
|
address2, pubKey2, _, err := s.Backend.AccountManager().CreateAccount(TestConfig.Account1.Password)
|
|
|
|
s.NoError(err)
|
|
|
|
s.T().Logf("Account created: {address: %s, key: %s}", address2, pubKey2)
|
|
|
|
|
|
|
|
// try selecting with wrong password
|
2018-03-22 12:31:12 +00:00
|
|
|
err = s.Backend.SelectAccount(address1, "wrongPassword")
|
2017-10-11 14:20:51 +00:00
|
|
|
expectedErr := errors.New("cannot retrieve a valid key for a given account: could not decrypt key with given passphrase")
|
|
|
|
s.EqualError(expectedErr, err.Error(), "select account is expected to throw error: wrong password used")
|
|
|
|
|
2018-03-22 12:31:12 +00:00
|
|
|
err = s.Backend.SelectAccount(address1, TestConfig.Account1.Password)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// select another account, make sure that previous account is wiped out from Whisper cache
|
2018-03-22 12:31:12 +00:00
|
|
|
s.NoError(s.Backend.SelectAccount(address2, TestConfig.Account1.Password))
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AccountsTestSuite) TestSelectedAccountOnRestart() {
|
2017-10-23 16:03:07 +00:00
|
|
|
s.StartTestBackend()
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
// create test accounts
|
2018-03-28 08:24:55 +00:00
|
|
|
address1, _, _, err := s.Backend.AccountManager().CreateAccount(TestConfig.Account1.Password)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
2018-03-28 08:24:55 +00:00
|
|
|
address2, _, _, err := s.Backend.AccountManager().CreateAccount(TestConfig.Account1.Password)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// make sure that no account is selected by default
|
2019-01-09 08:47:06 +00:00
|
|
|
selectedAccount, err := s.Backend.AccountManager().SelectedWalletAccount()
|
2017-10-11 14:20:51 +00:00
|
|
|
s.EqualError(account.ErrNoAccountSelected, err.Error(), "account selected, but should not be")
|
|
|
|
s.Nil(selectedAccount)
|
|
|
|
|
|
|
|
// select account
|
2018-03-22 12:31:12 +00:00
|
|
|
err = s.Backend.SelectAccount(address1, "wrongPassword")
|
2017-10-11 14:20:51 +00:00
|
|
|
expectedErr := errors.New("cannot retrieve a valid key for a given account: could not decrypt key with given passphrase")
|
|
|
|
s.EqualError(expectedErr, err.Error())
|
|
|
|
|
2018-03-22 12:31:12 +00:00
|
|
|
s.NoError(s.Backend.SelectAccount(address2, TestConfig.Account1.Password))
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
// stop node (and all of its sub-protocols)
|
2018-04-16 12:36:09 +00:00
|
|
|
nodeConfig := s.Backend.StatusNode().Config()
|
|
|
|
s.NotNil(nodeConfig)
|
2017-10-11 14:20:51 +00:00
|
|
|
preservedNodeConfig := *nodeConfig
|
2018-02-09 13:37:56 +00:00
|
|
|
s.NoError(s.Backend.StopNode())
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
// make sure that account is still selected
|
2019-01-09 08:47:06 +00:00
|
|
|
selectedAccount, err = s.Backend.AccountManager().SelectedWalletAccount()
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
|
|
|
s.NotNil(selectedAccount)
|
|
|
|
s.Equal(selectedAccount.Address.Hex(), address2, "incorrect address selected")
|
|
|
|
|
|
|
|
// resume node
|
2018-09-13 16:31:29 +00:00
|
|
|
s.Require().NoError(s.Backend.StartNode(&preservedNodeConfig))
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
// re-check selected account (account2 MUST be selected)
|
2019-01-09 08:47:06 +00:00
|
|
|
selectedAccount, err = s.Backend.AccountManager().SelectedWalletAccount()
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
|
|
|
s.NotNil(selectedAccount)
|
|
|
|
s.Equal(selectedAccount.Address.Hex(), address2, "incorrect address selected")
|
|
|
|
|
|
|
|
// now restart node using RestartNode() method, and make sure that account is still available
|
|
|
|
s.RestartTestNode()
|
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
|
|
|
// now logout, and make sure that on restart no account is selected (i.e. logout works properly)
|
2018-04-20 15:39:53 +00:00
|
|
|
s.NoError(s.Backend.Logout())
|
2017-10-11 14:20:51 +00:00
|
|
|
s.RestartTestNode()
|
|
|
|
|
2019-01-09 08:47:06 +00:00
|
|
|
selectedAccount, err = s.Backend.AccountManager().SelectedWalletAccount()
|
2017-10-11 14:20:51 +00:00
|
|
|
s.EqualError(account.ErrNoAccountSelected, err.Error())
|
|
|
|
s.Nil(selectedAccount)
|
|
|
|
}
|