@zendfi/sdk 0.8.4 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,402 @@
1
+ /**
2
+ * Wallet Connector
3
+ * Simplifies integration with Solana wallets (Phantom, Solflare, Backpack, etc.)
4
+ *
5
+ * @example
6
+ * ```typescript
7
+ * import { WalletConnector } from '@zendfi/sdk/helpers';
8
+ *
9
+ * // Auto-detect and connect
10
+ * const wallet = await WalletConnector.detectAndConnect();
11
+ * console.log(`Connected: ${wallet.address}`);
12
+ * console.log(`Provider: ${wallet.provider}`);
13
+ *
14
+ * // Sign transaction
15
+ * const result = await wallet.signTransaction(transaction);
16
+ *
17
+ * // Disconnect
18
+ * await wallet.disconnect();
19
+ * ```
20
+ */
21
+ interface ConnectedWallet {
22
+ address: string;
23
+ provider: 'phantom' | 'solflare' | 'backpack' | 'coinbase' | 'trust' | 'unknown';
24
+ publicKey: any;
25
+ signTransaction: (tx: any) => Promise<any>;
26
+ signAllTransactions: (txs: any[]) => Promise<any[]>;
27
+ signMessage: (message: Uint8Array) => Promise<{
28
+ signature: Uint8Array;
29
+ }>;
30
+ disconnect: () => Promise<void>;
31
+ isConnected: () => boolean;
32
+ raw: any;
33
+ }
34
+ interface WalletConnectorConfig {
35
+ /** Preferred wallet provider (if multiple detected) */
36
+ preferredProvider?: 'phantom' | 'solflare' | 'backpack' | 'coinbase' | 'trust';
37
+ /** Auto-connect on page load if previously connected */
38
+ autoConnect?: boolean;
39
+ /** Show connection UI if no wallet detected */
40
+ showInstallPrompt?: boolean;
41
+ /** Network (mainnet-beta, devnet, testnet) */
42
+ network?: string;
43
+ }
44
+ /**
45
+ * Wallet Connector
46
+ * Detects and connects to Solana wallets
47
+ */
48
+ declare class WalletConnector {
49
+ private static connectedWallet;
50
+ /**
51
+ * Detect and connect to a Solana wallet
52
+ */
53
+ static detectAndConnect(config?: WalletConnectorConfig): Promise<ConnectedWallet>;
54
+ /**
55
+ * Detect available Solana wallets
56
+ */
57
+ static detectWallets(): Array<'phantom' | 'solflare' | 'backpack' | 'coinbase' | 'trust'>;
58
+ /**
59
+ * Connect to a specific wallet provider
60
+ */
61
+ static connectToProvider(provider: 'phantom' | 'solflare' | 'backpack' | 'coinbase' | 'trust'): Promise<ConnectedWallet>;
62
+ /**
63
+ * Sign and submit a transaction
64
+ */
65
+ static signAndSubmit(transaction: any, wallet: ConnectedWallet, connection: any): Promise<{
66
+ signature: string;
67
+ }>;
68
+ /**
69
+ * Get current connected wallet
70
+ */
71
+ static getConnectedWallet(): ConnectedWallet | null;
72
+ /**
73
+ * Disconnect current wallet
74
+ */
75
+ static disconnect(): Promise<void>;
76
+ /**
77
+ * Listen for wallet connection changes
78
+ */
79
+ static onAccountChange(callback: (publicKey: any) => void): () => void;
80
+ /**
81
+ * Listen for wallet disconnection
82
+ */
83
+ static onDisconnect(callback: () => void): () => void;
84
+ /**
85
+ * Show install prompt UI
86
+ */
87
+ private static showInstallPrompt;
88
+ /**
89
+ * Check if wallet is installed
90
+ */
91
+ static isWalletInstalled(provider: 'phantom' | 'solflare' | 'backpack' | 'coinbase' | 'trust'): boolean;
92
+ /**
93
+ * Get wallet download URL
94
+ */
95
+ static getWalletUrl(provider: 'phantom' | 'solflare' | 'backpack' | 'coinbase' | 'trust'): string;
96
+ }
97
+ /**
98
+ * React Hook for Wallet Connection (optional)
99
+ * Export as separate module to avoid forcing React dependency
100
+ */
101
+ declare function createWalletHook(): () => {
102
+ wallet: any;
103
+ connecting: any;
104
+ error: any;
105
+ connect: (config?: WalletConnectorConfig) => Promise<void>;
106
+ disconnect: () => Promise<void>;
107
+ isConnected: boolean;
108
+ };
109
+
110
+ /**
111
+ * Transaction Polling Utilities
112
+ * Poll Solana transactions with exponential backoff
113
+ *
114
+ * @example
115
+ * ```typescript
116
+ * import { TransactionPoller } from '@zendfi/sdk/helpers';
117
+ *
118
+ * // Wait for confirmation
119
+ * const status = await TransactionPoller.waitForConfirmation(
120
+ * signature,
121
+ * { timeout: 60000, interval: 2000 }
122
+ * );
123
+ *
124
+ * if (status.confirmed) {
125
+ * console.log(`Confirmed in slot ${status.slot}`);
126
+ * }
127
+ * ```
128
+ */
129
+ interface TransactionStatus {
130
+ confirmed: boolean;
131
+ signature: string;
132
+ slot?: number;
133
+ blockTime?: number;
134
+ confirmations?: number;
135
+ error?: string;
136
+ }
137
+ interface PollingOptions {
138
+ /** Maximum time to wait in ms (default: 60000 = 1 minute) */
139
+ timeout?: number;
140
+ /** Initial polling interval in ms (default: 2000 = 2 seconds) */
141
+ interval?: number;
142
+ /** Maximum polling interval in ms (default: 10000 = 10 seconds) */
143
+ maxInterval?: number;
144
+ /** Maximum number of attempts (default: 30) */
145
+ maxAttempts?: number;
146
+ /** Commitment level (default: 'confirmed') */
147
+ commitment?: 'processed' | 'confirmed' | 'finalized';
148
+ /** RPC endpoint (optional, uses default if not provided) */
149
+ rpcUrl?: string;
150
+ }
151
+ /**
152
+ * Transaction Poller
153
+ * Poll transaction status with smart backoff
154
+ */
155
+ declare class TransactionPoller {
156
+ /**
157
+ * Wait for transaction confirmation
158
+ */
159
+ static waitForConfirmation(signature: string, options?: PollingOptions): Promise<TransactionStatus>;
160
+ /**
161
+ * Check transaction status via RPC
162
+ */
163
+ private static checkTransactionStatus;
164
+ /**
165
+ * Check if commitment level is reached
166
+ */
167
+ private static isCommitmentReached;
168
+ /**
169
+ * Get default RPC URL based on environment
170
+ */
171
+ private static getDefaultRpcUrl;
172
+ /**
173
+ * Poll multiple transactions in parallel
174
+ */
175
+ static waitForMultiple(signatures: string[], options?: PollingOptions): Promise<TransactionStatus[]>;
176
+ /**
177
+ * Get transaction details after confirmation
178
+ */
179
+ static getTransactionDetails(signature: string, rpcUrl?: string): Promise<any>;
180
+ /**
181
+ * Check if transaction exists on chain
182
+ */
183
+ static exists(signature: string, rpcUrl?: string): Promise<boolean>;
184
+ /**
185
+ * Get recent blockhash (useful for transaction building)
186
+ */
187
+ static getRecentBlockhash(rpcUrl?: string): Promise<{
188
+ blockhash: string;
189
+ lastValidBlockHeight: number;
190
+ }>;
191
+ /**
192
+ * Sleep utility
193
+ */
194
+ private static sleep;
195
+ }
196
+ /**
197
+ * Transaction Monitor
198
+ * Monitor transaction status with callbacks
199
+ */
200
+ declare class TransactionMonitor {
201
+ private monitors;
202
+ /**
203
+ * Start monitoring a transaction
204
+ */
205
+ monitor(signature: string, callbacks: {
206
+ onConfirmed?: (status: TransactionStatus) => void;
207
+ onFailed?: (status: TransactionStatus) => void;
208
+ onTimeout?: () => void;
209
+ }, options?: PollingOptions): void;
210
+ /**
211
+ * Stop monitoring a transaction
212
+ */
213
+ stopMonitoring(signature: string): void;
214
+ /**
215
+ * Stop all monitors
216
+ */
217
+ stopAll(): void;
218
+ /**
219
+ * Get active monitors
220
+ */
221
+ getActiveMonitors(): string[];
222
+ }
223
+
224
+ /**
225
+ * Development Tools & Utilities
226
+ * Debugging helpers for development and testing
227
+ *
228
+ * @example
229
+ * ```typescript
230
+ * import { DevTools } from '@zendfi/sdk/helpers';
231
+ *
232
+ * // Enable debug mode
233
+ * DevTools.enableDebugMode();
234
+ *
235
+ * // Create test session key
236
+ * const testKey = await DevTools.createTestSessionKey();
237
+ *
238
+ * // Mock wallet for testing
239
+ * const mockWallet = DevTools.mockWallet();
240
+ *
241
+ * // Log transaction flow
242
+ * DevTools.logTransactionFlow(paymentId);
243
+ * ```
244
+ */
245
+ interface MockWallet {
246
+ address: string;
247
+ publicKey: any;
248
+ signTransaction: (tx: any) => Promise<any>;
249
+ signMessage: (msg: Uint8Array) => Promise<{
250
+ signature: Uint8Array;
251
+ }>;
252
+ isConnected: () => boolean;
253
+ disconnect: () => Promise<void>;
254
+ }
255
+ interface TestSessionKey {
256
+ sessionKeyId: string;
257
+ sessionWallet: string;
258
+ privateKey: Uint8Array;
259
+ budget: number;
260
+ }
261
+ /**
262
+ * Development Tools
263
+ * Utilities for debugging and testing
264
+ */
265
+ declare class DevTools {
266
+ private static debugEnabled;
267
+ private static requestLog;
268
+ /**
269
+ * Enable debug mode (logs all API requests/responses)
270
+ */
271
+ static enableDebugMode(): void;
272
+ /**
273
+ * Disable debug mode
274
+ */
275
+ static disableDebugMode(): void;
276
+ /**
277
+ * Check if debug mode is enabled
278
+ */
279
+ static isDebugEnabled(): boolean;
280
+ /**
281
+ * Log API request
282
+ */
283
+ static logRequest(method: string, url: string, body?: any): void;
284
+ /**
285
+ * Log API response
286
+ */
287
+ static logResponse(method: string, url: string, status: number, data: any, duration?: number): void;
288
+ /**
289
+ * Get request log
290
+ */
291
+ static getRequestLog(): Array<{
292
+ timestamp: Date;
293
+ method: string;
294
+ url: string;
295
+ status?: number;
296
+ duration?: number;
297
+ }>;
298
+ /**
299
+ * Clear request log
300
+ */
301
+ static clearRequestLog(): void;
302
+ /**
303
+ * Create a test session key (devnet only)
304
+ */
305
+ static createTestSessionKey(): Promise<TestSessionKey>;
306
+ /**
307
+ * Create a mock wallet for testing
308
+ */
309
+ static mockWallet(address?: string): MockWallet;
310
+ /**
311
+ * Log transaction flow (visual diagram in console)
312
+ */
313
+ static logTransactionFlow(paymentId: string): void;
314
+ /**
315
+ * Log session key lifecycle
316
+ */
317
+ static logSessionKeyLifecycle(sessionKeyId: string): void;
318
+ /**
319
+ * Benchmark API request
320
+ */
321
+ static benchmarkRequest<T>(name: string, fn: () => Promise<T>): Promise<{
322
+ result: T;
323
+ durationMs: number;
324
+ }>;
325
+ /**
326
+ * Stress test (send multiple concurrent requests)
327
+ */
328
+ static stressTest<T>(name: string, fn: () => Promise<T>, concurrency?: number, iterations?: number): Promise<{
329
+ totalRequests: number;
330
+ successful: number;
331
+ failed: number;
332
+ avgDurationMs: number;
333
+ minDurationMs: number;
334
+ maxDurationMs: number;
335
+ }>;
336
+ /**
337
+ * Inspect ZendFi SDK configuration
338
+ */
339
+ static inspectConfig(client: any): void;
340
+ /**
341
+ * Generate test data
342
+ */
343
+ static generateTestData(): {
344
+ userWallet: string;
345
+ agentId: string;
346
+ sessionKeyId: string;
347
+ paymentId: string;
348
+ };
349
+ /**
350
+ * Check if running in development environment
351
+ */
352
+ private static isDevelopment;
353
+ /**
354
+ * Generate test Solana address
355
+ */
356
+ private static generateTestAddress;
357
+ /**
358
+ * Generate test ID with prefix
359
+ */
360
+ private static generateTestId;
361
+ /**
362
+ * Get Solana Web3.js
363
+ */
364
+ private static getSolanaWeb3;
365
+ }
366
+ /**
367
+ * Performance Monitor
368
+ * Track SDK performance metrics
369
+ */
370
+ declare class PerformanceMonitor {
371
+ private metrics;
372
+ /**
373
+ * Record a metric
374
+ */
375
+ record(name: string, value: number): void;
376
+ /**
377
+ * Get statistics for a metric
378
+ */
379
+ getStats(name: string): {
380
+ count: number;
381
+ avg: number;
382
+ min: number;
383
+ max: number;
384
+ p50: number;
385
+ p95: number;
386
+ p99: number;
387
+ } | null;
388
+ /**
389
+ * Get all metrics
390
+ */
391
+ getAllStats(): Record<string, ReturnType<typeof this.getStats>>;
392
+ /**
393
+ * Print report
394
+ */
395
+ printReport(): void;
396
+ /**
397
+ * Clear all metrics
398
+ */
399
+ clear(): void;
400
+ }
401
+
402
+ export { type ConnectedWallet, DevTools, type MockWallet, PerformanceMonitor, type PollingOptions, type TestSessionKey, TransactionMonitor, TransactionPoller, type TransactionStatus, WalletConnector, createWalletHook };