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/nym-moultest123/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/nym-moultest123/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/nym-moultest123/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/nym-moultest123/x/daily/b58demo/v0)\n\n")

	raw, err := hex.DecodeString(arg)
	if err != nil {
		sb.WriteString("**Invalid hex input.**\n\n")
		sb.WriteString("`" + 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")
	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/nym-moultest123/x/daily/b58demo/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186410
  max_deposit = "5000000ugnot"

render_example_test.gno

package b58demo

// 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/nym-moultest123/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`
}