Skip to content Search Apps Documentation Source Content 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.

v1 source pure

Package safe holds the pure checks and formulas every platform realm shares: text and SVG validation, basis-point mat...

Overview

Package safe holds the pure checks and formulas every platform realm shares: text and SVG validation, basis-point math, payee splits, merkle proofs, provenance hashes and the one JSON writer. Nothing here has state.

Constants 4

const AvgBlockMs

1const AvgBlockMs = 3540
source

AvgBlockMs is the measured mainnet average block time (RA §1: 200k blocks in 708,660 s). Use it only for UI estimates and defaults; every on-chain rule is stated in blocks.

const BpsDenominator

1const BpsDenominator = 10000
source

BpsDenominator is 100% in basis points (EIP-2981, royalty/v0).

const Domain

1const Domain = "art"
source

Domain separates every hash this platform commits to from hashes of the same bytes made for any other purpose. It equals the brand slug and changes with it (paths change anyway when the brand is chosen).

const MaxMerkleDepth

1const MaxMerkleDepth = 32
source

MaxMerkleDepth bounds a proof: 2^32 leaves is far beyond any allowlist.

Functions 26

func AllowlistLeaf

1func AllowlistLeaf(cid string, addr address) string
source

AllowlistLeaf is the leaf for addr in collection cid's allowlist tree.

func AssertBps

1func AssertBps(field string, bps, cap int64)
source

AssertBps panics unless 0 <= bps <= cap.

func AssertHex

1func AssertHex(field, s string, nBytes int) string
source

AssertHex checks that s is exactly nBytes of hex and returns it lowercased.

func AssertNonEmptyText

1func AssertNonEmptyText(field, s string, maxLen int)
source

AssertNonEmptyText is AssertText that also refuses "".

func AssertSVG

1func AssertSVG(svg string, maxBytes int) error
source

AssertSVG checks an engine's raw SVG output: size, envelope, the forbidden constructs above, inline event handlers, and any href or url() that does not point inside the document. The frontend shows art only through <img>, which never runs scripts; this check is the defence in depth for every other place a data: URI ends up (RA M-7, design §5.4).

func AssertSymbol

1func AssertSymbol(field, s string)
source

AssertSymbol enforces the grc721 symbol alphabet ([A-Za-z0-9_-], 1..11 bytes) up front, so the error names the field instead of surfacing from grc721.NewToken.

func AssertText

1func AssertText(field, s string, maxLen int)
source

AssertText panics with the field name when CheckText rejects s.

func BlocksFor

1func BlocksFor(seconds int64) int64
source

BlocksFor converts a duration to blocks, rounding up.

func BpsOf

1func BpsOf(amount, bps int64) int64
source

BpsOf returns floor(amount * bps / 10000) without ever forming the full product, so it cannot overflow for any amount >= 0 and 0 <= bps <= 10000.

func CheckText

1func CheckText(s string, maxLen int) error
source

CheckText reports why s may not be stored as user-visible text.

Rejected: invalid UTF-8; C0 and C1 control characters, which include tab, newline and carriage return (the record-injection vector of RA H-1); DEL; U+2028/U+2029, which end a line in older JavaScript parsers (RA L-12); and every bidi control or zero-width character that lets one string render as another.

func Commit

1func Commit(chainID, cid, role, secret string) string
source

Commit is the value a creator ("creator") or the beacon ("beacon") commits to for collection cid on chain chainID.

func FormatPayees

1func FormatPayees(ps []types.Payee) string
source

FormatPayees is the inverse of ParsePayees.

func InitialEntropy

1func InitialEntropy(chainID, cid string) string
source

InitialEntropy seeds a collection's mint-entropy accumulator.

func JSONString

1func JSONString(s string) string
source

JSONString returns s as a quoted JSON string. Beyond the mandatory escapes it also escapes U+2028/U+2029 (RA L-12) and '<', '>', '&', so the output is safe to inline in HTML as well.

func MerkleParent

1func MerkleParent(aHex, bHex string) string
source

MerkleParent is hashPair on hex nodes, for tests and the off-chain builder.

func MerkleVerify

1func MerkleVerify(rootHex, leafHex, proof string) bool
source

MerkleVerify checks a sorted-pair sha256 proof ("hex,hex,…", leaf to root). An empty proof means the tree has a single leaf equal to the root. Malformed input is simply false: proofs come from untrusted callers.

func MixEntropy

1func MixEntropy(acc string, to address, height, first, last int64) string
source

MixEntropy folds one mint call into the accumulator. The inputs are public, but none existed when the secrets were committed, which is what stops a committer from choosing the map their own mints receive.

func PadHex

1func PadHex(n int64, width int) string
source

PadHex renders n in lowercase hex, zero-padded to width.

func PadInt

1func PadInt(n int64, width int) string
source

PadInt renders n with leading zeros to width digits, so decimal ids and prices sort correctly as avl keys. n must be >= 0.

func ParseID

1func ParseID(field, s string) int64
source

ParseID parses a positive decimal id ("1", "42"). Leading zeros, signs and empty strings are refused so every id has exactly one spelling.

func ParsePayees

1func ParsePayees(field, s string, max int) []types.Payee
source

ParsePayees parses "g1…:7000,g1…:3000": 1..max distinct valid addresses, each with a positive share, shares summing to exactly 10000.

func Salt

1func Salt(cid, creatorSecret, beaconSecret, acc string) string
source

Salt binds both secrets ("" when not revealed) to the final accumulator.

func Seed

1func Seed(cid, tid, salt string) string
source

Seed is the per-token engine seed once the salt is final.

func SplitByBps

1func SplitByBps(amount int64, ps []types.Payee) []int64
source

SplitByBps divides amount by the payees' shares. The rounding remainder goes to the first payee, so the parts always sum to exactly amount — the property every "no balance left behind" invariant (I-3, I-4) relies on.

func XMLEscape

1func XMLEscape(s string) string
source

XMLEscape makes s safe as SVG/XML character data or a quoted attribute.

Types 1

type JSONWriter

struct
1type JSONWriter struct {
2	b        strings.Builder
3	hasItems []bool // one entry per open container
4	afterKey bool
5}
source

JSONWriter is the single JSON producer of every read API (design §2.5: reads return primitives or JSON, never positional text). It tracks commas itself, so callers only describe structure:

Example
1w := &safe.JSONWriter{}
2w.BeginObject(); w.Key("id"); w.Str("1"); w.EndObject()

Methods on JSONWriter

func BeginArray

method on JSONWriter
1func (w *JSONWriter) BeginArray()
source

func Bool

method on JSONWriter
1func (w *JSONWriter) Bool(v bool)
source

func BoolField

method on JSONWriter
1func (w *JSONWriter) BoolField(k string, v bool)
source

func EndArray

method on JSONWriter
1func (w *JSONWriter) EndArray()
source

func EndObject

method on JSONWriter
1func (w *JSONWriter) EndObject()
source

func Int

method on JSONWriter
1func (w *JSONWriter) Int(v int64)
source

func IntField

method on JSONWriter
1func (w *JSONWriter) IntField(k string, v int64)
source

func Key

method on JSONWriter
1func (w *JSONWriter) Key(k string)
source

Key starts an object member; the next value call writes its value.

func Raw

method on JSONWriter
1func (w *JSONWriter) Raw(v string)
source

Raw writes v verbatim. Only for JSON produced by another JSONWriter.

func Str

method on JSONWriter
1func (w *JSONWriter) Str(v string)
source

func StrField

method on JSONWriter
1func (w *JSONWriter) StrField(k, v string)
source

StrField and IntField are Key+value shorthands.

func String

method on JSONWriter
1func (w *JSONWriter) String() string
source

Dependencies

Source Files 7