2023-05-16 10:50:04 +00:00
|
|
|
package accounts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2023-06-02 15:06:51 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/common"
|
2023-05-16 10:50:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func GetWatchOnlyAccountsForTest() []*Account {
|
|
|
|
wo1 := &Account{
|
|
|
|
Address: types.Address{0x11},
|
|
|
|
Type: AccountTypeWatch,
|
|
|
|
Name: "WatchOnlyAcc1",
|
2023-06-02 15:06:51 +00:00
|
|
|
ColorID: common.CustomizationColorPrimary,
|
2023-05-16 10:50:04 +00:00
|
|
|
Emoji: "emoji-1",
|
|
|
|
}
|
|
|
|
wo2 := &Account{
|
|
|
|
Address: types.Address{0x12},
|
|
|
|
Type: AccountTypeWatch,
|
|
|
|
Name: "WatchOnlyAcc2",
|
2023-06-02 15:06:51 +00:00
|
|
|
ColorID: common.CustomizationColorPrimary,
|
2023-05-16 10:50:04 +00:00
|
|
|
Emoji: "emoji-1",
|
|
|
|
}
|
|
|
|
wo3 := &Account{
|
|
|
|
Address: types.Address{0x13},
|
|
|
|
Type: AccountTypeWatch,
|
|
|
|
Name: "WatchOnlyAcc3",
|
2023-06-02 15:06:51 +00:00
|
|
|
ColorID: common.CustomizationColorPrimary,
|
2023-05-16 10:50:04 +00:00
|
|
|
Emoji: "emoji-1",
|
|
|
|
}
|
|
|
|
|
|
|
|
return []*Account{wo1, wo2, wo3}
|
|
|
|
}
|
|
|
|
|
2023-05-24 14:40:40 +00:00
|
|
|
func GetProfileKeypairForTest(includeChatAccount bool, includeDefaultWalletAccount bool, includeAdditionalAccounts bool) *Keypair {
|
2023-05-16 10:50:04 +00:00
|
|
|
kp := &Keypair{
|
|
|
|
KeyUID: "0000000000000000000000000000000000000000000000000000000000000001",
|
|
|
|
Name: "Profile Name",
|
|
|
|
Type: KeypairTypeProfile,
|
|
|
|
DerivedFrom: "0x0001",
|
|
|
|
}
|
|
|
|
|
2023-05-24 14:40:40 +00:00
|
|
|
if includeChatAccount {
|
|
|
|
profileAccount := &Account{
|
|
|
|
Address: types.Address{0x01},
|
|
|
|
KeyUID: kp.KeyUID,
|
|
|
|
Wallet: false,
|
|
|
|
Chat: true,
|
|
|
|
Type: AccountTypeGenerated,
|
|
|
|
Path: "m/43'/60'/1581'/0'/0",
|
|
|
|
PublicKey: types.Hex2Bytes("0x000000001"),
|
|
|
|
Name: "Profile Name",
|
|
|
|
Operable: AccountFullyOperable,
|
|
|
|
}
|
|
|
|
kp.Accounts = append(kp.Accounts, profileAccount)
|
2023-05-16 10:50:04 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 14:40:40 +00:00
|
|
|
if includeDefaultWalletAccount {
|
|
|
|
defaultWalletAccount := &Account{
|
|
|
|
Address: types.Address{0x02},
|
|
|
|
KeyUID: kp.KeyUID,
|
|
|
|
Wallet: true,
|
|
|
|
Chat: false,
|
|
|
|
Type: AccountTypeGenerated,
|
|
|
|
Path: "m/44'/60'/0'/0/0",
|
|
|
|
PublicKey: types.Hex2Bytes("0x000000002"),
|
|
|
|
Name: "Generated Acc 1",
|
|
|
|
Emoji: "emoji-1",
|
2023-06-02 15:06:51 +00:00
|
|
|
ColorID: common.CustomizationColorPrimary,
|
2023-05-24 14:40:40 +00:00
|
|
|
Hidden: false,
|
|
|
|
Clock: 0,
|
|
|
|
Removed: false,
|
|
|
|
Operable: AccountFullyOperable,
|
|
|
|
}
|
|
|
|
kp.Accounts = append(kp.Accounts, defaultWalletAccount)
|
|
|
|
kp.LastUsedDerivationIndex = 0
|
2023-05-16 10:50:04 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 14:40:40 +00:00
|
|
|
if includeAdditionalAccounts {
|
2023-05-16 10:50:04 +00:00
|
|
|
generatedWalletAccount1 := &Account{
|
|
|
|
Address: types.Address{0x03},
|
|
|
|
KeyUID: kp.KeyUID,
|
|
|
|
Wallet: false,
|
|
|
|
Chat: false,
|
|
|
|
Type: AccountTypeGenerated,
|
|
|
|
Path: "m/44'/60'/0'/0/1",
|
|
|
|
PublicKey: types.Hex2Bytes("0x000000003"),
|
|
|
|
Name: "Generated Acc 2",
|
|
|
|
Emoji: "emoji-2",
|
2023-06-02 15:06:51 +00:00
|
|
|
ColorID: common.CustomizationColorPrimary,
|
2023-05-16 10:50:04 +00:00
|
|
|
Hidden: false,
|
|
|
|
Clock: 0,
|
|
|
|
Removed: false,
|
|
|
|
Operable: AccountFullyOperable,
|
|
|
|
}
|
|
|
|
kp.Accounts = append(kp.Accounts, generatedWalletAccount1)
|
|
|
|
kp.LastUsedDerivationIndex = 1
|
|
|
|
|
|
|
|
generatedWalletAccount2 := &Account{
|
|
|
|
Address: types.Address{0x04},
|
|
|
|
KeyUID: kp.KeyUID,
|
|
|
|
Wallet: false,
|
|
|
|
Chat: false,
|
|
|
|
Type: AccountTypeGenerated,
|
|
|
|
Path: "m/44'/60'/0'/0/2",
|
|
|
|
PublicKey: types.Hex2Bytes("0x000000004"),
|
|
|
|
Name: "Generated Acc 3",
|
|
|
|
Emoji: "emoji-3",
|
2023-06-02 15:06:51 +00:00
|
|
|
ColorID: common.CustomizationColorPrimary,
|
2023-05-16 10:50:04 +00:00
|
|
|
Hidden: false,
|
|
|
|
Clock: 0,
|
|
|
|
Removed: false,
|
|
|
|
Operable: AccountFullyOperable,
|
|
|
|
}
|
|
|
|
kp.Accounts = append(kp.Accounts, generatedWalletAccount2)
|
|
|
|
kp.LastUsedDerivationIndex = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
return kp
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetSeedImportedKeypair1ForTest() *Keypair {
|
|
|
|
kp := &Keypair{
|
|
|
|
KeyUID: "0000000000000000000000000000000000000000000000000000000000000002",
|
|
|
|
Name: "Seed Imported 1",
|
|
|
|
Type: KeypairTypeSeed,
|
|
|
|
DerivedFrom: "0x0002",
|
|
|
|
}
|
|
|
|
|
|
|
|
seedGeneratedWalletAccount1 := &Account{
|
|
|
|
Address: types.Address{0x21},
|
|
|
|
KeyUID: kp.KeyUID,
|
|
|
|
Wallet: false,
|
|
|
|
Chat: false,
|
|
|
|
Type: AccountTypeSeed,
|
|
|
|
Path: "m/44'/60'/0'/0/0",
|
|
|
|
PublicKey: types.Hex2Bytes("0x000000021"),
|
|
|
|
Name: "Seed Impo 1 Acc 1",
|
|
|
|
Emoji: "emoji-1",
|
2023-06-02 15:06:51 +00:00
|
|
|
ColorID: common.CustomizationColorPrimary,
|
2023-05-16 10:50:04 +00:00
|
|
|
Hidden: false,
|
|
|
|
Clock: 0,
|
|
|
|
Removed: false,
|
|
|
|
Operable: AccountFullyOperable,
|
|
|
|
}
|
|
|
|
kp.Accounts = append(kp.Accounts, seedGeneratedWalletAccount1)
|
|
|
|
kp.LastUsedDerivationIndex = 0
|
|
|
|
|
|
|
|
seedGeneratedWalletAccount2 := &Account{
|
|
|
|
Address: types.Address{0x22},
|
|
|
|
KeyUID: kp.KeyUID,
|
|
|
|
Wallet: false,
|
|
|
|
Chat: false,
|
|
|
|
Type: AccountTypeSeed,
|
|
|
|
Path: "m/44'/60'/0'/0/1",
|
|
|
|
PublicKey: types.Hex2Bytes("0x000000022"),
|
|
|
|
Name: "Seed Impo 1 Acc 2",
|
|
|
|
Emoji: "emoji-2",
|
2023-06-02 15:06:51 +00:00
|
|
|
ColorID: common.CustomizationColorPrimary,
|
2023-05-16 10:50:04 +00:00
|
|
|
Hidden: false,
|
|
|
|
Clock: 0,
|
|
|
|
Removed: false,
|
|
|
|
Operable: AccountFullyOperable,
|
|
|
|
}
|
|
|
|
kp.Accounts = append(kp.Accounts, seedGeneratedWalletAccount2)
|
|
|
|
kp.LastUsedDerivationIndex = 1
|
|
|
|
|
|
|
|
return kp
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetSeedImportedKeypair2ForTest() *Keypair {
|
|
|
|
kp := &Keypair{
|
|
|
|
KeyUID: "0000000000000000000000000000000000000000000000000000000000000003",
|
|
|
|
Name: "Seed Imported 2",
|
|
|
|
Type: KeypairTypeSeed,
|
|
|
|
DerivedFrom: "0x0003",
|
|
|
|
}
|
|
|
|
|
|
|
|
seedGeneratedWalletAccount1 := &Account{
|
|
|
|
Address: types.Address{0x31},
|
|
|
|
KeyUID: kp.KeyUID,
|
|
|
|
Wallet: false,
|
|
|
|
Chat: false,
|
|
|
|
Type: AccountTypeSeed,
|
|
|
|
Path: "m/44'/60'/0'/0/0",
|
|
|
|
PublicKey: types.Hex2Bytes("0x000000031"),
|
|
|
|
Name: "Seed Impo 2 Acc 1",
|
|
|
|
Emoji: "emoji-1",
|
2023-06-02 15:06:51 +00:00
|
|
|
ColorID: common.CustomizationColorPrimary,
|
2023-05-16 10:50:04 +00:00
|
|
|
Hidden: false,
|
|
|
|
Clock: 0,
|
|
|
|
Removed: false,
|
|
|
|
Operable: AccountFullyOperable,
|
|
|
|
}
|
|
|
|
kp.Accounts = append(kp.Accounts, seedGeneratedWalletAccount1)
|
|
|
|
kp.LastUsedDerivationIndex = 0
|
|
|
|
|
|
|
|
seedGeneratedWalletAccount2 := &Account{
|
|
|
|
Address: types.Address{0x32},
|
|
|
|
KeyUID: kp.KeyUID,
|
|
|
|
Wallet: false,
|
|
|
|
Chat: false,
|
|
|
|
Type: AccountTypeSeed,
|
|
|
|
Path: "m/44'/60'/0'/0/1",
|
|
|
|
PublicKey: types.Hex2Bytes("0x000000032"),
|
|
|
|
Name: "Seed Impo 2 Acc 2",
|
|
|
|
Emoji: "emoji-2",
|
2023-06-02 15:06:51 +00:00
|
|
|
ColorID: common.CustomizationColorPrimary,
|
2023-05-16 10:50:04 +00:00
|
|
|
Hidden: false,
|
|
|
|
Clock: 0,
|
|
|
|
Removed: false,
|
|
|
|
Operable: AccountFullyOperable,
|
|
|
|
}
|
|
|
|
kp.Accounts = append(kp.Accounts, seedGeneratedWalletAccount2)
|
|
|
|
kp.LastUsedDerivationIndex = 1
|
|
|
|
|
|
|
|
return kp
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetPrivKeyImportedKeypairForTest() *Keypair {
|
|
|
|
kp := &Keypair{
|
|
|
|
KeyUID: "0000000000000000000000000000000000000000000000000000000000000004",
|
|
|
|
Name: "Priv Key Imported",
|
|
|
|
Type: KeypairTypeKey,
|
|
|
|
DerivedFrom: "", // no derived from for private key imported kp
|
|
|
|
}
|
|
|
|
|
|
|
|
privKeyWalletAccount := &Account{
|
|
|
|
Address: types.Address{0x41},
|
|
|
|
KeyUID: kp.KeyUID,
|
|
|
|
Wallet: false,
|
|
|
|
Chat: false,
|
|
|
|
Type: AccountTypeKey,
|
|
|
|
Path: "m",
|
|
|
|
PublicKey: types.Hex2Bytes("0x000000041"),
|
|
|
|
Name: "Priv Key Impo Acc",
|
|
|
|
Emoji: "emoji-1",
|
2023-06-02 15:06:51 +00:00
|
|
|
ColorID: common.CustomizationColorPrimary,
|
2023-05-16 10:50:04 +00:00
|
|
|
Hidden: false,
|
|
|
|
Clock: 0,
|
|
|
|
Removed: false,
|
|
|
|
Operable: AccountFullyOperable,
|
|
|
|
}
|
|
|
|
kp.Accounts = append(kp.Accounts, privKeyWalletAccount)
|
|
|
|
|
|
|
|
return kp
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetProfileKeycardForTest() *Keycard {
|
2023-05-24 14:40:40 +00:00
|
|
|
profileKp := GetProfileKeypairForTest(true, true, true)
|
2023-05-16 10:50:04 +00:00
|
|
|
keycard1Addresses := []types.Address{}
|
|
|
|
for _, acc := range profileKp.Accounts {
|
|
|
|
keycard1Addresses = append(keycard1Addresses, acc.Address)
|
|
|
|
}
|
|
|
|
return &Keycard{
|
|
|
|
KeycardUID: "00000000000000000000000000000001",
|
|
|
|
KeycardName: "Card01",
|
|
|
|
KeycardLocked: false,
|
|
|
|
AccountsAddresses: keycard1Addresses,
|
|
|
|
KeyUID: profileKp.KeyUID,
|
2023-07-05 12:41:58 +00:00
|
|
|
Position: 0,
|
2023-05-16 10:50:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetKeycardForSeedImportedKeypair1ForTest() *Keycard {
|
|
|
|
seed1Kp := GetSeedImportedKeypair1ForTest()
|
|
|
|
keycard2Addresses := []types.Address{}
|
|
|
|
for _, acc := range seed1Kp.Accounts {
|
|
|
|
keycard2Addresses = append(keycard2Addresses, acc.Address)
|
|
|
|
}
|
|
|
|
return &Keycard{
|
|
|
|
KeycardUID: "00000000000000000000000000000002",
|
|
|
|
KeycardName: "Card02",
|
|
|
|
KeycardLocked: false,
|
|
|
|
AccountsAddresses: keycard2Addresses,
|
|
|
|
KeyUID: seed1Kp.KeyUID,
|
2023-07-05 12:41:58 +00:00
|
|
|
Position: 1,
|
2023-05-16 10:50:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetKeycardForSeedImportedKeypair2ForTest() *Keycard {
|
|
|
|
seed2Kp := GetSeedImportedKeypair2ForTest()
|
|
|
|
keycard4Addresses := []types.Address{}
|
|
|
|
for _, acc := range seed2Kp.Accounts {
|
|
|
|
keycard4Addresses = append(keycard4Addresses, acc.Address)
|
|
|
|
}
|
|
|
|
return &Keycard{
|
|
|
|
KeycardUID: "00000000000000000000000000000003",
|
|
|
|
KeycardName: "Card03",
|
|
|
|
KeycardLocked: false,
|
|
|
|
AccountsAddresses: keycard4Addresses,
|
|
|
|
KeyUID: seed2Kp.KeyUID,
|
2023-07-05 12:41:58 +00:00
|
|
|
Position: 2,
|
2023-05-16 10:50:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-05 12:41:58 +00:00
|
|
|
func Contains[T comparable](container []T, element T, isEqual func(T, T) bool) bool {
|
|
|
|
for _, e := range container {
|
|
|
|
if isEqual(e, element) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func HaveSameElements[T comparable](a []T, b []T, isEqual func(T, T) bool) bool {
|
|
|
|
for _, v := range a {
|
|
|
|
if !Contains(b, v, isEqual) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-05-16 10:50:04 +00:00
|
|
|
func SameAccounts(expected, real *Account) bool {
|
|
|
|
return expected.Address == real.Address &&
|
|
|
|
expected.KeyUID == real.KeyUID &&
|
|
|
|
expected.Wallet == real.Wallet &&
|
|
|
|
expected.Chat == real.Chat &&
|
|
|
|
expected.Type == real.Type &&
|
|
|
|
expected.Path == real.Path &&
|
|
|
|
string(expected.PublicKey) == string(real.PublicKey) &&
|
|
|
|
expected.Name == real.Name &&
|
|
|
|
expected.Emoji == real.Emoji &&
|
2023-06-02 15:06:51 +00:00
|
|
|
expected.ColorID == real.ColorID &&
|
2023-05-16 10:50:04 +00:00
|
|
|
expected.Hidden == real.Hidden &&
|
|
|
|
expected.Clock == real.Clock &&
|
|
|
|
expected.Removed == real.Removed
|
|
|
|
}
|
|
|
|
|
2023-07-19 11:50:16 +00:00
|
|
|
func SameAccountsIncludingPosition(expected, real *Account) bool {
|
|
|
|
return SameAccounts(expected, real) && expected.Position == real.Position
|
|
|
|
}
|
|
|
|
|
2023-05-16 10:50:04 +00:00
|
|
|
func SameAccountsWithDifferentOperable(expected, real *Account, expectedOperableValue AccountOperable) bool {
|
|
|
|
return SameAccounts(expected, real) && real.Operable == expectedOperableValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func SameKeypairs(expected, real *Keypair) bool {
|
|
|
|
same := expected.KeyUID == real.KeyUID &&
|
|
|
|
expected.Name == real.Name &&
|
|
|
|
expected.Type == real.Type &&
|
|
|
|
expected.DerivedFrom == real.DerivedFrom &&
|
|
|
|
expected.LastUsedDerivationIndex == real.LastUsedDerivationIndex &&
|
|
|
|
expected.Clock == real.Clock &&
|
|
|
|
len(expected.Accounts) == len(real.Accounts)
|
|
|
|
|
|
|
|
if same {
|
|
|
|
for i := range expected.Accounts {
|
|
|
|
found := false
|
|
|
|
for j := range real.Accounts {
|
|
|
|
if SameAccounts(expected.Accounts[i], real.Accounts[j]) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return same
|
|
|
|
}
|
|
|
|
|
|
|
|
func SameKeypairsWithDifferentSyncedFrom(expected, real *Keypair, ignoreSyncedFrom bool, expectedSyncedFromValue string,
|
|
|
|
expectedOperableValue AccountOperable) bool {
|
|
|
|
same := expected.KeyUID == real.KeyUID &&
|
|
|
|
expected.Name == real.Name &&
|
|
|
|
expected.Type == real.Type &&
|
|
|
|
expected.DerivedFrom == real.DerivedFrom &&
|
|
|
|
expected.LastUsedDerivationIndex == real.LastUsedDerivationIndex &&
|
|
|
|
expected.Clock == real.Clock &&
|
|
|
|
len(expected.Accounts) == len(real.Accounts)
|
|
|
|
|
|
|
|
if same && !ignoreSyncedFrom {
|
|
|
|
same = same && real.SyncedFrom == expectedSyncedFromValue
|
|
|
|
}
|
|
|
|
|
|
|
|
if same {
|
|
|
|
for i := range expected.Accounts {
|
|
|
|
found := false
|
|
|
|
for j := range real.Accounts {
|
|
|
|
if SameAccountsWithDifferentOperable(expected.Accounts[i], real.Accounts[j], expectedOperableValue) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return same
|
|
|
|
}
|
|
|
|
|
|
|
|
func SameKeycards(expected, real *Keycard) bool {
|
|
|
|
same := expected.KeycardUID == real.KeycardUID &&
|
|
|
|
expected.KeyUID == real.KeyUID &&
|
|
|
|
expected.KeycardName == real.KeycardName &&
|
|
|
|
expected.KeycardLocked == real.KeycardLocked &&
|
2023-07-05 12:41:58 +00:00
|
|
|
expected.Position == real.Position &&
|
2023-05-16 10:50:04 +00:00
|
|
|
len(expected.AccountsAddresses) == len(real.AccountsAddresses)
|
|
|
|
|
|
|
|
if same {
|
|
|
|
for i := range expected.AccountsAddresses {
|
|
|
|
found := false
|
|
|
|
for j := range real.AccountsAddresses {
|
|
|
|
if expected.AccountsAddresses[i] == real.AccountsAddresses[j] {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return same
|
|
|
|
}
|