Prefer to fail with InputTooShort over InvalidWord

This commit is contained in:
bemusementpark 2024-07-11 21:48:53 +09:30
parent 30ee3946ef
commit b453f69bfd
2 changed files with 6 additions and 9 deletions

View File

@ -35,14 +35,14 @@ class MnemonicCodecTest {
@Test
fun `decode one invalid word that is too short`() {
assertThrows(InvalidWord::class.java) {
assertThrows(InputTooShort::class.java) {
codec.decode("a")
}
}
@Test
fun `decode one invalid word`() {
assertThrows(InvalidWord::class.java) {
assertThrows(InputTooShort::class.java) {
codec.decode("abcd")
}
}
@ -99,7 +99,7 @@ class MnemonicCodecTest {
@Test
fun `decodeMnemonicOrHexAsByteArray with account id throws`() {
assertThrows(InvalidWord::class.java) {
assertThrows(InputTooShort::class.java) {
codec.decodeMnemonicOrHexAsByteArray("0582e1421da6f584a4795d30b654b4f25fed860afdf081075cb26a2b997e492f14").let(Hex::toStringCondensed)
}
}
@ -109,7 +109,7 @@ class MnemonicCodecTest {
// throws InvalidWord as 0f2ccde528622876b8f16e14db97dafcg is not a valid word on the english wordlist.
// It is also not a valid hex string, but we assume that a non-hex string is a recovery password.
assertThrows(InvalidWord::class.java) {
assertThrows(InputTooShort::class.java) {
codec.decodeMnemonicOrHexAsByteArray("0f2ccde528622876b8f16e14db97dafcg").let(Hex::toStringCondensed)
}
}

View File

@ -83,8 +83,8 @@ class MnemonicCodec(private val loadFileContents: (String) -> String) {
val prefixLength = languageConfiguration.prefixLength
val n = truncatedWordSet.size.toLong()
if (mnemonic.isEmpty()) throw DecodingError.InputTooShort
if (words.isEmpty()) throw DecodingError.InputTooShort
// Check preconditions
if (words.size < 13) throw DecodingError.InputTooShort
fun String.prefix() = substring(0 until prefixLength)
@ -96,9 +96,6 @@ class MnemonicCodec(private val loadFileContents: (String) -> String) {
val wordIndexes = wordPrefixes.map { truncatedWordSet.indexOf(it) }
.onEach { if (it < 0) throw DecodingError.InvalidWord }
// Check preconditions
if (words.size < 13) throw DecodingError.InputTooShort
// Verify checksum
val checksumIndex = determineChecksumIndex(words.dropLast(1), prefixLength)
val expectedChecksumWord = words[checksumIndex]