provenance.gno
9.51 Kb · 273 lines
1package core
2
3import (
4 "chain"
5 "chain/runtime"
6
7 "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia/safe/v1"
8 "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia/types/v1"
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// A collection launched without the beacon (only possible while no beacon
35// is configured, SA-M2) gives the creator the whole choice of the map; it
36// can have no creator reserve and is never reported as a fair launch
37// (FairLaunch, Provenance "fair").
38type provenance struct {
39 creatorCommit, beaconCommit string
40 acc string
41 creatorSecret, beaconSecret string
42 creatorRevealed bool
43 beaconRevealed bool
44 salt, badge string
45 finalHeight int64
46 round int64 // randomness provider round bound by RequestRandomness (0 = none)
47}
48
49const (
50 roleCreator = "creator"
51 roleBeacon = "beacon"
52)
53
54// BeaconCommit is the beacon's commitment, before any token exists.
55func BeaconCommit(cur realm, cid, commitHex string) {
56 assertNoDeposit(cur)
57 c := mustCollection(cid)
58 if beacon == "" || caller(cur) != beacon {
59 panic("only the configured beacon can commit")
60 }
61 if c.state == types.StateDraft || c.prov == nil || !c.useBeacon || usesProvider(c) {
62 panic("this collection is not waiting for a beacon")
63 }
64 if c.prov.beaconCommit != "" {
65 panic("the beacon already committed")
66 }
67 if c.minted() > 0 {
68 panic("too late: tokens were minted before the beacon committed")
69 }
70 commit := safe.AssertHex("commit", commitHex, 32)
71 if isBurned(c, commit) {
72 panic("this commitment's secret was already revealed for this collection")
73 }
74 c.prov.beaconCommit = commit
75 chain.Emit("BeaconCommitted", "cid", cid, "commit", commit)
76}
77
78func RevealCreatorSecret(cur realm, cid, secret string) {
79 assertNoDeposit(cur)
80 c := mustCollection(cid)
81 assertCreator(cur, c)
82 h := height()
83 assertRevealOpen(c, h)
84 if c.prov.creatorRevealed {
85 panic("the creator secret is already revealed")
86 }
87 if c.useBeacon {
88 // SA-M1: once the grace period starts only the beacon may reveal, so
89 // the creator can never reveal after seeing sb.
90 if c.prov.beaconRevealed {
91 panic("the beacon revealed first; the outcome is fixed")
92 }
93 if h >= revealDeadline(c)-BeaconGraceBlocks {
94 panic("the creator's reveal window ended at height " + itoa(revealDeadline(c)-BeaconGraceBlocks-1) +
95 "; only the beacon may reveal now")
96 }
97 }
98 assertSecret(c, roleCreator, secret, c.prov.creatorCommit)
99 c.prov.creatorSecret = secret
100 c.prov.creatorRevealed = true
101 chain.Emit("CreatorRevealed", "cid", cid, "secret", secret)
102 if revealsComplete(c) {
103 finalize(c, h)
104 }
105}
106
107func RevealBeaconSecret(cur realm, cid, secret string) {
108 assertNoDeposit(cur)
109 c := mustCollection(cid)
110 if beacon == "" || caller(cur) != beacon {
111 panic("only the configured beacon can reveal")
112 }
113 h := height()
114 assertRevealOpen(c, h)
115 if !c.useBeacon || usesProvider(c) || c.prov.beaconCommit == "" {
116 panic("this collection has no beacon commitment")
117 }
118 if c.prov.beaconRevealed {
119 panic("the beacon secret is already revealed")
120 }
121 if !c.prov.creatorRevealed && h < revealDeadline(c)-BeaconGraceBlocks {
122 panic("the beacon reveals after the creator, or in the window's last " + itoa(BeaconGraceBlocks) + " blocks")
123 }
124 assertSecret(c, roleBeacon, secret, c.prov.beaconCommit)
125 c.prov.beaconSecret = secret
126 c.prov.beaconRevealed = true
127 chain.Emit("BeaconRevealed", "cid", cid, "secret", secret)
128 // After the creator: both secrets are in (2/2). First, in the grace
129 // period: the creator can no longer reveal, so the map is fixed now
130 // (beacon-only) — nobody is left holding a choice (SA-M1).
131 finalize(c, h)
132}
133
134// Finalize is permissionless and never paused (I-2): immediately once every
135// expected secret is revealed, otherwise after revealDeadline (fallback).
136func Finalize(cur realm, cid string) {
137 assertNoDeposit(cur)
138 c := mustCollection(cid)
139 h := height()
140 refresh(c, h)
141 if c.state != types.StateClosed {
142 panic("only a closed collection can be finalized (state " + c.state.String() + ")")
143 }
144 if !revealsComplete(c) && h <= revealDeadline(c) {
145 panic("the reveal window is open until height " + itoa(revealDeadline(c)))
146 }
147 // SA-I9: a sale that minted nothing stays Closed, so its creator can
148 // still abort and relaunch; nobody else can lock it into Final.
149 if c.minted() == 0 {
150 panic("nothing was minted; the creator may abort and relaunch instead")
151 }
152 finalize(c, h)
153}
154
155func assertRevealOpen(c *collection, h int64) {
156 refresh(c, h)
157 if c.state != types.StateClosed {
158 panic("secrets are accepted only after the sale closed (state " + c.state.String() + ")")
159 }
160 if h > revealDeadline(c) {
161 panic("the reveal window closed; call Finalize")
162 }
163}
164
165func assertSecret(c *collection, role, secret, commit string) {
166 if len(secret) < MinSecretLen || len(secret) > MaxSecretLen {
167 panic("secret must be " + itoa(MinSecretLen) + ".." + itoa(MaxSecretLen) + " bytes")
168 }
169 safe.AssertText("secret", secret, MaxSecretLen)
170 if safe.Commit(runtime.ChainID(), c.id, role, secret) != commit {
171 panic("secret does not match the " + role + " commitment")
172 }
173}
174
175func revealsComplete(c *collection) bool {
176 return c.prov.creatorRevealed && (!c.useBeacon || c.prov.beaconRevealed)
177}
178
179// finalize runs at most once per collection (I-10): every caller checks
180// state Closed, and this sets Final.
181func finalize(c *collection, h int64) {
182 p := c.prov
183 p.salt = safe.Salt(c.id, p.creatorSecret, p.beaconSecret, p.acc)
184 switch {
185 case p.creatorRevealed && p.beaconRevealed:
186 p.badge = BadgeBoth
187 case p.creatorRevealed:
188 p.badge = BadgeCreatorOnly
189 case p.beaconRevealed:
190 p.badge = BadgeBeaconOnly
191 default:
192 p.badge = BadgeFallback
193 }
194 p.finalHeight = h
195 c.state = types.StateFinal
196 c.st.final = true
197 chain.Emit("CollectionFinal", "cid", c.id, "salt", p.salt, "badge", p.badge)
198}
199
200// VerifyProvenance recomputes every commitment and the salt from public
201// data. True only for a Final collection whose record is consistent.
202func VerifyProvenance(cid string) bool {
203 c := mustCollection(cid)
204 if c.state != types.StateFinal {
205 return false
206 }
207 p := c.prov
208 chainID := runtime.ChainID()
209 if p.creatorRevealed && safe.Commit(chainID, c.id, roleCreator, p.creatorSecret) != p.creatorCommit {
210 return false
211 }
212 if p.beaconRevealed && !usesProvider(c) && safe.Commit(chainID, c.id, roleBeacon, p.beaconSecret) != p.beaconCommit {
213 return false
214 }
215 // A provider's value was checked by FulfillRandomness against the
216 // provider's verified round (p.round); it is recomputable off chain
217 // from the provider's public source (e.g. the drand round).
218 return p.salt == safe.Salt(c.id, p.creatorSecret, p.beaconSecret, p.acc)
219}
220
221// FairLaunch is true only for a Final, consistent collection whose map no
222// single party could choose: launched with the beacon and finalized with
223// both secrets or with the beacon's alone (after the creator stayed
224// silent). Creator-only collections (no beacon: the creator alone knew the
225// only secret and could steer the last mint, SA-M2) and fallbacks are not
226// fair launches, even when VerifyProvenance is true.
227func FairLaunch(cid string) bool {
228 c := mustCollection(cid)
229 if !VerifyProvenance(cid) || !c.useBeacon {
230 return false
231 }
232 return c.prov.badge == BadgeBoth || c.prov.badge == BadgeBeaconOnly
233}
234
235func isBurned(c *collection, commit string) bool {
236 for _, b := range c.burnedCommits {
237 if b == commit {
238 return true
239 }
240 }
241 return false
242}
243
244// Provenance is the whole record as JSON; secrets and salt stay "" until
245// revealed / final.
246func Provenance(cid string) string {
247 c := mustCollection(cid)
248 w := &safe.JSONWriter{}
249 w.BeginObject()
250 w.StrField("cid", c.id)
251 w.StrField("chainId", runtime.ChainID())
252 w.StrField("state", stateAt(c, height()).String())
253 w.IntField("minted", c.minted())
254 w.IntField("reserved", c.reserved)
255 if p := c.prov; p != nil {
256 w.BoolField("useBeacon", c.useBeacon)
257 w.StrField("beaconSource", c.beaconSource)
258 w.IntField("round", p.round)
259 w.StrField("creatorCommit", p.creatorCommit)
260 w.StrField("beaconCommit", p.beaconCommit)
261 w.StrField("entropy", p.acc)
262 w.StrField("creatorSecret", p.creatorSecret)
263 w.StrField("beaconSecret", p.beaconSecret)
264 w.StrField("salt", p.salt)
265 w.StrField("badge", p.badge)
266 w.IntField("revealDeadline", revealDeadline(c))
267 w.IntField("finalHeight", p.finalHeight)
268 w.BoolField("verified", VerifyProvenance(cid))
269 w.BoolField("fair", FairLaunch(cid))
270 }
271 w.EndObject()
272 return w.String()
273}