diff --git a/src/nostr/consts.ts b/src/nostr/consts.ts index 52498b47..aa478034 100644 --- a/src/nostr/consts.ts +++ b/src/nostr/consts.ts @@ -1,5 +1,5 @@ -import { Npub } from '@src/model/nostr' -import { isUrl } from '@src/util' +import type { Npub } from '@model/nostr' +import { isUrl } from '@util/lnurl' import { normalizeURL } from './util' diff --git a/src/util/index.ts b/src/util/index.ts index 4ca780dc..8996d4b5 100644 --- a/src/util/index.ts +++ b/src/util/index.ts @@ -7,7 +7,7 @@ import { Buffer } from 'buffer/' import * as Clipboard from 'expo-clipboard' import { Linking, Share, Vibration } from 'react-native' -import { decodeUrlOrAddress, isLnurlOrAddress } from './lnurl' +import { decodeUrlOrAddress, isLnurlOrAddress, isUrl } from './lnurl' import { getLanguageCode } from './localization' import { isArr, isBuf, isNum, isStr } from './typeguards' @@ -135,11 +135,6 @@ export function getHistoryGroupDate(date: Date) { return isToday(date) ? 'Today' : getShortDateStr(date) } -export function isUrl(url: string) { - try { return !!new URL(url) } catch { /* ignore*/ } - return false -} - export function formatMintUrl(url: string) { const clean = url.startsWith('http') ? url.split('://')[1] : url if (clean.length < 30) { return clean } diff --git a/src/util/lnurl.ts b/src/util/lnurl.ts index d0cf80f7..d22cfe55 100644 --- a/src/util/lnurl.ts +++ b/src/util/lnurl.ts @@ -1,10 +1,7 @@ import type { ILnUrlPayRequest } from '@model' -import { cTo } from '@store/utils' import { bech32 } from 'bech32' import { Buffer } from 'buffer/' -import { isUrl } from '.' - const LNURL_REGEX = /^(?:http.*[&?]lightning=|lightning:)?(lnurl[0-9]{1,}[02-9ac-hj-np-z]+)/ @@ -19,6 +16,11 @@ export interface LightningAddress { domain: string } +export function isUrl(url: string) { + try { return !!new URL(url) } catch { /* ignore*/ } + return false +} + export function isLnurlOrAddress(lnUrlOrAddress: string) { return (isLnurl(lnUrlOrAddress) || isLnurlAddress(lnUrlOrAddress)) } @@ -27,7 +29,7 @@ export function isLnurlAddress(str: string) { const address = parseLightningAddress(str) if (address) { const { username, domain } = address - const protocol = domain.match(/\.onion$/) ? 'http' : 'https' + const protocol = domain.endsWith('.onion') ? 'http' : 'https' return isUrl(`${protocol}://${domain}/.well-known/lnurlp/${username}`) } return false @@ -125,7 +127,7 @@ export function getLnurlData(url?: string): Promise | null { export function getLnurlIdentifierFromMetadata(metadata: string) { try { - const parsed = cTo(metadata) + const parsed = JSON.parse(metadata) as string[][] const identidier = parsed.find(([key]) => key === 'text/identifier')?.[1] return identidier ?? 'Identifier not found' } catch (e) {