mirror of
https://github.com/aljazceru/ditto.git
synced 2026-01-24 07:44:28 +01:00
Start simplifying LNURL code with NLib
This commit is contained in:
@@ -1,14 +0,0 @@
|
||||
import { assertEquals } from '@/deps-test.ts';
|
||||
|
||||
import { lnurlDecode, lnurlEncode } from './lnurl.ts';
|
||||
|
||||
const lnurl = 'lnurl1dp68gurn8ghj7um5v93kketj9ehx2amn9uh8wetvdskkkmn0wahz7mrww4excup0dajx2mrv92x9xp';
|
||||
const url = 'https://stacker.news/.well-known/lnurlp/odell';
|
||||
|
||||
Deno.test('lnurlEncode', () => {
|
||||
assertEquals(lnurlEncode(url), lnurl);
|
||||
});
|
||||
|
||||
Deno.test('lnurlDecode', () => {
|
||||
assertEquals(lnurlDecode(lnurl), url);
|
||||
});
|
||||
@@ -1,19 +1,4 @@
|
||||
import { bech32 } from '@/deps.ts';
|
||||
|
||||
/** Encode a URL to LNURL format. */
|
||||
function lnurlEncode(url: string, limit = 2000): `lnurl1${string}` {
|
||||
const data = new TextEncoder().encode(url);
|
||||
const words = bech32.toWords(data);
|
||||
return bech32.encode('lnurl', words, limit);
|
||||
}
|
||||
|
||||
/** Decode a LNURL into a URL. */
|
||||
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);
|
||||
}
|
||||
import { LNURL } from '@/deps.ts';
|
||||
|
||||
/** Get an LNURL from a lud06 or lud16. */
|
||||
function getLnurl({ lud06, lud16 }: { lud06?: string; lud16?: string }, limit?: number): string | undefined {
|
||||
@@ -21,10 +6,10 @@ function getLnurl({ lud06, lud16 }: { lud06?: string; lud16?: string }, limit?:
|
||||
if (lud16) {
|
||||
const [name, host] = lud16.split('@');
|
||||
if (name && host) {
|
||||
const url = new URL(`/.well-known/lnurlp/${name}`, `https://${host}`).toString();
|
||||
return lnurlEncode(url, limit);
|
||||
const url = new URL(`/.well-known/lnurlp/${name}`, `https://${host}`);
|
||||
return LNURL.encode(url, limit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export { getLnurl, lnurlDecode, lnurlEncode };
|
||||
export { getLnurl };
|
||||
|
||||
Reference in New Issue
Block a user