Bech32m

object Bech32m

Bech32m encoding/decoding implementation based on BIP-350.

Differences from Bech32 (BIP-173):

  • Uses different checksum constant (0x2bc830a3 vs 0x1)

  • Fixes mutation weakness in original Bech32

  • Used by Midnight blockchain for address encoding

Format: [human-readable part]1[data][checksum]

Example Midnight address:

mn_addr_preview1qe8qj25qkva7ug6qf3rvl3y0a366ydt2nvq30rwk5ckznavfdansq8yfx3u
└─┬─┘ └──┬──┘ └──────────────────────┬────────────────────────┘
│ │ │
│ │ └── Data + Checksum (Bech32m encoded)
│ └────────────────────────────── Network ID
└───────────────────────────────────── Prefix

Reference:

  • BIP-350: https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki

  • @scure/base implementation: https://github.com/paulmillr/scure-base

  • Midnight SDK: @midnight-ntwrk/wallet-sdk-address-format

Compatibility: 100% compatible with Midnight SDK's Bech32m implementation

Functions

Link copied to clipboard
fun decode(bech32String: String): Pair<String, ByteArray>

Decodes a Bech32m string.

Link copied to clipboard
fun encode(hrp: String, data: ByteArray): String

Encodes data to Bech32m format.