Fix/recovery password copies linebreak (#909)

* Filtered any control characters or double-spaces from copied recovery password mnemonic

* Fix typo in comment

* Single char comment alignment adjustment

---------

Co-authored-by: alansley <aclansley@gmail.com>
pull/1710/head
AL-Session 2 months ago committed by GitHub
parent c05aca33be
commit 74f4d5a134
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -27,14 +27,33 @@ class RecoveryPasswordViewModel @Inject constructor(
): AndroidViewModel(application) {
val prefs = AppTextSecurePreferences(application)
// Regex to remove any spurious characters from our recovery password mnemonic.
// The regex matches are:
// - "\r" - carriage return,
// - "\n" - newline,
// - "\u2028" - unicode line separator,
// - "\u2029" - unicode paragraph separator,
// - "|\s{2,}" - two or more consecutive spaces.
val linebreakRemovalRegex = Regex("""[\r\n\u2028\u2029]+|\s{2,}""")
val seed = MutableStateFlow<String?>(null)
val mnemonic = seed.filterNotNull()
.map { MnemonicCodec { MnemonicUtilities.loadFileContents(application, it) }.encode(it, MnemonicCodec.Language.Configuration.english) }
.map {
MnemonicCodec {
MnemonicUtilities.loadFileContents(application, it)
}
.encode(it, MnemonicCodec.Language.Configuration.english)
.trim() // Remove any leading or trailing whitespace
}
.stateIn(viewModelScope, SharingStarted.Eagerly, "")
fun copyMnemonic() {
prefs.setHasViewedSeed(true)
ClipData.newPlainText("Seed", mnemonic.value)
// Ensure that our mnemonic words are separated by single spaces only without any control characters
val normalisedMnemonic = mnemonic.value.replace(linebreakRemovalRegex, " ")
ClipData.newPlainText("Seed", normalisedMnemonic)
.let(application.clipboard::setPrimaryClip)
}

Loading…
Cancel
Save