Skip to content

Instantly share code, notes, and snippets.

@gafiatulin
gafiatulin / Bech32.scala
Last active May 29, 2022 15:44
Scala implementation of Bech32 — general checksummed base32 format.
import scala.util.Try
// BIP173
// https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki
case object Bech32 {
type Int5 = Byte
final val CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"
final val CHARSET_MAP: Map[Char, Int5] = CHARSET.zipWithIndex.toMap.mapValues(_.toByte)
final val CHARSET_REVERSE_MAP: Map[Int5, Char] = CHARSET_MAP.map(_.swap)