Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dapp-connector): Prompt user only if window is focused and fix error handling #3722

Merged
merged 7 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading