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(backend): allow admin api to query all receivers and move checks #3314

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
19 changes: 9 additions & 10 deletions packages/backend/src/open_payments/payment/incoming/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,16 +243,15 @@ export class IncomingPayment
): OpenPaymentsIncomingPaymentWithPaymentMethod {
return {
...this.toOpenPaymentsType(walletAddress),
methods:
this.isExpiredOrComplete() || !ilpStreamCredentials
? []
: [
{
type: 'ilp',
ilpAddress: ilpStreamCredentials.ilpAddress,
sharedSecret: base64url(ilpStreamCredentials.sharedSecret)
}
]
methods: !ilpStreamCredentials
? []
: [
{
type: 'ilp',
ilpAddress: ilpStreamCredentials.ilpAddress,
sharedSecret: base64url(ilpStreamCredentials.sharedSecret)
}
]
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function handleSending(
throw LifecycleError.MissingBalance
}

if (!receiver) {
if (!Receiver.isActive(receiver)) {
// Payment is already (unexpectedly) done. Maybe this is a retry and the previous attempt failed to save the state to Postgres. Or the incoming payment could have been paid by a totally different payment in the time since the quote.
deps.logger.warn(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { Pagination, SortOrder } from '../../../shared/baseModel'
import { FilterString } from '../../../shared/filters'
import { IAppConfig } from '../../../config/app'
import { AssetService } from '../../../asset/service'
import { Receiver } from '../../receiver/model'

export interface OutgoingPaymentService
extends WalletAddressSubresourceService<OutgoingPayment> {
Expand Down Expand Up @@ -369,7 +370,7 @@ async function createOutgoingPayment(
)
const receiver = await deps.receiverService.get(payment.receiver)
stopTimerReceiver()
if (!receiver) {
if (!Receiver.isActive(receiver)) {
throw OutgoingPaymentError.InvalidQuote
}
const stopTimerPeer = deps.telemetry.startTimer(
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/open_payments/quote/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ export async function resolveReceiver(
options: CreateQuoteOptions
): Promise<Receiver> {
const receiver = await deps.receiverService.get(options.receiver)
if (!receiver) {
if (!Receiver.isActive(receiver)) {
deps.logger.info(
{ receiver: options.receiver },
'Could not create quote. Receiver not found'
Expand Down
36 changes: 24 additions & 12 deletions packages/backend/src/open_payments/receiver/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,10 @@ export class Receiver {
incomingPayment: OpenPaymentsIncomingPaymentWithPaymentMethod,
isLocal: boolean
) {
if (incomingPayment.completed) {
throw new Error('Cannot create receiver from completed incoming payment')
}

const expiresAt = incomingPayment.expiresAt
? new Date(incomingPayment.expiresAt)
: undefined

if (expiresAt && expiresAt.getTime() <= Date.now()) {
throw new Error('Cannot create receiver from expired incoming payment')
}

if (!incomingPayment.methods.length) {
throw new Error('Missing payment method(s) on incoming payment')
}

const incomingAmount = incomingPayment.incomingAmount
? parseAmount(incomingPayment.incomingAmount)
: undefined
Expand Down Expand Up @@ -120,4 +108,28 @@ export class Receiver {
requestCounter: Counter.from(0) as Counter
}
}

public static isActive(receiver: Receiver | undefined): receiver is Receiver {
if (!receiver) {
return false
}
const incomingPayment = receiver.incomingPayment
if (incomingPayment.completed) {
// throw new Error('Cannot create receiver from completed incoming payment')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: clean up comments

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return false
}
if (
incomingPayment.expiresAt &&
incomingPayment.expiresAt.getTime() <= Date.now()
) {
// throw new Error('Cannot create receiver from expired incoming payment')
return false
}
if (!incomingPayment.methods.length) {
// throw new Error('Missing payment method(s) on incoming payment')
return false
}

return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ function getStreamCredentials(
deps: ServiceDependencies,
payment: IncomingPayment
): IlpStreamCredentials | undefined {
if (payment.isExpiredOrComplete()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IncomingPayment.isExpiredOrComplete() isn't used anywhere else in the code with this deletion - perhaps it can be cleaned up?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given this, we should update the receiverService.getLocalIncomingPayment function to not error if we could not get stream credentials for the payment.

return undefined
}
const credentials = deps.streamServer.generateCredentials({
paymentTag: payment.id,
asset: {
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/tests/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export async function createQuote(
if (validDestination) {
const receiverService = await deps.use('receiverService')
const receiver = await receiverService.get(receiverUrl)
if (!receiver) {
if (!Receiver.isActive(receiver)) {
throw new Error('receiver not found')
}
if (!receiver.incomingAmount && !receiveAmount && !debitAmount) {
Expand Down
Loading