Skip to content

Commit

Permalink
fix: hardware wallet dApp connector now uses the same window size as …
Browse files Browse the repository at this point in the history
…regular wallets (#1499)
  • Loading branch information
AngelCastilloB authored Oct 30, 2024
1 parent 6ba8bba commit 7f6f357
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { cip30 as walletCip30 } from '@cardano-sdk/wallet';
import { ensureUiIsOpenAndLoaded } from './util';
import { userPromptService } from './services/dappService';
import { authenticator } from './authenticator';
import { wallet$, walletManager, walletRepository } from './wallet';
import { wallet$ } from './wallet';
import { runtime, Tabs, tabs } from 'webextension-polyfill';
import { exposeApi, RemoteApiPropertyType, cip30 } from '@cardano-sdk/web-extension';
import { DAPP_CHANNELS } from '../../../utils/constants';
Expand Down Expand Up @@ -47,7 +47,7 @@ export const confirmationCallback: walletCip30.CallbackConfirmation = {
signTx: pDebounce(
async () => {
try {
const tab = await ensureUiIsOpenAndLoaded({ walletManager, walletRepository }, '#/dapp/sign-tx');
const tab = await ensureUiIsOpenAndLoaded('#/dapp/sign-tx');
const ready = await userPromptService.readyToSignTx();
if (!ready) return false;
return cancelOnTabClose(tab);
Expand All @@ -62,7 +62,7 @@ export const confirmationCallback: walletCip30.CallbackConfirmation = {
signData: pDebounce(
async () => {
try {
const tab = await ensureUiIsOpenAndLoaded({ walletManager, walletRepository }, '#/dapp/sign-data');
const tab = await ensureUiIsOpenAndLoaded('#/dapp/sign-data');
const ready = await userPromptService.readyToSignData();
if (!ready) return false;
return cancelOnTabClose(tab);
Expand All @@ -86,7 +86,7 @@ export const confirmationCallback: walletCip30.CallbackConfirmation = {

const dappInfo = await senderToDappInfo(args.sender);
dappSetCollateral$.next({ dappInfo, collateralRequest: args.data });
await ensureUiIsOpenAndLoaded({ walletManager, walletRepository }, '#/dapp/set-collateral');
await ensureUiIsOpenAndLoaded('#/dapp/set-collateral');

return userPromptService.getCollateralRequest();
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { authenticator } from './authenticator';
import { AUTHORIZED_DAPPS_KEY } from '../types';
import { Wallet } from '@lace/cardano';
import { BehaviorSubject } from 'rxjs';
import { walletManager, walletRepository } from './wallet';
import { senderToDappInfo } from '@src/utils/senderToDappInfo';

const DEBOUNCE_THROTTLE = 500;
Expand All @@ -19,7 +18,7 @@ export const dappInfo$ = new BehaviorSubject<Wallet.DappInfo>(undefined);
export const requestAccess: RequestAccess = async (sender: Runtime.MessageSender) => {
const { logo, name, url } = await senderToDappInfo(sender);
dappInfo$.next({ logo, name, url });
await ensureUiIsOpenAndLoaded({ walletManager, walletRepository }, '#/dapp/connect', false);
await ensureUiIsOpenAndLoaded('#/dapp/connect');
const isAllowed = await userPromptService.allowOrigin(url);
if (isAllowed === 'deny') return Promise.reject();
if (isAllowed === 'allow') {
Expand Down
37 changes: 8 additions & 29 deletions apps/browser-extension-wallet/src/lib/scripts/background/util.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
/* eslint-disable no-magic-numbers */
import {
HW_POPUP_WINDOW,
HW_POPUP_WINDOW_NAMI,
POPUP_WINDOW,
POPUP_WINDOW_NAMI,
POPUP_WINDOW_NAMI_TITLE
} from '@src/utils/constants';
import { POPUP_WINDOW, POPUP_WINDOW_NAMI, POPUP_WINDOW_NAMI_TITLE } from '@src/utils/constants';
import { runtime, Tabs, tabs, Windows, windows } from 'webextension-polyfill';
import { Wallet } from '@lace/cardano';
import { BackgroundStorage } from '../types';
Expand Down Expand Up @@ -76,23 +70,18 @@ const createWindow = (
/**
* launchCip30Popup
* @param url - Originating url of current dapp
* @param windowType 'normal' for hardware wallet interactions, 'popup' for everything else
* @returns tab - Tab of currently launched dApp connector
*/
export const launchCip30Popup = async (url: string, windowType: Windows.CreateType): Promise<Tabs.Tab> => {
export const launchCip30Popup = async (url: string): Promise<Tabs.Tab> => {
const currentWindow = await windows.getCurrent();
const tab = await createTab(`../dappConnector.html${url}`, false);
const { namiMigration } = await getBackgroundStorage();
let windowSize = windowType === 'popup' ? POPUP_WINDOW : HW_POPUP_WINDOW;

if (namiMigration?.mode === 'nami') {
windowSize = windowType === 'popup' ? POPUP_WINDOW_NAMI : HW_POPUP_WINDOW_NAMI;
}
const windowSize = namiMigration?.mode === 'nami' ? POPUP_WINDOW_NAMI : POPUP_WINDOW;

const newWindow = await createWindow(
tab.id,
calculatePopupWindowPositionAndSize(currentWindow, windowSize),
windowType,
'popup',
true
);
newWindow.alwaysOnTop = true;
Expand Down Expand Up @@ -145,25 +134,15 @@ export const closeAllLaceWindows = async (shouldRemoveTab?: (url: string) => boo
}
};

export const ensureUiIsOpenAndLoaded = async (
services: WalletManagementServices,
url?: string,
checkKeyAgent = true
): Promise<Tabs.Tab> => {
const isHardwareWallet = checkKeyAgent
? await (async () => {
const active = await getActiveWallet(services);
return active?.wallet.type === WalletType.Ledger || active?.wallet.type === WalletType.Trezor;
})()
: undefined;

const windowType: Windows.CreateType = isHardwareWallet ? 'normal' : 'popup';
export const ensureUiIsOpenAndLoaded = async (url?: string): Promise<Tabs.Tab> => {
await closeAllLaceWindows((tabUrl) => DAPP_CONNECTOR_REGEX.test(tabUrl));

const tab = await launchCip30Popup(url, windowType);
const tab = await launchCip30Popup(url);

if (tab.status !== 'complete') {
await waitForTabLoad(tab);
}

return tab;
};

Expand Down
10 changes: 0 additions & 10 deletions apps/browser-extension-wallet/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,6 @@ export const POPUP_WINDOW_NAMI = {

export const POPUP_WINDOW_NAMI_TITLE = 'Nami (Lace)';

export const HW_POPUP_WINDOW = {
width: 900,
height: 630
};

export const HW_POPUP_WINDOW_NAMI = {
width: 400,
height: 800
};

export const DAPP_CHANNELS = {
userPrompt: `user-prompt-${process.env.WALLET_NAME}`,
authenticator: `authenticator-${process.env.WALLET_NAME}`,
Expand Down

0 comments on commit 7f6f357

Please sign in to comment.