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

11.23 Kb · 319 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.
 16package gallery
 17
 18import (
 19	"strconv"
 20	"strings"
 21
 22	"gno.land/p/nt/markdown/sanitize/v0"
 23	"gno.land/p/nt/ufmt/v0"
 24	"gno.land/r/sys/users"
 25
 26	"gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia/media/v1"
 27	"gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia/types/v1"
 28	assets "gno.land/r/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia3/assets/v1"
 29	"gno.land/r/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia3/core"
 30	upload "gno.land/r/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia3/engines/upload"
 31	market "gno.land/r/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia3/market/v1"
 32)
 33
 34const (
 35	pageSize   = 20
 36	homeLimit  = 20
 37	maxPageNum = 500
 38)
 39
 40func Render(path string) string {
 41	route, query, _ := strings.Cut(path, "?")
 42	parts := strings.Split(route, "/")
 43	switch {
 44	case route == "":
 45		return home()
 46	case len(parts) == 2 && parts[0] == "c":
 47		return collectionPage(parts[1], pageFrom(query))
 48	case len(parts) == 4 && parts[0] == "c" && parts[2] == "t":
 49		return tokenPage(parts[1], parts[3])
 50	case len(parts) > 1 && parts[0] == "e":
 51		return enginePage(strings.TrimPrefix(route, "e/"))
 52	case len(parts) == 2 && parts[0] == "u":
 53		return userPage(parts[1])
 54	}
 55	return "# Not found\n\n[Back to the gallery](" + base() + ")\n"
 56}
 57
 58func home() string {
 59	n := core.CollectionCount()
 60	out := ufmt.Sprintf("# Gallery\n\n%d collections. Art is computed on chain from each collection's engine and revealed seed.\n\n", n)
 61	for id := n; id >= 1 && id > n-homeLimit; id-- {
 62		info := core.Info(itoa(id))
 63		out += ufmt.Sprintf("- [%s](%s) — %s, %d/%d minted\n",
 64			plainText(info.Name), link("c/"+info.ID), info.State.String(), info.Minted, info.MaxSupply)
 65	}
 66	return out
 67}
 68
 69func collectionPage(cid string, page int64) string {
 70	info := core.Info(cid)
 71	out := "# " + plainText(info.Name) + " (" + plainText(info.Symbol) + ")\n\n"
 72	if info.Description != "" {
 73		out += plainText(info.Description) + "\n\n"
 74	}
 75	out += ufmt.Sprintf("- State: **%s**, %d/%d minted, royalty %d bps\n", info.State.String(), info.Minted, info.MaxSupply, info.RoyaltyBps)
 76	out += "- Creator: " + who(info.Creator) + "\n"
 77	out += "- Engine: [" + plainText(info.EngineID) + "](" + link("e/"+info.EngineID) + ")\n"
 78	if info.Badge != "" {
 79		out += "- Provenance: **" + info.Badge + "** — " + fairness(cid) + "\n"
 80	}
 81	if info.EngineID == upload.SelfPath() {
 82		out += uploadCollectionLines(cid)
 83	}
 84	if f := market.Floor(cid); f > 0 {
 85		out += ufmt.Sprintf("- Floor: %d ugnot\n", f)
 86	}
 87	out += "\n## Tokens\n\n"
 88	first := (page-1)*pageSize + 1
 89	for n := first; n < first+pageSize && n <= info.Minted; n++ {
 90		tid := itoa(n)
 91		owner, err := core.OwnerOf(cid, tid)
 92		if err != nil {
 93			continue
 94		}
 95		line := "- [#" + tid + "](" + link("c/"+cid+"/t/"+tid) + ") owned by " + who(owner)
 96		if _, price, ok := market.ActiveListing(cid, tid); ok {
 97			line += ufmt.Sprintf(", listed at %d ugnot", price)
 98		}
 99		out += line + "\n"
100	}
101	if first+pageSize <= info.Minted {
102		out += "\n[Next page](" + link(ufmt.Sprintf("c/%s?page=%d", cid, page+1)) + ")\n"
103	}
104	return out
105}
106
107func tokenPage(cid, tid string) string {
108	info := core.Info(cid)
109	owner, err := core.OwnerOf(cid, tid)
110	if err != nil {
111		return "# Token not found\n"
112	}
113	isUpload := info.EngineID == upload.SelfPath()
114	uri, _ := core.TokenURI(cid, tid)
115	var art media.Artwork
116	var slot, edition int64
117	ready := false
118	if isUpload {
119		ready, slot, edition, art = upload.Assignment(cid, tid)
120		if ready && art.Kind == media.KindChain {
121			uri = upload.TokenImage(cid, tid) // the stored file itself (data: URI, gnoweb img-src data:)
122		}
123	}
124	out := "# " + plainText(info.Name) + " #" + tid + "\n\n"
125	out += "![" + plainText(info.Name) + " #" + tid + "](" + sanitize.ImageURL(uri) + ")\n\n"
126	out += "- Owner: " + who(owner) + "\n"
127	if isUpload {
128		out += uploadTokenLines(cid, ready, edition, art)
129	}
130	if id, price, ok := market.ActiveListing(cid, tid); ok {
131		out += ufmt.Sprintf("- Listed: %d ugnot (listing %s)\n", price, id)
132	}
133	out += "\n## Traits\n\n"
134	traits := core.TokenTraitList(cid, tid)
135	if isUpload {
136		traits = upload.TokenTraitList(cid, tid)
137		out += "Tier and traits assigned by the creator.\n\n"
138	}
139	for _, t := range traits {
140		out += "- " + plainText(t.Type) + ": " + plainText(t.Value) + "\n"
141	}
142	out += "\n## How to verify\n\n"
143	if info.State != types.StateFinal {
144		out += "The collection is not revealed yet; every token shows the same placeholder.\n"
145		return out
146	}
147	if isUpload {
148		return out + uploadVerifySteps(cid, tid, ready, slot, art)
149	}
150	out += "1. Read `Provenance(\"" + cid + "\")` on core from another RPC and recompute the commitments and the salt.\n"
151	out += "2. Seed: `TokenSeed(\"" + cid + "\", \"" + tid + "\")` = sha256(\"" + "art|seed|v1|" + cid + "|" + tid + "|\" + salt).\n"
152	out += "3. Render it yourself: `RenderWith(engine, params, seed)` must return this image.\n\n"
153	out += "Provenance badge **" + info.Badge + "**, on-chain check: " + strconv.FormatBool(core.VerifyProvenance(cid)) +
154		"; " + fairness(cid) + "\n"
155	return out
156}
157
158// --- uploaded collections (docs/uploaded-collections.md §5.4, §9) -------------
159
160// badgeLabel is the site's storage badge text (§9).
161func badgeLabel(b string) string {
162	switch b {
163	case "onchain":
164		return "On-chain"
165	case "ipfs":
166		return "IPFS · hash-locked"
167	case "mixed":
168		return "Mixed (on-chain and IPFS)"
169	case "computed":
170		return "Computed on-chain"
171	}
172	return "Not ready"
173}
174
175func uploadCollectionLines(cid string) string {
176	ready, reason, store, badge := upload.Readiness(cid)
177	out := "- Storage: **" + badgeLabel(badge) + "**\n"
178	if !ready {
179		out += "- **Not ready (" + reason + ")**: the artwork set is not locked to this collection; do not mint.\n"
180	}
181	if store == "" {
182		return out
183	}
184	si := assets.Info(store)
185	out += ufmt.Sprintf("- Artwork set: store %s, %d artworks, %d slots (%d on-chain, %d IPFS); verifiable on chain / IPFS\n",
186		store, si.Artworks, si.Slots, si.OnchainN, si.IPFSN)
187	tiers, slots, untiered := assets.TierSlots(store)
188	if len(tiers) > 0 {
189		var parts []string
190		for i := len(tiers) - 1; i >= 0; i-- { // high -> low, like a drop table
191			if slots[i] > 0 {
192				parts = append(parts, ufmt.Sprintf("%d %s", slots[i], plainText(tiers[i])))
193			}
194		}
195		if untiered > 0 {
196			parts = append(parts, ufmt.Sprintf("%d untiered", untiered))
197		}
198		out += "- Set composition (creator-assigned tiers, slots of " + itoa(si.Slots) + "): " +
199			strings.Join(parts, " · ") + "\n"
200	}
201	return out
202}
203
204func uploadTokenLines(cid string, ready bool, edition int64, a media.Artwork) string {
205	_, _, _, badge := upload.Readiness(cid)
206	out := "- Storage: **" + badgeLabel(badge) + "**\n"
207	if !ready {
208		return out
209	}
210	if a.Title != "" {
211		out += "- Artwork: " + plainText(a.Title) + "\n"
212	}
213	if a.Tier != "" {
214		out += "- Tier: **" + plainText(a.Tier) + "** (assigned by the creator)\n"
215	}
216	if a.Editions > 1 {
217		out += ufmt.Sprintf("- Edition %d of %d\n", edition, a.Editions)
218	}
219	out += ufmt.Sprintf("- File: %s, %d bytes, sha256 `%s`\n", media.MimeType(a.Mime), a.Size, a.SHA256)
220	if a.Kind != media.KindChain {
221		// never a remote image (SA-M4): the address is escaped text
222		out += "- IPFS: " + plainText("ipfs://"+a.CID) + " (fetch it from any gateway and check the sha256)\n"
223	}
224	return out
225}
226
227func uploadVerifySteps(cid, tid string, ready bool, slot int64, a media.Artwork) string {
228	if !ready {
229		return "This uploaded collection is not bound to a sealed artwork set; no artwork is assigned.\n"
230	}
231	store := upload.StoreOf(cid)
232	out := "1. Read `Provenance(\"" + cid + "\")` on core from another RPC and recompute the commitments and the salt.\n"
233	out += "2. Key: `K = sha256(\"art|upload|perm|v1|" + cid + "|\" + salt)`, slot: `Permute(K, slots, " + tid +
234		" - 1)` (media/v1; a single-artwork store uses slot = " + tid + " - 1).\n"
235	out += ufmt.Sprintf("3. `SlotArtwork(\"%s\", %d)` on the asset store gives artwork %d; its sha256 must be the file's.\n",
236		store, slot, a.Index)
237	out += "\nProvenance badge **" + core.Info(cid).Badge + "**, on-chain check: " +
238		strconv.FormatBool(core.VerifyProvenance(cid)) + "; " + fairness(cid) + "\n"
239	return out
240}
241
242// fairness never calls a collection "verified" unless it is a fair launch
243// (core.FairLaunch): a consistent record is not enough when one party could
244// choose the map (SA-M2).
245func fairness(cid string) string {
246	if core.FairLaunch(cid) {
247		return "fair launch, verified on chain"
248	}
249	if !core.VerifyProvenance(cid) {
250		return "NOT verified: the provenance record does not check out"
251	}
252	return "not a fair launch: one party could choose the map (record consistent)"
253}
254
255// plainText renders user text as inert markdown: sanitize.InlineText escapes
256// every inline-active character (\ * _ [ ] ( ) ~ > - + . ! ` # < &) and folds
257// newlines, and escaping ':' breaks bare-URL autolinks ("https\://…"), which
258// InlineText leaves alone (SA-M4).
259func plainText(s string) string {
260	return strings.ReplaceAll(sanitize.InlineText(s), ":", "\\:")
261}
262
263func enginePage(id string) string {
264	m := core.EngineManifest(id)
265	out := "# Engine " + plainText(m.Name) + " " + plainText(m.Version) + "\n\n"
266	out += "- Realm: " + sanitize.InlineCode(id) + "\n"
267	out += "- Author: " + who(m.Author) + "\n"
268	out += "- Status: **" + core.EngineStatus(id) + "**, sample fingerprint intact: " + strconv.FormatBool(core.VerifyEngine(id)) + "\n"
269	out += "- License: " + plainText(m.License) + "\n"
270	out += "- Traits: " + plainText(strings.Join(m.TraitTypes, ", ")) + "\n\n"
271	if m.Description != "" {
272		out += plainText(m.Description) + "\n"
273	}
274	return out
275}
276
277func userPage(name string) string {
278	u, _ := users.ResolveName(name)
279	if u == nil {
280		return "# Unknown user\n"
281	}
282	addr := u.Addr()
283	return "# @" + sanitize.InlineText(u.Name()) + "\n\n`" + addr.String() + "`\n\n" +
284		"Collections and listings of this address: reader realm, `Profile(" + addr.String() + ")`.\n"
285}
286
287// who renders an address as its r/sys/users name when it has one.
288func who(addr address) string {
289	if u := users.ResolveAddress(addr); u != nil {
290		return "[@" + sanitize.InlineText(u.Name()) + "](/u/" + sanitize.InlineText(u.Name()) + ")"
291	}
292	return "`" + addr.String() + "`"
293}
294
295func pageFrom(query string) int64 {
296	for _, kv := range strings.Split(query, "&") {
297		if strings.HasPrefix(kv, "page=") {
298			if n, err := strconv.ParseInt(strings.TrimPrefix(kv, "page="), 10, 64); err == nil && n >= 1 && n <= maxPageNum {
299				return n
300			}
301		}
302	}
303	return 1
304}
305
306// selfPath is captured at deploy; links are gnoweb paths without the domain.
307var selfPath string
308
309func init(cur realm) {
310	selfPath = cur.PkgPath()
311}
312
313func base() string {
314	return strings.TrimPrefix(selfPath, "gno.land")
315}
316
317func link(route string) string { return base() + ":" + route }
318
319func itoa(n int64) string { return strconv.FormatInt(n, 10) }