fix(grin): keep the slatepack address from overwriting the signing key

A v0.3.0 wallet registers two different Grin keys in sequence. Bootstrap
stores the Smirk-derived public key, which is what /auth/website/verify
checks a Grin signature against, and a deferred effect then registers the
canonical grin-wallet slatepack address. These are separate derivations,
not two encodings of one key, and both wrote (user_id, 'grin', 'primary').
The second silently replaced the first, so signing in to the website with
Grin could not match any account.

Route the value to its own row instead. A slatepack address is bech32 under
the grin/tgrin HRP and a hex public key cannot start with those letters,
since none of them are hex digits, so the two are separable by construction
rather than by guess. That matters because it fixes wallets that are already
shipped: they keep posting what they post today and land in the right row.

lookup_username picked with an unqualified find on the asset, which returns
an arbitrary row once an asset has two. It now selects by key_type, and for
Grin prefers the slatepack address, falling back to a primary row only while
that row still holds a pre-split address. A sender is never handed a public
key where an address belongs.

The migration is additive: existing bech32 values are copied into the new
row and the primary row is left for the next unlock to rewrite. Nothing is
deleted, so a user who never unlocks again keeps a working address.
This commit is contained in:
2026-09-14 23:03:13 -04:00
parent 985a05adf8
commit ddbba30765
2 changed files with 133 additions and 5 deletions
@@ -0,0 +1,25 @@
-- Split the Grin slatepack address out of the `primary` key row.
--
-- A v0.3.0 wallet registers two DIFFERENT Grin keys in sequence. Bootstrap
-- (`/auth/extension`) stores the Smirk-derived pubkey, hex, which is what
-- `/auth/website/verify` checks a Grin signature against. A deferred effect then
-- re-registers the canonical grin-wallet slatepack address, which is a separate
-- derivation, not another encoding of the same key. Both wrote
-- `(user_id, 'grin', 'primary')`, so the second silently replaced the first and
-- website sign-in with Grin stopped matching any account.
--
-- The slatepack address now gets its own `slatepack` row, so both values coexist:
-- sign-in reads `primary`, senders read `slatepack`, and the address -> user
-- lookup matches either because it does not filter on key_type.
--
-- Additive on purpose. Existing bech32 values are COPIED across and the `primary`
-- row is left in place, to be rewritten with the hex pubkey by the next unlock.
-- Nothing is deleted, so a user who never unlocks again keeps working discovery
-- instead of losing their address to a migration.
INSERT INTO user_keys (user_id, asset, public_key, public_spend_key, key_type)
SELECT user_id, asset, public_key, public_spend_key, 'slatepack'
FROM user_keys
WHERE asset = 'grin'
AND key_type = 'primary'
AND (public_key LIKE 'grin1%' OR public_key LIKE 'tgrin1%')
ON CONFLICT (user_id, asset, key_type) DO NOTHING;