diff --git a/packages/kit-bg/src/services/ServiceDApp.ts b/packages/kit-bg/src/services/ServiceDApp.ts index 0de824b7b99..589c9f52593 100644 --- a/packages/kit-bg/src/services/ServiceDApp.ts +++ b/packages/kit-bg/src/services/ServiceDApp.ts @@ -559,6 +559,10 @@ class ServiceDApp extends ServiceBase { isWalletConnectRequest, options, }); + const shouldAlignPrimaryAccount = await this.shouldAlignPrimaryAccount({ + origin, + storageType, + }); const allAccountsInfo = []; for (const networkImpl of networkImpls) { const accountsInfo = @@ -567,8 +571,8 @@ class ServiceDApp extends ServiceBase { storageType, networkImpl, ); - if (accountsInfo) { - if (accountsInfo.length === 1) { + if (Array.isArray(accountsInfo) && accountsInfo.length) { + if (shouldAlignPrimaryAccount) { const accountInfo = await this.alignPrimaryAccountToHomeAccount({ origin, connectedAccountInfo: accountsInfo[0], @@ -1218,7 +1222,7 @@ class ServiceDApp extends ServiceBase { return false; } const isOtherWallet = accountUtils.isOthersWallet({ - walletId: homeAccountSelectorInfo.walletId ?? '', + walletId: homeAccountSelectorInfo?.walletId ?? '', }); const isSameAccount = isOtherWallet ? connectedAccountInfo.othersWalletAccountId && @@ -1343,6 +1347,33 @@ class ServiceDApp extends ServiceBase { }, 20); } + @backgroundMethod() + async shouldAlignPrimaryAccount({ + origin, + storageType, + }: { + origin: string; + storageType: IConnectionStorageType; + }) { + if (storageType === 'walletConnect') { + return false; + } + + const currentSettings = await settingsPersistAtom.get(); + if ( + currentSettings.alignPrimaryAccountMode !== + EAlignPrimaryAccountMode.AlwaysUsePrimaryAccount + ) { + return false; + } + + const connectedAccounts = await this.findInjectedAccountByOrigin(origin); + if (Array.isArray(connectedAccounts) && connectedAccounts.length === 1) { + return true; + } + return false; + } + @backgroundMethod() async setIsAlignPrimaryAccountProcessing({ processing, @@ -1388,6 +1419,10 @@ class ServiceDApp extends ServiceBase { processing: false, }); }, 200); + } else { + void this.setIsAlignPrimaryAccountProcessing({ + processing: false, + }); } } diff --git a/packages/kit-bg/src/vaults/impls/ton/Vault.ts b/packages/kit-bg/src/vaults/impls/ton/Vault.ts index af4afeabd36..2fc1d2dafce 100644 --- a/packages/kit-bg/src/vaults/impls/ton/Vault.ts +++ b/packages/kit-bg/src/vaults/impls/ton/Vault.ts @@ -407,6 +407,7 @@ export default class Vault extends VaultBase { }); return { encodedTx: { + ...encodedTx, body: Buffer.from(await serializeUnsignedTx.body.toBoc(false)).toString( 'base64', ), diff --git a/packages/kit/src/views/DAppConnection/hooks/useDappAccountSwitch.ts b/packages/kit/src/views/DAppConnection/hooks/useDappAccountSwitch.ts index 1b688b25dbb..1fb024437c0 100644 --- a/packages/kit/src/views/DAppConnection/hooks/useDappAccountSwitch.ts +++ b/packages/kit/src/views/DAppConnection/hooks/useDappAccountSwitch.ts @@ -242,6 +242,12 @@ export function useDappAccountSwitch({ useEffect(() => { const sync = async () => { + if ( + settings?.alignPrimaryAccountMode !== + EAlignPrimaryAccountMode.AlwaysUsePrimaryAccount + ) { + return; + } if ( Array.isArray(result?.connectedAccountsInfo) && result?.connectedAccountsInfo?.length === 1 @@ -277,6 +283,7 @@ export function useDappAccountSwitch({ }; void sync(); }, [ + settings?.alignPrimaryAccountMode, result?.origin, result?.connectedAccountsInfo, refreshConnectionInfo, diff --git a/packages/kit/src/views/Discovery/components/SyncDappAccountToHomeProvider.tsx b/packages/kit/src/views/Discovery/components/SyncDappAccountToHomeProvider.tsx index 3a1ae72975e..be79c54c903 100644 --- a/packages/kit/src/views/Discovery/components/SyncDappAccountToHomeProvider.tsx +++ b/packages/kit/src/views/Discovery/components/SyncDappAccountToHomeProvider.tsx @@ -5,7 +5,6 @@ import { AccountSelectorProviderMirror } from '@onekeyhq/kit/src/components/Acco import { useAccountSelectorActions, useAccountSelectorContextDataAtom, - useActiveAccount, } from '@onekeyhq/kit/src/states/jotai/contexts/accountSelector'; import type { IAccountSelectorSelectedAccount } from '@onekeyhq/kit-bg/src/dbs/simple/entity/SimpleDbEntityAccountSelector'; import { useSettingsPersistAtom } from '@onekeyhq/kit-bg/src/states/jotai/atoms'; @@ -24,75 +23,48 @@ import { useSpotlight } from '../../../components/Spotlight'; export function useSyncDappAccountToHomeAccount() { const actions = useAccountSelectorActions(); - const { - activeAccount: { account: homeAccount }, - } = useActiveAccount({ num: 0 }); - const syncDappAccountToWallet = useCallback( async ({ dAppAccountInfos, - forceSelectNetwork, }: { dAppAccountInfos: IConnectionAccountInfo[] | null; - forceSelectNetwork: boolean; }) => { - try { - if (!Array.isArray(dAppAccountInfos) || dAppAccountInfos.length !== 1) { - return; - } - - const { serviceAccount } = backgroundApiProxy; - const dAppAccount = dAppAccountInfos[0]; - const { indexedAccountId, accountId, networkId } = dAppAccount; - const account = await serviceAccount.getAccount({ - accountId, - networkId: networkId ?? '', + if (!Array.isArray(dAppAccountInfos) || dAppAccountInfos.length !== 1) { + return; + } + + const { serviceAccount } = backgroundApiProxy; + const dAppAccount = dAppAccountInfos[0]; + const { indexedAccountId, accountId, networkId } = dAppAccount; + const account = await serviceAccount.getAccount({ + accountId, + networkId: networkId ?? '', + }); + const isOtherWallet = accountUtils.isOthersAccount({ + accountId, + }); + + if (isOtherWallet) { + await actions.current.confirmAccountSelect({ + num: 0, + indexedAccount: undefined, + othersWalletAccount: account, + autoChangeToAccountMatchedNetworkId: networkId, }); - const isOtherWallet = accountUtils.isOthersAccount({ - accountId, + } else { + const indexedAccount = await serviceAccount.getIndexedAccount({ + id: indexedAccountId ?? '', + }); + await actions.current.confirmAccountSelect({ + num: 0, + indexedAccount, + othersWalletAccount: undefined, + autoChangeToAccountMatchedNetworkId: undefined, + forceSelectToNetworkId: networkId, }); - - if (isOtherWallet) { - const isCompatibleNetwork = - accountUtils.isAccountCompatibleWithNetwork({ - // @ts-expect-error - account: homeAccount, - networkId: networkId ?? '', - }); - let autoChangeToAccountMatchedNetwork = forceSelectNetwork; - if (!forceSelectNetwork && !isCompatibleNetwork) { - autoChangeToAccountMatchedNetwork = false; - } - await actions.current.confirmAccountSelect({ - num: 0, - indexedAccount: undefined, - othersWalletAccount: account, - autoChangeToAccountMatchedNetworkId: - autoChangeToAccountMatchedNetwork ? networkId : undefined, - }); - } else { - const indexedAccount = await serviceAccount.getIndexedAccount({ - id: indexedAccountId ?? '', - }); - await actions.current.confirmAccountSelect({ - num: 0, - indexedAccount, - othersWalletAccount: undefined, - autoChangeToAccountMatchedNetworkId: undefined, - forceSelectToNetworkId: forceSelectNetwork ? networkId : undefined, - }); - } - } finally { - setTimeout(() => { - void backgroundApiProxy.serviceDApp.setIsAlignPrimaryAccountProcessing( - { - processing: false, - }, - ); - }, 20); } }, - [actions, homeAccount], + [actions], ); return { syncDappAccountToWallet }; @@ -127,7 +99,6 @@ function SyncDappAccountToHomeCmp({ } await syncDappAccountToWallet({ dAppAccountInfos, - forceSelectNetwork: true, }); if (isFirstVisitRef.current) { void tourVisited(1);