Skip to content

Commit

Permalink
feat(backend): remove payment pointer from url, retrieve it from quer…
Browse files Browse the repository at this point in the history
…y or body
  • Loading branch information
njlie committed Oct 16, 2023
1 parent 5e6377d commit 32edfd3
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ exports.down = function (knex) {
'ALTER INDEX "walletAddresses_pkey" RENAME TO "paymentPointers_pkey"'
),
knex.raw(
'ALTER INDEX "walletAddresses_url_index" RENAME TO "paymentPointers_url_index"'
'ALTER INDEX "walletaddresses_url_index" RENAME TO "paymentpointers_url_index"'
),
knex.raw(
'ALTER INDEX "walletaddresses_processat_index" RENAME TO "paymentpointers_processat_index"'
Expand All @@ -113,7 +113,7 @@ exports.down = function (knex) {
'ALTER TABLE "paymentPointers" DROP CONSTRAINT "walletaddresses_url_unique"'
),
knex.raw(
'ALTER TABLE "paymentPointers" DROP CONSTRAINT "walletaddreses_assetid_foreign"'
'ALTER TABLE "paymentPointers" DROP CONSTRAINT "walletaddresses_assetid_foreign"'
),
knex.schema.renameTable('walletAddressKeys', 'paymentPointerKeys'),
knex.schema.alterTable('paymentPointerKeys', function (table) {
Expand All @@ -124,7 +124,7 @@ exports.down = function (knex) {
'ALTER INDEX "walletAddressKeys_pkey" RENAME TO "paymentPointerKeys_pkey"'
),
knex.raw(
'ALTER TABLE "paymentpointerKeys" DROP CONSTRAINT "walletaddresskeys_paymentpointerid_foreign"'
'ALTER TABLE "paymentPointerKeys" DROP CONSTRAINT "walletaddresskeys_walletaddressid_foreign"'
),
knex.schema.alterTable('quotes', function (table) {
table.dropForeign(['walletAddressId'])
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@graphql-tools/load": "^8.0.0",
"@graphql-tools/schema": "^10.0.0",
"@interledger/http-signature-utils": "1.1.0",
"@interledger/open-payments": "3.1.0",
"@interledger/open-payments": "5.2.0",
"@interledger/openapi": "1.0.3",
"@interledger/pay": "0.4.0-alpha.9",
"@interledger/stream-receiver": "^0.3.3-alpha.3",
Expand Down
30 changes: 17 additions & 13 deletions packages/backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ export type HttpSigContext = AppContext & {
}

// Payment pointer subresources
interface GetCollectionQuery {
'wallet-address': string
}

type CollectionRequest<BodyT = never, QueryT = ParsedUrlQuery> = Omit<
PaymentPointerContext['request'],
'body'
Expand Down Expand Up @@ -383,7 +387,7 @@ export class App {
// POST /incoming-payments
// Create incoming payment
router.post<DefaultState, SignedCollectionContext<IncomingCreateBody>>(
PAYMENT_POINTER_PATH + '/incoming-payments',
'/incoming-payments',
createPaymentPointerMiddleware(),
createValidatorMiddleware<
ContextType<SignedCollectionContext<IncomingCreateBody>>
Expand All @@ -401,10 +405,10 @@ export class App {

// GET /incoming-payments
// List incoming payments
router.get<DefaultState, SignedCollectionContext>(
PAYMENT_POINTER_PATH + '/incoming-payments',
router.get<DefaultState, SignedCollectionContext<never, GetCollectionQuery>>(
'/incoming-payments',
createPaymentPointerMiddleware(),
createValidatorMiddleware<ContextType<SignedCollectionContext>>(
createValidatorMiddleware<ContextType<SignedCollectionContext<never, GetCollectionQuery>>>(
resourceServerSpec,
{
path: '/incoming-payments',
Expand All @@ -422,7 +426,7 @@ export class App {
// POST /outgoing-payment
// Create outgoing payment
router.post<DefaultState, SignedCollectionContext<OutgoingCreateBody>>(
PAYMENT_POINTER_PATH + '/outgoing-payments',
'/outgoing-payments',
createPaymentPointerMiddleware(),
createValidatorMiddleware<
ContextType<SignedCollectionContext<OutgoingCreateBody>>
Expand All @@ -440,10 +444,10 @@ export class App {

// GET /outgoing-payment
// List outgoing payments
router.get<DefaultState, SignedCollectionContext>(
PAYMENT_POINTER_PATH + '/outgoing-payments',
router.get<DefaultState, SignedCollectionContext<never, GetCollectionQuery>>(
'/outgoing-payments',
createPaymentPointerMiddleware(),
createValidatorMiddleware<ContextType<SignedCollectionContext>>(
createValidatorMiddleware<ContextType<SignedCollectionContext<never, GetCollectionQuery>>>(
resourceServerSpec,
{
path: '/outgoing-payments',
Expand All @@ -461,7 +465,7 @@ export class App {
// POST /quotes
// Create quote
router.post<DefaultState, SignedCollectionContext<QuoteCreateBody>>(
PAYMENT_POINTER_PATH + '/quotes',
'/quotes',
createPaymentPointerMiddleware(),
createValidatorMiddleware<
ContextType<SignedCollectionContext<QuoteCreateBody>>
Expand All @@ -480,7 +484,7 @@ export class App {
// GET /incoming-payments/{id}
// Read incoming payment
router.get<DefaultState, SignedSubresourceContext>(
PAYMENT_POINTER_PATH + '/incoming-payments/:id',
'/incoming-payments/:id',
createPaymentPointerMiddleware(),
createValidatorMiddleware<ContextType<SignedSubresourceContext>>(
resourceServerSpec,
Expand All @@ -500,7 +504,7 @@ export class App {
// POST /incoming-payments/{id}/complete
// Complete incoming payment
router.post<DefaultState, SignedSubresourceContext>(
PAYMENT_POINTER_PATH + '/incoming-payments/:id/complete',
'/incoming-payments/:id/complete',
createPaymentPointerMiddleware(),
createValidatorMiddleware<ContextType<SignedSubresourceContext>>(
resourceServerSpec,
Expand All @@ -520,7 +524,7 @@ export class App {
// GET /outgoing-payments/{id}
// Read outgoing payment
router.get<DefaultState, SignedSubresourceContext>(
PAYMENT_POINTER_PATH + '/outgoing-payments/:id',
'/outgoing-payments/:id',
createPaymentPointerMiddleware(),
createValidatorMiddleware<ContextType<SignedSubresourceContext>>(
resourceServerSpec,
Expand All @@ -540,7 +544,7 @@ export class App {
// GET /quotes/{id}
// Read quote
router.get<DefaultState, SignedSubresourceContext>(
PAYMENT_POINTER_PATH + '/quotes/:id',
'/quotes/:id',
createPaymentPointerMiddleware(),
createValidatorMiddleware<ContextType<SignedSubresourceContext>>(
resourceServerSpec,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ async function getIncomingPayment(
}

export type CreateBody = {
walletAddress: string
expiresAt?: string
incomingAmount?: AmountJSON
metadata?: Record<string, unknown>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ async function getOutgoingPayment(
}

export type CreateBody = {
walletAddress: string
quoteId: string
metadata?: Record<string, unknown>
}
Expand Down
16 changes: 15 additions & 1 deletion packages/backend/src/open_payments/payment_pointer/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import { AppContext } from '../../app'
import { CreateBody as IncomingCreateBody } from '../../open_payments/payment/incoming/routes'
import { CreateBody as OutgoingCreateBody } from '../../open_payments/payment/outgoing/routes'

type CreateBody = IncomingCreateBody | OutgoingCreateBody

export function createPaymentPointerMiddleware() {
return async (
ctx: AppContext,
next: () => Promise<unknown>
): Promise<void> => {
ctx.paymentPointerUrl = `https://${ctx.request.host}/${ctx.params.paymentPointerPath}`
if (ctx.path === '/incoming-payments' || ctx.path === '/outgoing-payments') {
if (ctx.method === 'GET') {
ctx.paymentPointerUrl = ctx.query['wallet-address'] as string
} else if (ctx.method === 'POST') {
ctx.paymentPointerUrl = (ctx.request.body as CreateBody).walletAddress
} else {
ctx.throw(401)
}
} else {
ctx.paymentPointerUrl = `https://${ctx.request.host}/${ctx.params.paymentPointerPath}`
}
const config = await ctx.container.use('config')
if (ctx.paymentPointerUrl !== config.paymentPointerUrl) {
const paymentPointerService = await ctx.container.use(
Expand Down
4 changes: 3 additions & 1 deletion packages/backend/src/shared/pagination.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { PaginationArgs } from '@interledger/open-payments'
import { PaginationArgs as OpenPaymentsPaginationArgs } from '@interledger/open-payments'

import { BaseModel, PageInfo, Pagination } from './baseModel'

type PaginationArgs = Omit<OpenPaymentsPaginationArgs, 'wallet-address'>

export function parsePaginationQueryParameters({
first,
last,
Expand Down
35 changes: 33 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 32edfd3

Please sign in to comment.