1- import type { ThirdwebClient } from "thirdweb" ;
21import { stringify } from "thirdweb/utils" ;
32import type { VerifyResponse } from "x402/types" ;
43import type {
@@ -12,10 +11,9 @@ import type {
1211export type WaitUntil = "simulated" | "submitted" | "confirmed" ;
1312
1413export type ThirdwebX402FacilitatorConfig = {
15- client : ThirdwebClient ;
16- serverWalletAddress : string ;
14+ walletSecret : string ;
15+ walletAddress : string ;
1716 waitUntil ?: WaitUntil ;
18- vaultAccessToken ?: string ;
1917 baseUrl ?: string ;
2018} ;
2119
@@ -47,7 +45,7 @@ export type ThirdwebX402Facilitator = {
4745 } ) => Promise < FacilitatorSupportedResponse > ;
4846} ;
4947
50- const DEFAULT_BASE_URL = "https://api.thirdweb.com/v1/payments/x402 " ;
48+ const DEFAULT_BASE_URL = "https://nexus- api.thirdweb.com" ;
5149
5250/**
5351 * Creates a facilitator for the x402 payment protocol.
@@ -58,21 +56,17 @@ const DEFAULT_BASE_URL = "https://api.thirdweb.com/v1/payments/x402";
5856 *
5957 * @example
6058 * ```ts
61- * import { facilitator } from "thirdweb/x402";
62- * import { createThirdwebClient } from "thirdweb";
59+ * import { createFacilitator } from "@thirdweb-dev/nexus";
6360 * import { paymentMiddleware } from 'x402-hono'
6461 *
65- * const client = createThirdwebClient({
66- * secretKey: "your-secret-key",
67- * });
68- * const thirdwebX402Facilitator = facilitator({
69- * client: client,
70- * serverWalletAddress: "0x1234567890123456789012345678901234567890",
62+ * const facilitator = createFacilitator({
63+ * walletSecret: <your-wallet-secret>,
64+ * walletAddress: <your-wallet-address>,
7165 * });
7266 *
7367 * // add the facilitator to any x402 payment middleware
7468 * const middleware = paymentMiddleware(
75- * "0x1234567890123456789012345678901234567890" ,
69+ * facilitator.address ,
7670 * {
7771 * "/api/paywall": {
7872 * price: "$0.01",
@@ -82,16 +76,16 @@ const DEFAULT_BASE_URL = "https://api.thirdweb.com/v1/payments/x402";
8276 * },
8377 * },
8478 * },
85- * thirdwebX402Facilitator ,
79+ * facilitator ,
8680 * );
8781 * ```
8882 *
8983 * #### Configuration Options
9084 *
9185 * ```ts
92- * const thirdwebX402Facilitator = facilitator ({
93- * client: client ,
94- * serverWalletAddress: "0x1234567890123456789012345678901234567890" ,
86+ * const thirdwebX402Facilitator = createFacilitator ({
87+ * walletSecret: <your-wallet-secret> ,
88+ * walletAddress: <your-wallet-address> ,
9589 * // Optional: Wait behavior for settlements
9690 * // - "simulated": Only simulate the transaction (fastest)
9791 * // - "submitted": Wait until transaction is submitted
@@ -101,43 +95,43 @@ const DEFAULT_BASE_URL = "https://api.thirdweb.com/v1/payments/x402";
10195
10296 * ```
10397 *
104- * @bridge x402
10598 */
106- export function facilitator (
99+ export function createFacilitator (
107100 config : ThirdwebX402FacilitatorConfig ,
108101) : ThirdwebX402Facilitator {
109- const secretKey = config . client . secretKey ;
110- if ( ! secretKey ) {
111- throw new Error ( "Client secret key is required for the x402 facilitator" ) ;
102+ if ( ! config . walletSecret ) {
103+ throw new Error ( "Wallet secret is required for the x402 facilitator" ) ;
112104 }
113- const serverWalletAddress = config . serverWalletAddress ;
114- if ( ! serverWalletAddress ) {
115- throw new Error (
116- "Server wallet address is required for the x402 facilitator" ,
117- ) ;
105+
106+ if ( ! config . walletAddress ) {
107+ throw new Error ( "Wallet address is required for the x402 facilitator" ) ;
118108 }
119- const facilitator = {
120- url : ( config . baseUrl ?? DEFAULT_BASE_URL ) as `${string } ://${string } `,
121- address : serverWalletAddress ,
122- createAuthHeaders : async ( ) => {
123- return {
124- verify : {
125- "x-secret-key" : secretKey ,
126- } ,
127- settle : {
128- "x-secret-key" : secretKey ,
129- ...( config . vaultAccessToken
130- ? { "x-vault-access-token" : config . vaultAccessToken }
131- : { } ) ,
132- } ,
133- supported : {
134- "x-secret-key" : secretKey ,
135- } ,
136- list : {
137- "x-secret-key" : secretKey ,
138- } ,
139- } ;
109+
110+ const BASE_URL = config . baseUrl ?? DEFAULT_BASE_URL ;
111+
112+ const AUTH_HEADERS = {
113+ verify : {
114+ authorization : `Bearer ${ config . walletSecret } ` ,
115+ } ,
116+ settle : {
117+ authorization : `Bearer ${ config . walletSecret } ` ,
118+ } ,
119+ supported : {
120+ authorization : `Bearer ${ config . walletSecret } ` ,
121+ } ,
122+ list : {
123+ authorization : `Bearer ${ config . walletSecret } ` ,
140124 } ,
125+ } as const ;
126+
127+ async function createAuthHeaders ( ) {
128+ return AUTH_HEADERS ;
129+ }
130+
131+ return {
132+ url : BASE_URL as `${string } ://${string } `,
133+ address : config . walletAddress ,
134+ createAuthHeaders,
141135 /**
142136 * Verifies a payment payload with the facilitator service
143137 *
@@ -149,13 +143,11 @@ export function facilitator(
149143 payload : RequestedPaymentPayload ,
150144 paymentRequirements : RequestedPaymentRequirements ,
151145 ) : Promise < FacilitatorVerifyResponse > {
152- const url = config . baseUrl ?? DEFAULT_BASE_URL ;
153-
154146 let headers = { "Content-Type" : "application/json" } ;
155- const authHeaders = await facilitator . createAuthHeaders ( ) ;
156- headers = { ...headers , ...authHeaders . verify } ;
157147
158- const res = await fetch ( `${ url } /verify` , {
148+ headers = { ...headers , ...AUTH_HEADERS . verify } ;
149+
150+ const res = await fetch ( new URL ( "/verify" , BASE_URL ) , {
159151 method : "POST" ,
160152 headers,
161153 body : stringify ( {
@@ -186,14 +178,11 @@ export function facilitator(
186178 paymentRequirements : RequestedPaymentRequirements ,
187179 waitUntil ?: WaitUntil ,
188180 ) : Promise < FacilitatorSettleResponse > {
189- const url = config . baseUrl ?? DEFAULT_BASE_URL ;
190-
191181 let headers = { "Content-Type" : "application/json" } ;
192- const authHeaders = await facilitator . createAuthHeaders ( ) ;
193- headers = { ...headers , ...authHeaders . settle } ;
182+ headers = { ...headers , ...AUTH_HEADERS . settle } ;
194183 const waitUntilParam = waitUntil || config . waitUntil ;
195184
196- const res = await fetch ( ` ${ url } /settle` , {
185+ const res = await fetch ( new URL ( " /settle" , BASE_URL ) , {
197186 method : "POST" ,
198187 headers,
199188 body : JSON . stringify ( {
@@ -222,22 +211,18 @@ export function facilitator(
222211 chainId : number ;
223212 tokenAddress ?: string ;
224213 } ) : Promise < FacilitatorSupportedResponse > {
225- const url = config . baseUrl ?? DEFAULT_BASE_URL ;
226-
227- // TODO: re-add caching? (see thirdweb/x402/facilitator.ts)
228- const authHeaders = await facilitator . createAuthHeaders ( ) ;
229214 const headers = {
230215 "Content-Type" : "application/json" ,
231- ...authHeaders . supported ,
216+ ...AUTH_HEADERS . supported ,
232217 } ;
233- const supportedUrl = new URL ( ` ${ url } /supported` ) ;
218+ const supportedUrl = new URL ( " /supported" , BASE_URL ) ;
234219 if ( filters ?. chainId ) {
235220 supportedUrl . searchParams . set ( "chainId" , filters . chainId . toString ( ) ) ;
236221 }
237222 if ( filters ?. tokenAddress ) {
238223 supportedUrl . searchParams . set ( "tokenAddress" , filters . tokenAddress ) ;
239224 }
240- const res = await fetch ( supportedUrl . toString ( ) , { headers } ) ;
225+ const res = await fetch ( supportedUrl , { headers } ) ;
241226
242227 if ( res . status !== 200 ) {
243228 throw new Error (
@@ -248,7 +233,5 @@ export function facilitator(
248233 const data = await res . json ( ) ;
249234 return data as FacilitatorSupportedResponse ;
250235 } ,
251- } ;
252-
253- return facilitator ;
236+ } as const satisfies ThirdwebX402Facilitator ;
254237}
0 commit comments