aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTorsten Grote <t@grobox.de>2022-11-17 13:53:48 -0300
committerTorsten Grote <t@grobox.de>2023-01-03 08:25:05 -0300
commit02ad9bde3e671a3566d68a6b968efe288e67d910 (patch)
tree9ff3cfc86c31def600fbdad0bba9bf103e65368f
parentec0738e633076e2d601a624eb8c4b06b6ebf22d8 (diff)
downloadtaler-android-02ad9bde3e671a3566d68a6b968efe288e67d910.tar.gz
taler-android-02ad9bde3e671a3566d68a6b968efe288e67d910.tar.bz2
taler-android-02ad9bde3e671a3566d68a6b968efe288e67d910.zip
Adapt to new seconds based time format
-rw-r--r--merchant-lib/src/main/java/net/taler/merchantlib/Orders.kt4
-rw-r--r--merchant-lib/src/test/java/net/taler/merchantlib/MerchantApiTest.kt9
-rw-r--r--merchant-terminal/src/main/java/net/taler/merchantpos/order/Order.kt2
-rw-r--r--merchant-terminal/src/main/java/net/taler/merchantpos/payment/PaymentManager.kt4
-rw-r--r--taler-kotlin-android/src/main/java/net/taler/common/Time.kt68
-rw-r--r--taler-kotlin-android/src/test/java/net/taler/common/ContractTermsTest.kt8
-rw-r--r--taler-kotlin-android/src/test/java/net/taler/common/TimeTest.kt10
-rw-r--r--wallet/src/main/java/net/taler/wallet/deposit/TransactionDepositComposable.kt2
-rw-r--r--wallet/src/main/java/net/taler/wallet/peer/TransactionPeerPullCredit.kt4
-rw-r--r--wallet/src/main/java/net/taler/wallet/peer/TransactionPeerPullDebit.kt4
-rw-r--r--wallet/src/main/java/net/taler/wallet/peer/TransactionPeerPushCredit.kt4
-rw-r--r--wallet/src/main/java/net/taler/wallet/peer/TransactionPeerPushDebit.kt4
-rw-r--r--wallet/src/test/java/net/taler/wallet/payment/PaymentResponsesTest.kt8
13 files changed, 53 insertions, 78 deletions
diff --git a/merchant-lib/src/main/java/net/taler/merchantlib/Orders.kt b/merchant-lib/src/main/java/net/taler/merchantlib/Orders.kt
index 391abf5..9348ded 100644
--- a/merchant-lib/src/main/java/net/taler/merchantlib/Orders.kt
+++ b/merchant-lib/src/main/java/net/taler/merchantlib/Orders.kt
@@ -21,14 +21,14 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonClassDiscriminator
import net.taler.common.ContractTerms
-import net.taler.common.Duration
+import net.taler.common.RelativeTime
@Serializable
data class PostOrderRequest(
@SerialName("order")
val contractTerms: ContractTerms,
@SerialName("refund_delay")
- val refundDelay: Duration? = null
+ val refundDelay: RelativeTime? = null
)
@Serializable
diff --git a/merchant-lib/src/test/java/net/taler/merchantlib/MerchantApiTest.kt b/merchant-lib/src/test/java/net/taler/merchantlib/MerchantApiTest.kt
index a89e2d3..9d6defd 100644
--- a/merchant-lib/src/test/java/net/taler/merchantlib/MerchantApiTest.kt
+++ b/merchant-lib/src/test/java/net/taler/merchantlib/MerchantApiTest.kt
@@ -19,7 +19,6 @@ package net.taler.merchantlib
import io.ktor.http.HttpStatusCode.Companion.NotFound
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
-import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import net.taler.common.Amount
import net.taler.common.ContractProduct
@@ -173,7 +172,7 @@ class MerchantApiTest {
"order_id": "2020.217-0281FGXCS25P2",
"row_id": 183,
"timestamp": {
- "t_ms": 1596542338000
+ "t_s": 1596542338
},
"amount": "TESTKUDOS:1",
"summary": "Chips",
@@ -184,7 +183,7 @@ class MerchantApiTest {
"order_id": "2020.216-01G2ZPXSP6BYT",
"row_id": 154,
"timestamp": {
- "t_ms": 1596468174000
+ "t_s": 1596468174
},
"amount": "TESTKUDOS:0.8",
"summary": "Peanuts",
@@ -203,7 +202,7 @@ class MerchantApiTest {
assertEquals(true, order1.paid)
assertEquals(true, order1.refundable)
assertEquals("Chips", order1.summary)
- assertEquals(Timestamp(1596542338000), order1.timestamp)
+ assertEquals(Timestamp.fromMillis(1596542338000), order1.timestamp)
val order2 = it.orders[1]
assertEquals(Amount("TESTKUDOS", 0, 80000000), order2.amount)
@@ -211,7 +210,7 @@ class MerchantApiTest {
assertEquals(false, order2.paid)
assertEquals(false, order2.refundable)
assertEquals("Peanuts", order2.summary)
- assertEquals(Timestamp(1596468174000), order2.timestamp)
+ assertEquals(Timestamp.fromMillis(1596468174000), order2.timestamp)
}
}
diff --git a/merchant-terminal/src/main/java/net/taler/merchantpos/order/Order.kt b/merchant-terminal/src/main/java/net/taler/merchantpos/order/Order.kt
index 9860dbd..2d4d32c 100644
--- a/merchant-terminal/src/main/java/net/taler/merchantpos/order/Order.kt
+++ b/merchant-terminal/src/main/java/net/taler/merchantpos/order/Order.kt
@@ -117,7 +117,7 @@ data class Order(val id: Int, val currency: String, val availableCategories: Map
}
fun toContractTerms(): ContractTerms {
- val deadline = Timestamp(now() + HOURS.toMillis(1))
+ val deadline = Timestamp.fromMillis(now() + HOURS.toMillis(1))
return ContractTerms(
summary = summary,
summaryI18n = summaryI18n,
diff --git a/merchant-terminal/src/main/java/net/taler/merchantpos/payment/PaymentManager.kt b/merchant-terminal/src/main/java/net/taler/merchantpos/payment/PaymentManager.kt
index d703a31..02f66fa 100644
--- a/merchant-terminal/src/main/java/net/taler/merchantpos/payment/PaymentManager.kt
+++ b/merchant-terminal/src/main/java/net/taler/merchantpos/payment/PaymentManager.kt
@@ -26,7 +26,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
-import net.taler.common.Duration
+import net.taler.common.RelativeTime
import net.taler.common.assertUiThread
import net.taler.merchantlib.CheckPaymentResponse
import net.taler.merchantlib.MerchantApi
@@ -74,7 +74,7 @@ class PaymentManager(
mPayment.value = Payment(order, order.summary, configManager.currency!!)
val request = PostOrderRequest(
contractTerms = order.toContractTerms(),
- refundDelay = Duration(HOURS.toMillis(1))
+ refundDelay = RelativeTime.fromMillis(HOURS.toMillis(1))
)
api.postOrder(merchantConfig, request).handle(::onNetworkError) { orderResponse ->
assertUiThread()
diff --git a/taler-kotlin-android/src/main/java/net/taler/common/Time.kt b/taler-kotlin-android/src/main/java/net/taler/common/Time.kt
index 61bbce8..f280d5f 100644
--- a/taler-kotlin-android/src/main/java/net/taler/common/Time.kt
+++ b/taler-kotlin-android/src/main/java/net/taler/common/Time.kt
@@ -29,50 +29,31 @@ import kotlin.math.max
@Serializable
data class Timestamp(
- @SerialName("t_ms")
- @Serializable(NeverSerializer::class)
- val old_ms: Long? = null,
@SerialName("t_s")
@Serializable(NeverSerializer::class)
- private val s: Long? = null,
+ private val s: Long,
) : Comparable<Timestamp> {
- constructor(ms: Long) : this(ms, null)
-
companion object {
private const val NEVER: Long = -1
- fun now(): Timestamp = Timestamp(System.currentTimeMillis())
+ fun now(): Timestamp = fromMillis(System.currentTimeMillis())
fun never(): Timestamp = Timestamp(NEVER)
+ fun fromMillis(ms: Long) = Timestamp(ms / 1000L)
}
- val ms: Long = if (s != null) {
- s * 1000L
- } else if (old_ms !== null) {
- old_ms
- } else {
- throw Exception("timestamp didn't have t_s or t_ms")
- }
-
-
- /**
- * Returns a copy of this [Timestamp] rounded to seconds.
- */
- fun truncateSeconds(): Timestamp {
- if (ms == NEVER) return Timestamp(ms)
- return Timestamp((ms / 1000L) * 1000L)
- }
+ val ms: Long = s * 1000L
- operator fun minus(other: Timestamp): Duration = when {
- ms == NEVER -> Duration(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: Timestamp): RelativeTime = when {
+ ms == NEVER -> RelativeTime.fromMillis(RelativeTime.FOREVER)
+ other.ms == NEVER -> throw Error("Invalid argument for timestamp comparison")
+ ms < other.ms -> RelativeTime.fromMillis(0)
+ else -> RelativeTime.fromMillis(ms - other.ms)
}
- operator fun minus(other: Duration): Timestamp = when {
+ operator fun minus(other: RelativeTime): Timestamp = when {
ms == NEVER -> this
- other.ms == Duration.FOREVER -> Timestamp(0)
- else -> Timestamp(max(0, ms - other.ms))
+ other.ms == RelativeTime.FOREVER -> fromMillis(0)
+ else -> fromMillis(max(0, ms - other.ms))
}
override fun compareTo(other: Timestamp): Int {
@@ -88,26 +69,21 @@ data class Timestamp(
}
@Serializable
-data class Duration(
- @SerialName("d_ms")
- @Serializable(ForeverSerializer::class) val old_ms: Long? = null,
- @SerialName("d_s")
+data class RelativeTime(
+ /**
+ * Duration in microseconds or "forever" to represent an infinite duration.
+ * Numeric values are capped at 2^53 - 1 inclusive.
+ */
+ @SerialName("d_us")
@Serializable(ForeverSerializer::class)
- private val s: Long? = null,
+ private val s: Long,
) {
- val ms: Long = if (s != null) {
- s * 1000L
- } else if (old_ms !== null) {
- old_ms
- } else {
- throw Exception("duration didn't have d_s or d_ms")
- }
-
- constructor(ms: Long) : this(ms, null)
+ val ms: Long = s * 1000L
companion object {
internal const val FOREVER: Long = -1
- fun forever(): Duration = Duration(FOREVER)
+ fun forever(): RelativeTime = fromMillis(FOREVER)
+ fun fromMillis(ms: Long) = RelativeTime(ms / 100L)
}
}
diff --git a/taler-kotlin-android/src/test/java/net/taler/common/ContractTermsTest.kt b/taler-kotlin-android/src/test/java/net/taler/common/ContractTermsTest.kt
index f7b83a9..3e1ebc4 100644
--- a/taler-kotlin-android/src/test/java/net/taler/common/ContractTermsTest.kt
+++ b/taler-kotlin-android/src/test/java/net/taler/common/ContractTermsTest.kt
@@ -37,14 +37,14 @@ class ContractTermsTest {
},
"fulfillment_url":"https://shop.test.taler.net/essay/1._The_Free_Software_Definition",
"summary":"Essay: 1. The Free Software Definition",
- "refund_deadline":{"t_ms":"never"},
- "wire_transfer_deadline":{"t_ms":1596128564000},
+ "refund_deadline":{"t_s":"never"},
+ "wire_transfer_deadline":{"t_s":1596128564},
"products":[],
"h_wire":"KV40K023N8EC1F5100TYNS23C4XN68Y1Z3PTJSWFGTMCNYD54KT4S791V2VQ91SZANN86VDAA369M4VEZ0KR6DN71EVRRZA71K681M0",
"wire_method":"x-taler-bank",
"order_id":"2020.212-01M9VKEAPF76C",
- "timestamp":{"t_ms":1596128114000},
- "pay_deadline":{"t_ms":"never"},
+ "timestamp":{"t_s":1596128114},
+ "pay_deadline":{"t_s":"never"},
"max_wire_fee":"TESTKUDOS:1",
"max_fee":"TESTKUDOS:1",
"wire_fee_amortization":3,
diff --git a/taler-kotlin-android/src/test/java/net/taler/common/TimeTest.kt b/taler-kotlin-android/src/test/java/net/taler/common/TimeTest.kt
index beda621..4049940 100644
--- a/taler-kotlin-android/src/test/java/net/taler/common/TimeTest.kt
+++ b/taler-kotlin-android/src/test/java/net/taler/common/TimeTest.kt
@@ -18,9 +18,9 @@ package net.taler.common
import kotlinx.serialization.json.Json.Default.decodeFromString
import kotlinx.serialization.json.Json.Default.encodeToString
-import kotlin.random.Random
import org.junit.Assert.assertEquals
import org.junit.Test
+import kotlin.random.Random
// TODO test other functionality of Timestamp and Duration
class TimeTest {
@@ -29,18 +29,18 @@ class TimeTest {
fun testSerialize() {
for (i in 0 until 42) {
val t = Random.nextLong()
- assertEquals("""{"t_ms":$t}""", encodeToString(Timestamp.serializer(), Timestamp(t)))
+ assertEquals("""{"t_s":$t}""", encodeToString(Timestamp.serializer(), Timestamp(t)))
}
- assertEquals("""{"t_ms":"never"}""", encodeToString(Timestamp.serializer(), Timestamp.never()))
+ assertEquals("""{"t_s":"never"}""", encodeToString(Timestamp.serializer(), Timestamp.never()))
}
@Test
fun testDeserialize() {
for (i in 0 until 42) {
val t = Random.nextLong()
- assertEquals(Timestamp(t), decodeFromString(Timestamp.serializer(), """{ "t_ms": $t }"""))
+ assertEquals(Timestamp(t), decodeFromString(Timestamp.serializer(), """{ "t_s": $t }"""))
}
- assertEquals(Timestamp.never(), decodeFromString(Timestamp.serializer(), """{ "t_ms": "never" }"""))
+ assertEquals(Timestamp.never(), decodeFromString(Timestamp.serializer(), """{ "t_s": "never" }"""))
}
}
diff --git a/wallet/src/main/java/net/taler/wallet/deposit/TransactionDepositComposable.kt b/wallet/src/main/java/net/taler/wallet/deposit/TransactionDepositComposable.kt
index aa75c56..7c5c7a2 100644
--- a/wallet/src/main/java/net/taler/wallet/deposit/TransactionDepositComposable.kt
+++ b/wallet/src/main/java/net/taler/wallet/deposit/TransactionDepositComposable.kt
@@ -82,7 +82,7 @@ fun TransactionDepositComposable(t: TransactionDeposit, onDelete: () -> Unit) {
fun TransactionDepositComposablePreview() {
val t = TransactionDeposit(
transactionId = "transactionId",
- timestamp = Timestamp(System.currentTimeMillis() - 360 * 60 * 1000),
+ timestamp = Timestamp.fromMillis(System.currentTimeMillis() - 360 * 60 * 1000),
pending = true,
depositGroupId = "fooBar",
amountRaw = Amount.fromDouble("TESTKUDOS", 42.1337),
diff --git a/wallet/src/main/java/net/taler/wallet/peer/TransactionPeerPullCredit.kt b/wallet/src/main/java/net/taler/wallet/peer/TransactionPeerPullCredit.kt
index 3179024..82a16be 100644
--- a/wallet/src/main/java/net/taler/wallet/peer/TransactionPeerPullCredit.kt
+++ b/wallet/src/main/java/net/taler/wallet/peer/TransactionPeerPullCredit.kt
@@ -81,13 +81,13 @@ fun ColumnScope.TransactionPeerPullCreditComposable(t: TransactionPeerPullCredit
fun TransactionPeerPullCreditPreview() {
val t = TransactionPeerPullCredit(
transactionId = "transactionId",
- timestamp = Timestamp(System.currentTimeMillis() - 360 * 60 * 1000),
+ timestamp = Timestamp.fromMillis(System.currentTimeMillis() - 360 * 60 * 1000),
pending = true,
exchangeBaseUrl = "https://exchange.example.org/",
amountRaw = Amount.fromDouble("TESTKUDOS", 42.23),
amountEffective = Amount.fromDouble("TESTKUDOS", 42.1337),
info = PeerInfoShort(
- expiration = Timestamp(System.currentTimeMillis() + 60 * 60 * 1000),
+ expiration = Timestamp.fromMillis(System.currentTimeMillis() + 60 * 60 * 1000),
summary = "test invoice",
),
talerUri = "https://exchange.example.org/peer/pull/credit",
diff --git a/wallet/src/main/java/net/taler/wallet/peer/TransactionPeerPullDebit.kt b/wallet/src/main/java/net/taler/wallet/peer/TransactionPeerPullDebit.kt
index 823126b..fc14ec1 100644
--- a/wallet/src/main/java/net/taler/wallet/peer/TransactionPeerPullDebit.kt
+++ b/wallet/src/main/java/net/taler/wallet/peer/TransactionPeerPullDebit.kt
@@ -61,13 +61,13 @@ fun TransactionPeerPullDebitComposable(t: TransactionPeerPullDebit) {
fun TransactionPeerPullDebitPreview() {
val t = TransactionPeerPullDebit(
transactionId = "transactionId",
- timestamp = Timestamp(System.currentTimeMillis() - 360 * 60 * 1000),
+ timestamp = Timestamp.fromMillis(System.currentTimeMillis() - 360 * 60 * 1000),
pending = true,
exchangeBaseUrl = "https://exchange.example.org/",
amountRaw = Amount.fromDouble("TESTKUDOS", 42.1337),
amountEffective = Amount.fromDouble("TESTKUDOS", 42.23),
info = PeerInfoShort(
- expiration = Timestamp(System.currentTimeMillis() + 60 * 60 * 1000),
+ expiration = Timestamp.fromMillis(System.currentTimeMillis() + 60 * 60 * 1000),
summary = "test invoice",
),
)
diff --git a/wallet/src/main/java/net/taler/wallet/peer/TransactionPeerPushCredit.kt b/wallet/src/main/java/net/taler/wallet/peer/TransactionPeerPushCredit.kt
index b986f57..ae8e7ac 100644
--- a/wallet/src/main/java/net/taler/wallet/peer/TransactionPeerPushCredit.kt
+++ b/wallet/src/main/java/net/taler/wallet/peer/TransactionPeerPushCredit.kt
@@ -61,13 +61,13 @@ fun TransactionPeerPushCreditComposable(t: TransactionPeerPushCredit) {
fun TransactionPeerPushCreditPreview() {
val t = TransactionPeerPushCredit(
transactionId = "transactionId",
- timestamp = Timestamp(System.currentTimeMillis() - 360 * 60 * 1000),
+ timestamp = Timestamp.fromMillis(System.currentTimeMillis() - 360 * 60 * 1000),
pending = true,
exchangeBaseUrl = "https://exchange.example.org/",
amountRaw = Amount.fromDouble("TESTKUDOS", 42.23),
amountEffective = Amount.fromDouble("TESTKUDOS", 42.1337),
info = PeerInfoShort(
- expiration = Timestamp(System.currentTimeMillis() + 60 * 60 * 1000),
+ expiration = Timestamp.fromMillis(System.currentTimeMillis() + 60 * 60 * 1000),
summary = "test invoice",
),
)
diff --git a/wallet/src/main/java/net/taler/wallet/peer/TransactionPeerPushDebit.kt b/wallet/src/main/java/net/taler/wallet/peer/TransactionPeerPushDebit.kt
index 18528f9..2eb1c86 100644
--- a/wallet/src/main/java/net/taler/wallet/peer/TransactionPeerPushDebit.kt
+++ b/wallet/src/main/java/net/taler/wallet/peer/TransactionPeerPushDebit.kt
@@ -79,13 +79,13 @@ fun ColumnScope.TransactionPeerPushDebitComposable(t: TransactionPeerPushDebit)
fun TransactionPeerPushDebitPreview() {
val t = TransactionPeerPushDebit(
transactionId = "transactionId",
- timestamp = Timestamp(System.currentTimeMillis() - 360 * 60 * 1000),
+ timestamp = Timestamp.fromMillis(System.currentTimeMillis() - 360 * 60 * 1000),
pending = true,
exchangeBaseUrl = "https://exchange.example.org/",
amountRaw = Amount.fromDouble("TESTKUDOS", 42.1337),
amountEffective = Amount.fromDouble("TESTKUDOS", 42.23),
info = PeerInfoShort(
- expiration = Timestamp(System.currentTimeMillis() + 60 * 60 * 1000),
+ expiration = Timestamp.fromMillis(System.currentTimeMillis() + 60 * 60 * 1000),
summary = "test invoice",
),
talerUri = "https://exchange.example.org/peer/pull/credit",
diff --git a/wallet/src/test/java/net/taler/wallet/payment/PaymentResponsesTest.kt b/wallet/src/test/java/net/taler/wallet/payment/PaymentResponsesTest.kt
index da2378e..f1eaa76 100644
--- a/wallet/src/test/java/net/taler/wallet/payment/PaymentResponsesTest.kt
+++ b/wallet/src/test/java/net/taler/wallet/payment/PaymentResponsesTest.kt
@@ -42,16 +42,16 @@ class PaymentResponsesTest {
"wire_method": "x-taler-bank",
"order_id": "2020.240-01MD5F476HMXW",
"timestamp": {
- "t_ms": 1598538535000
+ "t_s": 1598538535
},
"refund_deadline": {
- "t_ms": 1598538835000
+ "t_s": 1598538835
},
"pay_deadline": {
- "t_ms": 1598538835000
+ "t_s": 1598538835
},
"wire_transfer_deadline": {
- "t_ms": 1598542135000
+ "t_s": 1598542135
},
"max_wire_fee": "CHF:0.1",
"max_fee": "CHF:0.1",