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.

verified.gno

5.29 Kb · 165 lines
  1package rooms
  2
  3import (
  4	"chain"
  5	"strconv"
  6	"strings"
  7	"time"
  8
  9	"gno.land/p/nt/avl/v0"
 10	"gno.land/r/nt/grc20reg/v0"
 11)
 12
 13// Verified assets. A registry proves where a token or a collection comes
 14// from, not that it is worth anything: anyone can register a token called
 15// USDC, and an issuer keeps power over its ledger. So a room opened with
 16// Open accepts only verified assets: GNOT and wrapped GNOT from the start,
 17// then tokens and NFT collections the admin verifies, each with a public
 18// reason. A room opened with OpenMarket accepts any registered asset; the
 19// name of that call is what the wallet shows when it is signed.
 20//
 21// A verification counts only VerifyDelay after the admin makes it, so a
 22// stolen admin key cannot slip a worthless token into ordinary rooms before
 23// anyone notices; it is public from the moment it is made. Removing a
 24// verification counts at once: an ordinary room holding that asset can no
 25// longer settle, and everyone can still take their items back.
 26
 27// VerifyDelay is how long a new verification waits before it counts.
 28const VerifyDelay = 24 * 60 * 60 // seconds
 29
 30// maxReason bounds a verification's public reason.
 31const maxReason = 280
 32
 33type verification struct {
 34	reason string
 35	since  int64 // unix seconds from which it counts
 36	by     address
 37}
 38
 39var verified avl.Tree // asset class -> *verification
 40
 41// seedVerified verifies GNOT and wrapped GNOT from the start.
 42func seedVerified() {
 43	verified.Set("coin:ugnot", &verification{reason: "The native coin of gno.land."})
 44	verified.Set("token:"+wugnotKey, &verification{reason: "GNOT wrapped one to one by gno.land/r/gnoland/wugnot, a realm with no admin."})
 45}
 46
 47// Verify makes a coin denom, a registered GRC20 token or a registered NFT
 48// collection acceptable in ordinary rooms from VerifyDelay from now. kind
 49// is coin, token or nft; key is the denom, the registry key or the
 50// collection key. reason is public: say why this asset is safe to trade.
 51func Verify(cur realm, kind, key, reason string) {
 52	assertAdmin(cur)
 53	key = strings.TrimSpace(key)
 54	switch kind {
 55	case KindCoin:
 56		assertDenom(key)
 57	case KindToken:
 58		if grc20reg.Get(key) == nil {
 59			panic("token is not in the GRC20 registry")
 60		}
 61	case KindNFT:
 62		mustCollection(key)
 63	default:
 64		panic("kind must be coin, token or nft")
 65	}
 66	reason = strings.TrimSpace(reason)
 67	if reason == "" || len(reason) > maxReason {
 68		panic("give a reason of 1 to " + strconv.Itoa(maxReason) + " bytes")
 69	}
 70	for _, r := range reason {
 71		if r < 0x20 || r == 0x7f {
 72			panic("the reason has a control character")
 73		}
 74	}
 75	since := time.Now().Unix() + VerifyDelay
 76	verified.Set(kind+":"+key, &verification{reason: reason, since: since, by: callerOf(cur)})
 77	chain.Emit("VerifyProposed",
 78		"kind", kind,
 79		"key", key,
 80		"since", strconv.FormatInt(since, 10),
 81		"reason", reason,
 82	)
 83}
 84
 85// Unverify removes a verification at once. Ordinary rooms stop accepting
 86// the asset, and those that hold it can no longer settle; deposits stay
 87// withdrawable. reason is public.
 88func Unverify(cur realm, kind, key, reason string) {
 89	assertAdmin(cur)
 90	class := kind + ":" + strings.TrimSpace(key)
 91	if !verified.Has(class) {
 92		panic("that asset is not verified")
 93	}
 94	verified.Remove(class)
 95	chain.Emit("Unverified", "kind", kind, "key", strings.TrimSpace(key), "reason", strings.TrimSpace(reason))
 96}
 97
 98// IsVerified reports whether an asset counts as verified now. denom is a
 99// coin denom, a registry key, or a collection key (with or without
100// "#token id").
101func IsVerified(kind, denom string) bool {
102	return isVerified(kind, denom, time.Now().Unix())
103}
104
105func classOf(kind, denom string) string {
106	if kind == KindNFT {
107		key, _, _ := strings.Cut(denom, "#")
108		return KindNFT + ":" + key
109	}
110	return kind + ":" + denom
111}
112
113func isVerified(kind, denom string, now int64) bool {
114	v := verified.Get(classOf(kind, denom))
115	return v != nil && now >= v.(*verification).since
116}
117
118// assertAccepts refuses an asset an ordinary room does not take.
119func (r *room) assertAccepts(kind, denom string) {
120	if r.openMarket || isVerified(kind, denom, time.Now().Unix()) {
121		return
122	}
123	panic("this room takes only verified assets, and " + denom + " is not verified: trade it in an open-market room")
124}
125
126// assertStillVerified stops an ordinary room from settling when one of its
127// assets has lost its verification since it went in.
128func (r *room) assertStillVerified() {
129	if r.openMarket {
130		return
131	}
132	now := time.Now().Unix()
133	for _, list := range [][]asset{r.maker.assets, r.taker.assets, r.want} {
134		for _, a := range list {
135			if !isVerified(a.kind, a.denom, now) {
136				panic(a.denom + " is no longer verified, so this room cannot settle: take your items back")
137			}
138		}
139	}
140}
141
142// VerifiedJSON lists verifications, counted or still waiting, with their
143// public reasons.
144func VerifiedJSON(offset, limit int) string {
145	offset, limit = page(offset, limit)
146	now := time.Now().Unix()
147	items := []string{}
148	verified.IterateByOffset(offset, limit, func(class string, v any) bool {
149		kind, key, _ := strings.Cut(class, ":")
150		ver := v.(*verification)
151		items = append(items, jsonObject(
152			"kind", jsonString(kind),
153			"key", jsonString(key),
154			"reason", jsonString(ver.reason),
155			"since", jsonInt(ver.since),
156			"active", jsonBool(now >= ver.since),
157		))
158		return false
159	})
160	return jsonObject(
161		"assets", jsonArray(items),
162		"more", jsonBool(offset+len(items) < verified.Size()),
163		"now", jsonInt(now),
164	)
165}