Skip to content

Commit

Permalink
fix(bg/openPayments): handle tab close before accept (#627)
Browse files Browse the repository at this point in the history
  • Loading branch information
sidvishnoi authored Sep 25, 2024
1 parent 2325cb3 commit 4559b9c
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/background/services/openPayments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,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 type { Tabs } from 'webextension-polyfill';
import type { Browser, Tabs } from 'webextension-polyfill';
import { getExchangeRates, getRateOfPay, toAmount } from '../utils';
import { exportJWK, generateEd25519KeyPair } from '@/shared/crypto';
import { bytesToHex } from '@noble/hashes/utils';
Expand Down Expand Up @@ -89,6 +89,9 @@ interface CreateOutgoingPaymentParams {
}

type TabUpdateCallback = Parameters<Tabs.onUpdatedEvent['addListener']>[0];
type TabRemovedCallback = Parameters<
Browser['tabs']['onRemoved']['addListener']
>[0];

const enum ErrorCode {
CONTINUATION_FAILED = 'continuation_failed',
Expand Down Expand Up @@ -570,9 +573,16 @@ export class OpenPaymentsService {
}

private async getInteractionInfo(url: string): Promise<InteractionParams> {
return await new Promise((res) => {
return await new Promise((resolve, reject) => {
this.browser.tabs.create({ url }).then((tab) => {
if (!tab.id) return;
const tabCloseListener: TabRemovedCallback = (tabId) => {
if (tabId !== tab.id) return;

this.browser.tabs.onRemoved.removeListener(tabCloseListener);
reject(new Error('Tab closed before getting interaction info'));
};

const getInteractionInfo: TabUpdateCallback = async (
tabId,
changeInfo,
Expand All @@ -590,15 +600,18 @@ export class OpenPaymentsService {
result === 'grant_invalid'
) {
this.browser.tabs.onUpdated.removeListener(getInteractionInfo);
this.browser.tabs.onRemoved.removeListener(tabCloseListener);
}

if (interactRef && hash) {
res({ interactRef, hash, tabId });
resolve({ interactRef, hash, tabId });
}
} catch {
/* do nothing */
}
};

this.browser.tabs.onRemoved.addListener(tabCloseListener);
this.browser.tabs.onUpdated.addListener(getInteractionInfo);
});
});
Expand Down

0 comments on commit 4559b9c

Please sign in to comment.