Wallets
API reference for managing test wallets in x402test.
getWallet()
Section titled “getWallet()”Gets or creates a test wallet with auto-funded USDC.
import { getWallet } from "x402test";
const wallet = await getWallet();
console.log("Address:", wallet.publicKey.toBase58());console.log("Balance:", wallet.balance, "USDC");console.log("Token Account:", wallet.tokenAccount.toBase58());Returns: Promise<TestWallet>
Notes:
- Creates wallet on first call
- Automatically funds with 10 SOL and 1000 USDC
- Saves wallet to
.x402test-wallets.json - Reuses existing wallet on subsequent calls
TestWallet
Section titled “TestWallet”interface TestWallet { keypair: Keypair; publicKey: PublicKey; tokenAccount: PublicKey; usdcMint: PublicKey; balance: number;}Properties:
keypair(Keypair): Solana keypair for signingpublicKey(PublicKey): Wallet public keytokenAccount(PublicKey): USDC token account addressusdcMint(PublicKey): USDC mint addressbalance(number): USDC balance
getUsdcMint()
Section titled “getUsdcMint()”Gets the USDC mint address.
import { getUsdcMint } from "x402test";
const usdcMint = getUsdcMint();console.log("USDC Mint:", usdcMint.toBase58());Returns: PublicKey
Throws: Error if USDC mint not initialized
resetWallets()
Section titled “resetWallets()”Resets all wallets and creates new ones.
import { resetWallets } from "x402test";
await resetWallets();console.log("Wallets reset - new wallets created");Returns: Promise<void>
Notes:
- Deletes
.x402test-wallets.json - Next
getWallet()call creates fresh wallets - Useful for testing isolation
Using Wallets
Section titled “Using Wallets”Making Payments
Section titled “Making Payments”import { getWallet, createPayment } from "x402test";
const wallet = await getWallet();const signature = await createPayment(wallet, requirements);Checking Balance
Section titled “Checking Balance”const wallet = await getWallet();
if (wallet.balance < 1) { console.warn("Low balance:", wallet.balance, "USDC");}Multiple Wallets
Section titled “Multiple Wallets”import { getWallet, resetWallets } from "x402test";
// Get first walletconst wallet1 = await getWallet();console.log("Wallet 1:", wallet1.publicKey.toBase58());
// Reset and get new walletawait resetWallets();const wallet2 = await getWallet();console.log("Wallet 2:", wallet2.publicKey.toBase58());Wallet Persistence
Section titled “Wallet Persistence”Wallets are saved to .x402test-wallets.json:
{ "wallets": [ { "publicKey": "FcxKSp7YxqYXdq...", "secretKey": [...], "tokenAccounts": { "USDC": "EPjFWdd5AufqSSqeM2..." } } ], "mints": { "USDC": "EPjFWdd5AufqSSqeM2..." }}Security Note: This file contains private keys. Never commit to version control!
Add to .gitignore:
.x402test-wallets.jsonWallet Configuration
Section titled “Wallet Configuration”Wallets are automatically configured with:
- SOL Balance: 10 SOL (for transaction fees)
- USDC Balance: 1000 USDC (for payments)
- Network: Solana devnet or localnet
- Token Program: SPL Token Program
Manual Wallet Usage
Section titled “Manual Wallet Usage”Creating Transactions
Section titled “Creating Transactions”import { getWallet } from "x402test";import { Transaction, SystemProgram } from "@solana/web3.js";import { getConnection } from "x402test";
const wallet = await getWallet();const connection = getConnection();
// Create custom transactionconst transaction = new Transaction().add( SystemProgram.transfer({ fromPubkey: wallet.publicKey, toPubkey: recipient, lamports: 1000000, }));
// Sign and sendconst signature = await sendAndConfirmTransaction(connection, transaction, [ wallet.keypair,]);Token Operations
Section titled “Token Operations”import { getWallet, getUsdcMint } from "x402test";import { getAccount } from "@solana/spl-token";import { getConnection } from "x402test";
const wallet = await getWallet();const connection = getConnection();
// Get token account infoconst tokenAccount = await getAccount(connection, wallet.tokenAccount);
console.log("Balance:", tokenAccount.amount);console.log("Mint:", tokenAccount.mint.toBase58());console.log("Owner:", tokenAccount.owner.toBase58());Connection Management
Section titled “Connection Management”getConnection()
Section titled “getConnection()”Gets the Solana connection instance.
import { getConnection } from "x402test";
const connection = getConnection();const balance = await connection.getBalance(wallet.publicKey);Parameters:
rpcUrl(string, optional): Custom RPC URL
Returns: Connection
setRpcUrl()
Section titled “setRpcUrl()”Changes the RPC URL.
import { setRpcUrl } from "x402test";
setRpcUrl("https://api.devnet.solana.com");Parameters:
rpcUrl(string): New RPC URL
getRpcUrl()
Section titled “getRpcUrl()”Gets the current RPC URL.
import { getRpcUrl } from "x402test";
console.log("RPC URL:", getRpcUrl());Returns: string
Complete Example
Section titled “Complete Example”import { getWallet, getUsdcMint, createPayment, verifyPayment, resetWallets,} from "x402test";
async function testWallets() { // Get wallet const wallet = await getWallet(); console.log("Address:", wallet.publicKey.toBase58()); console.log("Balance:", wallet.balance, "USDC");
// Get USDC mint const usdcMint = getUsdcMint(); console.log("USDC Mint:", usdcMint.toBase58());
// Create payment const signature = await createPayment(wallet, requirements); console.log("Payment signature:", signature);
// Verify payment const result = await verifyPayment(signature, recipient, amount, usdcMint);
console.log("Verified:", result.isValid);
// Reset for next test await resetWallets();}Troubleshooting
Section titled “Troubleshooting”Insufficient Balance
Section titled “Insufficient Balance”const wallet = await getWallet();
if (wallet.balance < requiredAmount) { // Reset to get fresh funded wallet await resetWallets(); const newWallet = await getWallet(); console.log("New balance:", newWallet.balance);}Connection Issues
Section titled “Connection Issues”import { setRpcUrl, getConnection } from "x402test";
// Try different RPCsetRpcUrl("https://api.devnet.solana.com");
const connection = getConnection();try { await connection.getLatestBlockhash(); console.log("Connection OK");} catch (error) { console.error("Connection failed:", error);}Wallet File Corruption
Section titled “Wallet File Corruption”rm .x402test-wallets.jsonThen run your tests again to create fresh wallets.
Best Practices
Section titled “Best Practices”- Add to .gitignore: Never commit wallet files
- Reset Between Tests: Use
resetWallets()for test isolation - Check Balances: Verify sufficient funds before payments
- Use Local Validator: Faster and more reliable than devnet
- Clean Up: Delete wallet files after testing
Next Steps
Section titled “Next Steps”- Payment Methods - Creating payments
- Verification - Verifying payments
- Examples - Complete examples