Skip to content

Conversation

@thomasxm
Copy link

Summary

Adds Bech32 and Bech32m encoding/decoding operations as specified in BIP-0173 and BIP-0350. These encodings are widely used for Bitcoin SegWit/Taproot addresses, Lightning Network invoices, Nostr keys, and other cryptocurrency applications.

New Operations

  • To Bech32 - Encodes data to Bech32 or Bech32m format with configurable HRP
  • From Bech32 - Decodes Bech32/Bech32m strings with auto-detection support

Features

  • Configurable Human-Readable Part (HRP) for different use cases
  • Support for both Bech32 (BIP-0173) and Bech32m (BIP-0350)
  • Auto-detect mode tries Bech32 first, then Bech32m
  • Bitcoin SegWit mode for proper witness version handling
  • Multiple output formats: Hex, Raw, Bitcoin scriptPubKey, HRP: Hex, JSON
  • Magic auto-detection patterns for Bitcoin/Litecoin/AGE addresses
  • Comprehensive error messages for invalid input

Use Cases

  • Bitcoin SegWit addresses (bc1q...)
  • Bitcoin Taproot addresses (bc1p...)
  • Lightning Network payment requests
  • Nostr public/private keys (npub/nsec)
  • Litecoin addresses (ltc1...)
  • AGE encryption keys

Technical Details

  • Pure JavaScript implementation (no external dependencies)
  • Algorithm verified against official BIP reference implementations
  • Maximum output length enforced at 90 characters per specification
  • Validates HRP characters (ASCII 33-126)
  • Mixed case detection and rejection per BIP specification
  • Proper SegWit witness version handling (v0-v16)
  • Correct padding validation per BIP-0173 convertbits specification

Files Changed

  • src/core/lib/Bech32.mjs - NEW - Core Bech32/Bech32m library implementation
  • src/core/operations/ToBech32.mjs - NEW - Encode operation
  • src/core/operations/FromBech32.mjs - NEW - Decode operation
  • src/core/config/Categories.json - Added operations to Data format category
  • tests/operations/tests/Bech32.mjs - NEW - Test suite (60 test cases)
  • tests/operations/index.mjs - Registered test module

Testing

  • All 1776 tests pass
  • BIP-0173 and BIP-0350 official test vectors included
  • Bitcoin mainnet/testnet address round-trip tests
  • SegWit witness program validation tests
  • scriptPubKey output format verification
  • Error handling tests for invalid inputs
  • Verified against real-world Bitcoin addresses

Test Vectors

Includes official test vectors from:

  • BIP-0173 (Bech32 for SegWit v0)
  • BIP-0350 (Bech32m for SegWit v1+)

Test 1: Bitcoin Mainnet SegWit v0 (P2WPKH)

  • Input: BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4
  • Output (Bitcoin scriptPubKey): 0014751e76e8199196d454941c45d1b3a323f1433bd6

Test 2: Bitcoin Testnet SegWit v0 (P2WSH)

  • Input: tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7
  • Output (Bitcoin scriptPubKey): 00201863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262

Test 3: Witness v1 (64-byte program)

  • Input: bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7k7grplx
  • Output (Bitcoin scriptPubKey): 5128751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd6

https://slowli.github.io/bech32-buffer/

References


Screenshot 2026-01-12 at 22 58 31

@thomasxm thomasxm closed this Jan 12, 2026
@thomasxm thomasxm reopened this Jan 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant