package qrcregistry import ( "encoding/base64" "encoding/hex" "strconv" "strings" "time" "gno.land/p/nym-rootlatch001/qrc/v1" ) // Has reports whether account has a commit. func Has(account address) bool { return get(canonical(account)) != nil } // Count returns the number of accounts that hold a commit and, among them, // the number that bound a new public key. func Count() (accounts, withKey int) { return commits.Size(), bound } // IsUsed reports whether one-time key number leaf of account was spent. It // is false for an account without commit and for a leaf out of range. It // only knows of keys spent by a transaction that succeeded: a signature made // public in a failed one leaves its leaf unspent here, yet no wallet should // sign with that leaf again. func IsUsed(account address, leaf int) bool { c := get(canonical(account)) if c == nil || leaf < 0 || leaf >= qrc.Leaves(c.height) { return false } return c.isUsed(leaf) } // Key returns a copy of the public key last bound to account, with its // label. ok is false if the account has no commit or never bound a key. func Key(account address) (keyType string, key []byte, ok bool) { c := get(canonical(account)) if c == nil || c.keyBinds == 0 { return "", nil, false } return c.keyType, append([]byte(nil), c.key...), true } // KeyBase64 returns the public key last bound to account, base64 encoded, or // an empty string if there is none. func KeyBase64(account address) string { c := get(canonical(account)) if c == nil || c.keyBinds == 0 { return "" } return base64.StdEncoding.EncodeToString(c.key) } // Lookup returns what the registry holds about account as a JSON object, or // the JSON value null if the account has no commit. It leaves out the roots // of earlier generations. // // registeredAt, rotatedAt and keyAt are block heights, rotatedAt being 0 // before any rotation; registeredTime is in Unix seconds. "used" is the // bitmap of the one-time keys spent in the current trees, in hexadecimal: // key number n is spent when bit n%8, counting from the least significant, // of byte n/8 is set. The key fields are only present once a key was bound; // keyLeaf is a leaf of generation keyGeneration, which a later rotation // retires. The key itself is served by KeyBase64. func Lookup(account address) string { account = canonical(account) c := get(account) if c == nil { return "null" } var b strings.Builder b.WriteString(`{"account":"` + account.String() + `"`) b.WriteString(`,"rank":` + strconv.Itoa(c.rank)) b.WriteString(`,"registeredAt":` + strconv.FormatInt(c.registeredAt, 10)) b.WriteString(`,"registeredTime":` + strconv.FormatInt(c.registeredTime, 10)) b.WriteString(`,"generation":` + strconv.FormatUint(uint64(c.generation), 10)) b.WriteString(`,"rotatedAt":` + strconv.FormatInt(c.rotatedAt, 10)) b.WriteString(`,"height":` + strconv.Itoa(c.height)) b.WriteString(`,"rootSHA256":"` + hex.EncodeToString(c.rootSHA256[:]) + `"`) b.WriteString(`,"rootKeccak256":"` + hex.EncodeToString(c.rootKeccak256[:]) + `"`) b.WriteString(`,"used":"` + hex.EncodeToString(c.used) + `"`) b.WriteString(`,"usedCount":` + strconv.Itoa(c.usedCount)) if c.keyBinds > 0 { b.WriteString(`,"keyType":` + strconv.Quote(c.keyType)) b.WriteString(`,"keySize":` + strconv.Itoa(len(c.key))) b.WriteString(`,"keySHA256":"` + hashHex(c.key) + `"`) b.WriteString(`,"keyLeaf":` + strconv.Itoa(c.keyLeaf)) b.WriteString(`,"keyGeneration":` + strconv.FormatUint(uint64(c.keyGeneration), 10)) b.WriteString(`,"keyAt":` + strconv.FormatInt(c.keyAt, 10)) b.WriteString(`,"keyBinds":` + strconv.Itoa(c.keyBinds)) } b.WriteString("}") return b.String() } // Latest returns the accounts that registered last, newest first, at most // latestKept of them, as a JSON array of objects. Each object holds the // account, its rank, the height of its trees, the number of one-time keys // spent in its current trees, its generation and, once a key was bound, the // label of that key. // The rest of what the registry holds about an account is served by Lookup. func Latest() string { var b strings.Builder b.WriteString("[") for i, account := range latest { c := commits.Get(account).(*commit) if i > 0 { b.WriteString(",") } b.WriteString(`{"account":"` + account + `"`) b.WriteString(`,"rank":` + strconv.Itoa(c.rank)) b.WriteString(`,"height":` + strconv.Itoa(c.height)) b.WriteString(`,"usedCount":` + strconv.Itoa(c.usedCount)) b.WriteString(`,"generation":` + strconv.FormatUint(uint64(c.generation), 10)) if c.keyBinds > 0 { b.WriteString(`,"keyType":` + strconv.Quote(c.keyType)) } b.WriteString("}") } b.WriteString("]") return b.String() } // Render shows the registry: its home page for an empty path, the page of an // account for "
" or "account?address=
". func Render(path string) string { route, query := path, "" if i := strings.IndexByte(path, '?'); i >= 0 { route, query = path[:i], path[i+1:] } switch route { case "": return renderHome() case "account": // A space typed around the address arrives as '+'. return renderAccount(strings.Trim(queryValue(query, "address"), "+")) } return renderAccount(route) } func renderHome() string { accounts, withKey := Count() var b strings.Builder b.WriteString("# Quantum Recovery Commits\n\n") 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") b.WriteString("An account registers here the roots of two Merkle trees of Lamport one-time keys, ") b.WriteString("one hashed with SHA-256 and one with Keccak-256. Should its usual key ever be broken, ") b.WriteString("these keys can still name a new public key for it. This realm only records that key: ") b.WriteString("for it to sign for the account would take a change to the chain. ") b.WriteString("It implements the registration step that the Quantum Proof Cryptography Roadmap of the ") b.WriteString("[gno.land LAWS](https://github.com/gnolang/gno/blob/v1.8.1/docs/LAWS.md#quantum-proof-cryptography-roadmap) proposes.\n\n") b.WriteString("**" + strconv.Itoa(accounts) + "** " + plural(accounts, "account holds", "accounts hold") + " a commit. ") b.WriteString("**" + strconv.Itoa(withKey) + "** " + plural(withKey, "has", "have") + " bound a new public key.\n\n") b.WriteString("## Look up an account\n\n") b.WriteString("\n") b.WriteString("\t\n") b.WriteString("\n\n") if accounts > 0 { b.WriteString("## Latest registrations\n\n") b.WriteString("| # | Account | One-time keys used | New public key |\n") b.WriteString("|---|---|---|---|\n") for _, account := range latest { c := commits.Get(account).(*commit) b.WriteString("| " + strconv.Itoa(c.rank)) b.WriteString(" | " + account) b.WriteString(" | " + strconv.Itoa(c.usedCount) + " of " + strconv.Itoa(qrc.Leaves(c.height))) b.WriteString(" | " + keySummary(c) + " |\n") } b.WriteString("\n") } b.WriteString("This realm is an independent prototype. It has no owner and nobody can change it.\n") return b.String() } func renderAccount(input string) string { account := canonical(address(input)) if !account.IsValid() { return "# Not an address\n\nAn account page needs a valid address, such as `g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5`.\n" } var b strings.Builder b.WriteString("# " + account.String() + "\n\n") c := get(account) if c == nil { b.WriteString("**No commit.** This account has no commit in this registry. ") b.WriteString("If its key were broken today, nothing here could tell its owner from the thief.\n") return b.String() } if c.usedCount == qrc.Leaves(c.height) { b.WriteString("**No one-time key left.** Every key of this commit was used: ") b.WriteString("nothing can bind, rotate or authorize for this account any more. ") } else { b.WriteString("**Recoverable.** ") } b.WriteString("This account registered its commit at block ") b.WriteString(strconv.FormatInt(c.registeredAt, 10)) b.WriteString(" (" + time.Unix(c.registeredTime, 0).UTC().Format("2006-01-02") + ")") b.WriteString(", as account number " + strconv.Itoa(c.rank) + " of this registry.\n\n") b.WriteString("| | |\n|---|---|\n") b.WriteString("| SHA-256 root | `" + hex.EncodeToString(c.rootSHA256[:]) + "` |\n") b.WriteString("| Keccak-256 root | `" + hex.EncodeToString(c.rootKeccak256[:]) + "` |\n") b.WriteString("| One-time keys | " + strconv.Itoa(qrc.Leaves(c.height)) + ", " + strconv.Itoa(c.usedCount) + " used |\n") b.WriteString("| Generation | " + strconv.FormatUint(uint64(c.generation), 10)) if c.rotatedAt > 0 { b.WriteString(", rotated at block " + strconv.FormatInt(c.rotatedAt, 10)) } b.WriteString(" |\n") b.WriteString("| New public key | " + keySummary(c)) if c.keyBinds > 0 { b.WriteString(", SHA-256 `" + hashHex(c.key) + "`") b.WriteString(", bound at block " + strconv.FormatInt(c.keyAt, 10)) b.WriteString(" with one-time key " + strconv.Itoa(c.keyLeaf)) b.WriteString(" of generation " + strconv.FormatUint(uint64(c.keyGeneration), 10)) } b.WriteString(" |\n") return b.String() } // keySummary describes the key bound to c. The label is whatever the account // chose, so it goes in a code span: nothing in it can then turn into a link // or into formatting on a page that other people read. func keySummary(c *commit) string { if c.keyBinds == 0 { return "none yet" } return "`" + c.keyType + "`, " + strconv.Itoa(len(c.key)) + " " + plural(len(c.key), "byte", "bytes") } func plural(n int, one, many string) string { if n == 1 { return one } return many } // queryValue returns the raw value of the first parameter called name in a // URL query string, or "". Values are not unescaped: the only one read here // is an address, which needs no escaping. A space typed around it arrives as // '+', which the caller trims. func queryValue(query, name string) string { for _, part := range strings.Split(query, "&") { if strings.HasPrefix(part, name+"=") { return part[len(name)+1:] } } return "" }