-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add stealth address #18
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
Open
snawaz
wants to merge
6
commits into
main
Choose a base branch
from
snawaz/stealth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3d8ee7a
feat: add stealth address
snawaz 68c56a7
show tx signature on failed handle save
snawaz f240a46
save handle worked
snawaz f957f97
make 'save handle' two steps process: create-and-delegate and then up…
snawaz ee32852
invoke /transfer-queue/ensure-crank
snawaz b02a4f1
align stealth handle UI with raw-handle (string) PDAs
snawaz 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { | ||
| Connection, | ||
| PublicKey, | ||
| SendTransactionError, | ||
| Transaction, | ||
| VersionedTransaction, | ||
| } from "@solana/web3.js"; | ||
| import { | ||
| createPaymentsEphemeralConnection, | ||
| createServerSolanaConnection, | ||
| } from "@/lib/solana-rpc"; | ||
|
|
||
| function base64ToUint8Array(base64: string) { | ||
| const buffer = Buffer.from(base64, "base64"); | ||
| return new Uint8Array(buffer); | ||
| } | ||
|
|
||
| class FeePayerFundingError extends Error { | ||
| constructor(message: string) { | ||
| super(message); | ||
| this.name = "FeePayerFundingError"; | ||
| } | ||
| } | ||
|
|
||
| function getFeePayer(rawTransaction: Uint8Array) { | ||
| try { | ||
| const transaction = Transaction.from(rawTransaction); | ||
| return transaction.feePayer ?? null; | ||
| } catch { | ||
| try { | ||
| const transaction = VersionedTransaction.deserialize(rawTransaction); | ||
| return transaction.message.staticAccountKeys[0] ?? null; | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async function requireFundedBaseFeePayer( | ||
| connection: Connection, | ||
| feePayer: PublicKey | null | ||
| ) { | ||
| if (!feePayer) { | ||
| return; | ||
| } | ||
|
|
||
| const lamports = await connection.getBalance(feePayer, "confirmed"); | ||
| if (lamports > 0) { | ||
| return; | ||
| } | ||
|
|
||
| const feePayerAddress = feePayer.toBase58(); | ||
| throw new FeePayerFundingError( | ||
| `Base fee payer ${feePayerAddress} has no SOL on the configured base RPC` | ||
| ); | ||
| } | ||
|
|
||
| async function getSendTransactionLogs( | ||
| error: SendTransactionError, | ||
| connection: Connection | ||
| ) { | ||
| if (error.logs?.length) { | ||
| return error.logs; | ||
| } | ||
|
|
||
| try { | ||
| return await error.getLogs(connection); | ||
| } catch { | ||
| return []; | ||
| } | ||
| } | ||
|
|
||
| export async function POST(request: NextRequest) { | ||
| let connection: Connection | null = null; | ||
|
|
||
| try { | ||
| const body = await request.json(); | ||
| const { | ||
| signedTransaction, | ||
| blockhash, | ||
| lastValidBlockHeight, | ||
| sendTo, | ||
| } = body as { | ||
| signedTransaction?: string; | ||
| blockhash?: string; | ||
| lastValidBlockHeight?: number; | ||
| sendTo?: "base" | "ephemeral"; | ||
| }; | ||
|
snawaz marked this conversation as resolved.
|
||
|
|
||
| if ( | ||
| typeof signedTransaction !== "string" || | ||
| !signedTransaction || | ||
| typeof blockhash !== "string" || | ||
| !blockhash || | ||
| typeof lastValidBlockHeight !== "number" || | ||
| (sendTo !== "base" && sendTo !== "ephemeral") | ||
| ) { | ||
| return NextResponse.json( | ||
| { | ||
| error: | ||
| "Missing signedTransaction, blockhash, lastValidBlockHeight, or sendTo", | ||
| }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| const authHeader = request.headers.get("authorization") ?? ""; | ||
| const authToken = authHeader.match(/^Bearer\s+(.+)$/i)?.[1]?.trim(); | ||
| if (sendTo === "ephemeral" && !authToken) { | ||
| return NextResponse.json( | ||
| { error: "Authentication is required for ephemeral submission" }, | ||
| { status: 401 } | ||
| ); | ||
| } | ||
|
|
||
| connection = | ||
| sendTo === "ephemeral" | ||
| ? createPaymentsEphemeralConnection(authToken) | ||
| : createServerSolanaConnection(); | ||
| const rawTransaction = base64ToUint8Array(signedTransaction); | ||
| if (sendTo === "base") { | ||
| await requireFundedBaseFeePayer(connection, getFeePayer(rawTransaction)); | ||
| } | ||
|
|
||
| const signature = await connection.sendRawTransaction(rawTransaction, { | ||
| skipPreflight: sendTo === "ephemeral", | ||
| preflightCommitment: "confirmed", | ||
| maxRetries: 10, | ||
| }); | ||
|
|
||
| const confirmation = await connection.confirmTransaction( | ||
| { | ||
| signature, | ||
| blockhash, | ||
| lastValidBlockHeight, | ||
| }, | ||
| "confirmed" | ||
| ); | ||
|
|
||
| if (confirmation.value.err) { | ||
| return NextResponse.json( | ||
| { | ||
| error: "Transaction failed on-chain", | ||
| details: JSON.stringify(confirmation.value.err), | ||
| signature, | ||
| }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| return NextResponse.json({ signature }); | ||
| } catch (error) { | ||
| if (error instanceof FeePayerFundingError) { | ||
| return NextResponse.json( | ||
| { | ||
| error: error.message, | ||
| details: | ||
| "Fund this wallet on the same base RPC used by the Pay server, or point SOLANA_RPC_URL at the chain where the wallet is funded.", | ||
| logs: [], | ||
| }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| if (error instanceof SendTransactionError && connection) { | ||
| const logs = await getSendTransactionLogs(error, connection); | ||
| const transactionError = error.transactionError; | ||
| const message = transactionError.message || error.message; | ||
|
|
||
| console.error("Payments send transaction error:", { | ||
| message, | ||
| logs, | ||
| }); | ||
|
|
||
| return NextResponse.json( | ||
| { | ||
| error: message, | ||
| details: logs.length > 0 ? logs.join("\n") : error.message, | ||
| logs, | ||
| }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| console.error("Payments send error:", error); | ||
| return NextResponse.json( | ||
| { | ||
| error: | ||
| error instanceof Error ? error.message : "Failed to send transaction", | ||
| }, | ||
| { status: 502 } | ||
| ); | ||
| } | ||
| } | ||
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,162 @@ | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { PublicKey } from "@solana/web3.js"; | ||
| import { | ||
| PAYMENTS_CLUSTER, | ||
| PAYMENTS_ENDPOINTS, | ||
| getPaymentsApiUrl, | ||
| getPaymentsTimeoutSignal, | ||
| } from "@/lib/payments"; | ||
| import { getPaymentsErrorMessage } from "@/lib/payments-errors"; | ||
| import { | ||
| STEALTH_POOL_MAX_DESTINATIONS, | ||
| getExactStealthHandleInput, | ||
| isStealthHandleInput, | ||
| } from "@/lib/stealth-handles"; | ||
|
|
||
| interface StealthPoolBuildRequest { | ||
| payer?: string; | ||
| authority?: string; | ||
| handle?: string; | ||
| destinations?: string[]; | ||
| splitAcrossKeys?: boolean; | ||
| } | ||
|
|
||
| export async function GET(request: NextRequest) { | ||
| try { | ||
| const handle = getExactStealthHandleInput( | ||
| request.nextUrl.searchParams.get("handle") ?? "" | ||
| ); | ||
| if (!handle || !isStealthHandleInput(handle)) { | ||
| return NextResponse.json( | ||
| { error: "Missing or invalid .block handle" }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| const upstreamUrl = new URL(getPaymentsApiUrl(PAYMENTS_ENDPOINTS.stealthPool)); | ||
| upstreamUrl.searchParams.set("handle", handle); | ||
| if (PAYMENTS_CLUSTER) { | ||
| upstreamUrl.searchParams.set("cluster", PAYMENTS_CLUSTER); | ||
| } | ||
|
|
||
| const upstreamRes = await fetch(upstreamUrl, { | ||
| method: "GET", | ||
| signal: getPaymentsTimeoutSignal(), | ||
| cache: "no-store", | ||
| }); | ||
|
|
||
| const responseBody = await upstreamRes.json().catch(() => null); | ||
| if (!upstreamRes.ok) { | ||
| return NextResponse.json( | ||
| { | ||
| error: getPaymentsErrorMessage(upstreamRes.status, responseBody), | ||
| details: responseBody, | ||
| }, | ||
| { status: upstreamRes.status } | ||
| ); | ||
| } | ||
|
|
||
| return NextResponse.json(responseBody); | ||
| } catch (error) { | ||
| console.error("Payments stealth pool status error:", error); | ||
| return NextResponse.json( | ||
| { | ||
| error: | ||
| error instanceof Error | ||
| ? error.message | ||
| : "Failed to fetch stealth pool status", | ||
| }, | ||
| { status: 500 } | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export async function POST(request: NextRequest) { | ||
| try { | ||
| const authHeader = request.headers.get("authorization"); | ||
| if (!authHeader?.startsWith("Bearer ")) { | ||
| return NextResponse.json( | ||
| { error: "Missing private-session auth token" }, | ||
| { status: 401 } | ||
| ); | ||
| } | ||
|
snawaz marked this conversation as resolved.
|
||
|
|
||
| const body = (await request.json()) as StealthPoolBuildRequest; | ||
| const handle = | ||
| typeof body.handle === "string" | ||
| ? getExactStealthHandleInput(body.handle) | ||
| : ""; | ||
|
|
||
| if ( | ||
| typeof body.payer !== "string" || | ||
| typeof body.authority !== "string" || | ||
| !handle || | ||
| !isStealthHandleInput(handle) || | ||
| !Array.isArray(body.destinations) || | ||
| body.destinations.length < 1 || | ||
| body.destinations.length > STEALTH_POOL_MAX_DESTINATIONS || | ||
| (body.splitAcrossKeys !== undefined && | ||
| typeof body.splitAcrossKeys !== "boolean") | ||
| ) { | ||
| return NextResponse.json( | ||
| { error: "Missing or invalid stealth pool parameters" }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| try { | ||
| new PublicKey(body.payer); | ||
| new PublicKey(body.authority); | ||
| body.destinations.forEach((destination) => new PublicKey(destination)); | ||
| } catch { | ||
| return NextResponse.json( | ||
| { error: "Invalid payer, authority, or destination public key" }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| const upstreamRes = await fetch(getPaymentsApiUrl(PAYMENTS_ENDPOINTS.stealthPool), { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| Authorization: authHeader, | ||
| }, | ||
| body: JSON.stringify({ | ||
| payer: body.payer, | ||
| authority: body.authority, | ||
| handle, | ||
| destinations: body.destinations, | ||
| ...(body.splitAcrossKeys !== undefined | ||
| ? { splitAcrossKeys: body.splitAcrossKeys } | ||
| : {}), | ||
| ...(PAYMENTS_CLUSTER ? { cluster: PAYMENTS_CLUSTER } : {}), | ||
| }), | ||
| signal: getPaymentsTimeoutSignal(), | ||
| cache: "no-store", | ||
| }); | ||
|
|
||
| const responseBody = await upstreamRes.json().catch(() => null); | ||
| if (!upstreamRes.ok) { | ||
| return NextResponse.json( | ||
| { | ||
| error: getPaymentsErrorMessage(upstreamRes.status, responseBody), | ||
| details: responseBody, | ||
| }, | ||
| { status: upstreamRes.status } | ||
| ); | ||
| } | ||
|
|
||
| return NextResponse.json(responseBody); | ||
| } catch (error) { | ||
| console.error("Payments stealth pool build error:", error); | ||
| return NextResponse.json( | ||
| { | ||
| error: | ||
| error instanceof Error | ||
| ? error.message | ||
| : "Failed to build stealth pool transaction", | ||
| }, | ||
| { status: 500 } | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.