-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathElectrumConfigViewModel.kt
More file actions
354 lines (310 loc) · 12.3 KB
/
ElectrumConfigViewModel.kt
File metadata and controls
354 lines (310 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package to.bitkit.ui.settings.advanced
import android.content.Context
import androidx.core.net.toUri
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import org.lightningdevkit.ldknode.Network
import to.bitkit.R
import to.bitkit.data.SettingsStore
import to.bitkit.di.BgDispatcher
import to.bitkit.env.Env
import to.bitkit.models.ElectrumProtocol
import to.bitkit.models.ElectrumServer
import to.bitkit.models.ElectrumServerPeer
import to.bitkit.models.MAX_VALID_PORT
import to.bitkit.models.Toast
import to.bitkit.models.getDefaultPort
import to.bitkit.repositories.LightningRepo
import to.bitkit.ui.shared.toast.ToastEventBus
import javax.inject.Inject
@HiltViewModel
class ElectrumConfigViewModel @Inject constructor(
@BgDispatcher private val bgDispatcher: CoroutineDispatcher,
@ApplicationContext private val context: Context,
private val settingsStore: SettingsStore,
private val lightningRepo: LightningRepo,
) : ViewModel() {
private val _uiState = MutableStateFlow(ElectrumConfigUiState())
val uiState: StateFlow<ElectrumConfigUiState> = _uiState.asStateFlow()
val defaultElectrumPorts = listOf("51002", "50002", "51001", "50001")
init {
observeState()
}
private fun observeState() {
viewModelScope.launch(bgDispatcher) {
lightningRepo.lightningState.collect { lightningState ->
val isNodeRunning = lightningState.nodeStatus?.isRunning == true
val hasSyncError = lightningState.lastSyncError != null
_uiState.update { currentState ->
currentState.copy(
isConnected = isNodeRunning && !hasSyncError,
)
}
}
}
viewModelScope.launch(bgDispatcher) {
settingsStore.data.map { it.electrumServer }.distinctUntilChanged()
.collect { electrumServerUrl ->
val electrumServer = ElectrumServer.parse(electrumServerUrl)
val connectedPeer = ElectrumServerPeer(
host = electrumServer.host,
port = electrumServer.getPort().toString(),
protocol = electrumServer.protocol,
)
_uiState.update {
val newState = it.copy(
connectedPeer = connectedPeer,
host = connectedPeer.host,
port = connectedPeer.port,
protocol = connectedPeer.protocol,
)
newState.copy(hasEdited = computeHasEdited(newState))
}
}
}
}
fun setHost(host: String) {
_uiState.update {
val newState = it.copy(host = host.trim())
newState.copy(hasEdited = computeHasEdited(newState))
}
}
fun setPort(port: String) {
_uiState.update {
val newState = it.copy(port = port.trim())
newState.copy(hasEdited = computeHasEdited(newState))
}
}
fun setProtocol(protocol: ElectrumProtocol) {
_uiState.update {
// Toggle the port if the protocol changes and the default ports are still used
val newPort = if (it.port.isEmpty() || it.port in defaultElectrumPorts) {
protocol.getDefaultPort().toString()
} else {
it.port
}
val newState = it.copy(
protocol = protocol,
port = newPort,
)
newState.copy(hasEdited = computeHasEdited(newState))
}
}
fun resetToDefault() {
val defaultServer = ElectrumServer.parse(Env.electrumServerUrl)
_uiState.update {
val newState = it.copy(
host = defaultServer.host,
port = defaultServer.getPort().toString(),
protocol = defaultServer.protocol,
)
newState.copy(hasEdited = computeHasEdited(newState))
}
}
@Suppress("ComplexCondition")
fun connectToServer() {
val currentState = _uiState.value
val port = currentState.port.toIntOrNull()
val protocol = currentState.protocol
if (currentState.host.isBlank() || port == null || port <= 0 || protocol == null) return
_uiState.update { it.copy(isLoading = true) }
viewModelScope.launch(bgDispatcher) {
runCatching {
val electrumServer = ElectrumServer.fromUserInput(
host = currentState.host,
port = port,
protocol = protocol,
)
val serverUrl = electrumServer.toString()
lightningRepo.restartWithElectrumServer(serverUrl)
.onSuccess {
_uiState.update {
it.copy(
isLoading = false,
connectionResult = Result.success(Unit),
hasEdited = false,
)
}
}
.onFailure { error -> throw error }
}.onFailure { e ->
_uiState.update {
it.copy(
isLoading = false,
connectionResult = Result.failure(e),
)
}
}
}
}
fun validateInput(
host: String = _uiState.value.host,
port: String = _uiState.value.port,
): String? {
var error: String? = null
if (host.isBlank() && port.isBlank()) {
error = context.getString(R.string.settings__es__error_host_port)
} else if (host.isBlank()) {
error = context.getString(R.string.settings__es__error_host)
} else if (port.isBlank()) {
error = context.getString(R.string.settings__es__error_port)
} else {
val portNumber = port.toIntOrNull()
if (portNumber == null || portNumber <= 0 || portNumber > MAX_VALID_PORT) {
error = context.getString(R.string.settings__es__error_port_invalid)
}
}
val url = "$host:$port"
if (!isValidURL(url)) {
error = context.getString(R.string.settings__es__error_invalid_http)
}
return error
}
private fun isValidURL(data: String): Boolean {
// Add 'http://' if the protocol is missing to enable URL parsing
val normalizedData = if (!data.startsWith("http://") && !data.startsWith("https://")) {
"http://$data"
} else {
data
}
return try {
val url = normalizedData.toUri()
val hostname = url.host ?: return false
// Allow standard domains, custom TLDs like .local, and IPv4 addresses
val isValidDomainOrIP = hostname.matches(
Regex(
"^([a-z\\d]([a-z\\d-]*[a-z\\d])*\\.)+[a-z\\d-]+|(\\d{1,3}\\.){3}\\d{1,3}$",
RegexOption.IGNORE_CASE
)
)
// Always allow .local domains
if (hostname.endsWith(".local")) {
return true
}
// Allow localhost in development mode
if (Env.isDebug && data.contains("localhost")) {
return true
}
isValidDomainOrIP
} catch (_: Throwable) {
// If URL constructor fails, it's not a valid URL
false
}
}
private fun computeHasEdited(state: ElectrumConfigUiState): Boolean {
val protocol = state.protocol ?: return false
val uiPeer = ElectrumServerPeer(
host = state.host,
port = state.port,
protocol = protocol,
)
return uiPeer != state.connectedPeer
}
fun clearConnectionResult() {
_uiState.update { it.copy(connectionResult = null) }
}
fun onClickConnect() {
viewModelScope.launch {
val validationError = validateInput()
if (validationError != null) {
ToastEventBus.send(
type = Toast.ToastType.WARNING,
title = context.getString(R.string.settings__es__error_peer),
description = validationError,
)
} else {
connectToServer()
}
}
}
fun onScan(data: String) {
viewModelScope.launch {
val parseResult = parseElectrumScanData(data)
val serverPeer = parseResult.getOrDefault(ElectrumServerPeer("", "", ElectrumProtocol.TCP))
val host = serverPeer.host
val port = serverPeer.port
val protocol = serverPeer.protocol
val validationError = validateInput(host, port)
if (validationError != null) {
ToastEventBus.send(
type = Toast.ToastType.WARNING,
title = context.getString(R.string.settings__es__error_peer),
description = validationError,
)
return@launch
}
setHost(host)
setPort(port)
setProtocol(protocol)
connectToServer()
}
}
private fun parseElectrumScanData(data: String): Result<ElectrumServerPeer> {
return when {
// Handle plain format: host:port or host:port:s (Umbrel format)
!data.startsWith("http://") && !data.startsWith("https://") -> {
runCatching {
val parts = data.split(":")
val host = parts.getOrNull(0) ?: ""
val port = parts.getOrNull(1) ?: ""
val shortProtocol = parts.getOrNull(2)
val protocol = if (shortProtocol != null) {
// Support Umbrel connection URL format
if (shortProtocol == "s") ElectrumProtocol.SSL else ElectrumProtocol.TCP
} else {
// Prefix protocol for common ports if missing
getProtocolForPort(port)
}
return@runCatching ElectrumServerPeer(host, port, protocol)
}
}
// Handle URLs with http:// or https:// prefix
else -> {
runCatching {
val uri = data.toUri()
val host = uri.host ?: ""
val port = if (uri.port > 0) {
uri.port.toString()
} else {
if (uri.scheme == "https") "443" else "80"
}
val protocol = if (uri.scheme == "https") ElectrumProtocol.SSL else ElectrumProtocol.TCP
return@runCatching ElectrumServerPeer(host, port, protocol)
}
}
}
}
private fun getProtocolForPort(port: String): ElectrumProtocol {
if (port == "443") return ElectrumProtocol.SSL
// Network-specific logic for testnet
if (Env.network == Network.TESTNET) {
return if (port == "51002") ElectrumProtocol.SSL else ElectrumProtocol.TCP
}
// Default logic for mainnet and other networks
return when (port) {
"50002", "51002" -> ElectrumProtocol.SSL
"50001", "51001" -> ElectrumProtocol.TCP
else -> ElectrumProtocol.TCP // Default to TCP
}
}
}
data class ElectrumConfigUiState(
val isConnected: Boolean = false,
val connectedPeer: ElectrumServerPeer? = null,
val host: String = "",
val port: String = "",
val protocol: ElectrumProtocol? = null,
val isLoading: Boolean = false,
val connectionResult: Result<Unit>? = null,
val hasEdited: Boolean = false,
)