-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathverify-payment-request-token.ts
More file actions
65 lines (58 loc) · 1.76 KB
/
verify-payment-request-token.ts
File metadata and controls
65 lines (58 loc) · 1.76 KB
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
import type { Resolvable } from "@agentcommercekit/did"
import { verifyJwt, type JwtVerified } from "@agentcommercekit/jwt"
import * as v from "valibot"
import { InvalidPaymentRequestTokenError } from "./errors"
import type { PaymentRequest } from "./payment-request"
import { paymentRequestSchema } from "./schemas/valibot"
interface ValidatePaymentRequestTokenOptions {
/**
* The resolver to use for did resolution
*/
resolver?: Resolvable
/**
* Whether to verify the expiry of the payment request token
*/
verifyExpiry?: boolean
/**
* The issuer to verify the payment request token against
*/
issuer?: string
}
/**
* Verify a payment request token
*
* @param token - The payment request token to verify
* @param options - The {@link ValidatePaymentRequestTokenOptions} to use
* @returns The {@link PaymentRequest} parsed from the payment request token and the parsed JWT
*/
export async function verifyPaymentRequestToken(
token: string,
options: ValidatePaymentRequestTokenOptions = {},
): Promise<{ paymentRequest: PaymentRequest; parsed: JwtVerified }> {
let parsedPaymentRequestToken: JwtVerified
try {
parsedPaymentRequestToken = await verifyJwt(token, {
resolver: options.resolver,
issuer: options.issuer,
policies: {
aud: false,
exp: options.verifyExpiry ?? true,
},
})
} catch (err) {
throw new InvalidPaymentRequestTokenError(undefined, { cause: err })
}
const { success, output } = v.safeParse(
paymentRequestSchema,
parsedPaymentRequestToken.payload,
)
if (!success) {
throw new InvalidPaymentRequestTokenError(
"Payment Request token is not a valid PaymentRequest",
)
}
return {
paymentRequest: output,
parsed: parsedPaymentRequestToken,
}
}