diff --git a/packages/taler-util/src/payto.ts b/packages/taler-util/src/payto.ts index 1ac081a76..dd35b44be 100644 --- a/packages/taler-util/src/payto.ts +++ b/packages/taler-util/src/payto.ts @@ -55,6 +55,66 @@ export interface PaytoUriBitcoin extends PaytoUriGeneric { const paytoPfx = "payto://"; +export function buildPayto( + type: "iban", + iban: string, + bic: string | undefined, +): PaytoUriIBAN; +export function buildPayto( + type: "bitcoin", + address: string, + reserve: string | undefined, +): PaytoUriBitcoin; +export function buildPayto( + type: "x-taler-bank", + host: string, + account: string, +): PaytoUriTalerBank; +export function buildPayto( + type: "iban" | "bitcoin" | "x-taler-bank", + first: string, + second?: string, +): PaytoUriGeneric { + switch (type) { + case "bitcoin": { + const result: PaytoUriBitcoin = { + isKnown: true, + targetType: "bitcoin", + targetPath: first, + params: {}, + segwitAddrs: !second ? [] : generateFakeSegwitAddress(second, first), + }; + return result; + } + case "iban": { + const result: PaytoUriIBAN = { + isKnown: true, + targetType: "iban", + iban: first, + params: {}, + targetPath: !second ? first : `${second}/${first}`, + }; + return result; + } + case "x-taler-bank": { + if (!second) throw Error("missing account for payto://x-taler-bank"); + const result: PaytoUriTalerBank = { + isKnown: true, + targetType: "x-taler-bank", + host: first, + account: second, + params: {}, + targetPath: `${first}/${second}`, + }; + return result; + } + default: { + const unknownType: never = type; + throw Error(`unknown payto:// type ${unknownType}`); + } + } +} + /** * Add query parameters to a payto URI */ @@ -84,8 +144,8 @@ export function stringifyPaytoUri(p: PaytoUri): string { const url = new URL(`${paytoPfx}${p.targetType}/${p.targetPath}`); const paramList = !p.params ? [] : Object.entries(p.params); paramList.forEach(([key, value]) => { - url.searchParams.set(key, value) - }) + url.searchParams.set(key, value); + }); return url.href; }