@@ -69,6 +69,25 @@ func mustXKey(key string) *hdkeychain.ExtendedKey {
69
69
return xkey
70
70
}
71
71
72
+ func checkShownAccountsLen (t * testing.T , b * Backend , expectedLoaded int , expectedPersisted int ) {
73
+ t .Helper ()
74
+ cntLoaded := 0
75
+ for _ , acct := range b .Accounts () {
76
+ if ! acct .Config ().Config .HiddenBecauseUnused {
77
+ cntLoaded ++
78
+ }
79
+ }
80
+ require .Equal (t , expectedLoaded , cntLoaded )
81
+
82
+ cntPersisted := 0
83
+ for _ , acct := range b .Config ().AccountsConfig ().Accounts {
84
+ if ! acct .HiddenBecauseUnused {
85
+ cntPersisted ++
86
+ }
87
+ }
88
+ require .Equal (t , expectedPersisted , cntPersisted )
89
+ }
90
+
72
91
// A keystore with a similar config to a BitBox02 - supporting unified and multiple accounts, no
73
92
// legacy P2PKH.
74
93
func makeBitbox02LikeKeystore () * keystoremock.KeystoreMock {
@@ -1061,8 +1080,7 @@ func TestAccountSupported(t *testing.T) {
1061
1080
// Registering a Bitcoin-only like keystore loads also the altcoins that were persisted
1062
1081
// previously, because they are marked watch-only, so they should be visible.
1063
1082
b .registerKeystore (bb02BtcOnly )
1064
- require .Len (t , b .Accounts (), 3 )
1065
- require .Len (t , b .Config ().AccountsConfig ().Accounts , 3 )
1083
+ checkShownAccountsLen (t , b , 3 , 3 )
1066
1084
1067
1085
// If watch-only is disabled, then these will not be loaded if not supported by the keystore.
1068
1086
require .NoError (t , b .SetWatchonly (false ))
@@ -1071,8 +1089,7 @@ func TestAccountSupported(t *testing.T) {
1071
1089
// Registering a Bitcoin-only like keystore loads only the Bitcoin account, even though altcoins
1072
1090
// were persisted previously.
1073
1091
b .registerKeystore (bb02BtcOnly )
1074
- require .Len (t , b .Accounts (), 1 )
1075
- require .Len (t , b .Config ().AccountsConfig ().Accounts , 3 )
1092
+ checkShownAccountsLen (t , b , 1 , 3 )
1076
1093
}
1077
1094
1078
1095
func TestInactiveAccount (t * testing.T ) {
@@ -1096,33 +1113,28 @@ func TestInactiveAccount(t *testing.T) {
1096
1113
1097
1114
// Deactive an account.
1098
1115
require .NoError (t , b .SetAccountActive ("v0-55555555-btc-0" , false ))
1099
- require .Len (t , b .Accounts (), 3 )
1100
- require .Len (t , b .Config ().AccountsConfig ().Accounts , 3 )
1116
+ checkShownAccountsLen (t , b , 3 , 3 )
1101
1117
require .True (t , b .Config ().AccountsConfig ().Lookup ("v0-55555555-btc-0" ).Inactive )
1102
1118
require .False (t , ! lookup (b .Accounts (), "v0-55555555-btc-0" ).Config ().Config .Inactive )
1103
1119
1104
1120
// Reactivate.
1105
1121
require .NoError (t , b .SetAccountActive ("v0-55555555-btc-0" , true ))
1106
- require .Len (t , b .Accounts (), 3 )
1107
- require .Len (t , b .Config ().AccountsConfig ().Accounts , 3 )
1122
+ checkShownAccountsLen (t , b , 3 , 3 )
1108
1123
require .False (t , b .Config ().AccountsConfig ().Lookup ("v0-55555555-btc-0" ).Inactive )
1109
1124
require .True (t , ! lookup (b .Accounts (), "v0-55555555-btc-0" ).Config ().Config .Inactive )
1110
1125
1111
1126
// Deactivating an ETH account with tokens also removes the tokens
1112
1127
require .NoError (t , b .SetTokenActive ("v0-55555555-eth-0" , "eth-erc20-usdt" , true ))
1113
1128
require .NoError (t , b .SetTokenActive ("v0-55555555-eth-0" , "eth-erc20-bat" , true ))
1114
- require .Len (t , b .Accounts (), 5 )
1115
- require .Len (t , b .Config ().AccountsConfig ().Accounts , 3 )
1129
+ checkShownAccountsLen (t , b , 5 , 3 )
1116
1130
require .NoError (t , b .SetAccountActive ("v0-55555555-eth-0" , false ))
1117
- require .Len (t , b .Accounts (), 5 )
1118
- require .Len (t , b .Config ().AccountsConfig ().Accounts , 3 )
1131
+ checkShownAccountsLen (t , b , 5 , 3 )
1119
1132
require .False (t , ! lookup (b .Accounts (), "v0-55555555-eth-0" ).Config ().Config .Inactive )
1120
1133
require .False (t , ! lookup (b .Accounts (), "v0-55555555-eth-0-eth-erc20-usdt" ).Config ().Config .Inactive )
1121
1134
require .False (t , ! lookup (b .Accounts (), "v0-55555555-eth-0-eth-erc20-bat" ).Config ().Config .Inactive )
1122
1135
// Reactivating restores them again.
1123
1136
require .NoError (t , b .SetAccountActive ("v0-55555555-eth-0" , true ))
1124
- require .Len (t , b .Accounts (), 5 )
1125
- require .Len (t , b .Config ().AccountsConfig ().Accounts , 3 )
1137
+ checkShownAccountsLen (t , b , 5 , 3 )
1126
1138
require .True (t , ! lookup (b .Accounts (), "v0-55555555-eth-0" ).Config ().Config .Inactive )
1127
1139
require .True (t , ! lookup (b .Accounts (), "v0-55555555-eth-0-eth-erc20-usdt" ).Config ().Config .Inactive )
1128
1140
require .True (t , ! lookup (b .Accounts (), "v0-55555555-eth-0-eth-erc20-bat" ).Config ().Config .Inactive )
@@ -1131,15 +1143,13 @@ func TestInactiveAccount(t *testing.T) {
1131
1143
require .NoError (t , b .SetAccountActive ("v0-55555555-btc-0" , false ))
1132
1144
require .NoError (t , b .SetAccountActive ("v0-55555555-ltc-0" , false ))
1133
1145
require .NoError (t , b .SetAccountActive ("v0-55555555-eth-0" , false ))
1134
- require .Len (t , b .Accounts (), 5 )
1135
- require .Len (t , b .Config ().AccountsConfig ().Accounts , 3 )
1146
+ checkShownAccountsLen (t , b , 5 , 3 )
1136
1147
1137
1148
// Re-registering the keystore (i.e. replugging the device) ends in the same state: no
1138
1149
// additional accounts created.
1139
1150
b .DeregisterKeystore ()
1140
1151
b .registerKeystore (bitbox02LikeKeystore )
1141
- require .Len (t , b .Accounts (), 5 )
1142
- require .Len (t , b .Config ().AccountsConfig ().Accounts , 3 )
1152
+ checkShownAccountsLen (t , b , 5 , 3 )
1143
1153
}
1144
1154
1145
1155
// Test that taproot subaccounts are added if a keytore gains taproot support (e.g. BitBox02 gained
@@ -1209,8 +1219,7 @@ func TestTaprootUpgrade(t *testing.T) {
1209
1219
1210
1220
// 1) Registering a new keystore persists a set of initial default accounts.
1211
1221
b .registerKeystore (bitbox02NoTaproot )
1212
- require .Len (t , b .Accounts (), 3 )
1213
- require .Len (t , b .Config ().AccountsConfig ().Accounts , 3 )
1222
+ checkShownAccountsLen (t , b , 3 , 3 )
1214
1223
btcAccount := lookup (b .Accounts (), "v0-55555555-btc-0" )
1215
1224
require .NotNil (t , btcAccount )
1216
1225
ltcAccount := lookup (b .Accounts (), "v0-55555555-ltc-0" )
@@ -1230,8 +1239,7 @@ func TestTaprootUpgrade(t *testing.T) {
1230
1239
// "Unplug", then insert an updated keystore with taproot support.
1231
1240
b .DeregisterKeystore ()
1232
1241
b .registerKeystore (bitbox02Taproot )
1233
- require .Len (t , b .Accounts (), 3 )
1234
- require .Len (t , b .Config ().AccountsConfig ().Accounts , 3 )
1242
+ checkShownAccountsLen (t , b , 3 , 3 )
1235
1243
btcAccount = lookup (b .Accounts (), "v0-55555555-btc-0" )
1236
1244
require .NotNil (t , btcAccount )
1237
1245
ltcAccount = lookup (b .Accounts (), "v0-55555555-ltc-0" )
@@ -1270,15 +1278,18 @@ func TestMaybeAddHiddenUnusedAccounts(t *testing.T) {
1270
1278
b .registerKeystore (makeBitbox02LikeKeystore ())
1271
1279
1272
1280
// Initial accounts added: Bitcoin, Litecoin, Ethereum.
1273
- require .Len (t , b .accounts , 3 )
1274
- require .Len (t , b .config .AccountsConfig ().Accounts , 3 )
1281
+ checkShownAccountsLen (t , b , 3 , 3 )
1275
1282
1276
1283
// Up to 6 hidden accounts for BTC/LTC are added to be scanned even if the accounts are all
1277
- // empty.
1278
- for i := 1 ; i <= 5 ; i ++ {
1284
+ // empty. Calling this function too many times does not add more than that.
1285
+ for i := 1 ; i <= 10 ; i ++ {
1279
1286
b .maybeAddHiddenUnusedAccounts ()
1280
- require .Len (t , b .accounts , 3 + 2 * i )
1281
- require .Len (t , b .config .AccountsConfig ().Accounts , 3 + 2 * i )
1287
+ }
1288
+
1289
+ require .Len (t , b .accounts , 3 + 2 * 5 )
1290
+ require .Len (t , b .config .AccountsConfig ().Accounts , 3 + 2 * 5 )
1291
+
1292
+ for i := 1 ; i <= 5 ; i ++ {
1282
1293
for _ , addedAccountCode := range []string {
1283
1294
fmt .Sprintf ("v0-55555555-btc-%d" , i ),
1284
1295
fmt .Sprintf ("v0-55555555-ltc-%d" , i ),
0 commit comments