wallet-stack 1.0.0-alpha.126 → 1.0.0-alpha.127
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/account/saga.test.ts +31 -7
- package/src/account/saga.ts +7 -3
- package/src/web3/saga.test.ts +6 -14
- package/src/web3/saga.ts +9 -14
package/package.json
CHANGED
package/src/account/saga.test.ts
CHANGED
|
@@ -21,7 +21,7 @@ import Logger from 'src/utils/Logger'
|
|
|
21
21
|
import { ViemKeychainAccount } from 'src/viem/keychainAccountToAccount'
|
|
22
22
|
import { getKeychainAccounts } from 'src/web3/contracts'
|
|
23
23
|
import networkConfig from 'src/web3/networkConfig'
|
|
24
|
-
import { UnlockResult,
|
|
24
|
+
import { UnlockResult, createAccount, unlockAccount } from 'src/web3/saga'
|
|
25
25
|
import { walletAddressSelector } from 'src/web3/selectors'
|
|
26
26
|
import { initializeAccountSuccess, saveSignedMessage } from './actions'
|
|
27
27
|
|
|
@@ -140,6 +140,33 @@ describe('initializeAccount', () => {
|
|
|
140
140
|
mockFetch.resetMocks()
|
|
141
141
|
})
|
|
142
142
|
|
|
143
|
+
it('should call createAccount when not restoring', async () => {
|
|
144
|
+
await expectSaga(initializeAccountSaga)
|
|
145
|
+
.provide([
|
|
146
|
+
[select(choseToRestoreAccountSelector), false],
|
|
147
|
+
[call(createAccount), undefined],
|
|
148
|
+
[call(generateSignedMessage), undefined],
|
|
149
|
+
])
|
|
150
|
+
.call(createAccount)
|
|
151
|
+
.put(initializeAccountSuccess())
|
|
152
|
+
.run()
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
it('should skip createAccount when restoring', async () => {
|
|
156
|
+
mockFetch.mockResponse(JSON.stringify({ data: { phoneNumbers: [] } }))
|
|
157
|
+
|
|
158
|
+
await expectSaga(initializeAccountSaga)
|
|
159
|
+
.provide([
|
|
160
|
+
[select(choseToRestoreAccountSelector), true],
|
|
161
|
+
[call(generateSignedMessage), undefined],
|
|
162
|
+
[call(retrieveSignedMessage), 'some signed message'],
|
|
163
|
+
[select(walletAddressSelector), '0xabc'],
|
|
164
|
+
])
|
|
165
|
+
.not.call.fn(createAccount)
|
|
166
|
+
.put(initializeAccountSuccess())
|
|
167
|
+
.run()
|
|
168
|
+
})
|
|
169
|
+
|
|
143
170
|
it('should handle the last previously verified phone number', async () => {
|
|
144
171
|
mockFetch.mockResponse(
|
|
145
172
|
JSON.stringify({ data: { phoneNumbers: ['+1302123456', '+31619123456'] } })
|
|
@@ -147,9 +174,8 @@ describe('initializeAccount', () => {
|
|
|
147
174
|
|
|
148
175
|
await expectSaga(initializeAccountSaga)
|
|
149
176
|
.provide([
|
|
150
|
-
[call(getOrCreateAccount), undefined],
|
|
151
|
-
[call(generateSignedMessage), undefined],
|
|
152
177
|
[select(choseToRestoreAccountSelector), true],
|
|
178
|
+
[call(generateSignedMessage), undefined],
|
|
153
179
|
[call(retrieveSignedMessage), 'some signed message'],
|
|
154
180
|
[select(walletAddressSelector), '0xabc'],
|
|
155
181
|
])
|
|
@@ -176,9 +202,8 @@ describe('initializeAccount', () => {
|
|
|
176
202
|
|
|
177
203
|
await expectSaga(initializeAccountSaga)
|
|
178
204
|
.provide([
|
|
179
|
-
[call(getOrCreateAccount), undefined],
|
|
180
|
-
[call(generateSignedMessage), undefined],
|
|
181
205
|
[select(choseToRestoreAccountSelector), true],
|
|
206
|
+
[call(generateSignedMessage), undefined],
|
|
182
207
|
[call(retrieveSignedMessage), 'some signed message'],
|
|
183
208
|
[select(walletAddressSelector), '0xabc'],
|
|
184
209
|
])
|
|
@@ -194,9 +219,8 @@ describe('initializeAccount', () => {
|
|
|
194
219
|
|
|
195
220
|
await expectSaga(initializeAccountSaga)
|
|
196
221
|
.provide([
|
|
197
|
-
[call(getOrCreateAccount), undefined],
|
|
198
|
-
[call(generateSignedMessage), undefined],
|
|
199
222
|
[select(choseToRestoreAccountSelector), true],
|
|
223
|
+
[call(generateSignedMessage), undefined],
|
|
200
224
|
[call(retrieveSignedMessage), 'some signed message'],
|
|
201
225
|
[select(walletAddressSelector), '0xabc'],
|
|
202
226
|
])
|
package/src/account/saga.ts
CHANGED
|
@@ -40,7 +40,7 @@ import { safely } from 'src/utils/safely'
|
|
|
40
40
|
import { clearStoredAccounts } from 'src/web3/KeychainAccounts'
|
|
41
41
|
import { getKeychainAccounts } from 'src/web3/contracts'
|
|
42
42
|
import networkConfig from 'src/web3/networkConfig'
|
|
43
|
-
import {
|
|
43
|
+
import { createAccount, getWalletAddress, unlockAccount } from 'src/web3/saga'
|
|
44
44
|
import { walletAddressSelector } from 'src/web3/selectors'
|
|
45
45
|
import { call, put, select, spawn, take, takeLeading } from 'typed-redux-saga'
|
|
46
46
|
const TAG = 'account/saga'
|
|
@@ -75,10 +75,14 @@ export function* initializeAccountSaga() {
|
|
|
75
75
|
Logger.debug(TAG + '@initializeAccountSaga', 'Creating account')
|
|
76
76
|
try {
|
|
77
77
|
AppAnalytics.track(OnboardingEvents.initialize_account_start)
|
|
78
|
-
yield* call(getOrCreateAccount)
|
|
79
|
-
yield* call(generateSignedMessage)
|
|
80
78
|
|
|
81
79
|
const choseToRestoreAccount = yield* select(choseToRestoreAccountSelector)
|
|
80
|
+
if (!choseToRestoreAccount) {
|
|
81
|
+
yield* call(createAccount)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
yield* call(generateSignedMessage)
|
|
85
|
+
|
|
82
86
|
if (choseToRestoreAccount) {
|
|
83
87
|
yield* call(handlePreviouslyVerifiedPhoneNumber)
|
|
84
88
|
}
|
package/src/web3/saga.test.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { call, select } from 'redux-saga/effects'
|
|
|
4
4
|
import { generateSignedMessage } from 'src/account/saga'
|
|
5
5
|
import { ErrorMessages } from 'src/app/ErrorMessages'
|
|
6
6
|
import { storeMnemonic } from 'src/backup/utils'
|
|
7
|
+
import { clearStoredAccounts } from 'src/web3/KeychainAccounts'
|
|
7
8
|
import { currentLanguageSelector } from 'src/i18n/selectors'
|
|
8
9
|
import { getPasswordSaga, retrieveSignedMessage } from 'src/pincode/authentication'
|
|
9
10
|
import { MnemonicLanguages, MnemonicStrength, generateMnemonic } from 'src/utils/account'
|
|
@@ -12,7 +13,7 @@ import {
|
|
|
12
13
|
UnlockResult,
|
|
13
14
|
getConnectedAccount,
|
|
14
15
|
getConnectedUnlockedAccount,
|
|
15
|
-
|
|
16
|
+
createAccount,
|
|
16
17
|
getWalletAddress,
|
|
17
18
|
unlockAccount,
|
|
18
19
|
assignAccountFromPrivateKey,
|
|
@@ -40,15 +41,7 @@ const state = createMockStore({
|
|
|
40
41
|
web3: { account: mockAccount },
|
|
41
42
|
}).getState()
|
|
42
43
|
|
|
43
|
-
describe(
|
|
44
|
-
it('returns an existing account', async () => {
|
|
45
|
-
await expectSaga(getOrCreateAccount)
|
|
46
|
-
.withState(state)
|
|
47
|
-
.not.call.fn(generateMnemonic)
|
|
48
|
-
.returns('0x0000000000000000000000000000000000007e57')
|
|
49
|
-
.run()
|
|
50
|
-
})
|
|
51
|
-
|
|
44
|
+
describe(createAccount, () => {
|
|
52
45
|
it.each`
|
|
53
46
|
expectedAddress | expectedPrivateDek | mnemonic
|
|
54
47
|
${'0xE025583d25Eff2C254999b5904C97bAe9B3F8D83'} | ${'0xb6812219f7003c27cc1ef17c2033c033a38cfc52d83f176a0667086787d59d39'} | ${'avellana novio zona pinza ducha íntimo amante diluir toldo peón ocio encía gen balcón carro lingote millón amasar mármol bondad toser soledad croqueta agosto'}
|
|
@@ -57,10 +50,9 @@ describe(getOrCreateAccount, () => {
|
|
|
57
50
|
`(
|
|
58
51
|
'creates a new account $expectedAddress',
|
|
59
52
|
async ({ expectedAddress, expectedPrivateDek, mnemonic }) => {
|
|
60
|
-
await expectSaga(
|
|
53
|
+
await expectSaga(createAccount)
|
|
61
54
|
.withState(state)
|
|
62
55
|
.provide([
|
|
63
|
-
[select(currentAccountSelector), null],
|
|
64
56
|
[matchers.call.fn(generateMnemonic), mnemonic],
|
|
65
57
|
[
|
|
66
58
|
call(storeMnemonic, mnemonic, expectedAddress),
|
|
@@ -71,6 +63,7 @@ describe(getOrCreateAccount, () => {
|
|
|
71
63
|
],
|
|
72
64
|
[call(getPasswordSaga, expectedAddress, false, true), 'somePassword'],
|
|
73
65
|
])
|
|
66
|
+
.call(clearStoredAccounts)
|
|
74
67
|
.put(setAccount(expectedAddress))
|
|
75
68
|
.returns(expectedAddress)
|
|
76
69
|
.run()
|
|
@@ -86,10 +79,9 @@ describe(getOrCreateAccount, () => {
|
|
|
86
79
|
`(
|
|
87
80
|
'creates an account with a mnemonic in $expectedMnemonicLang when app language is $appLang',
|
|
88
81
|
async ({ appLang, expectedMnemonicLang }) => {
|
|
89
|
-
const { returnValue } = await expectSaga(
|
|
82
|
+
const { returnValue } = await expectSaga(createAccount)
|
|
90
83
|
.withState(state)
|
|
91
84
|
.provide([
|
|
92
|
-
[select(currentAccountSelector), null],
|
|
93
85
|
[select(currentLanguageSelector), appLang],
|
|
94
86
|
[
|
|
95
87
|
matchers.call.fn(storeMnemonic),
|
package/src/web3/saga.ts
CHANGED
|
@@ -12,28 +12,23 @@ import Logger from 'src/utils/Logger'
|
|
|
12
12
|
import { MnemonicLanguages, MnemonicStrength, generateMnemonic } from 'src/utils/account'
|
|
13
13
|
import { privateKeyToAddress } from 'src/utils/address'
|
|
14
14
|
import { ensureError } from 'src/utils/ensureError'
|
|
15
|
+
import { clearStoredAccounts } from 'src/web3/KeychainAccounts'
|
|
15
16
|
import { Actions, SetAccountAction, setAccount } from 'src/web3/actions'
|
|
16
17
|
import { UNLOCK_DURATION } from 'src/web3/consts'
|
|
17
18
|
import { getKeychainAccounts } from 'src/web3/contracts'
|
|
18
|
-
import {
|
|
19
|
+
import { walletAddressSelector } from 'src/web3/selectors'
|
|
19
20
|
import { call, put, select, take } from 'typed-redux-saga'
|
|
20
21
|
import { RootState } from '../redux/reducers'
|
|
21
22
|
|
|
22
23
|
const TAG = 'web3/saga'
|
|
23
24
|
|
|
24
|
-
export function*
|
|
25
|
-
const account = yield* select(currentAccountSelector)
|
|
26
|
-
if (account) {
|
|
27
|
-
Logger.debug(
|
|
28
|
-
TAG + '@getOrCreateAccount',
|
|
29
|
-
'Tried to create account twice, returning the existing one'
|
|
30
|
-
)
|
|
31
|
-
return account
|
|
32
|
-
}
|
|
33
|
-
|
|
25
|
+
export function* createAccount() {
|
|
34
26
|
let privateKey: string | undefined
|
|
35
27
|
try {
|
|
36
|
-
Logger.debug(TAG + '@
|
|
28
|
+
Logger.debug(TAG + '@createAccount', 'Clearing stored accounts')
|
|
29
|
+
yield* call(clearStoredAccounts)
|
|
30
|
+
|
|
31
|
+
Logger.debug(TAG + '@createAccount', 'Creating a new account')
|
|
37
32
|
|
|
38
33
|
const mnemonicBitLength = MnemonicStrength.s128_12words
|
|
39
34
|
const mnemonicLanguage = MnemonicLanguages.english
|
|
@@ -45,7 +40,7 @@ export function* getOrCreateAccount() {
|
|
|
45
40
|
}
|
|
46
41
|
let duplicateInMnemonic = checkDuplicate(mnemonic)
|
|
47
42
|
while (duplicateInMnemonic) {
|
|
48
|
-
Logger.debug(TAG + '@
|
|
43
|
+
Logger.debug(TAG + '@createAccount', 'Regenerating mnemonic to avoid duplicates')
|
|
49
44
|
mnemonic = yield* call(generateMnemonic, mnemonicBitLength, mnemonicLanguage)
|
|
50
45
|
duplicateInMnemonic = checkDuplicate(mnemonic)
|
|
51
46
|
}
|
|
@@ -71,7 +66,7 @@ export function* getOrCreateAccount() {
|
|
|
71
66
|
} catch (err) {
|
|
72
67
|
const error = ensureError(err)
|
|
73
68
|
const sanitizedError = Logger.sanitizeError(error, privateKey)
|
|
74
|
-
Logger.error(TAG + '@
|
|
69
|
+
Logger.error(TAG + '@createAccount', 'Error creating account', sanitizedError)
|
|
75
70
|
throw new Error(ErrorMessages.ACCOUNT_SETUP_FAILED)
|
|
76
71
|
}
|
|
77
72
|
}
|