loci.gno

1.67 Kb ยท 66 lines
 1package loci
 2
 3import (
 4	"encoding/base64"
 5	"std"
 6
 7	"gno.land/p/demo/ufmt"
 8	"gno.land/p/n2p5/loci"
 9)
10
11var store *loci.LociStore
12
13func init() {
14	store = loci.New()
15}
16
17// Set takes a base64 encoded string and stores it in the Loci store.
18// Keyed by the address of the caller. It also emits a "set" event with
19// the address of the caller.
20func Set(cur realm, value string) {
21	b, err := base64.StdEncoding.DecodeString(value)
22	if err != nil {
23		panic(err)
24	}
25	store.Set(b)
26	std.Emit("SetValue", "ForAddr", string(std.PreviousRealm().Address()))
27}
28
29// Get retrieves the value stored at the provided address and
30// returns it as a base64 encoded string.
31func Get(cur realm, addr std.Address) string {
32	return base64.StdEncoding.EncodeToString(store.Get(addr))
33}
34
35func Render(path string) string {
36	if path == "" {
37		return `
38# Welcome to Loci
39
40Loci is a simple key-value store keyed by the caller's gno.land address. 
41Only the caller can set the value for their address, but anyone can 
42retrieve the value for any address. There are only two functions: Set and Get.
43If you'd like to set a value, simply base64 encode any message you'd like and
44it will be stored in in Loci. If you'd like to retrieve a value, simply provide 
45the address of the value you'd like to retrieve.
46
47For convenience, you can also use gnoweb to view the value for a given address,
48if one exists. For instance append :g1j39fhg29uehm7twwnhvnpz3ggrm6tprhq65t0t to
49this URL to view the value stored at that address.
50`
51	}
52	return renderGet(cross, std.Address(path))
53}
54
55func renderGet(cur realm, addr std.Address) string {
56	value := "```\n" + Get(cur, addr) + "\n```"
57
58	return ufmt.Sprintf(`
59# Loci Value Viewer
60
61**Address:** %s
62
63%s
64
65`, addr, value)
66}