Skip to content

Commit

Permalink
fix: close tab on declined grant (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
raducristianpopa committed Jun 25, 2024
1 parent 0e85840 commit 64ae7f9
Showing 1 changed file with 40 additions and 23 deletions.
63 changes: 40 additions & 23 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 @@ -91,6 +91,8 @@ interface CreateOutgoingPaymentParams {
quoteId: string
}

type TabUpdateCallback = Parameters<Tabs.onUpdatedEvent['addListener']>[0]

export class OpenPaymentsService {
client?: AuthenticatedClient

Expand Down Expand Up @@ -306,7 +308,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(
grant.interact.redirect
)

Expand Down Expand Up @@ -424,31 +426,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) return
const getInteractionInfo: TabUpdateCallback = async (
tabId,
changeInfo
) => {
if (tabId !== tab.id) return
try {
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 })
}
} catch (e) {
/* do nothing */
}
})
}
}
this.browser.tabs.onUpdated.addListener(getInteractionInfo)
})
})
}

Expand Down

0 comments on commit 64ae7f9

Please sign in to comment.