11import { AppListenerEffectAPI } from 'store/types'
22import { selectAccounts , selectActiveAccount } from 'store/account/slice'
3- import { selectWalletById } from 'store/wallet/slice'
3+ import { selectActiveWallet } from 'store/wallet/slice'
44import { RpcMethod , RpcRequest } from 'store/rpc/types'
55import { rpcErrors } from '@metamask/rpc-errors'
6- import { WalletType } from 'services/wallet/types'
76import WalletService from 'services/wallet/WalletService'
87import { HandleResponse , RpcRequestHandler } from '../../types'
98
@@ -21,7 +20,14 @@ class AvalancheGetAccountsHandler
2120 ) : HandleResponse => {
2221 const state = listenerApi . getState ( )
2322 const accounts = selectAccounts ( state )
23+ const activeWallet = selectActiveWallet ( state )
2424 const activeAccount = selectActiveAccount ( state )
25+ if ( ! activeWallet ) {
26+ return {
27+ success : false ,
28+ error : rpcErrors . internal ( 'no active wallet' )
29+ }
30+ }
2531
2632 if ( ! activeAccount ) {
2733 return {
@@ -33,15 +39,22 @@ class AvalancheGetAccountsHandler
3339 // Process accounts and add xpubXP where available
3440 const accountsArray = await Promise . all (
3541 Object . values ( accounts ) . map ( async account => {
36- const wallet = selectWalletById ( account . walletId ) ( state )
37- const xpubXP = wallet
38- ? await this . getXpubXP ( account . walletId , wallet . type , account . index )
39- : undefined
42+ let xpubXP
43+
44+ try {
45+ xpubXP = await WalletService . getRawXpubXP ( {
46+ walletId : activeWallet . id ,
47+ walletType : activeWallet . type ,
48+ accountIndex : account . index
49+ } )
50+ } catch ( error ) {
51+ xpubXP = undefined
52+ }
4053
4154 return {
4255 ...account ,
43- walletType : wallet ? .type ,
44- walletName : wallet ? .name ,
56+ walletType : activeWallet . type ,
57+ walletName : activeWallet . name ,
4558 xpubXP,
4659 active : account . id === activeAccount . id
4760 }
@@ -50,23 +63,6 @@ class AvalancheGetAccountsHandler
5063
5164 return { success : true , value : accountsArray }
5265 }
53-
54- // Helper function to get xpubXP for supported wallet types
55- private getXpubXP = async (
56- walletId : string ,
57- walletType : WalletType ,
58- accountIndex : number
59- ) : Promise < string | undefined > => {
60- try {
61- return await WalletService . getRawXpubXP ( {
62- walletId,
63- walletType,
64- accountIndex
65- } )
66- } catch ( error ) {
67- return undefined
68- }
69- }
7066}
7167
7268export const avalancheGetAccountsHandler = new AvalancheGetAccountsHandler ( )
0 commit comments