diff options
Diffstat (limited to 'wallet/src/commonMain')
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/lib/wallet/Amount.kt | 26 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/lib/wallet/Base32Crockford.kt (renamed from wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/Base32Crockford.kt) | 2 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/lib/wallet/Db.kt (renamed from wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/Db.kt) | 6 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/lib/wallet/PaytoUri.kt (renamed from wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/PaytoUri.kt) | 2 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/lib/wallet/Time.kt | 24 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/lib/wallet/Types.kt (renamed from wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/Types.kt) | 3 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/lib/wallet/Utils.kt (renamed from wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/Utils.kt) | 2 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/lib/wallet/Version.kt | 26 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/lib/wallet/WalletApi.kt (renamed from wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/WalletApi.kt) | 23 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/lib/wallet/crypto/Crypto.kt (renamed from wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/Crypto.kt) | 2 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/lib/wallet/crypto/CryptoImpl.kt (renamed from wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/CryptoImpl.kt) | 4 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/lib/wallet/crypto/Deposit.kt (renamed from wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/Deposit.kt) | 12 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/lib/wallet/crypto/Kdf.kt (renamed from wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/Kdf.kt) | 2 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/lib/wallet/crypto/Planchet.kt (renamed from wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/Planchet.kt) | 7 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/lib/wallet/crypto/Recoup.kt (renamed from wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/Recoup.kt) | 10 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/lib/wallet/crypto/Refresh.kt (renamed from wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/Refresh.kt) | 23 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/lib/wallet/crypto/Signature.kt (renamed from wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/Signature.kt) | 12 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/lib/wallet/exchange/Auditor.kt (renamed from wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/exchange/Auditor.kt) | 2 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/lib/wallet/exchange/Denomination.kt (renamed from wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/exchange/Denomination.kt) | 14 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/lib/wallet/exchange/Exchange.kt (renamed from wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/exchange/Exchange.kt) | 40 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/lib/wallet/exchange/ExchangeRecord.kt (renamed from wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/exchange/ExchangeRecord.kt) | 4 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/lib/wallet/exchange/Keys.kt (renamed from wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/exchange/Keys.kt) | 4 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/lib/wallet/exchange/Wire.kt (renamed from wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/exchange/Wire.kt) | 6 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/lib/wallet/operations/Withdraw.kt (renamed from wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/operations/Withdraw.kt) | 43 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/Amount.kt | 210 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/TalerUri.kt | 60 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/Time.kt | 85 | ||||
-rw-r--r-- | wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/Version.kt | 78 |
28 files changed, 192 insertions, 540 deletions
diff --git a/wallet/src/commonMain/kotlin/net/taler/lib/wallet/Amount.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/Amount.kt new file mode 100644 index 0000000..7273dba --- /dev/null +++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/Amount.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 + +import net.taler.lib.common.Amount +import net.taler.lib.wallet.crypto.CryptoImpl.Companion.toByteArray + +fun Amount.toByteArray() = ByteArray(8 + 4 + 12).apply { + value.toByteArray().copyInto(this, 0, 0, 8) + fraction.toByteArray().copyInto(this, 8, 0, 4) + currency.encodeToByteArray().copyInto(this, 12) +} diff --git a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/Base32Crockford.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/Base32Crockford.kt index 9043731..2517e85 100644 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/Base32Crockford.kt +++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/Base32Crockford.kt @@ -14,7 +14,7 @@ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -package net.taler.wallet.kotlin +package net.taler.lib.wallet class EncodingException : Exception("Invalid encoding") diff --git a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/Db.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/Db.kt index 3a5ecd6..df809c7 100644 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/Db.kt +++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/Db.kt @@ -14,12 +14,12 @@ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -package net.taler.wallet.kotlin +package net.taler.lib.wallet import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock -import net.taler.wallet.kotlin.exchange.DenominationRecord -import net.taler.wallet.kotlin.exchange.ExchangeRecord +import net.taler.lib.wallet.exchange.DenominationRecord +import net.taler.lib.wallet.exchange.ExchangeRecord internal interface Db { suspend fun put(exchange: ExchangeRecord) diff --git a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/PaytoUri.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/PaytoUri.kt index f6b11d2..2ae9813 100644 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/PaytoUri.kt +++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/PaytoUri.kt @@ -14,7 +14,7 @@ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -package net.taler.wallet.kotlin +package net.taler.lib.wallet data class PaytoUri( val targetType: String, diff --git a/wallet/src/commonMain/kotlin/net/taler/lib/wallet/Time.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/Time.kt new file mode 100644 index 0000000..e67474b --- /dev/null +++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/Time.kt @@ -0,0 +1,24 @@ +/* + * 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 + +import net.taler.lib.common.Timestamp +import net.taler.lib.wallet.crypto.CryptoImpl.Companion.toByteArray + +fun Timestamp.roundedToByteArray(): ByteArray = ByteArray(8).apply { + (truncateSeconds().ms * 1000L).toByteArray().copyInto(this) +} diff --git a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/Types.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/Types.kt index 04b17e7..b068bda 100644 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/Types.kt +++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/Types.kt @@ -14,8 +14,9 @@ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -package net.taler.wallet.kotlin +package net.taler.lib.wallet +import net.taler.lib.common.Amount class CoinRecord( /** diff --git a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/Utils.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/Utils.kt index 2549195..498b8a8 100644 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/Utils.kt +++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/Utils.kt @@ -14,7 +14,7 @@ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -package net.taler.wallet.kotlin +package net.taler.lib.wallet import io.ktor.client.HttpClient import io.ktor.client.features.json.JsonFeature diff --git a/wallet/src/commonMain/kotlin/net/taler/lib/wallet/Version.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/Version.kt new file mode 100644 index 0000000..ba9be3c --- /dev/null +++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/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 + +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/wallet/kotlin/WalletApi.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/WalletApi.kt index 11fd181..bfe2825 100644 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/WalletApi.kt +++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/WalletApi.kt @@ -14,18 +14,21 @@ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -package net.taler.wallet.kotlin +package net.taler.lib.wallet 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 +import net.taler.lib.common.Amount +import net.taler.lib.common.Timestamp +import net.taler.lib.common.Version +import net.taler.lib.wallet.crypto.Crypto +import net.taler.lib.wallet.crypto.CryptoFactory +import net.taler.lib.wallet.crypto.Signature +import net.taler.lib.wallet.exchange.Exchange +import net.taler.lib.wallet.exchange.ExchangeListItem +import net.taler.lib.wallet.exchange.GetExchangeTosResult +import net.taler.lib.wallet.operations.Withdraw +import net.taler.lib.wallet.operations.WithdrawalDetails +import net.taler.lib.wallet.operations.WithdrawalDetailsForUri public class WalletApi { diff --git a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/Crypto.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/crypto/Crypto.kt index 226aa64..cbb486a 100644 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/Crypto.kt +++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/crypto/Crypto.kt @@ -14,7 +14,7 @@ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -package net.taler.wallet.kotlin.crypto +package net.taler.lib.wallet.crypto internal interface Crypto { fun sha256(input: ByteArray): ByteArray diff --git a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/CryptoImpl.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/crypto/CryptoImpl.kt index 0780e45..6b7cb8e 100644 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/CryptoImpl.kt +++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/crypto/CryptoImpl.kt @@ -14,9 +14,7 @@ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -package net.taler.wallet.kotlin.crypto - -import net.taler.wallet.kotlin.crypto.CryptoImpl.Companion.toByteArray +package net.taler.lib.wallet.crypto abstract class CryptoImpl : Crypto { diff --git a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/Deposit.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/crypto/Deposit.kt index 3156d3f..66255d8 100644 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/Deposit.kt +++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/crypto/Deposit.kt @@ -14,12 +14,14 @@ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -package net.taler.wallet.kotlin.crypto +package net.taler.lib.wallet.crypto -import net.taler.wallet.kotlin.Amount -import net.taler.wallet.kotlin.Base32Crockford -import net.taler.wallet.kotlin.Timestamp -import net.taler.wallet.kotlin.crypto.Signature.Companion.WALLET_COIN_DEPOSIT +import net.taler.lib.common.Amount +import net.taler.lib.common.Timestamp +import net.taler.lib.wallet.Base32Crockford +import net.taler.lib.wallet.crypto.Signature.Companion.WALLET_COIN_DEPOSIT +import net.taler.lib.wallet.roundedToByteArray +import net.taler.lib.wallet.toByteArray /** * Deposit operations are requested by a merchant during a transaction. diff --git a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/Kdf.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/crypto/Kdf.kt index 44f55cc..2d714f8 100644 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/Kdf.kt +++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/crypto/Kdf.kt @@ -14,7 +14,7 @@ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -package net.taler.wallet.kotlin.crypto +package net.taler.lib.wallet.crypto import kotlin.experimental.xor import kotlin.math.ceil diff --git a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/Planchet.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/crypto/Planchet.kt index b29007e..22aa786 100644 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/Planchet.kt +++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/crypto/Planchet.kt @@ -14,10 +14,11 @@ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -package net.taler.wallet.kotlin.crypto +package net.taler.lib.wallet.crypto -import net.taler.wallet.kotlin.Amount -import net.taler.wallet.kotlin.Base32Crockford +import net.taler.lib.common.Amount +import net.taler.lib.wallet.Base32Crockford +import net.taler.lib.wallet.toByteArray internal class Planchet(private val crypto: Crypto) { diff --git a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/Recoup.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/crypto/Recoup.kt index 0f2b6df..c7678e4 100644 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/Recoup.kt +++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/crypto/Recoup.kt @@ -14,12 +14,12 @@ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -package net.taler.wallet.kotlin.crypto +package net.taler.lib.wallet.crypto -import net.taler.wallet.kotlin.Base32Crockford -import net.taler.wallet.kotlin.CoinRecord -import net.taler.wallet.kotlin.CoinSourceType.REFRESH -import net.taler.wallet.kotlin.crypto.Signature.Companion.WALLET_COIN_RECOUP +import net.taler.lib.wallet.Base32Crockford +import net.taler.lib.wallet.CoinRecord +import net.taler.lib.wallet.CoinSourceType.REFRESH +import net.taler.lib.wallet.crypto.Signature.Companion.WALLET_COIN_RECOUP internal class Recoup(private val crypto: Crypto) { diff --git a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/Refresh.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/crypto/Refresh.kt index cd24b07..90478ef 100644 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/Refresh.kt +++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/crypto/Refresh.kt @@ -14,17 +14,18 @@ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -package net.taler.wallet.kotlin.crypto - -import net.taler.wallet.kotlin.Amount -import net.taler.wallet.kotlin.Base32Crockford -import net.taler.wallet.kotlin.CoinRecord -import net.taler.wallet.kotlin.Timestamp -import net.taler.wallet.kotlin.crypto.Signature.Companion.WALLET_COIN_LINK -import net.taler.wallet.kotlin.crypto.Signature.Companion.WALLET_COIN_MELT -import net.taler.wallet.kotlin.crypto.Signature.PurposeBuilder -import net.taler.wallet.kotlin.exchange.DenominationSelectionInfo -import net.taler.wallet.kotlin.exchange.SelectedDenomination +package net.taler.lib.wallet.crypto + +import net.taler.lib.common.Amount +import net.taler.lib.wallet.Base32Crockford +import net.taler.lib.wallet.CoinRecord +import net.taler.lib.common.Timestamp +import net.taler.lib.wallet.crypto.Signature.Companion.WALLET_COIN_LINK +import net.taler.lib.wallet.crypto.Signature.Companion.WALLET_COIN_MELT +import net.taler.lib.wallet.crypto.Signature.PurposeBuilder +import net.taler.lib.wallet.exchange.DenominationSelectionInfo +import net.taler.lib.wallet.exchange.SelectedDenomination +import net.taler.lib.wallet.toByteArray internal class Refresh(private val crypto: Crypto) { diff --git a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/Signature.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/crypto/Signature.kt index 9b06756..006c004 100644 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/Signature.kt +++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/crypto/Signature.kt @@ -14,12 +14,14 @@ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -package net.taler.wallet.kotlin.crypto +package net.taler.lib.wallet.crypto -import net.taler.wallet.kotlin.Base32Crockford -import net.taler.wallet.kotlin.crypto.CryptoImpl.Companion.toByteArray -import net.taler.wallet.kotlin.exchange.DenominationRecord -import net.taler.wallet.kotlin.exchange.WireFee +import net.taler.lib.wallet.Base32Crockford +import net.taler.lib.wallet.crypto.CryptoImpl.Companion.toByteArray +import net.taler.lib.wallet.exchange.DenominationRecord +import net.taler.lib.wallet.exchange.WireFee +import net.taler.lib.wallet.roundedToByteArray +import net.taler.lib.wallet.toByteArray internal class Signature(private val crypto: Crypto) { diff --git a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/exchange/Auditor.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/exchange/Auditor.kt index 4df0bdf..248da8d 100644 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/exchange/Auditor.kt +++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/exchange/Auditor.kt @@ -14,7 +14,7 @@ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -package net.taler.wallet.kotlin.exchange +package net.taler.lib.wallet.exchange import kotlinx.serialization.Serializable diff --git a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/exchange/Denomination.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/exchange/Denomination.kt index 88a81fd..fca9e3f 100644 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/exchange/Denomination.kt +++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/exchange/Denomination.kt @@ -14,15 +14,15 @@ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -package net.taler.wallet.kotlin.exchange +package net.taler.lib.wallet.exchange import kotlinx.serialization.Serializable -import net.taler.wallet.kotlin.Amount -import net.taler.wallet.kotlin.Base32Crockford -import net.taler.wallet.kotlin.Duration -import net.taler.wallet.kotlin.Timestamp -import net.taler.wallet.kotlin.exchange.DenominationStatus.Unverified -import net.taler.wallet.kotlin.exchange.DenominationStatus.VerifiedGood +import net.taler.lib.common.Amount +import net.taler.lib.common.Duration +import net.taler.lib.common.Timestamp +import net.taler.lib.wallet.Base32Crockford +import net.taler.lib.wallet.exchange.DenominationStatus.Unverified +import net.taler.lib.wallet.exchange.DenominationStatus.VerifiedGood /** * Denomination as found in the /keys response from the exchange. diff --git a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/exchange/Exchange.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/exchange/Exchange.kt index 7a6ac7f..4d89cd6 100644 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/exchange/Exchange.kt +++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/exchange/Exchange.kt @@ -14,7 +14,7 @@ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -package net.taler.wallet.kotlin.exchange +package net.taler.lib.wallet.exchange import io.ktor.client.HttpClient import io.ktor.client.request.accept @@ -24,23 +24,23 @@ import io.ktor.client.statement.readText import io.ktor.http.ContentType import io.ktor.http.HttpHeaders import io.ktor.http.HttpStatusCode -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.compareVersions -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.DenominationStatus.Unverified -import net.taler.wallet.kotlin.exchange.ExchangeUpdateReason.Initial -import net.taler.wallet.kotlin.exchange.ExchangeUpdateStatus.FetchKeys -import net.taler.wallet.kotlin.exchange.ExchangeUpdateStatus.FetchTerms -import net.taler.wallet.kotlin.exchange.ExchangeUpdateStatus.FetchWire -import net.taler.wallet.kotlin.exchange.ExchangeUpdateStatus.FinalizeUpdate -import net.taler.wallet.kotlin.exchange.ExchangeUpdateStatus.Finished -import net.taler.wallet.kotlin.getDefaultHttpClient +import net.taler.lib.common.Amount +import net.taler.lib.common.Timestamp +import net.taler.lib.common.Version +import net.taler.lib.wallet.Base32Crockford +import net.taler.lib.wallet.Db +import net.taler.lib.wallet.DbFactory +import net.taler.lib.wallet.crypto.Crypto +import net.taler.lib.wallet.crypto.CryptoFactory +import net.taler.lib.wallet.crypto.Signature +import net.taler.lib.wallet.exchange.DenominationStatus.Unverified +import net.taler.lib.wallet.exchange.ExchangeUpdateReason.Initial +import net.taler.lib.wallet.exchange.ExchangeUpdateStatus.FetchKeys +import net.taler.lib.wallet.exchange.ExchangeUpdateStatus.FetchTerms +import net.taler.lib.wallet.exchange.ExchangeUpdateStatus.FetchWire +import net.taler.lib.wallet.exchange.ExchangeUpdateStatus.FinalizeUpdate +import net.taler.lib.wallet.exchange.ExchangeUpdateStatus.Finished +import net.taler.lib.wallet.getDefaultHttpClient internal class Exchange( private val crypto: Crypto = CryptoFactory.getCrypto(), @@ -53,8 +53,8 @@ internal class Exchange( ) { companion object { - private const val PROTOCOL_VERSION = "8:0:0" - fun getVersionMatch(version: String) = compareVersions(PROTOCOL_VERSION, version) + private val PROTOCOL_VERSION = Version(8, 0, 0) + fun getVersionMatch(version: String) = PROTOCOL_VERSION.compare(Version.parse(version)) fun normalizeUrl(exchangeBaseUrl: String): String { var url = exchangeBaseUrl if (!url.startsWith("http")) url = "http://$url" diff --git a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/exchange/ExchangeRecord.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/exchange/ExchangeRecord.kt index 9bfd649..bb8bbd1 100644 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/exchange/ExchangeRecord.kt +++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/exchange/ExchangeRecord.kt @@ -14,9 +14,9 @@ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -package net.taler.wallet.kotlin.exchange +package net.taler.lib.wallet.exchange -import net.taler.wallet.kotlin.Timestamp +import net.taler.lib.common.Timestamp /** * Exchange record as stored in the wallet's database. diff --git a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/exchange/Keys.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/exchange/Keys.kt index 54806f9..12b29db 100644 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/exchange/Keys.kt +++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/exchange/Keys.kt @@ -14,12 +14,12 @@ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -package net.taler.wallet.kotlin.exchange +package net.taler.lib.wallet.exchange import io.ktor.client.HttpClient import io.ktor.client.request.get import kotlinx.serialization.Serializable -import net.taler.wallet.kotlin.Timestamp +import net.taler.lib.common.Timestamp /** * Structure that the exchange gives us in /keys. diff --git a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/exchange/Wire.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/exchange/Wire.kt index c8fae88..0dca4dd 100644 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/exchange/Wire.kt +++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/exchange/Wire.kt @@ -14,14 +14,14 @@ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -package net.taler.wallet.kotlin.exchange +package net.taler.lib.wallet.exchange import io.ktor.client.HttpClient import io.ktor.client.request.get import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import net.taler.wallet.kotlin.Amount -import net.taler.wallet.kotlin.Timestamp +import net.taler.lib.common.Amount +import net.taler.lib.common.Timestamp @Serializable internal data class Wire( diff --git a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/operations/Withdraw.kt b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/operations/Withdraw.kt index e51e9ec..69c68d1 100644 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/operations/Withdraw.kt +++ b/wallet/src/commonMain/kotlin/net/taler/lib/wallet/operations/Withdraw.kt @@ -14,32 +14,33 @@ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -package net.taler.wallet.kotlin.operations +package net.taler.lib.wallet.operations import io.ktor.client.HttpClient import io.ktor.client.request.get import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -import net.taler.wallet.kotlin.Amount -import net.taler.wallet.kotlin.Db -import net.taler.wallet.kotlin.DbFactory -import net.taler.wallet.kotlin.TalerUri.parseWithdrawUri -import net.taler.wallet.kotlin.Timestamp -import net.taler.wallet.kotlin.VersionMatchResult -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.DenominationRecord -import net.taler.wallet.kotlin.exchange.DenominationSelectionInfo -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 -import net.taler.wallet.kotlin.getDefaultHttpClient +import net.taler.lib.common.Amount +import net.taler.lib.common.TalerUri.parseWithdrawUri +import net.taler.lib.common.Timestamp +import net.taler.lib.common.Version +import net.taler.lib.common.Version.VersionMatchResult +import net.taler.lib.wallet.Db +import net.taler.lib.wallet.DbFactory +import net.taler.lib.wallet.crypto.Crypto +import net.taler.lib.wallet.crypto.CryptoFactory +import net.taler.lib.wallet.crypto.Signature +import net.taler.lib.wallet.exchange.DenominationRecord +import net.taler.lib.wallet.exchange.DenominationSelectionInfo +import net.taler.lib.wallet.exchange.DenominationStatus.Unverified +import net.taler.lib.wallet.exchange.DenominationStatus.VerifiedBad +import net.taler.lib.wallet.exchange.DenominationStatus.VerifiedGood +import net.taler.lib.wallet.exchange.Exchange +import net.taler.lib.wallet.exchange.ExchangeListItem +import net.taler.lib.wallet.exchange.ExchangeRecord +import net.taler.lib.wallet.exchange.ExchangeWireInfo +import net.taler.lib.wallet.exchange.SelectedDenomination +import net.taler.lib.wallet.getDefaultHttpClient internal class Withdraw( private val httpClient: HttpClient = getDefaultHttpClient(), diff --git a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/Amount.kt b/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/Amount.kt deleted file mode 100644 index 2d39bb3..0000000 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/Amount.kt +++ /dev/null @@ -1,210 +0,0 @@ -/* - * 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 - -import kotlinx.serialization.Decoder -import kotlinx.serialization.Encoder -import kotlinx.serialization.KSerializer -import kotlinx.serialization.Serializable -import kotlinx.serialization.Serializer -import net.taler.wallet.kotlin.crypto.CryptoImpl.Companion.toByteArray -import kotlin.math.floor -import kotlin.math.pow -import kotlin.math.roundToInt - -class AmountParserException(msg: String? = null, cause: Throwable? = null) : Exception(msg, cause) -class AmountOverflowException(msg: String? = null, cause: Throwable? = null) : Exception(msg, cause) - -@Serializable -data class Amount( - /** - * name of the currency using either a three-character ISO 4217 currency code, - * or a regional currency identifier starting with a "*" followed by at most 10 characters. - * ISO 4217 exponents in the name are not supported, - * although the "fraction" is corresponds to an ISO 4217 exponent of 6. - */ - val currency: String, - - /** - * The integer part may be at most 2^52. - * Note that "1" here would correspond to 1 EUR or 1 USD, depending on currency, not 1 cent. - */ - val value: Long, - - /** - * Unsigned 32 bit fractional value to be added to value representing - * an additional currency fraction, in units of one hundred millionth (1e-8) - * of the base currency value. For example, a fraction - * of 50_000_000 would correspond to 50 cents. - */ - val fraction: Int -) : Comparable<Amount> { - - @Serializer(forClass = Amount::class) - companion object : KSerializer<Amount> { - - private const val FRACTIONAL_BASE: Int = 100000000 // 1e8 - - @Suppress("unused") - private val REGEX = Regex("""^[-_*A-Za-z0-9]{1,12}:([0-9]+)\.?([0-9]+)?$""") - private val REGEX_CURRENCY = Regex("""^[-_*A-Za-z0-9]{1,12}$""") - internal val MAX_VALUE = 2.0.pow(52).toLong() - private const val MAX_FRACTION_LENGTH = 8 - internal const val MAX_FRACTION = 99_999_999 - - fun zero(currency: String): Amount { - return Amount(checkCurrency(currency), 0, 0) - } - - fun fromJSONString(str: String): Amount { - val split = str.split(":") - if (split.size != 2) throw AmountParserException("Invalid Amount Format") - return fromString(split[0], split[1]) - } - - fun fromString(currency: String, str: String): Amount { - // value - val valueSplit = str.split(".") - val value = checkValue(valueSplit[0].toLongOrNull()) - // fraction - val fraction: Int = if (valueSplit.size > 1) { - val fractionStr = valueSplit[1] - if (fractionStr.length > MAX_FRACTION_LENGTH) - throw AmountParserException("Fraction $fractionStr too long") - val fraction = "0.$fractionStr".toDoubleOrNull() - ?.times(FRACTIONAL_BASE) - ?.roundToInt() - checkFraction(fraction) - } else 0 - return Amount(checkCurrency(currency), value, fraction) - } - - fun min(currency: String): Amount = Amount(currency, 0, 1) - fun max(currency: String): Amount = Amount(currency, MAX_VALUE, MAX_FRACTION) - -// fun fromJsonObject(json: JSONObject): Amount { -// val currency = checkCurrency(json.optString("currency")) -// val value = checkValue(json.optString("value").toLongOrNull()) -// val fraction = checkFraction(json.optString("fraction").toIntOrNull()) -// return Amount(currency, value, fraction) -// } - - private fun checkCurrency(currency: String): String { - if (!REGEX_CURRENCY.matches(currency)) - throw AmountParserException("Invalid currency: $currency") - return currency - } - - private fun checkValue(value: Long?): Long { - if (value == null || value > MAX_VALUE) - throw AmountParserException("Value $value greater than $MAX_VALUE") - return value - } - - private fun checkFraction(fraction: Int?): Int { - if (fraction == null || fraction > MAX_FRACTION) - throw AmountParserException("Fraction $fraction greater than $MAX_FRACTION") - return fraction - } - - override fun serialize(encoder: Encoder, value: Amount) { - encoder.encodeString(value.toJSONString()) - } - - override fun deserialize(decoder: Decoder): Amount { - return fromJSONString(decoder.decodeString()) - } - } - - val amountStr: String - get() = if (fraction == 0) "$value" else { - var f = fraction - var fractionStr = "" - while (f > 0) { - fractionStr += f / (FRACTIONAL_BASE / 10) - f = (f * 10) % FRACTIONAL_BASE - } - "$value.$fractionStr" - } - - operator fun plus(other: Amount): Amount { - check(currency == other.currency) { "Can only subtract from same currency" } - val resultValue = value + other.value + floor((fraction + other.fraction).toDouble() / FRACTIONAL_BASE).toLong() - if (resultValue > MAX_VALUE) - throw AmountOverflowException() - val resultFraction = (fraction + other.fraction) % FRACTIONAL_BASE - return Amount(currency, resultValue, resultFraction) - } - - operator fun times(factor: Int): Amount { - // TODO consider replacing with a faster implementation - if (factor == 0) return zero(currency) - var result = this - for (i in 1 until factor) result += this - return result - } - - operator fun minus(other: Amount): Amount { - check(currency == other.currency) { "Can only subtract from same currency" } - var resultValue = value - var resultFraction = fraction - if (resultFraction < other.fraction) { - if (resultValue < 1L) - throw AmountOverflowException() - resultValue-- - resultFraction += FRACTIONAL_BASE - } - check(resultFraction >= other.fraction) - resultFraction -= other.fraction - if (resultValue < other.value) - throw AmountOverflowException() - resultValue -= other.value - return Amount(currency, resultValue, resultFraction) - } - - fun isZero(): Boolean { - return value == 0L && fraction == 0 - } - - fun toJSONString(): String { - return "$currency:$amountStr" - } - - fun toByteArray() = ByteArray(8 + 4 + 12).apply { - value.toByteArray().copyInto(this, 0, 0, 8) - fraction.toByteArray().copyInto(this, 8, 0, 4) - currency.encodeToByteArray().copyInto(this, 12) - } - - override fun toString(): String { - return "$amountStr $currency" - } - - override fun compareTo(other: Amount): Int { - check(currency == other.currency) { "Can only compare amounts with the same currency" } - when { - value == other.value -> { - if (fraction < other.fraction) return -1 - if (fraction > other.fraction) return 1 - return 0 - } - value < other.value -> return -1 - else -> return 1 - } - } - -} diff --git a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/TalerUri.kt b/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/TalerUri.kt deleted file mode 100644 index c489d71..0000000 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/TalerUri.kt +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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 - -internal object TalerUri { - - private const val SCHEME = "taler://" - private const val SCHEME_INSECURE = "taler+http://" - private const val AUTHORITY_PAY = "pay" - private const val AUTHORITY_WITHDRAW = "withdraw" - private const val AUTHORITY_REFUND = "refund" - private const val AUTHORITY_TIP = "tip" - - data class WithdrawUriResult( - val bankIntegrationApiBaseUrl: String, - val withdrawalOperationId: String - ) - - /** - * Parses a withdraw URI and returns a bank status URL or null if the URI was invalid. - */ - fun parseWithdrawUri(uri: String): WithdrawUriResult? { - val (resultScheme, prefix) = when { - uri.startsWith(SCHEME, ignoreCase = true) -> { - Pair("https://", "${SCHEME}${AUTHORITY_WITHDRAW}/") - } - uri.startsWith(SCHEME_INSECURE, ignoreCase = true) -> { - Pair("http://", "${SCHEME_INSECURE}${AUTHORITY_WITHDRAW}/") - } - else -> return null - } - if (!uri.startsWith(prefix)) return null - val parts = uri.let { - (if (it.endsWith("/")) it.dropLast(1) else it).substring(prefix.length).split('/') - } - if (parts.size < 2) return null - val host = parts[0].toLowerCase() - val pathSegments = parts.slice(1 until parts.size - 1).joinToString("/") - val withdrawId = parts.last() - if (withdrawId.isBlank()) return null - val url = "${resultScheme}${host}/${pathSegments}" - - return WithdrawUriResult(url, withdrawId) - } - -} diff --git a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/Time.kt b/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/Time.kt deleted file mode 100644 index 4143389..0000000 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/Time.kt +++ /dev/null @@ -1,85 +0,0 @@ -/* - * 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 - -import com.soywiz.klock.DateTime -import kotlinx.serialization.SerialName -import kotlinx.serialization.Serializable -import net.taler.wallet.kotlin.Duration.Companion.FOREVER -import net.taler.wallet.kotlin.crypto.CryptoImpl.Companion.toByteArray -import kotlin.math.max - -@Serializable -data class Timestamp( - @SerialName("t_ms") - val ms: Long -) : Comparable<Timestamp> { - - companion object { - const val NEVER: Long = -1 // TODO or UINT64_MAX? - fun now(): Timestamp = Timestamp(DateTime.now().unixMillisLong) - } - - /** - * Returns a copy of this [Timestamp] rounded to seconds. - */ - fun truncateSeconds(): Timestamp { - if (ms == NEVER) return Timestamp(ms) - return Timestamp((ms / 1000L) * 1000L) - } - - fun roundedToByteArray(): ByteArray = ByteArray(8).apply { - (truncateSeconds().ms * 1000L).toByteArray().copyInto(this) - } - - operator fun minus(other: Timestamp): Duration = when { - ms == NEVER -> Duration(FOREVER) - other.ms == NEVER -> throw Error("Invalid argument for timestamp comparision") - ms < other.ms -> Duration(0) - else -> Duration(ms - other.ms) - } - - operator fun minus(other: Duration): Timestamp = when { - ms == NEVER -> this - other.ms == FOREVER -> Timestamp(0) - else -> Timestamp(max(0, ms - other.ms)) - } - - override fun compareTo(other: Timestamp): Int { - return if (ms == NEVER) { - if (other.ms == NEVER) 0 - else 1 - } else { - if (other.ms == NEVER) -1 - else ms.compareTo(other.ms) - } - } - -} - -@Serializable -data class Duration( - /** - * Duration in milliseconds. - */ - @SerialName("d_ms") - val ms: Long -) { - companion object { - const val FOREVER: Long = -1 // TODO or UINT64_MAX? - } -} diff --git a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/Version.kt b/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/Version.kt deleted file mode 100644 index 45e7840..0000000 --- a/wallet/src/commonMain/kotlin/net/taler/wallet/kotlin/Version.kt +++ /dev/null @@ -1,78 +0,0 @@ -/* - * 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 - -import kotlin.math.sign - -/** - * Semantic versioning, but libtool-style. - * See https://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html - */ - -/** - * Result of comparing two libtool versions. - */ -data class VersionMatchResult( - /** - * Is the first version compatible with the second? - */ - val compatible: Boolean, - /** - * Is the first version older (-1), newer (+1) or identical (0)? - */ - val currentCmp: Int -) - -data class Version( - val current: Int, - val revision: Int, - val age: Int -) - -/** - * Compare two libtool-style version strings. - */ -fun compareVersions(me: String,other: String): VersionMatchResult? { - val meVer = parseVersion (me) - val otherVer = parseVersion (other) - if (meVer == null || otherVer == null) return null - - val compatible = meVer.current - meVer.age <= otherVer.current && - meVer.current >= otherVer.current - otherVer.age - - val currentCmp = sign((meVer.current - otherVer.current).toDouble()).toInt() - - return VersionMatchResult(compatible, currentCmp) -} - -fun parseVersion(v: String): Version? { - val elements = v.split(":") - if (elements.size != 3) return null - val (currentStr, revisionStr, ageStr) = elements - val current = currentStr.toIntOrNull() - val revision = revisionStr.toIntOrNull() - val age = ageStr.toIntOrNull() - if (current == null || revision == null || age == null) return null - return Version(current, revision, age) -} - -class SupportedVersions( - val walletVersion: Version, - val exchangeVersion: Version, - val bankVersion: Version, - val merchantVersion: Version -) |