Skip to content

Commit

Permalink
fix: enhance modal session handling to reject on user cancellation
Browse files Browse the repository at this point in the history
Signed-off-by: Mohamed Arbani <[email protected]>
  • Loading branch information
MohamedArbani committed Nov 21, 2024
1 parent e10fa05 commit dea126a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
8 changes: 5 additions & 3 deletions demos/react-dapp/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
transactionToBase64String,
SignAndExecuteQueryParams,
ExecuteTransactionParams,
} from '../../../dist/src/index'
} from '../../../dist/index'

import React, { useEffect, useMemo, useState } from 'react'
import Modal from './components/Modal'
Expand Down Expand Up @@ -171,7 +171,8 @@ const App: React.FC = () => {
message,
}

const { signatureMap } = await dAppConnector!.signMessage(params)
const signMessageResult = await dAppConnector!.signMessage(params)
const signatureMap = signMessageResult.result.signatureMap
const accountPublicKey = PublicKey.fromString(publicKey)
const verified = verifyMessageSignature(message, signatureMap, accountPublicKey)
console.log('SignatureMap: ', signatureMap)
Expand All @@ -195,7 +196,8 @@ const App: React.FC = () => {
query: queryToBase64String(query),
}

const { response } = await dAppConnector!.signAndExecuteQuery(params)
const execution = await dAppConnector!.signAndExecuteQuery(params)
const response = execution.result.response
const bytes = Buffer.from(response, 'base64')
const accountInfo = AccountInfo.fromBytes(bytes)
console.log('AccountInfo: ', accountInfo)
Expand Down
20 changes: 18 additions & 2 deletions src/lib/dapp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,24 @@ export class DAppConnector {
try {
const { uri, approval } = await this.connectURI(pairingTopic)
this.walletConnectModal.openModal({ uri })
const session = await approval()
await this.onSessionConnected(session)

const session = await new Promise<SessionTypes.Struct>(async (resolve, reject) => {
this.walletConnectModal.subscribeModal((state: { open: boolean }) => {
// the modal was closed so reject the promise
if (!state.open) {
reject(new Error('User rejected pairing'))
}
})

try {
const approvedSession = await approval()
await this.onSessionConnected(approvedSession)
resolve(approvedSession)
} catch (error) {
reject(error)
}
})

return session
} finally {
this.walletConnectModal.closeModal()
Expand Down

0 comments on commit dea126a

Please sign in to comment.