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: enhance modal session handling to reject on user cancellation #348

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
11 changes: 6 additions & 5 deletions demos/react-dapp/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ import {
verifySignerSignature,
transactionToTransactionBody,
SignTransactionParams,
base64StringToSignatureMap,
Uint8ArrayToBase64String,
tmctl marked this conversation as resolved.
Show resolved Hide resolved
extractFirstSignature,
} from '../../../dist'

Expand Down Expand Up @@ -210,7 +208,8 @@ const App: React.FC = () => {
message,
}

const { signatureMap } = await dAppConnector!.signMessage(params)
const signMessageResult = await dAppConnector!.signMessage(params)
const signatureMap = signMessageResult.result.signatureMap
tmctl marked this conversation as resolved.
Show resolved Hide resolved
const accountPublicKey = PublicKey.fromString(publicKey)
const verified = verifyMessageSignature(message, signatureMap, accountPublicKey)
console.log('SignatureMap: ', signatureMap)
Expand Down Expand Up @@ -261,7 +260,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
tmctl marked this conversation as resolved.
Show resolved Hide resolved
const bytes = Buffer.from(response, 'base64')
const accountInfo = AccountInfo.fromBytes(bytes)
console.log('AccountInfo: ', accountInfo)
Expand Down Expand Up @@ -586,7 +586,8 @@ const App: React.FC = () => {
let session: SessionTypes.Struct
setIsLoading(true)
if (extensionId) session = await dAppConnector.connectExtension(extensionId)
else session = await dAppConnector.openModal()
// Open modal with showErrorOnReject set to true
tmctl marked this conversation as resolved.
Show resolved Hide resolved
else session = await dAppConnector.openModal(undefined, true)

setNewSession(session)
} finally {
Expand Down
28 changes: 25 additions & 3 deletions src/lib/dapp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,36 @@ export class DAppConnector {
/**
* Initiates the WalletConnect connection flow using a QR code.
* @param pairingTopic - The pairing topic for the connection (optional).
* @param showErrorOnReject - Whether to show an error when the user rejects the pairing (default: false).
tmctl marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Promise<SessionTypes.Struct>} - A Promise that resolves when the connection process is complete.
*/
public async openModal(pairingTopic?: string): Promise<SessionTypes.Struct> {
public async openModal(
pairingTopic?: string,
showErrorOnReject: boolean = false,
tmctl marked this conversation as resolved.
Show resolved Hide resolved
): Promise<SessionTypes.Struct> {
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) => {
tmctl marked this conversation as resolved.
Show resolved Hide resolved
if (showErrorOnReject) {
tmctl marked this conversation as resolved.
Show resolved Hide resolved
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