|
| 1 | +import 'dart:async'; |
| 2 | +import 'package:flutter/foundation.dart'; |
| 3 | +import '../models/wallet.dart'; |
| 4 | +import '../models/network_config.dart'; |
| 5 | +import '../adapters/fuego_node_adapter.dart'; |
| 6 | +import '../adapters/fuego_wallet_adapter_native.dart'; |
| 7 | +import '../services/wallet_daemon_service.dart'; |
| 8 | + |
| 9 | +/// Hybrid wallet provider that uses native crypto when available |
| 10 | +/// and falls back to RPC for blockchain sync |
| 11 | +class WalletProviderHybrid extends ChangeNotifier { |
| 12 | + final FuegoNodeAdapter _nodeAdapter; |
| 13 | + final FuegoWalletAdapterNative _walletAdapter; |
| 14 | + |
| 15 | + Wallet? _wallet; |
| 16 | + List<WalletTransaction> _transactions = []; |
| 17 | + bool _isLoading = false; |
| 18 | + bool _isConnected = false; |
| 19 | + bool _isSyncing = false; |
| 20 | + String? _error; |
| 21 | + Timer? _syncTimer; |
| 22 | + NetworkConfig _networkConfig = NetworkConfig.mainnet; |
| 23 | + |
| 24 | + // Native crypto status |
| 25 | + bool _useNativeCrypto = false; |
| 26 | + |
| 27 | + WalletProviderHybrid({ |
| 28 | + FuegoNodeAdapter? nodeAdapter, |
| 29 | + FuegoWalletAdapterNative? walletAdapter, |
| 30 | + }) : _nodeAdapter = nodeAdapter ?? FuegoNodeAdapter.instance, |
| 31 | + _walletAdapter = walletAdapter ?? FuegoWalletAdapterNative.instance { |
| 32 | + _init(); |
| 33 | + } |
| 34 | + |
| 35 | + Future<void> _init() async { |
| 36 | + // Try to initialize native crypto |
| 37 | + _useNativeCrypto = await _walletAdapter.initNativeCrypto(); |
| 38 | + |
| 39 | + // Listen to node adapter events |
| 40 | + _listenToNodeEvents(); |
| 41 | + |
| 42 | + // Listen to wallet adapter events |
| 43 | + _listenToWalletEvents(); |
| 44 | + } |
| 45 | + |
| 46 | + void _listenToNodeEvents() { |
| 47 | + // Handle node adapter events |
| 48 | + } |
| 49 | + |
| 50 | + void _listenToWalletEvents() { |
| 51 | + // Handle wallet adapter events for UI updates |
| 52 | + } |
| 53 | + |
| 54 | + // Getters |
| 55 | + Wallet? get wallet => _wallet; |
| 56 | + List<WalletTransaction> get transactions => _transactions; |
| 57 | + bool get isLoading => _isLoading; |
| 58 | + bool get isConnected => _isConnected; |
| 59 | + bool get isSyncing => _isSyncing; |
| 60 | + String? get error => _error; |
| 61 | + NetworkConfig get networkConfig => _networkConfig; |
| 62 | + bool get useNativeCrypto => _useNativeCrypto; |
| 63 | + |
| 64 | + bool get hasWallet => _wallet != null; |
| 65 | + bool get isWalletSynced => _wallet?.synced ?? false; |
| 66 | + double get syncProgress => _wallet?.syncProgress ?? 0.0; |
| 67 | + |
| 68 | + /// Create a new wallet using native crypto (if available) or RPC |
| 69 | + Future<bool> createWallet({String? password}) async { |
| 70 | + try { |
| 71 | + _setLoading(true); |
| 72 | + |
| 73 | + bool success; |
| 74 | + if (_useNativeCrypto) { |
| 75 | + success = await _walletAdapter.createWalletNative( |
| 76 | + password: password, |
| 77 | + onEvent: _handleWalletEvent, |
| 78 | + ); |
| 79 | + } else { |
| 80 | + // Fall back to RPC |
| 81 | + await _startWalletDaemon(); |
| 82 | + success = await _walletAdapter.createWalletNative(password: password); |
| 83 | + } |
| 84 | + |
| 85 | + if (success) { |
| 86 | + await _loadWalletData(); |
| 87 | + _setError(null); |
| 88 | + return true; |
| 89 | + } |
| 90 | + |
| 91 | + return false; |
| 92 | + } catch (e) { |
| 93 | + _setError('Failed to create wallet: $e'); |
| 94 | + return false; |
| 95 | + } finally { |
| 96 | + _setLoading(false); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /// Create wallet from mnemonic seed phrase |
| 101 | + Future<bool> createWalletFromMnemonic({ |
| 102 | + required String mnemonic, |
| 103 | + String? password, |
| 104 | + }) async { |
| 105 | + try { |
| 106 | + _setLoading(true); |
| 107 | + |
| 108 | + // First, try to derive private keys from mnemonic using native crypto |
| 109 | + if (_useNativeCrypto) { |
| 110 | + // TODO: Implement mnemonic-to-keys conversion |
| 111 | + // This will call fuego_mnemonic_to_key FFI function |
| 112 | + } |
| 113 | + |
| 114 | + // Create wallet with keys |
| 115 | + bool success = await _walletAdapter.createWithKeysNative( |
| 116 | + viewKey: 'TODO', |
| 117 | + spendKey: 'TODO', |
| 118 | + password: password, |
| 119 | + onEvent: _handleWalletEvent, |
| 120 | + ); |
| 121 | + |
| 122 | + if (success) { |
| 123 | + await _loadWalletData(); |
| 124 | + _setError(null); |
| 125 | + return true; |
| 126 | + } |
| 127 | + |
| 128 | + return false; |
| 129 | + } catch (e) { |
| 130 | + _setError('Failed to create wallet from mnemonic: $e'); |
| 131 | + return false; |
| 132 | + } finally { |
| 133 | + _setLoading(false); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /// Open existing wallet |
| 138 | + Future<bool> openWallet({required String walletPath, String? password}) async { |
| 139 | + try { |
| 140 | + _setLoading(true); |
| 141 | + |
| 142 | + // Start wallet daemon if not running |
| 143 | + await _startWalletDaemon(); |
| 144 | + |
| 145 | + // TODO: Implement wallet opening via adapter |
| 146 | + await _loadWalletData(); |
| 147 | + |
| 148 | + _setError(null); |
| 149 | + return true; |
| 150 | + } catch (e) { |
| 151 | + _setError('Failed to open wallet: $e'); |
| 152 | + return false; |
| 153 | + } finally { |
| 154 | + _setLoading(false); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + /// Get mnemonic seed phrase for backup |
| 159 | + Future<String?> getMnemonicSeed() async { |
| 160 | + if (!_useNativeCrypto) { |
| 161 | + // Fall back to RPC if native crypto not available |
| 162 | + return await _getMnemonicFromRPC(); |
| 163 | + } |
| 164 | + |
| 165 | + // TODO: Call fuego_key_to_mnemonic FFI function |
| 166 | + return null; |
| 167 | + } |
| 168 | + |
| 169 | + Future<String?> _getMnemonicFromRPC() async { |
| 170 | + // Implement RPC-based mnemonic retrieval |
| 171 | + return null; |
| 172 | + } |
| 173 | + |
| 174 | + /// Send transaction |
| 175 | + Future<bool> sendTransaction({ |
| 176 | + required String recipient, |
| 177 | + required int amount, |
| 178 | + String? paymentId, |
| 179 | + }) async { |
| 180 | + try { |
| 181 | + if (!hasWallet) { |
| 182 | + _setError('No wallet loaded'); |
| 183 | + return false; |
| 184 | + } |
| 185 | + |
| 186 | + final txHash = await _walletAdapter.sendTransactionNative( |
| 187 | + destinations: {recipient: amount}, |
| 188 | + paymentId: paymentId, |
| 189 | + ); |
| 190 | + |
| 191 | + // Refresh wallet data |
| 192 | + await _loadWalletData(); |
| 193 | + |
| 194 | + return true; |
| 195 | + } catch (e) { |
| 196 | + _setError('Failed to send transaction: $e'); |
| 197 | + return false; |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + /// Start wallet daemon for blockchain sync |
| 202 | + Future<void> _startWalletDaemon() async { |
| 203 | + await WalletDaemonService.startWalletd(); |
| 204 | + _isConnected = true; |
| 205 | + notifyListeners(); |
| 206 | + } |
| 207 | + |
| 208 | + /// Load wallet data after operations |
| 209 | + Future<void> _loadWalletData() async { |
| 210 | + // TODO: Fetch wallet data from adapter |
| 211 | + _updateWallet(); |
| 212 | + } |
| 213 | + |
| 214 | + void _updateWallet() { |
| 215 | + notifyListeners(); |
| 216 | + } |
| 217 | + |
| 218 | + void _handleWalletEvent(WalletEvent event) { |
| 219 | + switch (event.type) { |
| 220 | + case WalletEventType.opened: |
| 221 | + case WalletEventType.created: |
| 222 | + _loadWalletData(); |
| 223 | + break; |
| 224 | + case WalletEventType.transactionCreated: |
| 225 | + _loadWalletData(); |
| 226 | + break; |
| 227 | + case WalletEventType.synchronizationProgress: |
| 228 | + _setSyncing(true); |
| 229 | + // Update sync progress from event data |
| 230 | + break; |
| 231 | + case WalletEventType.openFailed: |
| 232 | + case WalletEventType.creationFailed: |
| 233 | + _setError(event.message ?? 'Operation failed'); |
| 234 | + _setLoading(false); |
| 235 | + break; |
| 236 | + default: |
| 237 | + break; |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + void _setLoading(bool loading) { |
| 242 | + _isLoading = loading; |
| 243 | + notifyListeners(); |
| 244 | + } |
| 245 | + |
| 246 | + void _setSyncing(bool syncing) { |
| 247 | + _isSyncing = syncing; |
| 248 | + notifyListeners(); |
| 249 | + } |
| 250 | + |
| 251 | + void _setError(String? error) { |
| 252 | + _error = error; |
| 253 | + notifyListeners(); |
| 254 | + } |
| 255 | + |
| 256 | + @override |
| 257 | + void dispose() { |
| 258 | + _syncTimer?.cancel(); |
| 259 | + _walletAdapter.dispose(); |
| 260 | + super.dispose(); |
| 261 | + } |
| 262 | +} |
| 263 | + |
0 commit comments