|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:io'; |
| 3 | +import 'dart:convert'; |
| 4 | +import 'package:flutter/foundation.dart'; |
| 5 | +import 'package:flutter/services.dart'; |
| 6 | +import 'package:path_provider/path_provider.dart'; |
| 7 | +import 'package:path/path.dart' as path; |
| 8 | + |
| 9 | +class WalletDaemonService { |
| 10 | + static Process? _walletdProcess; |
| 11 | + static bool _isRunning = false; |
| 12 | + static int _walletdPort = 8070; |
| 13 | + static String? _walletdPath; |
| 14 | + static String? _walletPath; |
| 15 | + static String? _daemonAddress; |
| 16 | + static int? _daemonPort; |
| 17 | + |
| 18 | + /// Initialize the wallet daemon service |
| 19 | + static Future<void> initialize({ |
| 20 | + required String daemonAddress, |
| 21 | + required int daemonPort, |
| 22 | + String? walletPath, |
| 23 | + }) async { |
| 24 | + _daemonAddress = daemonAddress; |
| 25 | + _daemonPort = daemonPort; |
| 26 | + _walletPath = walletPath; |
| 27 | + |
| 28 | + // Extract walletd binary |
| 29 | + _walletdPath = await _extractWalletdBinary(); |
| 30 | + |
| 31 | + debugPrint('WalletDaemonService initialized'); |
| 32 | + debugPrint('Daemon: $_daemonAddress:$_daemonPort'); |
| 33 | + debugPrint('Walletd binary: $_walletdPath'); |
| 34 | + debugPrint('Wallet path: $_walletPath'); |
| 35 | + } |
| 36 | + |
| 37 | + /// Extract the walletd binary from assets |
| 38 | + static Future<String> _extractWalletdBinary() async { |
| 39 | + final Directory tempDir = await getTemporaryDirectory(); |
| 40 | + final String binaryName = Platform.isWindows |
| 41 | + ? 'fuego-walletd-windows.exe' |
| 42 | + : Platform.isMacOS |
| 43 | + ? 'fuego-walletd-macos' |
| 44 | + : 'fuego-walletd-linux'; |
| 45 | + |
| 46 | + final File binaryFile = File(path.join(tempDir.path, 'fuego-walletd')); |
| 47 | + |
| 48 | + // Extract from assets if not already extracted |
| 49 | + if (!await binaryFile.exists()) { |
| 50 | + await binaryFile.create(recursive: true); |
| 51 | + await binaryFile.writeAsBytes( |
| 52 | + await rootBundle.load('assets/bin/$binaryName').then((data) => data.buffer.asUint8List()) |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + // Set executable permissions for non-Windows platforms |
| 57 | + if (!Platform.isWindows) { |
| 58 | + await Process.run('chmod', ['+x', binaryFile.path]); |
| 59 | + } |
| 60 | + |
| 61 | + return binaryFile.path; |
| 62 | + } |
| 63 | + |
| 64 | + /// Start the wallet daemon |
| 65 | + static Future<bool> startWalletd({ |
| 66 | + String? walletPath, |
| 67 | + String? password, |
| 68 | + }) async { |
| 69 | + if (_isRunning) { |
| 70 | + debugPrint('Walletd is already running'); |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + if (_walletdPath == null) { |
| 75 | + throw Exception('WalletDaemonService not initialized'); |
| 76 | + } |
| 77 | + |
| 78 | + try { |
| 79 | + // Prepare command arguments |
| 80 | + final List<String> args = [ |
| 81 | + '--daemon-address', '$_daemonAddress', |
| 82 | + '--daemon-port', '$_daemonPort.toString()', |
| 83 | + '--rpc-bind-port', '_walletdPort.toString()', |
| 84 | + '--log-level', '1', // Info level |
| 85 | + '--non-interactive', |
| 86 | + ]; |
| 87 | + |
| 88 | + // Add wallet path if provided |
| 89 | + if (walletPath != null) { |
| 90 | + args.addAll(['--wallet-file', walletPath]); |
| 91 | + } |
| 92 | + |
| 93 | + // Add password if provided |
| 94 | + if (password != null) { |
| 95 | + args.addAll(['--password', password]); |
| 96 | + } |
| 97 | + |
| 98 | + debugPrint('Starting walletd with args: $args'); |
| 99 | + |
| 100 | + // Start the process |
| 101 | + _walletdProcess = await Process.start(_walletdPath!, args); |
| 102 | + |
| 103 | + // Listen to stdout and stderr |
| 104 | + _walletdProcess!.stdout.transform(utf8.decoder).listen((data) { |
| 105 | + debugPrint('Walletd stdout: $data'); |
| 106 | + }); |
| 107 | + |
| 108 | + _walletdProcess!.stderr.transform(utf8.decoder).listen((data) { |
| 109 | + debugPrint('Walletd stderr: $data'); |
| 110 | + }); |
| 111 | + |
| 112 | + // Wait a moment for startup |
| 113 | + await Future.delayed(const Duration(seconds: 2)); |
| 114 | + |
| 115 | + // Check if process is still running |
| 116 | + if (_walletdProcess!.exitCode == null) { |
| 117 | + _isRunning = true; |
| 118 | + debugPrint('Walletd started successfully on port $_walletdPort'); |
| 119 | + return true; |
| 120 | + } else { |
| 121 | + debugPrint('Walletd failed to start'); |
| 122 | + return false; |
| 123 | + } |
| 124 | + } catch (e) { |
| 125 | + debugPrint('Error starting walletd: $e'); |
| 126 | + return false; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + /// Stop the wallet daemon |
| 131 | + static Future<void> stopWalletd() async { |
| 132 | + if (!_isRunning || _walletdProcess == null) { |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + try { |
| 137 | + _walletdProcess!.kill(); |
| 138 | + await _walletdProcess!.exitCode; |
| 139 | + _walletdProcess = null; |
| 140 | + _isRunning = false; |
| 141 | + debugPrint('Walletd stopped'); |
| 142 | + } catch (e) { |
| 143 | + debugPrint('Error stopping walletd: $e'); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /// Check if walletd is running |
| 148 | + static bool get isRunning => _isRunning; |
| 149 | + |
| 150 | + /// Get the walletd port |
| 151 | + static int get port => _walletdPort; |
| 152 | + |
| 153 | + /// Get the walletd URL |
| 154 | + static String get url => 'http://localhost:$_walletdPort'; |
| 155 | + |
| 156 | + /// Restart walletd with new parameters |
| 157 | + static Future<bool> restartWalletd({ |
| 158 | + String? walletPath, |
| 159 | + String? password, |
| 160 | + }) async { |
| 161 | + await stopWalletd(); |
| 162 | + await Future.delayed(const Duration(seconds: 1)); |
| 163 | + return await startWalletd(walletPath: walletPath, password: password); |
| 164 | + } |
| 165 | + |
| 166 | + /// Create a new wallet |
| 167 | + static Future<bool> createWallet({ |
| 168 | + required String walletPath, |
| 169 | + required String password, |
| 170 | + }) async { |
| 171 | + if (_walletdPath == null) { |
| 172 | + throw Exception('WalletDaemonService not initialized'); |
| 173 | + } |
| 174 | + |
| 175 | + try { |
| 176 | + final List<String> args = [ |
| 177 | + '--daemon-address', '$_daemonAddress', |
| 178 | + '--daemon-port', '$_daemonPort.toString()', |
| 179 | + '--wallet-file', walletPath, |
| 180 | + '--password', password, |
| 181 | + '--generate-new-wallet', |
| 182 | + '--non-interactive', |
| 183 | + ]; |
| 184 | + |
| 185 | + debugPrint('Creating wallet with args: $args'); |
| 186 | + |
| 187 | + final ProcessResult result = await Process.run(_walletdPath!, args); |
| 188 | + |
| 189 | + if (result.exitCode == 0) { |
| 190 | + debugPrint('Wallet created successfully'); |
| 191 | + return true; |
| 192 | + } else { |
| 193 | + debugPrint('Wallet creation failed: ${result.stderr}'); |
| 194 | + return false; |
| 195 | + } |
| 196 | + } catch (e) { |
| 197 | + debugPrint('Error creating wallet: $e'); |
| 198 | + return false; |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + /// Open an existing wallet |
| 203 | + static Future<bool> openWallet({ |
| 204 | + required String walletPath, |
| 205 | + required String password, |
| 206 | + }) async { |
| 207 | + return await startWalletd( |
| 208 | + walletPath: walletPath, |
| 209 | + password: password, |
| 210 | + ); |
| 211 | + } |
| 212 | +} |
0 commit comments