Files
iam/internal/wallet/anchor_test.go
zeekayandHanzo Dev e8d2f5bdf0 Pin the derivation, the call data, and the wallet a claim names
The derivation is pinned rather than described, because both live subject
shapes reach it and the natural key is the whole reason the rendering exists.
Injectivity is asserted over real subjects rather than argued: two people must
never derive one identifier, and the claim rests on the alphabets not
overlapping.

The chain leg is exercised against a stub node, because what has to be right —
the call data, the signature, the order — needs no block. The four selectors
are pinned against an independent keccak implementation over the same
signatures, so a change to a signature string is a red test rather than a
silent revert. The dynamic-tuple layouts are pinned byte for byte: an offset
computed from the wrong base still produces well-formed call data.

Raw transactions are DECODED and re-verified rather than compared to what the
encoder was handed. The test carries a small RLP list reader for exactly that
reason — the assertion is over the bytes a node would parse, and the signer is
recovered from a rebuilt EIP-155 payload, which is the only thing the
registry's access control actually sees.

The claims are read off a token minted through the real confidential flow, not
a hand-built Identity: resolving them in identityOf is what makes every mint
path carry them, and only a real mint proves it. UserInfo is checked to agree
with the token, and then to DISAGREE with a stale one — a wallet unlinked a
minute ago stops counting immediately, which is why it reads the store rather
than echoing the claim.

The unlink tests pin the fault that was live: the last-credential rule counted
"does this account have a wallet", so an account whose only credential was one
wallet read that wallet as proof removing it was safe. Beside it, the arm that
keeps the fix honest — a second wallet IS another way in, so that unlink is
permitted.

The anchor-off tests exist because "optional" has to mean the link path is
indistinguishable with it off, or every deployment that has not configured a
registry is running a second, untested path.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-04 00:58:41 -07:00

70 lines
2.4 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package wallet
import (
"testing"
wc "github.com/luxwallet/connect/go/walletconnect"
)
// Anchoring is optional, and "optional" has to mean the link path is
// indistinguishable with it off — otherwise every deployment that has not
// configured a registry is running a second, untested code path.
// No settings, no registry. The link path calls Report on the nil value, and a
// nil *did.Registry has to tolerate that: it is what keeps a branch out of the
// handler.
func TestAnchorIsOffWithoutSettings(t *testing.T) {
if r := registry(); r != nil {
t.Fatalf("a registry was built with no settings named: controller %s", r.Controller())
}
}
// The whole link, with no registry: the wallet is bound, the caller is signed
// in, and nothing waited on a chain. This is the state every deployment is in
// until one is configured, so it is the state that has to be pinned.
func TestLinkSucceedsWithNoRegistry(t *testing.T) {
app, db := newServer(t)
a := seed(t, db, opts{signup: false})
u, tok := bearer(t, db, a, "hanzo", "alice")
ch := mintFor(t, app, "evm")
msg, sig := signWith(t, signer(t), ch, wc.ChainEVM, addr)
code, m := post(t, app, PathVerify, body(a, wc.ChainEVM, addr, msg, sig), map[string]string{
"Origin": "https://" + host,
"Authorization": "Bearer " + tok,
})
if got := okData(t, code, m); got != "hanzo/alice" {
t.Fatalf("data = %q, want the session identity", got)
}
w := wallets(t, db)
if len(w) != 1 || w[0].User != u.Name || w[0].Address != addr {
t.Fatalf("wallet = %+v, want it bound to the session user", w)
}
}
// Signing in with a wallet already on file binds nothing new, which is what
// keeps a re-login from adding a duplicate verification method to the on-chain
// document every time. The fact is carried by signin.bound; this pins the store
// side of it, which is what bound is derived from.
func TestReloginBindsNothingNew(t *testing.T) {
app, db := newServer(t)
a := seed(t, db, opts{signup: true})
if _, m := signIn(t, app, a); m["status"] != "ok" {
t.Fatalf("first sign-in: %v", m["msg"])
}
first := wallets(t, db)
if len(first) != 1 {
t.Fatalf("first sign-in bound %d wallets, want 1", len(first))
}
if _, m := signIn(t, app, a); m["status"] != "ok" {
t.Fatalf("second sign-in: %v", m["msg"])
}
if again := wallets(t, db); len(again) != 1 {
t.Fatalf("second sign-in left %d wallets, want the same 1", len(again))
}
}