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.

pafta.gno

8.08 Kb · 221 lines
  1// Package pafta is the adapter that registers the Pafta engine
  2// (p/…/sinopia/art/pafta/v1) with core. Core knows the engine only as this
  3// realm's package path plus an interface value.
  4//
  5// Pafta is a collection-level engine: 256 tokens are the 256 sheets of one
  6// map. Which map and which sheet a token is must stay unknown until the sale
  7// is over (otherwise buyers could target the summit or the lone island), so
  8// both come from the collection's final salt (art.Placement):
  9//
 10//   - core's own TokenURI / TokenCard / TokenTraits call for a Final Pafta
 11//     collection is "placed": core passes the collection's final salt in
 12//     engine.Input.Salt (core-final round), Render and Traits check that the
 13//     seed is exactly core's (seed == safe.Seed(cid, tid, salt)) and draw
 14//     sheet tile(tid) of map mapSeed(cid, salt) — no read-back of core;
 15//   - everything else is "standalone" (art.Render / art.Traits of the seed):
 16//     core.RenderWith / RenderWithInput / EngineTraits (CollectionID "0",
 17//     Salt ""), the registration sample and any forged input;
 18//   - before Final core never calls Render or Traits (placeholder /
 19//     "Unrevealed"), core.Salt is "" and so are Sheet and Sheets here;
 20//   - CheckLaunch refuses any supply but 256 (one token per sheet) and
 21//     GateLaunch (engine.LaunchGate) refuses open mode: an open
 22//     collection's salt is public from Launch, so a shared map would be
 23//     targetable. Every Pafta collection is therefore a 256-sheet
 24//     commit-reveal collection, which is what lets Render trust Input.Salt.
 25//
 26// The adapter keeps no state of its own (selfPath is set once by init) and
 27// has no admin: what it renders is fixed by the p/ package it imports. Never
 28// mark this realm private (design §5.4: a re-deployable adapter could swap
 29// the art). Per-chain values (author key, fee) live in config.gno.
 30package pafta
 31
 32import (
 33	"errors"
 34	"strconv"
 35
 36	art "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/art/pafta/v1"
 37	"gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/engine/v2"
 38	"gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/safe/v2"
 39	"gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/types/v2"
 40	"gno.land/r/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/core"
 41)
 42
 43// selfPath is this realm's package path = the engine id in core.
 44var selfPath string
 45
 46func init(cur realm) {
 47	selfPath = cur.PkgPath()
 48}
 49
 50// Author is the engine-author key that registers, claims and may deprecate
 51// this engine in core.
 52func Author() address { return author }
 53
 54// SelfPath is the engine id.
 55func SelfPath() string { return selfPath }
 56
 57var errSupply = errors.New("pafta: a Pafta collection is exactly " + strconv.Itoa(art.Sheets) +
 58	" sheets of one map; launch it with maxSupply " + strconv.Itoa(art.Sheets))
 59
 60// impl is zero-size and declared here, so core runs it with this realm's
 61// authority (interrealm v2 borrow rule #1).
 62type impl struct{}
 63
 64func (impl) Manifest() engine.Manifest {
 65	m := art.Manifest()
 66	m.Author = author
 67	m.FeeBps = AuthorFeeBps
 68	return m
 69}
 70
 71func (impl) ValidateParams(params string) error { return art.ValidateParams(params) }
 72
 73// TraitTypes: every collection uses the manifest's list.
 74func (impl) TraitTypes(params string) []string { return art.Manifest().TraitTypes }
 75
 76// CheckLaunch: one token per sheet, so the supply is exactly 256.
 77func (impl) CheckLaunch(cid, params string, maxSupply int64) error {
 78	if maxSupply != art.Sheets {
 79		return errSupply
 80	}
 81	return nil
 82}
 83
 84var errOpen = errors.New("pafta: a Pafta map stays hidden until the reveal; launch in reveal mode (open mode publishes the salt)")
 85
 86// GateLaunch (engine.LaunchGate): reveal mode only.
 87func (impl) GateLaunch(c engine.Collection) error {
 88	if c.Mode != engine.ModeReveal {
 89		return errOpen
 90	}
 91	return nil
 92}
 93
 94func (impl) Traits(in engine.Input) []engine.Trait {
 95	if p, ok := placed(in); ok {
 96		cfg, _ := art.ParseParams(in.Params)
 97		return art.SheetTraits(in.Seed, cfg, p)
 98	}
 99	return art.Traits(in)
100}
101
102func (impl) Render(in engine.Input) string {
103	if p, ok := placed(in); ok {
104		cfg, _ := art.ParseParams(in.Params)
105		return art.SheetSVG(in.Seed, cfg, p)
106	}
107	return art.Render(in)
108}
109
110// Media: an svg engine; Render is the art.
111func (impl) Media(in engine.Input) engine.Media { return engine.Media{} }
112
113// Register is an explicit call, not init: under the inert package policy
114// init runs inside the approver's transaction (RA B-1). It registers this
115// realm, once, and core accepts it only when the transaction is signed by
116// the manifest author (author, config.gno). salt "" = no commit
117// reservation; the fingerprint is in core's in-source reserved table
118// (r/core config.gno), so only that key can register this art anywhere.
119func Register(cur realm) string {
120	return core.RegisterEngine(cross(cur), impl{}, "")
121}
122
123// --- placement ---------------------------------------------------------------
124
125// placed answers core's real token call for a Final Pafta collection: the
126// salt is core's (Input.Salt, set only on token reads of a Final
127// collection), the collection id a real one and the seed exactly
128// safe.Seed(cid, tid, salt). Studio inputs (CollectionID "0", Salt "")
129// and anything inconsistent are standalone.
130func placed(in engine.Input) (art.Place, bool) {
131	if in.CollectionID == "" || in.CollectionID == "0" || !safe.IsHex32(in.Salt) {
132		return art.Place{}, false
133	}
134	n := tidNum(in.TokenID)
135	if n < 1 || n > art.Sheets || in.Seed != safe.Seed(in.CollectionID, in.TokenID, in.Salt) {
136		return art.Place{}, false
137	}
138	return art.Placement(in.CollectionID, n, in.Salt), true
139}
140
141// finalSalt is the salt of a Final commit-reveal Pafta collection of 256 and
142// the numeric token id when tid is a valid one; ok false otherwise (the
143// Sheet/Sheets reads, which have no engine input).
144func finalSalt(info types.CollectionInfo, tid string) (string, int64, bool) {
145	if info.EngineID != selfPath || info.State != types.StateFinal || info.Mode != types.ModeReveal ||
146		info.MaxSupply != art.Sheets || !safe.IsHex32(info.Salt) {
147		return "", 0, false
148	}
149	n := tidNum(tid)
150	if n < 1 || n > art.Sheets {
151		return "", 0, false
152	}
153	return info.Salt, n, true
154}
155
156// tidNum is the canonical decimal token id, 0 otherwise.
157func tidNum(tid string) int64 {
158	if tid == "" || len(tid) > 4 || tid[0] == '0' {
159		return 0
160	}
161	var n int64
162	for i := 0; i < len(tid); i++ {
163		c := tid[i]
164		if c < '0' || c > '9' {
165			return 0
166		}
167		n = n*10 + int64(c-'0')
168	}
169	return n
170}
171
172// --- reads for composite views -------------------------------------------------
173
174// Sheet is token tid's place on the map once collection cid is Final:
175// {"cid","tid","tile","sheet","col","row"} (tile 0..255 row-major, col and
176// row 0..15), or null (not a Final commit-reveal Pafta collection, or no
177// such token). Before Final it is always null: nothing about the map exists
178// yet.
179func Sheet(cid, tid string) string {
180	info := core.Info(cid)
181	salt, n, ok := finalSalt(info, tid)
182	if !ok || n > info.Minted {
183		return "null"
184	}
185	if _, err := core.OwnerOf(info.ID, tid); err != nil {
186		return "null"
187	}
188	t := art.Placement(info.ID, n, salt).Tile
189	return `{"cid":"` + info.ID + `","tid":"` + tid + `","tile":` + strconv.Itoa(t) + `,"sheet":"` + art.SheetCode(t) +
190		`","col":` + strconv.Itoa(t%art.Side) + `,"row":` + strconv.Itoa(t/art.Side) + `}`
191}
192
193// Sheets is the whole layout of a Final collection: {"cid","mapSeed",
194// "tiles":[tid of each sheet 0..255, "" where no token was minted]} or null
195// (as Sheet). A composite view (gallery, reader) crops each token's
196// TokenURI to the map face (art.MapX, MapY, MapSize of a Width x Height
197// sheet) and lays the faces edge to edge in this order; burned tokens stay
198// listed (check OwnerOf).
199func Sheets(cid string) string {
200	info := core.Info(cid)
201	salt, _, ok := finalSalt(info, "1")
202	if !ok {
203		return "null"
204	}
205	tiles := make([]int64, art.Sheets)
206	for n := int64(1); n <= info.Minted && n <= art.Sheets; n++ {
207		tiles[art.Placement(info.ID, n, salt).Tile] = n
208	}
209	out := `{"cid":"` + info.ID + `","mapSeed":"` + art.MapSeed(info.ID, salt) + `","tiles":[`
210	for t, n := range tiles {
211		if t > 0 {
212			out += ","
213		}
214		if n == 0 {
215			out += `""`
216		} else {
217			out += `"` + strconv.FormatInt(n, 10) + `"`
218		}
219	}
220	return out + "]}"
221}