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.

types.gno

5.16 Kb · 216 lines
  1// Package cust0dy_core holds the pure domain logic for Cust0dy: asset
  2// types and validation, the escrow state machine, and fee math. It has no
  3// chain imports, so every rule is unit-testable off-chain.
  4package cust0dy_core
  5
  6import (
  7	"errors"
  8	"strings"
  9)
 10
 11// AssetKind identifies what is being traded on the marketplace.
 12type AssetKind int
 13
 14const (
 15	KindDomain AssetKind = iota // Web2 domain names (example.com)
 16	KindSocial                  // social handles (X, Instagram, ...)
 17	KindGno                     // Gno-native handles
 18)
 19
 20func (k AssetKind) String() string {
 21	switch k {
 22	case KindDomain:
 23		return "domain"
 24	case KindSocial:
 25		return "social"
 26	case KindGno:
 27		return "gno-handle"
 28	default:
 29		return "unknown"
 30	}
 31}
 32
 33// ParseKind accepts "domain", "social", "gno" (and a few obvious aliases).
 34func ParseKind(s string) (AssetKind, error) {
 35	switch strings.ToLower(strings.TrimSpace(s)) {
 36	case "domain", "domains", "d":
 37		return KindDomain, nil
 38	case "social", "handle", "social-handle", "s":
 39		return KindSocial, nil
 40	case "gno", "gno-handle", "gnohandle", "g":
 41		return KindGno, nil
 42	default:
 43		return 0, errors.New("unknown asset kind " + s + " (want domain, social or gno)")
 44	}
 45}
 46
 47// ValidateAsset checks that value is plausible for its kind. It cannot prove
 48// ownership; that is what the off-chain verification flow is for.
 49func ValidateAsset(k AssetKind, value string) error {
 50	v := strings.TrimSpace(value)
 51	if v == "" {
 52		return errors.New("empty asset value")
 53	}
 54	switch k {
 55	case KindDomain:
 56		return validateDomain(v)
 57	case KindSocial:
 58		return validateHandle(strings.TrimPrefix(v, "@"), 1, 30, "social handle")
 59	case KindGno:
 60		return validateHandle(strings.TrimPrefix(v, "@"), 1, 39, "gno handle")
 61	default:
 62		return errors.New("unknown asset kind")
 63	}
 64}
 65
 66func validateDomain(d string) error {
 67	d = strings.ToLower(d)
 68	if len(d) > 253 {
 69		return errors.New("domain too long")
 70	}
 71	labels := strings.Split(d, ".")
 72	if len(labels) < 2 {
 73		return errors.New("domain needs at least one dot, like example.com")
 74	}
 75	for i, l := range labels {
 76		if l == "" {
 77			return errors.New("empty label in domain")
 78		}
 79		if len(l) > 63 {
 80			return errors.New("label too long in domain")
 81		}
 82		if strings.HasPrefix(l, "-") || strings.HasSuffix(l, "-") {
 83			return errors.New("labels cannot start or end with a hyphen")
 84		}
 85		for _, r := range l {
 86			if !((r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || r == '-') {
 87				return errors.New("bad character in domain")
 88			}
 89		}
 90		if i == len(labels)-1 && !onlyLetters(l) {
 91			return errors.New("top-level domain must be letters")
 92		}
 93	}
 94	return nil
 95}
 96
 97func onlyLetters(s string) bool {
 98	for _, r := range s {
 99		if r < 'a' || r > 'z' {
100			return false
101		}
102	}
103	return len(s) >= 2
104}
105
106func validateHandle(h string, min, max int, what string) error {
107	if len(h) < min || len(h) > max {
108		return errors.New(what + " must be 1-" + itoa(max) + " characters")
109	}
110	for _, r := range h {
111		ok := (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') ||
112			(r >= '0' && r <= '9') || r == '_' || r == '.' || r == '-'
113		if !ok {
114			return errors.New("bad character in " + what)
115		}
116	}
117	return nil
118}
119
120func itoa(n int) string {
121	if n == 0 {
122		return "0"
123	}
124	digits := ""
125	for n > 0 {
126		digits = string(rune('0'+n%10)) + digits
127		n /= 10
128	}
129	return digits
130}
131
132// ListingState is the lifecycle of one marketplace listing.
133type ListingState int
134
135const (
136	ListingOpen ListingState = iota
137	ListingSold
138	ListingCancelled
139)
140
141func (s ListingState) String() string {
142	switch s {
143	case ListingOpen:
144		return "open"
145	case ListingSold:
146		return "sold"
147	case ListingCancelled:
148		return "cancelled"
149	default:
150		return "unknown"
151	}
152}
153
154// State is the escrow state machine. Every trade moves through these states;
155// terminal states never change again.
156type State int
157
158const (
159	StateFunded State = iota
160	StateTransferSubmitted
161	StateCompleted
162	StateDisputed
163	StateResolvedSeller
164	StateResolvedBuyer
165	StateRefunded
166)
167
168func (s State) String() string {
169	switch s {
170	case StateFunded:
171		return "funded"
172	case StateTransferSubmitted:
173		return "transfer-submitted"
174	case StateCompleted:
175		return "completed"
176	case StateDisputed:
177		return "disputed"
178	case StateResolvedSeller:
179		return "resolved-seller"
180	case StateResolvedBuyer:
181		return "resolved-buyer"
182	case StateRefunded:
183		return "refunded"
184	default:
185		return "unknown"
186	}
187}
188
189// Terminal reports whether the escrow can no longer change state.
190func (s State) Terminal() bool {
191	switch s {
192	case StateCompleted, StateResolvedSeller, StateResolvedBuyer, StateRefunded:
193		return true
194	default:
195		return false
196	}
197}
198
199// CanBuyerConfirm: the buyer may confirm receipt once the seller has
200// submitted the transfer (or while still merely funded; confirming early is
201// the buyer's own risk).
202func CanBuyerConfirm(s State) bool {
203	return s == StateFunded || s == StateTransferSubmitted
204}
205
206// CanTimeoutRefund: the automatic refund guard applies only while the seller
207// has never submitted a transfer. Once a transfer is on the table, the
208// resolution path is buyer confirmation or a dispute.
209func CanTimeoutRefund(s State) bool {
210	return s == StateFunded
211}
212
213// CanDispute: either party may open a dispute while the trade is live.
214func CanDispute(s State) bool {
215	return s == StateFunded || s == StateTransferSubmitted
216}