payto URI builder

This commit is contained in:
Sebastian 2023-01-17 15:55:01 -03:00
parent 3cde52effc
commit 5be2d128ed
No known key found for this signature in database
GPG Key ID: BE4FF68352439FC1

View File

@ -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;
}