-
Notifications
You must be signed in to change notification settings - Fork 33
[WIP] Replace unmaintained packages #750
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import { useCallback, useEffect, useRef, useState } from 'react' | ||
| import EncryptedStorage from 'react-native-encrypted-storage' | ||
| import RNFS from 'react-native-fs' | ||
| import crypto from 'react-native-quick-crypto' | ||
| import * as ExpoCrypto from 'expo-crypto' | ||
| import * as SecureStore from 'expo-secure-store' | ||
| import * as FileSystem from 'expo-file-system' | ||
| import { useMutation, useQuery, UseQueryResult } from 'react-query' | ||
| import { | ||
| Conversation, | ||
|
|
@@ -762,16 +762,16 @@ | |
| } { | ||
| const { data: address, refetch } = useQuery<string | null>( | ||
| ['xmtp', 'address'], | ||
| () => EncryptedStorage.getItem('xmtp.address') | ||
| () => SecureStore.getItem('xmtp.address') | ||
| ) | ||
| return { | ||
| address, | ||
| save: async (address: string) => { | ||
| await EncryptedStorage.setItem('xmtp.address', address) | ||
| SecureStore.setItem('xmtp.address', address) | ||
| await refetch() | ||
| }, | ||
| clear: async () => { | ||
| await EncryptedStorage.removeItem('xmtp.address') | ||
| await SecureStore.deleteItemAsync('xmtp.address') | ||
| await refetch() | ||
| }, | ||
| } | ||
|
|
@@ -784,11 +784,11 @@ | |
| try { | ||
| const key = `xmtp-${network}` | ||
|
|
||
| const result = await EncryptedStorage.getItem(key) | ||
| if ((result && clear === true) || !result) { | ||
| const result = SecureStore.getItem(key) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SecureStore calls in - const result = SecureStore.getItem(key)
+ const result = await SecureStore.getItem(key)
|
||
| if (!result || clear === true) { | ||
| if (result) { | ||
| console.log('Removing existing dbEncryptionKey', key) | ||
| await EncryptedStorage.removeItem(key) | ||
| await SecureStore.deleteItemAsync(key) | ||
| } | ||
|
|
||
| // Generate random bytes for the encryption key | ||
|
|
@@ -797,13 +797,13 @@ | |
|
|
||
| // Convert to string for storage | ||
| const randomBytesString = uint8ArrayToHexString(randomBytes) | ||
| await EncryptedStorage.setItem(key, randomBytesString) | ||
| SecureStore.setItem(key, randomBytesString) | ||
|
|
||
| return randomBytes | ||
| } else { | ||
| // Convert stored string back to Uint8Array | ||
| return hexStringToUint8Array(result) | ||
| } | ||
|
|
||
| // Convert stored string back to Uint8Array | ||
| return hexStringToUint8Array(result) | ||
| } catch (error) { | ||
| console.error('Error in getDbEncryptionKey:', error) | ||
| // Re-throw or handle as needed | ||
|
|
@@ -831,9 +831,15 @@ | |
| return new Uint8Array(byteArray) | ||
| } | ||
|
|
||
| function ensureFileUri(path: string): string { | ||
| return path.startsWith('file://') ? path : `file://${path}` | ||
| } | ||
|
|
||
| async function fileExists(path: string): Promise<boolean> { | ||
| try { | ||
| return await RNFS.exists(path) | ||
| const uri = ensureFileUri(path) | ||
| const info = await FileSystem.getInfoAsync(uri) | ||
| return info.exists | ||
| } catch (error) { | ||
| console.error('Error checking file existence:', error) | ||
| return false | ||
|
|
@@ -842,8 +848,12 @@ | |
|
|
||
| async function getFileSize(path: string): Promise<number> { | ||
| try { | ||
| const stats = await RNFS.stat(path) | ||
| return stats.size | ||
| const uri = ensureFileUri(path) | ||
| const info = await FileSystem.getInfoAsync(uri, { size: true }) | ||
| if (info.exists) { | ||
| return info.size | ||
| } | ||
| return 0 | ||
| } catch (error) { | ||
| console.error('Error getting file size:', error) | ||
| return 0 | ||
|
|
@@ -853,13 +863,18 @@ | |
| async function calculateFileDigest(path: string): Promise<string> { | ||
| try { | ||
| // Read the file content | ||
| const fileContent = await RNFS.readFile(path, 'base64') | ||
| const uri = ensureFileUri(path) | ||
| const fileContent = await FileSystem.readAsStringAsync(uri, { | ||
| encoding: FileSystem.EncodingType.Base64, | ||
| }) | ||
| const buffer = Buffer.from(fileContent, 'base64') | ||
|
|
||
| // Create SHA-256 hash using react-native-quick-crypto | ||
| const hash = crypto.createHash('sha256') | ||
| hash.update(buffer.buffer) | ||
| return hash.digest('hex') | ||
| // Create SHA-256 hash | ||
| const digest = await ExpoCrypto.digest( | ||
| ExpoCrypto.CryptoDigestAlgorithm.SHA256, | ||
| buffer | ||
| ) | ||
| return Buffer.from(digest).toString('hex') | ||
| } catch (error) { | ||
| console.error('Error calculating file digest:', error) | ||
| throw error | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both the fetcher in
useQueryand thesavelogic insavecall non-existent synchronous methods (SecureStore.getItem/setItem) and don’t await them, leading to undefined-methodTypeErrors and race conditions (e.g.refetch()before storage). Update toawait SecureStore.getItemAsync(...)in your query fetcher andawait SecureStore.setItemAsync(...)insaveto ensure the values exist and are persisted before proceeding.