summaryrefslogtreecommitdiff
path: root/bank/src/main/kotlin/tech/libeufin/bank/Config.kt
blob: 54f2c44bca5c51111eba1e917f4bc0d39e957d2a (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
/*
 * This file is part of LibEuFin.
 * Copyright (C) 2023 Taler Systems S.A.

 * LibEuFin is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation; either version 3, or
 * (at your option) any later version.

 * LibEuFin 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 Affero General
 * Public License for more details.

 * You should have received a copy of the GNU Affero General Public
 * License along with LibEuFin; see the file COPYING.  If not, see
 * <http://www.gnu.org/licenses/>
 */
package tech.libeufin.bank

import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import tech.libeufin.common.*
import java.nio.file.Path
import java.time.Duration

private val logger: Logger = LoggerFactory.getLogger("libeufin-bank")

/**
 * Application the parsed configuration.
 */
data class BankConfig(
    val name: String,
    val regionalCurrency: String,
    val regionalCurrencySpec: CurrencySpecification,
    val allowRegistration: Boolean,
    val allowAccountDeletion: Boolean,
    val allowEditName: Boolean,
    val allowEditCashout: Boolean,
    val defaultDebtLimit: TalerAmount,
    val registrationBonus: TalerAmount,
    val suggestedWithdrawalExchange: String?,
    val allowConversion: Boolean,
    val fiatCurrency: String?,
    val fiatCurrencySpec: CurrencySpecification?,
    val spaPath: Path?,
    val tanChannels: Map<TanChannel, Pair<Path, Map<String, String>>>,
    val payto: BankPaytoCtx,
    val wireMethod: WireMethod,
    val gcAbortAfter: Duration,
    val gcCleanAfter: Duration,
    val gcDeleteAfter: Duration
)

@Serializable
data class ConversionRate (
    val cashin_ratio: DecimalNumber,
    val cashin_fee: TalerAmount,
    val cashin_tiny_amount: TalerAmount,
    val cashin_rounding_mode: RoundingMode,
    val cashin_min_amount: TalerAmount,
    val cashout_ratio: DecimalNumber,
    val cashout_fee: TalerAmount,
    val cashout_tiny_amount: TalerAmount,
    val cashout_rounding_mode: RoundingMode,
    val cashout_min_amount: TalerAmount,
)

sealed interface ServerConfig {
    data class Unix(val path: String, val mode: Int): ServerConfig
    data class Tcp(val addr: String, val port: Int): ServerConfig
}

fun talerConfig(configPath: Path?): TalerConfig = BANK_CONFIG_SOURCE.fromFile(configPath)

fun TalerConfig.loadDbConfig(): DatabaseConfig  {
    return DatabaseConfig(
        dbConnStr = requireString("libeufin-bankdb-postgres", "config"),
        sqlDir = requirePath("libeufin-bankdb-postgres", "sql_dir")
    )
}

fun TalerConfig.loadServerConfig(): ServerConfig {
    return when (val method = requireString("libeufin-bank", "serve")) {
        "tcp" -> ServerConfig.Tcp(lookupString("libeufin-bank", "address") ?: requireString("libeufin-bank", "bind_to"), requireNumber("libeufin-bank", "port"))
        "unix" -> ServerConfig.Unix(requireString("libeufin-bank", "unixpath"), requireNumber("libeufin-bank", "unixpath_mode"))
        else -> throw TalerConfigError.invalid("server method", "libeufin-bank", "serve", "expected 'tcp' or 'unix' got '$method'")
    }
}

fun TalerConfig.loadBankConfig(): BankConfig {
    val regionalCurrency = requireString("libeufin-bank", "currency")
    var fiatCurrency: String? = null
    var fiatCurrencySpec: CurrencySpecification? = null
    val allowConversion = lookupBoolean("libeufin-bank", "allow_conversion") ?: false
    if (allowConversion) {
        fiatCurrency = requireString("libeufin-bank", "fiat_currency")
        fiatCurrencySpec = currencySpecificationFor(fiatCurrency) 
    }
    val tanChannels = buildMap {
        for (channel in TanChannel.entries) {
            lookupPath("libeufin-bank", "tan_$channel")?.let {
                put(channel, Pair(it, jsonMap("libeufin-bank", "tan_${channel}_env") ?: mapOf()))
            }
        }
    }
    val method = when (val type = lookupString("libeufin-bank", "wire_type")) {
        "iban" -> WireMethod.IBAN
        "x-taler-bank" -> WireMethod.X_TALER_BANK
        null -> {
            val err = TalerConfigError.missing("payment target type", "libeufin-bank", "wire_type").message
            logger.warn("$err, defaulting to 'iban' but will fail in a future update")
            WireMethod.IBAN
        }
        else -> throw TalerConfigError.invalid("payment target type", "libeufin-bank", "wire_type", "expected 'iban' or 'x-taler-bank' got '$type'")
    }
    val payto = BankPaytoCtx(
        bic = lookupString("libeufin-bank", "iban_payto_bic"),
        hostname = lookupString("libeufin-bank", "x_taler_bank_payto_hostname")
    )
    when (method) {
        WireMethod.IBAN -> if (payto.bic == null) {
            logger.warn(TalerConfigError.missing("BIC", "libeufin-bank", "iban_payto_bic").message + " will fail in a future update")
        }
        WireMethod.X_TALER_BANK -> if (payto.hostname == null) {
            logger.warn(TalerConfigError.missing("hostname", "libeufin-bank", "x_taler_bank_payto_hostname").message + " will fail in a future update")
        }
    }
    return BankConfig(
        name = lookupString("libeufin-bank", "name") ?: "Taler Bank",
        regionalCurrency = regionalCurrency,
        regionalCurrencySpec = currencySpecificationFor(regionalCurrency),
        allowRegistration = lookupBoolean("libeufin-bank", "allow_registration") ?: false,
        allowAccountDeletion = lookupBoolean("libeufin-bank", "allow_account_deletion") ?: false,
        allowEditName = lookupBoolean("libeufin-bank", "allow_edit_name") ?: false,
        allowEditCashout = lookupBoolean("libeufin-bank", "allow_edit_cashout_payto_uri") ?: false,
        defaultDebtLimit = amount("libeufin-bank", "default_debt_limit", regionalCurrency) ?: TalerAmount(0, 0, regionalCurrency),
        registrationBonus = amount("libeufin-bank", "registration_bonus", regionalCurrency) ?: TalerAmount(0, 0, regionalCurrency),
        suggestedWithdrawalExchange = lookupString("libeufin-bank", "suggested_withdrawal_exchange"),
        spaPath = lookupPath("libeufin-bank", "spa"),
        allowConversion = allowConversion,
        fiatCurrency = fiatCurrency,
        fiatCurrencySpec = fiatCurrencySpec,
        tanChannels = tanChannels,
        payto = payto,
        wireMethod = method,
        gcAbortAfter = requireDuration("libeufin-bank", "gc_abort_after"),
        gcCleanAfter = requireDuration("libeufin-bank", "gc_clean_after"),
        gcDeleteAfter = requireDuration("libeufin-bank", "gc_delete_after"),
    )
}

fun String.notEmptyOrNull(): String? = if (isEmpty()) null else this

fun TalerConfig.currencySpecificationFor(currency: String): CurrencySpecification
    = sections.find {
        it.startsWith("CURRENCY-") && requireBoolean(it, "enabled") && requireString(it, "code") == currency
    }?.let { loadCurrencySpecification(it) } ?: run {
        logger.warn("Missing currency specification for $currency, using sane defaults")
        CurrencySpecification(
            name = currency,
            num_fractional_input_digits = 2,
            num_fractional_normal_digits = 2,
            num_fractional_trailing_zero_digits = 2,
            alt_unit_names = mapOf("0" to currency)
        )
    }

private fun TalerConfig.loadCurrencySpecification(section: String): CurrencySpecification {
    return CurrencySpecification(
        name = requireString(section, "name"),
        num_fractional_input_digits = requireNumber(section, "fractional_input_digits"),
        num_fractional_normal_digits = requireNumber(section, "fractional_normal_digits"),
        num_fractional_trailing_zero_digits = requireNumber(section, "fractional_trailing_zero_digits"),
        alt_unit_names = requireJsonMap(section, "alt_unit_names")
    )
}

private fun TalerConfig.jsonMap(section: String, option: String): Map<String, String>? {
    val raw = lookupString(section, option) ?: return null
    try {
        return Json.decodeFromString(raw)
    } catch (e: Exception) {
        throw TalerConfigError.invalid("json key/value map", section, option, "'$raw' is malformed")
    }
}

private fun TalerConfig.requireJsonMap(section: String, option: String): Map<String, String>
    = jsonMap(section, option) ?: throw TalerConfigError.missing("json key/value map", section, option)

private fun TalerConfig.amount(section: String, option: String, currency: String): TalerAmount? {
    val raw = lookupString(section, option) ?: return null
    val amount = try {
        TalerAmount(raw)
    } catch (e: Exception) {
        throw TalerConfigError.invalid("amount", section, option, "amount '$raw' is malformed")
    }

    if (amount.currency != currency) {
        throw TalerConfigError.invalid("amount", section, option, "expected currency $currency got ${amount.currency}")
    }
    return amount
}

private fun TalerConfig.requireAmount(section: String, option: String, currency: String): TalerAmount =
    amount(section, option, currency) ?: throw TalerConfigError.missing("amount", section, option)

private fun TalerConfig.decimalNumber(section: String, option: String): DecimalNumber? {
    val raw = lookupString(section, option) ?: return null
    try {
        return DecimalNumber(raw)
    } catch (e: Exception) {
        throw TalerConfigError.invalid("decimal number", section, option, "number '$raw' is malformed")
    }
}

private fun TalerConfig.requireDecimalNumber(section: String, option: String): DecimalNumber
    = decimalNumber(section, option) ?: throw TalerConfigError.missing("decimal number", section, option)