aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-util
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2024-04-09 19:55:45 -0300
committerSebastian <sebasjm@gmail.com>2024-04-09 19:55:45 -0300
commit86e02c6ecdde78ed741d89c5a64f6bfb79a2426e (patch)
tree27d30041774d6043b079950b23917def1f335e7e /packages/taler-util
parent9dec13056fa7af00e6ba16e6e57d527e3aa25185 (diff)
downloadwallet-core-86e02c6ecdde78ed741d89c5a64f6bfb79a2426e.tar.xz
fix #8494
Diffstat (limited to 'packages/taler-util')
-rw-r--r--packages/taler-util/src/taleruri.test.ts47
-rw-r--r--packages/taler-util/src/taleruri.ts67
2 files changed, 113 insertions, 1 deletions
diff --git a/packages/taler-util/src/taleruri.test.ts b/packages/taler-util/src/taleruri.test.ts
index dbd175fe5..7f10d21fd 100644
--- a/packages/taler-util/src/taleruri.test.ts
+++ b/packages/taler-util/src/taleruri.test.ts
@@ -17,6 +17,7 @@
import test from "ava";
import { AmountString } from "./taler-types.js";
import {
+ parseAddExchangeUri,
parseDevExperimentUri,
parsePayPullUri,
parsePayPushUri,
@@ -26,6 +27,7 @@ import {
parseRestoreUri,
parseWithdrawExchangeUri,
parseWithdrawUri,
+ stringifyAddExchange,
stringifyDevExperimentUri,
stringifyPayPullUri,
stringifyPayPushUri,
@@ -506,6 +508,51 @@ test("taler withdraw exchange URI with amount (stringify)", (t) => {
);
});
+
+/**
+ * 5.13 action: add-exchange https://lsd.gnunet.org/lsd0006/#name-action-add-exchange
+ */
+
+test("taler add exchange URI (parse)", (t) => {
+ {
+ const r1 = parseAddExchangeUri(
+ "taler://add-exchange/exchange.example.com/",
+ );
+ if (!r1) {
+ t.fail();
+ return;
+ }
+ t.deepEqual(
+ r1.exchangeBaseUrl,
+ "https://exchange.example.com/",
+ );
+ }
+ {
+ const r2 = parseAddExchangeUri(
+ "taler://add-exchange/exchanges.example.com/api/",
+ );
+ if (!r2) {
+ t.fail();
+ return;
+ }
+ t.deepEqual(
+ r2.exchangeBaseUrl,
+ "https://exchanges.example.com/api/",
+ );
+ }
+
+});
+
+test("taler add exchange URI (stringify)", (t) => {
+ const url = stringifyAddExchange({
+ exchangeBaseUrl: "https://exchange.demo.taler.net",
+ });
+ t.deepEqual(
+ url,
+ "taler://add-exchange/exchange.demo.taler.net/",
+ );
+});
+
/**
* wrong uris
*/
diff --git a/packages/taler-util/src/taleruri.ts b/packages/taler-util/src/taleruri.ts
index db8a58185..b4f9db6ef 100644
--- a/packages/taler-util/src/taleruri.ts
+++ b/packages/taler-util/src/taleruri.ts
@@ -41,7 +41,8 @@ export type TalerUri =
| BackupRestoreUri
| RefundUriResult
| WithdrawUriResult
- | WithdrawExchangeUri;
+ | WithdrawExchangeUri
+ | AddExchangeUri;
declare const __action_str: unique symbol;
export type TalerUriString = string & { [__action_str]: true };
@@ -127,6 +128,11 @@ export interface WithdrawExchangeUri {
amount?: AmountString;
}
+export interface AddExchangeUri {
+ type: TalerUriAction.AddExchange;
+ exchangeBaseUrl: string;
+}
+
/**
* Parse a taler[+http]://withdraw URI.
* Return undefined if not passed a valid URI.
@@ -177,6 +183,53 @@ export function parseWithdrawUri(s: string): WithdrawUriResult | undefined {
}
/**
+ * Parse a taler[+http]://withdraw URI.
+ * Return undefined if not passed a valid URI.
+ */
+export function parseAddExchangeUriWithError(s: string) {
+ const pi = parseProtoInfoWithError(s, "add-exchange");
+ if (pi.type === "fail") {
+ return pi;
+ }
+ const parts = pi.body.rest.split("/");
+
+ if (parts.length < 2) {
+ return opKnownTalerFailure(TalerErrorCode.WALLET_TALER_URI_MALFORMED, {
+ code: TalerErrorCode.WALLET_TALER_URI_MALFORMED,
+ });
+ }
+
+ const host = parts[0].toLowerCase();
+ const pathSegments = parts.slice(1, parts.length - 1);
+ /**
+ * The statement below does not tolerate a slash-ended URI.
+ * This results in (1) the withdrawalId being passed as the
+ * empty string, and (2) the bankIntegrationApi ending with the
+ * actual withdrawal operation ID. That can be fixed by
+ * trimming the parts-list. FIXME
+ */
+ const p = [host, ...pathSegments].join("/");
+
+ const result: AddExchangeUri = {
+ type: TalerUriAction.AddExchange,
+ exchangeBaseUrl: canonicalizeBaseUrl(
+ `${pi.body.innerProto}://${p}/`,
+ ),
+ };
+ return opFixedSuccess(result);
+}
+
+/**
+ *
+ * @deprecated use parseWithdrawUriWithError
+ */
+export function parseAddExchangeUri(s: string): AddExchangeUri | undefined {
+ const r = parseAddExchangeUriWithError(s);
+ if (r.type === "fail") return undefined;
+ return r.body;
+}
+
+/**
* @deprecated use TalerUriAction
*/
export enum TalerUriType {
@@ -203,6 +256,7 @@ export enum TalerUriAction {
Restore = "restore",
DevExperiment = "dev-experiment",
WithdrawExchange = "withdraw-exchange",
+ AddExchange = "add-exchange",
}
interface TalerUriProtoInfo {
@@ -270,6 +324,7 @@ const parsers: { [A in TalerUriAction]: Parser } = {
[TalerUriAction.Withdraw]: parseWithdrawUri,
[TalerUriAction.DevExperiment]: parseDevExperimentUri,
[TalerUriAction.WithdrawExchange]: parseWithdrawExchangeUri,
+ [TalerUriAction.AddExchange]: parseAddExchangeUri,
};
export function parseTalerUri(string: string): TalerUri | undefined {
@@ -313,6 +368,9 @@ export function stringifyTalerUri(uri: TalerUri): string {
case TalerUriAction.WithdrawExchange: {
return stringifyWithdrawExchange(uri);
}
+ case TalerUriAction.AddExchange: {
+ return stringifyAddExchange(uri);
+ }
}
}
@@ -592,6 +650,13 @@ export function stringifyWithdrawExchange({
return `${proto}://withdraw-exchange/${path}${exchangePub ?? ""}${query}`;
}
+export function stringifyAddExchange({
+ exchangeBaseUrl,
+}: Omit<AddExchangeUri, "type">): string {
+ const { proto, path } = getUrlInfo(exchangeBaseUrl);
+ return `${proto}://add-exchange/${path}`;
+}
+
export function stringifyDevExperimentUri({
devExperimentId,
}: Omit<DevExperimentUri, "type">): string {