media.gno
4.85 Kb · 174 lines
1// Package media is the pure half of Sinopia's uploaded collections
2// (docs/uploaded-collections.md §5.1, §0a): media kinds and types, the CIDv1
3// grammar the platform accepts, image header checks, and the salt-keyed
4// permutation that assigns tokens to artworks. It has no state; the asset
5// store (r/…/assets/v1), the upload engine (r/…/engines/upload) and the
6// frontend (chain/cid.ts, chain/perm.ts) share these definitions, pinned by
7// the golden vectors in docs/test-vectors/upload-v1.json.
8package media
9
10import (
11 "errors"
12 "strconv"
13)
14
15// Storage kinds of one artwork.
16const (
17 KindChain = 1 // bytes stored in assets/v1
18 KindIPFSRaw = 2 // raw-leaf CIDv1: the CID's digest is the file's sha256 (checked on chain)
19 KindIPFSUnixFS = 3 // dag-pb CIDv1: digest of the root node, file sha256 stored separately
20)
21
22// Media types. GIF is defined for completeness but refused by ParseMime
23// (decision D5: svg, png, webp, jpeg only).
24const (
25 MimeSVG = 1
26 MimePNG = 2
27 MimeWebP = 3
28 MimeJPEG = 4
29 MimeGIF = 5
30)
31
32// Limits (decision D4 and design §4).
33const (
34 MaxDim = 4096 // px per side, decompression-bomb guard
35 MaxSVGBytes = 16384 // on-chain SVG: AssertSVG costs ~30-50k gas/B (WP0)
36 MaxRasterBytes = 262144 // on-chain png/webp/jpeg per artwork
37 MaxIPFSRaw = 1 << 20 // single raw block
38 MaxIPFSUnixFS = 50 << 20 // declared; the frontend enforces it while streaming
39)
40
41// CID codecs.
42const (
43 CodecRaw = 0x55
44 CodecDagPB = 0x70
45)
46
47// PermDomain separates the permutation key from every other platform hash
48// (under safe.Domain "art").
49const PermDomain = "art|upload|perm|v1|"
50
51// Artwork is one record of a store as plain data. It crosses realm
52// boundaries as a copy and never points into store state.
53type Artwork struct {
54 Index int64
55 Kind, Mime int
56 Size, Have, Editions, SlotEnd int64
57 SHA256, Digest, CID, Title, Tier string
58 Traits []string // one value per store trait type ("" = none)
59 Complete bool
60 // JSON escaped once by the store (filled by assets/v1 reads, "" otherwise):
61 // TierJSON is the tier name as a JSON string ("" = no tier); TraitsJSON the
62 // creator traits as comma-joined {"trait_type":…,"value":…} items in store
63 // order ("" = none). Lets token reads avoid per-call escaping (review UP-M1).
64 TierJSON, TraitsJSON string
65}
66
67// SlotStart is the first slot of the artwork's range [SlotStart, SlotEnd).
68func (a Artwork) SlotStart() int64 { return a.SlotEnd - a.Editions }
69
70// StoreInfo is a store's header as plain data.
71type StoreInfo struct {
72 ID string
73 Owner address
74 Label string
75 Sealed bool
76 SealedHeight int64
77 Artworks, Slots, OnchainN, IPFSN, OnchainBytes int64
78 TraitTypes, Tiers []string
79}
80
81// MimeType is the IANA type of a media constant ("" for an unknown one).
82func MimeType(m int) string {
83 switch m {
84 case MimeSVG:
85 return "image/svg+xml"
86 case MimePNG:
87 return "image/png"
88 case MimeWebP:
89 return "image/webp"
90 case MimeJPEG:
91 return "image/jpeg"
92 case MimeGIF:
93 return "image/gif"
94 }
95 return ""
96}
97
98// MimeName is the short name ParseMime accepts ("svg", "png", …).
99func MimeName(m int) string {
100 switch m {
101 case MimeSVG:
102 return "svg"
103 case MimePNG:
104 return "png"
105 case MimeWebP:
106 return "webp"
107 case MimeJPEG:
108 return "jpeg"
109 case MimeGIF:
110 return "gif"
111 }
112 return ""
113}
114
115// ParseMime maps "svg" | "png" | "webp" | "jpeg" to its constant and panics
116// on anything else, "gif" included (D5).
117func ParseMime(s string) int {
118 switch s {
119 case "svg":
120 return MimeSVG
121 case "png":
122 return MimePNG
123 case "webp":
124 return MimeWebP
125 case "jpeg":
126 return MimeJPEG
127 case "gif":
128 panic("mime: gif is not supported (svg, png, webp, jpeg)")
129 }
130 panic("mime: must be svg, png, webp or jpeg, got " + strconv.Quote(s))
131}
132
133// KindName is "chain" or "ipfs".
134func KindName(k int) string {
135 switch k {
136 case KindChain:
137 return "chain"
138 case KindIPFSRaw, KindIPFSUnixFS:
139 return "ipfs"
140 }
141 return ""
142}
143
144// MaxBytes is the size cap of one artwork of this kind and type.
145func MaxBytes(kind, mime int) int64 {
146 switch kind {
147 case KindChain:
148 if mime == MimeSVG {
149 return MaxSVGBytes
150 }
151 return MaxRasterBytes
152 case KindIPFSRaw:
153 return MaxIPFSRaw
154 case KindIPFSUnixFS:
155 return MaxIPFSUnixFS
156 }
157 return 0
158}
159
160// Badge is a store's storage badge from its counters (design §9):
161// "onchain", "ipfs", "mixed", or "empty" for a store without artworks.
162func Badge(onchainN, ipfsN int64) string {
163 switch {
164 case onchainN == 0 && ipfsN == 0:
165 return "empty"
166 case ipfsN == 0:
167 return "onchain"
168 case onchainN == 0:
169 return "ipfs"
170 }
171 return "mixed"
172}
173
174func mediaErr(msg string) error { return errors.New("media: " + msg) }