github.com/status-im/status-go@v1.1.0/multiaccounts/accounts/test_helper.go (about)

     1  package accounts
     2  
     3  import (
     4  	"github.com/status-im/status-go/eth-node/types"
     5  	"github.com/status-im/status-go/multiaccounts/common"
     6  )
     7  
     8  func GetWatchOnlyAccountsForTest() []*Account {
     9  	wo1 := &Account{
    10  		Address: types.Address{0x11},
    11  		Type:    AccountTypeWatch,
    12  		Name:    "WatchOnlyAcc1",
    13  		ColorID: common.CustomizationColorPrimary,
    14  		Emoji:   "emoji-1",
    15  	}
    16  	wo2 := &Account{
    17  		Address: types.Address{0x12},
    18  		Type:    AccountTypeWatch,
    19  		Name:    "WatchOnlyAcc2",
    20  		ColorID: common.CustomizationColorPrimary,
    21  		Emoji:   "emoji-1",
    22  	}
    23  	wo3 := &Account{
    24  		Address: types.Address{0x13},
    25  		Type:    AccountTypeWatch,
    26  		Name:    "WatchOnlyAcc3",
    27  		ColorID: common.CustomizationColorPrimary,
    28  		Emoji:   "emoji-1",
    29  	}
    30  
    31  	return []*Account{wo1, wo2, wo3}
    32  }
    33  
    34  func GetProfileKeypairForTest(includeChatAccount bool, includeDefaultWalletAccount bool, includeAdditionalAccounts bool) *Keypair {
    35  	kp := &Keypair{
    36  		KeyUID:      "0000000000000000000000000000000000000000000000000000000000000001",
    37  		Name:        "Profile Name",
    38  		Type:        KeypairTypeProfile,
    39  		DerivedFrom: "0x0001",
    40  	}
    41  
    42  	if includeChatAccount {
    43  		profileAccount := &Account{
    44  			Address:               types.Address{0x01},
    45  			KeyUID:                kp.KeyUID,
    46  			Wallet:                false,
    47  			Chat:                  true,
    48  			Type:                  AccountTypeGenerated,
    49  			Path:                  "m/43'/60'/1581'/0'/0",
    50  			PublicKey:             types.Hex2Bytes("0x000000001"),
    51  			Name:                  "Profile Name",
    52  			Operable:              AccountFullyOperable,
    53  			ProdPreferredChainIDs: "1",
    54  			TestPreferredChainIDs: "5",
    55  		}
    56  		kp.Accounts = append(kp.Accounts, profileAccount)
    57  	}
    58  
    59  	if includeDefaultWalletAccount {
    60  		defaultWalletAccount := &Account{
    61  			Address:               types.Address{0x02},
    62  			KeyUID:                kp.KeyUID,
    63  			Wallet:                true,
    64  			Chat:                  false,
    65  			Type:                  AccountTypeGenerated,
    66  			Path:                  "m/44'/60'/0'/0/0",
    67  			PublicKey:             types.Hex2Bytes("0x000000002"),
    68  			Name:                  "Generated Acc 1",
    69  			Emoji:                 "emoji-1",
    70  			ColorID:               common.CustomizationColorPrimary,
    71  			Hidden:                false,
    72  			Clock:                 0,
    73  			Removed:               false,
    74  			Operable:              AccountFullyOperable,
    75  			ProdPreferredChainIDs: "1",
    76  			TestPreferredChainIDs: "5",
    77  		}
    78  		kp.Accounts = append(kp.Accounts, defaultWalletAccount)
    79  		kp.LastUsedDerivationIndex = 0
    80  	}
    81  
    82  	if includeAdditionalAccounts {
    83  		generatedWalletAccount1 := &Account{
    84  			Address:               types.Address{0x03},
    85  			KeyUID:                kp.KeyUID,
    86  			Wallet:                false,
    87  			Chat:                  false,
    88  			Type:                  AccountTypeGenerated,
    89  			Path:                  "m/44'/60'/0'/0/1",
    90  			PublicKey:             types.Hex2Bytes("0x000000003"),
    91  			Name:                  "Generated Acc 2",
    92  			Emoji:                 "emoji-2",
    93  			ColorID:               common.CustomizationColorPrimary,
    94  			Hidden:                false,
    95  			Clock:                 0,
    96  			Removed:               false,
    97  			Operable:              AccountFullyOperable,
    98  			ProdPreferredChainIDs: "1",
    99  			TestPreferredChainIDs: "5",
   100  		}
   101  		kp.Accounts = append(kp.Accounts, generatedWalletAccount1)
   102  		kp.LastUsedDerivationIndex = 1
   103  
   104  		generatedWalletAccount2 := &Account{
   105  			Address:               types.Address{0x04},
   106  			KeyUID:                kp.KeyUID,
   107  			Wallet:                false,
   108  			Chat:                  false,
   109  			Type:                  AccountTypeGenerated,
   110  			Path:                  "m/44'/60'/0'/0/2",
   111  			PublicKey:             types.Hex2Bytes("0x000000004"),
   112  			Name:                  "Generated Acc 3",
   113  			Emoji:                 "emoji-3",
   114  			ColorID:               common.CustomizationColorPrimary,
   115  			Hidden:                false,
   116  			Clock:                 0,
   117  			Removed:               false,
   118  			Operable:              AccountFullyOperable,
   119  			ProdPreferredChainIDs: "1",
   120  			TestPreferredChainIDs: "5",
   121  		}
   122  		kp.Accounts = append(kp.Accounts, generatedWalletAccount2)
   123  		kp.LastUsedDerivationIndex = 2
   124  	}
   125  
   126  	return kp
   127  }
   128  
   129  func GetSeedImportedKeypair1ForTest() *Keypair {
   130  	kp := &Keypair{
   131  		KeyUID:      "0000000000000000000000000000000000000000000000000000000000000002",
   132  		Name:        "Seed Imported 1",
   133  		Type:        KeypairTypeSeed,
   134  		DerivedFrom: "0x0002",
   135  	}
   136  
   137  	seedGeneratedWalletAccount1 := &Account{
   138  		Address:   types.Address{0x21},
   139  		KeyUID:    kp.KeyUID,
   140  		Wallet:    false,
   141  		Chat:      false,
   142  		Type:      AccountTypeSeed,
   143  		Path:      "m/44'/60'/0'/0/0",
   144  		PublicKey: types.Hex2Bytes("0x000000021"),
   145  		Name:      "Seed Impo 1 Acc 1",
   146  		Emoji:     "emoji-1",
   147  		ColorID:   common.CustomizationColorPrimary,
   148  		Hidden:    false,
   149  		Clock:     0,
   150  		Removed:   false,
   151  		Operable:  AccountFullyOperable,
   152  	}
   153  	kp.Accounts = append(kp.Accounts, seedGeneratedWalletAccount1)
   154  	kp.LastUsedDerivationIndex = 0
   155  
   156  	seedGeneratedWalletAccount2 := &Account{
   157  		Address:   types.Address{0x22},
   158  		KeyUID:    kp.KeyUID,
   159  		Wallet:    false,
   160  		Chat:      false,
   161  		Type:      AccountTypeSeed,
   162  		Path:      "m/44'/60'/0'/0/1",
   163  		PublicKey: types.Hex2Bytes("0x000000022"),
   164  		Name:      "Seed Impo 1 Acc 2",
   165  		Emoji:     "emoji-2",
   166  		ColorID:   common.CustomizationColorPrimary,
   167  		Hidden:    false,
   168  		Clock:     0,
   169  		Removed:   false,
   170  		Operable:  AccountFullyOperable,
   171  	}
   172  	kp.Accounts = append(kp.Accounts, seedGeneratedWalletAccount2)
   173  	kp.LastUsedDerivationIndex = 1
   174  
   175  	return kp
   176  }
   177  
   178  func GetSeedImportedKeypair2ForTest() *Keypair {
   179  	kp := &Keypair{
   180  		KeyUID:      "0000000000000000000000000000000000000000000000000000000000000003",
   181  		Name:        "Seed Imported 2",
   182  		Type:        KeypairTypeSeed,
   183  		DerivedFrom: "0x0003",
   184  	}
   185  
   186  	seedGeneratedWalletAccount1 := &Account{
   187  		Address:   types.Address{0x31},
   188  		KeyUID:    kp.KeyUID,
   189  		Wallet:    false,
   190  		Chat:      false,
   191  		Type:      AccountTypeSeed,
   192  		Path:      "m/44'/60'/0'/0/0",
   193  		PublicKey: types.Hex2Bytes("0x000000031"),
   194  		Name:      "Seed Impo 2 Acc 1",
   195  		Emoji:     "emoji-1",
   196  		ColorID:   common.CustomizationColorPrimary,
   197  		Hidden:    false,
   198  		Clock:     0,
   199  		Removed:   false,
   200  		Operable:  AccountFullyOperable,
   201  	}
   202  	kp.Accounts = append(kp.Accounts, seedGeneratedWalletAccount1)
   203  	kp.LastUsedDerivationIndex = 0
   204  
   205  	seedGeneratedWalletAccount2 := &Account{
   206  		Address:   types.Address{0x32},
   207  		KeyUID:    kp.KeyUID,
   208  		Wallet:    false,
   209  		Chat:      false,
   210  		Type:      AccountTypeSeed,
   211  		Path:      "m/44'/60'/0'/0/1",
   212  		PublicKey: types.Hex2Bytes("0x000000032"),
   213  		Name:      "Seed Impo 2 Acc 2",
   214  		Emoji:     "emoji-2",
   215  		ColorID:   common.CustomizationColorPrimary,
   216  		Hidden:    false,
   217  		Clock:     0,
   218  		Removed:   false,
   219  		Operable:  AccountFullyOperable,
   220  	}
   221  	kp.Accounts = append(kp.Accounts, seedGeneratedWalletAccount2)
   222  	kp.LastUsedDerivationIndex = 1
   223  
   224  	return kp
   225  }
   226  
   227  func GetPrivKeyImportedKeypairForTest() *Keypair {
   228  	kp := &Keypair{
   229  		KeyUID:      "0000000000000000000000000000000000000000000000000000000000000004",
   230  		Name:        "Priv Key Imported",
   231  		Type:        KeypairTypeKey,
   232  		DerivedFrom: "", // no derived from for private key imported kp
   233  	}
   234  
   235  	privKeyWalletAccount := &Account{
   236  		Address:   types.Address{0x41},
   237  		KeyUID:    kp.KeyUID,
   238  		Wallet:    false,
   239  		Chat:      false,
   240  		Type:      AccountTypeKey,
   241  		Path:      "m",
   242  		PublicKey: types.Hex2Bytes("0x000000041"),
   243  		Name:      "Priv Key Impo Acc",
   244  		Emoji:     "emoji-1",
   245  		ColorID:   common.CustomizationColorPrimary,
   246  		Hidden:    false,
   247  		Clock:     0,
   248  		Removed:   false,
   249  		Operable:  AccountFullyOperable,
   250  	}
   251  	kp.Accounts = append(kp.Accounts, privKeyWalletAccount)
   252  
   253  	return kp
   254  }
   255  
   256  func GetProfileKeycardForTest() *Keycard {
   257  	profileKp := GetProfileKeypairForTest(true, true, true)
   258  	keycard1Addresses := []types.Address{}
   259  	for _, acc := range profileKp.Accounts {
   260  		keycard1Addresses = append(keycard1Addresses, acc.Address)
   261  	}
   262  	return &Keycard{
   263  		KeycardUID:        "00000000000000000000000000000001",
   264  		KeycardName:       "Card01",
   265  		KeycardLocked:     false,
   266  		AccountsAddresses: keycard1Addresses,
   267  		KeyUID:            profileKp.KeyUID,
   268  		Position:          0,
   269  	}
   270  }
   271  
   272  func GetKeycardForSeedImportedKeypair1ForTest() *Keycard {
   273  	seed1Kp := GetSeedImportedKeypair1ForTest()
   274  	keycard2Addresses := []types.Address{}
   275  	for _, acc := range seed1Kp.Accounts {
   276  		keycard2Addresses = append(keycard2Addresses, acc.Address)
   277  	}
   278  	return &Keycard{
   279  		KeycardUID:        "00000000000000000000000000000002",
   280  		KeycardName:       "Card02",
   281  		KeycardLocked:     false,
   282  		AccountsAddresses: keycard2Addresses,
   283  		KeyUID:            seed1Kp.KeyUID,
   284  		Position:          1,
   285  	}
   286  }
   287  
   288  func GetKeycardForSeedImportedKeypair2ForTest() *Keycard {
   289  	seed2Kp := GetSeedImportedKeypair2ForTest()
   290  	keycard4Addresses := []types.Address{}
   291  	for _, acc := range seed2Kp.Accounts {
   292  		keycard4Addresses = append(keycard4Addresses, acc.Address)
   293  	}
   294  	return &Keycard{
   295  		KeycardUID:        "00000000000000000000000000000003",
   296  		KeycardName:       "Card03",
   297  		KeycardLocked:     false,
   298  		AccountsAddresses: keycard4Addresses,
   299  		KeyUID:            seed2Kp.KeyUID,
   300  		Position:          2,
   301  	}
   302  }
   303  
   304  func Contains[T comparable](container []T, element T, isEqual func(T, T) bool) bool {
   305  	for _, e := range container {
   306  		if isEqual(e, element) {
   307  			return true
   308  		}
   309  	}
   310  	return false
   311  }
   312  
   313  func HaveSameElements[T comparable](a []T, b []T, isEqual func(T, T) bool) bool {
   314  	for _, v := range a {
   315  		if !Contains(b, v, isEqual) {
   316  			return false
   317  		}
   318  	}
   319  	return true
   320  }
   321  
   322  func SameAccounts(expected, real *Account) bool {
   323  	return expected.Address == real.Address &&
   324  		expected.KeyUID == real.KeyUID &&
   325  		expected.Wallet == real.Wallet &&
   326  		expected.Chat == real.Chat &&
   327  		expected.Type == real.Type &&
   328  		expected.Path == real.Path &&
   329  		string(expected.PublicKey) == string(real.PublicKey) &&
   330  		expected.Name == real.Name &&
   331  		expected.Emoji == real.Emoji &&
   332  		expected.ColorID == real.ColorID &&
   333  		expected.Hidden == real.Hidden &&
   334  		expected.Clock == real.Clock &&
   335  		expected.Removed == real.Removed &&
   336  		expected.ProdPreferredChainIDs == real.ProdPreferredChainIDs &&
   337  		expected.TestPreferredChainIDs == real.TestPreferredChainIDs
   338  }
   339  
   340  func SameAccountsIncludingPosition(expected, real *Account) bool {
   341  	return SameAccounts(expected, real) && expected.Position == real.Position
   342  }
   343  
   344  func SameAccountsWithDifferentOperable(expected, real *Account, expectedOperableValue AccountOperable) bool {
   345  	return SameAccounts(expected, real) && real.Operable == expectedOperableValue
   346  }
   347  
   348  func SameKeypairs(expected, real *Keypair) bool {
   349  	same := expected.KeyUID == real.KeyUID &&
   350  		expected.Name == real.Name &&
   351  		expected.Type == real.Type &&
   352  		expected.DerivedFrom == real.DerivedFrom &&
   353  		expected.LastUsedDerivationIndex == real.LastUsedDerivationIndex &&
   354  		expected.Clock == real.Clock &&
   355  		len(expected.Accounts) == len(real.Accounts)
   356  
   357  	if same {
   358  		for i := range expected.Accounts {
   359  			found := false
   360  			for j := range real.Accounts {
   361  				if SameAccounts(expected.Accounts[i], real.Accounts[j]) {
   362  					found = true
   363  					break
   364  				}
   365  			}
   366  
   367  			if !found {
   368  				return false
   369  			}
   370  		}
   371  	}
   372  
   373  	return same
   374  }
   375  
   376  func SameKeypairsWithDifferentSyncedFrom(expected, real *Keypair, ignoreSyncedFrom bool, expectedSyncedFromValue string,
   377  	expectedOperableValue AccountOperable) bool {
   378  	same := expected.KeyUID == real.KeyUID &&
   379  		expected.Name == real.Name &&
   380  		expected.Type == real.Type &&
   381  		expected.DerivedFrom == real.DerivedFrom &&
   382  		expected.LastUsedDerivationIndex == real.LastUsedDerivationIndex &&
   383  		expected.Clock == real.Clock &&
   384  		len(expected.Accounts) == len(real.Accounts)
   385  
   386  	if same && !ignoreSyncedFrom {
   387  		same = same && real.SyncedFrom == expectedSyncedFromValue
   388  	}
   389  
   390  	if same {
   391  		for i := range expected.Accounts {
   392  			found := false
   393  			for j := range real.Accounts {
   394  				if SameAccountsWithDifferentOperable(expected.Accounts[i], real.Accounts[j], expectedOperableValue) {
   395  					found = true
   396  					break
   397  				}
   398  			}
   399  
   400  			if !found {
   401  				return false
   402  			}
   403  		}
   404  	}
   405  
   406  	return same
   407  }
   408  
   409  func SameKeycards(expected, real *Keycard) bool {
   410  	same := expected.KeycardUID == real.KeycardUID &&
   411  		expected.KeyUID == real.KeyUID &&
   412  		expected.KeycardName == real.KeycardName &&
   413  		expected.KeycardLocked == real.KeycardLocked &&
   414  		expected.Position == real.Position &&
   415  		len(expected.AccountsAddresses) == len(real.AccountsAddresses)
   416  
   417  	if same {
   418  		for i := range expected.AccountsAddresses {
   419  			found := false
   420  			for j := range real.AccountsAddresses {
   421  				if expected.AccountsAddresses[i] == real.AccountsAddresses[j] {
   422  					found = true
   423  					break
   424  				}
   425  			}
   426  
   427  			if !found {
   428  				return false
   429  			}
   430  		}
   431  	}
   432  
   433  	return same
   434  }