diff options
author | Sebastian <sebasjm@gmail.com> | 2023-06-26 12:54:54 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-06-26 12:54:54 -0300 |
commit | 74579ac2f2a4dd46487fb4d2bb2f2b26961a54f1 (patch) | |
tree | 95317d59ece7d61fafd29f00dda148d545c72849 /packages/taler-util/src/taleruri.ts | |
parent | 4b61945f6b7b073acf5de825b7678bddc56ad5d0 (diff) | |
download | wallet-core-74579ac2f2a4dd46487fb4d2bb2f2b26961a54f1.tar.xz |
withdraw exchange URI
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 { |