Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/choices/framework.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Framework } from '../types/framework';

export const FRAMEWORK_OPTIONS = (
Object.keys({ nextjs: null, 'vite-react': null } as Record<
Object.keys({ nextjs: null, 'vite-react': null, node: null } as Record<
Framework,
null
>) as Framework[]
Expand Down
12 changes: 12 additions & 0 deletions src/lib/choices/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,19 @@ const VITE_REACT_TEMPLATE_OPTIONS: ITemplate[] = [
},
];

const NODE_TEMPLATE_OPTIONS: ITemplate[] = [
{
name: 'x402 Server (ERC-7710)',
value: 'x402-server',
description:
'A Node.js Express server with x402 payment middleware and ERC-7710 support',
framework: 'node',
isWeb3AuthSupported: false,
},
];

export const TEMPLATE_OPTIONS: ITemplate[] = [
...NEXTJS_TEMPLATE_OPTIONS,
...VITE_REACT_TEMPLATE_OPTIONS,
...NODE_TEMPLATE_OPTIONS,
];
2 changes: 1 addition & 1 deletion src/lib/types/framework.ts
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';
5 changes: 5 additions & 0 deletions templates/node/x402-server/.env.example
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
22 changes: 22 additions & 0 deletions templates/node/x402-server/.gitignore
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
26 changes: 26 additions & 0 deletions templates/node/x402-server/package.json
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"
}
}
62 changes: 62 additions & 0 deletions templates/node/x402-server/src/index.ts
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;
Comment on lines +10 to +11
Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Member Author

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?


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}`);
});
39 changes: 39 additions & 0 deletions templates/node/x402-server/src/scheme.ts
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,
},
};
}
}
16 changes: 16 additions & 0 deletions templates/node/x402-server/tsconfig.json
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"]
}
Loading