Skip to content

Commit

Permalink
fix(dapp-connector): Prompt user only if window is focused and fix er…
Browse files Browse the repository at this point in the history
…ror handling (#3722)
  • Loading branch information
michaeljscript authored Nov 5, 2024
1 parent 2a659ff commit d79a936
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
1 change: 0 additions & 1 deletion apps/wallet-mobile/.storybook/storybook.requires.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export const WebViewItem = ({tab, index}: Props) => {
injectedJavaScriptBeforeContentLoaded={initScript}
onMessage={handleEvent}
style={[styles.roundedInsideContainer]}
allowsFullscreenVideo={isTabActive}
/>

{tabsOpen && (
Expand Down
34 changes: 27 additions & 7 deletions packages/dapp-connector/src/connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -103,6 +107,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}
Expand All @@ -114,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))
Expand All @@ -134,8 +153,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)
Expand Down Expand Up @@ -212,16 +230,18 @@ 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

if (message.toLowerCase().includes('user rejected')) {
logMessage('User Rejected')
return new CIP30Error('User Rejected', -3)
return getUserRejectedError()
}
logMessage('Error:' + error.message)
return new CIP30Error(error.message, -1)
logMessage('Error:' + message)
return new CIP30Error(message, -1)
}

const walletObj = Object.freeze({
Expand Down

0 comments on commit d79a936

Please sign in to comment.