ws402
Version:
WebSocket implementation of X402 protocol for pay-as-you-go digital resources with automatic refunds
96 lines (95 loc) • 2.68 kB
TypeScript
import { PaymentProvider, PaymentVerification } from '../types';
export interface BasePaymentProviderConfig {
/** Base RPC endpoint URL */
rpcEndpoint: string;
/** Merchant wallet address to receive payments */
merchantWallet: string;
/** Merchant private key for signing refund transactions */
merchantPrivateKey?: string;
/** Network: 'base' | 'base-goerli' | 'base-sepolia' */
network?: 'base' | 'base-goerli' | 'base-sepolia';
/** Conversion rate: wei to native units */
conversionRate?: number;
/** ERC20 token address (optional, for token payments) */
erc20Token?: string;
/** Payment timeout in milliseconds */
paymentTimeout?: number;
/** Chain ID */
chainId?: number;
/** Enable automatic refunds (requires merchantPrivateKey) */
autoRefund?: boolean;
}
/**
* Base Blockchain Payment Provider for WS402
*
* Supports:
* - Native ETH payments on Base
* - ERC20 token payments
* - On-chain payment verification
* - Automatic refunds
*/
export declare class BasePaymentProvider implements PaymentProvider {
private provider;
private merchantWallet;
private wallet?;
private config;
private pendingPayments;
constructor(config: BasePaymentProviderConfig);
/**
* Check merchant wallet balance
*/
private checkWalletBalance;
/**
* Generate payment details for Base blockchain
*/
generatePaymentDetails(amount: number): any;
/**
* Verify payment on Base blockchain
*/
verifyPayment(proof: any): Promise<PaymentVerification>;
/**
* Issue refund via Base blockchain transaction
*/
issueRefund(proof: any, amount: number): Promise<void>;
/**
* Verify ERC20 token transfer
*/
private verifyERC20Transfer;
/**
* Generate unique reference for payment tracking
*/
private generateReference;
/**
* Validate amount is positive
*/
private validateAmount;
/**
* Log payment activity
*/
private log;
/**
* Get pending payment info
*/
getPendingPayment(reference: string): {
amount: number;
amountETH: string;
timestamp: number;
recipient: string;
} | undefined;
/**
* Clean up expired pending payments
*/
cleanupExpiredPayments(): number;
/**
* Get connection info
*/
getConnectionInfo(): {
rpcEndpoint: string;
network: "base" | "base-goerli" | "base-sepolia";
chainId: number;
merchantWallet: string;
erc20Token: string | undefined;
autoRefundEnabled: boolean;
walletConnected: boolean;
};
}