diff --git a/src/app/lightning.ts b/src/app/lightning.ts index fd7f4c86..ed8d40f8 100644 --- a/src/app/lightning.ts +++ b/src/app/lightning.ts @@ -1,10 +1,29 @@ import {writable} from "svelte/store" import {nwc} from "@getalby/sdk" +import {bech32ToHex, displayUrl, tryCatch} from "@welshman/lib" import type {Maybe} from "@welshman/lib" import {isNWCWallet, isWebLNWallet} from "@welshman/util" import type {Wallet} from "@welshman/util" import {withGetter} from "@welshman/store" +/** + * A profile's lnurl is the bech32 form zapping needs — both lud06 and lud16 normalize + * into it — so decode it before showing it to anyone. The well-known endpoint a + * lightning address encodes to becomes the address again; anything else is a plain url. + */ +export const displayLnurl = (lnurl: string) => { + const url = tryCatch(() => bech32ToHex(lnurl)) + const address = url?.match(/^https?:\/\/([^/]+)\/\.well-known\/lnurlp\/(.+)$/) + + if (address) { + const [, domain, name] = address + + return `${name}@${domain}` + } + + return url ? displayUrl(url) : lnurl +} + export const wallet = withGetter(writable>(undefined)) export const getWebLn = () => (window as any).webln diff --git a/src/routes/settings/wallet/+page.svelte b/src/routes/settings/wallet/+page.svelte index 9de11664..53b3d7f8 100644 --- a/src/routes/settings/wallet/+page.svelte +++ b/src/routes/settings/wallet/+page.svelte @@ -1,7 +1,7 @@