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: close tab on declined grant #364

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Changes from 2 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
59 changes: 37 additions & 22 deletions src/background/services/openPayments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import * as ed from '@noble/ed25519'
import { type Request } from 'http-message-signatures'
import { signMessage } from 'http-message-signatures/lib/httpbis'
import { createContentDigestHeader } from 'httpbis-digest-headers'
import { Browser } from 'webextension-polyfill'
import { Browser, Tabs } from 'webextension-polyfill'
import {
getCurrentActiveTab,
getExchangeRates,
Expand Down Expand Up @@ -306,7 +306,7 @@ export class OpenPaymentsService {

// Q: Should this be moved to continuation polling?
// https://github.com/interledger/open-payments/issues/385
const { interactRef, hash } = await this.confirmPayment(
const { interactRef, hash } = await this.getInteractionInfo(
raducristianpopa marked this conversation as resolved.
Show resolved Hide resolved
grant.interact.redirect
)

Expand Down Expand Up @@ -424,31 +424,46 @@ export class OpenPaymentsService {
if (calculatedHash !== hash) throw new Error('Invalid interaction hash')
}

private async confirmPayment(url: string): Promise<InteractionParams> {
private async closeTab(currentTab: number, tabToClose: number) {
await this.browser.tabs.update(currentTab, { active: true })
await this.browser.tabs.remove(tabToClose)
}

private async getInteractionInfo(url: string): Promise<InteractionParams> {
const currentTab = await getCurrentActiveTab(this.browser)

return await new Promise((res) => {
if (url) {
this.browser.tabs.create({ url }).then((tab) => {
if (tab.id) {
this.browser.tabs.onUpdated.addListener((tabId, changeInfo) => {
try {
const tabUrl = new URL(changeInfo.url || '')
const interactRef = tabUrl.searchParams.get('interact_ref')
const hash = tabUrl.searchParams.get('hash')

if (tabId === tab.id && interactRef && hash) {
this.browser.tabs.update(currentTab.id, { active: true })
this.browser.tabs.remove(tab.id)
res({ interactRef, hash })
}
} catch (e) {
/* do nothing */
this.browser.tabs.create({ url }).then((tab) => {
if (tab.id) {
const getInteractionInfo: Parameters<
Tabs.onUpdatedEvent['addListener']
>[0] = async (tabId, changeInfo) => {
if (tabId !== tab.id) return
try {
raducristianpopa marked this conversation as resolved.
Show resolved Hide resolved
const tabUrl = new URL(changeInfo.url || '')
const interactRef = tabUrl.searchParams.get('interact_ref')
const hash = tabUrl.searchParams.get('hash')
const result = tabUrl.searchParams.get('result')

if (
(interactRef && hash) ||
result === 'grant_rejected' ||
result === 'grant_invalid'
) {
await this.closeTab(currentTab.id!, tabId)
this.browser.tabs.onUpdated.removeListener(getInteractionInfo)
}
})

if (interactRef && hash) {
res({ interactRef, hash })
raducristianpopa marked this conversation as resolved.
Show resolved Hide resolved
}
} catch (e) {
/* do nothing */
}
}
})
}
this.browser.tabs.onUpdated.addListener(getInteractionInfo)
}
})
})
}

Expand Down
Loading