From 165486a11268ab3d8009506916cd22d233cd248b Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Sat, 7 Dec 2019 18:42:18 +0100 Subject: auto-refund --- src/util/helpers.ts | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'src/util/helpers.ts') diff --git a/src/util/helpers.ts b/src/util/helpers.ts index eb8a1c7b2..3831e84af 100644 --- a/src/util/helpers.ts +++ b/src/util/helpers.ts @@ -24,7 +24,7 @@ import { AmountJson } from "./amounts"; import * as Amounts from "./amounts"; -import { Timestamp } from "../walletTypes"; +import { Timestamp, Duration } from "../walletTypes"; /** * Show an amount in a form suitable for the user. @@ -152,25 +152,37 @@ export function extractTalerStampOrThrow(stamp: string): Timestamp { } /** - * Check if a timestamp is in the right format. + * Extract a duration from a Taler duration string. */ -export function timestampCheck(stamp: string): boolean { - return getTalerStampSec(stamp) !== null; +export function extractTalerDuration(duration: string): Duration | undefined { + const m = duration.match(/\/?Delay\(([0-9]*)\)\/?/); + if (!m || !m[1]) { + return undefined; + } + return { + d_ms: parseInt(m[1], 10) * 1000, + }; } - /** - * Get a JavaScript Date object from a Taler date string. - * Returns null if input is not in the right format. + * Extract a duration from a Taler duration string. */ -export function getTalerStampDate(stamp: string): Date | null { - const sec = getTalerStampSec(stamp); - if (sec == null) { - return null; +export function extractTalerDurationOrThrow(duration: string): Duration { + const r = extractTalerDuration(duration); + if (!r) { + throw Error("invalid duration"); } - return new Date(sec * 1000); + return r; +} + +/** + * Check if a timestamp is in the right format. + */ +export function timestampCheck(stamp: string): boolean { + return getTalerStampSec(stamp) !== null; } + /** * Compute the hash function of a JSON object. */ -- cgit v1.2.3