bech32

package
v0.783.0 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2026 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package bech32 decodes a Bech32 / Bech32m string — the encoding modern Bitcoin uses for SegWit addresses (bc1…/tb1…), and which Nostr (npub/nsec/note), Lightning (lnbc…), and Cosmos-family chains also use — into its human-readable prefix (HRP), data payload, and checksum variant, and interprets SegWit addresses (witness version + program + type). It is the Bech32 companion to base58check_decode, closing out Bitcoin-address coverage for crypto-forensics / IR / pentest loot. Pure offline transform; no network or device.

Wrap-vs-native judgement

Native. Bech32 is a public spec (BIP-173 / BIP-350): a base-32 charset, a BCH checksum over the polynomial defined there (constant 1 for Bech32, 0x2bc830a3 for Bech32m), and a 5-bit↔8-bit regrouping — pure integer maths, stdlib only, nothing to wrap.

What this covers / defers

  • General Bech32 and Bech32m decode: HRP, the 5→8-bit data payload, and the checksum variant — works for any HRP (Bitcoin, Nostr, Lightning, Cosmos…).
  • SegWit address interpretation for the bc/tb/bcrt HRPs: witness version, witness program, and the address type (P2WPKH / P2WSH / P2TR), with the BIP-173/350 variant rule enforced (v0 ⇒ Bech32, v1+ ⇒ Bech32m).
  • Nostr (npub/nsec/note) and Lightning (lnbc…) HRPs are labelled; their inner TLV / invoice structure is left to the caller (a separate surface).
  • Base58Check (legacy 1…/3… addresses, WIF) is the other encoding — see base58check_decode.

Verifiable / no confidently-wrong output

Anchored to the BIP-173 / BIP-350 test vectors — A12UEL5L (Bech32, HRP "a"), A1LQFN3A (Bech32m, HRP "a"), and the P2WPKH address bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4 → witness v0, program 751e76e8199196d454941c45d1b3a323f1433bd6. A string whose checksum does not validate, or a SegWit address whose variant does not match its witness version, is reported as such rather than asserted valid; mixed-case input, a missing separator, or an out-of-charset character is rejected.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Result

type Result struct {
	Input         string `json:"input"`
	HRP           string `json:"hrp"`
	Variant       string `json:"variant"` // "bech32" / "bech32m" / "invalid"
	ChecksumValid bool   `json:"checksum_valid"`
	// DataHex is the data payload regrouped from 5-bit to 8-bit (when it is
	// byte-aligned). Empty for an empty or non-byte-aligned payload.
	DataHex string `json:"data_hex,omitempty"`
	// Type labels a recognised artifact (SegWit address class, Nostr, Lightning).
	Type string `json:"type,omitempty"`
	// SegWit fields (set when the HRP is a SegWit network).
	WitnessVersion    *int   `json:"witness_version,omitempty"`
	WitnessProgramHex string `json:"witness_program_hex,omitempty"`
	Note              string `json:"note,omitempty"`
}

Result is the decoded view of a Bech32/Bech32m string.

func Decode

func Decode(input string) (*Result, error)

Decode parses and checksums a Bech32/Bech32m string and interprets SegWit addresses. A structural error (mixed case, no separator, bad charset, bad length) is returned as an error; a bad checksum or a SegWit-rule violation is reported in the Result.

Jump to

Keyboard shortcuts

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