{"_id":"@adipundir/starknet-x402","name":"@adipundir/starknet-x402","dist-tags":{"latest":"0.1.0"},"versions":{"0.1.0":{"name":"@adipundir/starknet-x402","version":"0.1.0","description":"x402 payment protocol SDK for Starknet - HTTP-native micropayments","main":"./dist/index.js","module":"./dist/index.mjs","types":"./dist/index.d.ts","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.mjs","require":"./dist/index.js"}},"scripts":{"build":"tsup src/index.ts --format cjs,esm --dts","dev":"tsup src/index.ts --format cjs,esm --dts --watch","lint":"tslint 'src/**/*.ts'","clean":"rm -rf dist"},"keywords":["x402","starknet","payments","micropayments","http","blockchain","crypto"],"author":{"name":"Aditya Pundir"},"license":"MIT","peerDependencies":{"next":"^15.0.0","starknet":"^7.0.0"},"devDependencies":{"@types/node":"^20.19.21","tsup":"^8.5.0","typescript":"^5.9.3"},"repository":{"type":"git","url":"git+https://github.com/adipundir/starknet-x402.git"},"bugs":{"url":"https://github.com/adipundir/starknet-x402/issues"},"homepage":"https://github.com/adipundir/starknet-x402#readme","_id":"@adipundir/starknet-x402@0.1.0","gitHead":"b9a044df82a27452e3c18faf6ac26ab78e5852b5","_nodeVersion":"22.19.0","_npmVersion":"10.9.3","dist":{"integrity":"sha512-I0E8RBNaEPzrNSl7yq3h1I9Hqdi9uDaJzDmMtrK/8m9lidUkLxN7k8XTtfemmHYKNC0MU1US6c0gOd3JhNX5zA==","shasum":"c6f2a9e95fae3a04f0438e91e6b927dad911f51d","tarball":"https://registry.npmjs.org/@adipundir/starknet-x402/-/starknet-x402-0.1.0.tgz","fileCount":6,"unpackedSize":54098,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIAVdOXao5pb76f9/IDA96owcT32uOxSJjMbCLbN40ltwAiBQ8s8hzm/o52mGzvpsbf+408GXc+azBGiwyeiOVKCWQw=="}]},"_npmUser":{"name":"adipundir","email":"pundir.aditya@outlook.com"},"directories":{},"maintainers":[{"name":"adipundir","email":"pundir.aditya@outlook.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/starknet-x402_0.1.0_1760565577177_0.601950525118292"},"_hasShrinkwrap":false}},"time":{"created":"2025-10-15T21:59:37.120Z","0.1.0":"2025-10-15T21:59:37.366Z","modified":"2025-10-15T21:59:37.611Z"},"maintainers":[{"name":"adipundir","email":"pundir.aditya@outlook.com"}],"description":"x402 payment protocol SDK for Starknet - HTTP-native micropayments","homepage":"https://github.com/adipundir/starknet-x402#readme","keywords":["x402","starknet","payments","micropayments","http","blockchain","crypto"],"repository":{"type":"git","url":"git+https://github.com/adipundir/starknet-x402.git"},"author":{"name":"Aditya Pundir"},"bugs":{"url":"https://github.com/adipundir/starknet-x402/issues"},"license":"MIT","readme":"# @x402/starknet-sdk\n\n> x402 payment protocol SDK for Starknet - HTTP-native micropayments\n\n## Installation\n\n```bash\nnpm install @x402/starknet-sdk starknet\n```\n\n## Features\n\n- 🔐 **Secure**: Off-chain signatures, on-chain settlement\n- ⚡ **Fast**: Verify payments without waiting for blockchain confirmation\n- 💰 **Flexible**: Support any ERC20 token (STRK, ETH, USDC, etc.)\n- 🎯 **Simple**: Easy integration with Next.js and Express\n- 📦 **TypeScript**: Full type safety\n\n## Quick Start\n\n### Server Setup (Next.js)\n\n```typescript\nimport { paymentMiddleware } from '@x402/starknet-sdk';\n\nexport const config = {\n  matcher: '/api/protected/:path*',\n};\n\nexport default paymentMiddleware(\n  process.env.RECIPIENT_ADDRESS!, // Where payments go\n  {\n    '/api/protected/weather': {\n      price: '10000000000000000', // 0.01 STRK\n      tokenAddress: '0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d',\n      network: 'sepolia',\n      config: {\n        description: 'Access to weather API',\n        mimeType: 'application/json',\n      },\n    },\n  },\n  {\n    url: 'http://localhost:3000/api/facilitator',\n  }\n);\n```\n\n### Client Usage\n\n```typescript\nimport { Account, RpcProvider } from 'starknet';\n\n// 1. Initialize Starknet account\nconst provider = new RpcProvider({ nodeUrl: 'https://starknet-sepolia.public.blastapi.io' });\nconst account = new Account(provider, accountAddress, privateKey, '1');\n\n// 2. Make payment request\nconst response = await fetch('http://localhost:3000/api/protected/weather');\n\nif (response.status === 402) {\n  const paymentRequirements = await response.json();\n  const scheme = paymentRequirements.accepts[0];\n\n  // 3. Sign payment\n  const nonce = '0x' + Array.from(new Uint8Array(31))\n    .map(() => Math.floor(Math.random() * 256).toString(16).padStart(2, '0'))\n    .join('');\n  \n  const deadline = Math.floor(Date.now() / 1000) + 3600; // 1 hour\n\n  const message = {\n    types: {\n      StarkNetDomain: [\n        { name: 'name', type: 'felt' },\n        { name: 'version', type: 'felt' },\n        { name: 'chainId', type: 'felt' },\n      ],\n      Payment: [\n        { name: 'from', type: 'felt' },\n        { name: 'to', type: 'felt' },\n        { name: 'token', type: 'felt' },\n        { name: 'amount', type: 'felt' },\n        { name: 'nonce', type: 'felt' },\n        { name: 'deadline', type: 'felt' },\n      ],\n    },\n    primaryType: 'Payment',\n    domain: {\n      name: 'x402 Payment',\n      version: '1',\n      chainId: '0x534e5f5345504f4c4941',\n    },\n    message: {\n      from: accountAddress.toLowerCase(),\n      to: scheme.payTo.toLowerCase(),\n      token: scheme.asset.toLowerCase(),\n      amount: scheme.maxAmountRequired,\n      nonce: nonce.toLowerCase(),\n      deadline: deadline.toString(),\n    },\n  };\n\n  const sig = await account.signMessage(message);\n  const sigR = typeof sig[0] === 'bigint' ? '0x' + sig[0].toString(16) : sig[0];\n  const sigS = typeof sig[1] === 'bigint' ? '0x' + sig[1].toString(16) : sig[1];\n\n  // 4. Send payment header\n  const paymentPayload = {\n    x402Version: 1,\n    scheme: 'exact',\n    network: 'starknet-sepolia',\n    payload: {\n      from: accountAddress.toLowerCase(),\n      to: scheme.payTo.toLowerCase(),\n      token: scheme.asset.toLowerCase(),\n      amount: scheme.maxAmountRequired,\n      nonce: nonce.toLowerCase(),\n      deadline,\n      signature: { r: sigR, s: sigS },\n    },\n  };\n\n  const paymentHeader = Buffer.from(JSON.stringify(paymentPayload)).toString('base64');\n\n  const paidResponse = await fetch('http://localhost:3000/api/protected/weather', {\n    headers: {\n      'X-PAYMENT': paymentHeader,\n    },\n  });\n\n  const data = await paidResponse.json();\n  console.log(data); // Weather data!\n}\n```\n\n## API Reference\n\n### `paymentMiddleware(recipientAddress, routes, facilitatorConfig)`\n\nCreates a Next.js middleware for x402 payments.\n\n**Parameters:**\n- `recipientAddress` (string): Starknet address where payments should be sent\n- `routes` (object): Map of route paths to their payment requirements\n- `facilitatorConfig` (object): Configuration for the payment facilitator\n\n**Returns:** Next.js middleware function\n\n### Types\n\n```typescript\ninterface RouteConfig {\n  price: string; // Amount in wei\n  tokenAddress: string; // ERC20 token contract address\n  network?: 'sepolia' | 'mainnet';\n  config?: {\n    description?: string;\n    mimeType?: string;\n    maxTimeoutSeconds?: number;\n  };\n}\n\ninterface PaymentRequirements {\n  scheme: string;\n  network: string;\n  maxAmountRequired: string;\n  resource: string;\n  description: string;\n  payTo: string;\n  asset: string;\n}\n```\n\n## Architecture\n\n```\n┌─────────┐                    ┌──────────┐\n│ Client  │ ─── X-PAYMENT ───> │ Server   │\n└─────────┘                    └────┬─────┘\n                                    │\n                              ┌─────┴─────┐\n                              │ Middleware│\n                              └─────┬─────┘\n                                    │\n                         ┌──────────┴──────────┐\n                         │                     │\n                    ┌────▼────┐          ┌────▼────┐\n                    │ Verify  │          │ Settle  │\n                    │ (fast)  │          │(on-chain)│\n                    └─────────┘          └─────────┘\n```\n\n## Security\n\n- ✅ Off-chain signatures (gasless for users)\n- ✅ ERC20 `transfer_from` pattern (requires approval)\n- ✅ Nonce prevents replay attacks\n- ✅ Deadline prevents stale payments\n- ✅ Exact amount control\n- ✅ Recipient address locked\n\n## License\n\nMIT\n\n","readmeFilename":"README.md","_rev":"1-06f7e457ff088d8fb1ddf3e0b3999110"}