1- import type { ThirdwebClient } from "thirdweb" ;
2- import { stringify } from "thirdweb/utils" ;
31import type { VerifyResponse } from "x402/types" ;
42import type {
53 FacilitatorSettleResponse ,
@@ -8,14 +6,14 @@ import type {
86 RequestedPaymentPayload ,
97 RequestedPaymentRequirements ,
108} from "./schemas.js" ;
9+ import { stringify } from "./utils.js" ;
1110
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,39 @@ 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 } ` ,
140121 } ,
122+ list : {
123+ authorization : `Bearer ${ config . walletSecret } ` ,
124+ } ,
125+ } as const ;
126+
127+ return {
128+ url : BASE_URL as `${string } ://${string } `,
129+ address : config . walletAddress ,
130+ createAuthHeaders : async ( ) => AUTH_HEADERS ,
141131 /**
142132 * Verifies a payment payload with the facilitator service
143133 *
@@ -149,13 +139,11 @@ export function facilitator(
149139 payload : RequestedPaymentPayload ,
150140 paymentRequirements : RequestedPaymentRequirements ,
151141 ) : Promise < FacilitatorVerifyResponse > {
152- const url = config . baseUrl ?? DEFAULT_BASE_URL ;
153-
154142 let headers = { "Content-Type" : "application/json" } ;
155- const authHeaders = await facilitator . createAuthHeaders ( ) ;
156- headers = { ...headers , ...authHeaders . verify } ;
157143
158- const res = await fetch ( `${ url } /verify` , {
144+ headers = { ...headers , ...AUTH_HEADERS . verify } ;
145+
146+ const res = await fetch ( new URL ( "/verify" , BASE_URL ) , {
159147 method : "POST" ,
160148 headers,
161149 body : stringify ( {
@@ -186,14 +174,11 @@ export function facilitator(
186174 paymentRequirements : RequestedPaymentRequirements ,
187175 waitUntil ?: WaitUntil ,
188176 ) : Promise < FacilitatorSettleResponse > {
189- const url = config . baseUrl ?? DEFAULT_BASE_URL ;
190-
191177 let headers = { "Content-Type" : "application/json" } ;
192- const authHeaders = await facilitator . createAuthHeaders ( ) ;
193- headers = { ...headers , ...authHeaders . settle } ;
178+ headers = { ...headers , ...AUTH_HEADERS . settle } ;
194179 const waitUntilParam = waitUntil || config . waitUntil ;
195180
196- const res = await fetch ( ` ${ url } /settle` , {
181+ const res = await fetch ( new URL ( " /settle" , BASE_URL ) , {
197182 method : "POST" ,
198183 headers,
199184 body : JSON . stringify ( {
@@ -222,22 +207,18 @@ export function facilitator(
222207 chainId : number ;
223208 tokenAddress ?: string ;
224209 } ) : 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 ( ) ;
229210 const headers = {
230211 "Content-Type" : "application/json" ,
231- ...authHeaders . supported ,
212+ ...AUTH_HEADERS . supported ,
232213 } ;
233- const supportedUrl = new URL ( ` ${ url } /supported` ) ;
214+ const supportedUrl = new URL ( " /supported" , BASE_URL ) ;
234215 if ( filters ?. chainId ) {
235216 supportedUrl . searchParams . set ( "chainId" , filters . chainId . toString ( ) ) ;
236217 }
237218 if ( filters ?. tokenAddress ) {
238219 supportedUrl . searchParams . set ( "tokenAddress" , filters . tokenAddress ) ;
239220 }
240- const res = await fetch ( supportedUrl . toString ( ) , { headers } ) ;
221+ const res = await fetch ( supportedUrl , { headers } ) ;
241222
242223 if ( res . status !== 200 ) {
243224 throw new Error (
@@ -248,7 +229,5 @@ export function facilitator(
248229 const data = await res . json ( ) ;
249230 return data as FacilitatorSupportedResponse ;
250231 } ,
251- } ;
252-
253- return facilitator ;
232+ } as const satisfies ThirdwebX402Facilitator ;
254233}
0 commit comments