-
Notifications
You must be signed in to change notification settings - Fork 8
Allow pasting all words at once during hot wallet import #525
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
Conversation
📝 WalkthroughWalkthroughThis PR adds mnemonic paste detection to the hot wallet import flow on both Android and iOS platforms. When users paste 12 or more space-separated words, the system detects this as a mnemonic input, parses it, validates the word count and validity, and populates the word grid accordingly. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In
`@android/app/src/main/java/org/bitcoinppl/cove/flows/NewWalletFlow/hot_wallet/HotWalletImportScreen.kt`:
- Around line 104-122: The keyed remember for enteredWords using
remember(currentNumberOfWords) causes the state to be recreated (losing pasted
words) when currentNumberOfWords changes; remove the key so enteredWords is
remembered across the transient mode change and explicitly reinitialize its
contents when you truly want to switch sizes (e.g., provide an
initEnteredWords(numberOfGroups) helper and call it only when confirming a size
change), and modify handlePasteMnemonic to set and clear an isHandlingPaste flag
(or otherwise defer size-based reinitialization until after paste handling
finishes) so pasted words are applied to the stable enteredWords state; update
references to enteredWords, currentNumberOfWords, handlePasteMnemonic and
isHandlingPaste accordingly.
In `@ios/Cove/Flows/NewWalletFlow/HotWallet/HotWalletImportScreen.swift`:
- Around line 600-629: The handler handlePasteMnemonic currently calls
setWords(grouped) before validating the pasted mnemonic; validate first using
groupedPlainWordsOf(mnemonic:groups:) (with the same groupsOf value) and only
call setWords(grouped) after the validation succeeds, otherwise set alertState =
.init(.invalidWords) on error and do not update the UI; keep the existing
try/catch around groupedPlainWordsOf and move it above the setWords call so the
UI is populated only on successful validation.
🧹 Nitpick comments (1)
android/app/src/main/java/org/bitcoinppl/cove/flows/NewWalletFlow/hot_wallet/HotWalletImportScreen.kt (1)
195-220: Validation occurs after state update.The
groupedPlainWordsOfvalidation happens aftersetWords, so invalid mnemonics are displayed in the UI with an error alert. If this is intentional (letting users see and fix invalid words), consider adding a brief comment. Otherwise, move validation beforesetWordsto prevent invalid data from being displayed.
| // use local state so we can update when paste changes word count | ||
| var currentNumberOfWords by remember { mutableStateOf(numberOfWords) } | ||
|
|
||
| val wordCount = | ||
| when (numberOfWords) { | ||
| when (currentNumberOfWords) { | ||
| NumberOfBip39Words.TWELVE -> 12 | ||
| NumberOfBip39Words.TWENTY_FOUR -> 24 | ||
| } | ||
|
|
||
| val numberOfGroups = wordCount / GROUPS_OF | ||
| var enteredWords by remember(numberOfWords) { | ||
| var enteredWords by remember(currentNumberOfWords) { | ||
| mutableStateOf(List(numberOfGroups) { List(GROUPS_OF) { "" } }) | ||
| } | ||
|
|
||
| var alertState by remember { mutableStateOf(AlertState.None) } | ||
| var duplicateWalletId by remember { mutableStateOf<WalletId?>(null) } | ||
| var genericErrorMessage by remember { mutableStateOf("") } | ||
| var focusedField by remember(numberOfWords) { mutableIntStateOf(0) } | ||
| var tabIndex by remember(numberOfWords) { mutableIntStateOf(0) } | ||
| var focusedField by remember(currentNumberOfWords) { mutableIntStateOf(0) } | ||
| var tabIndex by remember(currentNumberOfWords) { mutableIntStateOf(0) } |
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.
Potential bug: keyed remember may discard pasted words when word count changes.
When currentNumberOfWords changes (e.g., pasting 24 words while in 12-word mode), the remember(currentNumberOfWords) recreates enteredWords with empty lists on recomposition. The update in setWords happens on the old state instance before recomposition, so the pasted words may be lost.
Consider removing the key from remember and handling initialization explicitly, or ensuring state updates occur after recomposition settles:
Suggested approach
- var enteredWords by remember(currentNumberOfWords) {
+ var enteredWords by remember {
mutableStateOf(List(numberOfGroups) { List(GROUPS_OF) { "" } })
}
+
+ // Reset enteredWords when word count changes (except during paste)
+ var isHandlingPaste by remember { mutableStateOf(false) }
+ LaunchedEffect(currentNumberOfWords) {
+ if (!isHandlingPaste) {
+ val groups = if (currentNumberOfWords == NumberOfBip39Words.TWELVE) 1 else 2
+ enteredWords = List(groups) { List(GROUPS_OF) { "" } }
+ }
+ isHandlingPaste = false
+ }Then set isHandlingPaste = true at the start of handlePasteMnemonic.
🤖 Prompt for AI Agents
In
`@android/app/src/main/java/org/bitcoinppl/cove/flows/NewWalletFlow/hot_wallet/HotWalletImportScreen.kt`
around lines 104 - 122, The keyed remember for enteredWords using
remember(currentNumberOfWords) causes the state to be recreated (losing pasted
words) when currentNumberOfWords changes; remove the key so enteredWords is
remembered across the transient mode change and explicitly reinitialize its
contents when you truly want to switch sizes (e.g., provide an
initEnteredWords(numberOfGroups) helper and call it only when confirming a size
change), and modify handlePasteMnemonic to set and clear an isHandlingPaste flag
(or otherwise defer size-based reinitialization until after paste handling
finishes) so pasted words are applied to the stable enteredWords state; update
references to enteredWords, currentNumberOfWords, handlePasteMnemonic and
isHandlingPaste accordingly.
| func handlePasteMnemonic(_ mnemonicString: String) { | ||
| // extract word-like tokens, stripping numbers and punctuation | ||
| let words = mnemonicString | ||
| .split(whereSeparator: { $0.isWhitespace }) | ||
| .map { String($0).lowercased() } | ||
| .filter { word in | ||
| // keep only alphabetic strings (filters out "1.", "2)", etc.) | ||
| word.allSatisfy(\.isLetter) | ||
| } | ||
|
|
||
| // need 12 or 24 words | ||
| guard words.count == 12 || words.count == 24 else { | ||
| alertState = .init(.invalidWords) | ||
| return | ||
| } | ||
|
|
||
| // group words into chunks of groupsOf (12) | ||
| let grouped = stride(from: 0, to: words.count, by: groupsOf).map { | ||
| Array(words[$0 ..< min($0 + groupsOf, words.count)]) | ||
| } | ||
| setWords(grouped) | ||
|
|
||
| // validate - show alert if invalid | ||
| do { | ||
| _ = try groupedPlainWordsOf(mnemonic: words.joined(separator: " "), groups: UInt8(groupsOf)) | ||
| } catch { | ||
| Log.debug("Invalid pasted mnemonic: \(error)") | ||
| alertState = .init(.invalidWords) | ||
| } | ||
| } |
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.
Validation should occur before populating the UI.
The current flow calls setWords(grouped) before validating with groupedPlainWordsOf. If the pasted words aren't valid BIP39 words, they'll be populated into the input fields before the error alert appears, leaving the UI in an invalid state.
Consider validating first and only setting words on success:
Proposed fix
func handlePasteMnemonic(_ mnemonicString: String) {
// extract word-like tokens, stripping numbers and punctuation
let words = mnemonicString
.split(whereSeparator: { $0.isWhitespace })
.map { String($0).lowercased() }
.filter { word in
// keep only alphabetic strings (filters out "1.", "2)", etc.)
word.allSatisfy(\.isLetter)
}
// need 12 or 24 words
guard words.count == 12 || words.count == 24 else {
alertState = .init(.invalidWords)
return
}
+ // validate words are valid BIP39 before populating UI
+ do {
+ _ = try groupedPlainWordsOf(mnemonic: words.joined(separator: " "), groups: UInt8(groupsOf))
+ } catch {
+ Log.debug("Invalid pasted mnemonic: \(error)")
+ alertState = .init(.invalidWords)
+ return
+ }
+
// group words into chunks of groupsOf (12)
let grouped = stride(from: 0, to: words.count, by: groupsOf).map {
Array(words[$0 ..< min($0 + groupsOf, words.count)])
}
setWords(grouped)
-
- // validate - show alert if invalid
- do {
- _ = try groupedPlainWordsOf(mnemonic: words.joined(separator: " "), groups: UInt8(groupsOf))
- } catch {
- Log.debug("Invalid pasted mnemonic: \(error)")
- alertState = .init(.invalidWords)
- }
}🤖 Prompt for AI Agents
In `@ios/Cove/Flows/NewWalletFlow/HotWallet/HotWalletImportScreen.swift` around
lines 600 - 629, The handler handlePasteMnemonic currently calls
setWords(grouped) before validating the pasted mnemonic; validate first using
groupedPlainWordsOf(mnemonic:groups:) (with the same groupsOf value) and only
call setWords(grouped) after the validation succeeds, otherwise set alertState =
.init(.invalidWords) on error and do not update the UI; keep the existing
try/catch around groupedPlainWordsOf and move it above the setWords call so the
UI is populated only on successful validation.
Greptile SummaryAdded mnemonic paste functionality allowing users to paste complete 12 or 24-word mnemonics directly into any input field during hot wallet import. The implementation detects multi-word pastes, filters out numeric prefixes (e.g., "1.", "2."), validates the word count and mnemonic validity, then auto-populates all fields and navigates to the last page. Key Changes:
Implementation Quality:
Confidence Score: 5/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant User
participant TextField as WordInputField
participant Card as HotWalletImportCard
participant Screen as HotWalletImportScreen
participant Validator as groupedPlainWordsOf
User->>TextField: Paste mnemonic (12-24 words)
TextField->>TextField: Detect paste (split by whitespace)
alt Word count >= 12
TextField->>Card: onPasteMnemonic(mnemonicString)
Card->>Screen: handlePasteMnemonic(mnemonicString)
Screen->>Screen: Extract alphabetic tokens
Screen->>Screen: Filter numbered prefixes (1., 2., etc)
alt Word count == 12 or 24
Screen->>Screen: Group words into chunks of 12
Screen->>Validator: groupedPlainWordsOf(words, groups)
alt Validation succeeds
Validator-->>Screen: Valid mnemonic
Screen->>Screen: setWords(grouped)
Screen->>Screen: Update currentNumberOfWords
Screen->>Screen: Focus last field on last page
Screen-->>User: Display filled fields
else Validation fails
Validator-->>Screen: Invalid mnemonic
Screen->>Screen: Set alertState = InvalidWords
Screen-->>User: Show error alert
end
else Invalid word count
Screen->>Screen: Set alertState = InvalidWords/GenericError
Screen-->>User: Show error alert
end
else Word count < 12
TextField->>TextField: Process as normal single-word input
end
|
Closes #387
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.