UNPKG

@gorbchain-xyz/chaindecode

Version:

GorbchainSDK V1.3+ - Complete Solana development toolkit with advanced cryptography, messaging, and collaboration features. Build secure applications with blockchain, DeFi, and end-to-end encryption.

232 lines (231 loc) 7.13 kB
/** * Wallet Integration Utilities - Universal wallet connection and management * * This module provides comprehensive wallet integration for: * - Solana web3 providers (Phantom, Solflare, etc.) * - Deep link wallet providers * - Custom injected scripts * - WalletConnect protocol * - Hardware wallets */ import type { GorbchainSDK } from '../sdk/GorbchainSDK.js'; /** * Supported wallet types */ export type WalletType = 'trashpack' | 'phantom' | 'solflare' | 'backpack' | 'glow' | 'slope' | 'sollet' | 'walletconnect' | 'ledger' | 'custom' | 'injected'; /** * Wallet connection status */ export type WalletStatus = 'disconnected' | 'connecting' | 'connected' | 'error' | 'locked'; /** * Wallet provider interface */ export interface WalletProvider { /** Whether the wallet is connected */ isConnected: boolean; /** Public key of connected wallet */ publicKey: string | null; /** Sign transaction */ signTransaction: (transaction: any) => Promise<any>; /** Sign message */ signMessage: (message: Uint8Array) => Promise<Uint8Array>; /** Connect wallet */ connect: () => Promise<void>; /** Disconnect wallet */ disconnect: () => Promise<void>; /** Wallet-specific features */ features?: string[]; } /** * Rich wallet information with portfolio and metadata */ export interface RichWallet { /** Wallet address */ address: string; /** Wallet type */ type: WalletType; /** Wallet name */ name: string; /** Connection status */ status: WalletStatus; /** Wallet provider instance */ provider?: WalletProvider; /** Portfolio summary */ portfolio: { /** Total SOL balance */ solBalance: number; /** Formatted SOL balance */ solBalanceFormatted: string; /** Total USD value (if available) */ totalValueUsd?: number; /** Number of tokens */ totalTokens: number; /** Number of NFTs */ totalNFTs: number; /** Number of recent transactions */ recentTransactions: number; /** Top token holdings */ topTokens: Array<{ symbol: string; balance: string; valueUsd?: number; }>; }; /** Wallet metadata */ metadata: { /** Wallet icon URL */ icon?: string; /** Wallet website */ website?: string; /** Supported features */ features: string[]; /** Last connected timestamp */ lastConnected?: number; /** Connection count */ connectionCount: number; }; /** Network information */ network: { /** Current network */ current: string; /** Supported networks */ supported: string[]; /** Custom RPC endpoints */ customRPC?: string[]; }; } /** * Wallet discovery result */ export interface WalletDiscovery { /** Available wallets */ available: Array<{ type: WalletType; name: string; icon?: string; installed: boolean; deepLink?: string; downloadUrl?: string; }>; /** Recommended wallets */ recommended: WalletType[]; /** Previously connected wallets */ previouslyConnected: WalletType[]; } /** * Universal wallet manager for comprehensive wallet integration * * @example * ```typescript * const sdk = new GorbchainSDK({ rpcEndpoint: 'https://rpc.gorbchain.xyz' }); * const walletManager = new UniversalWalletManager(sdk); * * // Discover available wallets * const discovery = await walletManager.discoverWallets(); * console.log(`Found ${discovery.available.length} wallets`); * * // Connect to a specific wallet * const wallet = await walletManager.connectWallet('phantom'); * console.log(`Connected to ${wallet.name}: ${wallet.address}`); * console.log(`Portfolio value: $${wallet.portfolio.totalValueUsd}`); * * // Auto-connect to best available wallet * const autoWallet = await walletManager.autoConnect({ * preferredWallets: ['phantom', 'solflare'], * includePortfolio: true * }); * ``` */ export declare class UniversalWalletManager { private sdk; private connectedWallet; private providers; private eventListeners; constructor(sdk: GorbchainSDK); /** * Discover all available wallets on the device/browser * * @param options - Discovery options * @returns Promise resolving to wallet discovery results */ discoverWallets(options?: { /** Whether to check for deep link support */ includeDeepLinks?: boolean; /** Whether to check for mobile wallets */ includeMobile?: boolean; /** Whether to check for hardware wallets */ includeHardware?: boolean; }): Promise<WalletDiscovery>; /** * Connect to a specific wallet and get rich wallet information * * @param walletType - Type of wallet to connect to * @param options - Connection options * @returns Promise resolving to rich wallet information */ connectWallet(walletType: WalletType, options?: { /** Whether to include portfolio analysis */ includePortfolio?: boolean; /** Whether to remember this connection */ rememberConnection?: boolean; /** Custom connection parameters */ connectionParams?: Record<string, any>; }): Promise<RichWallet>; /** * Auto-connect to the best available wallet * * @param options - Auto-connection options * @returns Promise resolving to connected wallet or null */ autoConnect(options?: { /** Preferred wallet types in order */ preferredWallets?: WalletType[]; /** Whether to include portfolio */ includePortfolio?: boolean; /** Whether to only try previously connected wallets */ onlyPrevious?: boolean; }): Promise<RichWallet | null>; /** * Disconnect from current wallet */ disconnect(): Promise<void>; /** * Get current connected wallet */ getConnectedWallet(): RichWallet | null; /** * Sign transaction with connected wallet */ signTransaction(transaction: any): Promise<any>; /** * Sign message with connected wallet */ signMessage(message: string | Uint8Array): Promise<Uint8Array>; /** * Add event listener */ on(event: string, listener: Function): void; /** * Remove event listener */ off(event: string, listener: Function): void; private initializeProviders; private checkWalletInstalled; private checkLedgerSupport; private connectionCache; private readonly STORAGE_KEY; private wasWalletPreviouslyConnected; private rememberWalletConnection; private getCachedConnections; private loadConnectionsFromStorage; private saveConnections; private getPreviousConnections; private getConnectionCount; private getWalletProvider; private getWalletFeatures; private loadWalletPortfolio; private getWalletName; private getWalletIcon; private getWalletWebsite; private emit; }