bech32m

package module
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 24, 2026 License: MIT Imports: 5 Imported by: 0

README

GoDoc Coverage Status Build Status

bech32m

A Go package for encoding and decoding Bech32, Bech32m, and CashAddr addresses.

Features

  • Bech32 encoding/decoding (BIP-173, segwit v0)
  • Bech32m encoding/decoding (BIP-350, segwit v1+)
  • CashAddr encoding/decoding (Bitcoin Cash)
  • Segwit address encode/decode helpers
  • Zero-allocation polymod checksum using lookup tables

Install

go get github.com/KarpelesLab/bech32m

Usage

Segwit addresses
// Decode a segwit address
witver, witprog, err := bech32m.SegwitAddrDecode("bc", "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4")

// Encode a segwit address
addr, err := bech32m.SegwitAddrEncode("bc", witver, witprog)
CashAddr addresses
// Decode a CashAddr address
vers, payload, err := bech32m.CashAddrDecode("bitcoincash:", "bitcoincash:qr6m7j9njldwwzlg9v7v53unlr4jkmx6eylep8ekg2")

// Encode a CashAddr address
addr, err := bech32m.CashAddrEncode("bitcoincash:", vers, payload)
Low-level Bech32/Bech32m
// Encode raw data with Bech32m
encoded := bech32m.Encode("hrp", data, bech32m.Bech32m)

// Decode any Bech32/Bech32m/CashAddr string
hrp, data, spec, err := bech32m.Decode(encoded)

Documentation

Overview

Package bech32m implements the Bech32 and Bech32m encoding formats used for segregated witness (segwit) addresses as defined in BIP-0173 and BIP-0350, as well as the CashAddr format used by Bitcoin Cash.

Index

Constants

View Source
const (
	Bech32   = 1  // Bech32 encoding (BIP-0173, used for segwit v0)
	Bech32m  = 2  // Bech32m encoding (BIP-0350, used for segwit v1+)
	CashAddr = 3  // CashAddr encoding (Bitcoin Cash)
	Failed   = -1 // Indicates checksum verification failure

)

Encoding spec constants returned by Decode to indicate which encoding was used.

Variables

View Source
var (
	ErrMaxLengthExceeded = errors.New("bech32m: overall max length exceeded")
	ErrMixedCase         = errors.New("bech32m: mixed case found in address")
	ErrInvalidChecksum   = errors.New("bech32m: invalid checksum")
	ErrCorruptInput      = errors.New("bech32m: corrupt base32 data")
)

Sentinel errors returned during decoding.

Functions

func CashAddrDecode

func CashAddrDecode(hrp, addr string) (byte, []byte, error)

CashAddrDecode decodes a CashAddr-encoded address, returning the version byte, payload data, and any error. The hrp parameter should include the trailing colon (e.g. "bitcoincash:").

func CashAddrEncode

func CashAddrEncode(hrp string, vers byte, buf []byte) (string, error)

CashAddrEncode encodes a CashAddr address from the given hrp (including trailing colon), version byte, and hash payload. The payload length must be one of the standard CashAddr sizes (20, 24, 28, 32, 40, 48, 56, or 64 bytes).

func Decode

func Decode(bechString string) (string, []byte, int, error)

Decode validates and decodes a Bech32, Bech32m, or CashAddr string, returning the human-readable part, data bytes, and encoding spec (Bech32, Bech32m, or CashAddr).

func Encode

func Encode(hrp string, data []byte, spec int) string

Encode computes a Bech32, Bech32m, or CashAddr encoded string given HRP, data values, and the encoding spec. The data slice is not modified.

func SegwitAddrDecode

func SegwitAddrDecode(hrp, addr string) (byte, []byte, error)

SegwitAddrDecode decode a segwit address.

func SegwitAddrEncode

func SegwitAddrEncode(hrp string, witver byte, witprog []byte) (string, error)

SegwitAddrEncode encode a segwit address.

func SegwitAddrEncodeNoCheck

func SegwitAddrEncodeNoCheck(hrp string, witver byte, witprog []byte) (string, error)

SegwitAddrEncodeNoCheck is the same as SegwitAddrEncode but skips the round-trip decode validation of the generated address.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL