From 2b0a1f533956d97cb92e3c76ce7c11b2d8ff8299 Mon Sep 17 00:00:00 2001 From: Michal Date: Mon, 28 Oct 2024 10:49:42 +0000 Subject: [PATCH 1/6] fix(dapp-connector): Do not prompt the user when the screen is not fused --- packages/dapp-connector/src/connector.js | 26 ++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/packages/dapp-connector/src/connector.js b/packages/dapp-connector/src/connector.js index 333dab0c8d..d044c38368 100644 --- a/packages/dapp-connector/src/connector.js +++ b/packages/dapp-connector/src/connector.js @@ -103,6 +103,19 @@ const initWallet = ({iconUrl, apiVersion, walletName, supportedExtensions, sessi logMessage('Unhandled rejection:' + serializeError(event.reason)) }) + const isWindowVisible = () => { + return document.visibilityState === 'visible' + } + + const getUserRejectedError = () => { + return new CIP30Error('User Rejected', -3) + } + + const throwUserRejectedError = () => { + logMessage('User Rejected') + throw getUserRejectedError() + } + /** * @param {Error | Object} error * @returns {string} @@ -134,8 +147,7 @@ const initWallet = ({iconUrl, apiVersion, walletName, supportedExtensions, sessi */ const createApi = async (cardanoEnableResponse) => { if (!cardanoEnableResponse) { - logMessage('User Rejected') - throw new CIP30Error('User Rejected', -3) + return throwUserRejectedError() } localStorage.setItem('yoroi-session-id', sessionId) @@ -148,19 +160,21 @@ const initWallet = ({iconUrl, apiVersion, walletName, supportedExtensions, sessi getExtensions: (...args) => callExternalMethod('api.getExtensions', args), getNetworkId: (...args) => callExternalMethod('api.getNetworkId', args), getUtxos: (...args) => callExternalMethod('api.getUtxos', args), - getCollateral: (...args) => callExternalMethod('api.getCollateral', args), + getCollateral: (...args) => + isWindowVisible() ? callExternalMethod('api.getCollateral', args) : throwUserRejectedError(), getBalance: (...args) => callExternalMethod('api.getBalance', args), getUsedAddresses: (...args) => callExternalMethod('api.getUsedAddresses', args), getUnusedAddresses: (...args) => callExternalMethod('api.getUnusedAddresses', args), getChangeAddress: (...args) => callExternalMethod('api.getChangeAddress', args), getRewardAddresses: (...args) => callExternalMethod('api.getRewardAddresses', args), - signTx: (...args) => callExternalMethod('api.signTx', args), - signData: (...args) => callExternalMethod('api.signData', args), + signTx: (...args) => (isWindowVisible() ? callExternalMethod('api.signTx', args) : throwUserRejectedError()), + signData: (...args) => (isWindowVisible() ? callExternalMethod('api.signData', args) : throwUserRejectedError()), submitTx: (...args) => callExternalMethod('api.submitTx', args), experimental: {on: () => {}}, cip95: supportsCIP95 ? { - signData: (...args) => callExternalMethod('api.cip95.signData', args), + signData: (...args) => + isWindowVisible() ? callExternalMethod('api.cip95.signData', args) : throwUserRejectedError(), getPubDRepKey: (...args) => callExternalMethod('api.cip95.getPubDRepKey', args), getRegisteredPubStakeKeys: (...args) => callExternalMethod('api.cip95.getRegisteredPubStakeKeys', args), getUnregisteredPubStakeKeys: (...args) => callExternalMethod('api.cip95.getUnregisteredPubStakeKeys', args), From fac5b0ecfc7e8abdcdc52d7f929b56e7fa7109aa Mon Sep 17 00:00:00 2001 From: Michal Date: Tue, 5 Nov 2024 09:23:41 +0000 Subject: [PATCH 2/6] Refactor --- packages/dapp-connector/src/connector.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/dapp-connector/src/connector.js b/packages/dapp-connector/src/connector.js index d044c38368..88a26610a4 100644 --- a/packages/dapp-connector/src/connector.js +++ b/packages/dapp-connector/src/connector.js @@ -79,6 +79,10 @@ const initWallet = ({iconUrl, apiVersion, walletName, supportedExtensions, sessi const callExternalMethod = (method, args = undefined, options = {}) => { const requestId = getRandomId() + if (apisWhichRequireWindowFocus.includes(method) && !isWindowVisible()) { + return new Promise(() => throwUserRejectedError()) + } + if (options?.doNotWaitForResponse) { postMessage({id: requestId, method, source: 'dapp-connector', params: {args, browserContext: getContext()}}) return Promise.resolve() @@ -127,6 +131,8 @@ const initWallet = ({iconUrl, apiVersion, walletName, supportedExtensions, sessi return JSON.stringify(error) } + const apisWhichRequireWindowFocus = ['api.getCollateral', 'api.signTx', 'api.signData', 'api.cip95.signData'] + window.addEventListener('message', (event) => { if (!event.data || typeof event.data.id !== 'string') return logMessage('Received message ' + JSON.stringify(event.data)) @@ -160,21 +166,19 @@ const initWallet = ({iconUrl, apiVersion, walletName, supportedExtensions, sessi getExtensions: (...args) => callExternalMethod('api.getExtensions', args), getNetworkId: (...args) => callExternalMethod('api.getNetworkId', args), getUtxos: (...args) => callExternalMethod('api.getUtxos', args), - getCollateral: (...args) => - isWindowVisible() ? callExternalMethod('api.getCollateral', args) : throwUserRejectedError(), + getCollateral: (...args) => callExternalMethod('api.getCollateral', args), getBalance: (...args) => callExternalMethod('api.getBalance', args), getUsedAddresses: (...args) => callExternalMethod('api.getUsedAddresses', args), getUnusedAddresses: (...args) => callExternalMethod('api.getUnusedAddresses', args), getChangeAddress: (...args) => callExternalMethod('api.getChangeAddress', args), getRewardAddresses: (...args) => callExternalMethod('api.getRewardAddresses', args), - signTx: (...args) => (isWindowVisible() ? callExternalMethod('api.signTx', args) : throwUserRejectedError()), - signData: (...args) => (isWindowVisible() ? callExternalMethod('api.signData', args) : throwUserRejectedError()), + signTx: (...args) => callExternalMethod('api.signTx', args), + signData: (...args) => callExternalMethod('api.signData', args), submitTx: (...args) => callExternalMethod('api.submitTx', args), experimental: {on: () => {}}, cip95: supportsCIP95 ? { - signData: (...args) => - isWindowVisible() ? callExternalMethod('api.cip95.signData', args) : throwUserRejectedError(), + signData: (...args) => callExternalMethod('api.cip95.signData', args), getPubDRepKey: (...args) => callExternalMethod('api.cip95.getPubDRepKey', args), getRegisteredPubStakeKeys: (...args) => callExternalMethod('api.cip95.getRegisteredPubStakeKeys', args), getUnregisteredPubStakeKeys: (...args) => callExternalMethod('api.cip95.getUnregisteredPubStakeKeys', args), From 201f16a19d4eeeaefa6e70afb4c620648256e541 Mon Sep 17 00:00:00 2001 From: Michal Date: Tue, 5 Nov 2024 09:39:49 +0000 Subject: [PATCH 3/6] Add full screen check --- apps/wallet-mobile/.storybook/storybook.requires.js | 1 - .../src/features/Discover/useCases/BrowseDapp/WebViewItem.tsx | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/wallet-mobile/.storybook/storybook.requires.js b/apps/wallet-mobile/.storybook/storybook.requires.js index 25d1517202..6f266bf904 100644 --- a/apps/wallet-mobile/.storybook/storybook.requires.js +++ b/apps/wallet-mobile/.storybook/storybook.requires.js @@ -118,7 +118,6 @@ const getStories = () => { "./src/features/Discover/useCases/BrowseDapp/BrowserTabsBar.stories.tsx": require("../src/features/Discover/useCases/BrowseDapp/BrowserTabsBar.stories.tsx"), "./src/features/Discover/useCases/BrowseDapp/BrowserToolbar.stories.tsx": require("../src/features/Discover/useCases/BrowseDapp/BrowserToolbar.stories.tsx"), "./src/features/Discover/useCases/BrowseDapp/WebViewItem.stories.tsx": require("../src/features/Discover/useCases/BrowseDapp/WebViewItem.stories.tsx"), - "./src/features/Discover/useCases/ReviewTransaction/ReviewTransaction.stories.tsx": require("../src/features/Discover/useCases/ReviewTransaction/ReviewTransaction.stories.tsx"), "./src/features/Discover/useCases/SearchDappInBrowser/SearchDappInBrowserScreen.stories.tsx": require("../src/features/Discover/useCases/SearchDappInBrowser/SearchDappInBrowserScreen.stories.tsx"), "./src/features/Discover/useCases/SelectDappFromList/CountDAppsAvailable/CountDAppsAvailable.stories.tsx": require("../src/features/Discover/useCases/SelectDappFromList/CountDAppsAvailable/CountDAppsAvailable.stories.tsx"), "./src/features/Discover/useCases/SelectDappFromList/CountDAppsConnected/CountDAppsConnected.stories.tsx": require("../src/features/Discover/useCases/SelectDappFromList/CountDAppsConnected/CountDAppsConnected.stories.tsx"), diff --git a/apps/wallet-mobile/src/features/Discover/useCases/BrowseDapp/WebViewItem.tsx b/apps/wallet-mobile/src/features/Discover/useCases/BrowseDapp/WebViewItem.tsx index 7060a8acf9..70b3988401 100644 --- a/apps/wallet-mobile/src/features/Discover/useCases/BrowseDapp/WebViewItem.tsx +++ b/apps/wallet-mobile/src/features/Discover/useCases/BrowseDapp/WebViewItem.tsx @@ -125,6 +125,7 @@ export const WebViewItem = ({tab, index}: Props) => { injectedJavaScriptBeforeContentLoaded={initScript} onMessage={handleEvent} style={[styles.roundedInsideContainer]} + allowsFullscreenVideo={isTabActive} /> {tabsOpen && ( From 666a457683a6057dba971e66165b5d477b9dfa4f Mon Sep 17 00:00:00 2001 From: Michal Date: Tue, 5 Nov 2024 10:44:44 +0000 Subject: [PATCH 4/6] Fix error handling --- packages/dapp-connector/src/connector.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/dapp-connector/src/connector.js b/packages/dapp-connector/src/connector.js index 88a26610a4..304856b7f1 100644 --- a/packages/dapp-connector/src/connector.js +++ b/packages/dapp-connector/src/connector.js @@ -230,16 +230,19 @@ const initWallet = ({iconUrl, apiVersion, walletName, supportedExtensions, sessi } /** - * @param {Error} error + * @param {Error | String} error * @returns {CIP30Error} */ const normalizeError = (error) => { - if (error.message.toLowerCase().includes('user rejected')) { + const message = typeof error === 'string' ? error : error.message + + logMessage(`normalize error: ${message}`) + if (message.toLowerCase().includes('user rejected')) { logMessage('User Rejected') return new CIP30Error('User Rejected', -3) } - logMessage('Error:' + error.message) - return new CIP30Error(error.message, -1) + logMessage('Error:' + message) + return new CIP30Error(message, -1) } const walletObj = Object.freeze({ From e3288b878ca8eb48f38870af81361d6d56ae8b08 Mon Sep 17 00:00:00 2001 From: Michal Date: Tue, 5 Nov 2024 10:45:20 +0000 Subject: [PATCH 5/6] Update connector.js --- packages/dapp-connector/src/connector.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/dapp-connector/src/connector.js b/packages/dapp-connector/src/connector.js index 304856b7f1..0c558fc13a 100644 --- a/packages/dapp-connector/src/connector.js +++ b/packages/dapp-connector/src/connector.js @@ -236,7 +236,6 @@ const initWallet = ({iconUrl, apiVersion, walletName, supportedExtensions, sessi const normalizeError = (error) => { const message = typeof error === 'string' ? error : error.message - logMessage(`normalize error: ${message}`) if (message.toLowerCase().includes('user rejected')) { logMessage('User Rejected') return new CIP30Error('User Rejected', -3) From d785b352c6fff731dd1e7767be09969a4fd39ffc Mon Sep 17 00:00:00 2001 From: Michal Date: Tue, 5 Nov 2024 10:46:02 +0000 Subject: [PATCH 6/6] Update connector.js --- packages/dapp-connector/src/connector.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dapp-connector/src/connector.js b/packages/dapp-connector/src/connector.js index 0c558fc13a..cd74bc19ac 100644 --- a/packages/dapp-connector/src/connector.js +++ b/packages/dapp-connector/src/connector.js @@ -238,7 +238,7 @@ const initWallet = ({iconUrl, apiVersion, walletName, supportedExtensions, sessi if (message.toLowerCase().includes('user rejected')) { logMessage('User Rejected') - return new CIP30Error('User Rejected', -3) + return getUserRejectedError() } logMessage('Error:' + message) return new CIP30Error(message, -1)