From 8390c60031b87e034a1bb6b830910faa6c2cc4e6 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 20 Oct 2022 14:53:29 -0300 Subject: parse and construct taler recovery URI --- packages/taler-util/src/taleruri.ts | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'packages/taler-util') diff --git a/packages/taler-util/src/taleruri.ts b/packages/taler-util/src/taleruri.ts index baca3efac..45f9a90f2 100644 --- a/packages/taler-util/src/taleruri.ts +++ b/packages/taler-util/src/taleruri.ts @@ -14,7 +14,9 @@ GNU Taler; see the file COPYING. If not, see */ +import { BackupRecovery } from "./backup-types.js"; import { canonicalizeBaseUrl } from "./helpers.js"; +import { initNodePrng } from "./prng-node.js"; import { URLSearchParams, URL } from "./url.js"; export interface PayUriResult { @@ -95,6 +97,7 @@ export enum TalerUriType { TalerNotifyReserve = "taler-notify-reserve", TalerPayPush = "taler-pay-push", TalerPayPull = "taler-pay-pull", + TalerRecovery = "taler-recovery", TalerDevExperiment = "taler-dev-experiment", Unknown = "unknown", } @@ -107,6 +110,12 @@ const talerActionPayPush = "pay-push"; */ export function classifyTalerUri(s: string): TalerUriType { const sl = s.toLowerCase(); + if (sl.startsWith("taler://recovery/")) { + return TalerUriType.TalerRecovery; + } + if (sl.startsWith("taler+http://recovery/")) { + return TalerUriType.TalerRecovery; + } if (sl.startsWith("taler://pay/")) { return TalerUriType.TalerPay; } @@ -362,3 +371,40 @@ export function constructPayPullUri(args: { } return `${proto}://pay-pull/${url.host}${url.pathname}${args.contractPriv}`; } + +export function constructRecoveryUri(args: BackupRecovery): string { + const key = args.walletRootPriv + const urls = args.providers.map(p => `p=${canonicalizeBaseUrl(p.url)}`).join("&") + + return `taler://recovery/${key}?${urls}` +} +export function parseRecoveryUri(uri: string): BackupRecovery | undefined { + const pi = parseProtoInfo(uri, "recovery"); + if (!pi) { + return undefined; + } + const idx = pi.rest.indexOf("?"); + if (idx === -1) { + return undefined + } + const path = pi.rest.slice(0, idx) + const params = pi.rest.slice(idx + 1) + if (!path || !params) { + return undefined; + } + const parts = path.split("/"); + const walletRootPriv = parts[0]; + if (!walletRootPriv) return undefined + const providers = new Array<{ url: string }>(); + const args = params.split("&") + for (const param in args) { + const eq = args[param].indexOf("=") + if (eq === -1) return undefined; + const name = args[param].slice(0, eq) + const value = args[param].slice(eq + 1) + if (name !== "p" || !value) return undefined; + providers.push({ url: value }) + } + return { walletRootPriv, providers } +} + -- cgit v1.2.3