diff options
author | Sebastian <sebasjm@gmail.com> | 2023-01-17 15:55:01 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-01-17 16:01:26 -0300 |
commit | 5be2d128ed088a77d6448ac1ebf25aba3716bd81 (patch) | |
tree | 9296b79e2cd273eeac5081c0c2d5a881e8181b81 | |
parent | 3cde52effc5bc1ec82285b52938bc024083c3278 (diff) |
payto URI builder
-rw-r--r-- | packages/taler-util/src/payto.ts | 64 |
1 files changed, 62 insertions, 2 deletions
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; } |