Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@ jest.mock('store/account/slice', () => {
}
})

jest.mock('store/wallet/slice', () => ({
selectWalletById: () => () => ({
id: 'wallet-1',
name: 'Test Wallet',
type: 'MNEMONIC'
})
}))

jest.mock('services/wallet/WalletService', () => ({
__esModule: true,
default: {
getRawXpubXP: jest
.fn()
.mockResolvedValue(
'xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5'
)
}
}))

jest.mock('utils/Logger', () => ({
warn: jest.fn()
}))

const mockDispatch = jest.fn()
const mockListenerApi = {
getState: jest.fn(),
Expand Down Expand Up @@ -44,7 +67,7 @@ describe('avalanche_getAccounts handler', () => {
})

describe('handle', () => {
it('should return success with the list of available accounts', async () => {
it('should return success with the list of available accounts including xpubXP for mnemonic wallets', async () => {
const result = await handler.handle(testRequest, mockListenerApi)

expect(result).toEqual({
Expand All @@ -63,7 +86,10 @@ describe('avalanche_getAccounts handler', () => {
active: true,
type: 'primary',
walletId: 'wallet-1',
walletType: 'mnemonic'
walletType: 'MNEMONIC',
walletName: 'Test Wallet',
xpubXP:
'xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5'
},
{
id: '1',
Expand All @@ -78,10 +104,40 @@ describe('avalanche_getAccounts handler', () => {
active: false,
type: 'primary',
walletId: 'wallet-1',
walletType: 'mnemonic'
walletType: 'MNEMONIC',
walletName: 'Test Wallet',
xpubXP:
'xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5'
}
]
})
})

it('should return accounts without xpubXP for non-supported wallet types', async () => {
// Mock a Ledger wallet (doesn't support xpubXP)
const mockWalletSlice = require('store/wallet/slice')
mockWalletSlice.selectWalletById = () => () => ({
id: 'wallet-1',
name: 'Ledger Wallet',
type: 'LEDGER'
})

// Mock WalletService to throw error for Ledger wallets
const mockWalletService = require('services/wallet/WalletService')
mockWalletService.default.getRawXpubXP.mockRejectedValueOnce(
new Error('Unsupported wallet type')
)

const result = await handler.handle(testRequest, mockListenerApi)

expect(result.success).toBe(true)
if (result.success) {
const accounts = result.value as any[]
expect(accounts).toHaveLength(2)
expect(accounts[0].xpubXP).toBeUndefined()
expect(accounts[0].walletType).toBe('LEDGER')
expect(accounts[0].walletName).toBe('Ledger Wallet')
}
})
})
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { AppListenerEffectAPI } from 'store/types'
import { selectAccounts, selectActiveAccount } from 'store/account/slice'
import { selectWalletById } from 'store/wallet/slice'
import { RpcMethod, RpcRequest } from 'store/rpc/types'
import { rpcErrors } from '@metamask/rpc-errors'
import { WalletType } from 'services/wallet/types'
import WalletService from 'services/wallet/WalletService'
import { HandleResponse, RpcRequestHandler } from '../../types'

export type AvalancheGetAccountsRpcRequest =
Expand All @@ -16,20 +19,47 @@ class AvalancheGetAccountsHandler
_request: AvalancheGetAccountsRpcRequest,
listenerApi: AppListenerEffectAPI
): HandleResponse => {
const accounts = selectAccounts(listenerApi.getState())
const activeAccount = selectActiveAccount(listenerApi.getState())
if (!activeAccount)
const state = listenerApi.getState()
const accounts = selectAccounts(state)
const activeAccount = selectActiveAccount(state)

if (!activeAccount) {
return {
success: false,
error: rpcErrors.internal('no active account')
}
}

const accountsArray = Object.values(accounts).map(account => {
return {
...account,
active: account.id === activeAccount.id
// Helper function to get xpubXP for supported wallet types
const getXpubXP = async (
walletId: string,
walletType: WalletType
): Promise<string | undefined> => {
try {
return await WalletService.getRawXpubXP({ walletId, walletType })
} catch (error) {
return undefined
}
})
}

// Process accounts and add xpubXP where available
const accountsArray = await Promise.all(
Object.values(accounts).map(async account => {
const wallet = selectWalletById(account.walletId)(state)
const xpubXP = wallet
? await getXpubXP(account.walletId, wallet.type)
: undefined

return {
...account,
walletType: wallet?.type,
walletName: wallet?.name,
xpubXP,
active: account.id === activeAccount.id
}
})
)

return { success: true, value: accountsArray }
}
}
Expand Down
Loading