Skip to content

Commit

Permalink
fix require cycle. replace onion regex with string.endsWith()
Browse files Browse the repository at this point in the history
  • Loading branch information
KKA11010 committed Mar 27, 2024
1 parent addb906 commit 7f7c7d0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/nostr/consts.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
7 changes: 1 addition & 6 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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 }
Expand Down
12 changes: 7 additions & 5 deletions src/util/lnurl.ts
Original file line number Diff line number Diff line change
@@ -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]+)/

Expand All @@ -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))
}
Expand All @@ -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
Expand Down Expand Up @@ -125,7 +127,7 @@ export function getLnurlData(url?: string): Promise<ILnUrlPayRequest> | null {

export function getLnurlIdentifierFromMetadata(metadata: string) {
try {
const parsed = cTo<string[][]>(metadata)
const parsed = JSON.parse(metadata) as string[][]
const identidier = parsed.find(([key]) => key === 'text/identifier')?.[1]
return identidier ?? 'Identifier not found'
} catch (e) {
Expand Down

0 comments on commit 7f7c7d0

Please sign in to comment.