From 98a1a9af55d27e02634bacf32ec38d3425e0b8a2 Mon Sep 17 00:00:00 2001 From: Nikita Date: Mon, 24 Feb 2025 12:41:38 +0400 Subject: [PATCH] fix: be able to connect if permission set and wallet is locked --- src/composables/aeSdk.ts | 6 ---- src/composables/permissions.ts | 17 ++++++++-- .../libs/EthereumRpcMethodsHandler.ts | 33 ++++--------------- 3 files changed, 21 insertions(+), 35 deletions(-) diff --git a/src/composables/aeSdk.ts b/src/composables/aeSdk.ts index 9746e294d..5145c6956 100644 --- a/src/composables/aeSdk.ts +++ b/src/composables/aeSdk.ts @@ -12,7 +12,6 @@ import { Encoded, } from '@aeternity/aepp-sdk'; import { WalletApi } from '@aeternity/aepp-sdk/es/aepp-wallet-communication/rpc/types'; -import { isEmpty } from 'lodash-es'; import type { INetwork, @@ -80,7 +79,6 @@ export function useAeSdk() { onNetworkChange, } = useNetworks(); const { - activeAccount, accountsAddressList, getLastActiveProtocolAccount, onAccountChange, @@ -154,8 +152,6 @@ export function useAeSdk() { const aepp = aeppInfo[aeppId]; const host = IS_OFFSCREEN_TAB ? aepp.origin : origin; if (await checkOrAskPermission(METHODS.subscribeAddress, host)) { - // Waiting for activeAccount to sync back to the background - await watchUntilTruthy(() => !isEmpty(activeAccount.value)); return getLastActiveProtocolAccount(PROTOCOLS.aeternity)!.address; } return Promise.reject(new RpcRejectedByUserError()); @@ -164,8 +160,6 @@ export function useAeSdk() { const aepp = aeppInfo[aeppId]; const host = IS_OFFSCREEN_TAB ? aepp.origin : origin; if (await checkOrAskPermission(METHODS.address, host)) { - // Waiting for activeAccount to sync back to the background - await watchUntilTruthy(() => !isEmpty(activeAccount.value)); return accountsAddressList.value; } return Promise.reject(new RpcRejectedByUserError()); diff --git a/src/composables/permissions.ts b/src/composables/permissions.ts index 6c0be9839..5b3c21f8c 100644 --- a/src/composables/permissions.ts +++ b/src/composables/permissions.ts @@ -1,4 +1,5 @@ import { METHODS } from '@aeternity/aepp-sdk'; +import { isEmpty } from 'lodash-es'; import type { IAppData, @@ -24,10 +25,11 @@ import { STORAGE_KEYS, PROTOCOLS, } from '@/constants'; -import { getCleanModalOptions } from '@/utils'; +import { getCleanModalOptions, watchUntilTruthy } from '@/utils'; import { aettosToAe, isTxOfASupportedType } from '@/protocols/aeternity/helpers'; import { openPopup } from '@/offscreen/popupHandler'; import migratePermissionsVuexToComposable from '@/migrations/003-permissions-vuex-to-composable'; +import { useAccounts } from '@/composables'; import { useStorageRef } from './storageRef'; import { useModals } from './modals'; @@ -164,11 +166,22 @@ export function usePermissions() { ): Promise { let app: IAppData | undefined; let props = getCleanModalOptions(modalProps); + const { activeAccount } = useAccounts(); if (fullUrl) { const url = new URL(fullUrl); if (checkPermission(url.host, method, modalProps.tx)) { - return true; + try { + await Promise.race( + [ + watchUntilTruthy(() => !isEmpty(activeAccount.value)), + new Promise((_r, reject) => setTimeout(reject, 1000)), + ], + ); + return true; + } catch (error) { + // Intentionally ignoring the error + } } app = { diff --git a/src/protocols/ethereum/libs/EthereumRpcMethodsHandler.ts b/src/protocols/ethereum/libs/EthereumRpcMethodsHandler.ts index 41affcc1b..c85cd9a94 100644 --- a/src/protocols/ethereum/libs/EthereumRpcMethodsHandler.ts +++ b/src/protocols/ethereum/libs/EthereumRpcMethodsHandler.ts @@ -8,7 +8,7 @@ import { isEmpty } from 'lodash-es'; import type { IModalProps } from '@/types'; import type { IEthRpcMethodParameters, EthRpcSupportedMethods } from '@/protocols/ethereum/types'; -import { sleep, watchUntilTruthy } from '@/utils'; +import { watchUntilTruthy } from '@/utils'; import { ProtocolAdapterFactory } from '@/lib/ProtocolAdapterFactory'; import { EtherscanService, EtherscanDefaultResponse } from '@/protocols/ethereum/libs/EtherscanService'; import { useEthNetworkSettings } from '@/protocols/ethereum/composables/ethNetworkSettings'; @@ -22,7 +22,6 @@ import { import { CONNECT_PERMISSIONS, - PERMISSION_DEFAULTS, PROTOCOLS, } from '@/constants'; import { @@ -46,12 +45,8 @@ const ERROR_USER_REJECTED_REQUEST = { const isCheckingPermissions = ref(false); async function checkOrAskEthPermission(aepp: string) { - const { - addPermission, - checkOrAskPermission, - permissions, - } = usePermissions(); - + const { checkOrAskPermission } = usePermissions(); + const { activeAccount } = useAccounts(); await watchUntilTruthy(() => !isCheckingPermissions.value); isCheckingPermissions.value = true; @@ -67,20 +62,7 @@ async function checkOrAskEthPermission(aepp: string) { ], }, ); - const { hostname: host } = new URL(aepp); - if (permission && !permissions.value[host]?.address) { - // awaiting for default permissions to be synced - // with background after being set in `checkOrAskPermission` - await sleep(50); - addPermission({ - ...PERMISSION_DEFAULTS, - ...(permissions.value[host] || {}), - address: true, - addressList: true, - host, - name: host, - }); - } + await watchUntilTruthy(() => !isEmpty(activeAccount.value)); isCheckingPermissions.value = false; return permission; @@ -93,7 +75,7 @@ export async function handleEthereumRpcMethod( name?: string, ): Promise<{ result?: any; error?: { code: number; message: string } }> { const { checkPermission, checkOrAskPermission, removePermission } = usePermissions(); - const { activeAccount, getLastActiveProtocolAccount } = useAccounts(); + const { getLastActiveProtocolAccount } = useAccounts(); const { activeNetwork, networks, switchNetwork } = useNetworks(); const { ethActiveNetworkSettings, ethActiveNetworkPredefinedSettings } = useEthNetworkSettings(); @@ -107,7 +89,6 @@ export async function handleEthereumRpcMethod( const { host } = new URL(aepp); if (checkPermission(host, METHODS.address)) { - await watchUntilTruthy(() => !isEmpty(activeAccount.value)); const ethereumAccount = getLastActiveProtocolAccount(PROTOCOLS.ethereum); return { @@ -120,9 +101,7 @@ export async function handleEthereumRpcMethod( } if (method === ETH_RPC_METHODS.requestAccounts) { - const permitted = await checkOrAskEthPermission(aepp); - if (permitted) { - await watchUntilTruthy(() => !isEmpty(activeAccount.value)); + if (await checkOrAskEthPermission(aepp)) { return { result: [getLastActiveProtocolAccount(PROTOCOLS.ethereum)!.address] }; } return ERROR_USER_REJECTED_REQUEST;