@@ -2,6 +2,13 @@ import { fromEventPattern, Observable } from 'rxjs'
2
2
import { filter , takeUntil , take , share , switchMap } from 'rxjs/operators'
3
3
import partition from 'lodash.partition'
4
4
import { providers } from 'ethers'
5
+ import { weiToEth } from '@web3-onboard/common'
6
+ import { disconnectWallet$ } from './streams.js'
7
+ import { updateAccount , updateWallet } from './store/actions.js'
8
+ import { validEnsChain } from './utils.js'
9
+ import disconnect from './disconnect.js'
10
+ import { state } from './store/index.js'
11
+ import { getBlocknativeSdk } from './services.js'
5
12
6
13
import type {
7
14
ChainId ,
@@ -14,14 +21,14 @@ import type {
14
21
SelectAccountsRequest
15
22
} from '@web3-onboard/common'
16
23
17
- import { weiToEth } from '@web3-onboard/common'
18
- import { disconnectWallet$ } from './streams.js'
19
- import type { Account , Address , Balances , Ens , WalletState } from './types.js'
20
- import { updateAccount , updateWallet } from './store/actions.js'
21
- import { validEnsChain } from './utils.js'
22
- import disconnect from './disconnect.js'
23
- import { state } from './store/index.js'
24
- import { getBlocknativeSdk } from './services .js'
24
+ import type {
25
+ Account ,
26
+ Address ,
27
+ Balances ,
28
+ Ens ,
29
+ WalletPermission ,
30
+ WalletState
31
+ } from './types .js'
25
32
26
33
export const ethersProviders : {
27
34
[ key : string ] : providers . StaticJsonRpcProvider
@@ -112,6 +119,17 @@ export function trackWallet(
112
119
113
120
// when account changed, set it to first account and subscribe to events
114
121
accountsChanged$ . subscribe ( async ( [ address ] ) => {
122
+ // sync accounts with internal state
123
+ // in the case of an account has been manually disconnected
124
+ try {
125
+ await syncWalletConnectedAccounts ( label )
126
+ } catch ( error ) {
127
+ console . warn (
128
+ 'Web3Onboard: Error whilst trying to sync connected accounts:' ,
129
+ error
130
+ )
131
+ }
132
+
115
133
// no address, then no account connected, so disconnect wallet
116
134
// this could happen if user locks wallet,
117
135
// or if disconnects app from wallet
@@ -180,11 +198,12 @@ export function trackWallet(
180
198
const balanceProm = getBalance ( address , chain )
181
199
const account = accounts . find ( account => account . address === address )
182
200
183
- const ensProm = account . ens
184
- ? Promise . resolve ( account . ens )
185
- : validEnsChain ( connectedWalletChain . id )
186
- ? getEns ( address , chain )
187
- : Promise . resolve ( null )
201
+ const ensProm =
202
+ account && account . ens
203
+ ? Promise . resolve ( account . ens )
204
+ : validEnsChain ( connectedWalletChain . id )
205
+ ? getEns ( address , chain )
206
+ : Promise . resolve ( null )
188
207
189
208
return Promise . all ( [ Promise . resolve ( address ) , balanceProm , ensProm ] )
190
209
} )
@@ -390,3 +409,41 @@ export function addNewChain(
390
409
]
391
410
} )
392
411
}
412
+
413
+ export async function getPermissions (
414
+ provider : EIP1193Provider
415
+ ) : Promise < WalletPermission [ ] > {
416
+ try {
417
+ const permissions = ( await provider . request ( {
418
+ method : 'wallet_getPermissions'
419
+ } ) ) as WalletPermission [ ]
420
+
421
+ return Array . isArray ( permissions ) ? permissions : [ ]
422
+ } catch ( error ) {
423
+ return [ ]
424
+ }
425
+ }
426
+
427
+ export async function syncWalletConnectedAccounts (
428
+ label : WalletState [ 'label' ]
429
+ ) : Promise < void > {
430
+ const wallet = state . get ( ) . wallets . find ( wallet => wallet . label === label )
431
+ const permissions = await getPermissions ( wallet . provider )
432
+ const accountsPermissions = permissions . find (
433
+ ( { parentCapability } ) => parentCapability === 'eth_accounts'
434
+ )
435
+
436
+ if ( accountsPermissions ) {
437
+ const { value : connectedAccounts } = accountsPermissions . caveats . find (
438
+ ( { type } ) => type === 'restrictReturnedAccounts'
439
+ ) || { value : null }
440
+
441
+ if ( connectedAccounts ) {
442
+ const syncedAccounts = wallet . accounts . filter ( ( { address } ) =>
443
+ connectedAccounts . includes ( address )
444
+ )
445
+
446
+ updateWallet ( wallet . label , { ...wallet , accounts : syncedAccounts } )
447
+ }
448
+ }
449
+ }
0 commit comments