provenance.gno
11.77 Kb · 330 lines
1package core
2
3import (
4 "chain"
5 "chain/runtime"
6
7 "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/safe/v2"
8 "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/types/v2"
9)
10
11// Commit–reveal v2 (design §4.2–4.3, closes RA H-3, M-5, M-6).
12//
13// The map is fixed by three inputs: the creator's secret sc, the beacon's
14// secret sb and the public mint-entropy accumulator acc. Neither committer
15// knows the other's secret while minting is open, so neither can time the
16// last mint for a good map. Order of reveals:
17//
18// 1. nothing is accepted before Closed (an early reveal informs the other
19// side);
20// 2. the creator may reveal until revealDeadline - BeaconGraceBlocks; the
21// beacon follows at any time before the deadline;
22// 3. inside the last BeaconGraceBlocks only the beacon may reveal. If the
23// creator stayed silent, the beacon's reveal finalizes at once
24// ("beacon-only"), so the creator never sees sb while it can still
25// choose (SA-M1; this also closes the mempool race of a creator
26// front-running the beacon's grace reveal);
27// 4. after revealDeadline anyone finalizes with whatever was revealed.
28//
29// What stays open, by design: the beacon, as last revealer after the
30// creator, can still choose between revealing and not (one bit). It runs
31// public code and the badge shows the outcome. The transfer lock
32// (tokens.gno) keeps anyone from trading on early knowledge until Final.
33//
34// The second secret sb comes from the committed beacon (below) or, when the
35// collection was launched with a randomness provider, from the provider's
36// round bound after the creator's turn (randomness.gno); the order rules and
37// badges are the same.
38//
39// A collection launched without the beacon (only possible while no beacon
40// or provider is configured, SA-M2) gives the creator the whole choice of the map; it
41// can have no creator reserve and is never reported as a fair launch
42// (FairLaunch, Provenance "fair").
43type provenance struct {
44 creatorCommit, beaconCommit string
45 acc string
46 creatorSecret, beaconSecret string
47 creatorRevealed bool
48 beaconRevealed bool
49 salt, badge string
50 finalHeight int64
51 round int64 // randomness provider round (0 = none bound yet; randomness.gno)
52 roundHeight int64 // height the round was bound at; its value is taken only later (CV-I6)
53}
54
55const (
56 roleCreator = "creator"
57 roleBeacon = "beacon"
58)
59
60// BeaconCommit is the beacon's commitment, before any token exists.
61func BeaconCommit(cur realm, cid, commitHex string) {
62 assertNoDeposit(cur)
63 c := mustCollection(cid)
64 if beacon == "" || caller(cur) != beacon {
65 panic("only the configured beacon can commit")
66 }
67 if c.state == types.StateDraft || c.prov == nil || !c.useBeacon || usesProvider(c) {
68 panic("this collection is not waiting for a beacon")
69 }
70 if c.prov.beaconCommit != "" {
71 panic("the beacon already committed")
72 }
73 if c.minted() > 0 {
74 panic("too late: tokens were minted before the beacon committed")
75 }
76 commit := safe.AssertHex("commit", commitHex, 32)
77 if isBurned(c, commit) {
78 panic("this commitment's secret was already revealed for this collection")
79 }
80 c.prov.beaconCommit = commit
81 chain.Emit("BeaconCommitted", "cid", c.id, "commit", commit)
82}
83
84func RevealCreatorSecret(cur realm, cid, secret string) {
85 assertNoDeposit(cur)
86 c := mustCollection(cid)
87 assertCreator(cur, c)
88 assertNotOpen(c)
89 h := height()
90 assertRevealOpen(c, h)
91 if c.prov.creatorRevealed {
92 panic("the creator secret is already revealed")
93 }
94 if c.useBeacon {
95 // SA-M1: once the grace period starts only the beacon may reveal, so
96 // the creator can never reveal after seeing sb.
97 if c.prov.beaconRevealed {
98 panic("the beacon revealed first; the outcome is fixed")
99 }
100 if h >= revealDeadline(c)-BeaconGraceBlocks {
101 panic("the creator's reveal window ended at height " + itoa(revealDeadline(c)-BeaconGraceBlocks-1) +
102 "; only the beacon may reveal now")
103 }
104 }
105 assertSecret(c, roleCreator, secret, c.prov.creatorCommit)
106 c.prov.creatorSecret = secret
107 c.prov.creatorRevealed = true
108 chain.Emit("CreatorRevealed", "cid", c.id, "secret", secret)
109 if usesProvider(c) && c.prov.round == 0 {
110 // CV-M1: the reveal binds the provider's round in the same tx, so
111 // nobody can delay, time or skip the request afterwards. If the
112 // provider cannot name a fresh round now, anyone binds one later
113 // (RequestRandomness, Finalize); the creator's secret is public then.
114 bindRound(c, h)
115 }
116 if revealsComplete(c) && c.minted() > 0 { // SEC-F1: 0 mints stays Closed (abortable)
117 finalize(c, h)
118 }
119}
120
121func RevealBeaconSecret(cur realm, cid, secret string) {
122 assertNoDeposit(cur)
123 c := mustCollection(cid)
124 if beacon == "" || caller(cur) != beacon {
125 panic("only the configured beacon can reveal")
126 }
127 h := height()
128 assertRevealOpen(c, h)
129 if !c.useBeacon || usesProvider(c) || c.prov.beaconCommit == "" {
130 panic("this collection has no beacon commitment")
131 }
132 if c.prov.beaconRevealed {
133 panic("the beacon secret is already revealed")
134 }
135 if c.minted() == 0 { // SEC-F1 (SA-I9): a 0-mint sale stays abortable
136 panic("nothing was minted; the creator may abort and relaunch instead")
137 }
138 if !c.prov.creatorRevealed && h < revealDeadline(c)-BeaconGraceBlocks {
139 panic("the beacon reveals after the creator, or in the window's last " + itoa(BeaconGraceBlocks) + " blocks")
140 }
141 assertSecret(c, roleBeacon, secret, c.prov.beaconCommit)
142 c.prov.beaconSecret = secret
143 c.prov.beaconRevealed = true
144 chain.Emit("BeaconRevealed", "cid", c.id, "secret", secret)
145 // After the creator: both secrets are in (2/2). First, in the grace
146 // period: the creator can no longer reveal, so the map is fixed now
147 // (beacon-only) — nobody is left holding a choice (SA-M1).
148 finalize(c, h)
149}
150
151// Finalize is permissionless and never paused (I-2): immediately once every
152// expected secret is revealed, otherwise after revealDeadline (fallback).
153// For a provider collection it first binds or consumes the provider's round
154// (settleProvider) and falls back only when the provider failed to deliver.
155func Finalize(cur realm, cid string) {
156 assertNoDeposit(cur)
157 c := mustCollection(cid)
158 h := height()
159 refresh(c, h)
160 if c.state != types.StateClosed {
161 panic("only a closed collection can be finalized (state " + c.state.String() + ")")
162 }
163 if !revealsComplete(c) && h <= revealDeadline(c) {
164 panic("the reveal window is open until height " + itoa(revealDeadline(c)))
165 }
166 // SA-I9: a sale that minted nothing stays Closed, so its creator can
167 // still abort and relaunch; nobody else can lock it into Final.
168 if c.minted() == 0 {
169 panic("nothing was minted; the creator may abort and relaunch instead")
170 }
171 // CV-M1: a provider collection consumes its round whoever calls; the
172 // fallback needs the provider to have failed for ProviderWaitBlocks.
173 if usesProvider(c) && settleProvider(c, h) {
174 return
175 }
176 finalize(c, h)
177}
178
179func assertRevealOpen(c *collection, h int64) {
180 refresh(c, h)
181 if c.state != types.StateClosed {
182 panic("secrets are accepted only after the sale closed (state " + c.state.String() + ")")
183 }
184 if h > revealDeadline(c) {
185 panic("the reveal window closed; call Finalize")
186 }
187}
188
189func assertSecret(c *collection, role, secret, commit string) {
190 if len(secret) < MinSecretLen || len(secret) > MaxSecretLen {
191 panic("secret must be " + itoa(MinSecretLen) + ".." + itoa(MaxSecretLen) + " bytes")
192 }
193 safe.AssertText("secret", secret, MaxSecretLen)
194 if safe.Commit(runtime.ChainID(), c.id, role, secret) != commit {
195 panic("secret does not match the " + role + " commitment")
196 }
197}
198
199func revealsComplete(c *collection) bool {
200 if c.mode == types.ModeOpen {
201 return true // nothing to reveal: Finalize once Closed (I-17)
202 }
203 return c.prov.creatorRevealed && (!c.useBeacon || c.prov.beaconRevealed)
204}
205
206func assertNotOpen(c *collection) {
207 if c.mode == types.ModeOpen {
208 panic("an open collection has no secrets")
209 }
210}
211
212// openSalt is an open collection's salt: public from Launch, so anyone can
213// compute every token's art before minting (I-17).
214func openSalt(cid string) string {
215 return safe.Sha256Hex(safe.Domain + "|open|v1|" + runtime.ChainID() + "|" + cid)
216}
217
218// finalize runs at most once per collection (I-10): every caller checks
219// state Closed, and this sets Final.
220func finalize(c *collection, h int64) {
221 p := c.prov
222 p.salt = safe.Salt(c.id, p.creatorSecret, p.beaconSecret, p.acc)
223 switch {
224 case c.mode == types.ModeOpen:
225 p.salt = openSalt(c.id)
226 p.badge = BadgeOpen
227 case p.creatorRevealed && p.beaconRevealed:
228 p.badge = BadgeBoth
229 case p.creatorRevealed:
230 p.badge = BadgeCreatorOnly
231 case p.beaconRevealed:
232 p.badge = BadgeBeaconOnly
233 default:
234 p.badge = BadgeFallback
235 }
236 p.finalHeight = h
237 c.state = types.StateFinal
238 c.st.final = true
239 chain.Emit("CollectionFinal", "cid", c.id, "salt", p.salt, "badge", p.badge)
240}
241
242// VerifyProvenance recomputes every commitment and the salt from public
243// data. True only for a Final collection whose record is consistent.
244func VerifyProvenance(cid string) bool {
245 c := mustCollection(cid)
246 if c.state != types.StateFinal {
247 return false
248 }
249 p := c.prov
250 if c.mode == types.ModeOpen {
251 return p.salt == openSalt(c.id)
252 }
253 chainID := runtime.ChainID()
254 if p.creatorRevealed && safe.Commit(chainID, c.id, roleCreator, p.creatorSecret) != p.creatorCommit {
255 return false
256 }
257 if p.beaconRevealed && !usesProvider(c) && safe.Commit(chainID, c.id, roleBeacon, p.beaconSecret) != p.beaconCommit {
258 return false
259 }
260 // A provider's value was checked when consumed (FulfillRandomness or
261 // Finalize) against the provider's verified round (p.round); it is recomputable off chain
262 // from the provider's public source (e.g. the drand round).
263 return p.salt == safe.Salt(c.id, p.creatorSecret, p.beaconSecret, p.acc)
264}
265
266// FairLaunch is true only for a Final, consistent collection whose map no
267// single party could choose: launched with the beacon and finalized with
268// both secrets or with the beacon's alone (after the creator stayed
269// silent). Creator-only collections (no beacon: the creator alone knew the
270// only secret and could steer the last mint, SA-M2) and fallbacks are not
271// fair launches, even when VerifyProvenance is true.
272func FairLaunch(cid string) bool {
273 c := mustCollection(cid)
274 if c.mode == types.ModeOpen || !VerifyProvenance(cid) || !c.useBeacon {
275 return false
276 }
277 return c.prov.badge == BadgeBoth || c.prov.badge == BadgeBeaconOnly
278}
279
280func isBurned(c *collection, commit string) bool {
281 for _, b := range c.burnedCommits {
282 if b == commit {
283 return true
284 }
285 }
286 return false
287}
288
289// Provenance is the whole record as JSON; secrets and salt stay "" until
290// revealed / final.
291func Provenance(cid string) string {
292 c := mustCollection(cid)
293 w := &safe.JSONWriter{}
294 w.BeginObject()
295 w.StrField("cid", c.id)
296 w.StrField("chainId", runtime.ChainID())
297 w.StrField("state", stateAt(c, height()).String())
298 w.IntField("minted", c.minted())
299 w.IntField("reserved", c.reserved)
300 if p := c.prov; p != nil {
301 w.StrField("mode", modeName(c.mode))
302 w.BoolField("useBeacon", c.useBeacon)
303 w.StrField("beaconSource", c.beaconSource)
304 w.IntField("round", p.round)
305 w.IntField("roundHeight", p.roundHeight)
306 w.StrField("creatorCommit", p.creatorCommit)
307 w.StrField("beaconCommit", p.beaconCommit)
308 w.StrField("entropy", p.acc)
309 w.StrField("creatorSecret", p.creatorSecret)
310 w.StrField("beaconSecret", p.beaconSecret)
311 w.StrField("salt", p.salt)
312 w.StrField("badge", p.badge)
313 w.IntField("revealDeadline", revealDeadline(c))
314 w.IntField("finalHeight", p.finalHeight)
315 w.BoolField("verified", VerifyProvenance(cid))
316 w.BoolField("fair", FairLaunch(cid))
317 }
318 w.EndObject()
319 return w.String()
320}
321
322// Salt is cid's final salt (hex), "" before Final (M-5: the upload adapter
323// and other readers no longer parse Provenance JSON).
324func Salt(cid string) string {
325 c := mustCollection(cid)
326 if c.state != types.StateFinal {
327 return ""
328 }
329 return c.prov.salt
330}