media.gno
8.27 Kb · 248 lines
1// Package media is the pure half of Sinopia's uploaded collections, version
2// 2 (docs/uploaded-collections.md §5.1, §0a; mainnet plan §2.1, §3.2 WP-F):
3// storage kinds and media types, the typed views of a store (Artwork,
4// StoreInfo), the storage badge and the salt-keyed permutation that assigns
5// tokens to artworks. It has no state.
6//
7// v2 against v1: the image header checks and the CIDv1 grammar moved into
8// p/safe/v2 (core validates engine media with them, I-15). The functions of
9// the same name here delegate to safe/v2, so the asset store
10// (r/…/assets/v1), the upload engine (r/…/engines/upload) and core apply
11// one implementation. Everything else is v1's code unchanged; the
12// permutation keeps its domain "art|upload|perm|v1|" (same algorithm, same
13// golden vectors). Frontend twins: chain/perm.ts (Permute, PermKey, Slot),
14// chain/cid.ts (CIDv1) and checkImage in chain/upload.ts, pinned by
15// docs/test-vectors/upload-v2.json (docs/test-vectors/gen-upload-v2.py).
16package media
17
18import (
19 "errors"
20 "strconv"
21
22 "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/safe/v2"
23)
24
25// Storage kinds of one artwork.
26const (
27 KindChain = 1 // bytes stored in assets/v1
28 KindIPFSRaw = 2 // raw-leaf CIDv1: the CID's digest is the file's sha256 (checked on chain)
29 KindIPFSUnixFS = 3 // dag-pb CIDv1: digest of the root node, file sha256 stored separately
30)
31
32// Media types (the record byte of assets/v1). GIF is defined for
33// completeness but refused by ParseMime (decision D5: svg, png, webp, jpeg).
34const (
35 MimeSVG = 1
36 MimePNG = 2
37 MimeWebP = 3
38 MimeJPEG = 4
39 MimeGIF = 5
40)
41
42// Limits (decision D4 and design §4).
43const (
44 MaxDim = safe.MaxDim // 4096 px per side, decompression-bomb guard
45 MaxSVGBytes = safe.MaxSVGImageBytes // 16,384: on-chain SVG (= engine/v2 MaxMediaSVGBytes)
46 MaxRasterBytes = 262144 // on-chain png/webp/jpeg per artwork (= engine/v2 MaxMediaBytes)
47 MaxIPFSRaw = 1 << 20 // single raw block
48 MaxIPFSUnixFS = 50 << 20 // declared; the frontend enforces it while streaming
49)
50
51// CID codecs (safe/v2).
52const (
53 CodecRaw = safe.CodecRaw
54 CodecDagPB = safe.CodecDagPB
55)
56
57// IPFSScheme prefixes an IPFS URI ("ipfs://<CIDv1>").
58const IPFSScheme = safe.IPFSScheme
59
60// PermDomain separates the permutation key from every other platform hash
61// (under safe.Domain "art"). Unchanged from v1: same algorithm, same keys.
62const PermDomain = "art|upload|perm|v1|"
63
64// Artwork is one record of a store as plain data. It crosses realm
65// boundaries as a copy and never points into store state.
66type Artwork struct {
67 Index int64
68 Kind, Mime int
69 Size, Have, Editions, SlotEnd int64
70 SHA256, Digest, CID, Title, Tier string
71 Traits []string // one value per store trait type ("" = none)
72 Complete bool
73 // JSON escaped once by the store (filled by assets/v1 reads, "" otherwise):
74 // TierJSON is the tier name as a JSON string ("" = no tier); TraitsJSON the
75 // creator traits as comma-joined {"trait_type":…,"value":…} items in store
76 // order ("" = none). Lets token reads avoid per-call escaping (review UP-M1).
77 TierJSON, TraitsJSON string
78}
79
80// SlotStart is the first slot of the artwork's range [SlotStart, SlotEnd).
81func (a Artwork) SlotStart() int64 { return a.SlotEnd - a.Editions }
82
83// MimeType is the artwork's IANA media type.
84func (a Artwork) MimeType() string { return MimeType(a.Mime) }
85
86// OnChain reports whether the artwork's bytes live in the store.
87func (a Artwork) OnChain() bool { return a.Kind == KindChain }
88
89// URI is "ipfs://<cid>" for an IPFS artwork, "" for on-chain bytes.
90func (a Artwork) URI() string {
91 if a.Kind == KindChain || a.CID == "" {
92 return ""
93 }
94 return IPFSScheme + a.CID
95}
96
97// MimeVerified reports whether the chain checked the file against its
98// declared type (UP-I6): on-chain bytes passed CheckImage when their last
99// chunk arrived; an IPFS file's type is the creator's claim (the chain never
100// sees the bytes; a raw CID binds the bytes, not the type).
101func (a Artwork) MimeVerified() bool { return a.Kind == KindChain && a.Complete }
102
103// StoreInfo is a store's header as plain data.
104type StoreInfo struct {
105 ID string
106 Owner address
107 Label string
108 Sealed bool
109 SealedHeight int64
110 Artworks, Slots, OnchainN, IPFSN, OnchainBytes int64
111 TraitTypes, Tiers []string
112}
113
114// HasEditions reports whether some artwork of the store has more than one
115// edition (slots > artworks).
116func (s StoreInfo) HasEditions() bool { return s.Slots > s.Artworks }
117
118// Badge is the store's storage badge (see Badge).
119func (s StoreInfo) Badge() string { return Badge(s.OnchainN, s.IPFSN) }
120
121// MimeType is the IANA type of a media constant ("" for an unknown one).
122func MimeType(m int) string {
123 switch m {
124 case MimeSVG:
125 return safe.MimeSVG
126 case MimePNG:
127 return safe.MimePNG
128 case MimeWebP:
129 return safe.MimeWebP
130 case MimeJPEG:
131 return safe.MimeJPEG
132 case MimeGIF:
133 return safe.MimeGIF
134 }
135 return ""
136}
137
138// MimeName is the short name ParseMime accepts ("svg", "png", …).
139func MimeName(m int) string {
140 switch m {
141 case MimeSVG:
142 return "svg"
143 case MimePNG:
144 return "png"
145 case MimeWebP:
146 return "webp"
147 case MimeJPEG:
148 return "jpeg"
149 case MimeGIF:
150 return "gif"
151 }
152 return ""
153}
154
155// ParseMime maps "svg" | "png" | "webp" | "jpeg" to its constant and panics
156// on anything else, "gif" included (D5).
157func ParseMime(s string) int {
158 switch s {
159 case "svg":
160 return MimeSVG
161 case "png":
162 return MimePNG
163 case "webp":
164 return MimeWebP
165 case "jpeg":
166 return MimeJPEG
167 case "gif":
168 panic("mime: gif is not supported (svg, png, webp, jpeg)")
169 }
170 panic("mime: must be svg, png, webp or jpeg, got " + strconv.Quote(s))
171}
172
173// KindName is "chain" or "ipfs".
174func KindName(k int) string {
175 switch k {
176 case KindChain:
177 return "chain"
178 case KindIPFSRaw, KindIPFSUnixFS:
179 return "ipfs"
180 }
181 return ""
182}
183
184// MaxBytes is the size cap of one artwork of this kind and type.
185func MaxBytes(kind, mime int) int64 {
186 switch kind {
187 case KindChain:
188 if mime == MimeSVG {
189 return MaxSVGBytes
190 }
191 return MaxRasterBytes
192 case KindIPFSRaw:
193 return MaxIPFSRaw
194 case KindIPFSUnixFS:
195 return MaxIPFSUnixFS
196 }
197 return 0
198}
199
200// Badge is a store's storage badge from its counters (design §9):
201// "onchain", "ipfs", "mixed", or "empty" for a store without artworks.
202func Badge(onchainN, ipfsN int64) string {
203 switch {
204 case onchainN == 0 && ipfsN == 0:
205 return "empty"
206 case ipfsN == 0:
207 return "onchain"
208 case onchainN == 0:
209 return "ipfs"
210 }
211 return "mixed"
212}
213
214// --- delegated to safe/v2 (one implementation for store, adapter and core) ---
215
216// CheckImage checks a complete file against its declared type (v1
217// signature): SVG must pass safe.AssertSVG with MaxSVGBytes (w = h = 0); a
218// raster file must carry its type's magic bytes and header dimensions of
219// 1..MaxDim per side (safe.ImageDims). Error texts are v1's.
220func CheckImage(mime int, data string) (w, h int, err error) {
221 switch mime {
222 case MimeSVG:
223 if err := safe.AssertSVG(data, MaxSVGBytes); err != nil {
224 return 0, 0, err
225 }
226 return 0, 0, nil
227 case MimePNG, MimeWebP, MimeJPEG, MimeGIF:
228 return safe.ImageDims(MimeType(mime), data)
229 }
230 return 0, 0, errors.New("media: unknown media type " + strconv.Itoa(mime))
231}
232
233// ParseCIDv1 returns the codec (CodecRaw or CodecDagPB) and the sha2-256
234// digest as lower-case hex (safe/v2 grammar, unchanged from media/v1).
235func ParseCIDv1(s string) (codec int, digestHex string, err error) { return safe.ParseCIDv1(s) }
236
237// CIDFromDigest is the inverse of ParseCIDv1; it panics on an unsupported
238// codec or a digest that is not 64 lower-case hex characters.
239func CIDFromDigest(codec int, digestHex string) string { return safe.CIDFromDigest(codec, digestHex) }
240
241// IsIPFSURI reports whether s is "ipfs://" + a CIDv1 ParseCIDv1 accepts.
242func IsIPFSURI(s string) bool { return safe.IsIPFSURI(s) }
243
244// IsHex32 reports whether s is exactly 64 lower-case hex characters.
245func IsHex32(s string) bool { return safe.IsHex32(s) }
246
247// Raw32 decodes exactly 64 lower-case hex characters into 32 raw bytes.
248func Raw32(s string) (string, bool) { return safe.Raw32(s) }