-
Notifications
You must be signed in to change notification settings - Fork 33
feat: Experimenta pay x420 #1636
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
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
083595f
support for faremeter
maxsch-xmint a498c43
refactor to support other facilitators
maxsch-xmint 083dec3
add http options
maxsch-xmint c62efbb
add changeset
maxsch-xmint c1a2c6c
rever tsconfig giles
maxsch-xmint 253b63e
mend
maxsch-xmint 65501f1
rever .d.ts file
maxsch-xmint d7e5d17
break circular dependency
maxsch-xmint 622ddfe
break circular dependency
maxsch-xmint f806001
Update apps/wallets/quickstart-devkit/app/api/x402/wallet/route.ts
maxsch-xmint 6c67cac
lint
maxsch-xmint c306cb3
lint
maxsch-xmint 3e528fc
lint
maxsch-xmint da395c8
return body
maxsch-xmint 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
Some comments aren't visible on the classic Files Changed page.
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,14 @@ | ||
| --- | ||
| "@crossmint/client-sdk-walletconnect": patch | ||
| "@crossmint/client-sdk-react-native-ui": patch | ||
| "@crossmint/wallets-quickstart-devkit": patch | ||
| "expo-demo": patch | ||
| "@crossmint/client-sdk-react-ui": patch | ||
| "@crossmint/auth-ssr-nextjs-demo": patch | ||
| "@crossmint/client-sdk-nextjs-starter": patch | ||
| "@crossmint/client-sdk-base": patch | ||
| "@crossmint/common-sdk-base": patch | ||
| "@crossmint/wallets-sdk": patch | ||
| --- | ||
|
|
||
| Add experimental_payX402 to wallets sdk |
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
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,52 @@ | ||
| import { NextResponse } from "next/server"; | ||
| import { Keypair } from "@solana/web3.js"; | ||
| import { createCrossmint, CrossmintWallets, SolanaWallet } from "@crossmint/wallets-sdk"; | ||
|
|
||
| export async function POST(request: Request) { | ||
| try { | ||
| const { walletAddress, paymentUrl, chain } = await request.json(); | ||
|
|
||
| const serverKey = process.env.CROSSMINT_SERVER_API_KEY; | ||
| const adminPrivateKey = process.env.X402_ADMIN_PRIVATE_KEY; | ||
| const adminEmail = process.env.X402_ADMIN_EMAIL; | ||
|
|
||
| if (!serverKey || !adminPrivateKey || !adminEmail) { | ||
| return NextResponse.json( | ||
| { error: "Missing CROSSMINT_SERVER_API_KEY or X402_ADMIN_PRIVATE_KEY or X402_ADMIN_EMAIL env vars" }, | ||
| { status: 500 } | ||
| ); | ||
|
maxsch-xmint marked this conversation as resolved.
|
||
| } | ||
|
|
||
| const privateKeyBytes = new Uint8Array(Buffer.from(adminPrivateKey, "base64")); | ||
| const keypair = Keypair.fromSecretKey(privateKeyBytes); | ||
|
|
||
| const crossmint = createCrossmint({ apiKey: serverKey }); | ||
| const wallets = CrossmintWallets.from(crossmint); | ||
|
|
||
| const wallet = await wallets.getWallet(walletAddress, { | ||
| owner: `email:${adminEmail}`, | ||
| chain, | ||
| signer: { | ||
| type: "external-wallet", | ||
| address: keypair.publicKey.toBase58(), | ||
| onSignTransaction: async (tx) => { | ||
| tx.sign([keypair]); | ||
| return tx; | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| if (chain === "solana") { | ||
| const solanaWallet = SolanaWallet.from(wallet); | ||
| await solanaWallet.experimental_payX402(paymentUrl); | ||
| } else { | ||
| throw new Error(`X402 payments are not supported for chain ${chain}`); | ||
| } | ||
|
|
||
| return NextResponse.json({ success: true }); | ||
| } catch (err: any) { | ||
| const message = err?.message || String(err); | ||
| console.error("x402 pay error:", err); | ||
| return NextResponse.json({ success: false, error: message }, { status: 500 }); | ||
|
maxsch-xmint marked this conversation as resolved.
|
||
| } | ||
| } | ||
52 changes: 52 additions & 0 deletions
52
apps/wallets/quickstart-devkit/app/api/x402/wallet/route.ts
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,52 @@ | ||
| import { NextResponse } from "next/server"; | ||
| import { Keypair, type VersionedTransaction } from "@solana/web3.js"; | ||
| import { createCrossmint, CrossmintWallets } from "@crossmint/wallets-sdk"; | ||
|
|
||
| export async function POST() { | ||
| try { | ||
| const serverKey = process.env.CROSSMINT_SERVER_API_KEY; | ||
| const adminPrivateKey = process.env.X402_ADMIN_PRIVATE_KEY; | ||
| const adminEmail = process.env.X402_ADMIN_EMAIL; | ||
|
|
||
| if (!serverKey || !adminPrivateKey || !adminEmail) { | ||
| return NextResponse.json( | ||
| { error: "Missing CROSSMINT_SERVER_API_KEY or X402_ADMIN_PRIVATE_KEY or X402_ADMIN_EMAIL env vars" }, | ||
| { status: 500 } | ||
| ); | ||
| } | ||
|
maxsch-xmint marked this conversation as resolved.
|
||
|
|
||
| const privateKeyBytes = new Uint8Array(Buffer.from(adminPrivateKey, "base64")); | ||
| const keypair = Keypair.fromSecretKey(privateKeyBytes); | ||
|
|
||
| const crossmint = createCrossmint({ apiKey: serverKey }); | ||
| const wallets = CrossmintWallets.from(crossmint); | ||
|
|
||
| const signerConfig = { | ||
| type: "external-wallet" as const, | ||
| address: keypair.publicKey.toBase58(), | ||
| onSignTransaction: async (tx: VersionedTransaction) => { | ||
| tx.sign([keypair]); | ||
| return tx; | ||
| }, | ||
| }; | ||
|
|
||
| let wallet; | ||
| try { | ||
| wallet = await wallets.getWallet(keypair.publicKey.toBase58(), { | ||
| owner: `email:${adminEmail}`, | ||
| chain: "solana", | ||
| signer: signerConfig, | ||
| }); | ||
| } catch { | ||
| wallet = await wallets.createWallet({ | ||
| owner: `email:${adminEmail}`, | ||
| chain: "solana", | ||
| signer: signerConfig, | ||
| }); | ||
| } | ||
|
maxsch-xmint marked this conversation as resolved.
|
||
|
|
||
| return NextResponse.json({ address: wallet.address }); | ||
| } catch (err: any) { | ||
| return NextResponse.json({ error: err.message }, { status: 500 }); | ||
| } | ||
|
maxsch-xmint marked this conversation as resolved.
|
||
| } | ||
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,130 @@ | ||
| "use client"; | ||
|
|
||
| import { useState } from "react"; | ||
|
|
||
| export default function X402Page() { | ||
| const [chain, setChain] = useState("solana"); | ||
| const [paymentUrl, setPaymentUrl] = useState("http://localhost:8402/protected"); | ||
| const [walletAddress, setWalletAddress] = useState<string | null>(null); | ||
| const [walletLoading, setWalletLoading] = useState(false); | ||
| const [walletError, setWalletError] = useState<string | null>(null); | ||
|
|
||
| const [payStatus, setPayStatus] = useState<"idle" | "success" | "error">("idle"); | ||
| const [payLoading, setPayLoading] = useState(false); | ||
| const [payError, setPayError] = useState<string | null>(null); | ||
|
|
||
| async function handleCreateWallet() { | ||
| setWalletLoading(true); | ||
| setWalletError(null); | ||
| try { | ||
| const res = await fetch("/api/x402/wallet", { | ||
| method: "POST", | ||
| headers: { "Content-Type": "application/json" }, | ||
| body: JSON.stringify({ chain }), | ||
| }); | ||
| const data = await res.json(); | ||
| if (!res.ok) { | ||
| throw new Error(data.error ?? "Failed to create wallet"); | ||
| } | ||
| setWalletAddress(data.address); | ||
| } catch (err: any) { | ||
| setWalletError(err.message); | ||
| } finally { | ||
| setWalletLoading(false); | ||
| } | ||
| } | ||
|
|
||
| async function handlePay() { | ||
| if (!walletAddress) return; | ||
| setPayLoading(true); | ||
| setPayStatus("idle"); | ||
| setPayError(null); | ||
| try { | ||
| const res = await fetch("/api/x402/pay", { | ||
| method: "POST", | ||
| headers: { "Content-Type": "application/json" }, | ||
| body: JSON.stringify({ walletAddress, paymentUrl, chain }), | ||
| }); | ||
| const data = await res.json(); | ||
| if (data.success) { | ||
| setPayStatus("success"); | ||
| } else { | ||
| setPayStatus("error"); | ||
| setPayError(data.error ?? "Payment failed"); | ||
| } | ||
| } catch (err: any) { | ||
| setPayStatus("error"); | ||
| setPayError(err.message); | ||
| } finally { | ||
| setPayLoading(false); | ||
| } | ||
| } | ||
|
|
||
| const statusColor = payStatus === "success" ? "bg-green-500" : payStatus === "error" ? "bg-red-500" : "bg-gray-400"; | ||
|
|
||
| return ( | ||
| <div className="min-h-screen p-8 max-w-2xl mx-auto"> | ||
| <h1 className="text-2xl font-bold mb-8">X402 Payment Test Suite</h1> | ||
|
|
||
| <section className="mb-8 space-y-4"> | ||
| <h2 className="text-lg font-semibold">Setup</h2> | ||
|
|
||
| <div> | ||
| <label className="block text-sm font-medium mb-1">Chain</label> | ||
| <select | ||
| value={chain} | ||
| onChange={(e) => setChain(e.target.value)} | ||
| className="border rounded px-3 py-2 w-full" | ||
| > | ||
| <option value="solana">solana</option> | ||
| </select> | ||
| </div> | ||
|
|
||
| <div> | ||
| <label className="block text-sm font-medium mb-1">Payment URL</label> | ||
| <input | ||
| type="text" | ||
| value={paymentUrl} | ||
| onChange={(e) => setPaymentUrl(e.target.value)} | ||
| placeholder="https://..." | ||
| className="border rounded px-3 py-2 w-full" | ||
| /> | ||
| </div> | ||
|
|
||
| <button | ||
| onClick={handleCreateWallet} | ||
| disabled={walletLoading} | ||
| className="bg-blue-600 text-white px-4 py-2 rounded hover:bg-blue-700 disabled:opacity-50" | ||
| > | ||
| {walletLoading ? "Creating..." : "Create / Get Wallet"} | ||
| </button> | ||
|
|
||
| {walletAddress && ( | ||
| <p className="text-sm"> | ||
| Wallet address: <code className="bg-gray-100 px-1 rounded">{walletAddress}</code> | ||
| </p> | ||
| )} | ||
| {walletError && <p className="text-sm text-red-600">{walletError}</p>} | ||
| </section> | ||
|
|
||
| {walletAddress && ( | ||
| <section className="space-y-4"> | ||
| <h2 className="text-lg font-semibold">Test Suite</h2> | ||
|
|
||
| <div className="flex items-center gap-4"> | ||
| <button | ||
| onClick={handlePay} | ||
| disabled={payLoading} | ||
| className="bg-purple-600 text-white px-4 py-2 rounded hover:bg-purple-700 disabled:opacity-50" | ||
| > | ||
| {payLoading ? "Paying..." : "farameter/corbits"} | ||
| </button> | ||
| <span className={`inline-block w-4 h-4 rounded-full ${statusColor}`} /> | ||
| </div> | ||
|
|
||
| {payError && <p className="text-sm text-red-600">{payError}</p>} | ||
| </section> | ||
| )} | ||
| </div> | ||
| ); | ||
| } |
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
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
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
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
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
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.