Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

bech32demo.gno

// Package bech32demo is a small gnoweb demo of the Bech32 and Bech32m codec
// provided by the [p/moul/x/daily/bech32](/p/moul/x/daily/bech32/v0) library:
// it takes a real gno.land address apart, shows the BIP-173 and BIP-350 test
// vectors side by side, and reproduces the length-extension weakness that is
// the whole reason the second variant exists.
//
// It contains no codec logic of its own. Stateless, so Render is deterministic.
package bech32demo

import (
	"strconv"
	"strings"

	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/md/v0"
	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/kit/ui/v0"
	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/daily/bech32/v0"
	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/daily/hexdump/v0"
)

// sample is a real mainnet address: human-readable part "g", 20 payload bytes.
const sample = "g1manfred47kzduec920z88wfr64ylksmdcedlf5"

// The BIP-173 (Bech32) and BIP-350 (Bech32m) test vectors, paired by shape so
// the only difference on each row is the six-character checksum.
var vectors = [][2]string{
	{"a12uel5l", "a1lqfn3a"},
	{"?1ezyfcl", "?1v759aa"},
	{
		"split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w",
		"split1checkupstagehandshakeupstreamerranterredcaperredlc445v",
	},
}

// The insertion that Bech32 accepts and Bech32m refuses. Both strings below
// carry a checksum Bech32 verifies; only the second has the extra "q".
const (
	extBase     = "a1q3g6mnsp"
	extInserted = "a1q3g6mnsqp"
	extMBase    = "a1q6dp699p"
	extMInsert  = "a1q6dp699qp"
)

// Render renders the demo for gnoweb.
//
//	Render("")        / Render("/")  -> the full demo
//	Render("/<string>")              -> decode that string
func Render(path string) string {
	var b strings.Builder
	b.WriteString("# Bech32\n\n")
	b.WriteString("Addresses that carry their own error-detecting checksum, demoing the ")
	b.WriteString("[`p/moul/x/daily/bech32`](/p/moul/x/daily/bech32/v0) library.\n\n")

	if in := parseArg(path); in != "" {
		b.WriteString(decodeOne(in))
		return b.String()
	}

	b.WriteString("## A gno.land address, taken apart\n\n")
	b.WriteString(decodeOne(sample))

	b.WriteString("\n## The two variants\n\n")
	b.WriteString("Same string shape, same data, different checksum constant: ")
	b.WriteString("`1` for Bech32, `0x2bc830a3` for Bech32m.\n\n")
	vt := ui.NewTable("Bech32 (BIP-173)", "Bech32m (BIP-350)")
	for _, pair := range vectors {
		vt.Row(md.InlineCode(pair[0]), md.InlineCode(pair[1]))
	}
	b.WriteString(vt.String() + "\n")

	b.WriteString("## Why Bech32m exists\n\n")
	b.WriteString("A Bech32 string ending in `p` keeps a valid checksum when `q` ")
	b.WriteString("characters are inserted in front of that final `p`, so a reader ")
	b.WriteString("accepts a string nobody wrote. Bech32m's constant closes it:\n\n")
	wt := ui.NewTable("string", "variant", "verdict")
	for _, s := range []string{extBase, extInserted, extMBase, extMInsert} {
		_, _, v, err := bech32.Decode(s)
		if err != nil {
			wt.Row(md.InlineCode(s), "-", "rejected: "+err.Error())
			continue
		}
		wt.Row(md.InlineCode(s), v.String(), "accepted")
	}
	b.WriteString(wt.String() + "\n")

	b.WriteString("Pass a string of your own to decode it: ")
	b.WriteString("`Render(\"/<bech32 string>\")`.\n")
	return b.String()
}

// decodeOne renders one string's decode. Everything derived from the input is
// escaped: the argument reaches this realm from whoever typed the url.
func decodeOne(in string) string {
	var b strings.Builder
	b.WriteString("Decoding " + ui.Inline(in) + ":\n\n")

	hrp, payload, variant, err := bech32.DecodeBytes(in)
	if err != nil {
		b.WriteString("> " + ui.Inline(err.Error()) + "\n")
		return b.String()
	}

	t := ui.NewTable("field", "value")
	// hrp bytes run the whole printable-ASCII range, so this cell is escaped,
	// and escaped text can never sit inside a code span.
	t.Row("human-readable part", ui.Cell(hrp))
	t.Row("variant", variant.String())
	t.Row("payload", strconv.Itoa(len(payload))+" bytes")
	t.Row("payload (hex)", md.InlineCode(hexOf(payload)))
	b.WriteString(t.String())
	return b.String()
}

// hexOf renders a payload through the hexdump library, which owns the digits.
func hexOf(payload []byte) string {
	var b strings.Builder
	for _, c := range payload {
		b.WriteString(hexdump.Hex(c))
	}
	return b.String()
}

// parseArg strips the one routing slash in front of the argument and returns
// the rest verbatim. Nothing else is removed: a slash is legal in a
// human-readable part, so cutting at one refuses a valid string, and trimming
// whitespace would render a clean decode of a string nobody typed.
func parseArg(path string) string {
	return strings.TrimPrefix(path, "/")
}

bech32demo_test.gno

package bech32demo

import (
	"strings"
	"testing"

	"gno.land/p/nt/uassert/v0"
)

// The argument path decodes a string of the caller's choosing.
func TestRenderArgument(t *testing.T) {
	got := Render("/a1lqfn3a")
	uassert.True(t, strings.Contains(got, "| variant | bech32m |"), got)
	uassert.True(t, strings.Contains(got, "| payload | 0 bytes |"))
	// The full demo's sections are not rendered for an argument.
	uassert.False(t, strings.Contains(got, "## Why Bech32m exists"))
}

// A string that does not decode says why, instead of rendering half a table.
func TestRenderRejects(t *testing.T) {
	got := Render("/g1manfred47kzduec920z88wfr64ylksmdcedlf4")
	uassert.True(t, strings.Contains(got, "bech32: bad checksum"), got)
	uassert.False(t, strings.Contains(got, "| field | value |"))
}

// A caller controls the whole argument, so anything echoed back is escaped.
// Without it, "|" opens a column and "[x](y)" renders as a link.
func TestRenderEscapesArgument(t *testing.T) {
	got := Render("/*boom*")
	uassert.False(t, strings.Contains(got, "Decoding *boom*"))
	uassert.True(t, strings.Contains(got, "Decoding \\*boom\\*"), got)
}

// A slash is legal in a human-readable part, so the argument is taken whole
// rather than cut at the first one.
func TestRenderSlashInHRP(t *testing.T) {
	got := Render("/a/b1mqd7sj")
	uassert.True(t, strings.Contains(got, "human-readable part | a/b |"), got)
}

// Nothing is trimmed. A trailing non-breaking space is part of the string the
// caller submitted, and the library refuses it: trimming it first would render
// a clean decode of a string nobody typed.
func TestRenderDoesNotTrim(t *testing.T) {
	got := Render("/g1manfred47kzduec920z88wfr64ylksmdcedlf5 ")
	uassert.True(t, strings.Contains(got, "bech32: invalid data character"), got)
	uassert.False(t, strings.Contains(got, "20 bytes"))
}

gnomod.toml

module = "gno.land/r/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/daily/bech32demo/v0"
gno = "0.9"
private = true

[addpkg]
  creator = "g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv"
  height = 755778
  max_deposit = "5000000ugnot"

render_example_test.gno

package bech32demo

// ExampleRender pins the realm's Render output as a testable example. The
// vectors are BIP-173's and BIP-350's, so this doubles as a conformance test,
// and the last table is the length-extension weakness reproduced in full.
func ExampleRender() {
	print(Render(""))
	// Output:
	// # Bech32
	//
	// Addresses that carry their own error-detecting checksum, demoing the [`p/moul/x/daily/bech32`](/p/moul/x/daily/bech32/v0) library.
	//
	// ## A gno.land address, taken apart
	//
	// Decoding g1manfred47kzduec920z88wfr64ylksmdcedlf5:
	//
	// | field | value |
	// | --- | --- |
	// | human-readable part | g |
	// | variant | bech32 |
	// | payload | 20 bytes |
	// | payload (hex) | `df6691e5b5f584de670553c473b923d549fb436d` |
	//
	// ## The two variants
	//
	// Same string shape, same data, different checksum constant: `1` for Bech32, `0x2bc830a3` for Bech32m.
	//
	// | Bech32 (BIP-173) | Bech32m (BIP-350) |
	// | --- | --- |
	// | `a12uel5l` | `a1lqfn3a` |
	// | `?1ezyfcl` | `?1v759aa` |
	// | `split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w` | `split1checkupstagehandshakeupstreamerranterredcaperredlc445v` |
	//
	// ## Why Bech32m exists
	//
	// A Bech32 string ending in `p` keeps a valid checksum when `q` characters are inserted in front of that final `p`, so a reader accepts a string nobody wrote. Bech32m's constant closes it:
	//
	// | string | variant | verdict |
	// | --- | --- | --- |
	// | `a1q3g6mnsp` | bech32 | accepted |
	// | `a1q3g6mnsqp` | bech32 | accepted |
	// | `a1q6dp699p` | bech32m | accepted |
	// | `a1q6dp699qp` | - | rejected: bech32: bad checksum |
	//
	// Pass a string of your own to decode it: `Render("/<bech32 string>")`.
}