gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-wallet-core] branch master updated (a49959d2c -> e56d3ba8e)


From: gnunet
Subject: [taler-wallet-core] branch master updated (a49959d2c -> e56d3ba8e)
Date: Tue, 21 Feb 2023 01:15:34 +0100

This is an automated email from the git hooks/post-receive script.

dold pushed a change to branch master
in repository wallet-core.

    from a49959d2c wallet-core: support long-polling for peer push credit
     new b9b1dd73f taler-util: parse duration strings
     new e56d3ba8e taler-wallet-cli: make purse expiration configurable

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../taler-util/src/{payto.test.ts => time.test.ts} | 34 ++++++++++------
 packages/taler-util/src/time.ts                    | 43 ++++++++++++++++++++
 packages/taler-wallet-cli/src/index.ts             | 46 +++++++++++++++-------
 .../taler-wallet-core/src/operations/pay-peer.ts   |  6 ++-
 .../taler-wallet-core/src/operations/withdraw.ts   |  2 -
 5 files changed, 101 insertions(+), 30 deletions(-)
 copy packages/taler-util/src/{payto.test.ts => time.test.ts} (57%)

diff --git a/packages/taler-util/src/payto.test.ts 
b/packages/taler-util/src/time.test.ts
similarity index 57%
copy from packages/taler-util/src/payto.test.ts
copy to packages/taler-util/src/time.test.ts
index 66a05b3a2..876b35b9a 100644
--- a/packages/taler-util/src/payto.test.ts
+++ b/packages/taler-util/src/time.test.ts
@@ -15,17 +15,25 @@
  */
 
 import test from "ava";
-
-import { parsePaytoUri } from "./payto.js";
-
-test("basic payto parsing", (t) => {
-  const r1 = parsePaytoUri("https://example.com/";);
-  t.is(r1, undefined);
-
-  const r2 = parsePaytoUri("payto:blabla");
-  t.is(r2, undefined);
-
-  const r3 = parsePaytoUri("payto://x-taler-bank/123");
-  t.is(r3?.targetType, "x-taler-bank");
-  t.is(r3?.targetPath, "123");
+import { Duration } from "./time.js";
+
+test("duration parsing", (t) => {
+  const d1 = Duration.fromPrettyString("1h");
+  t.deepEqual(d1.d_ms, 60 * 60 * 1000);
+
+  const d2 = Duration.fromPrettyString("  2h 1s 3m");
+  t.deepEqual(d2.d_ms, 2 * 60 * 60 * 1000 + 3 * 60 * 1000 + 1000);
+
+  t.throws(() => {
+    Duration.fromPrettyString("5g");
+  });
+  t.throws(() => {
+    Duration.fromPrettyString("s");
+  });
+  t.throws(() => {
+    Duration.fromPrettyString("s5");
+  });
+  t.throws(() => {
+    Duration.fromPrettyString("5 5 s");
+  });
 });
diff --git a/packages/taler-util/src/time.ts b/packages/taler-util/src/time.ts
index 21963ee6b..8b4f4cac3 100644
--- a/packages/taler-util/src/time.ts
+++ b/packages/taler-util/src/time.ts
@@ -113,6 +113,49 @@ export namespace Duration {
     return { d_ms: deadline.t_ms - now.t_ms };
   }
 
+  export function fromPrettyString(s: string): Duration {
+    let dMs = 0;
+    let currentNum = "";
+    let parsingNum = true;
+    for (let i = 0; i < s.length; i++) {
+      const cc = s.charCodeAt(i);
+      if (cc >= "0".charCodeAt(0) && cc <= "9".charCodeAt(0)) {
+        if (!parsingNum) {
+          throw Error("invalid duration, unexpected number");
+        }
+        currentNum += s[i];
+        continue;
+      }
+      if (s[i] == " ") {
+        if (currentNum != "") {
+          parsingNum = false;
+        }
+        continue;
+      }
+
+      if (currentNum == "") {
+        throw Error("invalid duration, missing number");
+      }
+
+      if (s[i] === "s") {
+        dMs += 1000 * Number.parseInt(currentNum, 10);
+      } else if (s[i] === "m") {
+        dMs += 60 * 1000 * Number.parseInt(currentNum, 10);
+      } else if (s[i] === "h") {
+        dMs += 60 * 60 * 1000 * Number.parseInt(currentNum, 10);
+      } else if (s[i] === "d") {
+        dMs += 24 * 60 * 60 * 1000 * Number.parseInt(currentNum, 10);
+      } else {
+        throw Error("invalid duration, unsupported unit");
+      }
+      currentNum = "";
+      parsingNum = true;
+    }
+    return {
+      d_ms: dMs,
+    };
+  }
+
   export function max(d1: Duration, d2: Duration): Duration {
     return durationMax(d1, d2);
   }
diff --git a/packages/taler-wallet-cli/src/index.ts 
b/packages/taler-wallet-cli/src/index.ts
index 30959d9ed..93cd5457a 100644
--- a/packages/taler-wallet-cli/src/index.ts
+++ b/packages/taler-wallet-cli/src/index.ts
@@ -1043,8 +1043,23 @@ peerCli
   .maybeOption("summary", ["--summary"], clk.STRING, {
     help: "Summary to use in the contract terms.",
   })
+  .maybeOption("purseExpiration", ["--purse-expiration"], clk.STRING)
   .maybeOption("exchangeBaseUrl", ["--exchange"], clk.STRING)
   .action(async (args) => {
+    let purseExpiration: AbsoluteTime;
+
+    if (args.initiatePayPull.purseExpiration) {
+      purseExpiration = AbsoluteTime.addDuration(
+        AbsoluteTime.now(),
+        Duration.fromPrettyString(args.initiatePayPull.purseExpiration),
+      );
+    } else {
+      purseExpiration = AbsoluteTime.addDuration(
+        AbsoluteTime.now(),
+        Duration.fromSpec({ hours: 1 }),
+      );
+    }
+
     await withWallet(args, async (wallet) => {
       const resp = await wallet.client.call(
         WalletApiOperation.InitiatePeerPullCredit,
@@ -1053,13 +1068,7 @@ peerCli
           partialContractTerms: {
             amount: args.initiatePayPull.amount,
             summary: args.initiatePayPull.summary ?? "Invoice",
-            // FIXME: Make the expiration configurable
-            purse_expiration: AbsoluteTime.toTimestamp(
-              AbsoluteTime.addDuration(
-                AbsoluteTime.now(),
-                Duration.fromSpec({ hours: 1 }),
-              ),
-            ),
+            purse_expiration: AbsoluteTime.toTimestamp(purseExpiration),
           },
         },
       );
@@ -1092,7 +1101,22 @@ peerCli
   .maybeOption("summary", ["--summary"], clk.STRING, {
     help: "Summary to use in the contract terms.",
   })
+  .maybeOption("purseExpiration", ["--purse-expiration"], clk.STRING)
   .action(async (args) => {
+    let purseExpiration: AbsoluteTime;
+
+    if (args.payPush.purseExpiration) {
+      purseExpiration = AbsoluteTime.addDuration(
+        AbsoluteTime.now(),
+        Duration.fromPrettyString(args.payPush.purseExpiration),
+      );
+    } else {
+      purseExpiration = AbsoluteTime.addDuration(
+        AbsoluteTime.now(),
+        Duration.fromSpec({ hours: 1 }),
+      );
+    }
+
     await withWallet(args, async (wallet) => {
       const resp = await wallet.client.call(
         WalletApiOperation.InitiatePeerPushDebit,
@@ -1100,13 +1124,7 @@ peerCli
           partialContractTerms: {
             amount: args.payPush.amount,
             summary: args.payPush.summary ?? "Payment",
-            // FIXME: Make the expiration configurable
-            purse_expiration: AbsoluteTime.toTimestamp(
-              AbsoluteTime.addDuration(
-                AbsoluteTime.now(),
-                Duration.fromSpec({ hours: 1 }),
-              ),
-            ),
+            purse_expiration: AbsoluteTime.toTimestamp(purseExpiration),
           },
         },
       );
diff --git a/packages/taler-wallet-core/src/operations/pay-peer.ts 
b/packages/taler-wallet-core/src/operations/pay-peer.ts
index 4dcc06076..5178839a4 100644
--- a/packages/taler-wallet-core/src/operations/pay-peer.ts
+++ b/packages/taler-wallet-core/src/operations/pay-peer.ts
@@ -97,7 +97,6 @@ import {
   runLongpollAsync,
   runOperationWithErrorReporting,
   spendCoins,
-  storeOperationPending,
 } from "../operations/common.js";
 import {
   readSuccessResponseJsonOrErrorCode,
@@ -220,6 +219,11 @@ export async function selectPeerCoins(
   ws: InternalWalletState,
   instructedAmount: AmountJson,
 ): Promise<SelectPeerCoinsResult> {
+  if (Amounts.isZero(instructedAmount)) {
+    // Other parts of the code assume that we have at least
+    // one coin to spend.
+    throw new Error("amount of zero not allowed");
+  }
   return await ws.db
     .mktx((x) => [
       x.exchanges,
diff --git a/packages/taler-wallet-core/src/operations/withdraw.ts 
b/packages/taler-wallet-core/src/operations/withdraw.ts
index aba2948cd..3c3878792 100644
--- a/packages/taler-wallet-core/src/operations/withdraw.ts
+++ b/packages/taler-wallet-core/src/operations/withdraw.ts
@@ -118,8 +118,6 @@ import {
 } from "../versions.js";
 import {
   makeTransactionId,
-  storeOperationError,
-  storeOperationPending,
 } from "./common.js";
 import {
   getExchangeDetails,

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]