From 31df0ac5d025a8a9bc7965fb4034e54ae03ff3d9 Mon Sep 17 00:00:00 2001 From: Gaubee Date: Thu, 26 Feb 2026 15:18:18 +0800 Subject: [PATCH] fix(biobridge): source external account from bio_selectAccount --- miniapps/biobridge/src/App.test.tsx | 37 ++++++++++++++++++++ miniapps/biobridge/src/App.tsx | 52 +++-------------------------- 2 files changed, 41 insertions(+), 48 deletions(-) diff --git a/miniapps/biobridge/src/App.test.tsx b/miniapps/biobridge/src/App.test.tsx index 8411a89a2..a14e2a891 100644 --- a/miniapps/biobridge/src/App.test.tsx +++ b/miniapps/biobridge/src/App.test.tsx @@ -178,4 +178,41 @@ describe('Forge App', () => { ) }) }) + + it('should use bio_selectAccount for external chain account', async () => { + mockBio.request.mockImplementation(({ method, params }: { method: string; params?: Array<{ chain?: string }> }) => { + if (method === 'bio_closeSplashScreen') return Promise.resolve(null) + if (method === 'bio_selectAccount') { + const chain = params?.[0]?.chain + if (chain === 'ethereum') { + return Promise.resolve({ address: '0xexternal-bio', chain: 'ethereum' }) + } + return Promise.resolve({ address: 'bfmeta123', chain: 'bfmeta' }) + } + return Promise.resolve(null) + }) + + render() + + await waitFor(() => { + expect(screen.getByTestId('connect-button')).toBeInTheDocument() + }) + + fireEvent.click(screen.getByTestId('connect-button')) + + await waitFor(() => { + expect(screen.getByText(/支付/)).toBeInTheDocument() + expect(screen.getByText('0xexternal-bio')).toBeInTheDocument() + }) + + expect(mockBio.request).toHaveBeenCalledWith( + expect.objectContaining({ + method: 'bio_selectAccount', + params: [{ chain: 'ethereum' }], + }) + ) + expect(mockEthereum.request).not.toHaveBeenCalledWith( + expect.objectContaining({ method: 'eth_requestAccounts' }) + ) + }) }) diff --git a/miniapps/biobridge/src/App.tsx b/miniapps/biobridge/src/App.tsx index a840e05bf..9854fa771 100644 --- a/miniapps/biobridge/src/App.tsx +++ b/miniapps/biobridge/src/App.tsx @@ -7,7 +7,6 @@ import { useState, useCallback, useEffect, useMemo } from 'react'; import { useTranslation } from 'react-i18next'; import type { BioAccount } from '@biochain/bio-sdk'; import { normalizeChainId } from '@biochain/bio-sdk'; -import { getChainType, getEvmChainIdFromApi } from '@/lib/chain'; import { parseAmount } from '@/lib/fee'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card'; @@ -170,53 +169,10 @@ export default function App() { setError(null); try { const externalChain = activeOption.externalChain; - const chainType = getChainType(externalChain); - - let extAcc: BioAccount; - - if (chainType === 'evm') { - if (!window.ethereum) { - throw new Error('Ethereum provider not available'); - } - const evmChainId = getEvmChainIdFromApi(externalChain); - if (evmChainId) { - await window.ethereum.request({ - method: 'wallet_switchEthereumChain', - params: [{ chainId: evmChainId }], - }); - } - const accounts = await window.ethereum.request({ - method: 'eth_requestAccounts', - }); - if (!accounts || accounts.length === 0) { - throw new Error('No accounts returned'); - } - extAcc = { - address: accounts[0], - chain: normalizeChainId(externalChain), - publicKey: '', - }; - } else if (chainType === 'tron') { - if (!window.tronLink) { - throw new Error('TronLink provider not available'); - } - const result = await window.tronLink.request<{ code: number; message: string; data: { base58: string } }>({ - method: 'tron_requestAccounts', - }); - if (!result || result.code !== 200) { - throw new Error('TRON connection failed'); - } - extAcc = { - address: result.data.base58, - chain: 'tron', - publicKey: '', - }; - } else { - extAcc = await window.bio.request({ - method: 'bio_selectAccount', - params: [{ chain: normalizeChainId(externalChain) }], - }); - } + const extAcc = await window.bio.request({ + method: 'bio_selectAccount', + params: [{ chain: normalizeChainId(externalChain) }], + }); setExternalAccount(extAcc); const intAcc = await window.bio.request({