summaryrefslogtreecommitdiff
path: root/src/commonMain/kotlin/net/taler/wallet/kotlin/reserve/ReserveManager.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/commonMain/kotlin/net/taler/wallet/kotlin/reserve/ReserveManager.kt')
-rw-r--r--src/commonMain/kotlin/net/taler/wallet/kotlin/reserve/ReserveManager.kt126
1 files changed, 126 insertions, 0 deletions
diff --git a/src/commonMain/kotlin/net/taler/wallet/kotlin/reserve/ReserveManager.kt b/src/commonMain/kotlin/net/taler/wallet/kotlin/reserve/ReserveManager.kt
new file mode 100644
index 0000000..c8e7bd4
--- /dev/null
+++ b/src/commonMain/kotlin/net/taler/wallet/kotlin/reserve/ReserveManager.kt
@@ -0,0 +1,126 @@
+/*
+ * 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.kotlin.reserve
+
+import io.ktor.client.HttpClient
+import net.taler.wallet.kotlin.Amount
+import net.taler.wallet.kotlin.Base32Crockford
+import net.taler.wallet.kotlin.Db
+import net.taler.wallet.kotlin.DbFactory
+import net.taler.wallet.kotlin.Timestamp
+import net.taler.wallet.kotlin.crypto.Crypto
+import net.taler.wallet.kotlin.crypto.CryptoFactory
+import net.taler.wallet.kotlin.exchange.Exchange
+import net.taler.wallet.kotlin.getDefaultHttpClient
+import net.taler.wallet.kotlin.reserve.ReserveRecordStatus.REGISTERING_BANK
+import net.taler.wallet.kotlin.reserve.ReserveRecordStatus.UNCONFIRMED
+
+internal class ReserveManager(
+ private val crypto: Crypto = CryptoFactory.getCrypto(),
+ private val httpClient: HttpClient = getDefaultHttpClient(),
+ private val db: Db = DbFactory().openDb()
+) {
+
+ /**
+ * Request to create a reserve.
+ */
+ data class CreateReserveRequest(
+ /**
+ * The initial amount for the reserve.
+ */
+ val amount: Amount,
+
+ /**
+ * Exchange URL where the bank should create the reserve.
+ */
+ val exchangeBaseUrl: String,
+
+ /**
+ * Payto URI that identifies the exchange's account that the funds for this reserve go into.
+ */
+ val exchangePaytoUri: String,
+
+ /**
+ * Wire details (as a payto URI) for the bank account that sent the funds to the exchange.
+ */
+ val senderPaytoUri: String? = null,
+
+ /**
+ * URL to fetch the withdraw status from the bank.
+ */
+ val bankWithdrawStatusUrl: String? = null
+ )
+
+ /**
+ * Response for the create reserve request to the wallet.
+ */
+ data class CreateReserveResponse(
+ /**
+ * Exchange URL where the bank should create the reserve.
+ * The URL is normalized in the response.
+ */
+ val exchangeBaseUrl: String,
+
+ /**
+ * Reserve public key of the newly created reserve.
+ */
+ val reservePub: String
+ )
+
+ /**
+ * Create a reserve, but do not flag it as confirmed yet.
+ */
+ fun create(request: CreateReserveRequest): CreateReserveResponse {
+ val keyPair = crypto.createEddsaKeyPair()
+ val now = Timestamp.now()
+ val exchangeBaseUrl = Exchange.normalizeUrl(request.exchangeBaseUrl)
+ val bankInfo = request.bankWithdrawStatusUrl?.let { bankWithdrawStatusUrl ->
+ ReserveBankInfo(
+ statusUrl = bankWithdrawStatusUrl,
+ amount = request.amount,
+ bankWithdrawalGroupId = Base32Crockford.encode(crypto.getRandomBytes(32)),
+ withdrawalStarted = false
+ )
+ }
+ val reserve = Reserve(
+ reservePub = keyPair.encodedPublicKey,
+ reservePriv = keyPair.encodedPrivateKey,
+ exchangeBaseUrl = exchangeBaseUrl,
+ currency = request.amount.currency,
+ timestampCreated = now,
+ senderPaytoUri = request.senderPaytoUri,
+ exchangePaytoUri = request.exchangePaytoUri,
+ bankInfo = bankInfo,
+ reserveStatus = if (request.bankWithdrawStatusUrl == null) UNCONFIRMED else REGISTERING_BANK,
+ retryInfo = RetryInfo.getInitial()
+ )
+ // TODO store reserve history in DB
+ // TODO store senderPaytoUri in DB
+ // TODO store currency record in DB
+ // TODO store withdraw URI in DB
+// db.put(reserve)
+ return CreateReserveResponse(
+ exchangeBaseUrl = exchangeBaseUrl,
+ reservePub = keyPair.encodedPublicKey
+ )
+ }
+
+ fun processBankStatus(reservePub: String) {
+ TODO("Not yet implemented")
+ }
+
+}