2023-05-16 10:50:04 +00:00
|
|
|
package accounts
|
2023-02-20 09:52:14 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
)
|
|
|
|
|
2023-05-09 18:48:56 +00:00
|
|
|
func TestKeycards(t *testing.T) {
|
2023-02-20 09:52:14 +00:00
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
|
|
|
|
2023-05-24 14:40:40 +00:00
|
|
|
kp1 := GetProfileKeypairForTest(true, true, true)
|
2023-05-16 10:50:04 +00:00
|
|
|
keycard1 := GetProfileKeycardForTest()
|
|
|
|
|
|
|
|
kp2 := GetSeedImportedKeypair1ForTest()
|
|
|
|
keycard2 := GetKeycardForSeedImportedKeypair1ForTest()
|
|
|
|
|
|
|
|
keycard2Copy := GetKeycardForSeedImportedKeypair1ForTest()
|
|
|
|
keycard2Copy.KeycardUID = keycard2Copy.KeycardUID + "C"
|
2024-01-18 18:54:54 +00:00
|
|
|
keycard2Copy.KeycardName = keycard2Copy.KeycardName + "Copy"
|
2023-07-05 12:41:58 +00:00
|
|
|
keycard2Copy.Position = keycard2Copy.Position + 1
|
2023-05-16 10:50:04 +00:00
|
|
|
|
|
|
|
// Pre-condition
|
|
|
|
err := db.SaveOrUpdateKeypair(kp1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = db.SaveOrUpdateKeypair(kp2)
|
|
|
|
require.NoError(t, err)
|
2023-07-25 15:17:17 +00:00
|
|
|
dbKeypairs, err := db.GetActiveKeypairs()
|
2023-05-16 10:50:04 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-05 12:41:58 +00:00
|
|
|
require.Equal(t, 2, len(dbKeypairs))
|
2023-02-20 09:52:14 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
// Test adding/reading keycards
|
|
|
|
err = db.SaveOrUpdateKeycard(*keycard1, 0, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
dbKeycard1, err := db.GetKeycardByKeycardUID(keycard1.KeycardUID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, SameKeycards(keycard1, dbKeycard1))
|
|
|
|
|
|
|
|
err = db.SaveOrUpdateKeycard(*keycard2, 0, false)
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-05 12:41:58 +00:00
|
|
|
dbKeycard2, err := db.GetKeycardByKeycardUID(keycard2.KeycardUID)
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-05 12:41:58 +00:00
|
|
|
require.True(t, SameKeycards(keycard2, dbKeycard2))
|
2023-02-20 09:52:14 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
err = db.SaveOrUpdateKeycard(*keycard2Copy, 0, false)
|
2023-02-21 09:50:48 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-05 12:41:58 +00:00
|
|
|
dbKeycard2Copy, err := db.GetKeycardByKeycardUID(keycard2Copy.KeycardUID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, SameKeycards(keycard2Copy, dbKeycard2Copy))
|
|
|
|
|
|
|
|
dbKeycards, err := db.GetKeycardsWithSameKeyUID(keycard2.KeyUID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, len(dbKeycards))
|
|
|
|
require.True(t, Contains(dbKeycards, keycard2, SameKeycards))
|
|
|
|
require.True(t, Contains(dbKeycards, keycard2Copy, SameKeycards))
|
|
|
|
|
|
|
|
dbKeycards, err = db.GetAllKnownKeycards()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 3, len(dbKeycards))
|
|
|
|
require.True(t, Contains(dbKeycards, keycard1, SameKeycards))
|
|
|
|
require.True(t, Contains(dbKeycards, keycard2, SameKeycards))
|
|
|
|
require.True(t, Contains(dbKeycards, keycard2Copy, SameKeycards))
|
|
|
|
|
|
|
|
nextPosition, err := db.GetPositionForNextNewKeycard()
|
2023-02-21 09:50:48 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-05 12:41:58 +00:00
|
|
|
require.Equal(t, uint64(len(dbKeycards)), nextPosition)
|
|
|
|
|
|
|
|
// test adding additional accounts to keycard
|
|
|
|
keycard1.AccountsAddresses = append(keycard1.AccountsAddresses, types.Address{0x05}, types.Address{0x06})
|
|
|
|
err = db.SaveOrUpdateKeycard(*keycard1, 0, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
dbKeycard1, err = db.GetKeycardByKeycardUID(keycard1.KeycardUID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(keycard1.AccountsAddresses), len(dbKeycard1.AccountsAddresses))
|
|
|
|
require.True(t, SameKeycards(keycard1, dbKeycard1))
|
|
|
|
|
|
|
|
// Test seting a new keycard name
|
|
|
|
keycard1.KeycardName = "Card101"
|
|
|
|
err = db.SetKeycardName(keycard1.KeycardUID, keycard1.KeycardName, 1000)
|
|
|
|
require.NoError(t, err)
|
|
|
|
dbKeycard1, err = db.GetKeycardByKeycardUID(keycard1.KeycardUID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, SameKeycards(keycard1, dbKeycard1))
|
2023-02-21 09:50:48 +00:00
|
|
|
|
2023-02-20 09:52:14 +00:00
|
|
|
// Test locking a keycard
|
2023-07-05 12:41:58 +00:00
|
|
|
keycard1.KeycardLocked = true
|
2023-05-09 18:48:56 +00:00
|
|
|
err = db.KeycardLocked(keycard1.KeycardUID, 1001)
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-05 12:41:58 +00:00
|
|
|
dbKeycard1, err = db.GetKeycardByKeycardUID(keycard1.KeycardUID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, SameKeycards(keycard1, dbKeycard1))
|
|
|
|
|
|
|
|
// Test unlocking a keycard
|
|
|
|
keycard1.KeycardLocked = false
|
|
|
|
err = db.KeycardUnlocked(keycard1.KeycardUID, 1002)
|
|
|
|
require.NoError(t, err)
|
|
|
|
dbKeycard1, err = db.GetKeycardByKeycardUID(keycard1.KeycardUID)
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-05 12:41:58 +00:00
|
|
|
require.True(t, SameKeycards(keycard1, dbKeycard1))
|
|
|
|
|
|
|
|
// Test update keycard uid
|
|
|
|
oldKeycardUID := keycard1.KeycardUID
|
|
|
|
keycard1.KeycardUID = "00000000000000000000000000000000"
|
|
|
|
err = db.UpdateKeycardUID(oldKeycardUID, keycard1.KeycardUID, 1003)
|
|
|
|
require.NoError(t, err)
|
|
|
|
dbKeycard1, err = db.GetKeycardByKeycardUID(keycard1.KeycardUID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, SameKeycards(keycard1, dbKeycard1))
|
2023-02-20 09:52:14 +00:00
|
|
|
|
|
|
|
// Test detleting accounts (addresses) for a certain keycard
|
|
|
|
const numOfAccountsToRemove = 2
|
2023-05-09 18:48:56 +00:00
|
|
|
require.Greater(t, len(keycard1.AccountsAddresses), numOfAccountsToRemove)
|
|
|
|
accountsToRemove := keycard1.AccountsAddresses[:numOfAccountsToRemove]
|
2023-07-05 12:41:58 +00:00
|
|
|
keycard1.AccountsAddresses = keycard1.AccountsAddresses[numOfAccountsToRemove:]
|
|
|
|
err = db.DeleteKeycardAccounts(keycard1.KeycardUID, accountsToRemove, 1004)
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-05 12:41:58 +00:00
|
|
|
dbKeycard1, err = db.GetKeycardByKeycardUID(keycard1.KeycardUID)
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-05 12:41:58 +00:00
|
|
|
require.True(t, SameKeycards(keycard1, dbKeycard1))
|
2023-02-20 09:52:14 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
// Test detleting a keycard
|
|
|
|
err = db.DeleteKeycard(keycard1.KeycardUID, 1006)
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-05 12:41:58 +00:00
|
|
|
dbKeycards, err = db.GetAllKnownKeycards()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, len(dbKeycards))
|
|
|
|
dbKeycards, err = db.GetKeycardsWithSameKeyUID(keycard1.KeyUID)
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-05 12:41:58 +00:00
|
|
|
require.Equal(t, 0, len(dbKeycards))
|
|
|
|
dbKeycard, err := db.GetKeycardByKeycardUID(keycard1.KeycardUID)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, err == ErrNoKeycardForPassedKeycardUID)
|
|
|
|
require.Nil(t, dbKeycard)
|
2023-02-20 09:52:14 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
// Test detleting all keycards for KeyUID
|
|
|
|
dbKeycards, err = db.GetKeycardsWithSameKeyUID(keycard2.KeyUID)
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-05 12:41:58 +00:00
|
|
|
require.Equal(t, 2, len(dbKeycards))
|
|
|
|
err = db.DeleteAllKeycardsWithKeyUID(keycard2.KeyUID, 1007)
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-05 12:41:58 +00:00
|
|
|
dbKeycards, err = db.GetAllKnownKeycards()
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-05 12:41:58 +00:00
|
|
|
require.Equal(t, 0, len(dbKeycards))
|
|
|
|
dbKeycards, err = db.GetKeycardsWithSameKeyUID(keycard2.KeyUID)
|
2023-02-20 09:52:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-05 12:41:58 +00:00
|
|
|
require.Equal(t, 0, len(dbKeycards))
|
|
|
|
dbKeycard, err = db.GetKeycardByKeycardUID(keycard2.KeycardUID)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, err == ErrNoKeycardForPassedKeycardUID)
|
|
|
|
require.Nil(t, dbKeycard)
|
2023-02-20 09:52:14 +00:00
|
|
|
}
|
2023-05-16 10:50:04 +00:00
|
|
|
|
|
|
|
func TestKeycardsRemovalWhenDeletingKeypair(t *testing.T) {
|
|
|
|
db, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
kp2 := GetSeedImportedKeypair1ForTest()
|
|
|
|
keycard2 := GetKeycardForSeedImportedKeypair1ForTest()
|
|
|
|
|
|
|
|
keycard2Copy := GetKeycardForSeedImportedKeypair1ForTest()
|
|
|
|
keycard2Copy.KeycardUID = keycard2Copy.KeycardUID + "C"
|
2024-01-18 18:54:54 +00:00
|
|
|
keycard2Copy.KeycardName = keycard2Copy.KeycardName + "Copy"
|
2023-07-05 12:41:58 +00:00
|
|
|
keycard2Copy.Position = keycard2Copy.Position + 1
|
|
|
|
|
|
|
|
// Pre-condition
|
2023-05-16 10:50:04 +00:00
|
|
|
err := db.SaveOrUpdateKeypair(kp2)
|
|
|
|
require.NoError(t, err)
|
2023-07-25 15:17:17 +00:00
|
|
|
dbKeypairs, err := db.GetActiveKeypairs()
|
2023-05-16 10:50:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(dbKeypairs))
|
|
|
|
|
|
|
|
// Pre-condition - save keycards referring to previously added keypair
|
2023-07-05 12:41:58 +00:00
|
|
|
err = db.SaveOrUpdateKeycard(*keycard2, 0, false)
|
2023-05-16 10:50:04 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-05 12:41:58 +00:00
|
|
|
dbKeycard2, err := db.GetKeycardByKeycardUID(keycard2.KeycardUID)
|
2023-05-16 10:50:04 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-05 12:41:58 +00:00
|
|
|
require.True(t, SameKeycards(keycard2, dbKeycard2))
|
2023-05-16 10:50:04 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
err = db.SaveOrUpdateKeycard(*keycard2Copy, 0, false)
|
2023-05-16 10:50:04 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-05 12:41:58 +00:00
|
|
|
dbKeycard2Copy, err := db.GetKeycardByKeycardUID(keycard2Copy.KeycardUID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, SameKeycards(keycard2Copy, dbKeycard2Copy))
|
2023-05-16 10:50:04 +00:00
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
// Check db state
|
|
|
|
dbKeycards, err := db.GetKeycardsWithSameKeyUID(keycard2.KeyUID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, len(dbKeycards))
|
|
|
|
require.True(t, Contains(dbKeycards, keycard2, SameKeycards))
|
|
|
|
require.True(t, Contains(dbKeycards, keycard2Copy, SameKeycards))
|
2023-05-16 10:50:04 +00:00
|
|
|
|
|
|
|
// Remove keypair
|
2023-07-20 15:00:39 +00:00
|
|
|
err = db.RemoveKeypair(kp2.KeyUID, 0)
|
2023-05-16 10:50:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Check db state after deletion
|
2023-07-25 15:17:17 +00:00
|
|
|
dbKeypairs, err = db.GetActiveKeypairs()
|
2023-05-16 10:50:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(dbKeypairs))
|
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
dbKeycards, err = db.GetAllKnownKeycards()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(dbKeycards))
|
|
|
|
dbKeycards, err = db.GetKeycardsWithSameKeyUID(kp2.KeyUID)
|
2023-05-16 10:50:04 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-05 12:41:58 +00:00
|
|
|
require.Equal(t, 0, len(dbKeycards))
|
|
|
|
dbKeycard, err := db.GetKeycardByKeycardUID(keycard2.KeycardUID)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, err == ErrNoKeycardForPassedKeycardUID)
|
|
|
|
require.Nil(t, dbKeycard)
|
2023-05-16 10:50:04 +00:00
|
|
|
}
|