Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

views.gno

9.96 Kb · 256 lines
  1package qrcregistry
  2
  3import (
  4	"encoding/base64"
  5	"encoding/hex"
  6	"strconv"
  7	"strings"
  8	"time"
  9
 10	"gno.land/p/nym-rootlatch001/qrc/v1"
 11)
 12
 13// Has reports whether account has a commit.
 14func Has(account address) bool {
 15	return get(canonical(account)) != nil
 16}
 17
 18// Count returns the number of accounts that hold a commit and, among them,
 19// the number that bound a new public key.
 20func Count() (accounts, withKey int) {
 21	return commits.Size(), bound
 22}
 23
 24// IsUsed reports whether one-time key number leaf of account was spent. It
 25// is false for an account without commit and for a leaf out of range. It
 26// only knows of keys spent by a transaction that succeeded: a signature made
 27// public in a failed one leaves its leaf unspent here, yet no wallet should
 28// sign with that leaf again.
 29func IsUsed(account address, leaf int) bool {
 30	c := get(canonical(account))
 31	if c == nil || leaf < 0 || leaf >= qrc.Leaves(c.height) {
 32		return false
 33	}
 34	return c.isUsed(leaf)
 35}
 36
 37// Key returns a copy of the public key last bound to account, with its
 38// label. ok is false if the account has no commit or never bound a key.
 39func Key(account address) (keyType string, key []byte, ok bool) {
 40	c := get(canonical(account))
 41	if c == nil || c.keyBinds == 0 {
 42		return "", nil, false
 43	}
 44	return c.keyType, append([]byte(nil), c.key...), true
 45}
 46
 47// KeyBase64 returns the public key last bound to account, base64 encoded, or
 48// an empty string if there is none.
 49func KeyBase64(account address) string {
 50	c := get(canonical(account))
 51	if c == nil || c.keyBinds == 0 {
 52		return ""
 53	}
 54	return base64.StdEncoding.EncodeToString(c.key)
 55}
 56
 57// Lookup returns what the registry holds about account as a JSON object, or
 58// the JSON value null if the account has no commit. It leaves out the roots
 59// of earlier generations.
 60//
 61// registeredAt, rotatedAt and keyAt are block heights, rotatedAt being 0
 62// before any rotation; registeredTime is in Unix seconds. "used" is the
 63// bitmap of the one-time keys spent in the current trees, in hexadecimal:
 64// key number n is spent when bit n%8, counting from the least significant,
 65// of byte n/8 is set. The key fields are only present once a key was bound;
 66// keyLeaf is a leaf of generation keyGeneration, which a later rotation
 67// retires. The key itself is served by KeyBase64.
 68func Lookup(account address) string {
 69	account = canonical(account)
 70	c := get(account)
 71	if c == nil {
 72		return "null"
 73	}
 74	var b strings.Builder
 75	b.WriteString(`{"account":"` + account.String() + `"`)
 76	b.WriteString(`,"rank":` + strconv.Itoa(c.rank))
 77	b.WriteString(`,"registeredAt":` + strconv.FormatInt(c.registeredAt, 10))
 78	b.WriteString(`,"registeredTime":` + strconv.FormatInt(c.registeredTime, 10))
 79	b.WriteString(`,"generation":` + strconv.FormatUint(uint64(c.generation), 10))
 80	b.WriteString(`,"rotatedAt":` + strconv.FormatInt(c.rotatedAt, 10))
 81	b.WriteString(`,"height":` + strconv.Itoa(c.height))
 82	b.WriteString(`,"rootSHA256":"` + hex.EncodeToString(c.rootSHA256[:]) + `"`)
 83	b.WriteString(`,"rootKeccak256":"` + hex.EncodeToString(c.rootKeccak256[:]) + `"`)
 84	b.WriteString(`,"used":"` + hex.EncodeToString(c.used) + `"`)
 85	b.WriteString(`,"usedCount":` + strconv.Itoa(c.usedCount))
 86	if c.keyBinds > 0 {
 87		b.WriteString(`,"keyType":` + strconv.Quote(c.keyType))
 88		b.WriteString(`,"keySize":` + strconv.Itoa(len(c.key)))
 89		b.WriteString(`,"keySHA256":"` + hashHex(c.key) + `"`)
 90		b.WriteString(`,"keyLeaf":` + strconv.Itoa(c.keyLeaf))
 91		b.WriteString(`,"keyGeneration":` + strconv.FormatUint(uint64(c.keyGeneration), 10))
 92		b.WriteString(`,"keyAt":` + strconv.FormatInt(c.keyAt, 10))
 93		b.WriteString(`,"keyBinds":` + strconv.Itoa(c.keyBinds))
 94	}
 95	b.WriteString("}")
 96	return b.String()
 97}
 98
 99// Latest returns the accounts that registered last, newest first, at most
100// latestKept of them, as a JSON array of objects. Each object holds the
101// account, its rank, the height of its trees, the number of one-time keys
102// spent in its current trees, its generation and, once a key was bound, the
103// label of that key.
104// The rest of what the registry holds about an account is served by Lookup.
105func Latest() string {
106	var b strings.Builder
107	b.WriteString("[")
108	for i, account := range latest {
109		c := commits.Get(account).(*commit)
110		if i > 0 {
111			b.WriteString(",")
112		}
113		b.WriteString(`{"account":"` + account + `"`)
114		b.WriteString(`,"rank":` + strconv.Itoa(c.rank))
115		b.WriteString(`,"height":` + strconv.Itoa(c.height))
116		b.WriteString(`,"usedCount":` + strconv.Itoa(c.usedCount))
117		b.WriteString(`,"generation":` + strconv.FormatUint(uint64(c.generation), 10))
118		if c.keyBinds > 0 {
119			b.WriteString(`,"keyType":` + strconv.Quote(c.keyType))
120		}
121		b.WriteString("}")
122	}
123	b.WriteString("]")
124	return b.String()
125}
126
127// Render shows the registry: its home page for an empty path, the page of an
128// account for "<address>" or "account?address=<address>".
129func Render(path string) string {
130	route, query := path, ""
131	if i := strings.IndexByte(path, '?'); i >= 0 {
132		route, query = path[:i], path[i+1:]
133	}
134	switch route {
135	case "":
136		return renderHome()
137	case "account":
138		// A space typed around the address arrives as '+'.
139		return renderAccount(strings.Trim(queryValue(query, "address"), "+"))
140	}
141	return renderAccount(route)
142}
143
144func renderHome() string {
145	accounts, withKey := Count()
146
147	var b strings.Builder
148	b.WriteString("# Quantum Recovery Commits\n\n")
149	b.WriteString("Lets an account name a new public key in a way that does not rest on the signature scheme its wallet uses today.\n\n")
150	b.WriteString("An account registers here the roots of two Merkle trees of Lamport one-time keys, ")
151	b.WriteString("one hashed with SHA-256 and one with Keccak-256. Should its usual key ever be broken, ")
152	b.WriteString("these keys can still name a new public key for it. This realm only records that key: ")
153	b.WriteString("for it to sign for the account would take a change to the chain. ")
154	b.WriteString("It implements the registration step that the Quantum Proof Cryptography Roadmap of the ")
155	b.WriteString("[gno.land LAWS](https://github.com/gnolang/gno/blob/v1.8.1/docs/LAWS.md#quantum-proof-cryptography-roadmap) proposes.\n\n")
156	b.WriteString("**" + strconv.Itoa(accounts) + "** " + plural(accounts, "account holds", "accounts hold") + " a commit. ")
157	b.WriteString("**" + strconv.Itoa(withKey) + "** " + plural(withKey, "has", "have") + " bound a new public key.\n\n")
158
159	b.WriteString("## Look up an account\n\n")
160	b.WriteString("<gno-form path=\"account\">\n")
161	b.WriteString("\t<gno-input name=\"address\" type=\"text\" placeholder=\"Address (g1...)\" />\n")
162	b.WriteString("</gno-form>\n\n")
163
164	if accounts > 0 {
165		b.WriteString("## Latest registrations\n\n")
166		b.WriteString("| # | Account | One-time keys used | New public key |\n")
167		b.WriteString("|---|---|---|---|\n")
168		for _, account := range latest {
169			c := commits.Get(account).(*commit)
170			b.WriteString("| " + strconv.Itoa(c.rank))
171			b.WriteString(" | " + account)
172			b.WriteString(" | " + strconv.Itoa(c.usedCount) + " of " + strconv.Itoa(qrc.Leaves(c.height)))
173			b.WriteString(" | " + keySummary(c) + " |\n")
174		}
175		b.WriteString("\n")
176	}
177
178	b.WriteString("This realm is an independent prototype. It has no owner and nobody can change it.\n")
179	return b.String()
180}
181
182func renderAccount(input string) string {
183	account := canonical(address(input))
184	if !account.IsValid() {
185		return "# Not an address\n\nAn account page needs a valid address, such as `g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5`.\n"
186	}
187
188	var b strings.Builder
189	b.WriteString("# " + account.String() + "\n\n")
190	c := get(account)
191	if c == nil {
192		b.WriteString("**No commit.** This account has no commit in this registry. ")
193		b.WriteString("If its key were broken today, nothing here could tell its owner from the thief.\n")
194		return b.String()
195	}
196
197	if c.usedCount == qrc.Leaves(c.height) {
198		b.WriteString("**No one-time key left.** Every key of this commit was used: ")
199		b.WriteString("nothing can bind, rotate or authorize for this account any more. ")
200	} else {
201		b.WriteString("**Recoverable.** ")
202	}
203	b.WriteString("This account registered its commit at block ")
204	b.WriteString(strconv.FormatInt(c.registeredAt, 10))
205	b.WriteString(" (" + time.Unix(c.registeredTime, 0).UTC().Format("2006-01-02") + ")")
206	b.WriteString(", as account number " + strconv.Itoa(c.rank) + " of this registry.\n\n")
207
208	b.WriteString("| | |\n|---|---|\n")
209	b.WriteString("| SHA-256 root | `" + hex.EncodeToString(c.rootSHA256[:]) + "` |\n")
210	b.WriteString("| Keccak-256 root | `" + hex.EncodeToString(c.rootKeccak256[:]) + "` |\n")
211	b.WriteString("| One-time keys | " + strconv.Itoa(qrc.Leaves(c.height)) + ", " + strconv.Itoa(c.usedCount) + " used |\n")
212	b.WriteString("| Generation | " + strconv.FormatUint(uint64(c.generation), 10))
213	if c.rotatedAt > 0 {
214		b.WriteString(", rotated at block " + strconv.FormatInt(c.rotatedAt, 10))
215	}
216	b.WriteString(" |\n")
217	b.WriteString("| New public key | " + keySummary(c))
218	if c.keyBinds > 0 {
219		b.WriteString(", SHA-256 `" + hashHex(c.key) + "`")
220		b.WriteString(", bound at block " + strconv.FormatInt(c.keyAt, 10))
221		b.WriteString(" with one-time key " + strconv.Itoa(c.keyLeaf))
222		b.WriteString(" of generation " + strconv.FormatUint(uint64(c.keyGeneration), 10))
223	}
224	b.WriteString(" |\n")
225	return b.String()
226}
227
228// keySummary describes the key bound to c. The label is whatever the account
229// chose, so it goes in a code span: nothing in it can then turn into a link
230// or into formatting on a page that other people read.
231func keySummary(c *commit) string {
232	if c.keyBinds == 0 {
233		return "none yet"
234	}
235	return "`" + c.keyType + "`, " + strconv.Itoa(len(c.key)) + " " + plural(len(c.key), "byte", "bytes")
236}
237
238func plural(n int, one, many string) string {
239	if n == 1 {
240		return one
241	}
242	return many
243}
244
245// queryValue returns the raw value of the first parameter called name in a
246// URL query string, or "". Values are not unescaped: the only one read here
247// is an address, which needs no escaping. A space typed around it arrives as
248// '+', which the caller trims.
249func queryValue(query, name string) string {
250	for _, part := range strings.Split(query, "&") {
251		if strings.HasPrefix(part, name+"=") {
252			return part[len(name)+1:]
253		}
254	}
255	return ""
256}