-
Notifications
You must be signed in to change notification settings - Fork 18
feat: add faucet API route at /api/faucet #121
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
5 commits
Select commit
Hold shift + click to select a range
806745d
feat: add faucet API route at /api/faucet
brendanjryan 96ff9f6
docs: update faucet curl examples to use docs.tempo.xyz/api/faucet
brendanjryan 2d890e3
fix: align faucet CORS with index-supply pattern
brendanjryan 9af3f7e
fix: use chain default RPC URL, align error message with index-supply
brendanjryan 95915ce
fix: remove GET handler, POST-only to prevent prefetcher/crawler abuse
brendanjryan 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import { createClient, http } from 'viem' | ||
| import { tempoModerato } from 'viem/chains' | ||
| import { Actions } from 'viem/tempo' | ||
|
|
||
| function getClient() { | ||
| return createClient({ | ||
| chain: tempoModerato, | ||
| transport: http(), | ||
| }) | ||
| } | ||
|
|
||
| export async function POST(request: Request): Promise<Response> { | ||
| const origin = request.headers.get('origin') | ||
| const corsHeaders = cors(origin) | ||
|
|
||
| let body: { address?: string } | ||
| try { | ||
| body = await request.json() | ||
| } catch { | ||
| return Response.json( | ||
| { data: null, error: 'Invalid request: could not parse JSON' }, | ||
| { status: 400, headers: corsHeaders }, | ||
| ) | ||
| } | ||
|
|
||
| const address = body?.address | ||
| if (!address || !/^0x[a-fA-F0-9]{40}$/.test(address)) { | ||
| return Response.json( | ||
| { data: null, error: 'Invalid or missing address' }, | ||
| { status: 400, headers: corsHeaders }, | ||
| ) | ||
| } | ||
|
|
||
| return fund(address.toLowerCase() as `0x${string}`, corsHeaders) | ||
| } | ||
|
|
||
| export async function OPTIONS(request: Request): Promise<Response> { | ||
| const origin = request.headers.get('origin') | ||
| return new Response(null, { status: 200, headers: cors(origin) }) | ||
| } | ||
|
|
||
| async function fund(address: `0x${string}`, headers: Record<string, string>): Promise<Response> { | ||
| try { | ||
| const client = getClient() | ||
| const hashes = await Actions.faucet.fund(client, { account: address }) | ||
|
|
||
| const data = hashes.map((hash) => ({ hash })) | ||
| return Response.json({ data, error: null }, { headers }) | ||
| } catch (error) { | ||
| console.error('Faucet error:', error) | ||
| const message = error instanceof Error ? error.message : 'Unknown error occurred' | ||
| return Response.json({ data: null, error: message }, { status: 500, headers }) | ||
| } | ||
| } | ||
|
|
||
| function cors(origin: string | null): Record<string, string> { | ||
| const allowedOrigins = ['https://docs.tempo.xyz'] | ||
|
|
||
| if (origin?.includes('vercel.app')) allowedOrigins.push(origin) | ||
| if (process.env.NODE_ENV === 'development') allowedOrigins.push('http://localhost:5173') | ||
|
|
||
| const headers: Record<string, string> = { | ||
| 'Access-Control-Allow-Methods': 'POST, OPTIONS', | ||
| 'Access-Control-Allow-Headers': 'Content-Type, x-api-token', | ||
| } | ||
|
|
||
| if (origin && allowedOrigins.some((allowed) => origin.startsWith(allowed))) | ||
| headers['Access-Control-Allow-Origin'] = origin | ||
|
|
||
| return headers | ||
| } | ||
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
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.
The CORS check uses
origin.startsWith(allowed), so an attacker-controlled origin likehttps://docs.tempo.xyz.attacker.compasses the whitelist and receivesAccess-Control-Allow-Origin. This weakens the intended browser-origin restriction for this endpoint; compare parsed origins/hosts with exact trusted values (or enforce proper subdomain boundary checks) rather than prefix matching.Useful? React with 👍 / 👎.