@@ -32,36 +32,60 @@ extension SettingsViewModel {
3232 return ( success: false , host: host, port: port, errorMessage: validationError)
3333 }
3434
35- // Update current server config immediately after validation
36- electrumCurrentServer = ElectrumServer (
35+ // Create server config (don't save yet - only save after successful connection)
36+ let serverConfig = ElectrumServer (
3737 host: host,
3838 portString: port,
3939 protocolType: electrumSelectedProtocol
4040 )
4141
4242 do {
43- // Save the configuration to settings
44- electrumConfigService. saveServerConfig ( electrumCurrentServer)
45-
4643 // Restart the Lightning node with the new Electrum server
4744 let currentRgsUrl = rgsConfigService. getCurrentServerUrl ( )
4845 try await lightningService. restart (
49- electrumServerUrl: electrumCurrentServer . url ,
46+ electrumServerUrl: serverConfig . fullUrl ,
5047 rgsServerUrl: currentRgsUrl. isEmpty ? nil : currentRgsUrl
5148 )
5249
50+ // Wait a bit for the connection to establish and verify it's actually working
51+ try await Task . sleep ( nanoseconds: 1_000_000_000 ) // 1 second
52+
53+ // Verify the node is actually running and connected
54+ guard let status = lightningService. status, status. isRunning else {
55+ electrumIsLoading = false
56+ Logger . error ( " Electrum connection failed: Node is not running after restart " )
57+
58+ // Reload form and connection status from actual current server (node may have fallen back to previous server)
59+ let actualServer = electrumConfigService. getCurrentServer ( )
60+ updateForm ( with: actualServer)
61+ electrumCurrentServer = actualServer
62+ // Check if node is actually running with the previous server
63+ electrumIsConnected = lightningService. status? . isRunning == true
64+
65+ return ( success: false , host: host, port: port, errorMessage: t ( " settings__es__server_error_description " ) )
66+ }
67+
68+ // Only save the configuration after successful connection validation
69+ electrumConfigService. saveServerConfig ( serverConfig)
70+ electrumCurrentServer = serverConfig
5371 electrumIsConnected = true
5472 electrumIsLoading = false
5573
56- Logger . info ( " Successfully connected to Electrum server: \( electrumCurrentServer . url ) " )
74+ Logger . info ( " Successfully connected to Electrum server: \( serverConfig . fullUrl ) " )
5775
5876 return ( success: true , host: host, port: port, errorMessage: nil )
5977 } catch {
60- electrumIsConnected = false
6178 electrumIsLoading = false
6279
6380 Logger . error ( error, context: " Failed to connect to Electrum server " )
6481
82+ // Reload form and connection status from actual current server (node may have fallen back to previous server)
83+ let actualServer = electrumConfigService. getCurrentServer ( )
84+ updateForm ( with: actualServer)
85+ electrumCurrentServer = actualServer
86+ // Check if node is actually running with the previous server
87+ electrumIsConnected = lightningService. status? . isRunning == true
88+
6589 return ( success: false , host: host, port: port, errorMessage: nil )
6690 }
6791 }
@@ -151,6 +175,20 @@ extension SettingsViewModel {
151175 }
152176
153177 private func parseElectrumScanData( _ data: String ) -> ElectrumServer ? {
178+ // Handle URLs with tcp:// or ssl:// prefix
179+ if data. hasPrefix ( " tcp:// " ) || data. hasPrefix ( " ssl:// " ) {
180+ let protocolType : ElectrumProtocol = data. hasPrefix ( " ssl:// " ) ? . ssl : . tcp
181+ let urlWithoutProtocol = String ( data. dropFirst ( 6 ) ) // Remove "ssl://" or "tcp://"
182+ let components = urlWithoutProtocol. split ( separator: " : " )
183+
184+ guard components. count >= 2 else { return nil }
185+
186+ let host = String ( components [ 0 ] )
187+ let port = String ( components [ 1 ] )
188+
189+ return ElectrumServer ( host: host, portString: port, protocolType: protocolType)
190+ }
191+
154192 // Handle plain format: host:port or host:port:s (Umbrel format)
155193 if !data. hasPrefix ( " http:// " ) && !data. hasPrefix ( " https:// " ) {
156194 let parts = data. split ( separator: " : " )
0 commit comments