add payto stringify

This commit is contained in:
Sebastian 2022-04-11 11:31:33 -03:00
parent fae6c420a3
commit 56c2a9c6a6
No known key found for this signature in database
GPG Key ID: BE4FF68352439FC1

View File

@ -51,6 +51,19 @@ interface PaytoUriBitcoin extends PaytoUriGeneric {
const paytoPfx = "payto://"; const paytoPfx = "payto://";
function buildSegwitGenerator(result: PaytoUriBitcoin, targetPath: string) {
//generate segwit address just once, save addr in payto object
//and use it as cache
return function generateSegwitAddress(reserve: string): { addr1: string, addr2: string } {
if (result.addr1 && result.addr2) return { addr1: result.addr1, addr2: result.addr2 };
const { addr1, addr2 } = generateFakeSegwitAddress(reserve, targetPath)
result.addr1 = addr1
result.addr2 = addr2
return { addr1, addr2 }
}
}
/** /**
* Add query parameters to a payto URI * Add query parameters to a payto URI
*/ */
@ -66,6 +79,30 @@ export function addPaytoQueryParams(
return paytoPfx + acct + "?" + searchParams.toString(); return paytoPfx + acct + "?" + searchParams.toString();
} }
/**
* Serialize a PaytoURI into a valid payto:// string
*
* @param p
* @returns
*/
export function stringifyPaytoUri(p: PaytoUri): string {
const url = `${paytoPfx}${p.targetType}//${p.targetPath}`
if (p.params) {
const search = Object.entries(p.params)
.map(([key, value]) => `${key}=${value}`)
.join("&");
return `${url}?${search}`
}
return url
}
/**
* Parse a valid payto:// uri into a PaytoUri object
* RFC 8905
*
* @param s
* @returns
*/
export function parsePaytoUri(s: string): PaytoUri | undefined { export function parsePaytoUri(s: string): PaytoUri | undefined {
if (!s.startsWith(paytoPfx)) { if (!s.startsWith(paytoPfx)) {
return undefined; return undefined;
@ -123,16 +160,7 @@ export function parsePaytoUri(s: string): PaytoUri | undefined {
generateSegwitAddress: (): any => null generateSegwitAddress: (): any => null
} }
//generate segwit address just once, save addr in payto object result.generateSegwitAddress = buildSegwitGenerator(result, targetPath)
//and use it as cache
function generateSegwitAddress(reserve: string) {
if (result.addr1 && result.addr2) return { addr1: result.addr1, addr2: result.addr2 };
const { addr1, addr2 } = generateFakeSegwitAddress(reserve, targetPath)
result.addr1 = addr1
result.addr2 = addr2
return { addr1, addr2 }
}
result.generateSegwitAddress = generateSegwitAddress
return result; return result;
} }