Skip to content

Commit a0e160f

Browse files
committed
update to use nexus keys instead
1 parent dfa35f6 commit a0e160f

File tree

6 files changed

+94
-132
lines changed

6 files changed

+94
-132
lines changed

packages/nexus/knip.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
2-
"$schema": "https://unpkg.com/knip@5/schema.json",
3-
"entry": ["src/exports/**"],
4-
"ignore": ["src/**/__generated__/**", "**/*.bench.ts"],
5-
"ignoreBinaries": ["printf"],
6-
"ignoreDependencies": ["tslib"],
7-
"project": ["src/**/*.{ts,tsx}"],
8-
"rules": {
9-
"enumMembers": "off",
10-
"optionalPeerDependencies": "off"
11-
}
2+
"$schema": "https://unpkg.com/knip@5/schema.json",
3+
"entry": ["src/exports/**"],
4+
"ignore": ["src/**/__generated__/**", "**/*.bench.ts"],
5+
"ignoreBinaries": ["printf"],
6+
"ignoreDependencies": ["tslib", "thirdweb"],
7+
"project": ["src/**/*.{ts,tsx}"],
8+
"rules": {
9+
"enumMembers": "off",
10+
"optionalPeerDependencies": "off"
11+
}
1212
}
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
export type {
2-
HTTPRequestStructure,
3-
Money,
4-
PaymentMiddlewareConfig,
5-
Resource,
2+
HTTPRequestStructure,
3+
Money,
4+
PaymentMiddlewareConfig,
5+
Resource,
66
} from "x402/types";
77
export { decodePayment, encodePayment } from "../encode.js";
88
export {
9-
facilitator,
10-
type ThirdwebX402Facilitator,
11-
type ThirdwebX402FacilitatorConfig,
12-
type WaitUntil,
9+
createFacilitator,
10+
type ThirdwebX402Facilitator,
11+
type ThirdwebX402FacilitatorConfig,
12+
type WaitUntil,
1313
} from "../facilitator.js";
1414
export { wrapFetchWithPayment } from "../fetchWithPayment.js";
1515
export { settlePayment } from "../settle-payment.js";
1616
export type {
17-
ERC20TokenAmount,
18-
PaymentArgs,
19-
PaymentRequiredResult,
20-
SettlePaymentArgs,
21-
SettlePaymentResult,
22-
SupportedSignatureType,
23-
VerifyPaymentResult,
17+
ERC20TokenAmount,
18+
PaymentArgs,
19+
PaymentRequiredResult,
20+
SettlePaymentArgs,
21+
SettlePaymentResult,
22+
SupportedSignatureType,
23+
VerifyPaymentResult,
2424
} from "../types.js";
2525
export { verifyPayment } from "../verify-payment.js";

packages/nexus/src/facilitator.ts

Lines changed: 52 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { ThirdwebClient } from "thirdweb";
21
import { stringify } from "thirdweb/utils";
32
import type { VerifyResponse } from "x402/types";
43
import type {
@@ -12,10 +11,9 @@ import type {
1211
export type WaitUntil = "simulated" | "submitted" | "confirmed";
1312

1413
export 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
}

packages/nexus/src/fetchWithPayment.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ import { createPaymentHeader } from "./sign.js";
2626
*
2727
* @example
2828
* ```typescript
29-
* import { wrapFetchWithPayment } from "thirdweb/x402";
29+
* import { wrapFetchWithPayment } from "@thirdweb-dev/nexus";
3030
* import { createThirdwebClient } from "thirdweb";
3131
* import { createWallet } from "thirdweb/wallets";
3232
*
33-
* const client = createThirdwebClient({ clientId: "your-client-id" });
33+
* const client = createThirdwebClient({ clientId: "your-thirdweb-client-id" });
3434
* const wallet = createWallet("io.metamask");
3535
* await wallet.connect({ client })
3636
*
@@ -44,7 +44,6 @@ import { createPaymentHeader } from "./sign.js";
4444
* @throws {Error} If a payment has already been attempted for this request
4545
* @throws {Error} If there's an error creating the payment header
4646
*
47-
* @bridge x402
4847
*/
4948
export function wrapFetchWithPayment(
5049
fetch: typeof globalThis.fetch,

packages/nexus/src/settle-payment.ts

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,12 @@ import {
2323
*
2424
* ```ts
2525
* // Usage in a Next.js API route
26-
* import { settlePayment, facilitator } from "thirdweb/x402";
27-
* import { createThirdwebClient } from "thirdweb";
26+
* import { settlePayment, createFacilitator } from "@thirdweb-dev/nexus";
2827
* import { arbitrumSepolia } from "thirdweb/chains";
2928
*
30-
* const client = createThirdwebClient({
31-
* secretKey: process.env.THIRDWEB_SECRET_KEY,
32-
* });
33-
*
34-
* const thirdwebFacilitator = facilitator({
35-
* client,
36-
* serverWalletAddress: "0x1234567890123456789012345678901234567890",
29+
* const facilitator = createFacilitator({
30+
* walletSecret: <your-wallet-secret>,
31+
* walletAddress: <your-wallet-address>,
3732
* });
3833
*
3934
* export async function GET(request: Request) {
@@ -44,10 +39,9 @@ import {
4439
* resourceUrl: "https://api.example.com/premium-content",
4540
* method: "GET",
4641
* paymentData,
47-
* payTo: "0x1234567890123456789012345678901234567890",
4842
* network: arbitrumSepolia, // or any other chain
4943
* price: "$0.10", // or { amount: "100000", asset: { address: "0x...", decimals: 6 } }
50-
* facilitator: thirdwebFacilitator,
44+
* facilitator,
5145
* routeConfig: {
5246
* description: "Access to premium API content",
5347
* mimeType: "application/json",
@@ -73,17 +67,12 @@ import {
7367
* ```ts
7468
* // Usage in Express middleware
7569
* import express from "express";
76-
* import { settlePayment, facilitator } from "thirdweb/x402";
77-
* import { createThirdwebClient } from "thirdweb";
70+
* import { settlePayment, createFacilitator } from "@thirdweb-dev/nexus";
7871
* import { arbitrumSepolia } from "thirdweb/chains";
7972
*
80-
* const client = createThirdwebClient({
81-
* secretKey: process.env.THIRDWEB_SECRET_KEY,
82-
* });
83-
*
84-
* const thirdwebFacilitator = facilitator({
85-
* client,
86-
* serverWalletAddress: "0x1234567890123456789012345678901234567890",
73+
* const facilitator = createFacilitator({
74+
* walletSecret: <your-wallet-secret>,
75+
* walletAddress: <your-wallet-address>,
8776
* });
8877
*
8978
* const app = express();
@@ -94,11 +83,10 @@ import {
9483
* resourceUrl: `${req.protocol}://${req.get('host')}${req.originalUrl}`,
9584
* method: req.method,
9685
* paymentData: req.headers["x-payment"],
97-
* payTo: "0x1234567890123456789012345678901234567890",
9886
* network: arbitrumSepolia, // or any other chain
9987
* price: "$0.05",
10088
* waitUntil: "submitted",
101-
* facilitator: thirdwebFacilitator,
89+
* facilitator,
10290
* });
10391
*
10492
* if (result.status === 200) {
@@ -122,7 +110,6 @@ import {
122110
*
123111
* @public
124112
* @beta
125-
* @bridge x402
126113
*/
127114
export async function settlePayment(
128115
args: SettlePaymentArgs,

0 commit comments

Comments
 (0)