Encodings

Bech32

BIP173 bech32 with a prefix and 5-bit words and a BCH checksum. SegWit v0 and Lightning and Nostr
IDbech3217 / 20bech32 · 32 characters
Encoding / Bech32

Bech32

A BCH checksum that catches four typos.

Overhead
+62% on random bytes
Checksum
checked on decode
Standard
BIP173
Used by
SegWit v0, Lightning, Nostr, Cosmos
family 2 / 20

Sample

Out
hi1dpjkcmr0ypmk7unvvsssgdwk9f
Back
"hello world!" · prefix hi

Options

prefixstring
required
Human-readable part, such as bc, tb or npub
limitnumber
default 90
Longest result allowed; BIP173 sets 90, Lightning invoices need more

Access

Importimport { bech32 } from "@agntn/encodings/bech32"
CLIencodings encode bech32 'hello world!' --prefix hi
Tryplayground with the sample above
Kinbech32m

A human-readable prefix, a 1, then 5-bit words and six characters of checksum. The checksum is a BCH code that catches any four wrong characters. Base58 never came close.

ts
encode("bech32", "gm", { prefix: "test" });  // "test1vaks69jerm"
decode("bech32", "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4");
// bytes: the 20-byte program, details: { prefix: "bc", witnessVersion: 0 }

A valid segwit v0 address comes back as its witness program. Anything else comes back as the bytes its words carry. A Nostr npub, say, or a Cosmos account. Mixed case is an error. Over 90 characters too, unless you raise limit. Lightning invoice? Raise it.

Checksums has the long version, segwit rules included.