mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-24 10:35:19 +00:00
Ignore extra spaces and special characters in Recovery Password entry
This commit is contained in:
parent
750dfb455d
commit
1cd7a57515
@ -46,7 +46,7 @@ internal class LinkDeviceViewModel @Inject constructor(
|
||||
fun onContinue() {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
codec.decodeAsByteArray(state.value.recoveryPhrase).let(::onSuccess)
|
||||
codec.sanitizeAndDecodeAsByteArray(state.value.recoveryPhrase).let(::onSuccess)
|
||||
} catch (e: Exception) {
|
||||
onFailure(e)
|
||||
}
|
||||
|
@ -76,6 +76,20 @@ class MnemonicCodecTest {
|
||||
assertEquals("0f2ccde528622876b8f16e14db97dafc", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `sanitizeAndDecodeAsByteArray with mnemonic with unnecessary spaces`() {
|
||||
val result = codec.sanitizeAndDecodeAsByteArray(" fuming nearby kennel husband dejected pepper jaded because dads goggles tufts tomorrow dejected ").let(Hex::toStringCondensed)
|
||||
|
||||
assertEquals("0f2ccde528622876b8f16e14db97dafc", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `sanitizeAndDecodeAsByteArray with mnemonic with special characters`() {
|
||||
val result = codec.sanitizeAndDecodeAsByteArray("...fuming nearby.kennel.husband . dejected pepper jaded because dads goggles tufts tomorrow dejected@").let(Hex::toStringCondensed)
|
||||
|
||||
assertEquals("0f2ccde528622876b8f16e14db97dafc", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `decodeMnemonicOrHexAsByteArray with hex`() {
|
||||
val result = codec.decodeMnemonicOrHexAsByteArray("0f2ccde528622876b8f16e14db97dafc").let(Hex::toStringCondensed)
|
||||
|
@ -114,8 +114,15 @@ class MnemonicCodec(private val loadFileContents: (String) -> String) {
|
||||
}.joinToString(separator = "") { it }
|
||||
}
|
||||
|
||||
fun sanitizeAndDecodeAsByteArray(mnemonic: String): ByteArray = sanitizeRecoveryPhrase(mnemonic).let(::decode).let(Hex::fromStringCondensed)
|
||||
fun decodeAsByteArray(mnemonic: String): ByteArray = decode(mnemonic = mnemonic).let(Hex::fromStringCondensed)
|
||||
|
||||
private fun sanitizeRecoveryPhrase(rawMnemonic: String): String = rawMnemonic
|
||||
.replace("[^\\w]+".toRegex(), " ") // replace any sequence of non-word characters with a space
|
||||
.trim() // remove leading and trailing whitespace (which may have been from prior special chars)
|
||||
.split("\\s+".toRegex()) // split on the now properly positioned spaces
|
||||
.joinToString(" ") // reassemble
|
||||
|
||||
fun decodeMnemonicOrHexAsByteArray(mnemonicOrHex: String): ByteArray = try {
|
||||
decode(mnemonic = mnemonicOrHex).let(Hex::fromStringCondensed)
|
||||
} catch (decodeException: Exception) {
|
||||
|
Loading…
Reference in New Issue
Block a user