-
Notifications
You must be signed in to change notification settings - Fork 9
/
client.ts
437 lines (368 loc) · 13.6 KB
/
client.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import type { JwtPayload } from '@web5/crypto'
import type { ErrorDetail } from './types.js'
import type { DidDocument, BearerDid } from '@web5/dids'
import {
Balance,
Close,
MessageModel,
Order,
Parser,
Rfq,
} from '@tbdex/protocol'
import {
RequestError,
ResponseError,
InvalidDidError,
MissingServiceEndpointError,
RequestTokenMissingClaimsError,
RequestTokenAudienceMismatchError,
RequestTokenSigningError,
RequestTokenVerificationError,
RequestTokenIssuerSignerMismatchError
} from './errors/index.js'
import { resolveDid, Offering, Message } from '@tbdex/protocol'
import { utils as didUtils } from '@web5/dids'
import { typeid } from 'typeid-js'
import { Jwt, JwtVerifyResult } from '@web5/credentials'
import queryString from 'query-string'
import ms from 'ms'
/**
* Parameters for generating a request token
* @beta
*/
export type GenerateRequestTokenParams = {
requesterDid: BearerDid
pfiDid: string
}
/**
* Parameters for verifying a request token
* @beta
*/
export type VerifyRequestTokenParams = {
requestToken: string
pfiDid: string
}
/**
* Required jwt claims expected in a request token
* @beta
*/
export const requestTokenRequiredClaims = ['aud', 'iss', 'exp', 'iat', 'jti']
/**
* HTTP client for interacting with TBDex PFIs
* @beta
*/
export class TbdexHttpClient {
/**
* Sends an RFQ and options to the PFI to initiate an exchange
* @param rfq - The RFQ message that will be sent to the PFI
* @param opts.replyTo A callback URL where the PFI will send subsequent messages
* @throws if message verification fails
* @throws if recipient DID resolution fails
* @throws if recipient DID does not have a PFI service entry
*/
static async createExchange(rfq: Rfq, opts?: { replyTo?: string }): Promise<void> {
await rfq.verify()
const { to: pfiDid } = rfq.metadata
const requestBody = JSON.stringify({ message: rfq, replyTo: opts?.replyTo })
await TbdexHttpClient.sendMessage(pfiDid, 'POST', `/exchanges`, requestBody)
}
/**
* Sends the Order message to the PFI
* @param - order The Order message that will be sent to the PFI
* @throws if message verification fails
* @throws if recipient DID resolution fails
* @throws if recipient DID does not have a PFI service entry
*/
static async submitOrder(order: Order): Promise<void> {
await order.verify()
const { to: pfiDid, exchangeId } = order.metadata
const requestBody = JSON.stringify({ message: order })
await TbdexHttpClient.sendMessage(pfiDid, 'PUT', `/exchanges/${exchangeId}`, requestBody)
}
/**
* Sends the Close message to the PFI
* @param - close The Close message that will be sent to the PFI
* @throws if message verification fails
* @throws if recipient DID resolution fails
* @throws if recipient DID does not have a PFI service entry
*/
static async submitClose(close: Close): Promise<void> {
await close.verify()
const { to: pfiDid, exchangeId } = close.metadata
const requestBody = JSON.stringify({ message: close })
await TbdexHttpClient.sendMessage(pfiDid, 'PUT', `/exchanges/${exchangeId}`, requestBody)
}
private static async sendMessage(pfiDid: string, verb: 'GET' | 'PUT' | 'POST', path: string, requestBody: string): Promise<void> {
const pfiServiceEndpoint = await TbdexHttpClient.getPfiServiceEndpoint(pfiDid)
const apiRoute = `${pfiServiceEndpoint}${path}`
let response: Response
try {
response = await fetch(apiRoute, {
method : verb,
headers : { 'content-type': 'application/json' },
body : requestBody
})
} catch (e) {
throw new RequestError({ message: `Failed to send message to ${pfiDid}`, recipientDid: pfiDid, url: apiRoute, cause: e })
}
if (!response.ok) {
const errorDetails = await response.json() as ErrorDetail[]
throw new ResponseError({ statusCode: response.status, details: errorDetails, recipientDid: pfiDid, url: apiRoute })
}
}
/**
* gets offerings from the pfi provided
* @param opts - options
* @beta
*/
static async getOfferings(opts: GetOfferingsOptions): Promise<Offering[]> {
const { pfiDid } = opts
const pfiServiceEndpoint = await TbdexHttpClient.getPfiServiceEndpoint(pfiDid)
const apiRoute = `${pfiServiceEndpoint}/offerings`
let response: Response
try {
response = await fetch(apiRoute)
} catch (e) {
throw new RequestError({ message: `Failed to get offerings from ${pfiDid}`, recipientDid: pfiDid, url: apiRoute, cause: e })
}
const offerings: Offering[] = []
if (!response.ok) {
const errorDetails = await response.json() as ErrorDetail[]
throw new ResponseError({ statusCode: response.status, details: errorDetails, recipientDid: pfiDid, url: apiRoute })
}
const responseBody = await response.json()
const jsonOfferings = responseBody.data as any[]
for (let jsonOffering of jsonOfferings) {
const offering = await Offering.parse(jsonOffering)
offerings.push(offering)
}
return offerings
}
/**
* gets balances from the pfi provided
* @param opts - options
* @beta
*/
static async getBalances(opts: GetBalancesOptions): Promise<Balance[]> {
const { pfiDid, did } = opts
const pfiServiceEndpoint = await TbdexHttpClient.getPfiServiceEndpoint(pfiDid)
const apiRoute = `${pfiServiceEndpoint}/balances`
const requestToken = await TbdexHttpClient.generateRequestToken({ requesterDid: did, pfiDid })
let response: Response
try {
response = await fetch(apiRoute, {
headers: {
authorization: `Bearer ${requestToken}`
}
})
} catch (e) {
throw new RequestError({ message: `Failed to get balances from ${pfiDid}`, recipientDid: pfiDid, url: apiRoute, cause: e })
}
if (!response.ok) {
const errorDetails = await response.json() as ErrorDetail[]
throw new ResponseError({ statusCode: response.status, details: errorDetails, recipientDid: pfiDid, url: apiRoute })
}
const responseBody = await response.json() as { data: Balance[] }
const data: Balance[] = responseBody.data
return data
}
/**
* get a specific exchange from the pfi provided
* @param opts - options
*/
static async getExchange(opts: GetExchangeOptions): Promise<Message[]> {
const { pfiDid, exchangeId, did } = opts
const pfiServiceEndpoint = await TbdexHttpClient.getPfiServiceEndpoint(pfiDid)
const apiRoute = `${pfiServiceEndpoint}/exchanges/${exchangeId}`
const requestToken = await TbdexHttpClient.generateRequestToken({ requesterDid: did, pfiDid })
let response: Response
try {
response = await fetch(apiRoute, {
headers: {
authorization: `Bearer ${requestToken}`
}
})
} catch (e) {
throw new RequestError({ message: `Failed to get exchange from ${pfiDid}`, recipientDid: pfiDid, url: apiRoute, cause: e })
}
const messages: Message[] = []
if (!response.ok) {
const errorDetails = await response.json() as ErrorDetail[]
throw new ResponseError({ statusCode: response.status, details: errorDetails, recipientDid: pfiDid, url: apiRoute })
}
const responseBody = await response.json() as { data: MessageModel[] }
for (let jsonMessage of responseBody.data) {
const message = await Parser.parseMessage(jsonMessage)
messages.push(message)
}
return messages
}
// TODO: Wrap Message[] in Exchange object and verify each message
/**
* returns all exchanges created by requester
* @param opts - options
*/
static async getExchanges(opts: GetExchangesOptions): Promise<Message[][]> {
const { pfiDid, filter, did } = opts
const pfiServiceEndpoint = await TbdexHttpClient.getPfiServiceEndpoint(pfiDid)
const queryParams = filter ? `?${queryString.stringify(filter)}` : ''
const apiRoute = `${pfiServiceEndpoint}/exchanges${queryParams}`
const requestToken = await TbdexHttpClient.generateRequestToken({ requesterDid: did, pfiDid })
let response: Response
try {
response = await fetch(apiRoute, {
headers: {
authorization: `Bearer ${requestToken}`
}
})
} catch (e) {
throw new RequestError({ message: `Failed to get exchanges from ${pfiDid}`, recipientDid: pfiDid, url: apiRoute, cause: e })
}
const exchanges: Message[][] = []
if (!response.ok) {
const errorDetails = await response.json() as ErrorDetail[]
throw new ResponseError({ statusCode: response.status, details: errorDetails, recipientDid: pfiDid, url: apiRoute })
}
const responseBody = await response.json() as { data: MessageModel[][] }
for (let jsonExchange of responseBody.data) {
const exchange: Message[] = []
for (let jsonMessage of jsonExchange) {
const message = await Parser.parseMessage(jsonMessage)
exchange.push(message)
}
exchanges.push(exchange)
}
return exchanges
}
/**
* returns the PFI service entry from the DID Doc of the DID provided
* @param did - the pfi's DID
*/
static async getPfiServiceEndpoint(did: string) {
let didDocument: DidDocument
try {
didDocument = await resolveDid(did)
} catch (e) {
throw new InvalidDidError(e.message)
}
const [didService] = didUtils.getServices({ didDocument, type: 'PFI' })
if (!didService?.serviceEndpoint) {
throw new MissingServiceEndpointError(`${did} has no PFI service entry`)
}
return didService.serviceEndpoint
}
/**
* Creates and signs a request token ([JWT](https://datatracker.ietf.org/doc/html/rfc7519))
* that's included as the value of Authorization header for requests sent to a PFI API's
* endpoints that require authentication
*
* JWT payload with the following claims:
* * `aud`
* * `iss`
* * `exp`
* * `iat`
* * `jti` The JWT is then signed and returned.
*
* @returns the request token (JWT)
* @throws {@link RequestTokenSigningError} If an error occurs during the token generation.
*/
static async generateRequestToken(params: GenerateRequestTokenParams): Promise<string> {
const { pfiDid, requesterDid } = params
const now = Date.now()
const exp = (now + ms('1m'))
const jwtPayload: JwtPayload = {
aud : pfiDid,
iss : requesterDid.uri,
exp : Math.floor(exp / 1000),
iat : Math.floor(now / 1000),
jti : typeid().getSuffix()
}
try {
return await Jwt.sign({ signerDid: requesterDid, payload: jwtPayload })
} catch(e) {
throw new RequestTokenSigningError({ message: e.message, cause: e })
}
}
/**
* Validates and verifies the integrity of a request token ([JWT](https://datatracker.ietf.org/doc/html/rfc7519))
* generated by {@link TbdexHttpClient.generateRequestToken}. Specifically:
* * verifies integrity of the JWT
* * ensures all required claims are present and valid.
* * ensures the token has not expired
* * ensures token audience matches the expected PFI DID.
*
* @returns the requester's DID as a string if the token is valid.
* @throws {@link RequestTokenVerificationError} If the token is invalid, expired, or has been tampered with
* @throws {@link RequestTokenMissingClaimsError} If the token does not contain all required claims
* @throws {@link RequestTokenAudienceMismatchError} If the token's `aud` property does not match the PFI's DID
*/
static async verifyRequestToken(params: VerifyRequestTokenParams): Promise<string> {
let result: JwtVerifyResult
try {
result = await Jwt.verify({ jwt: params.requestToken })
} catch(e) {
throw new RequestTokenVerificationError({ message: e.message, cause: e })
}
const { header: requestTokenHeader, payload: requestTokenPayload } = result
// check to ensure all expected claims are present
for (let claim of requestTokenRequiredClaims) {
if (!requestTokenPayload[claim]) {
throw new RequestTokenMissingClaimsError({ message: `Request token missing ${claim} claim. Expected ${requestTokenRequiredClaims}.` })
}
}
// TODO: decide if we want to ensure that the expiration date is not longer than 1 minute after the issuance date
if (requestTokenPayload.aud !== params.pfiDid) {
throw new RequestTokenAudienceMismatchError({ message: 'Request token contains invalid audience. Expected aud property to be PFI DID.' })
}
const signerKid = requestTokenHeader.kid!
const issuerDid = requestTokenPayload.iss!
if (!signerKid.includes(issuerDid)) {
throw new RequestTokenIssuerSignerMismatchError({ message: 'Request token issuer does not match signer' })
}
return issuerDid
}
}
/**
* options passed to {@link TbdexHttpClient.getOfferings} method
* @beta
*/
export type GetOfferingsOptions = {
/** the DID of the PFI from whom you want to get offerings */
pfiDid: string
}
/**
* options passed to {@link TbdexHttpClient.getBalances} method
* @beta
*/
export type GetBalancesOptions = {
/** the DID of the PFI from whom you want to get balances */
pfiDid: string
did: BearerDid
}
/**
* options passed to {@link TbdexHttpClient.getExchange} method
* @beta
*/
export type GetExchangeOptions = {
/** the DID of the PFI from whom you want to get offerings */
pfiDid: string
/** the exchange you want to fetch */
exchangeId: string
/** the message author's DID */
did: BearerDid
}
/**
* options passed to {@link TbdexHttpClient.getExchanges} method
* @beta
*/
export type GetExchangesOptions = {
/** the DID of the PFI from whom you want to get offerings */
pfiDid: string
/** the message author's DID */
did: BearerDid,
/** the filter to select the desired exchanges */
filter?: {
/** ID or IDs of exchanges to get */
id: string | string[]
}
}