From 0b633b56bcf9d0314dd196aa4878bffe016b7251 Mon Sep 17 00:00:00 2001 From: Philipp Walter Date: Mon, 7 Oct 2024 11:33:21 +0200 Subject: [PATCH 1/2] fix(settings): fix slow electrum server RegExp --- src/screens/Settings/ElectrumConfig/index.tsx | 43 +++++++++++++------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/screens/Settings/ElectrumConfig/index.tsx b/src/screens/Settings/ElectrumConfig/index.tsx index b2522939a..5c9af70f5 100644 --- a/src/screens/Settings/ElectrumConfig/index.tsx +++ b/src/screens/Settings/ElectrumConfig/index.tsx @@ -1,7 +1,7 @@ import React, { memo, ReactElement, useEffect, useState } from 'react'; import { StyleSheet } from 'react-native'; import { err, ok, Result } from '@synonymdev/result'; -import Url from 'url-parse'; +import parseUrl from 'url-parse'; import { useTranslation } from 'react-i18next'; import isEqual from 'lodash/isEqual'; import { EProtocol } from 'beignet'; @@ -42,20 +42,35 @@ const radioButtons: RadioButtonItem[] = [ ]; const isValidURL = (data: string): boolean => { - const pattern = new RegExp( - '^(https?:\\/\\/)?' + // protocol - '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name - '((\\d{1,3}\\.){3}\\d{1,3}))' + // IP (v4) address - '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*', // port and path - 'i', - ); - - // wave through everything 'localhost' for development - if (__DEV__ && data.includes('localhost')) { - return true; + // Add 'http://' if the protocol is missing to enable URL parsing + let normalizedData = data; + if (!/^https?:\/\//i.test(data)) { + normalizedData = 'http://' + data; } - return !!pattern.test(data); + try { + const url = parseUrl(normalizedData); + + // Allow standard domains, custom TLDs like .local, and IPv4 addresses + const isValidDomainOrIP = !!url.hostname.match( + /^([a-z\d]([a-z\d-]*[a-z\d])*\.[a-z\d-]+|(\d{1,3}\.){3}\d{1,3})$/i, + ); + + // Always allow .local domains + if (url.hostname.endsWith('.local')) { + return true; + } + + // Allow localhost in development mode + if (__DEV__ && data.includes('localhost')) { + return true; + } + + return isValidDomainOrIP; + } catch (e) { + // If URL constructor fails, it's not a valid URL + return false; + } }; const validateInput = ( @@ -218,7 +233,7 @@ const ElectrumConfig = ({ protocol: _protocol, }; } else { - const url = new Url(data); + const url = parseUrl(data); connectData = { host: url.hostname, From d5427d7f2691255e6b96eedefe7256cef61d539d Mon Sep 17 00:00:00 2001 From: Philipp Walter Date: Mon, 7 Oct 2024 11:33:45 +0200 Subject: [PATCH 2/2] fix(transfer): fix error message --- src/utils/lightning/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/lightning/index.ts b/src/utils/lightning/index.ts index 3e8c084f2..2ed33b6b0 100644 --- a/src/utils/lightning/index.ts +++ b/src/utils/lightning/index.ts @@ -1075,7 +1075,7 @@ export const addPeer = async ({ } if (!isValidLightningNodePublicKey(parsedUri.value.publicKey)) { - return err(i18n.t('lightning:error_add_msg')); + return err(i18n.t('lightning:error_add')); } const res = await lm.addPeer({ @@ -1086,7 +1086,7 @@ export const addPeer = async ({ }); if (res.isErr()) { - res.error.message = i18n.t('lightning:error_add_msg', { + res.error.message = i18n.t('lightning:error_add', { raw: res.error.message, }); }