Skip to content
Open
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
5 changes: 4 additions & 1 deletion src/app/wallet-setup/import-wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import {
} from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { toast } from 'sonner-native';
import { useMnemonicClipboardCleanup } from '@/hooks/useMnemonicClipboardCleanup';

export default function ImportWalletScreen() {
const router = useDebouncedNavigation();
const insets = useSafeAreaInsets();
const [secretWords, setSecretWords] = useState<string[]>(Array(12).fill(''));
const { scheduleClearClipboard } = useMnemonicClipboardCleanup();

const handleWordChange = (index: number, text: string) => {
const newWords = [...secretWords];
Expand Down Expand Up @@ -56,6 +58,7 @@ export default function ImportWalletScreen() {
setSecretWords(newWords);

toast.success('12 words have been pasted from clipboard');
scheduleClearClipboard(words.join(' '));
} catch (error) {
console.error('Paste error:', error);
toast.error('Could not paste from clipboard');
Expand All @@ -69,7 +72,7 @@ export default function ImportWalletScreen() {
};

const isFormValid = () => {
return secretWords.every(word => word.trim().length > 0);
return secretWords.every((word) => word.trim().length > 0);
};

const validateSeedPhrase = (phrase: string): boolean => {
Expand Down
45 changes: 45 additions & 0 deletions src/hooks/useMnemonicClipboardCleanup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as Clipboard from 'expo-clipboard';
import { useEffect, useRef } from 'react';

const CLEAR_CLIPBOARD_AFTER_MS = 5 * 60 * 1000; //5 mins

const normalizePhrase = (s: string) => s.trim().toLowerCase().replace(/\s+/g, ' ');

const checkIsMnemonic = (s: string) => {
const words = normalizePhrase(s).split(' ');
return words.length === 12 && words.every(Boolean);
};

export function useMnemonicClipboardCleanup() {
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const lastMnemonicRef = useRef<string | null>(null);

const scheduleClearClipboard = (mnemonic: string) => {
if (timerRef.current) clearTimeout(timerRef.current);

lastMnemonicRef.current = normalizePhrase(mnemonic);

timerRef.current = setTimeout(async () => {
try {
const current = await Clipboard.getStringAsync();
const normalizedCurrent = normalizePhrase(current);

if (
lastMnemonicRef.current &&
normalizedCurrent === lastMnemonicRef.current &&
checkIsMnemonic(normalizedCurrent)
) {
await Clipboard.setStringAsync('');
}
} catch {}
}, CLEAR_CLIPBOARD_AFTER_MS);
};

useEffect(() => {
return () => {
if (timerRef.current) clearTimeout(timerRef.current);
};
}, []);

return { scheduleClearClipboard };
}