Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#263 #264

Merged
merged 5 commits into from
Nov 19, 2023
Merged

#263 #264

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default (): JestConfigWithTsJest => ({
// '!**/src/styles/**',
'!**/src/consts/**',
// '!**/src/components/**',
'!**/src/AppEntry.ts',
'!**/src/App.ts',
'!**/src/shim.ts',
// '!**/src/storage/store/AsyncStore.ts',
'!**/src/storage/store/SecureStore.ts',
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"cpd:tsx": "npx -y jscpd -c config/cpd.config.tsx.json src",
"cpd": "npm run cpd:ts && npm run cpd:tsx",
"deps:c": "npx -y madge --circular --ts-config tsconfig.json --extensions ts,tsx .",
"eas-build-pre-install": "echo \"require('./src/AppEntry.ts')\" > index.js",
"eas-build-pre-install": "echo \"require('./src/App.ts')\" > index.js",
"update-translations": "node utils/update-translations.js",
"android": "expo run:android"
},
Expand All @@ -44,7 +44,7 @@
"@react-navigation/core": "^6.4.10",
"@react-navigation/native": "^6.1.9",
"@react-navigation/native-stack": "^6.9.17",
"@sentry/react-native": "5.5.0",
"@sentry/react-native": "^5.5.0",
"@shopify/flash-list": "1.4.3",
"crypto-js": "4.2.0",
"expo": "^49.0.18",
Expand Down Expand Up @@ -84,6 +84,7 @@
"text-encoding-polyfill": "^0.6.7"
},
"devDependencies": {
"@babel/core": "^7.23.3",
"@babel/plugin-proposal-private-methods": "^7.18.6",
"@getify/eslint-plugin-proper-arrows": "^11.0.3",
"@testing-library/react-native": "^12.4.0",
Expand Down Expand Up @@ -118,8 +119,7 @@
"ts-jest-resolver": "^2.0.1",
"ts-node": "^10.9.1",
"typescript": "^5.2.2",
"websql": "^2.0.3",
"@babel/core": "^7.23.3"
"websql": "^2.0.3"
},
"homepage": ".",
"contributors": [
Expand Down Expand Up @@ -151,10 +151,10 @@
"blind-signatures",
"lightning-network"
],
"version": "0.1.0",
"version": "0.1.1",
"license": "AGPL-3.0-only",
"bugs": {
"url": "https://github.com/cashubtc/eNuts/issues"
},
"main": "src/AppEntry.ts"
"main": "src/App.ts"
}
File renamed without changes.
3 changes: 3 additions & 0 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ function _App() {
}

const handlePinForeground = async () => {
// check if app has pw
const pw = await secureStore.get(SECURESTORE_KEY)
if (isNull(pw)) { return }
// check if app is locked
const now = Math.ceil(Date.now() / 1000)
const lockData = await store.getObj<ILockData>(STORE_KEYS.lock)
Expand Down
64 changes: 61 additions & 3 deletions src/components/ClipboardModal.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,76 @@
import { getEncodedToken } from '@cashu/cashu-ts'
import { type RootStackParamList } from '@model/nav'
import { type NavigationProp, useNavigation } from '@react-navigation/core'
import { useFocusClaimContext } from '@src/context/FocusClaim'
import { usePromptContext } from '@src/context/Prompt'
import { useThemeContext } from '@src/context/Theme'
import { NS } from '@src/i18n'
import { globals } from '@styles'
import { copyStrToClipboard, formatInt, formatMintUrl, formatSatStr } from '@util'
import { addToHistory } from '@store/latestHistoryEntries'
import { globals, mainColors } from '@styles'
import { copyStrToClipboard, formatInt, formatMintUrl, formatSatStr, isErr } from '@util'
import { claimToken } from '@wallet'
import { getTokenInfo } from '@wallet/proofs'
import { useTranslation } from 'react-i18next'
import { Text } from 'react-native'

import Button, { TxtButton } from './Button'
import useLoading from './hooks/Loading'
import Loading from './Loading'
import MyModal from './modal'
import Txt from './Txt'

type StackNavigation = NavigationProp<RootStackParamList>

export default function ClipboardModal() {
const nav = useNavigation<StackNavigation>()
const { t } = useTranslation([NS.common])
const { color, highlight } = useThemeContext()
const { tokenInfo, claimOpen, closeModal, handleRedeem } = useFocusClaimContext()
const { tokenInfo, claimOpen, setClaimOpen, setClaimed, closeModal } = useFocusClaimContext()
const { loading, startLoading, stopLoading } = useLoading()
const { openPromptAutoClose } = usePromptContext()

const handleRedeem = async () => {
startLoading()
if (!tokenInfo) {
setClaimOpen(false)
return stopLoading()
}
const encoded = getEncodedToken(tokenInfo.decoded)
try {
const success = await claimToken(encoded)
if (!success) {
stopLoading()
setClaimOpen(false)
return openPromptAutoClose({ msg: t('invalidOrSpent', { ns: NS.common }) })
}
} catch (e) {
stopLoading()
setClaimOpen(false)
return openPromptAutoClose({ msg: isErr(e) ? e.message : t('claimTokenErr', { ns: NS.error }) })
}
const info = getTokenInfo(encoded)
if (!info) {
stopLoading()
setClaimOpen(false)
return openPromptAutoClose({ msg: t('tokenInfoErr', { ns: NS.common }) })
}
stopLoading()
setClaimOpen(false)
// add as history entry (receive ecash)
await addToHistory({
amount: info.value,
type: 1,
value: encoded,
mints: info.mints,
})
nav.navigate('success', {
amount: info?.value,
memo: info?.decoded.memo,
isClaim: true
})
setClaimed(true)
}

return (
tokenInfo &&
<MyModal type='bottom' animation='slide' visible={claimOpen} close={closeModal}>
Expand All @@ -31,6 +88,7 @@ export default function ClipboardModal() {
<Button
txt={t('accept')}
onPress={() => void handleRedeem()}
icon={loading ? <Loading color={mainColors.WHITE} /> : undefined}
/>
<TxtButton
txt={t('cancel')}
Expand Down
56 changes: 4 additions & 52 deletions src/context/FocusClaim.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
import { getEncodedToken } from '@cashu/cashu-ts'
import { isIOS } from '@consts'
import { getMintsUrls } from '@db'
import { l } from '@log'
import type { ITokenInfo } from '@model'
import type { RootStackParamList } from '@model/nav'
import { type NavigationProp, useNavigation } from '@react-navigation/core'
import { NS } from '@src/i18n'
import { store } from '@store'
import { STORE_KEYS } from '@store/consts'
import { addToHistory } from '@store/latestHistoryEntries'
import { getStrFromClipboard, hasTrustedMint, isCashuToken, isErr, sleep } from '@util'
import { claimToken, isTokenSpendable } from '@wallet'
import { getStrFromClipboard, hasTrustedMint, isCashuToken, sleep } from '@util'
import { isTokenSpendable } from '@wallet'
import { getTokenInfo } from '@wallet/proofs'
import { createContext, useCallback, useContext, useEffect, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { AppState } from 'react-native'

import { usePromptContext } from './Prompt'

type StackNavigation = NavigationProp<RootStackParamList>

const useFocusClaim = () => {
const { t } = useTranslation([NS.error])
// back-foreground state reference
const appState = useRef(AppState.currentState)
const nav = useNavigation<StackNavigation>()
const { openPromptAutoClose } = usePromptContext()
const [claimed, setClaimed] = useState(false)
// modal
const [claimOpen, setClaimOpen] = useState(false)
Expand Down Expand Up @@ -69,40 +56,6 @@ const useFocusClaim = () => {
}
}

const handleRedeem = async () => {
if (!tokenInfo) { return }
setClaimOpen(false)
const encoded = getEncodedToken(tokenInfo.decoded)
try {
const success = await claimToken(encoded).catch(l)
if (!success) {
openPromptAutoClose({ msg: t('invalidOrSpent', { ns: NS.common }) })
return
}
} catch (e) {
openPromptAutoClose({ msg: isErr(e) ? e.message : t('claimTokenErr') })
return
}
const info = getTokenInfo(encoded)
if (!info) {
openPromptAutoClose({ msg: t('tokenInfoErr', { ns: NS.common }) })
return
}
// add as history entry (receive ecash)
await addToHistory({
amount: info.value,
type: 1,
value: encoded,
mints: info.mints,
})
nav.navigate('success', {
amount: info?.value,
memo: info?.decoded.memo,
isClaim: true
})
setClaimed(true)
}

useEffect(() => {
// disable foreground claim on iOS
if (isIOS) { return }
Expand All @@ -129,9 +82,9 @@ const useFocusClaim = () => {
claimed,
setClaimed,
claimOpen,
setClaimOpen,
closeModal,
tokenInfo,
handleRedeem
}
}
type useFocusClaimType = ReturnType<typeof useFocusClaim>
Expand All @@ -144,10 +97,9 @@ const FocusClaimCtx = createContext<useFocusClaimType>({
claimed: false,
setClaimed: () => l(''),
claimOpen: false,
setClaimOpen: () => l(''),
closeModal: () => l(''),
tokenInfo: undefined,
// eslint-disable-next-line @typescript-eslint/await-thenable, no-return-await
handleRedeem: async () => await l('')
})

export const useFocusClaimContext = () => useContext(FocusClaimCtx)
Expand Down
2 changes: 0 additions & 2 deletions src/screens/Auth/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ export default function AuthPage({ navigation, route }: TAuthPageProps) {
return
}
setSuccess(true)
// navigate to dashboard
navigation.navigate(shouldRemove ? 'Security settings' : 'dashboard')
return
}
Expand Down Expand Up @@ -183,7 +182,6 @@ export default function AuthPage({ navigation, route }: TAuthPageProps) {
}
// skip pin setup
await store.set(STORE_KEYS.pinSkipped, '1')
// check if initial explainer has been viewed
navigation.navigate(shouldEdit ? 'Security settings' : 'dashboard')

}
Expand Down
7 changes: 4 additions & 3 deletions src/screens/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import { addMint, getBalance, getMintsUrls, hasMints } from '@db'
import { l } from '@log'
import OptsModal from '@modal/OptsModal'
import TrustMintModal from '@modal/TrustMint'
import type { TDashboardPageProps } from '@model/nav'
import type { TBeforeRemoveEvent, TDashboardPageProps } from '@model/nav'
import BottomNav from '@nav/BottomNav'
import { preventBack } from '@nav/utils'
import { useFocusClaimContext } from '@src/context/FocusClaim'
import { useInitialURL } from '@src/context/Linking'
import { useNostrContext } from '@src/context/Nostr'
Expand All @@ -29,7 +30,7 @@ import { claimToken, getMintsForPayment } from '@wallet'
import { getTokenInfo } from '@wallet/proofs'
import { useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { BackHandler, StyleSheet, TouchableOpacity, View } from 'react-native'
import { StyleSheet, TouchableOpacity, View } from 'react-native'

export default function Dashboard({ navigation, route }: TDashboardPageProps) {
const { t } = useTranslation([NS.common])
Expand Down Expand Up @@ -286,7 +287,7 @@ export default function Dashboard({ navigation, route }: TDashboardPageProps) {

// prevent back navigation - https://reactnavigation.org/docs/preventing-going-back/
useEffect(() => {
const backHandler = () => BackHandler.exitApp()
const backHandler = (e: TBeforeRemoveEvent) => preventBack(e, navigation.dispatch)
navigation.addListener('beforeRemove', backHandler)
return () => navigation.removeListener('beforeRemove', backHandler)
}, [navigation])
Expand Down
Loading