diff options
Diffstat (limited to 'src/commonMain/kotlin/net')
3 files changed, 87 insertions, 8 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) + } + } + } diff --git a/src/commonMain/kotlin/net/taler/wallet/kotlin/exchange/Exchange.kt b/src/commonMain/kotlin/net/taler/wallet/kotlin/exchange/Exchange.kt index 3af430f..7a6ac7f 100644 --- a/src/commonMain/kotlin/net/taler/wallet/kotlin/exchange/Exchange.kt +++ b/src/commonMain/kotlin/net/taler/wallet/kotlin/exchange/Exchange.kt @@ -223,3 +223,40 @@ internal class Exchange( } } + + +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/src/commonMain/kotlin/net/taler/wallet/kotlin/operations/Withdraw.kt b/src/commonMain/kotlin/net/taler/wallet/kotlin/operations/Withdraw.kt index 3981012..e51e9ec 100644 --- a/src/commonMain/kotlin/net/taler/wallet/kotlin/operations/Withdraw.kt +++ b/src/commonMain/kotlin/net/taler/wallet/kotlin/operations/Withdraw.kt @@ -35,6 +35,7 @@ import net.taler.wallet.kotlin.exchange.DenominationStatus.Unverified import net.taler.wallet.kotlin.exchange.DenominationStatus.VerifiedBad import net.taler.wallet.kotlin.exchange.DenominationStatus.VerifiedGood import net.taler.wallet.kotlin.exchange.Exchange +import net.taler.wallet.kotlin.exchange.ExchangeListItem import net.taler.wallet.kotlin.exchange.ExchangeRecord import net.taler.wallet.kotlin.exchange.ExchangeWireInfo import net.taler.wallet.kotlin.exchange.SelectedDenomination @@ -286,12 +287,6 @@ data class WithdrawalDetailsForUri( val possibleExchanges: List<ExchangeListItem> ) -data class ExchangeListItem( - val exchangeBaseUrl: String, - val currency: String, - val paytoUris: List<String> -) - data class WithdrawalDetails( /** * Did the user accept the current version of the exchange's terms of service? |