Skip to content

Commit

Permalink
Merge pull request #72 from FigureTechnologies/jd/checksum-display
Browse files Browse the repository at this point in the history
Fix Checksum Failed Message to Show the Mismatched Sums
  • Loading branch information
jdfigure authored May 9, 2024
2 parents 5f57604 + 13c3121 commit 107e48a
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions bech32/src/main/kotlin/tech/figure/hdwallet/bech32/Bech32.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ object Bech32 {
}

val dataBytes = dataString.map { c -> charset.indexOf(c).toByte() }.toByteArray()
val checkBytes = dataString.takeLast(CHECKSUM_SIZE).map { c -> charset.indexOf(c).toByte() }.toByteArray()
val actualSum = checksum(hrp, dataBytes.dropLast(CHECKSUM_SIZE).toByteArray())
require(1 == polymod(expandHrp(hrp).plus(dataBytes.map { d -> d.toInt() }))) {
"checksum failed: $checkBytes != $actualSum"
val providedCheckSum = dataString.takeLast(CHECKSUM_SIZE)
val actualSumBech32Data = actualSum.map { charset[it.toInt()] }.joinToString("")
"checksum failed: got $providedCheckSum but expected $actualSumBech32Data"
}

return Bech32Data(hrp, dataBytes.dropLast(CHECKSUM_SIZE).toByteArray())
Expand Down

0 comments on commit 107e48a

Please sign in to comment.