Skip to content

Commit

Permalink
check if incoming token is from default token
Browse files Browse the repository at this point in the history
  • Loading branch information
KKA11010 committed Dec 7, 2023
1 parent 2097e19 commit f5c5ad4
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 31 deletions.
2 changes: 1 addition & 1 deletion assets/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@
"backupQ": "Wie funktioniert es?",
"backupHint": "Der aktuelle Sicherungsprozess stellt eine rudimentäre Umsetzung dar. Er erstellt ein Cashu-Token aus allen Mints und zugehörige Beweise, das nach neuen Transaktionen ungültig wird. Um den Token auf einem neuen Gerät wiederherzustellen, folgen Sie dem vertrauten Beanspruchungsprozess, und der alte Kontostand wird ungültig. Vermeiden Sie es, es auf dem aktuellen Kontostand einzulösen, um Fehler vorzubeugen. Es sei darauf hingewiesen, dass wir aktiv an der Entwicklung einer Sicherungslösung mit Passphrase arbeiten, um die Sicherheit und Bequemlichkeit zu verbessern.",
"singleBackupHint": "Hinweis: Sie können auch eine Sicherung für eine einzelne Mint erstellen unter 'Optionen' > 'Mint Management' > 'Mint auswählen' > 'Guthabensicherung'.",
"noDefaultHint": "Du musst eine Standard-Mint einrichten, um einen automatischen Tausch durchzuführen."
"noDefaultHint": "Sie müssen eine Standard-Mint einrichten, um einen automatischen Tausch durchzuführen."
},
"error": {
"checkSpendableErr": "Fehler beim Überprüfen, ob der Token ausgegeben werden kann",
Expand Down
8 changes: 6 additions & 2 deletions src/context/FocusClaim.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { l } from '@log'
import type { ITokenInfo } from '@model'
import { store } from '@store'
import { STORE_KEYS } from '@store/consts'
import { getStrFromClipboard, hasTrustedMint, isCashuToken, sleep } from '@util'
import { getDefaultMint } from '@store/mintStore'
import { getStrFromClipboard, hasTrustedMint, isCashuToken, isStr, sleep } from '@util'
import { isTokenSpendable } from '@wallet'
import { getTokenInfo } from '@wallet/proofs'
import { createContext, useCallback, useContext, useEffect, useRef, useState } from 'react'
Expand Down Expand Up @@ -32,9 +33,12 @@ const useFocusClaim = () => {
const info = getTokenInfo(cleanedClipboard)
if (!info) { return false }
// check if mint is a trusted one
const defaultM = await getDefaultMint()
const userMints = await getMintsUrls()
// do not claim from clipboard when app comes to the foreground if mint from token is not trusted
if (!hasTrustedMint(userMints, info.mints)) { return false }
if (!hasTrustedMint(userMints, info.mints)|| (isStr(defaultM) && !info.mints.includes(defaultM))) {
return false
}
// check if token is spendable
try {
const isSpendable = await isTokenSpendable(cleanedClipboard)
Expand Down
6 changes: 4 additions & 2 deletions src/screens/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { NS } from '@src/i18n'
import { store } from '@store'
import { STORE_KEYS } from '@store/consts'
import { addToHistory } from '@store/latestHistoryEntries'
import { saveDefaultOnInit } from '@store/mintStore'
import { getDefaultMint, saveDefaultOnInit } from '@store/mintStore'
import { highlight as hi, mainColors } from '@styles'
import { extractStrFromURL, getStrFromClipboard, hasTrustedMint, isCashuToken, isErr, isLnInvoice, isStr } from '@util'
import { claimToken, getMintsForPayment } from '@wallet'
Expand Down Expand Up @@ -128,9 +128,10 @@ export default function Dashboard({ navigation, route }: TDashboardPageProps) {
}
// save token info in state
setTokenInfo(tokenInfo)
const defaultM = await getDefaultMint()
// check if user wants to trust the token mint
const userMints = await getMintsUrls()
if (!hasTrustedMint(userMints, tokenInfo.mints)) {
if (!hasTrustedMint(userMints, tokenInfo.mints) || (isStr(defaultM) && !tokenInfo.mints.includes(defaultM))) {
closeOptsModal()
// ask user for permission if token mint is not in his mint list
const t = setTimeout(() => {
Expand Down Expand Up @@ -180,6 +181,7 @@ export default function Dashboard({ navigation, route }: TDashboardPageProps) {
if (token.length) { return }
startLoading()
const clipboard = await getStrFromClipboard()
l({ clipboard })
if (!clipboard?.length || !isCashuToken(clipboard)) {
openPromptAutoClose({ msg: t('invalidOrSpent') })
closeOptsModal()
Expand Down
16 changes: 8 additions & 8 deletions src/screens/Payment/Receive/nostrDM/Token.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import { addMint } from '@db'
import { l } from '@log'
import type { ITokenInfo } from '@model'
import type { IContact, INostrDm } from '@model/nostr'
import { getNostrUsername, truncateStr } from '@nostr/util'
import { useNostrContext } from '@src/context/Nostr'
import { usePromptContext } from '@src/context/Prompt'
import { useThemeContext } from '@src/context/Theme'
import { NS } from '@src/i18n'
import { getNostrUsername, truncateStr } from '@src/nostr/util'
import { addToHistory } from '@store/latestHistoryEntries'
import { getDefaultMint } from '@store/mintStore'
import { updateNostrRedeemed } from '@store/nostrDms'
import { highlight as hi, mainColors } from '@styles'
import { formatMintUrl, formatSatStr } from '@util'
import { formatMintUrl, formatSatStr, isStr } from '@util'
import { claimToken } from '@wallet'
import { getTokenInfo } from '@wallet/proofs'
import { useEffect, useState } from 'react'
Expand Down Expand Up @@ -56,11 +57,11 @@ export default function Token({ sender, token, id, dms, setDms, mints }: ITokenP
const handleRedeem = async () => {
if (!info) { return }
startLoading()
// check for unknown mint
if (!mints.includes(info.mints[0] || '')) {
const defaultM = await getDefaultMint()
// check if user wants to trust the token mint
if (!mints.includes(info.mints[0] || '') || (isStr(defaultM) && !info.mints.includes(defaultM))) {
// show trust modal
setTrustModal(true)
return
return setTrustModal(true)
}
await receiveToken()
}
Expand All @@ -85,8 +86,7 @@ export default function Token({ sender, token, id, dms, setDms, mints }: ITokenP
if (!success) {
openPromptAutoClose({ msg: t('invalidOrSpent') })
await handleStoreRedeemed()
stopLoading()
return
return stopLoading()
}
// add as history entry (receive ecash from nostr)
await addToHistory({
Expand Down
15 changes: 7 additions & 8 deletions src/screens/QRScan/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import { useIsFocused } from '@react-navigation/core'
import { usePromptContext } from '@src/context/Prompt'
import { useThemeContext } from '@src/context/Theme'
import { NS } from '@src/i18n'
import { getDefaultMint } from '@store/mintStore'
import { globals, mainColors } from '@styles'
import { decodeLnInvoice, extractStrFromURL, hasTrustedMint, isCashuToken, isNull, isUrl } from '@util'
import { decodeLnInvoice, extractStrFromURL, hasTrustedMint, isCashuToken, isNull, isStr, isUrl } from '@util'
import { getTokenInfo } from '@wallet/proofs'
import { BarCodeScanner, PermissionStatus } from 'expo-barcode-scanner'
import { Camera, FlashMode } from 'expo-camera'
Expand Down Expand Up @@ -48,17 +49,16 @@ export default function QRScanPage({ navigation, route }: TQRScanPageProps) {
const handleCashuToken = async (data: string) => {
const info = getTokenInfo(data)
if (!info) {
openPromptAutoClose({ msg: t('invalidOrSpent') })
return
return openPromptAutoClose({ msg: t('invalidOrSpent') })
}
// save token info in state
setTokenInfo(info)
// check if user wants to trust the token mint
const defaultM = await getDefaultMint()
const userMints = await getMintsUrls()
if (!hasTrustedMint(userMints, info.mints)) {
if (!hasTrustedMint(userMints, info.mints) || (isStr(defaultM) && !info.mints.includes(defaultM))) {
// ask user for permission if token mint is not in his mint list
setTrustModal(true)
return
return setTrustModal(true)
}
navigation.navigate('qr processing', { tokenInfo: info, token: data })
}
Expand All @@ -70,8 +70,7 @@ export default function QRScanPage({ navigation, route }: TQRScanPageProps) {
openPromptAutoClose({ msg: t('invalidToken') })
stopLoading()
// close modal
setTrustModal(false)
return
return setTrustModal(false)
}
for (const mint of tokenInfo.mints) {
// eslint-disable-next-line no-await-in-loop
Expand Down
20 changes: 10 additions & 10 deletions src/screens/Settings/General/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export default function GeneralSettings({ navigation, route }: TGeneralSettingsP
<ScrollView alwaysBounceVertical={false}>
<View style={globals(color).wrapContainer}>
<MenuItem
txt={t('security', { ns: NS.topNav })}
icon={<LockIcon color={color.TEXT} />}
onPress={() => navigation.navigate('Security settings')}
txt={t('display', { ns: NS.topNav })}
icon={<PaletteIcon color={color.TEXT} />}
onPress={() => navigation.navigate('Display settings')}
hasSeparator
hasChevron
/>
Expand All @@ -39,6 +39,13 @@ export default function GeneralSettings({ navigation, route }: TGeneralSettingsP
hasSeparator
hasChevron
/>
<MenuItem
txt={t('security', { ns: NS.topNav })}
icon={<LockIcon color={color.TEXT} />}
onPress={() => navigation.navigate('Security settings')}
hasSeparator
hasChevron
/>
{nostr.nutPub.length > 0 &&
<MenuItem
txt={t('contacts', { ns: NS.bottomNav })}
Expand All @@ -48,13 +55,6 @@ export default function GeneralSettings({ navigation, route }: TGeneralSettingsP
hasChevron
/>
}
<MenuItem
txt={t('display', { ns: NS.topNav })}
icon={<PaletteIcon color={color.TEXT} />}
onPress={() => navigation.navigate('Display settings')}
hasSeparator
hasChevron
/>
<MenuItem
txt={t('language', { ns: NS.topNav })}
icon={<LanguageIcon color={color.TEXT} />}
Expand Down

0 comments on commit f5c5ad4

Please sign in to comment.