lnurl: increase default limit to 2000

This commit is contained in:
Alex Gleason 2024-01-15 17:26:53 -06:00
parent 455459bea7
commit 24d1ff4aec
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 6 additions and 6 deletions

View File

@ -1,28 +1,28 @@
import { bech32 } from '@/deps.ts';
/** Encode a URL to LNURL format. */
function lnurlEncode(url: string): `lnurl1${string}` {
function lnurlEncode(url: string, limit = 2000): `lnurl1${string}` {
const data = new TextEncoder().encode(url);
const words = bech32.toWords(data);
return bech32.encode('lnurl', words);
return bech32.encode('lnurl', words, limit);
}
/** Decode a LNURL into a URL. */
function lnurlDecode(lnurl: string): string {
const { prefix, words } = bech32.decode(lnurl);
function lnurlDecode(lnurl: string, limit = 2000): string {
const { prefix, words } = bech32.decode(lnurl, limit);
if (prefix !== 'lnurl') throw new Error('Invalid LNURL');
const data = new Uint8Array(bech32.fromWords(words));
return new TextDecoder().decode(data);
}
/** Get an LNURL from a lud06 or lud16. */
function getLnurl({ lud06, lud16 }: { lud06?: string; lud16?: string }): string | undefined {
function getLnurl({ lud06, lud16 }: { lud06?: string; lud16?: string }, limit?: number): string | undefined {
if (lud06) return lud06;
if (lud16) {
const [name, host] = lud16.split('@');
if (name && host) {
const url = new URL(`/.well-known/lnurlp/${name}`, `https://${host}`).toString();
return lnurlEncode(url);
return lnurlEncode(url, limit);
}
}
}