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(backround): decrease expiry time for one time payments #476

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Changes from all 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
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
Loading