Skip to content

Commit

Permalink
fix(backround): decrease expiry time for one-time payments (#476)
Browse files Browse the repository at this point in the history
Co-authored-by: Sid Vishnoi <8426945+sidvishnoi@users.noreply.github.com>
  • Loading branch information
raducristianpopa and sidvishnoi authored Aug 5, 2024
1 parent 8a3c955 commit e216ce1
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/background/services/paymentSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const HOUR_MS = 3600 * 1000
const MIN_SEND_AMOUNT = 1n // 1 unit

type PaymentSessionSource = 'tab-change' | 'request-id-reused' | 'new-link'
type IncomingPaymentSource = 'one-time' | 'continuous'

export class PaymentSession {
private rate: string
Expand Down Expand Up @@ -255,7 +256,7 @@ export class PaymentSession {
if (this.incomingPaymentUrl && !reset) return

try {
const incomingPayment = await this.createIncomingPayment()
const incomingPayment = await this.createIncomingPayment('continuous')
this.incomingPaymentUrl = incomingPayment.id
} catch (error) {
if (isKeyRevokedError(error)) {
Expand All @@ -266,7 +267,13 @@ export class PaymentSession {
}
}

private async createIncomingPayment(): Promise<IncomingPayment> {
private async createIncomingPayment(
source: IncomingPaymentSource
): Promise<IncomingPayment> {
const expiresAt = new Date(
Date.now() + 1000 * (source === 'continuous' ? 60 * 10 : 30)
).toISOString()

const incomingPaymentGrant =
await this.openPaymentsService.client!.grant.request(
{
Expand Down Expand Up @@ -297,7 +304,7 @@ export class PaymentSession {
},
{
walletAddress: this.receiver.id,
expiresAt: new Date(Date.now() + 1000 * 60 * 10).toISOString(),
expiresAt,
metadata: {
source: 'Web Monetization'
}
Expand All @@ -324,7 +331,7 @@ export class PaymentSession {
throw new Error('Attempted to send a payment to a disabled session.')
}

const incomingPayment = await this.createIncomingPayment().catch(
const incomingPayment = await this.createIncomingPayment('one-time').catch(
(error) => {
if (isKeyRevokedError(error)) {
this.events.emit('open_payments.key_revoked')
Expand Down

0 comments on commit e216ce1

Please sign in to comment.