Skip to content

Commit

Permalink
fix: fix scan qrcode the app crash
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhenQian committed Oct 8, 2024
1 parent 874b80a commit 75d2bc3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
40 changes: 30 additions & 10 deletions src/features/keystone/ScanQrCodeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,48 @@ import { URDecoder } from '@ngraveio/bc-ur'
import KeystoneSDK, { MultiAccounts, UR } from '@keystonehq/keystone-sdk'
import { RootNavigationProp } from 'src/navigation/rootTypes'
import { useNavigation } from '@react-navigation/native'
import { Alert } from 'react-native'
import { useTranslation } from 'react-i18next'
import { KeystoneAccountType } from './SelectKeystoneAccountsScreen'

const ScanQrCodeScreen = () => {
const { t } = useTranslation()
const navigation = useNavigation<RootNavigationProp>()
const [multiAccounts, setMultiAccounts] = useState<MultiAccounts>()
const decoder = useMemo(() => new URDecoder(), [])
const [isScanQrCodeComplete, setIsScanQrCodeComplete] = useState(false)
const [progress, setProgress] = useState<number>(0)
const [isUnexpectedQrCode, setIsUnexpectedQrCode] = useState(false)
const handleBarCodeScanned = (qrString: string) => {
decoder.receivePart(qrString.toLowerCase())
setProgress(Number((decoder.getProgress() * 100).toFixed(0)))
if (decoder.isComplete()) {
const ur = decoder.resultUR()
const qrCodeDataRes: MultiAccounts = new KeystoneSDK().parseMultiAccounts(
new UR(Buffer.from(ur.cbor.toString('hex'), 'hex'), ur.type),
)
setProgress(100)
setIsScanQrCodeComplete(true)
setMultiAccounts(qrCodeDataRes)
// fix unexpected qrcode string
try {
decoder.receivePart(qrString.toLowerCase())
setProgress(Number((decoder.getProgress() * 100).toFixed(0)))
if (decoder.isComplete()) {
const ur = decoder.resultUR()
const qrCodeDataRes: MultiAccounts =
new KeystoneSDK().parseMultiAccounts(
new UR(Buffer.from(ur.cbor.toString('hex'), 'hex'), ur.type),
)
setProgress(100)
setIsScanQrCodeComplete(true)
setMultiAccounts(qrCodeDataRes)
}
} catch (error) {
setIsUnexpectedQrCode(true)
}
}

useEffect(() => {
if (isUnexpectedQrCode) {
Alert.alert(
t('keystone.connectKeystoneStart.unexpectedQrCodeTitle'),
t('keystone.connectKeystoneStart.unexpectedQrCodeContent'),
)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isUnexpectedQrCode])

useEffect(() => {
if (isScanQrCodeComplete) {
const derivationAccounts: KeystoneAccountType[] = []
Expand Down
3 changes: 3 additions & 0 deletions src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,9 @@ export default {
scanQrCode: 'Scan QR Code',
warning: 'Please enable your camera permission via [Settings]',
ok: 'OK',
unexpectedQrCodeContent:
'The QR code you scanned is not valid. Please try again.',
unexpectedQrCodeTitle: 'Unexpected QR Code',
},
selectKeystoneAccounts: {
subtitle:
Expand Down

0 comments on commit 75d2bc3

Please sign in to comment.