-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathaccountPicker.test.ts
More file actions
367 lines (324 loc) · 13.4 KB
/
Copy pathaccountPicker.test.ts
File metadata and controls
367 lines (324 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import { Wallet } from 'ethers'
import { describe, expect, jest, test } from '@jest/globals'
import { suppressConsoleBeforeEach } from '../../../test/helpers/console'
import { makeMainController } from '../../../test/helpers/mainController'
import { DEFAULT_ACCOUNT_LABEL } from '../../consts/account'
import {
BIP44_STANDARD_DERIVATION_TEMPLATE,
DERIVATION_OPTIONS,
SMART_ACCOUNT_SIGNER_KEY_DERIVATION_OFFSET
} from '../../consts/derivation'
import { Account } from '../../interfaces/account'
import { isSmartAccount } from '../../libs/account/account'
import { getPrivateKeyFromSeed, KeyIterator } from '../../libs/keyIterator/keyIterator'
import wait from '../../utils/wait'
import { DEFAULT_PAGE, DEFAULT_PAGE_SIZE } from './accountPicker'
const key1to11BasicAccPublicAddresses = Array.from(
{ length: 11 },
(_, i) =>
new Wallet(getPrivateKeyFromSeed(process.env.SEED, null, i, BIP44_STANDARD_DERIVATION_TEMPLATE))
.address
)
const key1to11BasicAccUsedForSmartAccKeysOnlyPublicAddresses = Array.from(
{ length: 11 },
(_, i) =>
new Wallet(
getPrivateKeyFromSeed(
process.env.SEED,
null,
i + SMART_ACCOUNT_SIGNER_KEY_DERIVATION_OFFSET,
BIP44_STANDARD_DERIVATION_TEMPLATE
)
).address
)
const key1PublicAddress = key1to11BasicAccPublicAddresses[0]!
const basicAccount: Account = {
addr: key1PublicAddress,
associatedKeys: [key1PublicAddress],
initialPrivileges: [
[key1PublicAddress, '0x0000000000000000000000000000000000000000000000000000000000000001']
],
creation: null,
preferences: {
label: DEFAULT_ACCOUNT_LABEL,
pfp: key1PublicAddress
}
}
const prepareTest = async () => {
const { mainCtrl } = await makeMainController()
await mainCtrl.keystore.initialLoadPromise
return {
controller: mainCtrl.accountPicker
}
}
describe('AccountPicker', () => {
test('should initialize', async () => {
const { controller } = await prepareTest()
const keyIterator = new KeyIterator(process.env.SEED)
const hdPathTemplate = BIP44_STANDARD_DERIVATION_TEMPLATE
controller.setInitParams({ keyIterator, hdPathTemplate })
await controller.init()
expect(controller.page).toEqual(DEFAULT_PAGE)
expect(controller.pageSize).toEqual(DEFAULT_PAGE_SIZE)
expect(controller.isInitialized).toBeTruthy()
expect(controller.selectedAccounts.length).toEqual(1)
expect(controller.hdPathTemplate).toEqual(hdPathTemplate)
expect(controller.shouldGetAccountsUsedOnNetworks).toBeFalsy()
expect(controller.shouldSearchForLinkedAccounts).toBeFalsy()
})
describe('Negative tests', () => {
suppressConsoleBeforeEach()
test('should throw if AccountPicker controller method is requested, but the controller was not initialized beforehand', async () => {
const { controller } = await prepareTest()
const unsubscribe = controller.onError(() => {
const notInitializedErrorsCount = controller.emittedErrors.filter(
(e: any) =>
e.error.message ===
'accountPicker: requested a method of the AccountPicker controller, but the controller was not initialized'
).length
if (notInitializedErrorsCount === 4) {
expect(notInitializedErrorsCount).toEqual(4)
}
})
await controller.setPage({ page: 1 })
controller.selectAccount(basicAccount)
controller.deselectAccount(basicAccount)
await controller.addAccounts()
await wait(500) // wait a bit for the errors to be emitted
expect.assertions(1)
unsubscribe()
})
test('should throw if AccountPicker controller gets initialized, but the keyIterator is missing', async () => {
const { controller } = await prepareTest()
const unsubscribe = controller.onError((e) => {
const missingKeyIteratorError = e.error.message === 'accountPicker: missing keyIterator'
if (missingKeyIteratorError) {
expect(missingKeyIteratorError).toBeTruthy()
unsubscribe()
}
})
controller.setInitParams({
keyIterator: null,
hdPathTemplate: BIP44_STANDARD_DERIVATION_TEMPLATE
})
await controller.init()
await wait(500) // wait a bit for the error to be emitted
expect.assertions(1)
})
})
test('should retrieve smart account keys only when scanning for smart accounts', async () => {
const { controller } = await prepareTest()
const PAGE_SIZE = 5
const keyIterator = new KeyIterator(process.env.SEED)
const retrieveSpy = jest.spyOn(keyIterator, 'retrieve')
controller.setInitParams({
keyIterator,
pageSize: PAGE_SIZE,
hdPathTemplate: BIP44_STANDARD_DERIVATION_TEMPLATE,
shouldGetAccountsUsedOnNetworks: false,
shouldSearchForLinkedAccounts: false,
shouldAddNextAccountAutomatically: false
})
await controller.init()
await controller.setPage({ page: 1 })
expect(retrieveSpy).toHaveBeenLastCalledWith(
[{ from: 0, to: PAGE_SIZE - 1 }],
BIP44_STANDARD_DERIVATION_TEMPLATE
)
expect(controller.accountsOnPage).toHaveLength(5)
expect(controller.accountsOnPage.filter((a) => isSmartAccount(a.account))).toHaveLength(0)
expect(controller.accountsOnPage.filter((a) => !isSmartAccount(a.account))).toHaveLength(5)
await controller.findAndSetLinkedAccounts()
expect(retrieveSpy).toHaveBeenCalledWith(
[
{
from: SMART_ACCOUNT_SIGNER_KEY_DERIVATION_OFFSET,
to: SMART_ACCOUNT_SIGNER_KEY_DERIVATION_OFFSET + PAGE_SIZE - 1
}
],
BIP44_STANDARD_DERIVATION_TEMPLATE
)
expect(controller.accountsOnPage.filter((a) => !isSmartAccount(a.account))).toHaveLength(5)
expect(
controller.accountsOnPage.some((a) => isSmartAccount(a.account) && !a.isLinked)
).toBeTruthy()
await controller.setPage({ page: 2 })
expect(retrieveSpy).toHaveBeenLastCalledWith(
[{ from: PAGE_SIZE, to: PAGE_SIZE * 2 - 1 }],
BIP44_STANDARD_DERIVATION_TEMPLATE
)
expect(controller.accountsOnPage).toHaveLength(5)
expect(controller.accountsOnPage.filter((a) => isSmartAccount(a.account))).toHaveLength(0)
expect(controller.accountsOnPage.filter((a) => !isSmartAccount(a.account))).toHaveLength(5)
await controller.findAndSetLinkedAccounts()
expect(retrieveSpy).toHaveBeenCalledWith(
[
{
from: SMART_ACCOUNT_SIGNER_KEY_DERIVATION_OFFSET + PAGE_SIZE,
to: SMART_ACCOUNT_SIGNER_KEY_DERIVATION_OFFSET + PAGE_SIZE * 2 - 1
}
],
BIP44_STANDARD_DERIVATION_TEMPLATE
)
expect(controller.accountsOnPage.filter((a) => !isSmartAccount(a.account))).toHaveLength(5)
expect(
controller.accountsOnPage.some((a) => isSmartAccount(a.account) && !a.isLinked)
).toBeTruthy()
})
test('should find linked accounts', async () => {
const { controller } = await prepareTest()
const keyIterator = new KeyIterator(process.env.SEED)
controller.setInitParams({
keyIterator,
pageSize: 3,
hdPathTemplate: BIP44_STANDARD_DERIVATION_TEMPLATE,
shouldGetAccountsUsedOnNetworks: false,
shouldAddNextAccountAutomatically: false
})
await controller.init()
await controller.setPage({ page: 1 })
expect(controller.linkedAccountsLoading).toBe(false)
expect(controller.linkedAccountsError).toBeFalsy()
const linkedAccountsOnPage = controller.accountsOnPage.filter(({ isLinked }) => isLinked)
const accountsOnSlot1 = linkedAccountsOnPage
.filter(({ slot }) => slot === 1)
.map(({ account }) => account.addr)
// This account was manually added as a signer to one of our test accounts
expect(accountsOnSlot1).toContain('0x740523d7876Fbb8AF246c5B307f26d4b2D2BFDA9')
const accountsOnSlot3 = linkedAccountsOnPage
.filter(({ slot }) => slot === 3)
.map(({ account }) => account.addr)
// These accounts were manually added as signers to our test accounts
expect(accountsOnSlot3).toContain('0x0ace96748e66F42EBeA22D777C2a99eA2c83D8A6')
expect(accountsOnSlot3).toContain('0xc583f33d502dE560dd2C60D4103043d5998A98E5')
expect(accountsOnSlot3).toContain('0xbEC6dB2638b29ffEf42df2B5B76B531d420FF18E')
expect(accountsOnSlot3).toContain('0x997dF27B04C89796254e61B71c112250dE87a803')
})
test('should be able to select and then deselect an account', async () => {
const { controller } = await prepareTest()
const keyIterator = new KeyIterator(process.env.SEED)
controller.setInitParams({
keyIterator,
pageSize: 1,
hdPathTemplate: BIP44_STANDARD_DERIVATION_TEMPLATE,
shouldSearchForLinkedAccounts: false,
shouldGetAccountsUsedOnNetworks: false,
shouldAddNextAccountAutomatically: false
})
await controller.init()
await controller.setPage({ page: 1 })
controller.selectAccount(basicAccount)
const selectedAccountAddr = controller.selectedAccounts.map((a) => a.account.addr)
expect(selectedAccountAddr).toContain(basicAccount.addr)
controller.deselectAccount(basicAccount)
expect(controller.selectedAccounts).toHaveLength(0)
})
test('should NOT be able to select the same account more than once', async () => {
const { controller } = await prepareTest()
const keyIterator = new KeyIterator(process.env.SEED)
controller.setInitParams({
keyIterator,
pageSize: 1,
hdPathTemplate: BIP44_STANDARD_DERIVATION_TEMPLATE,
shouldSearchForLinkedAccounts: false,
shouldGetAccountsUsedOnNetworks: false,
shouldAddNextAccountAutomatically: false
})
await controller.init()
await controller.setPage({ page: 1 })
controller.selectAccount(basicAccount)
controller.selectAccount(basicAccount)
controller.selectAccount(basicAccount)
expect(controller.selectedAccounts).toHaveLength(1)
const selectedAccountAddr = controller.selectedAccounts.map((a) => a.account.addr)
expect(selectedAccountAddr).toContain(basicAccount.addr)
})
test('should be able to select all the keys of a selected EOA (always one key)', async () => {
const { controller } = await prepareTest()
const keyIterator = new KeyIterator(process.env.SEED)
controller.setInitParams({
keyIterator,
hdPathTemplate: BIP44_STANDARD_DERIVATION_TEMPLATE,
shouldSearchForLinkedAccounts: false,
shouldGetAccountsUsedOnNetworks: false,
shouldAddNextAccountAutomatically: false
})
await controller.init()
await controller.setPage({ page: 1 })
controller.selectAccount(basicAccount)
expect(controller.selectedAccounts[0]!.accountKeys).toHaveLength(1)
const keyAddr = controller.selectedAccounts[0]!.accountKeys[0]!.addr
const keyIndex = controller.selectedAccounts[0]!.accountKeys[0]!.index
expect(keyAddr).toEqual(basicAccount.addr)
expect(keyIndex).toEqual(0)
})
test('should be able to select all the keys of a selected smart account (derived key)', async () => {
const { controller } = await prepareTest()
const keyIterator = new KeyIterator(process.env.SEED)
controller.setInitParams({
keyIterator,
hdPathTemplate: BIP44_STANDARD_DERIVATION_TEMPLATE,
shouldSearchForLinkedAccounts: false,
shouldGetAccountsUsedOnNetworks: false,
shouldAddNextAccountAutomatically: false
})
await controller.init()
await controller.setPage({ page: 1 })
await controller.findAndSetLinkedAccounts()
const smartAccount = controller.accountsOnPage.find(
(x) => isSmartAccount(x.account) && !x.isLinked
)
if (smartAccount) controller.selectAccount(smartAccount.account)
expect(controller.selectedAccounts[0]!.accountKeys)
// Might contain other keys too, but this one should be in there,
// since that's the derived used only for smart account key
.toContainEqual({
addr: key1to11BasicAccUsedForSmartAccKeysOnlyPublicAddresses[0],
index: SMART_ACCOUNT_SIGNER_KEY_DERIVATION_OFFSET,
slot: 1
})
})
DERIVATION_OPTIONS.forEach(({ label, value }) => {
test(`should derive correctly ${label}`, async () => {
const { controller } = await prepareTest()
const keyIterator = new KeyIterator(process.env.SEED)
const pageSize = 5
controller.setInitParams({
keyIterator,
hdPathTemplate: value,
pageSize,
shouldSearchForLinkedAccounts: false,
shouldGetAccountsUsedOnNetworks: false,
shouldAddNextAccountAutomatically: false
})
await controller.init()
// Checks page 1 EOAs
await controller.setPage({ page: 1 })
const basicAccountsOnFirstPage = controller.accountsOnPage.filter(
(x) => !isSmartAccount(x.account)
)
const key1to5BasicAccPublicAddresses = Array.from(
{ length: pageSize },
(_, i) => new Wallet(getPrivateKeyFromSeed(process.env.SEED, null, i, value)).address
)
basicAccountsOnFirstPage.forEach((x) => {
const address = x.account.addr
expect(address).toBe(key1to5BasicAccPublicAddresses[x.index])
})
// Checks page 2 EOAs
await controller.setPage({ page: 2 })
const basicAccountsOnSecondPage = controller.accountsOnPage.filter(
(x) => !isSmartAccount(x.account)
)
const key6to10BasicAccPublicAddresses = Array.from(
{ length: pageSize },
(_, i) =>
new Wallet(getPrivateKeyFromSeed(process.env.SEED, null, i + pageSize, value)).address
)
basicAccountsOnSecondPage.forEach((x) => {
const address = x.account.addr
expect(address).toBe(key6to10BasicAccPublicAddresses[x.index - pageSize])
})
})
})
})