diff options
Diffstat (limited to 'packages/taler-util/src/taleruri.ts')
-rw-r--r-- | packages/taler-util/src/taleruri.ts | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/packages/taler-util/src/taleruri.ts b/packages/taler-util/src/taleruri.ts index cb0d74a12..fc140811b 100644 --- a/packages/taler-util/src/taleruri.ts +++ b/packages/taler-util/src/taleruri.ts @@ -15,6 +15,7 @@ */ import { canonicalizeBaseUrl } from "./helpers.js"; +import { AmountString } from "./taler-types.js"; import { URLSearchParams, URL } from "./url.js"; export type TalerUri = @@ -28,6 +29,7 @@ export type TalerUri = | TipUriResult | WithdrawUriResult | ExchangeUri + | WithdrawExchangeUri | AuditorUri; export interface PayUriResult { @@ -99,6 +101,13 @@ export interface BackupRestoreUri { providers: Array<string>; } +export interface WithdrawExchangeUri { + type: TalerUriAction.WithdrawExchange; + exchangeBaseUrl: string; + exchangePub: string; + amount?: AmountString; +} + /** * Parse a taler[+http]://withdraw URI. * Return undefined if not passed a valid URI. @@ -166,6 +175,7 @@ export enum TalerUriAction { Auditor = "auditor", Restore = "restore", DevExperiment = "dev-experiment", + WithdrawExchange = "withdraw-exchange", } interface TalerUriProtoInfo { @@ -207,6 +217,7 @@ const parsers: { [A in TalerUriAction]: Parser } = { [TalerUriAction.DevExperiment]: parseDevExperimentUri, [TalerUriAction.Exchange]: parseExchangeUri, [TalerUriAction.Auditor]: parseAuditorUri, + [TalerUriAction.WithdrawExchange]: parseWithdrawExchangeUri, }; export function parseTalerUri(string: string): TalerUri | undefined { @@ -253,6 +264,9 @@ export function stringifyTalerUri(uri: TalerUri): string { case TalerUriAction.Exchange: { return stringifyExchangeUri(uri); } + case TalerUriAction.WithdrawExchange: { + return stringifyWithdrawExchange(uri); + } case TalerUriAction.Auditor: { return stringifyAuditorUri(uri); } @@ -432,6 +446,37 @@ export function parseExchangeUri(s: string): ExchangeUri | undefined { exchangePub, }; } + +export function parseWithdrawExchangeUri( + s: string, +): WithdrawExchangeUri | undefined { + const pi = parseProtoInfo(s, "withdraw-exchange"); + if (!pi) { + return undefined; + } + const c = pi?.rest.split("?"); + const parts = c[0].split("/"); + if (parts.length < 2) { + return undefined; + } + const host = parts[0].toLowerCase(); + const exchangePub = parts[parts.length - 1]; + const pathSegments = parts.slice(1, parts.length - 1); + const hostAndSegments = [host, ...pathSegments].join("/"); + const exchangeBaseUrl = canonicalizeBaseUrl( + `${pi.innerProto}://${hostAndSegments}/`, + ); + const q = new URLSearchParams(c[1] ?? ""); + const amount = q.get("a") ?? undefined; + + return { + type: TalerUriAction.WithdrawExchange, + exchangeBaseUrl, + exchangePub, + amount, + }; +} + export function parseAuditorUri(s: string): AuditorUri | undefined { const pi = parseProtoInfo(s, "auditor"); if (!pi) { @@ -622,6 +667,17 @@ export function stringifyRestoreUri({ return `taler://restore/${walletRootPriv}/${list}`; } +export function stringifyWithdrawExchange({ + exchangeBaseUrl, + exchangePub, + amount, +}: Omit<WithdrawExchangeUri, "type">): string { + const { proto, path, query } = getUrlInfo(exchangeBaseUrl, { + a: amount, + }); + return `${proto}://withdraw-exchange/${path}${exchangePub}${query}`; +} + export function stringifyDevExperimentUri({ devExperimentId, }: Omit<DevExperimentUri, "type">): string { |