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.

gallery.gno

12.91 Kb · 349 lines
  1// Package gallery is the platform's gnoweb face (design §2.5.7): the
  2// product stays browsable on gno.land even without the website. It is
  3// private (re-deployable in place), read-only and holds nothing.
  4//
  5// Routes:
  6//
  7//	""                    latest collections
  8//	c/<cid>[?page=N]      one collection, 20 tokens per page
  9//	c/<cid>/t/<tid>       one token: art, traits, owner, listing, provenance
 10//	e/<engine pkgpath>    an engine's manifest and status
 11//	u/<name>              a registered user's collections and listings
 12//
 13// Every user-supplied string is rendered as plain text (plainText: RA M-9,
 14// SA-M4): no heading, link, image, HTML or autolink can come out of a name,
 15// description, trait or manifest field. Every list is paginated. The only
 16// images are core's checked data URIs (art, card, placeholder, or a media
 17// engine's checked file), never a remote address.
 18//
 19// sinopia4 (M-30): art, traits and media come from core only, so the
 20// gallery imports no engine, upload or asset realm and ships in the first
 21// package wave. Upload-only views (drop table, storage badge) are on the
 22// website, which reads the upload adapter.
 23package gallery
 24
 25import (
 26	"chain/runtime"
 27	"strconv"
 28	"strings"
 29
 30	"gno.land/p/nt/markdown/sanitize/v0"
 31	"gno.land/p/nt/ufmt/v0"
 32	"gno.land/r/sys/users"
 33
 34	"gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/engine/v2"
 35	"gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/types/v2"
 36	"gno.land/r/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/core"
 37	market "gno.land/r/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/market/v1"
 38)
 39
 40const (
 41	pageSize   = 20
 42	homeLimit  = 20
 43	maxPageNum = 500
 44)
 45
 46func Render(path string) string {
 47	route, query, _ := strings.Cut(path, "?")
 48	parts := strings.Split(route, "/")
 49	switch {
 50	case route == "":
 51		return home()
 52	case len(parts) == 2 && parts[0] == "c":
 53		return collectionPage(parts[1], pageFrom(query))
 54	case len(parts) == 4 && parts[0] == "c" && parts[2] == "t":
 55		return tokenPage(parts[1], parts[3])
 56	case len(parts) > 1 && parts[0] == "e":
 57		return enginePage(strings.TrimPrefix(route, "e/"))
 58	case len(parts) == 2 && parts[0] == "u":
 59		return userPage(parts[1])
 60	}
 61	return "# Not found\n\n[Back to the gallery](" + base() + ")\n"
 62}
 63
 64func home() string {
 65	n := core.CollectionCount()
 66	out := ufmt.Sprintf("# Gallery\n\n%d collections. Art is computed on chain from each collection's engine and revealed seed.\n\n", n)
 67	for id := n; id >= 1 && id > n-homeLimit; id-- {
 68		info := core.Info(itoa(id))
 69		out += ufmt.Sprintf("- [%s](%s) — %s, %d/%d minted\n",
 70			plainText(info.Name), link("c/"+info.ID), info.State.String(), info.Minted, info.MaxSupply)
 71	}
 72	return out
 73}
 74
 75func collectionPage(cid string, page int64) string {
 76	info := core.Info(cid)
 77	cid = info.ID // "c/<cid>" (K-1) renders and links as the bare id
 78	out := "# " + plainText(info.Name) + " (" + plainText(info.Symbol) + ")\n\n"
 79	if info.Description != "" {
 80		out += plainText(info.Description) + "\n\n"
 81	}
 82	out += ufmt.Sprintf("- State: **%s**, %d/%d minted, royalty %d bps\n", info.State.String(), info.Minted, info.MaxSupply, info.RoyaltyBps)
 83	out += "- Creator: " + who(info.Creator) + "\n"
 84	out += "- Engine: [" + plainText(info.EngineID) + "](" + link("e/"+info.EngineID) + ")\n"
 85	if isMedia(info.EngineID) {
 86		out += "- Media collection: each token's file is checked by core (PNG, WebP, JPEG header and size, SVG allowlist, " +
 87			"sha256, IPFS CIDv1) before it is served; anything else shows the engine's card\n"
 88	}
 89	if len(info.TraitTypes) > 0 {
 90		out += "- Trait types: " + plainText(strings.Join(info.TraitTypes, ", ")) + "\n"
 91	}
 92	if info.Mode == types.ModeOpen {
 93		out += "- Open edition: no reveal; the seed of every token is public from the launch\n"
 94	}
 95	if info.Soulbound {
 96		out += "- Non-transferable: tokens stay with the address they were minted to (no transfers, no market); " +
 97			"an owner may burn theirs after the reveal\n"
 98	}
 99	if info.Badge != "" {
100		out += "- Provenance: **" + info.Badge + "** — " + fairness(cid) + "\n"
101	}
102	if f := market.Floor(cid); f > 0 {
103		out += ufmt.Sprintf("- Floor: %d ugnot\n", f)
104	}
105	out += "\n## Tokens\n\n"
106	first := (page-1)*pageSize + 1
107	for n := first; n < first+pageSize && n <= info.Minted; n++ {
108		tid := itoa(n)
109		owner, err := core.OwnerOf(cid, tid)
110		if err != nil {
111			continue
112		}
113		line := "- [#" + tid + "](" + link("c/"+cid+"/t/"+tid) + ") owned by " + who(owner)
114		if _, price, ok := market.ActiveListing(cid, tid); ok {
115			line += ufmt.Sprintf(", listed at %d ugnot", price)
116		}
117		out += line + "\n"
118	}
119	if first+pageSize <= info.Minted {
120		out += "\n[Next page](" + link(ufmt.Sprintf("c/%s?page=%d", cid, page+1)) + ")\n"
121	}
122	return out
123}
124
125func tokenPage(cid, tid string) string {
126	info := core.Info(cid)
127	cid = info.ID
128	owner, err := core.OwnerOf(cid, tid)
129	if err != nil {
130		return "# Token not found\n"
131	}
132	img, fileLine := tokenImage(info, cid, tid)
133	out := "# " + plainText(info.Name) + " #" + tid + "\n\n"
134	out += "![" + plainText(info.Name) + " #" + tid + "](" + sanitize.ImageURL(img) + ")\n\n"
135	out += "- Owner: " + who(owner) + "\n"
136	if first := core.FirstOwner(cid, tid); first != owner {
137		out += "- First owner: " + who(first) + "\n"
138	}
139	out += fileLine
140	if id, price, ok := market.ActiveListing(cid, tid); ok {
141		out += ufmt.Sprintf("- Listed: %d ugnot (listing %s)\n", price, id)
142	}
143	out += "\n## Traits\n\n"
144	for _, t := range core.TokenTraitList(cid, tid) {
145		out += "- " + plainText(t.Type) + ": " + plainText(t.Value) + "\n"
146	}
147	out += "\n## How to verify\n\n"
148	if info.State != types.StateFinal {
149		out += "The collection is not revealed yet; every token shows the same placeholder.\n"
150		return out
151	}
152	if info.Mode == types.ModeOpen {
153		out += "1. Open edition: the salt is public, `Salt(\"" + cid + "\")` = sha256(\"art|open|v1|" + runtime.ChainID() +
154			"|" + cid + "\").\n"
155	} else {
156		out += "1. Read `Provenance(\"" + cid + "\")` on core from another RPC and recompute the commitments and the salt.\n"
157	}
158	out += "2. Seed: `TokenSeed(\"" + cid + "\", \"" + tid + "\")` = sha256(\"" + "art|seed|v1|" + cid + "|" + tid + "|\" + salt).\n"
159	if isMedia(info.EngineID) {
160		out += "3. `TokenMediaInfo(\"" + cid + "\", \"" + tid + "\")` names the file core serves and its sha256; " +
161			"`TokenURI` returns the checked bytes (or the IPFS address), `TokenCard` the card.\n\n"
162	} else {
163		out += "3. Render it yourself: `RenderWith(engine, params, seed)` must return this image.\n\n"
164	}
165	out += "Provenance badge **" + info.Badge + "**, on-chain check: " + strconv.FormatBool(core.VerifyProvenance(cid)) +
166		"; " + fairness(cid) + "\n"
167	return out
168}
169
170// --- media (engine/v2 KindMedia, read through core only: M-30, I-15) ---------
171
172// tokenImage is the image the page embeds and the file line under it. An
173// SVG engine's token is its art (TokenURI). A media engine's token is the
174// checked file when core serves it as a data URI (TokenURI), otherwise the
175// engine's checked SVG card (TokenCard): never a remote image (SA-M4); an
176// IPFS address is escaped text. TokenMediaInfo is read first, so a 256 KiB
177// file is encoded only when it is shown.
178func tokenImage(info types.CollectionInfo, cid, tid string) (string, string) {
179	if !isMedia(info.EngineID) {
180		uri, _ := core.TokenURI(cid, tid)
181		return uri, ""
182	}
183	showFile, line := mediaView(core.TokenMediaInfo(cid, tid))
184	var uri string
185	if showFile {
186		uri, _ = core.TokenURI(cid, tid)
187	} else {
188		uri, _ = core.TokenCard(cid, tid)
189	}
190	return uri, line
191}
192
193// mediaView decides from core.TokenMediaInfo whether the page embeds the
194// file itself (core serves it as a data URI: source "data") or the card,
195// and writes the file line. Every value is escaped as text.
196func mediaView(mi string) (showFile bool, line string) {
197	mime, sha := jsonStr(mi, "mime"), jsonStr(mi, "sha256")
198	switch jsonStr(mi, "source") {
199	case "data":
200		return true, "- File: " + plainText(mime) + ", " + jsonNum(mi, "bytes") + " bytes on chain, sha256 " +
201			sanitize.InlineCode(sha) + "\n"
202	case "ipfs":
203		line = "- IPFS: " + plainText(jsonStr(mi, "uri")) + " (" + plainText(mime)
204		if sha != "" {
205			line += ", sha256 " + sanitize.InlineCode(sha)
206		}
207		return false, line + "); fetch it from any gateway and check the hash. The image here is the on-chain card.\n"
208	case "card":
209		return false, "- File: not served (the engine gave no file core accepts); showing the engine's card\n"
210	}
211	return false, "" // placeholder before Final
212}
213
214// isMedia: the engine serves files (engine/v2 KindMedia).
215func isMedia(engineID string) bool {
216	return core.EngineManifest(engineID).Kind == engine.KindMedia
217}
218
219// jsonStr reads a string field of a flat JSON object written by core's
220// safe.JSONWriter (keys are ours; the values read here — source, mime,
221// sha256, an ipfs:// CIDv1 — never contain an escaped quote).
222func jsonStr(js, key string) string {
223	k := `"` + key + `":"`
224	i := strings.Index(js, k)
225	if i < 0 {
226		return ""
227	}
228	rest := js[i+len(k):]
229	if j := strings.IndexByte(rest, '"'); j >= 0 {
230		return rest[:j]
231	}
232	return ""
233}
234
235// jsonNum reads a non-negative integer field of the same JSON ("0" if absent).
236func jsonNum(js, key string) string {
237	k := `"` + key + `":`
238	i := strings.Index(js, k)
239	if i < 0 {
240		return "0"
241	}
242	j := i + len(k)
243	e := j
244	for e < len(js) && js[e] >= '0' && js[e] <= '9' {
245		e++
246	}
247	if e == j {
248		return "0"
249	}
250	return js[j:e]
251}
252
253// fairness never calls a collection "verified" unless it is a fair launch
254// (core.FairLaunch): a consistent record is not enough when one party could
255// choose the map (SA-M2). An open edition (I-17) is never a fair launch.
256//
257// SEC-F2: the committed beacon's secret is derived by the Sinopia operator,
258// so a fair badge on that path trusts the operator: beacon-only means the
259// operator alone knew the map while minting, and 2/2 assumes the operator
260// did not collude with the creator. Only a randomness provider (a public
261// round nobody knows in advance) needs no such trust.
262func fairness(cid string) string {
263	if core.FairLaunch(cid) {
264		if src := jsonStr(core.Provenance(cid), "beaconSource"); src != "" && src != types.BeaconCommitted {
265			return "fair launch, verified on chain (public randomness: " + src + ")"
266		}
267		if core.Info(cid).Badge == core.BadgeBeaconOnly {
268			return "verified on chain; fair only if you trust the Sinopia beacon operator (the creator did not reveal)"
269		}
270		return "fair launch, verified on chain (creator + Sinopia beacon; trusts the beacon operator not to collude with the creator)"
271	}
272	if core.Info(cid).Mode == types.ModeOpen {
273		if !core.VerifyProvenance(cid) {
274			return "NOT verified: the provenance record does not check out"
275		}
276		return "open edition: no reveal, the seed was public from the launch (not a fair launch)"
277	}
278	if !core.VerifyProvenance(cid) {
279		return "NOT verified: the provenance record does not check out"
280	}
281	return "not a fair launch: one party could choose the map (record consistent)"
282}
283
284// plainText renders user text as inert markdown: sanitize.InlineText escapes
285// every inline-active character (\ * _ [ ] ( ) ~ > - + . ! ` # < &) and folds
286// newlines, and escaping ':' breaks bare-URL autolinks ("https\://…"), which
287// InlineText leaves alone (SA-M4).
288func plainText(s string) string {
289	return strings.ReplaceAll(sanitize.InlineText(s), ":", "\\:")
290}
291
292func enginePage(id string) string {
293	m := core.EngineManifest(id)
294	out := "# Engine " + plainText(m.Name) + " " + plainText(m.Version) + "\n\n"
295	out += "- Realm: " + sanitize.InlineCode(id) + "\n"
296	out += "- Author: " + who(m.Author) + "\n"
297	out += "- Status: **" + core.EngineStatus(id) + "**, sample fingerprint intact: " + strconv.FormatBool(core.VerifyEngine(id)) + "\n"
298	out += "- Kind: " + plainText(m.Kind) + ", collections: " + jsonNum(core.Engine(id), "collections") + "\n"
299	out += "- License: " + plainText(m.License) + "\n"
300	out += "- Traits: " + plainText(strings.Join(m.TraitTypes, ", ")) + "\n\n"
301	if m.Description != "" {
302		out += plainText(m.Description) + "\n"
303	}
304	return out
305}
306
307func userPage(name string) string {
308	u, _ := users.ResolveName(name)
309	if u == nil {
310		return "# Unknown user\n"
311	}
312	addr := u.Addr()
313	return "# @" + sanitize.InlineText(u.Name()) + "\n\n`" + addr.String() + "`\n\n" +
314		"Collections and listings of this address: reader realm, `Profile(" + addr.String() + ")`.\n"
315}
316
317// who renders an address as its r/sys/users name when it has one.
318func who(addr address) string {
319	if u := users.ResolveAddress(addr); u != nil {
320		return "[@" + sanitize.InlineText(u.Name()) + "](/u/" + sanitize.InlineText(u.Name()) + ")"
321	}
322	return "`" + addr.String() + "`"
323}
324
325func pageFrom(query string) int64 {
326	for _, kv := range strings.Split(query, "&") {
327		if strings.HasPrefix(kv, "page=") {
328			if n, err := strconv.ParseInt(strings.TrimPrefix(kv, "page="), 10, 64); err == nil && n >= 1 && n <= maxPageNum {
329				return n
330			}
331		}
332	}
333	return 1
334}
335
336// selfPath is captured at deploy; links are gnoweb paths without the domain.
337var selfPath string
338
339func init(cur realm) {
340	selfPath = cur.PkgPath()
341}
342
343func base() string {
344	return strings.TrimPrefix(selfPath, "gno.land")
345}
346
347func link(route string) string { return base() + ":" + route }
348
349func itoa(n int64) string { return strconv.FormatInt(n, 10) }