render.gno
6.60 Kb · 187 lines
1package wesh
2
3import (
4 "chain/runtime"
5 "encoding/hex"
6 "strconv"
7 "strings"
8 "time"
9
10 "gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/md/v0"
11 "gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/wesh/v0"
12)
13
14// Render serves the directory index at "" and one identity card at "<name>".
15//
16// The index deliberately does not compute a Berty link per row: base58 over a
17// 70-byte payload is big-integer work, and it belongs on the page that a human
18// actually asked for.
19func Render(path string) string {
20 path = strings.Trim(path, "/")
21 if path == "" {
22 return renderIndex()
23 }
24 return renderIdentity(path)
25}
26
27func renderIndex() string {
28 var b strings.Builder
29 b.WriteString("# Wesh directory\n\n")
30 b.WriteString("Resolvable, self-signed [Wesh protocol](https://berty.tech/docs/protocol/) ")
31 b.WriteString("identities, on top of [`p/moul/x/wesh`](/p/moul/x/wesh/v0).\n\n")
32 b.WriteString("A Berty identity normally travels out of band as a QR code, cannot be ")
33 b.WriteString("looked up, cannot announce a seed rotation, and can never revoke a device. ")
34 b.WriteString("This realm addresses those three, and stores no secret of any kind.\n\n")
35
36 if byName.Size() == 0 {
37 b.WriteString("_No identity registered yet._\n")
38 return b.String()
39 }
40
41 b.WriteString("| name | owner | rev | mode | devices |\n")
42 b.WriteString("|---|---|---|---|---|\n")
43 byName.Iterate("", "", func(key string, value any) bool {
44 id := value.(*identity)
45 b.WriteString("| [" + id.name + "](/r/moul/x/wesh/v0:" + id.name + ") ")
46 b.WriteString("| `" + id.owner.String() + "` ")
47 b.WriteString("| " + strconv.Itoa(id.revision) + " ")
48 b.WriteString("| " + modeLabel(id) + " ")
49 b.WriteString("| " + strconv.Itoa(activeDevices(id)) + " active / " + strconv.Itoa(len(id.devices)) + " entries |\n")
50 return false
51 })
52 n := byName.Size()
53 if n == 1 {
54 b.WriteString("\n1 identity.\n")
55 } else {
56 b.WriteString("\n" + strconv.Itoa(n) + " identities.\n")
57 }
58 return b.String()
59}
60
61func renderIdentity(name string) string {
62 id := lookup(name)
63 if id == nil {
64 return "# Not found\n\nNo identity named " + md.InlineCode(name) + ". [Back to the directory](/r/moul/x/wesh/v0).\n"
65 }
66
67 var b strings.Builder
68 b.WriteString("# " + id.name + "\n\n")
69 if id.displayName != "" {
70 b.WriteString("_" + id.displayName + "_\n\n")
71 }
72 b.WriteString("| field | value |\n|---|---|\n")
73 b.WriteString("| owner | `" + id.owner.String() + "` |\n")
74 b.WriteString("| account key | `" + hex.EncodeToString(id.accountPK) + "` |\n")
75 b.WriteString("| mode | " + modeLabel(id) + " |\n")
76 b.WriteString("| revision | " + strconv.Itoa(id.revision) + " |\n")
77 b.WriteString("| registered | block " + strconv.FormatInt(id.history[0].height, 10) + " |\n")
78
79 b.WriteString("\n## Contact\n\n")
80 if id.committed {
81 b.WriteString("This identity publishes a **commitment**, `")
82 b.WriteString(hex.EncodeToString(id.payload))
83 b.WriteString("`, not its rendezvous seed.\n\n")
84 b.WriteString("The chain attests that this account key claimed that commitment from ")
85 b.WriteString("that gno address. The seed itself is shared out of band, and whoever ")
86 b.WriteString("receives it can check it here with `H(seed || salt)` before using it. ")
87 b.WriteString("The rendezvous point stays off chain.\n")
88 } else {
89 link, err := contactOf(id).WebLink()
90 if err != nil {
91 b.WriteString("_Contact is unrenderable: " + err.Error() + "_\n")
92 } else {
93 b.WriteString("Scan or open this in Berty to send a contact request:\n\n")
94 b.WriteString("```\n" + link + "\n```\n\n")
95 b.WriteString("| field | value |\n|---|---|\n")
96 b.WriteString("| rendezvous seed | `" + hex.EncodeToString(id.payload) + "` |\n")
97 now := time.Now().Unix()
98 period := wesh.RoundPeriod(now, wesh.DefaultRotationInterval)
99 point, _ := contactOf(id).RendezvousPointAt(now, wesh.DefaultRotationInterval)
100 b.WriteString("| rendezvous point, period " + strconv.FormatInt(period, 10) + " | `" + hex.EncodeToString(point) + "` |\n")
101 b.WriteString("\nThe point is `HMAC-SHA256(accountPK || seed, be64(period))`, derived ")
102 b.WriteString("exactly as weshnet derives it, and it rotates every 24h. Anyone can ")
103 b.WriteString("recompute it and check the DHT, so this entry is verifiable rather ")
104 b.WriteString("than merely asserted.\n")
105 }
106 }
107
108 b.WriteString("\n## Devices\n\n")
109 if len(id.devices) == 0 {
110 b.WriteString("No sigchain entry yet. Head is the genesis digest `")
111 b.WriteString(hex.EncodeToString(id.head) + "`.\n")
112 } else {
113 b.WriteString("Every entry below was signed by the account key and verified on chain, ")
114 b.WriteString("and chains to the digest of the one before it.\n\n")
115 b.WriteString("| seq | op | device | block | digest |\n|---|---|---|---|---|\n")
116 for _, e := range id.devices {
117 b.WriteString("| " + strconv.Itoa(e.seq) + " ")
118 b.WriteString("| " + e.op + " ")
119 b.WriteString("| `" + short(hex.EncodeToString(e.devicePK)) + "` ")
120 b.WriteString("| " + strconv.FormatInt(e.height, 10) + " ")
121 b.WriteString("| `" + short(hex.EncodeToString(e.digest)) + "` |\n")
122 }
123 b.WriteString("\nHead: `" + hex.EncodeToString(id.head) + "`\n")
124 }
125
126 if len(id.history) > 1 {
127 b.WriteString("\n## Rotation history\n\n")
128 b.WriteString("Weshnet has no way to announce that a seed was reset, so every link ")
129 b.WriteString("ever shared dies silently. These are the superseded values, kept so a ")
130 b.WriteString("stale link can be recognised as stale.\n\n")
131 b.WriteString("| rev | value | block |\n|---|---|---|\n")
132 for _, r := range id.history {
133 mark := ""
134 if r.revision == id.revision {
135 mark = " (current)"
136 }
137 b.WriteString("| " + strconv.Itoa(r.revision) + mark + " ")
138 b.WriteString("| `" + short(hex.EncodeToString(r.payload)) + "` ")
139 b.WriteString("| " + strconv.FormatInt(r.height, 10) + " |\n")
140 }
141 }
142
143 b.WriteString("\n[Back to the directory](/r/moul/x/wesh/v0) · chain `" + runtime.ChainID() + "`\n")
144 return b.String()
145}
146
147func modeLabel(id *identity) string {
148 if id.committed {
149 return "committed"
150 }
151 return "published"
152}
153
154// activeDevices counts the distinct devices whose latest sigchain entry is an
155// add. A device can be added, revoked and added again, so only the last entry
156// for each key decides.
157func activeDevices(id *identity) int {
158 seen := []string{}
159 for _, e := range id.devices {
160 key := string(e.devicePK)
161 known := false
162 for _, s := range seen {
163 if s == key {
164 known = true
165 break
166 }
167 }
168 if !known {
169 seen = append(seen, key)
170 }
171 }
172 n := 0
173 for _, k := range seen {
174 if deviceIsActive(id, []byte(k)) {
175 n++
176 }
177 }
178 return n
179}
180
181// short abbreviates a long hex string for table cells.
182func short(s string) string {
183 if len(s) <= 16 {
184 return s
185 }
186 return s[:8] + "…" + s[len(s)-8:]
187}