-
Notifications
You must be signed in to change notification settings - Fork 0
feat: facilitator #1
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
9 commits
Select commit
Hold shift + click to select a range
cf14a75
feat: init facilitator
lucky-ivanius f87b21b
feat(facilitator): dev setup
lucky-ivanius 9234620
feat: facilitator server
lucky-ivanius 9fe2b19
docs: add server and client example
lucky-ivanius 1445d5a
feat: global error handling
lucky-ivanius e171027
style: fix non-standard naming
lucky-ivanius c25ed38
fix: validation error 400
lucky-ivanius e5bec75
chore: remove logging
lucky-ivanius dec1614
refactor: raw error logging
lucky-ivanius 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 @@ | ||
| EVM_PRIVATE_KEY=0xYourEvmPrivateKey |
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,34 @@ | ||
| # dependencies (bun install) | ||
| node_modules | ||
|
|
||
| # output | ||
| out | ||
| dist | ||
| *.tgz | ||
|
|
||
| # code coverage | ||
| coverage | ||
| *.lcov | ||
|
|
||
| # logs | ||
| logs | ||
| _.log | ||
| report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json | ||
|
|
||
| # dotenv environment variable files | ||
| .env | ||
| .env.development.local | ||
| .env.test.local | ||
| .env.production.local | ||
| .env.local | ||
|
|
||
| # caches | ||
| .eslintcache | ||
| .cache | ||
| *.tsbuildinfo | ||
|
|
||
| # IntelliJ based IDEs | ||
| .idea | ||
|
|
||
| # Finder (MacOS) folder config | ||
| .DS_Store |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,36 @@ | ||
| import { x402Client, x402HTTPClient } from "@x402/core/client"; | ||
| import { ExactEvmScheme } from "@x402/evm/exact/client"; | ||
| import { wrapFetchWithPayment } from "@x402/fetch"; | ||
| import { privateKeyToAccount } from "viem/accounts"; | ||
|
|
||
| // Create signer | ||
| const signer = privateKeyToAccount( | ||
| process.env.EVM_PRIVATE_KEY as `0x${string}` | ||
| ); | ||
|
|
||
| // Create x402 client and register EVM scheme | ||
| const client = new x402Client(); | ||
| client.register("eip155:16661", new ExactEvmScheme(signer)); | ||
|
|
||
| // Wrap fetch with payment handling | ||
| const fetchWithPayment = wrapFetchWithPayment(fetch, client); | ||
|
|
||
| // Make request - payment is handled automatically | ||
| const response = await fetchWithPayment("http://localhost:3000/weather", { | ||
| method: "GET", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| }); | ||
|
|
||
| const data = await response.json(); | ||
| console.log("Response:", data); | ||
|
|
||
| // Get payment receipt from response headers | ||
| if (response.ok) { | ||
| const httpClient = new x402HTTPClient(client); | ||
| const paymentResponse = httpClient.getPaymentSettleResponse((name) => | ||
| response.headers.get(name) | ||
| ); | ||
| console.log("Payment settled:", paymentResponse); | ||
| } | ||
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,17 @@ | ||
| { | ||
| "name": "newt0n-client-example", | ||
| "module": "index.ts", | ||
| "type": "module", | ||
| "private": true, | ||
| "devDependencies": { | ||
| "@types/bun": "latest" | ||
| }, | ||
| "peerDependencies": { | ||
| "typescript": "^5" | ||
|
lucky-ivanius marked this conversation as resolved.
|
||
| }, | ||
| "dependencies": { | ||
| "@x402/evm": "^2.10.0", | ||
| "@x402/fetch": "^2.10.0", | ||
| "viem": "^2.48.0" | ||
| } | ||
| } | ||
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,30 @@ | ||
| { | ||
| "compilerOptions": { | ||
| // Environment setup & latest features | ||
| "lib": ["ESNext"], | ||
| "target": "ESNext", | ||
| "module": "Preserve", | ||
| "moduleDetection": "force", | ||
| "jsx": "react-jsx", | ||
| "allowJs": true, | ||
| "types": ["bun"], | ||
|
|
||
| // Bundler mode | ||
| "moduleResolution": "bundler", | ||
| "allowImportingTsExtensions": true, | ||
| "verbatimModuleSyntax": true, | ||
| "noEmit": true, | ||
|
|
||
| // Best practices | ||
| "strict": true, | ||
| "skipLibCheck": true, | ||
| "noFallthroughCasesInSwitch": true, | ||
| "noUncheckedIndexedAccess": true, | ||
| "noImplicitOverride": true, | ||
|
|
||
| // Some stricter flags (disabled by default) | ||
| "noUnusedLocals": false, | ||
| "noUnusedParameters": false, | ||
| "noPropertyAccessFromIndexSignature": false | ||
| } | ||
| } |
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,34 @@ | ||
| # dependencies (bun install) | ||
| node_modules | ||
|
|
||
| # output | ||
| out | ||
| dist | ||
| *.tgz | ||
|
|
||
| # code coverage | ||
| coverage | ||
| *.lcov | ||
|
|
||
| # logs | ||
| logs | ||
| _.log | ||
| report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json | ||
|
|
||
| # dotenv environment variable files | ||
| .env | ||
| .env.development.local | ||
| .env.test.local | ||
| .env.production.local | ||
| .env.local | ||
|
|
||
| # caches | ||
| .eslintcache | ||
| .cache | ||
| *.tsbuildinfo | ||
|
|
||
| # IntelliJ based IDEs | ||
| .idea | ||
|
|
||
| # Finder (MacOS) folder config | ||
| .DS_Store |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,64 @@ | ||
| import { HTTPFacilitatorClient } from "@x402/core/server"; | ||
| import { ExactEvmScheme } from "@x402/evm/exact/server"; | ||
| import { paymentMiddleware, x402ResourceServer } from "@x402/hono"; | ||
| import { Hono } from "hono"; | ||
| import { logger } from "hono/logger"; | ||
|
|
||
| const app = new Hono(); | ||
| const evmAddress = "0xYourEvmAddress"; | ||
|
|
||
| const facilitatorClient = new HTTPFacilitatorClient({ | ||
| url: "http://facilitator.newt0n.ai", | ||
| }); | ||
|
lucky-ivanius marked this conversation as resolved.
|
||
|
|
||
| app.use(logger()); | ||
|
|
||
| const zeroGExactScheme = new ExactEvmScheme(); | ||
| zeroGExactScheme.registerMoneyParser( | ||
| async (amount: number, _network: string) => { | ||
| const tokenAmount = Math.floor(amount * 1_000_000).toString(); | ||
|
|
||
| return { | ||
| amount: tokenAmount, | ||
| asset: "0x1f3aa82227281ca364bfb3d253b0f1af1da6473e", | ||
|
lucky-ivanius marked this conversation as resolved.
|
||
| extra: { | ||
| name: "Bridged USDC", | ||
| version: "2", | ||
| }, | ||
| }; | ||
| } | ||
| ); | ||
|
|
||
| app.use( | ||
| paymentMiddleware( | ||
| { | ||
| "GET /weather": { | ||
| accepts: [ | ||
| { | ||
| scheme: "exact", | ||
| price: "$0.001", | ||
| network: "eip155:16661", | ||
| payTo: evmAddress, | ||
| }, | ||
| ], | ||
| description: "Weather data", | ||
| mimeType: "application/json", | ||
| }, | ||
| }, | ||
| new x402ResourceServer(facilitatorClient).register( | ||
| "eip155:16661", | ||
| zeroGExactScheme | ||
| ) | ||
| ) | ||
| ); | ||
|
|
||
| app.get("/weather", (c) => { | ||
| return c.json({ | ||
| report: { | ||
| weather: "sunny", | ||
| temperature: 70, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| export default app; | ||
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,18 @@ | ||
| { | ||
| "name": "newt0n-server-example", | ||
| "module": "index.ts", | ||
| "type": "module", | ||
| "private": true, | ||
| "devDependencies": { | ||
| "@types/bun": "latest" | ||
| }, | ||
| "peerDependencies": { | ||
| "typescript": "^5" | ||
| }, | ||
| "dependencies": { | ||
| "@x402/core": "^2.10.0", | ||
| "@x402/evm": "^2.10.0", | ||
| "@x402/hono": "^2.10.0", | ||
| "hono": "^4.12.14" | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.