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("