From 12f9c4761de47fabb234c92abea4ba0c32263d91 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Sat, 13 Jan 2024 01:09:50 +0100 Subject: [PATCH] Fix isNaN checks --- lib/validate.js | 4 ++++ pages/settings/wallets/lightning-address.js | 12 ++++++------ worker/wallet.js | 5 +++-- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/validate.js b/lib/validate.js index dc840298..23768856 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -444,3 +444,7 @@ export const lud18PayerDataSchema = (k1) => object({ email: string().email('bad email address'), identifier: string() }) + +// check if something is _really_ a number. +// returns true for every number in this range: [-Infinity, ..., 0, ..., Infinity] +export const isNumber = x => typeof x === 'number' && !Number.isNaN(x) diff --git a/pages/settings/wallets/lightning-address.js b/pages/settings/wallets/lightning-address.js index 8fd04f50..bec0f74a 100644 --- a/pages/settings/wallets/lightning-address.js +++ b/pages/settings/wallets/lightning-address.js @@ -7,7 +7,7 @@ import { WalletButtonBar, WalletCard } from '../../../components/wallet-card' import { useMutation } from '@apollo/client' import { REMOVE_AUTOWITHDRAW, SET_AUTOWITHDRAW } from '../../../fragments/users' import { useToast } from '../../../components/toast' -import { lnAddrAutowithdrawSchema } from '../../../lib/validate' +import { lnAddrAutowithdrawSchema, isNumber } from '../../../lib/validate' import { useRouter } from 'next/router' import { useEffect, useState } from 'react' @@ -15,7 +15,7 @@ export const getServerSideProps = getGetServerSideProps({ authRequired: true }) function useAutoWithdrawEnabled () { const me = useMe() - return me?.privates?.lnAddr && !isNaN(me?.privates?.autoWithdrawThreshold) && !isNaN(me?.privates?.autoWithdrawMaxFeePercent) + return me?.privates?.lnAddr && isNumber(me?.privates?.autoWithdrawThreshold) && isNumber(me?.privates?.autoWithdrawMaxFeePercent) } export default function LightningAddress () { @@ -25,7 +25,7 @@ export default function LightningAddress () { const [setAutoWithdraw] = useMutation(SET_AUTOWITHDRAW) const enabled = useAutoWithdrawEnabled() const [removeAutoWithdraw] = useMutation(REMOVE_AUTOWITHDRAW) - const autoWithdrawThreshold = isNaN(me?.privates?.autoWithdrawThreshold) ? 10000 : me?.privates?.autoWithdrawThreshold + const autoWithdrawThreshold = isNumber(me?.privates?.autoWithdrawThreshold) ? me?.privates?.autoWithdrawThreshold : 10000 const [sendThreshold, setSendThreshold] = useState(Math.max(Math.floor(autoWithdrawThreshold / 10), 1)) useEffect(() => { @@ -39,8 +39,8 @@ export default function LightningAddress () {
{ @@ -73,7 +73,7 @@ export default function LightningAddress () { const value = e.target.value setSendThreshold(Math.max(Math.floor(value / 10), 1)) }} - hint={isNaN(sendThreshold) ? undefined : `note: will attempt withdrawal when desired balance is exceeded by ${sendThreshold} sats`} + hint={isNumber(sendThreshold) ? `note: will attempt withdraw when threshold is exceeded by ${sendThreshold} sats` : undefined} append={sats} />