Skip to content

Commit

Permalink
fix: Switch dApp connected account issues (#6229)
Browse files Browse the repository at this point in the history
* fix: Ton estimate fee params

* fix: Can not switch account issue

* fix: Should not switch for multiple chain

* fix: skip empty connectedInfo
  • Loading branch information
originalix authored Nov 19, 2024
1 parent e93ce6b commit 326ad38
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 64 deletions.
41 changes: 38 additions & 3 deletions packages/kit-bg/src/services/ServiceDApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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],
Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1388,6 +1419,10 @@ class ServiceDApp extends ServiceBase {
processing: false,
});
}, 200);
} else {
void this.setIsAlignPrimaryAccountProcessing({
processing: false,
});
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/kit-bg/src/vaults/impls/ton/Vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ export default class Vault extends VaultBase {
});
return {
encodedTx: {
...encodedTx,
body: Buffer.from(await serializeUnsignedTx.body.toBoc(false)).toString(
'base64',
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -277,6 +283,7 @@ export function useDappAccountSwitch({
};
void sync();
}, [
settings?.alignPrimaryAccountMode,
result?.origin,
result?.connectedAccountsInfo,
refreshConnectionInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 };
Expand Down Expand Up @@ -127,7 +99,6 @@ function SyncDappAccountToHomeCmp({
}
await syncDappAccountToWallet({
dAppAccountInfos,
forceSelectNetwork: true,
});
if (isFirstVisitRef.current) {
void tourVisited(1);
Expand Down

0 comments on commit 326ad38

Please sign in to comment.