summaryrefslogtreecommitdiff
path: root/src/commonMain/kotlin/net/taler/wallet/kotlin/WalletApi.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/commonMain/kotlin/net/taler/wallet/kotlin/WalletApi.kt')
-rw-r--r--src/commonMain/kotlin/net/taler/wallet/kotlin/WalletApi.kt51
1 files changed, 49 insertions, 2 deletions
diff --git a/src/commonMain/kotlin/net/taler/wallet/kotlin/WalletApi.kt b/src/commonMain/kotlin/net/taler/wallet/kotlin/WalletApi.kt
index 61a3a1a..11fd181 100644
--- a/src/commonMain/kotlin/net/taler/wallet/kotlin/WalletApi.kt
+++ b/src/commonMain/kotlin/net/taler/wallet/kotlin/WalletApi.kt
@@ -16,13 +16,25 @@
package net.taler.wallet.kotlin
+import io.ktor.client.HttpClient
+import net.taler.wallet.kotlin.crypto.Crypto
+import net.taler.wallet.kotlin.crypto.CryptoFactory
+import net.taler.wallet.kotlin.crypto.Signature
+import net.taler.wallet.kotlin.exchange.Exchange
+import net.taler.wallet.kotlin.exchange.ExchangeListItem
+import net.taler.wallet.kotlin.exchange.GetExchangeTosResult
import net.taler.wallet.kotlin.operations.Withdraw
import net.taler.wallet.kotlin.operations.WithdrawalDetails
import net.taler.wallet.kotlin.operations.WithdrawalDetailsForUri
public class WalletApi {
- private val withdrawManager = Withdraw()
+ 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)
public fun getVersions(): SupportedVersions {
return SupportedVersions(
@@ -42,7 +54,10 @@ public class WalletApi {
)
}
- public suspend fun getWithdrawalDetailsForAmount(exchangeBaseUrl: String, amount: Amount): WithdrawalDetails {
+ public suspend fun getWithdrawalDetailsForAmount(
+ exchangeBaseUrl: String,
+ amount: Amount
+ ): WithdrawalDetails {
val details = withdrawManager.getWithdrawalDetails(exchangeBaseUrl, amount)
return WithdrawalDetails(
tosAccepted = details.exchange.termsOfServiceAccepted,
@@ -51,4 +66,36 @@ public class WalletApi {
)
}
+ public suspend fun listExchanges(): List<ExchangeListItem> {
+ return db.listExchanges().mapNotNull { exchange ->
+ ExchangeListItem.fromExchangeRecord(exchange)
+ }
+ }
+
+ public suspend fun addExchange(exchangeBaseUrl: String): ExchangeListItem {
+ val exchange = exchangeManager.updateFromUrl(exchangeBaseUrl)
+ db.put(exchange)
+ return ExchangeListItem.fromExchangeRecord(exchange) ?: TODO("error handling")
+ }
+
+ public 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
+ )
+ }
+
+ public 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)
+ }
+ }
+
}