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.

Playground

gnomod.toml

module = "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/media/v2"
gno = "0.9"

[addpkg]
  creator = "g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06"
  height = 1450553
  max_deposit = "30000000ugnot"

media.gno

// Package media is the pure half of Sinopia's uploaded collections, version
// 2 (docs/uploaded-collections.md §5.1, §0a; mainnet plan §2.1, §3.2 WP-F):
// storage kinds and media types, the typed views of a store (Artwork,
// StoreInfo), the storage badge and the salt-keyed permutation that assigns
// tokens to artworks. It has no state.
//
// v2 against v1: the image header checks and the CIDv1 grammar moved into
// p/safe/v2 (core validates engine media with them, I-15). The functions of
// the same name here delegate to safe/v2, so the asset store
// (r/…/assets/v1), the upload engine (r/…/engines/upload) and core apply
// one implementation. Everything else is v1's code unchanged; the
// permutation keeps its domain "art|upload|perm|v1|" (same algorithm, same
// golden vectors). Frontend twins: chain/perm.ts (Permute, PermKey, Slot),
// chain/cid.ts (CIDv1) and checkImage in chain/upload.ts, pinned by
// docs/test-vectors/upload-v2.json (docs/test-vectors/gen-upload-v2.py).
package media

import (
	"errors"
	"strconv"

	"gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/safe/v2"
)

// Storage kinds of one artwork.
const (
	KindChain      = 1 // bytes stored in assets/v1
	KindIPFSRaw    = 2 // raw-leaf CIDv1: the CID's digest is the file's sha256 (checked on chain)
	KindIPFSUnixFS = 3 // dag-pb CIDv1: digest of the root node, file sha256 stored separately
)

// Media types (the record byte of assets/v1). GIF is defined for
// completeness but refused by ParseMime (decision D5: svg, png, webp, jpeg).
const (
	MimeSVG  = 1
	MimePNG  = 2
	MimeWebP = 3
	MimeJPEG = 4
	MimeGIF  = 5
)

// Limits (decision D4 and design §4).
const (
	MaxDim         = safe.MaxDim           // 4096 px per side, decompression-bomb guard
	MaxSVGBytes    = safe.MaxSVGImageBytes // 16,384: on-chain SVG (= engine/v2 MaxMediaSVGBytes)
	MaxRasterBytes = 262144                // on-chain png/webp/jpeg per artwork (= engine/v2 MaxMediaBytes)
	MaxIPFSRaw     = 1 << 20               // single raw block
	MaxIPFSUnixFS  = 50 << 20              // declared; the frontend enforces it while streaming
)

// CID codecs (safe/v2).
const (
	CodecRaw   = safe.CodecRaw
	CodecDagPB = safe.CodecDagPB
)

// IPFSScheme prefixes an IPFS URI ("ipfs://<CIDv1>").
const IPFSScheme = safe.IPFSScheme

// PermDomain separates the permutation key from every other platform hash
// (under safe.Domain "art"). Unchanged from v1: same algorithm, same keys.
const PermDomain = "art|upload|perm|v1|"

// Artwork is one record of a store as plain data. It crosses realm
// boundaries as a copy and never points into store state.
type Artwork struct {
	Index                            int64
	Kind, Mime                       int
	Size, Have, Editions, SlotEnd    int64
	SHA256, Digest, CID, Title, Tier string
	Traits                           []string // one value per store trait type ("" = none)
	Complete                         bool
	// JSON escaped once by the store (filled by assets/v1 reads, "" otherwise):
	// TierJSON is the tier name as a JSON string ("" = no tier); TraitsJSON the
	// creator traits as comma-joined {"trait_type":…,"value":…} items in store
	// order ("" = none). Lets token reads avoid per-call escaping (review UP-M1).
	TierJSON, TraitsJSON string
}

// SlotStart is the first slot of the artwork's range [SlotStart, SlotEnd).
func (a Artwork) SlotStart() int64 { return a.SlotEnd - a.Editions }

// MimeType is the artwork's IANA media type.
func (a Artwork) MimeType() string { return MimeType(a.Mime) }

// OnChain reports whether the artwork's bytes live in the store.
func (a Artwork) OnChain() bool { return a.Kind == KindChain }

// URI is "ipfs://<cid>" for an IPFS artwork, "" for on-chain bytes.
func (a Artwork) URI() string {
	if a.Kind == KindChain || a.CID == "" {
		return ""
	}
	return IPFSScheme + a.CID
}

// MimeVerified reports whether the chain checked the file against its
// declared type (UP-I6): on-chain bytes passed CheckImage when their last
// chunk arrived; an IPFS file's type is the creator's claim (the chain never
// sees the bytes; a raw CID binds the bytes, not the type).
func (a Artwork) MimeVerified() bool { return a.Kind == KindChain && a.Complete }

// StoreInfo is a store's header as plain data.
type StoreInfo struct {
	ID                                             string
	Owner                                          address
	Label                                          string
	Sealed                                         bool
	SealedHeight                                   int64
	Artworks, Slots, OnchainN, IPFSN, OnchainBytes int64
	TraitTypes, Tiers                              []string
}

// HasEditions reports whether some artwork of the store has more than one
// edition (slots > artworks).
func (s StoreInfo) HasEditions() bool { return s.Slots > s.Artworks }

// Badge is the store's storage badge (see Badge).
func (s StoreInfo) Badge() string { return Badge(s.OnchainN, s.IPFSN) }

// MimeType is the IANA type of a media constant ("" for an unknown one).
func MimeType(m int) string {
	switch m {
	case MimeSVG:
		return safe.MimeSVG
	case MimePNG:
		return safe.MimePNG
	case MimeWebP:
		return safe.MimeWebP
	case MimeJPEG:
		return safe.MimeJPEG
	case MimeGIF:
		return safe.MimeGIF
	}
	return ""
}

// MimeName is the short name ParseMime accepts ("svg", "png", …).
func MimeName(m int) string {
	switch m {
	case MimeSVG:
		return "svg"
	case MimePNG:
		return "png"
	case MimeWebP:
		return "webp"
	case MimeJPEG:
		return "jpeg"
	case MimeGIF:
		return "gif"
	}
	return ""
}

// ParseMime maps "svg" | "png" | "webp" | "jpeg" to its constant and panics
// on anything else, "gif" included (D5).
func ParseMime(s string) int {
	switch s {
	case "svg":
		return MimeSVG
	case "png":
		return MimePNG
	case "webp":
		return MimeWebP
	case "jpeg":
		return MimeJPEG
	case "gif":
		panic("mime: gif is not supported (svg, png, webp, jpeg)")
	}
	panic("mime: must be svg, png, webp or jpeg, got " + strconv.Quote(s))
}

// KindName is "chain" or "ipfs".
func KindName(k int) string {
	switch k {
	case KindChain:
		return "chain"
	case KindIPFSRaw, KindIPFSUnixFS:
		return "ipfs"
	}
	return ""
}

// MaxBytes is the size cap of one artwork of this kind and type.
func MaxBytes(kind, mime int) int64 {
	switch kind {
	case KindChain:
		if mime == MimeSVG {
			return MaxSVGBytes
		}
		return MaxRasterBytes
	case KindIPFSRaw:
		return MaxIPFSRaw
	case KindIPFSUnixFS:
		return MaxIPFSUnixFS
	}
	return 0
}

// Badge is a store's storage badge from its counters (design §9):
// "onchain", "ipfs", "mixed", or "empty" for a store without artworks.
func Badge(onchainN, ipfsN int64) string {
	switch {
	case onchainN == 0 && ipfsN == 0:
		return "empty"
	case ipfsN == 0:
		return "onchain"
	case onchainN == 0:
		return "ipfs"
	}
	return "mixed"
}

// --- delegated to safe/v2 (one implementation for store, adapter and core) ---

// CheckImage checks a complete file against its declared type (v1
// signature): SVG must pass safe.AssertSVG with MaxSVGBytes (w = h = 0); a
// raster file must carry its type's magic bytes and header dimensions of
// 1..MaxDim per side (safe.ImageDims). Error texts are v1's.
func CheckImage(mime int, data string) (w, h int, err error) {
	switch mime {
	case MimeSVG:
		if err := safe.AssertSVG(data, MaxSVGBytes); err != nil {
			return 0, 0, err
		}
		return 0, 0, nil
	case MimePNG, MimeWebP, MimeJPEG, MimeGIF:
		return safe.ImageDims(MimeType(mime), data)
	}
	return 0, 0, errors.New("media: unknown media type " + strconv.Itoa(mime))
}

// ParseCIDv1 returns the codec (CodecRaw or CodecDagPB) and the sha2-256
// digest as lower-case hex (safe/v2 grammar, unchanged from media/v1).
func ParseCIDv1(s string) (codec int, digestHex string, err error) { return safe.ParseCIDv1(s) }

// CIDFromDigest is the inverse of ParseCIDv1; it panics on an unsupported
// codec or a digest that is not 64 lower-case hex characters.
func CIDFromDigest(codec int, digestHex string) string { return safe.CIDFromDigest(codec, digestHex) }

// IsIPFSURI reports whether s is "ipfs://" + a CIDv1 ParseCIDv1 accepts.
func IsIPFSURI(s string) bool { return safe.IsIPFSURI(s) }

// IsHex32 reports whether s is exactly 64 lower-case hex characters.
func IsHex32(s string) bool { return safe.IsHex32(s) }

// Raw32 decodes exactly 64 lower-case hex characters into 32 raw bytes.
func Raw32(s string) (string, bool) { return safe.Raw32(s) }

perm.gno

package media

import (
	"crypto/sha256"
	"strconv"

	"gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/safe/v2"
)

// Token → artwork assignment (design §7; frontend chain/perm.ts is the twin; unchanged from media/v1).
//
//	K         = sha256hex(PermDomain + collectionID + "|" + salt)
//	slot(tid) = Permute(K, slots, tid-1)    (single-artwork store: tid-1)
//
// The salt exists only once the collection is Final (commit–reveal v2), so
// nobody, the creator included, knows any token's slot before every token
// was sold and both secrets were revealed.

// PermKey is the permutation key of collection cid with its final salt.
func PermKey(cid, salt string) string {
	return safe.Sha256Hex(PermDomain + cid + "|" + salt)
}

// Permute is a bijection on [0, n): a 4-round Feistel network over b bits
// (b = bitlen(n-1) rounded up to an even number ≥ 2, so 2^b < 4n) with cycle
// walking. Round function: F = big-endian uint64 of the first 8 bytes of
// sha256(keyHex + "|" + r + "|" + R), masked to h = b/2 bits, with r and R
// in decimal. It panics unless 1 ≤ n and 0 ≤ x < n.
func Permute(keyHex string, n, x int64) int64 {
	if n < 1 || x < 0 || x >= n {
		panic("media: permute: need 0 <= x < n")
	}
	if n == 1 {
		return 0
	}
	b := bitLen(uint64(n - 1))
	if b < 2 {
		b = 2
	}
	if b%2 == 1 {
		b++
	}
	h := uint(b / 2)
	mask := uint64(1)<<h - 1
	prefix := keyHex + "|"
	v := uint64(x)
	// The walk stays on x's cycle, which contains x itself (< n), so it
	// ends; the bound only guards against a broken hash.
	for walk := 0; walk < 1<<uint(b); walk++ {
		l, r := v>>h, v&mask
		for round := 0; round < 4; round++ {
			f := roundF(prefix+strconv.Itoa(round)+"|", r) & mask
			l, r = r, l^f
		}
		v = l<<h | r
		if v < uint64(n) {
			return int64(v)
		}
	}
	panic("media: permute: cycle walk did not terminate")
}

func roundF(prefix string, r uint64) uint64 {
	sum := sha256.Sum256([]byte(prefix + strconv.FormatUint(r, 10)))
	return uint64(sum[0])<<56 | uint64(sum[1])<<48 | uint64(sum[2])<<40 | uint64(sum[3])<<32 |
		uint64(sum[4])<<24 | uint64(sum[5])<<16 | uint64(sum[6])<<8 | uint64(sum[7])
}

func bitLen(v uint64) int {
	n := 0
	for v > 0 {
		n++
		v >>= 1
	}
	return n
}

// Slot is the slot of token tid (1-based) in a store of `artworks`
// artworks and `slots` slots: the identity tid-1 for a single-artwork store
// (1/1 and editions: "Edition 3 of 50"), Permute(K, slots, tid-1) otherwise.
func Slot(keyHex string, artworks, slots, tid int64) int64 {
	if tid < 1 || tid > slots {
		panic("media: slot: token id out of range")
	}
	if artworks == 1 {
		return tid - 1
	}
	return Permute(keyHex, slots, tid-1)
}