Search Apps Documentation Source Content File Folder Download Copy Actions Download

safeobjects.gno

1.99 Kb ยท 71 lines
 1package safeobjects
 2
 3import (
 4	"chain/runtime"
 5)
 6
 7// ExposedSafeObject methods exposed to the public. However, the methods all
 8// contain a caller check - making them, and the object itself "safe".
 9// The methods are only callable by the current admin.
10// A prime example of a safe object can be found in "p/demo/ownable".
11var ExposedSafeObject = SafeObject{
12	privilegedData: "This message can only be set by the admin.",
13	admin:          address("g125em6arxsnj49vx35f0n0z34putv5ty3376fg5"),
14}
15
16// SafeObject is an object that contains some privileged data
17// This data might be privileged because only certain users
18// have the edit rights, or for other reasons.
19type SafeObject struct {
20	privilegedData string
21	admin          address
22}
23
24// Set is a function only the admin can call
25func (so SafeObject) Set(value string) {
26	if runtime.CurrentRealm().Address() != so.admin {
27		panic("caller is not authorized")
28	}
29
30	so.privilegedData = value
31}
32
33// UpdateAdmin is a function only the admin can call
34func (so SafeObject) UpdateAdmin(newAdmin address) {
35	if runtime.CurrentRealm().Address() != so.admin {
36		panic("caller is not authorized")
37	}
38
39	if !newAdmin.IsValid() {
40		panic("new admin address is invalid")
41	}
42
43	so.admin = newAdmin
44}
45
46// Get is callable by anyone
47func (so SafeObject) Get() string {
48	return so.privilegedData
49}
50
51func Render(_ string) string {
52	return `
53# Safe Objects
54
55**Safe Objects** are objects that can be exposed at the realm top level
56while still keeping the write access to their memory limited.
57
58Safe Objects allow only authorized users (like admins) can modify their
59internal state, even if they are exposed at the realm top level.
60
61A prime example of a commonly used safe object is the Ownable object, found under in [**p/demo/ownable**](/p/demo/ownable).
62
63To call methods on exposed safe objects, users need to use [MsgRun](/r/docs/complexargs).
64
65---
66
67` + "`r/docs/safeobjects.ExposedSafeObject`" + ` values:
68- Message: ` + ExposedSafeObject.privilegedData + `
69- Admin: ` + ExposedSafeObject.admin.String()
70
71}