summaryrefslogtreecommitdiff
path: root/wallet/src/commonMain/kotlin/net/taler/lib/wallet/api
diff options
context:
space:
mode:
Diffstat (limited to 'wallet/src/commonMain/kotlin/net/taler/lib/wallet/api')
-rw-r--r--wallet/src/commonMain/kotlin/net/taler/lib/wallet/api/Exchanges.kt55
-rw-r--r--wallet/src/commonMain/kotlin/net/taler/lib/wallet/api/Version.kt26
-rw-r--r--wallet/src/commonMain/kotlin/net/taler/lib/wallet/api/WalletApi.kt29
-rw-r--r--wallet/src/commonMain/kotlin/net/taler/lib/wallet/api/WalletFactory.kt108
-rw-r--r--wallet/src/commonMain/kotlin/net/taler/lib/wallet/api/Withdrawal.kt53
5 files changed, 271 insertions, 0 deletions
diff --git a/wallet/src/commonMain/kotlin/net/taler/lib/wallet/api/Exchanges.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/api/Exchanges.kt
new file mode 100644
index 0000000..2542dd6
--- /dev/null
+++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/api/Exchanges.kt
@@ -0,0 +1,55 @@
+/*
+ * 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.lib.wallet.api
+
+import net.taler.lib.wallet.exchange.ExchangeRecord
+
+data class ExchangeListItem(
+ val exchangeBaseUrl: String,
+ val currency: String,
+ val paytoUris: List<String>
+) {
+ companion object {
+ fun fromExchangeRecord(exchange: ExchangeRecord): ExchangeListItem? {
+ return if (exchange.details == null || exchange.wireInfo == null) null
+ else ExchangeListItem(
+ exchangeBaseUrl = exchange.baseUrl,
+ currency = exchange.details.currency,
+ paytoUris = exchange.wireInfo.accounts.map {
+ it.paytoUri
+ }
+ )
+ }
+ }
+}
+
+data class GetExchangeTosResult(
+ /**
+ * Markdown version of the current ToS.
+ */
+ val tos: String,
+
+ /**
+ * Version tag of the current ToS.
+ */
+ val currentEtag: String,
+
+ /**
+ * Version tag of the last ToS that the user has accepted, if any.
+ */
+ val acceptedEtag: String? = null
+)
diff --git a/wallet/src/commonMain/kotlin/net/taler/lib/wallet/api/Version.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/api/Version.kt
new file mode 100644
index 0000000..12916d3
--- /dev/null
+++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/api/Version.kt
@@ -0,0 +1,26 @@
+/*
+ * 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.lib.wallet.api
+
+import net.taler.lib.common.Version
+
+class SupportedVersions(
+ val walletVersion: Version,
+ val exchangeVersion: Version,
+ val bankVersion: Version,
+ val merchantVersion: Version
+)
diff --git a/wallet/src/commonMain/kotlin/net/taler/lib/wallet/api/WalletApi.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/api/WalletApi.kt
new file mode 100644
index 0000000..026c682
--- /dev/null
+++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/api/WalletApi.kt
@@ -0,0 +1,29 @@
+/*
+ * 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.lib.wallet.api
+
+import net.taler.lib.common.Amount
+
+public interface WalletApi {
+ fun getVersions(): SupportedVersions
+ fun getWithdrawalDetailsForUri(talerWithdrawUri: String): WithdrawalDetailsForUri
+ fun getWithdrawalDetailsForAmount(exchangeBaseUrl: String, amount: Amount): WithdrawalDetails
+ fun listExchanges(): List<ExchangeListItem>
+ fun addExchange(exchangeBaseUrl: String): ExchangeListItem
+ fun getExchangeTos(exchangeBaseUrl: String): GetExchangeTosResult
+ fun setExchangeTosAccepted(exchangeBaseUrl: String, acceptedEtag: String)
+}
diff --git a/wallet/src/commonMain/kotlin/net/taler/lib/wallet/api/WalletFactory.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/api/WalletFactory.kt
new file mode 100644
index 0000000..d56f80c
--- /dev/null
+++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/api/WalletFactory.kt
@@ -0,0 +1,108 @@
+/*
+ * 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.lib.wallet.api
+
+import io.ktor.client.HttpClient
+import net.taler.lib.common.Amount
+import net.taler.lib.common.Timestamp
+import net.taler.lib.common.Version
+import net.taler.lib.crypto.Crypto
+import net.taler.lib.crypto.CryptoFactory
+import net.taler.lib.wallet.Db
+import net.taler.lib.wallet.DbFactory
+import net.taler.lib.wallet.crypto.Signature
+import net.taler.lib.wallet.exchange.Exchange
+import net.taler.lib.wallet.getDefaultHttpClient
+import net.taler.lib.wallet.operations.Withdraw
+
+
+public expect class WalletFactory {
+ fun createWalletApi(): WalletApi
+}
+
+internal class WalletApiImpl {
+
+ private val httpClient: HttpClient = getDefaultHttpClient()
+ private val db: Db = DbFactory().openDb()
+ private val crypto: Crypto = CryptoFactory.getCrypto()
+ private val signature: Signature = Signature(crypto)
+ private val exchangeManager: Exchange = Exchange(crypto, signature, httpClient, db = db)
+ private val withdrawManager = Withdraw(httpClient, db, crypto, signature, exchangeManager)
+
+ fun getVersions(): SupportedVersions {
+ return SupportedVersions(
+ walletVersion = Version(8, 0, 0),
+ exchangeVersion = Version(8, 0, 0),
+ bankVersion = Version(0, 0, 0),
+ merchantVersion = Version(1, 0, 0)
+ )
+ }
+
+ suspend fun getWithdrawalDetailsForUri(talerWithdrawUri: String): WithdrawalDetailsForUri {
+ val bankInfo = withdrawManager.getBankInfo(talerWithdrawUri)
+ return WithdrawalDetailsForUri(
+ amount = bankInfo.amount,
+ defaultExchangeBaseUrl = bankInfo.suggestedExchange,
+ possibleExchanges = emptyList()
+ )
+ }
+
+ suspend fun getWithdrawalDetailsForAmount(
+ exchangeBaseUrl: String,
+ amount: Amount
+ ): WithdrawalDetails {
+ val details = withdrawManager.getWithdrawalDetails(exchangeBaseUrl, amount)
+ return WithdrawalDetails(
+ tosAccepted = details.exchange.termsOfServiceAccepted,
+ amountRaw = amount,
+ amountEffective = amount - details.overhead - details.withdrawFee
+ )
+ }
+
+ suspend fun listExchanges(): List<ExchangeListItem> {
+ return db.listExchanges().mapNotNull { exchange ->
+ ExchangeListItem.fromExchangeRecord(exchange)
+ }
+ }
+
+ suspend fun addExchange(exchangeBaseUrl: String): ExchangeListItem {
+ val exchange = exchangeManager.updateFromUrl(exchangeBaseUrl)
+ db.put(exchange)
+ return ExchangeListItem.fromExchangeRecord(exchange) ?: TODO("error handling")
+ }
+
+ suspend fun getExchangeTos(exchangeBaseUrl: String): GetExchangeTosResult {
+ val record = db.getExchangeByBaseUrl(exchangeBaseUrl) ?: TODO("error handling")
+ return GetExchangeTosResult(
+ tos = record.termsOfServiceText ?: TODO("error handling"),
+ currentEtag = record.termsOfServiceLastEtag ?: TODO("error handling"),
+ acceptedEtag = record.termsOfServiceAcceptedEtag
+ )
+ }
+
+ suspend fun setExchangeTosAccepted(exchangeBaseUrl: String, acceptedEtag: String) {
+ db.transaction {
+ val record = getExchangeByBaseUrl(exchangeBaseUrl) ?: TODO("error handling")
+ val updatedRecord = record.copy(
+ termsOfServiceAcceptedEtag = acceptedEtag,
+ termsOfServiceAcceptedTimestamp = Timestamp.now()
+ )
+ put(updatedRecord)
+ }
+ }
+
+}
diff --git a/wallet/src/commonMain/kotlin/net/taler/lib/wallet/api/Withdrawal.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/api/Withdrawal.kt
new file mode 100644
index 0000000..88c96a4
--- /dev/null
+++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/api/Withdrawal.kt
@@ -0,0 +1,53 @@
+/*
+ * 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.lib.wallet.api
+
+import net.taler.lib.common.Amount
+
+public data class WithdrawalDetailsForUri(
+ /**
+ * The amount that the user wants to withdraw
+ */
+ val amount: Amount,
+
+ /**
+ * Exchange suggested by the wallet
+ */
+ val defaultExchangeBaseUrl: String?,
+
+ /**
+ * A list of exchanges that can be used for this withdrawal
+ */
+ val possibleExchanges: List<ExchangeListItem>
+)
+
+public data class WithdrawalDetails(
+ /**
+ * Did the user accept the current version of the exchange's terms of service?
+ */
+ val tosAccepted: Boolean,
+
+ /**
+ * Amount that will be transferred to the exchange.
+ */
+ val amountRaw: Amount,
+
+ /**
+ * Amount that will be added to the user's wallet balance.
+ */
+ val amountEffective: Amount
+)