Skip to content

Commit eca375a

Browse files
authored
fix(biobridge): source external account from bio_selectAccount (#481)
1 parent 81ad78f commit eca375a

2 files changed

Lines changed: 41 additions & 48 deletions

File tree

‎miniapps/biobridge/src/App.test.tsx‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,41 @@ describe('Forge App', () => {
178178
)
179179
})
180180
})
181+
182+
it('should use bio_selectAccount for external chain account', async () => {
183+
mockBio.request.mockImplementation(({ method, params }: { method: string; params?: Array<{ chain?: string }> }) => {
184+
if (method === 'bio_closeSplashScreen') return Promise.resolve(null)
185+
if (method === 'bio_selectAccount') {
186+
const chain = params?.[0]?.chain
187+
if (chain === 'ethereum') {
188+
return Promise.resolve({ address: '0xexternal-bio', chain: 'ethereum' })
189+
}
190+
return Promise.resolve({ address: 'bfmeta123', chain: 'bfmeta' })
191+
}
192+
return Promise.resolve(null)
193+
})
194+
195+
render(<App />)
196+
197+
await waitFor(() => {
198+
expect(screen.getByTestId('connect-button')).toBeInTheDocument()
199+
})
200+
201+
fireEvent.click(screen.getByTestId('connect-button'))
202+
203+
await waitFor(() => {
204+
expect(screen.getByText(/支付/)).toBeInTheDocument()
205+
expect(screen.getByText('0xexternal-bio')).toBeInTheDocument()
206+
})
207+
208+
expect(mockBio.request).toHaveBeenCalledWith(
209+
expect.objectContaining({
210+
method: 'bio_selectAccount',
211+
params: [{ chain: 'ethereum' }],
212+
})
213+
)
214+
expect(mockEthereum.request).not.toHaveBeenCalledWith(
215+
expect.objectContaining({ method: 'eth_requestAccounts' })
216+
)
217+
})
181218
})

‎miniapps/biobridge/src/App.tsx‎

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { useState, useCallback, useEffect, useMemo } from 'react';
77
import { useTranslation } from 'react-i18next';
88
import type { BioAccount } from '@biochain/bio-sdk';
99
import { normalizeChainId } from '@biochain/bio-sdk';
10-
import { getChainType, getEvmChainIdFromApi } from '@/lib/chain';
1110
import { parseAmount } from '@/lib/fee';
1211
import { Button } from '@/components/ui/button';
1312
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card';
@@ -170,53 +169,10 @@ export default function App() {
170169
setError(null);
171170
try {
172171
const externalChain = activeOption.externalChain;
173-
const chainType = getChainType(externalChain);
174-
175-
let extAcc: BioAccount;
176-
177-
if (chainType === 'evm') {
178-
if (!window.ethereum) {
179-
throw new Error('Ethereum provider not available');
180-
}
181-
const evmChainId = getEvmChainIdFromApi(externalChain);
182-
if (evmChainId) {
183-
await window.ethereum.request({
184-
method: 'wallet_switchEthereumChain',
185-
params: [{ chainId: evmChainId }],
186-
});
187-
}
188-
const accounts = await window.ethereum.request<string[]>({
189-
method: 'eth_requestAccounts',
190-
});
191-
if (!accounts || accounts.length === 0) {
192-
throw new Error('No accounts returned');
193-
}
194-
extAcc = {
195-
address: accounts[0],
196-
chain: normalizeChainId(externalChain),
197-
publicKey: '',
198-
};
199-
} else if (chainType === 'tron') {
200-
if (!window.tronLink) {
201-
throw new Error('TronLink provider not available');
202-
}
203-
const result = await window.tronLink.request<{ code: number; message: string; data: { base58: string } }>({
204-
method: 'tron_requestAccounts',
205-
});
206-
if (!result || result.code !== 200) {
207-
throw new Error('TRON connection failed');
208-
}
209-
extAcc = {
210-
address: result.data.base58,
211-
chain: 'tron',
212-
publicKey: '',
213-
};
214-
} else {
215-
extAcc = await window.bio.request<BioAccount>({
216-
method: 'bio_selectAccount',
217-
params: [{ chain: normalizeChainId(externalChain) }],
218-
});
219-
}
172+
const extAcc = await window.bio.request<BioAccount>({
173+
method: 'bio_selectAccount',
174+
params: [{ chain: normalizeChainId(externalChain) }],
175+
});
220176
setExternalAccount(extAcc);
221177

222178
const intAcc = await window.bio.request<BioAccount>({

0 commit comments

Comments
 (0)