-
-
Notifications
You must be signed in to change notification settings - Fork 2
Add x402 server template #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1dab1af
add x402 server template
AyushBherwani1998 a94b231
remvoe test files
AyushBherwani1998 7b30d4a
fix lint issues
AyushBherwani1998 2a7c396
update template
AyushBherwani1998 7b347aa
add review comments
AyushBherwani1998 1818944
minor fixes
AyushBherwani1998 c3d2c0b
remove unused import
AyushBherwani1998 26ad549
improve the template
AyushBherwani1998 eae44af
address reviewer comments
AyushBherwani1998 d1571e9
update casting
AyushBherwani1998 405e91e
address review comments
AyushBherwani1998 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| export type Framework = 'nextjs' | 'vite-react'; | ||
| export type Framework = 'nextjs' | 'vite-react' | 'node'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # Address that receives payment for the protected resource | ||
| PAY_TO_ADDRESS=0x... | ||
|
|
||
| # Override the default MetaMask Sentinel facilitator URL | ||
| FACILITATOR_URL=https://tx-sentinel-base-mainnet.dev-api.cx.metamask.io/platform/v2/x402 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # dependencies | ||
| node_modules/ | ||
|
|
||
| # build output | ||
| dist/ | ||
|
|
||
| # env files | ||
| .env | ||
|
|
||
| # misc | ||
| .DS_Store | ||
| *.pem | ||
|
|
||
| # debug | ||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
| .pnpm-debug.log* | ||
|
|
||
| # typescript | ||
| *.tsbuildinfo | ||
| .npmrc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| { | ||
| "name": "x402-server", | ||
| "version": "0.1.0", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "build": "tsc", | ||
| "dev": "tsx watch src/index.ts", | ||
| "start": "node dist/index.js" | ||
| }, | ||
| "dependencies": { | ||
| "@x402/core": "^2.12.0", | ||
| "@x402/evm": "^2.12.0", | ||
| "@x402/express": "^2.12.0", | ||
| "cors": "^2.8.5", | ||
| "dotenv": "^16.4.7", | ||
| "express": "^4.21.2" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/cors": "^2.8.17", | ||
| "@types/express": "^5.0.2", | ||
| "@types/node": "^22.15.3", | ||
| "tsx": "^4.19.4", | ||
| "typescript": "^5.8.3" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { config } from "dotenv"; | ||
| import express, { type Request, type Response } from "express"; | ||
| import cors from "cors"; | ||
| import { paymentMiddleware } from "@x402/express"; | ||
| import { x402ResourceServer, HTTPFacilitatorClient } from "@x402/core/server"; | ||
| import { Erc7710ExactEvmScheme } from "./scheme.js"; | ||
|
|
||
| config(); | ||
|
|
||
| const NETWORK_ID = "eip155:8453"; | ||
| const PORT = 4402; | ||
|
|
||
| const payToAddress = process.env.PAY_TO_ADDRESS as string; | ||
| if (!payToAddress) { | ||
| console.error("PAY_TO_ADDRESS environment variable is required"); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const facilitatorUrl = process.env.FACILITATOR_URL as string; | ||
| if (!facilitatorUrl) { | ||
| console.error("FACILITATOR_URL environment variable is required"); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const facilitatorClient = new HTTPFacilitatorClient({ url: facilitatorUrl }); | ||
|
|
||
| const app = express(); | ||
| app.use(cors({ exposedHeaders: ["PAYMENT-REQUIRED", "PAYMENT-RESPONSE"] })); | ||
|
|
||
| app.use( | ||
| paymentMiddleware( | ||
| { | ||
| "GET /api/hello": { | ||
| accepts: [ | ||
| { | ||
| scheme: "exact", | ||
| price: "$0.01", | ||
| network: NETWORK_ID, | ||
| payTo: payToAddress, | ||
| }, | ||
| ], | ||
| description: "Access to protected resource", | ||
| mimeType: "application/json", | ||
| }, | ||
| }, | ||
| new x402ResourceServer(facilitatorClient).register( | ||
| NETWORK_ID, | ||
| new Erc7710ExactEvmScheme(facilitatorClient), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| app.get("/api/hello", (_req: Request, res: Response) => { | ||
| res.json({ message: "Hello!" }); | ||
| }); | ||
|
|
||
| app.listen(PORT, () => { | ||
| console.log(`[seller] Server running on http://localhost:${PORT}`); | ||
| console.log(`[seller] Pay-to address: ${payToAddress}`); | ||
| console.log(`[seller] Facilitator URL: ${facilitatorUrl}`); | ||
| console.log(`[seller] Network: ${NETWORK_ID}`); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { ExactEvmScheme } from "@x402/evm/exact/server"; | ||
| import type { | ||
| PaymentRequirements, | ||
| SupportedKind, | ||
| } from "@x402/core/types"; | ||
| import type { FacilitatorClient } from "@x402/core/server"; | ||
|
|
||
| export class Erc7710ExactEvmScheme extends ExactEvmScheme { | ||
| constructor(private readonly facilitatorClient: FacilitatorClient) { | ||
| super(); | ||
| } | ||
|
|
||
| async enhancePaymentRequirements( | ||
| paymentRequirements: PaymentRequirements, | ||
| supportedKind: SupportedKind, | ||
| facilitatorExtensions: string[], | ||
| ): Promise<PaymentRequirements> { | ||
| const enhanced = await super.enhancePaymentRequirements( | ||
| paymentRequirements, | ||
| supportedKind, | ||
| facilitatorExtensions, | ||
| ); | ||
|
|
||
| const supported = await this.facilitatorClient.getSupported(); | ||
| const facilitators = [ | ||
| ...(supported.signers[paymentRequirements.network] ?? []), | ||
| ...(supported.signers["eip155:*"] ?? []) | ||
| ]; | ||
|
|
||
| return { | ||
| ...enhanced, | ||
| extra: { | ||
| ...enhanced.extra, | ||
| assetTransferMethod: "erc7710", | ||
| facilitators, | ||
| }, | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "ES2022", | ||
| "module": "ESNext", | ||
| "moduleResolution": "bundler", | ||
| "esModuleInterop": true, | ||
| "strict": true, | ||
| "outDir": "dist", | ||
| "rootDir": "src", | ||
| "skipLibCheck": true, | ||
| "forceConsistentCasingInFileNames": true, | ||
| "resolveJsonModule": true, | ||
| "declaration": true | ||
| }, | ||
| "include": ["src"] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should these guy sbe environment vars?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not necessarily, do you want me to move them?