Playground
blog.gno
// Package blog is Jae Kwon's personal blog on gno.land, built on
// gno.land/p/gnoland/blog/v0, the package behind gno.land's own blog.
//
// Who can do what:
// - The owner (Jae's Ledger key) decides who may write, and can hand the
// blog to a new owner in two steps, so a typo can't lose it.
// - Writers and the owner add, edit and remove posts.
// - Everyone else can only read.
//
// What this realm adds on top of the package:
// - Render answers only the blog's own routes, with slugs and tags limited
// to a-z, 0-9, '-' and '_', so nothing typed into a URL is echoed into
// the page as markdown (the package's tag page echoes it).
// - Edit removes the old post and adds it back, so the home page and the
// title index never keep a stale copy (the package's in-place edit does).
// - No comments, and nothing hands out a pointer to the blog or a post.
package blog
import (
"strings"
"time"
gnoblog "gno.land/p/gnoland/blog/v0"
"gno.land/p/nt/avl/v0"
)
var (
// owner is Jae's cold (Ledger) key.
owner = address("g1ecsuj0q572jr0dhu29q9njtnmw03hyu7tyyvv6")
// pendingOwner, when set, may accept ownership.
pendingOwner address
// writers may add, edit and remove posts: address string -> true.
writers avl.Tree
b = &gnoblog.Blog{
Title: "Jae Kwon",
Prefix: "/r/nym-nobody777/blog:",
}
)
func init() {
// The hot key that deploys this realm and holds the nym-nobody777 name.
writers.Set("g1e7ct57zwke7z4l96gl25w5l0f34ljnn37gtp3w", true)
}
// Post publishes a new post. pubDate is RFC3339, or empty for the block time.
// authors and tags are comma-separated.
func Post(cur realm, slug, title, body, pubDate, authors, tags string) {
caller := mustBeWriter(cur)
mustSlug(slug)
tagList := mustTags(tags)
checkErr(b.NewPost(caller, slug, title, body, pubDate, splitList(authors), tagList))
}
// Edit replaces the post at slug. An empty pubDate keeps the old date.
func Edit(cur realm, slug, title, body, pubDate, authors, tags string) {
caller := mustBeWriter(cur)
tagList := mustTags(tags)
old := b.GetPost(slug)
if old == nil {
panic(gnoblog.ErrNoSuchPost)
}
if pubDate == "" {
pubDate = old.CreatedAt.Format(time.RFC3339)
}
// Remove, then add back: if the new post is refused, the whole call
// fails and the old post stays.
b.RemovePost(slug)
checkErr(b.NewPost(caller, slug, title, body, pubDate, splitList(authors), tagList))
}
// Remove deletes the post at slug.
func Remove(cur realm, slug string) {
mustBeWriter(cur)
if b.GetPost(slug) == nil {
panic(gnoblog.ErrNoSuchPost)
}
b.RemovePost(slug)
}
// AddWriter lets addr add, edit and remove posts. Owner only.
func AddWriter(cur realm, addr address) {
mustBeOwner(cur)
if !addr.IsValid() {
panic("invalid address")
}
writers.Set(addr.String(), true)
}
// RemoveWriter takes writing rights away from addr. Owner only.
func RemoveWriter(cur realm, addr address) {
mustBeOwner(cur)
if _, removed := writers.Remove(addr.String()); !removed {
panic("not a writer")
}
}
// ProposeOwner names a new owner, who must then call AcceptOwnership.
// Owner only. Proposing the empty address cancels a pending proposal.
func ProposeOwner(cur realm, addr address) {
mustBeOwner(cur)
if addr != "" && !addr.IsValid() {
panic("invalid address")
}
pendingOwner = addr
}
// AcceptOwnership completes a handover started with ProposeOwner.
func AcceptOwnership(cur realm) {
caller := mustUserCall(cur)
if pendingOwner == "" || caller != pendingOwner {
panic("not the proposed owner")
}
owner = caller
pendingOwner = ""
}
// Owner returns the blog's owner.
func Owner() address { return owner }
// PendingOwner returns the proposed next owner, if any.
func PendingOwner() address { return pendingOwner }
// IsWriter reports whether addr may add, edit and remove posts.
func IsWriter(addr address) bool {
return addr == owner || writers.Has(addr.String())
}
// Render shows the blog. Only "", "p/<slug>" and "t/<tag>" are answered;
// anything else gets "404", so nothing from the URL reaches the page.
func Render(path string) string {
if !isRoute(path) {
return "404"
}
return b.Render(path)
}
func isRoute(path string) bool {
if path == "" {
return true
}
if strings.HasPrefix(path, "p/") || strings.HasPrefix(path, "t/") {
return isSlug(path[2:])
}
return false
}
// isSlug reports whether s is 1 to 100 characters of a-z, 0-9, '-' or '_'.
func isSlug(s string) bool {
if len(s) == 0 || len(s) > 100 {
return false
}
for i := 0; i < len(s); i++ {
c := s[i]
if !(c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '-' || c == '_') {
return false
}
}
return true
}
func mustSlug(s string) {
if !isSlug(s) {
panic("slug must be 1 to 100 characters of a-z, 0-9, - or _")
}
}
func mustTags(tags string) []string {
list := splitList(tags)
for _, t := range list {
if !isSlug(t) {
panic("tags must be 1 to 100 characters of a-z, 0-9, - or _")
}
}
return list
}
// splitList splits a comma-separated list, trimming spaces and dropping
// empty entries.
func splitList(s string) []string {
var out []string
for _, part := range strings.Split(s, ",") {
if part = strings.TrimSpace(part); part != "" {
out = append(out, part)
}
}
return out
}
func mustUserCall(cur realm) address {
if !cur.IsCurrent() {
panic("spoofed realm")
}
prev := cur.Previous()
if !prev.IsUserCall() {
panic("must be called directly by a user")
}
return prev.Address()
}
func mustBeOwner(cur realm) address {
caller := mustUserCall(cur)
if caller != owner {
panic("unauthorized: owner only")
}
return caller
}
func mustBeWriter(cur realm) address {
caller := mustUserCall(cur)
if !IsWriter(caller) {
panic("unauthorized: writers only")
}
return caller
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
blog_test.gno
package blog
import (
"strings"
"testing"
"time"
gnoblog "gno.land/p/gnoland/blog/v0"
"gno.land/p/nt/avl/v0"
"gno.land/p/nt/testutils/v0"
"gno.land/p/nt/uassert/v0"
)
var (
hot = address("g1e7ct57zwke7z4l96gl25w5l0f34ljnn37gtp3w")
cold = address("g1ecsuj0q572jr0dhu29q9njtnmw03hyu7tyyvv6")
bob = testutils.TestAddress("bob")
alice = testutils.TestAddress("alice")
)
const (
d1 = "2026-12-05T00:00:00Z"
d2 = "2026-12-06T00:00:00Z"
)
// reset puts the realm back to its state right after deployment.
func reset() {
owner = cold
pendingOwner = ""
writers = avl.Tree{}
writers.Set(hot.String(), true)
b = &gnoblog.Blog{Title: "Jae Kwon", Prefix: "/r/nym-nobody777/blog:"}
}
func TestRenderOnlyKnownRoutes(cur realm, t *testing.T) {
reset()
for _, path := range []string{
"t/[click](https:evil.com)",
"t/UPPER",
"t/",
"p/a b",
"p/<b>x</b>",
"x/abc",
"?foo",
"t/" + strings.Repeat("a", 101),
} {
uassert.Equal(t, "404", Render(path), path)
}
uassert.True(t, strings.Contains(Render(""), "No posts."))
}
func TestWritersPost(cur realm, t *testing.T) {
reset()
testing.SetOriginCaller(bob)
testing.SetRealm(testing.NewUserRealm(bob))
uassert.AbortsWithMessage(t, cur, "unauthorized: writers only", func() {
Post(cross(cur), "hello", "Hello", "body", d1, "jaekwon", "intro")
})
testing.SetOriginCaller(hot)
testing.SetRealm(testing.NewUserRealm(hot))
uassert.NotAborts(t, cur, func() {
Post(cross(cur), "hello", "Hello", "body", d1, "jaekwon", "intro, ai")
})
testing.SetOriginCaller(cold)
testing.SetRealm(testing.NewUserRealm(cold))
uassert.NotAborts(t, cur, func() {
Post(cross(cur), "second", "Second", "body 2", d2, "", "")
})
page := Render("p/hello")
uassert.True(t, strings.Contains(page, "# Hello"), page)
uassert.True(t, strings.Contains(page, "Written by jaekwon"), page)
uassert.True(t, strings.Contains(page, "Published by "+hot.String()), page)
uassert.True(t, strings.Contains(page, "[#ai](/r/nym-nobody777/blog:t/ai)"), page)
uassert.True(t, strings.Contains(Render("t/intro"), "Hello"))
uassert.True(t, strings.Contains(Render(""), "Second"))
}
func TestPostValidation(cur realm, t *testing.T) {
reset()
testing.SetOriginCaller(hot)
testing.SetRealm(testing.NewUserRealm(hot))
uassert.AbortsWithMessage(t, cur, "slug must be 1 to 100 characters of a-z, 0-9, - or _", func() {
Post(cross(cur), "Bad Slug", "T", "b", d1, "", "")
})
uassert.AbortsWithMessage(t, cur, "tags must be 1 to 100 characters of a-z, 0-9, - or _", func() {
Post(cross(cur), "ok", "T", "b", d1, "", "fine,[evil](x)")
})
uassert.AbortsWithMessage(t, cur, "post title is missing", func() {
Post(cross(cur), "ok", " ", "b", d1, "", "")
})
}
func TestEditLeavesNoStaleCopy(cur realm, t *testing.T) {
reset()
testing.SetOriginCaller(hot)
testing.SetRealm(testing.NewUserRealm(hot))
Post(cross(cur), "hello", "Hello", "body", d1, "", "old")
Edit(cross(cur), "hello", "Hello again", "new body", d2, "", "new")
uassert.Equal(t, 1, b.Posts.Size())
uassert.Equal(t, 1, b.PostsPublished.Size())
uassert.Equal(t, 1, b.PostsAlphabetical.Size())
home := Render("")
uassert.Equal(t, 1, strings.Count(home, "/r/nym-nobody777/blog:p/hello"), home)
uassert.True(t, strings.Contains(home, "Hello again"), home)
uassert.True(t, strings.Contains(Render("t/old"), "No posts."))
uassert.True(t, strings.Contains(Render("t/new"), "Hello again"))
// An empty pubDate keeps the post's date.
Edit(cross(cur), "hello", "Hello again", "newer body", "", "", "")
uassert.Equal(t, d2, b.GetPost("hello").CreatedAt.Format(time.RFC3339))
testing.SetOriginCaller(bob)
testing.SetRealm(testing.NewUserRealm(bob))
uassert.AbortsWithMessage(t, cur, "unauthorized: writers only", func() {
Edit(cross(cur), "hello", "Hijacked", "x", "", "", "")
})
testing.SetOriginCaller(hot)
testing.SetRealm(testing.NewUserRealm(hot))
uassert.AbortsWithMessage(t, cur, "no such post", func() {
Edit(cross(cur), "missing", "T", "b", "", "", "")
})
}
func TestRemove(cur realm, t *testing.T) {
reset()
testing.SetOriginCaller(hot)
testing.SetRealm(testing.NewUserRealm(hot))
Post(cross(cur), "hello", "Hello", "body", d1, "", "")
testing.SetOriginCaller(bob)
testing.SetRealm(testing.NewUserRealm(bob))
uassert.AbortsWithMessage(t, cur, "unauthorized: writers only", func() {
Remove(cross(cur), "hello")
})
testing.SetOriginCaller(hot)
testing.SetRealm(testing.NewUserRealm(hot))
Remove(cross(cur), "hello")
uassert.Equal(t, "404", Render("p/hello"))
uassert.True(t, strings.Contains(Render(""), "No posts."))
uassert.AbortsWithMessage(t, cur, "no such post", func() {
Remove(cross(cur), "hello")
})
}
func TestOwnerManagesWriters(cur realm, t *testing.T) {
reset()
testing.SetOriginCaller(hot)
testing.SetRealm(testing.NewUserRealm(hot))
uassert.AbortsWithMessage(t, cur, "unauthorized: owner only", func() {
AddWriter(cross(cur), bob)
})
testing.SetOriginCaller(cold)
testing.SetRealm(testing.NewUserRealm(cold))
AddWriter(cross(cur), bob)
uassert.True(t, IsWriter(bob))
testing.SetOriginCaller(bob)
testing.SetRealm(testing.NewUserRealm(bob))
uassert.NotAborts(t, cur, func() {
Post(cross(cur), "bobs", "Bob's", "b", d1, "", "")
})
testing.SetOriginCaller(cold)
testing.SetRealm(testing.NewUserRealm(cold))
RemoveWriter(cross(cur), bob)
RemoveWriter(cross(cur), hot)
uassert.False(t, IsWriter(bob))
uassert.False(t, IsWriter(hot))
uassert.True(t, IsWriter(cold))
uassert.AbortsWithMessage(t, cur, "not a writer", func() {
RemoveWriter(cross(cur), bob)
})
testing.SetOriginCaller(hot)
testing.SetRealm(testing.NewUserRealm(hot))
uassert.AbortsWithMessage(t, cur, "unauthorized: writers only", func() {
Post(cross(cur), "x", "X", "b", d2, "", "")
})
}
func TestOwnershipHandover(cur realm, t *testing.T) {
reset()
testing.SetOriginCaller(cold)
testing.SetRealm(testing.NewUserRealm(cold))
ProposeOwner(cross(cur), alice)
uassert.Equal(t, alice, PendingOwner())
testing.SetOriginCaller(bob)
testing.SetRealm(testing.NewUserRealm(bob))
uassert.AbortsWithMessage(t, cur, "not the proposed owner", func() {
AcceptOwnership(cross(cur))
})
testing.SetOriginCaller(alice)
testing.SetRealm(testing.NewUserRealm(alice))
AcceptOwnership(cross(cur))
uassert.Equal(t, alice, Owner())
uassert.Equal(t, address(""), PendingOwner())
testing.SetOriginCaller(cold)
testing.SetRealm(testing.NewUserRealm(cold))
uassert.AbortsWithMessage(t, cur, "unauthorized: owner only", func() {
AddWriter(cross(cur), bob)
})
testing.SetOriginCaller(alice)
testing.SetRealm(testing.NewUserRealm(alice))
uassert.AbortsWithMessage(t, cur, "not the proposed owner", func() {
AcceptOwnership(cross(cur))
})
}
func TestCodeRealmCannotWrite(cur realm, t *testing.T) {
reset()
testing.SetOriginCaller(hot)
testing.SetRealm(testing.NewCodeRealm("gno.land/r/someone/proxy"))
uassert.AbortsWithMessage(t, cur, "must be called directly by a user", func() {
Post(cross(cur), "hello", "Hello", "body", d1, "", "")
})
}
gnomod.toml
module = "gno.land/r/nym-nobody777/blog" gno = "0.9" [addpkg] creator = "g1e7ct57zwke7z4l96gl25w5l0f34ljnn37gtp3w" height = 2054106 max_deposit = "5000000ugnot"