diff --git a/src/services/ecosystem/handlers/wallet.test.ts b/src/services/ecosystem/handlers/wallet.test.ts new file mode 100644 index 000000000..811db7026 --- /dev/null +++ b/src/services/ecosystem/handlers/wallet.test.ts @@ -0,0 +1,47 @@ +import { describe, it, expect, beforeEach } from 'vitest' +import { handleChainId } from './wallet' +import { walletStore } from '@/stores/wallet' +import type { HandlerContext } from '../types' + +describe('handleChainId', () => { + const mockContext: HandlerContext = { + appId: 'test-app', + appName: 'Test App', + origin: 'https://test.com', + permissions: [], + } + + beforeEach(() => { + // Reset store state + walletStore.setState(() => ({ + wallets: [], + currentWalletId: null, + selectedChain: 'bfmeta', + chainPreferences: {}, + isLoading: false, + isInitialized: false, + migrationRequired: false, + })) + }) + + it('should return the currently selected chain ID', async () => { + // Set a different chain + walletStore.setState((state) => ({ + ...state, + selectedChain: 'ethereum', + })) + + const chainId = await handleChainId({}, mockContext) + expect(chainId).toBe('ethereum') + }) + + it('should return bfmeta if that is the selected chain', async () => { + walletStore.setState((state) => ({ + ...state, + selectedChain: 'bfmeta', + })) + + const chainId = await handleChainId({}, mockContext) + expect(chainId).toBe('bfmeta') + }) +}) diff --git a/src/services/ecosystem/handlers/wallet.ts b/src/services/ecosystem/handlers/wallet.ts index 6a58b14eb..b7236dfe3 100644 --- a/src/services/ecosystem/handlers/wallet.ts +++ b/src/services/ecosystem/handlers/wallet.ts @@ -6,6 +6,7 @@ import type { MethodHandler, BioAccount } from '../types' import { BioErrorCodes } from '../types' import { HandlerContext } from './context' import { getChainProvider } from '@/services/chain-adapter/providers' +import { walletStore } from '@/stores/wallet' // 兼容旧 API,逐步迁移到 HandlerContext let _showWalletPicker: ((opts?: { chain?: string; exclude?: string }) => Promise) | null = null @@ -104,8 +105,7 @@ export const handlePickWallet: MethodHandler = async (params, context) => { /** bio_chainId - Get current chain ID */ export const handleChainId: MethodHandler = async (_params, _context) => { - // TODO: Get from current selected chain - return 'bfmeta' + return walletStore.state.selectedChain } /** bio_getBalance - Get balance */