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

@@ -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]