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