summaryrefslogtreecommitdiff
path: root/wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt
blob: e308b2a0967e6784a896824ba19c4f538d7881b7 (plain)
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
 * This file is part of GNU Taler
 * (C) 2020 Taler Systems S.A.
 *
 * GNU Taler is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3, or (at your option) any later version.
 *
 * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */

package net.taler.wallet.withdraw

import android.net.Uri
import android.util.Log
import androidx.annotation.UiThread
import androidx.annotation.WorkerThread
import androidx.lifecycle.MutableLiveData
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlinx.serialization.Serializable
import net.taler.common.Amount
import net.taler.common.Bech32
import net.taler.common.Event
import net.taler.common.toEvent
import net.taler.wallet.TAG
import net.taler.wallet.backend.TalerErrorInfo
import net.taler.wallet.backend.WalletBackendApi
import net.taler.wallet.balances.ScopeInfo
import net.taler.wallet.exchanges.ExchangeFees
import net.taler.wallet.exchanges.ExchangeItem
import net.taler.wallet.transactions.WithdrawalExchangeAccountDetails
import net.taler.wallet.withdraw.WithdrawStatus.ReceivedDetails

sealed class WithdrawStatus {
    data class Loading(val talerWithdrawUri: String? = null) : WithdrawStatus()

    data class NeedsExchange(
        val talerWithdrawUri: String,
        val amount: Amount,
        val possibleExchanges: List<ExchangeItem>,
    ) : WithdrawStatus()

    data class TosReviewRequired(
        val talerWithdrawUri: String? = null,
        val exchangeBaseUrl: String,
        val amountRaw: Amount,
        val amountEffective: Amount,
        val withdrawalAccountList: List<WithdrawalExchangeAccountDetails>,
        val ageRestrictionOptions: List<Int>? = null,
        val tosText: String,
        val tosEtag: String,
        val showImmediately: Event<Boolean>,
        val possibleExchanges: List<ExchangeItem> = emptyList(),
    ) : WithdrawStatus()

    data class ReceivedDetails(
        val talerWithdrawUri: String? = null,
        val exchangeBaseUrl: String,
        val amountRaw: Amount,
        val amountEffective: Amount,
        val withdrawalAccountList: List<WithdrawalExchangeAccountDetails>,
        val ageRestrictionOptions: List<Int>? = null,
        val possibleExchanges: List<ExchangeItem> = emptyList(),
    ) : WithdrawStatus()

    data object Withdrawing : WithdrawStatus()

    data class Success(val currency: String, val transactionId: String) : WithdrawStatus()

    class ManualTransferRequired(
        val transactionId: String?,
        val transactionAmountRaw: Amount,
        val transactionAmountEffective: Amount,
        val exchangeBaseUrl: String,
        val withdrawalTransfers: List<TransferData>,
    ) : WithdrawStatus()

    data class Error(val message: String?) : WithdrawStatus()
}

sealed class TransferData {
    abstract val subject: String
    abstract val amountRaw: Amount
    abstract val amountEffective: Amount
    abstract val withdrawalAccount: WithdrawalExchangeAccountDetails

    val currency get() = withdrawalAccount.transferAmount?.currency

    data class Taler(
        override val subject: String,
        override val amountRaw: Amount,
        override val amountEffective: Amount,
        override val withdrawalAccount: WithdrawalExchangeAccountDetails,
        val receiverName: String? = null,
        val account: String,
    ): TransferData()

    data class IBAN(
        override val subject: String,
        override val amountRaw: Amount,
        override val amountEffective: Amount,
        override val withdrawalAccount: WithdrawalExchangeAccountDetails,
        val receiverName: String? = null,
        val iban: String,
    ): TransferData()

    data class Bitcoin(
        override val subject: String,
        override val amountRaw: Amount,
        override val amountEffective: Amount,
        override val withdrawalAccount: WithdrawalExchangeAccountDetails,
        val account: String,
        val segwitAddresses: List<String>,
    ): TransferData()
}

sealed class WithdrawTestStatus {
    object Withdrawing : WithdrawTestStatus()
    object Success : WithdrawTestStatus()
    data class Error(val message: String) : WithdrawTestStatus()
}

@Serializable
data class WithdrawalDetailsForUri(
    val amount: Amount,
    val defaultExchangeBaseUrl: String?,
    val possibleExchanges: List<ExchangeItem>,
)

@Serializable
data class WithdrawExchangeResponse(
    val exchangeBaseUrl: String,
    val amount: Amount? = null,
)

@Serializable
data class ManualWithdrawalDetails(
    val tosAccepted: Boolean,
    val amountRaw: Amount,
    val amountEffective: Amount,
    val withdrawalAccountsList: List<WithdrawalExchangeAccountDetails>,
    val ageRestrictionOptions: List<Int>? = null,
    val scopeInfo: ScopeInfo,
)

@Serializable
data class AcceptWithdrawalResponse(
    val transactionId: String,
)

@Serializable
data class AcceptManualWithdrawalResponse(
    val reservePub: String,
    val withdrawalAccountsList: List<WithdrawalExchangeAccountDetails>,
    val transactionId: String,
)

class WithdrawManager(
    private val api: WalletBackendApi,
    private val scope: CoroutineScope,
) {

    val withdrawStatus = MutableLiveData<WithdrawStatus>()
    val testWithdrawalStatus = MutableLiveData<WithdrawTestStatus>()

    var exchangeFees: ExchangeFees? = null
        private set

    fun withdrawTestkudos() = scope.launch {
        testWithdrawalStatus.value = WithdrawTestStatus.Withdrawing
        api.request<Unit>("withdrawTestkudos").onError {
            testWithdrawalStatus.value = WithdrawTestStatus.Error(it.userFacingMsg)
        }.onSuccess {
            testWithdrawalStatus.value = WithdrawTestStatus.Success
        }
    }

    fun getWithdrawalDetails(uri: String) = scope.launch {
        withdrawStatus.value = WithdrawStatus.Loading(uri)
        api.request("getWithdrawalDetailsForUri", WithdrawalDetailsForUri.serializer()) {
            put("talerWithdrawUri", uri)
        }.onError { error ->
            handleError("getWithdrawalDetailsForUri", error)
        }.onSuccess { details ->
            if (details.defaultExchangeBaseUrl == null) {
                withdrawStatus.value = WithdrawStatus.NeedsExchange(
                    talerWithdrawUri = uri,
                    amount = details.amount,
                    possibleExchanges = details.possibleExchanges,
                )
            } else getWithdrawalDetails(
                exchangeBaseUrl = details.defaultExchangeBaseUrl,
                amount = details.amount,
                showTosImmediately = false,
                uri = uri,
                possibleExchanges = details.possibleExchanges,
            )
        }
    }

    fun getWithdrawalDetails(
        exchangeBaseUrl: String,
        amount: Amount,
        showTosImmediately: Boolean = false,
        uri: String? = null,
        possibleExchanges: List<ExchangeItem> = emptyList(),
    ) = scope.launch {
        withdrawStatus.value = WithdrawStatus.Loading(uri)
        api.request("getWithdrawalDetailsForAmount", ManualWithdrawalDetails.serializer()) {
            put("exchangeBaseUrl", exchangeBaseUrl)
            put("amount", amount.toJSONString())
        }.onError { error ->
            handleError("getWithdrawalDetailsForAmount", error)
        }.onSuccess { details ->
            if (details.tosAccepted) {
                withdrawStatus.value = ReceivedDetails(
                    talerWithdrawUri = uri,
                    exchangeBaseUrl = exchangeBaseUrl,
                    amountRaw = details.amountRaw,
                    amountEffective = details.amountEffective,
                    withdrawalAccountList = details.withdrawalAccountsList,
                    ageRestrictionOptions = details.ageRestrictionOptions,
                    possibleExchanges = possibleExchanges,
                )
            } else getExchangeTos(exchangeBaseUrl, details, showTosImmediately, uri, possibleExchanges)
        }
    }

    @WorkerThread
    suspend fun prepareManualWithdrawal(uri: String): WithdrawExchangeResponse? {
        withdrawStatus.postValue(WithdrawStatus.Loading(uri))
        var response: WithdrawExchangeResponse? = null
        api.request("prepareWithdrawExchange", WithdrawExchangeResponse.serializer()) {
            put("talerUri", uri)
        }.onError {
            handleError("prepareWithdrawExchange", it)
        }.onSuccess {
            response = it
        }
        return response
    }

    private fun getExchangeTos(
        exchangeBaseUrl: String,
        details: ManualWithdrawalDetails,
        showImmediately: Boolean,
        uri: String?,
        possibleExchanges: List<ExchangeItem>,
    ) = scope.launch {
        api.request("getExchangeTos", TosResponse.serializer()) {
            put("exchangeBaseUrl", exchangeBaseUrl)
        }.onError {
            handleError("getExchangeTos", it)
        }.onSuccess {
            withdrawStatus.value = WithdrawStatus.TosReviewRequired(
                talerWithdrawUri = uri,
                exchangeBaseUrl = exchangeBaseUrl,
                amountRaw = details.amountRaw,
                amountEffective = details.amountEffective,
                withdrawalAccountList = details.withdrawalAccountsList,
                ageRestrictionOptions = details.ageRestrictionOptions,
                tosText = it.content,
                tosEtag = it.currentEtag,
                showImmediately = showImmediately.toEvent(),
                possibleExchanges = possibleExchanges,
            )
        }
    }

    /**
     * Accept the currently displayed terms of service.
     */
    fun acceptCurrentTermsOfService() = scope.launch {
        val s = withdrawStatus.value as WithdrawStatus.TosReviewRequired
        api.request<Unit>("setExchangeTosAccepted") {
            put("exchangeBaseUrl", s.exchangeBaseUrl)
            put("etag", s.tosEtag)
        }.onError {
            handleError("setExchangeTosAccepted", it)
        }.onSuccess {
            withdrawStatus.value = ReceivedDetails(
                talerWithdrawUri = s.talerWithdrawUri,
                exchangeBaseUrl = s.exchangeBaseUrl,
                amountRaw = s.amountRaw,
                amountEffective = s.amountEffective,
                withdrawalAccountList = s.withdrawalAccountList,
                ageRestrictionOptions = s.ageRestrictionOptions,
                possibleExchanges = s.possibleExchanges,
            )
        }
    }

    @UiThread
    fun acceptWithdrawal(restrictAge: Int? = null) = scope.launch {
        val status = withdrawStatus.value as ReceivedDetails
        withdrawStatus.value = WithdrawStatus.Withdrawing
        if (status.talerWithdrawUri == null) {
            acceptManualWithdrawal(status, restrictAge)
        } else {
            acceptBankIntegratedWithdrawal(status, restrictAge)
        }
    }

    private suspend fun acceptBankIntegratedWithdrawal(
        status: ReceivedDetails,
        restrictAge: Int? = null,
    ) {
        api.request("acceptBankIntegratedWithdrawal", AcceptWithdrawalResponse.serializer()) {
            restrictAge?.let { put("restrictAge", restrictAge) }
            put("exchangeBaseUrl", status.exchangeBaseUrl)
            put("talerWithdrawUri", status.talerWithdrawUri)
        }.onError {
            handleError("acceptBankIntegratedWithdrawal", it)
        }.onSuccess {
            withdrawStatus.value =
                WithdrawStatus.Success(status.amountRaw.currency, it.transactionId)
        }
    }

    private suspend fun acceptManualWithdrawal(status: ReceivedDetails, restrictAge: Int? = null) {
        api.request("acceptManualWithdrawal", AcceptManualWithdrawalResponse.serializer()) {
            restrictAge?.let { put("restrictAge", restrictAge) }
            put("exchangeBaseUrl", status.exchangeBaseUrl)
            put("amount", status.amountRaw.toJSONString())
        }.onError {
            handleError("acceptManualWithdrawal", it)
        }.onSuccess { response ->
            withdrawStatus.value = createManualTransferRequired(
                status = status,
                response = response,
            )
        }
    }

    private fun handleError(operation: String, error: TalerErrorInfo) {
        Log.e(TAG, "Error $operation $error")
        withdrawStatus.postValue(WithdrawStatus.Error(error.userFacingMsg))
    }

    /**
     * A hack to be able to view bank details for manual withdrawal with the same logic.
     * Don't call this from ongoing withdrawal processes as it destroys state.
     */
    fun viewManualWithdrawal(status: WithdrawStatus.ManualTransferRequired) {
        require(status.transactionId != null) { "No transaction ID given" }
        withdrawStatus.value = status
    }

}

fun createManualTransferRequired(
    transactionId: String,
    exchangeBaseUrl: String,
    amountRaw: Amount,
    amountEffective: Amount,
    withdrawalAccountList: List<WithdrawalExchangeAccountDetails>,
) = WithdrawStatus.ManualTransferRequired(
    transactionId = transactionId,
    transactionAmountRaw = amountRaw,
    transactionAmountEffective = amountEffective,
    exchangeBaseUrl = exchangeBaseUrl,
    withdrawalTransfers = withdrawalAccountList.mapNotNull {
        val uri = Uri.parse(it.paytoUri.replace("receiver-name=", "receiver_name="))
        if ("bitcoin".equals(uri.authority, true)) {
            val msg = uri.getQueryParameter("message").orEmpty()
            val reg = "\\b([A-Z0-9]{52})\\b".toRegex().find(msg)
            val reserve = reg?.value ?: uri.getQueryParameter("subject")!!
            val segwitAddresses = Bech32.generateFakeSegwitAddress(reserve, uri.pathSegments.first())
            TransferData.Bitcoin(
                account = uri.lastPathSegment!!,
                segwitAddresses = segwitAddresses,
                subject = reserve,
                amountRaw = amountRaw,
                amountEffective = amountEffective,
                withdrawalAccount = it.copy(paytoUri = uri.toString())
            )
        } else if (uri.authority.equals("x-taler-bank", true)) {
            TransferData.Taler(
                account = uri.lastPathSegment!!,
                receiverName = uri.getQueryParameter("receiver_name"),
                subject = uri.getQueryParameter("message") ?: "Error: No message in URI",
                amountRaw = amountRaw,
                amountEffective = amountEffective,
                withdrawalAccount = it.copy(paytoUri = uri.toString()),
            )
        } else if (uri.authority.equals("iban", true)) {
            TransferData.IBAN(
                iban = uri.lastPathSegment!!,
                receiverName = uri.getQueryParameter("receiver_name"),
                subject = uri.getQueryParameter("message") ?: "Error: No message in URI",
                amountRaw = amountRaw,
                amountEffective = amountEffective,
                withdrawalAccount = it.copy(paytoUri = uri.toString()),
            )
        } else null
    },
)

fun createManualTransferRequired(
    status: ReceivedDetails,
    response: AcceptManualWithdrawalResponse,
): WithdrawStatus.ManualTransferRequired = createManualTransferRequired(
    transactionId = response.transactionId,
    exchangeBaseUrl = status.exchangeBaseUrl,
    amountRaw = status.amountRaw,
    amountEffective = status.amountEffective,
    withdrawalAccountList = response.withdrawalAccountsList,
)