taler-typescript-core

Wallet core logic and WebUIs for various components
Log | Files | Refs | Submodules | README | LICENSE

commit c767e62e28d16cbde275f450d442741309294f21
parent 12656a8d864e5f97b51153b83dfe748279c05077
Author: Florian Dold <florian@dold.me>
Date:   Wed, 10 Jul 2024 12:54:10 +0200

util: fix payto URI construction

Diffstat:
Mpackages/taler-util/src/payto.test.ts | 14+++++++++++++-
Mpackages/taler-util/src/payto.ts | 10++++++++--
Mpackages/taler-util/src/whatwg-url.ts | 4++++
Mpackages/taler-wallet-core/src/withdraw.ts | 2+-
4 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/packages/taler-util/src/payto.test.ts b/packages/taler-util/src/payto.test.ts @@ -16,7 +16,7 @@ import test from "ava"; -import { PaytoString, parsePaytoUri, stringifyPaytoUri } from "./payto.js"; +import { PaytoString, addPaytoQueryParams, parsePaytoUri, stringifyPaytoUri } from "./payto.js"; test("basic payto parsing", (t) => { const r1 = parsePaytoUri("https://example.com/"); @@ -42,3 +42,15 @@ test("parsing payto with % carh", (t) => { t.is(stringifyPaytoUri(parsePaytoUri(payto1)!), payto1); }); + +test("adding payto query params", (t) => { + const payto1 = + "payto://iban/DE1231231231?receiver-name=John%20Doe" as PaytoString; + const out1 = addPaytoQueryParams(payto1, {}); + t.deepEqual(payto1, out1); + + const out2 = addPaytoQueryParams(payto1, { + foo: "42", + }); + t.deepEqual(out2, "payto://iban/DE1231231231?receiver-name=John%20Doe&foo=42"); +}); diff --git a/packages/taler-util/src/payto.ts b/packages/taler-util/src/payto.ts @@ -160,14 +160,20 @@ export function buildPayto( } /** - * Add query parameters to a payto URI + * Add query parameters to a payto URI. + * + * Existing parameters are preserved. */ export function addPaytoQueryParams( s: string, params: { [name: string]: string }, ): string { const [acct, search] = s.slice(paytoPfx.length).split("?"); - const paramList = !params ? [] : Object.entries(params); + const searchParams = new URLSearchParams(search || ""); + for (const [paramKey, paramValue] of Object.entries(params)) { + searchParams.set(paramKey, paramValue); + } + const paramList = [...searchParams.entries()]; if (paramList.length === 0) { return paytoPfx + acct; } diff --git a/packages/taler-util/src/whatwg-url.ts b/packages/taler-util/src/whatwg-url.ts @@ -424,6 +424,10 @@ export class URLSearchParamsImpl { return output; } + entries() { + return [...this._list.map(x => [x[0], x[1]])]; + } + forEach( callbackfn: ( value: string, diff --git a/packages/taler-wallet-core/src/withdraw.ts b/packages/taler-wallet-core/src/withdraw.ts @@ -3585,7 +3585,7 @@ async function fetchAccount( }); if (reservePub != null) { paytoUri = addPaytoQueryParams(paytoUri, { - message: `Taler ${reservePub}`, + message: `Taler<- ${reservePub}`, }); } const acctInfo: WithdrawalExchangeAccountDetails = {