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

fix: Crash on invalid words #653

Merged
merged 2 commits into from
Feb 24, 2025
Merged
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
84 changes: 54 additions & 30 deletions src/screens/InitWallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,53 @@ class NewWordsScreen extends React.Component {
}
}

/**
* Handles the validation of the raw seed words input
* @param {string} rawWords String containing all the words written by the user
* @returns {{words: string[], errorMessage: string, isValid: boolean}}
*/
function handleValidWords(rawWords) {
let trimmedWordsStr = rawWords.trim(/\s+/);
let errorMessage = '';
let isValid = false;

try {
const { valid, words } = walletUtils.wordsValid(trimmedWordsStr);
trimmedWordsStr = words;
isValid = valid;
} catch (e) {
// Handling unknown errors
if (!(e instanceof hathorErrors.InvalidWords)) {
throw e;
}

// Handling Invalid Words errors
isValid = false;
errorMessage = e.message;
const listOfInvalidWords = e.invalidWords.filter((word) => word.length !== 0);
if (listOfInvalidWords.length > 0) {
errorMessage = `${errorMessage} List of invalid words: ${e.invalidWords.join(', ')}.`;
}
}
const wordsArr = trimmedWordsStr.split(/\s+/);
const nonEmptyWords = wordsArr.filter((word) => word.length !== 0);
const lowercaseWords = nonEmptyWords.map((word) => word.toLowerCase());

return {
words: lowercaseWords,
errorMessage,
isValid,
}
}

class LoadWordsScreen extends React.Component {
/**
* State for the import wallet words screen
* @type {{rawWords: '', words: string[], errorMessage: string, isValid: boolean}}
*/
state = {
words: '',
rawWords: '',
words: [],
errorMessage: '',
isValid: false,
};
Expand Down Expand Up @@ -265,44 +309,24 @@ class LoadWordsScreen extends React.Component {
}) });

onChangeText = (text) => {
const words = text.trim(/\s+/);
let errorMessage = '';
let isValid = false;
if (words) {
try {
walletUtils.wordsValid(words);
isValid = true;
} catch (e) {
if (e instanceof hathorErrors.InvalidWords) {
errorMessage = e.message;
if (e.invalidWords && e.invalidWords.length > 0) {
errorMessage = `${errorMessage} List of invalid words: ${e.invalidWords.join(', ')}.`;
}
} else {
throw e;
}
}
}

const wordsArr = words.split(/\s+/);
const nonEmptyWords = wordsArr.filter((value) => value.length !== 0);
const nonEmptyWordsLowerCase = nonEmptyWords.map((value) => value.toLowerCase());
const { words, isValid, errorMessage } = handleValidWords(text);
this.setState({
words: nonEmptyWordsLowerCase,
rawWords: text,
words,
errorMessage,
isValid,
});
}

loadClicked = () => {
Keyboard.dismiss();
const words = this.state.words.join(' ');
this.setState({ errorMessage: '' });
const result = walletUtils.wordsValid(words);
if (result.valid) {
this.props.navigation.navigate('ChoosePinScreen', { words });
const { rawWords } = this.state;
const { isValid, errorMessage, words } = handleValidWords(rawWords);

if (isValid) {
this.props.navigation.navigate('ChoosePinScreen', { words: words.join(' ') });
} else {
this.setState({ errorMessage: result.message });
this.setState({ errorMessage });
}
}

Expand Down