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