package qrcregistry import ( "chain/runtime" "crypto/bech32" "crypto/sha256" "encoding/base64" "encoding/hex" "strconv" "strings" "testing" "gno.land/p/nt/testutils/v0" "gno.land/p/nt/uassert/v0" "gno.land/p/nym-rootlatch001/qrc/v1" "gno.land/p/nym-rootlatch001/qrcsigner/v1" ) const registryPath = "gno.land/r/nym-rootlatch001/qrcregistry/v1" // holder plays the owner of an account: it holds the one-time keys of one // commit and signs requests with them, as a wallet would off-chain. type holder struct { account address id qrc.ID height int kit *qrcsigner.Kit } // newHolder derives the commit number generation of account. The entropy // depends on the account alone, so that every generation of an account comes // from one secret, as it does in a wallet. func newHolder(account address, generation uint32) *holder { return newHolderAt(account, generation, qrc.MinHeight) } // newHolderAt is newHolder for trees of the given height. func newHolderAt(account address, generation uint32, height int) *holder { entropy := sha256.Sum256([]byte("test entropy of " + account.String())) id := qrc.ID{ ChainID: runtime.ChainID(), Registry: registryPath, Account: account.String(), Generation: generation, } return &holder{account: account, id: id, height: height, kit: qrcsigner.New(entropy[:], id, height)} } func (h *holder) roots() (rootSHA256, rootKeccak256 string) { a, b := h.kit.Roots() return hex.EncodeToString(a[:]), hex.EncodeToString(b[:]) } func (h *holder) signBind(leaf int, keyType string, key []byte) (proofSHA256, proofKeccak256 []byte) { return h.kit.Sign(leaf, qrc.BindMessage(h.id, uint32(leaf), keyType, key)) } func (h *holder) signRotate(leaf int, next *holder) (proofSHA256, proofKeccak256 []byte) { rootSHA256, rootKeccak256 := next.kit.Roots() return h.kit.Sign(leaf, qrc.RotateMessage(h.id, uint32(leaf), rootSHA256, rootKeccak256, next.height)) } func (h *holder) signAuthorize(leaf int, app string, payload []byte) (proofSHA256, proofKeccak256 []byte) { return h.kit.Sign(leaf, qrc.AuthorizeMessage(h.id, uint32(leaf), app, payload)) } // register registers h's commit. The test must have made h's account the // caller beforehand: testing.SetRealm only acts on the function that calls // it, so it cannot be called from here. The realm comes second because a // function that takes it first is a crossing function. func register(h *holder, rlm realm) { rootSHA256, rootKeccak256 := h.roots() Register(cross(rlm), rootSHA256, rootKeccak256, qrc.MinHeight) } // tamper returns a copy of proof with one bit of its byte number at flipped. func tamper(proof []byte, at int) []byte { changed := append([]byte(nil), proof...) changed[at] ^= 0x01 return changed } var ( // relayer has nothing to do with any commit. Requests sent from it must // stand on the one-time keys alone. relayer = testing.NewUserRealm(testutils.TestAddress("relayer")) testKey = []byte("these bytes stand for a public key of a scheme chosen after D-day") ) func TestRegister(cur realm, t *testing.T) { alice := testutils.TestAddress("reg-alice") h := newHolder(alice, 0) rootSHA256, rootKeccak256 := h.roots() zero := strings.Repeat("00", qrc.HashSize) accounts, withKey := Count() uassert.False(t, Has(alice)) uassert.Equal(t, "null", Lookup(alice)) // A realm cannot hold a commit, nor register one for the user calling it. testing.SetRealm(testing.NewCodeRealm("gno.land/r/someone/app")) uassert.AbortsContains(t, cur, "only a user account", func() { Register(cross(cur), rootSHA256, rootKeccak256, qrc.MinHeight) }) testing.SetRealm(testing.NewUserRealm(alice)) uassert.AbortsContains(t, cur, "64 hexadecimal characters", func() { Register(cross(cur), rootSHA256[:62], rootKeccak256, qrc.MinHeight) }) uassert.AbortsContains(t, cur, "64 hexadecimal characters", func() { Register(cross(cur), rootSHA256, "zz"+rootKeccak256[2:], qrc.MinHeight) }) uassert.AbortsContains(t, cur, "cannot be all zeros", func() { Register(cross(cur), zero, rootKeccak256, qrc.MinHeight) }) uassert.AbortsContains(t, cur, "cannot be all zeros", func() { Register(cross(cur), rootSHA256, zero, qrc.MinHeight) }) uassert.AbortsContains(t, cur, "two different trees", func() { Register(cross(cur), rootSHA256, rootSHA256, qrc.MinHeight) }) uassert.AbortsContains(t, cur, "height must be between 6 and 16", func() { Register(cross(cur), rootSHA256, rootKeccak256, qrc.MinHeight-1) }) uassert.AbortsContains(t, cur, "height must be between 6 and 16", func() { Register(cross(cur), rootSHA256, rootKeccak256, qrc.MaxHeight+1) }) uassert.False(t, Has(alice), "a rejected registration must leave nothing behind") Register(cross(cur), rootSHA256, rootKeccak256, qrc.MinHeight) uassert.True(t, Has(alice)) nowAccounts, nowWithKey := Count() uassert.Equal(t, accounts+1, nowAccounts) uassert.Equal(t, withKey, nowWithKey) for leaf := 0; leaf < qrc.Leaves(qrc.MinHeight); leaf++ { if IsUsed(alice, leaf) { t.Fatalf("leaf %d is used right after registration", leaf) } } if _, _, ok := Key(alice); ok { t.Fatalf("a key is bound right after registration") } uassert.Equal(t, "", KeyBase64(alice)) // The usual key of the account has no say over the commit any more. other := newHolder(alice, 1) otherSHA256, otherKeccak256 := other.roots() uassert.AbortsContains(t, cur, "already has a commit", func() { Register(cross(cur), otherSHA256, otherKeccak256, qrc.MinHeight) }) uassert.True(t, strings.Contains(Lookup(alice), `"rootSHA256":"`+rootSHA256+`"`)) } func TestBind(cur realm, t *testing.T) { alice := testutils.TestAddress("bind-alice") mallory := testutils.TestAddress("bind-mallory") h := newHolder(alice, 0) testing.SetRealm(testing.NewUserRealm(alice)) register(h, cur) _, withKey := Count() proofSHA256, proofKeccak256 := h.signBind(3, "ML-DSA-65", testKey) // Nothing here depends on who sends the transaction. testing.SetRealm(relayer) uassert.AbortsContains(t, cur, "no commit", func() { Bind(cross(cur), mallory, 3, "ML-DSA-65", testKey, proofSHA256, proofKeccak256) }) uassert.AbortsContains(t, cur, "leaf out of range", func() { Bind(cross(cur), alice, 64, "ML-DSA-65", testKey, proofSHA256, proofKeccak256) }) uassert.AbortsContains(t, cur, "leaf out of range", func() { Bind(cross(cur), alice, -1, "ML-DSA-65", testKey, proofSHA256, proofKeccak256) }) uassert.AbortsContains(t, cur, "key type", func() { Bind(cross(cur), alice, 3, "", testKey, proofSHA256, proofKeccak256) }) uassert.AbortsContains(t, cur, "key type", func() { Bind(cross(cur), alice, 3, "ML DSA <65>", testKey, proofSHA256, proofKeccak256) }) uassert.AbortsContains(t, cur, "key type", func() { Bind(cross(cur), alice, 3, "-ML-DSA-65", testKey, proofSHA256, proofKeccak256) }) uassert.AbortsContains(t, cur, "key type", func() { Bind(cross(cur), alice, 3, strings.Repeat("k", MaxKeyTypeSize+1), testKey, proofSHA256, proofKeccak256) }) uassert.AbortsContains(t, cur, "key must be between", func() { Bind(cross(cur), alice, 3, "ML-DSA-65", nil, proofSHA256, proofKeccak256) }) uassert.AbortsContains(t, cur, "key must be between", func() { Bind(cross(cur), alice, 3, "ML-DSA-65", make([]byte, MaxKeySize+1), proofSHA256, proofKeccak256) }) // A signature covers the leaf, the label and every byte of the key. uassert.AbortsContains(t, cur, "sha256 proof rejected", func() { Bind(cross(cur), alice, 4, "ML-DSA-65", testKey, proofSHA256, proofKeccak256) }) uassert.AbortsContains(t, cur, "sha256 proof rejected", func() { Bind(cross(cur), alice, 3, "ML-DSA-87", testKey, proofSHA256, proofKeccak256) }) uassert.AbortsContains(t, cur, "sha256 proof rejected", func() { Bind(cross(cur), alice, 3, "ML-DSA-65", append([]byte("x"), testKey...), proofSHA256, proofKeccak256) }) uassert.AbortsContains(t, cur, "sha256 proof rejected", func() { Bind(cross(cur), alice, 3, "ML-DSA-65", tamper(testKey, len(testKey)-1), proofSHA256, proofKeccak256) }) // Both trees must agree: one valid proof is not enough. uassert.AbortsContains(t, cur, "sha256 proof rejected", func() { Bind(cross(cur), alice, 3, "ML-DSA-65", testKey, tamper(proofSHA256, 0), proofKeccak256) }) uassert.AbortsContains(t, cur, "keccak256 proof rejected", func() { Bind(cross(cur), alice, 3, "ML-DSA-65", testKey, proofSHA256, tamper(proofKeccak256, 0)) }) uassert.AbortsContains(t, cur, "keccak256 proof rejected", func() { Bind(cross(cur), alice, 3, "ML-DSA-65", testKey, proofSHA256, tamper(proofKeccak256, qrc.SignatureSize+5)) }) uassert.AbortsContains(t, cur, "sha256 proof rejected", func() { Bind(cross(cur), alice, 3, "ML-DSA-65", testKey, proofKeccak256, proofSHA256) }) uassert.AbortsContains(t, cur, "keccak256 proof rejected", func() { Bind(cross(cur), alice, 3, "ML-DSA-65", testKey, proofSHA256, proofSHA256) }) uassert.AbortsContains(t, cur, "sha256 proof rejected: qrc: proof has the wrong size", func() { Bind(cross(cur), alice, 3, "ML-DSA-65", testKey, proofSHA256[:len(proofSHA256)-1], proofKeccak256) }) uassert.AbortsContains(t, cur, "keccak256 proof rejected: qrc: proof has the wrong size", func() { Bind(cross(cur), alice, 3, "ML-DSA-65", testKey, proofSHA256, nil) }) // Keys of another account's commit are useless here. stranger := newHolder(mallory, 0) strangerSHA256, strangerKeccak256 := stranger.signBind(3, "ML-DSA-65", testKey) uassert.AbortsContains(t, cur, "sha256 proof rejected", func() { Bind(cross(cur), alice, 3, "ML-DSA-65", testKey, strangerSHA256, strangerKeccak256) }) // None of the rejected requests cost the account a one-time key. for leaf := 0; leaf < qrc.Leaves(qrc.MinHeight); leaf++ { if IsUsed(alice, leaf) { t.Fatalf("leaf %d was burned by a rejected request", leaf) } } if _, _, ok := Key(alice); ok { t.Fatalf("a rejected request bound a key") } sent := append([]byte(nil), testKey...) Bind(cross(cur), alice, 3, "ML-DSA-65", sent, proofSHA256, proofKeccak256) keyType, got, ok := Key(alice) uassert.True(t, ok) uassert.Equal(t, "ML-DSA-65", keyType) uassert.Equal(t, string(testKey), string(got)) uassert.Equal(t, base64.StdEncoding.EncodeToString(testKey), KeyBase64(alice)) uassert.True(t, IsUsed(alice, 3)) uassert.False(t, IsUsed(alice, 2)) uassert.False(t, IsUsed(alice, 4)) _, nowWithKey := Count() uassert.Equal(t, withKey+1, nowWithKey) // The stored key shares no memory with what came in or with what goes out. sent[0] ^= 0xff got[1] ^= 0xff _, again, _ := Key(alice) uassert.Equal(t, string(testKey), string(again)) // A one-time key signs once. uassert.AbortsContains(t, cur, "already used", func() { Bind(cross(cur), alice, 3, "ML-DSA-65", testKey, proofSHA256, proofKeccak256) }) // Binding again, with a fresh leaf, replaces the key. second := []byte("a second public key") secondSHA256, secondKeccak256 := h.signBind(63, "SLH-DSA-SHA2-128s", second) Bind(cross(cur), alice, 63, "SLH-DSA-SHA2-128s", second, secondSHA256, secondKeccak256) keyType, got, _ = Key(alice) uassert.Equal(t, "SLH-DSA-SHA2-128s", keyType) uassert.Equal(t, string(second), string(got)) uassert.True(t, IsUsed(alice, 63)) _, nowWithKey = Count() uassert.Equal(t, withKey+1, nowWithKey, "an account counts once however many keys it bound") } func TestRotate(cur realm, t *testing.T) { alice := testutils.TestAddress("rot-alice") first := newHolder(alice, 0) second := newHolder(alice, 1) testing.SetRealm(testing.NewUserRealm(alice)) register(first, cur) firstSHA256, firstKeccak256 := first.roots() secondSHA256, secondKeccak256 := second.roots() testing.SetRealm(relayer) keySHA256, keyKeccak256 := first.signBind(0, "ML-DSA-65", testKey) Bind(cross(cur), alice, 0, "ML-DSA-65", testKey, keySHA256, keyKeccak256) proofSHA256, proofKeccak256 := first.signRotate(1, second) // The signature covers both new roots and the height. uassert.AbortsContains(t, cur, "sha256 proof rejected", func() { Rotate(cross(cur), alice, 1, secondKeccak256, secondSHA256, qrc.MinHeight, proofSHA256, proofKeccak256) }) uassert.AbortsContains(t, cur, "sha256 proof rejected", func() { Rotate(cross(cur), alice, 1, secondSHA256, secondKeccak256, qrc.MinHeight+1, proofSHA256, proofKeccak256) }) uassert.AbortsContains(t, cur, "sha256 proof rejected", func() { Rotate(cross(cur), alice, 2, secondSHA256, secondKeccak256, qrc.MinHeight, proofSHA256, proofKeccak256) }) uassert.AbortsContains(t, cur, "keccak256 proof rejected", func() { Rotate(cross(cur), alice, 1, secondSHA256, secondKeccak256, qrc.MinHeight, proofSHA256, tamper(proofKeccak256, 77)) }) uassert.AbortsContains(t, cur, "height must be between 6 and 16", func() { Rotate(cross(cur), alice, 1, secondSHA256, secondKeccak256, 5, proofSHA256, proofKeccak256) }) uassert.AbortsContains(t, cur, "two different trees", func() { Rotate(cross(cur), alice, 1, secondSHA256, secondSHA256, qrc.MinHeight, proofSHA256, proofKeccak256) }) uassert.AbortsContains(t, cur, "no commit", func() { Rotate(cross(cur), testutils.TestAddress("rot-nobody"), 1, secondSHA256, secondKeccak256, qrc.MinHeight, proofSHA256, proofKeccak256) }) // A request to keep a current root is refused even when properly signed. sameSHA256, sameKeccak256 := first.signRotate(1, first) uassert.AbortsContains(t, cur, "never be registered again", func() { Rotate(cross(cur), alice, 1, firstSHA256, firstKeccak256, qrc.MinHeight, sameSHA256, sameKeccak256) }) uassert.False(t, IsUsed(alice, 1)) Rotate(cross(cur), alice, 1, secondSHA256, secondKeccak256, qrc.MinHeight, proofSHA256, proofKeccak256) lookup := Lookup(alice) uassert.True(t, strings.Contains(lookup, `"generation":1`), lookup) uassert.True(t, strings.Contains(lookup, `"rootSHA256":"`+secondSHA256+`"`), lookup) uassert.True(t, strings.Contains(lookup, `"rootKeccak256":"`+secondKeccak256+`"`), lookup) uassert.True(t, strings.Contains(lookup, `"usedCount":0`), lookup) uassert.False(t, IsUsed(alice, 0), "the new trees start with every key unused") uassert.False(t, IsUsed(alice, 1)) // The public key bound before the rotation stays. keyType, got, ok := Key(alice) uassert.True(t, ok) uassert.Equal(t, "ML-DSA-65", keyType) uassert.Equal(t, string(testKey), string(got)) // Every key of the replaced trees is void, used or not. other := []byte("another public key") oldSHA256, oldKeccak256 := first.signBind(2, "ML-DSA-65", other) uassert.AbortsContains(t, cur, "sha256 proof rejected", func() { Bind(cross(cur), alice, 2, "ML-DSA-65", other, oldSHA256, oldKeccak256) }) // Sending the rotation request a second time changes nothing. uassert.AbortsContains(t, cur, "never be registered again", func() { Rotate(cross(cur), alice, 1, secondSHA256, secondKeccak256, qrc.MinHeight, proofSHA256, proofKeccak256) }) // The new trees speak for the account from now on. newSHA256, newKeccak256 := second.signBind(2, "ML-DSA-65", other) Bind(cross(cur), alice, 2, "ML-DSA-65", other, newSHA256, newKeccak256) _, got, _ = Key(alice) uassert.Equal(t, string(other), string(got)) // A retired root never comes back, whichever of the two places it takes. third := newHolder(alice, 2) thirdSHA256, thirdKeccak256 := third.roots() backSHA256, backKeccak256 := second.signRotate(3, first) uassert.AbortsContains(t, cur, "never be registered again", func() { Rotate(cross(cur), alice, 3, firstSHA256, firstKeccak256, qrc.MinHeight, backSHA256, backKeccak256) }) uassert.AbortsContains(t, cur, "never be registered again", func() { Rotate(cross(cur), alice, 3, thirdSHA256, firstSHA256, qrc.MinHeight, backSHA256, backKeccak256) }) uassert.AbortsContains(t, cur, "never be registered again", func() { Rotate(cross(cur), alice, 3, firstKeccak256, thirdKeccak256, qrc.MinHeight, backSHA256, backKeccak256) }) uassert.False(t, IsUsed(alice, 3)) onSHA256, onKeccak256 := second.signRotate(3, third) Rotate(cross(cur), alice, 3, thirdSHA256, thirdKeccak256, qrc.MinHeight, onSHA256, onKeccak256) uassert.True(t, strings.Contains(Lookup(alice), `"generation":2`)) } func TestAuthorize(cur realm, t *testing.T) { const ( vault = "gno.land/r/someone/vault" thief = "gno.land/r/someone/thief" ) alice := testutils.TestAddress("auth-alice") h := newHolder(alice, 0) testing.SetRealm(testing.NewUserRealm(alice)) register(h, cur) payload := []byte("withdraw 100ugnot to g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") proofSHA256, proofKeccak256 := h.signAuthorize(7, vault, payload) // A user cannot spend a key through Authorize, whatever it is signed for. testing.SetRealm(relayer) uassert.AbortsContains(t, cur, "only a realm", func() { Authorize(cross(cur), alice, 7, payload, proofSHA256, proofKeccak256) }) testing.SetRealm(testing.NewUserRealm(alice)) uassert.AbortsContains(t, cur, "only a realm", func() { Authorize(cross(cur), alice, 7, payload, proofSHA256, proofKeccak256) }) // An approval given to one realm is useless to another. testing.SetRealm(testing.NewCodeRealm(thief)) uassert.AbortsContains(t, cur, "sha256 proof rejected", func() { Authorize(cross(cur), alice, 7, payload, proofSHA256, proofKeccak256) }) testing.SetRealm(testing.NewCodeRealm(vault)) uassert.AbortsContains(t, cur, "sha256 proof rejected", func() { Authorize(cross(cur), alice, 7, []byte("withdraw 999ugnot"), proofSHA256, proofKeccak256) }) uassert.AbortsContains(t, cur, "sha256 proof rejected", func() { Authorize(cross(cur), alice, 8, payload, proofSHA256, proofKeccak256) }) uassert.AbortsContains(t, cur, "no commit", func() { Authorize(cross(cur), testutils.TestAddress("auth-nobody"), 7, payload, proofSHA256, proofKeccak256) }) uassert.False(t, IsUsed(alice, 7)) Authorize(cross(cur), alice, 7, payload, proofSHA256, proofKeccak256) uassert.True(t, IsUsed(alice, 7)) uassert.AbortsContains(t, cur, "already used", func() { Authorize(cross(cur), alice, 7, payload, proofSHA256, proofKeccak256) }) // A proof made for Authorize cannot bind a key, and the reverse. testing.SetRealm(relayer) authSHA256, authKeccak256 := h.signAuthorize(9, vault, testKey) uassert.AbortsContains(t, cur, "sha256 proof rejected", func() { Bind(cross(cur), alice, 9, vault, testKey, authSHA256, authKeccak256) }) bindSHA256, bindKeccak256 := h.signBind(9, vault, testKey) testing.SetRealm(testing.NewCodeRealm(vault)) uassert.AbortsContains(t, cur, "sha256 proof rejected", func() { Authorize(cross(cur), alice, 9, testKey, bindSHA256, bindKeccak256) }) uassert.False(t, IsUsed(alice, 9)) } // TestLatest registers more accounts than the home page lists and checks // that it shows the last ones, newest first. A registration only needs two // roots, so no key is derived here. func TestLatest(cur realm, t *testing.T) { const count = latestKept + 2 accounts := make([]address, count) before, _ := Count() for i := range accounts { accounts[i] = testutils.TestAddress("latest-" + string(rune('a'+i))) rootSHA256 := sha256.Sum256([]byte("sha256 root of " + accounts[i].String())) rootKeccak256 := sha256.Sum256([]byte("keccak256 root of " + accounts[i].String())) testing.SetRealm(testing.NewUserRealm(accounts[i])) Register(cross(cur), hex.EncodeToString(rootSHA256[:]), hex.EncodeToString(rootKeccak256[:]), qrc.MinHeight+i%3) } after, _ := Count() uassert.Equal(t, before+count, after) uassert.Equal(t, latestKept, len(latest)) home := Render("") at := 0 for i := count - 1; i >= count-latestKept; i-- { next := strings.Index(home[at:], accounts[i].String()) if next < 0 { t.Fatalf("account %d is missing or out of order on the home page:\n%s", i, home) } at += next } for i := 0; i < count-latestKept; i++ { uassert.False(t, strings.Contains(home, accounts[i].String()), "an account that left the list is still shown") uassert.True(t, Has(accounts[i]), "an account that left the list lost its commit") } uassert.True(t, strings.Contains(home, "| "+strconv.Itoa(after)+" | "+accounts[count-1].String()+" | 0 of "), home) // Latest lists the same accounts as the home page, in the same order, one // JSON object each. list := Latest() uassert.True(t, strings.HasPrefix(list, "[{"), list) uassert.True(t, strings.HasSuffix(list, "}]"), list) uassert.Equal(t, latestKept, strings.Count(list, `{"account":`), list) at = 0 for i := count - 1; i >= count-latestKept; i-- { object := `{"account":"` + accounts[i].String() + `","rank":` + strconv.Itoa(before+i+1) + `,"height":` + strconv.Itoa(qrc.MinHeight+i%3) + `,"usedCount":0,"generation":0}` next := strings.Index(list[at:], object) if next < 0 { t.Fatalf("account %d is missing, wrong or out of order in Latest:\n%s", i, list) } at += next + len(object) } for i := 0; i < count-latestKept; i++ { uassert.False(t, strings.Contains(list, accounts[i].String()), "an account that left the list is still in Latest") } } func TestViews(cur realm, t *testing.T) { alice := testutils.TestAddress("view-alice") nobody := testutils.TestAddress("view-nobody") h := newHolder(alice, 0) rootSHA256, rootKeccak256 := h.roots() uassert.True(t, strings.Contains(Render(nobody.String()), "**No commit.**")) uassert.True(t, strings.Contains(Render("account?address="+nobody.String()), "**No commit.**")) testing.SetRealm(testing.NewUserRealm(alice)) register(h, cur) testing.SetRealm(relayer) home := Render("") uassert.True(t, strings.Contains(home, "# Quantum Recovery Commits"), home) uassert.True(t, strings.Contains(home, alice.String()), home) uassert.True(t, strings.Contains(home, " | "+alice.String()+" | 0 of 64 | none yet |"), home) page := Render(alice.String()) uassert.True(t, strings.Contains(page, "**Recoverable.**"), page) uassert.True(t, strings.Contains(page, rootSHA256), page) uassert.True(t, strings.Contains(page, rootKeccak256), page) uassert.True(t, strings.Contains(page, "| One-time keys | 64, 0 used |"), page) uassert.True(t, strings.Contains(page, "| New public key | none yet |"), page) uassert.Equal(t, page, Render("account?address="+alice.String())) proofSHA256, proofKeccak256 := h.signBind(10, "ML-DSA-65", testKey) Bind(cross(cur), alice, 10, "ML-DSA-65", testKey, proofSHA256, proofKeccak256) digest := sha256.Sum256(testKey) page = Render(alice.String()) uassert.True(t, strings.Contains(page, "| One-time keys | 64, 1 used |"), page) uassert.True(t, strings.Contains(page, "`ML-DSA-65`, 65 bytes, SHA-256 `"+hex.EncodeToString(digest[:])+"`"), page) uassert.True(t, strings.Contains(page, "with one-time key 10"), page) // Leaf 10 is bit 2 of the second byte of the bitmap. lookup := Lookup(alice) uassert.True(t, strings.HasPrefix(lookup, `{"account":"`+alice.String()+`","rank":`), lookup) uassert.True(t, strings.Contains(lookup, `"generation":0,"rotatedAt":0,"height":6,`), lookup) uassert.True(t, strings.Contains(lookup, `"used":"0004000000000000","usedCount":1,`), lookup) uassert.True(t, strings.Contains(lookup, `"keyType":"ML-DSA-65","keySize":65,"keySHA256":"`+hex.EncodeToString(digest[:])+`","keyLeaf":10,`), lookup) uassert.True(t, strings.HasSuffix(lookup, `,"keyBinds":1}`), lookup) // Latest gives the label of the bound key, and nothing about the key. list := Latest() uassert.True(t, strings.Contains(list, `"account":"`+alice.String()+`","rank":`), list) uassert.True(t, strings.Contains(list, `,"height":6,"usedCount":1,"generation":0,"keyType":"ML-DSA-65"}`), list) uassert.False(t, strings.Contains(list, hex.EncodeToString(digest[:])), list) // Whatever the path holds, the page never repeats it unless it is an address. for _, path := range []string{ "", "account?address=", "account", "account?", "g1notanaddress", "[click](https://example.com)", alice.String() + "/extra", } { page := Render(path) uassert.True(t, strings.HasPrefix(page, "# Not an address"), path) uassert.False(t, strings.Contains(page, "script"), path) uassert.False(t, strings.Contains(page, "example.com"), path) } } // bech32mOf returns account with the bech32m checksum in place of the bech32 // one: the same 20 bytes, which the chain reads as the same account. func bech32mOf(account address) address { hrp, data, err := bech32.Decode(account.String()) if err != nil { panic(err) } spelled, err := bech32.EncodeM(hrp, data) if err != nil { panic(err) } return address(spelled) } // TestSpellings writes an address in capitals, as a QR code does, and with // the bech32m checksum. The chain reads all four spellings as one account, so // Bind, Rotate and the views must as well, while Authorize takes only the // spelling the chain writes, so that the calling realm holds the account that // signed. What is signed always names the account in lower case. func TestSpellings(cur realm, t *testing.T) { const vault = "gno.land/r/someone/vault" alice := testutils.TestAddress("caps-alice") loud := address(strings.ToUpper(alice.String())) quiet := bech32mOf(alice) loudQuiet := address(strings.ToUpper(quiet.String())) others := []address{loud, quiet, loudQuiet} spellings := []address{alice, loud, quiet, loudQuiet} mixed := address(strings.ToUpper(alice.String()[:20]) + alice.String()[20:]) for _, s := range spellings { uassert.True(t, s.IsValid(), "the chain takes "+s.String()) uassert.Equal(t, alice.String(), canonical(s).String(), s.String()) } uassert.False(t, mixed.IsValid(), "the chain refuses an address in mixed case") uassert.Equal(t, mixed.String(), canonical(mixed).String()) uassert.Equal(t, "", canonical(address("")).String()) uassert.Equal(t, "1234", canonical(address("1234")).String()) uassert.Equal(t, "