Skip to content

Commit

Permalink
fix: be able to connect if permission set and wallet is locked
Browse files Browse the repository at this point in the history
  • Loading branch information
CedrikNikita committed Feb 24, 2025
1 parent afe0c66 commit 98a1a9a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 35 deletions.
6 changes: 0 additions & 6 deletions src/composables/aeSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -80,7 +79,6 @@ export function useAeSdk() {
onNetworkChange,
} = useNetworks();
const {
activeAccount,
accountsAddressList,
getLastActiveProtocolAccount,
onAccountChange,
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand Down
17 changes: 15 additions & 2 deletions src/composables/permissions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { METHODS } from '@aeternity/aepp-sdk';
import { isEmpty } from 'lodash-es';

import type {
IAppData,
Expand All @@ -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';

Expand Down Expand Up @@ -164,11 +166,22 @@ export function usePermissions() {
): Promise<boolean> {
let app: IAppData | undefined;
let props = getCleanModalOptions<typeof modalProps>(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 = {
Expand Down
33 changes: 6 additions & 27 deletions src/protocols/ethereum/libs/EthereumRpcMethodsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -22,7 +22,6 @@ import {

import {
CONNECT_PERMISSIONS,
PERMISSION_DEFAULTS,
PROTOCOLS,
} from '@/constants';
import {
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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();

Expand All @@ -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 {
Expand All @@ -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;
Expand Down

0 comments on commit 98a1a9a

Please sign in to comment.