katman.gno
12.09 Kb · 343 lines
1// Package katman is the adapter of Katman, Sinopia's open PFP tool engine
2// (p/…/sinopia/art/katman/v1). Core knows the engine only as this realm's
3// package path plus an interface value.
4//
5// A collection's params name a kit: {"store":<id>,"palette":<name>}. Store 0
6// is the art package's built-in demo kit (the engine's sample and Studio
7// preview; CheckLaunch never launches it). Any other id is a sealed
8// assets/v1 store the artist uploaded; this realm reads it and nothing else.
9// The kit layout in a store (docs/engines/katman.md "Kit format"):
10//
11// - layers = the store's trait types, bottom first (1..8); the store's
12// tiers are the same names in the same order, so assets.TierSlots gives
13// every layer's weight line in one O(layers) read;
14// - one part = one on-chain SVG artwork: its tier is its layer, its only
15// non-empty trait value (in that layer's column) is the part's name, its
16// editions are the part's rarity weight; artworks are sorted by layer;
17// - every part file is exactly art.Root(w, h) + inner + "</svg>" with one
18// w x h canvas for the whole kit.
19//
20// A token draws one point per layer from its final seed (art.Draw); since a
21// layer's parts are consecutive editions, the point is a slot and
22// assets.SlotArtwork its part (O(log n)). Render stacks the drawn parts
23// (art.ComposeParts); Traits are layer name = part name.
24//
25// What holds, and why:
26// - art theft: GateCreate (engine.CreateGate) accepts a store kit only
27// when core's creator owns the store — core passes the creator
28// explicitly, so no OriginCaller read (stores are wallet-owned, so
29// the creator must be that wallet); core's paramsTaken refuses the same params to anyone else, so
30// only the owner's collections use the kit;
31// - no swap after sale: stores are sealed before Create and never change;
32// params are fixed by core;
33// - composition never fails at mint: the creator's Create runs the full
34// kit check (layout, every part's root and size, worst composed token
35// <= MaxSVGBytes); a sealed store and this code never change afterwards.
36// Even an unchecked kit (a Studio preview) composes an allowlisted
37// document: a part that does not fit is left out (art.ComposeParts);
38// - snipe resistance is core's: before Final every token is core's
39// placeholder and the seed does not exist yet; the kit itself is public
40// by design (like an engine's code).
41//
42// The adapter is stateless, has no admin and holds no coins. Never mark
43// this realm private (design §5.4).
44package katman
45
46import (
47 "errors"
48 "strconv"
49
50 art "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/art/katman/v1"
51 "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/engine/v2"
52 "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/media/v2"
53 assets "gno.land/r/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/assets/v1"
54 "gno.land/r/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/core"
55)
56
57// Author is the engine-author key that registers, claims and may deprecate
58// this engine in core.
59func Author() address { return author }
60
61// impl is zero-size and declared here, so core runs it with this realm's
62// authority (interrealm v2 borrow rule #1).
63type impl struct{}
64
65func (impl) Manifest() engine.Manifest {
66 m := art.Manifest()
67 m.Author = author
68 m.FeeBps = AuthorFeeBps
69 return m
70}
71
72// ValidateParams accepts "" and the canonical JSON. Store 0 is the demo
73// kit. A store kit needs an existing, sealed store laid out as a kit (cheap
74// O(layers) reads), in every context, so anyone can preview a kit (Studio's
75// RenderWith/TraitsWith); an unchecked kit still renders safely. Who may
76// use the kit, and the full per-part check, is GateCreate's job.
77func (impl) ValidateParams(params string) error {
78 cfg, err := art.ParseParams(params)
79 if err != nil {
80 return err
81 }
82 if cfg.Store == 0 {
83 return nil
84 }
85 return checkKit(cfg, "", false)
86}
87
88// GateCreate (engine.CreateGate) runs in core.CreateCollection, in the
89// creator's transaction, after ValidateParams: a store kit needs the
90// creator (as core records it) to own the store, and every part is checked
91// (checkKit full). The demo kit needs nothing here (CheckLaunch never
92// launches it).
93func (impl) GateCreate(c engine.Collection) error {
94 cfg, err := art.ParseParams(c.Params)
95 if err != nil {
96 return err
97 }
98 if cfg.Store == 0 {
99 return nil
100 }
101 if !c.Creator.IsValid() {
102 return errors.New("katman: invalid creator")
103 }
104 return checkKit(cfg, c.Creator, true)
105}
106
107// TraitTypes: the demo kit's layers, or the store kit's layer names.
108func (impl) TraitTypes(params string) []string {
109 cfg, err := art.ParseParams(params)
110 if err != nil || cfg.Store == 0 || !storeExists(cfg.Store) {
111 return art.DemoLayerNames()
112 }
113 names, _ := layout(storeID(cfg.Store))
114 return names
115}
116
117// CheckLaunch: only a store kit launches (the demo kit is a preview), and
118// its store must still be sealed (it always is: sealing is final).
119func (impl) CheckLaunch(cid, params string, maxSupply int64) error {
120 cfg, err := art.ParseParams(params)
121 if err != nil {
122 return err
123 }
124 if cfg.Store == 0 {
125 return errors.New("katman: collection " + cid + " uses the demo kit; a launch needs a part store (params store >= 1)")
126 }
127 if !storeExists(cfg.Store) || !assets.Info(storeID(cfg.Store)).Sealed {
128 return errors.New("katman: store " + storeID(cfg.Store) + " is not a sealed store")
129 }
130 return nil
131}
132
133func (impl) Traits(in engine.Input) []engine.Trait {
134 cfg := art.MustConfig(in.Params)
135 if cfg.Store == 0 {
136 return art.Traits(in)
137 }
138 id := storeID(cfg.Store)
139 names, totals := layout(id)
140 idx := drawn(id, in.Seed, totals)
141 out := make([]engine.Trait, len(names))
142 for k, i := range idx {
143 v := ""
144 if i >= 0 {
145 if a := assets.ArtworkMeta(id, i); k < len(a.Traits) {
146 v = a.Traits[k]
147 }
148 }
149 if v == "" {
150 v = "Part " + strconv.FormatInt(i+1, 10)
151 }
152 out[k] = engine.Trait{Type: names[k], Value: v}
153 }
154 return out
155}
156
157func (impl) Render(in engine.Input) string {
158 cfg := art.MustConfig(in.Params)
159 if cfg.Store == 0 {
160 return art.Render(in)
161 }
162 id := storeID(cfg.Store)
163 _, totals := layout(id)
164 idx := drawn(id, in.Seed, totals)
165 parts := make([]string, len(idx))
166 for k, i := range idx {
167 if i >= 0 {
168 parts[k] = assets.ArtworkBytes(id, i) // "" unless a complete on-chain file
169 }
170 }
171 return art.ComposeParts(in.Seed, cfg.Palette, parts)
172}
173
174// Media: an svg engine; Render is the art.
175func (impl) Media(in engine.Input) engine.Media { return engine.Media{} }
176
177// Register is an explicit call, not init: under the inert package policy
178// init runs inside the approver's transaction (RA B-1). It registers this
179// realm, once, and core accepts it only when the transaction is signed by
180// the manifest author (author, config.gno). salt "" = no commit
181// reservation; the fingerprint is in core's in-source reserved table
182// (r/core config.gno), so only that key can register this art anywhere.
183func Register(cur realm) string {
184 return core.RegisterEngine(cross(cur), impl{}, "")
185}
186
187// CheckKit is the full kit check of params, without the owner rule: ""
188// when a collection over them would be accepted (by the store's owner),
189// else the reason. The Create wizard calls it right after Seal (a query).
190func CheckKit(params string) string {
191 cfg, err := art.ParseParams(params)
192 if err != nil {
193 return err.Error()
194 }
195 if cfg.Store == 0 {
196 return ""
197 }
198 if err := checkKit(cfg, "", true); err != nil {
199 return err.Error()
200 }
201 return ""
202}
203
204// --- store kits ----------------------------------------------------------------
205
206func storeID(n int64) string { return strconv.FormatInt(n, 10) }
207
208func storeExists(n int64) bool { return n >= 1 && n <= assets.StoreCount() }
209
210// layout is a kit's layer names (its tiers) and weight lines (tier slots).
211func layout(id string) ([]string, []int64) {
212 names, totals, _ := assets.TierSlots(id)
213 return names, totals
214}
215
216// drawn maps a seed to the artwork index of every layer's part: the drawn
217// point of layer k is slot start_k + point (layers are consecutive slot
218// ranges in a checked kit). -1 for a layer without weight (unchecked kits).
219func drawn(id, seed string, totals []int64) []int64 {
220 at := art.Draw(seed, totals)
221 out := make([]int64, len(totals))
222 var start int64
223 for k, t := range totals {
224 out[k] = -1
225 if t > 0 {
226 out[k], _ = assets.SlotArtwork(id, start+at[k])
227 }
228 start += t
229 }
230 return out
231}
232
233// checkKit is the kit check. Always (O(layers)): the store exists and is
234// sealed; 1..MaxLayers layers; its tiers are its trait types; every layer
235// has weight; no untiered artwork; at most MaxParts parts, all on chain,
236// at most MaxKitBytes in all. With an owner: owner owns the store. With
237// full: every part (O(parts)) — an SVG; sorted by layer;
238// exactly its own layer's value set; names unique per layer; at most
239// MaxPartsPerLayer per layer; the file is art.Root(w, h) of the first part
240// + inner + "</svg>"; and the worst composed token under cfg's palette is at
241// most art.MaxSVGBytes.
242func checkKit(cfg art.Config, owner address, full bool) error {
243 if !storeExists(cfg.Store) {
244 return errors.New("katman: unknown store " + storeID(cfg.Store))
245 }
246 id := storeID(cfg.Store)
247 si := assets.Info(id)
248 if !si.Sealed {
249 return errors.New("katman: store " + id + " is not sealed")
250 }
251 n := len(si.TraitTypes)
252 if n < 1 || n > art.MaxLayers {
253 return errors.New("katman: a kit has 1.." + strconv.Itoa(art.MaxLayers) + " layers (the store's trait types)")
254 }
255 if len(si.Tiers) != n {
256 return errors.New("katman: the store's tiers must be its trait types (one tier per layer)")
257 }
258 for k := range si.Tiers {
259 if si.Tiers[k] != si.TraitTypes[k] {
260 return errors.New("katman: tier " + strconv.Itoa(k+1) + " must be the layer " + si.TraitTypes[k])
261 }
262 }
263 _, totals, untiered := assets.TierSlots(id)
264 if untiered != 0 {
265 return errors.New("katman: every part needs its layer as tier")
266 }
267 for k, t := range totals {
268 if t < 1 {
269 return errors.New("katman: layer " + si.TraitTypes[k] + " has no part")
270 }
271 }
272 if si.Artworks > art.MaxParts {
273 return errors.New("katman: a kit has at most " + strconv.Itoa(art.MaxParts) + " parts")
274 }
275 if si.OnchainN != si.Artworks {
276 return errors.New("katman: every part must be an on-chain SVG")
277 }
278 if si.OnchainBytes > art.MaxKitBytes {
279 return errors.New("katman: a kit holds at most " + strconv.Itoa(art.MaxKitBytes) + " bytes of parts")
280 }
281 if owner.IsValid() && owner != si.Owner {
282 return errors.New("katman: the creator does not own store " + id)
283 }
284 if !full {
285 return nil
286 }
287 root, w, h := "", 0, 0
288 maxInner := make([]int, n)
289 count := make([]int, n)
290 seen := map[string]bool{}
291 prev := 0
292 for i := int64(0); i < si.Artworks; i++ {
293 at := "katman: part " + strconv.FormatInt(i+1, 10) + ": "
294 a := assets.ArtworkMeta(id, i)
295 if a.Mime != media.MimeSVG {
296 return errors.New(at + "must be an SVG")
297 }
298 k := -1
299 for j, t := range si.Tiers {
300 if t == a.Tier {
301 k = j
302 }
303 }
304 if k < prev {
305 return errors.New(at + "parts must be sorted by layer (tier)")
306 }
307 prev = k
308 for j, v := range a.Traits {
309 if (j == k) != (v != "") {
310 return errors.New(at + "must name itself in its own layer " + si.TraitTypes[k] + " only")
311 }
312 }
313 if key := strconv.Itoa(k) + "|" + a.Traits[k]; seen[key] {
314 return errors.New(at + "two parts of layer " + si.TraitTypes[k] + " are named " + a.Traits[k])
315 } else {
316 seen[key] = true
317 }
318 count[k]++
319 if count[k] > art.MaxPartsPerLayer {
320 return errors.New(at + "a layer has at most " + strconv.Itoa(art.MaxPartsPerLayer) + " parts")
321 }
322 file := assets.ArtworkBytes(id, i)
323 if root == "" {
324 r, ww, hh, ok := art.ParseRoot(file)
325 if !ok {
326 return errors.New(at + `must start with <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 W H"> (W, H 1..4096)`)
327 }
328 root, w, h = r, ww, hh
329 }
330 inner, ok := art.Inner(file, root)
331 if !ok {
332 return errors.New(at + "must start with the kit's root " + root + " and end with </svg>")
333 }
334 if len(inner) > maxInner[k] {
335 maxInner[k] = len(inner)
336 }
337 }
338 if worst := art.WorstBytes(root, w, h, cfg.Palette, maxInner); worst > art.MaxSVGBytes {
339 return errors.New("katman: the largest token would be " + strconv.Itoa(worst) + " bytes; at most " +
340 strconv.Itoa(art.MaxSVGBytes) + " (make the largest part of some layer smaller)")
341 }
342 return nil
343}