Ignore extra spaces and special characters in Recovery Password entry

This commit is contained in:
Andrew 2024-06-27 21:45:13 +09:30
parent 750dfb455d
commit 1cd7a57515
3 changed files with 22 additions and 1 deletions

View File

@ -46,7 +46,7 @@ internal class LinkDeviceViewModel @Inject constructor(
fun onContinue() { fun onContinue() {
viewModelScope.launch { viewModelScope.launch {
try { try {
codec.decodeAsByteArray(state.value.recoveryPhrase).let(::onSuccess) codec.sanitizeAndDecodeAsByteArray(state.value.recoveryPhrase).let(::onSuccess)
} catch (e: Exception) { } catch (e: Exception) {
onFailure(e) onFailure(e)
} }

View File

@ -76,6 +76,20 @@ class MnemonicCodecTest {
assertEquals("0f2ccde528622876b8f16e14db97dafc", result) 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 @Test
fun `decodeMnemonicOrHexAsByteArray with hex`() { fun `decodeMnemonicOrHexAsByteArray with hex`() {
val result = codec.decodeMnemonicOrHexAsByteArray("0f2ccde528622876b8f16e14db97dafc").let(Hex::toStringCondensed) val result = codec.decodeMnemonicOrHexAsByteArray("0f2ccde528622876b8f16e14db97dafc").let(Hex::toStringCondensed)

View File

@ -114,8 +114,15 @@ class MnemonicCodec(private val loadFileContents: (String) -> String) {
}.joinToString(separator = "") { it } }.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) 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 { fun decodeMnemonicOrHexAsByteArray(mnemonicOrHex: String): ByteArray = try {
decode(mnemonic = mnemonicOrHex).let(Hex::fromStringCondensed) decode(mnemonic = mnemonicOrHex).let(Hex::fromStringCondensed)
} catch (decodeException: Exception) { } catch (decodeException: Exception) {