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

b58demo.gno

// Package b58demo is a small gnoweb demo of the Base58 codec provided by the
// [p/moul/x/daily/b58](/p/moul/x/daily/b58/v0) library: it renders the alphabet,
// a gallery of worked examples, and an interactive hex → Base58 round-tripper.
//
// It contains no codec logic of its own — everything comes from `b58.Encode`,
// `b58.Decode` and `b58.Alphabet`.
package b58demo

import (
	"encoding/hex"
	"strings"

	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/md/v0"
	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/daily/b58/v0"
)

// Render produces the gnoweb Markdown view.
//
//   - "/"        → the alphabet + a small gallery of worked examples.
//   - "/<hex>"   → decode the hex input to bytes, show its Base58, and the
//     hex-encoded decoded round-trip to prove Encode/Decode are inverse.
func Render(path string) string {
	arg := strings.TrimPrefix(path, "/")
	arg = strings.TrimSpace(arg)

	if arg == "" {
		return renderHome()
	}
	return renderHex(arg)
}

func renderHome() string {
	var sb strings.Builder
	sb.WriteString("# Base58 (Bitcoin alphabet)\n\n")
	sb.WriteString("Demo of the [`p/moul/x/daily/b58`](/p/moul/x/daily/b58/v0) library — ")
	sb.WriteString("base conversion from raw bytes (base-256) to base-58, using the ")
	sb.WriteString("Bitcoin alphabet (no `0`, `O`, `I`, `l`), pure byte-slice math, no `math/big`.\n\n")

	sb.WriteString("## Alphabet\n\n")
	sb.WriteString("```\n")
	sb.WriteString(b58.Alphabet)
	sb.WriteString("\n```\n\n")
	sb.WriteString("58 characters, index 0…57.\n\n")

	sb.WriteString("## Examples\n\n")
	sb.WriteString("| input (utf-8) | hex | Base58 |\n")
	sb.WriteString("|---|---|---|\n")
	examples := []string{"", "hello world", "gno.land", "Bitcoin"}
	for _, e := range examples {
		h := hex.EncodeToString([]byte(e))
		if h == "" {
			h = "(empty)"
		}
		label := e
		if label == "" {
			label = "(empty)"
		}
		sb.WriteString("| `" + label + "` | `" + h + "` | `" + b58.Encode([]byte(e)) + "` |\n")
	}
	sb.WriteString("\n")

	sb.WriteString("Leading zero bytes → leading `1`s:\n\n")
	sb.WriteString("| bytes | Base58 |\n|---|---|\n")
	sb.WriteString("| `[0x00]` | `" + b58.Encode([]byte{0}) + "` |\n")
	sb.WriteString("| `[0x00 0x00 0x00]` | `" + b58.Encode([]byte{0, 0, 0}) + "` |\n\n")

	sb.WriteString("## Try it\n\n")
	sb.WriteString("Append a hex string to the path to encode/round-trip it:\n\n")
	sb.WriteString("- `/68656c6c6f` — the bytes for \"hello\"\n")
	sb.WriteString("- `/00000000` — four zero bytes\n")
	sb.WriteString("- `/deadbeef`\n")
	return sb.String()
}

func renderHex(arg string) string {
	var sb strings.Builder
	sb.WriteString("# Base58 — round-trip\n\n")
	sb.WriteString("[← alphabet & examples](/r/moul/x/daily/b58demo/v0)\n\n")

	raw, err := hex.DecodeString(arg)
	if err != nil {
		sb.WriteString("**Invalid hex input.**\n\n")
		// arg is the visitor's URL path and this branch is the one ANY
		// non-hex path reaches, so the span has to be built by something that
		// sizes its fence past a backtick in the content. Hand-rolled
		// backticks here let a crafted path close the span and render its own
		// markdown on a gno.land page.
		sb.WriteString(md.InlineCode(arg) + " is not a valid hex string. ")
		sb.WriteString("Provide an even number of hex digits, e.g. `/deadbeef`.\n")
		return sb.String()
	}

	encoded := b58.Encode(raw)
	decoded := b58.Decode(encoded)
	roundTrip := hex.EncodeToString(decoded)

	inHex := arg
	if inHex == "" {
		inHex = "(empty)"
	}
	outHex := roundTrip
	if outHex == "" {
		outHex = "(empty)"
	}
	enc := encoded
	if enc == "" {
		enc = "(empty)"
	}

	sb.WriteString("| step | value |\n|---|---|\n")
	//gnovet:ignore render-path-in-code-span inHex is the output of a successful hex.DecodeString, so [0-9a-fA-F] only
	sb.WriteString("| hex input | `" + inHex + "` |\n")
	sb.WriteString("| bytes | `" + byteList(raw) + "` |\n")
	sb.WriteString("| **Base58** | `" + enc + "` |\n")
	sb.WriteString("| decoded (hex) | `" + outHex + "` |\n\n")

	if roundTrip == arg {
		sb.WriteString("✓ Round-trip matches: `Decode(Encode(x)) == x`.\n")
	} else {
		sb.WriteString("✗ Round-trip mismatch.\n")
	}
	return sb.String()
}

// byteList renders a byte slice as a compact space-separated hex list.
func byteList(b []byte) string {
	if len(b) == 0 {
		return "(none)"
	}
	const digits = "0123456789abcdef"
	out := make([]byte, 0, len(b)*3)
	for i, c := range b {
		if i > 0 {
			out = append(out, ' ')
		}
		out = append(out, digits[c>>4], digits[c&0x0f])
	}
	return string(out)
}

gnomod.toml

module = "gno.land/r/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/daily/b58demo/v0"
gno = "0.9"

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

render_example_test.gno

package b58demo

import (
	"strings"
	"testing"

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

// ExampleRender pins the realm's Render output as a gno testable example.
func ExampleRender() {
	print(Render(""))
	// Output:
	// # Base58 (Bitcoin alphabet)
	//
	// Demo of the [`p/moul/x/daily/b58`](/p/moul/x/daily/b58/v0) library — base conversion from raw bytes (base-256) to base-58, using the Bitcoin alphabet (no `0`, `O`, `I`, `l`), pure byte-slice math, no `math/big`.
	//
	// ## Alphabet
	//
	// ```
	// 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
	// ```
	//
	// 58 characters, index 0…57.
	//
	// ## Examples
	//
	// | input (utf-8) | hex | Base58 |
	// |---|---|---|
	// | `(empty)` | `(empty)` | `` |
	// | `hello world` | `68656c6c6f20776f726c64` | `StV1DL6CwTryKyV` |
	// | `gno.land` | `676e6f2e6c616e64` | `JJR1f4BQc9H` |
	// | `Bitcoin` | `426974636f696e` | `3WyEDWjcVB` |
	//
	// Leading zero bytes → leading `1`s:
	//
	// | bytes | Base58 |
	// |---|---|
	// | `[0x00]` | `1` |
	// | `[0x00 0x00 0x00]` | `111` |
	//
	// ## Try it
	//
	// Append a hex string to the path to encode/round-trip it:
	//
	// - `/68656c6c6f` — the bytes for "hello"
	// - `/00000000` — four zero bytes
	// - `/deadbeef`
}

// The invalid-hex branch puts the visitor's raw URL path into a code span, and
// ANY non-hex path reaches it. Building that span by hand let a backtick in the
// path close it early, so the rest of the path rendered as markdown on a
// gno.land page: a crafted link produced attacker-authored headings and links.
// Measured through goldmark v1.8.2 with gnoweb's extension set.
func TestInvalidHexPathCannotEscapeItsCodeSpan(t *testing.T) {
	out := Render("/zz")
	uassert.True(t, strings.Contains(out, "`zz`"),
		"a plain invalid path is not shown verbatim in a code span")

	// One backtick in the path must widen the fence, not close the span.
	out = Render("/a`b")
	uassert.True(t, strings.Contains(out, "``a`b``"),
		"the fence does not outscan the caller's backtick, so the span closes early")

	// The markdown that follows a closing backtick must stay inert.
	out = Render("/x` and [CLICK](https://evil.example) `")
	uassert.False(t, strings.Contains(out, "] and [CLICK]"),
		"caller markdown escaped the code span")
	uassert.True(t, strings.Contains(out, "is not a valid hex string"),
		"the realm's own sentence did not survive")
}