-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathroutes.ts
199 lines (184 loc) · 6.05 KB
/
routes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import base64url from 'base64url'
import { StreamCredentials } from '@interledger/stream-receiver'
import { Logger } from 'pino'
import {
ReadContext,
CreateContext,
CompleteContext,
ListContext
} from '../../../app'
import { IAppConfig } from '../../../config/app'
import { IncomingPaymentService } from './service'
import { IncomingPayment, IncomingPaymentState } from './model'
import {
errorToCode,
errorToMessage,
IncomingPaymentError,
isIncomingPaymentError
} from './errors'
import { AmountJSON, parseAmount } from '../../amount'
import {
getPageInfo,
parsePaginationQueryParameters
} from '../../../shared/pagination'
import { Pagination } from '../../../shared/baseModel'
import { ConnectionService } from '../../connection/service'
// Don't allow creating an incoming payment too far out. Incoming payments with no payments before they expire are cleaned up, since incoming payments creation is unauthenticated.
// TODO what is a good default value for this?
export const MAX_EXPIRY = 24 * 60 * 60 * 1000 // milliseconds
interface ServiceDependencies {
config: IAppConfig
logger: Logger
incomingPaymentService: IncomingPaymentService
connectionService: ConnectionService
}
export interface IncomingPaymentRoutes {
get(ctx: ReadContext): Promise<void>
create(ctx: CreateContext<CreateBody>): Promise<void>
complete(ctx: CompleteContext): Promise<void>
list(ctx: ListContext): Promise<void>
}
export function createIncomingPaymentRoutes(
deps_: ServiceDependencies
): IncomingPaymentRoutes {
const logger = deps_.logger.child({
service: 'IncomingPaymentRoutes'
})
const deps = { ...deps_, logger }
return {
get: (ctx: ReadContext) => getIncomingPayment(deps, ctx),
create: (ctx: CreateContext<CreateBody>) =>
createIncomingPayment(deps, ctx),
complete: (ctx: CompleteContext) => completeIncomingPayment(deps, ctx),
list: (ctx: ListContext) => listIncomingPayments(deps, ctx)
}
}
async function getIncomingPayment(
deps: ServiceDependencies,
ctx: ReadContext
): Promise<void> {
let incomingPayment: IncomingPayment | undefined
try {
incomingPayment = await deps.incomingPaymentService.get(
ctx.params.incomingPaymentId
)
} catch (err) {
ctx.throw(500, 'Error trying to get incoming payment')
}
if (!incomingPayment) return ctx.throw(404)
incomingPayment.paymentPointer = ctx.paymentPointer
const streamCredentials = deps.connectionService.get(incomingPayment)
ctx.body = incomingPaymentToBody(deps, incomingPayment, streamCredentials)
}
export type CreateBody = {
description?: string
expiresAt?: string
incomingAmount?: AmountJSON
externalRef?: string
}
async function createIncomingPayment(
deps: ServiceDependencies,
ctx: CreateContext<CreateBody>
): Promise<void> {
const { body } = ctx.request
let expiresAt: Date | undefined
if (body.expiresAt !== undefined) {
expiresAt = new Date(body.expiresAt)
if (Date.now() + MAX_EXPIRY < expiresAt.getTime())
return ctx.throw(400, 'expiry too high')
}
const incomingPaymentOrError = await deps.incomingPaymentService.create({
paymentPointerId: ctx.paymentPointer.id,
description: body.description,
externalRef: body.externalRef,
expiresAt,
incomingAmount: body.incomingAmount && parseAmount(body.incomingAmount)
})
if (isIncomingPaymentError(incomingPaymentOrError)) {
return ctx.throw(
errorToCode[incomingPaymentOrError],
errorToMessage[incomingPaymentOrError]
)
}
ctx.status = 201
incomingPaymentOrError.paymentPointer = ctx.paymentPointer
const streamCredentials = deps.connectionService.get(incomingPaymentOrError)
ctx.body = incomingPaymentToBody(
deps,
incomingPaymentOrError,
streamCredentials
)
}
async function completeIncomingPayment(
deps: ServiceDependencies,
ctx: CompleteContext
): Promise<void> {
let incomingPaymentOrError: IncomingPayment | IncomingPaymentError
try {
incomingPaymentOrError = await deps.incomingPaymentService.complete(
ctx.params.incomingPaymentId
)
} catch (err) {
ctx.throw(500, 'Error trying to complete incoming payment')
}
if (isIncomingPaymentError(incomingPaymentOrError)) {
return ctx.throw(
errorToCode[incomingPaymentOrError],
errorToMessage[incomingPaymentOrError]
)
}
incomingPaymentOrError.paymentPointer = ctx.paymentPointer
ctx.body = incomingPaymentToBody(deps, incomingPaymentOrError)
}
async function listIncomingPayments(
deps: ServiceDependencies,
ctx: ListContext
): Promise<void> {
const pagination = parsePaginationQueryParameters(ctx.request.query)
try {
const page = await deps.incomingPaymentService.getPaymentPointerPage(
ctx.paymentPointer.id,
pagination
)
const pageInfo = await getPageInfo(
(pagination: Pagination) =>
deps.incomingPaymentService.getPaymentPointerPage(
ctx.paymentPointer.id,
pagination
),
page
)
const result = {
pagination: pageInfo,
result: page.map((item: IncomingPayment) => {
item.paymentPointer = ctx.paymentPointer
return incomingPaymentToBody(deps, item)
})
}
ctx.body = result
} catch (_) {
ctx.throw(500, 'Error trying to list incoming payments')
}
}
function incomingPaymentToBody(
deps: ServiceDependencies,
incomingPayment: IncomingPayment,
streamCredentials?: StreamCredentials
) {
return Object.fromEntries(
Object.entries({
...incomingPayment.toJSON(),
id: `${incomingPayment.paymentPointer.url}/incoming-payments/${incomingPayment.id}`,
paymentPointer: incomingPayment.paymentPointer.url,
ilpStreamConnection: streamCredentials
? {
id: `${deps.config.publicHost}/connections/${incomingPayment.connectionId}`,
ilpAddress: streamCredentials.ilpAddress,
sharedSecret: base64url(streamCredentials.sharedSecret)
}
: `${deps.config.publicHost}/connections/${incomingPayment.connectionId}`,
state: null,
completed: incomingPayment.state === IncomingPaymentState.Completed
}).filter(([_, v]) => v != null)
)
}