From 7f7c7d0af5bd2603f006ce0af9d67cd0fdde14ff Mon Sep 17 00:00:00 2001 From: KKA11010 Date: Tue, 26 Mar 2024 19:27:54 +0100 Subject: [PATCH] fix require cycle. replace onion regex with string.endsWith() --- src/nostr/consts.ts | 4 ++-- src/util/index.ts | 7 +------ src/util/lnurl.ts | 12 +++++++----- 3 files changed, 10 insertions(+), 13 deletions(-) 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 083e01c3..56fe14cd 100644 --- a/src/util/index.ts +++ b/src/util/index.ts @@ -6,7 +6,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, isStr } from './typeguards' @@ -95,11 +95,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) {