PLAN TODO 2 EVIDENCE: Fix wallet.transfer signer binding (GAP-3 / F1)
Commit: be848703be375fd007be79ee95e630d8d1ecc58d
Branch: first-place-overhaul-todo2

=== CHANGED FILES ===
 src/lib/ledger.ts               |  56 ++++++++++++++++++------
 src/lib/ledger.test.ts          | 192 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------
 src/lib/ledgerConflicts.test.ts |  34 +++++++++-----

=== WHAT CHANGED ===
src/lib/ledger.ts's computeBalances (isTransferPayload branch): a
wallet.transfer is now applied ONLY when
`entry.payload.from === toAccountId(entry.authorPubkey)` - the signer must
own the account they claim to spend FROM. Imports `toAccountId` from
`../shell/accountId`, mirroring the exact signer-binding pattern already
used by `src/lib/governanceEvents.ts`'s `extractQvBallots`
(`toAccountId(entry.authorPubkey) === payload.voterId`).

A forged transfer (signer != from-account owner) lands in the SAME
`rejected` bucket `computeBalances` already used for credit-limit breaches
- no new rejection channel was invented. `RejectedTransfer` became a
discriminated union of `{ reason: "credit-limit-breach", wouldBeBalance,
creditLimit }` (unchanged) and the new `{ reason: "unauthorized-signer" }`.
The signer check runs BEFORE the credit-limit check, so a forged transfer
never reaches (or leaks) a real account's balance in any way.

No demo-fixture exemption was added or needed: a prior adversarial pass
already verified (and this session's new regression test independently
confirms) that every real `wallet.transfer` producer in `src/` signs with
the same identity whose account it names as `from`
(`src/shell/walletTransfer.ts`, `src/admin/seedEvents.ts`'s
`buildDemoWalletPayloads`). `wallet.issue` and every other payload type's
validation was left untouched.

Two existing test files used an unbound authorPubkey/`from` pairing (the
exact gap this fix closes) and were updated to bind them correctly, not
deleted: `src/lib/ledger.test.ts` and `src/lib/ledgerConflicts.test.ts`
(the double-spend-merge tests build raw entries the same way and hit the
identical gap).

=== NEW REGRESSION TEST (the acceptance-criteria scenario) ===
"rejects a wallet.transfer not signed by the from account's own key (a forged transfer)"
  - issue 100 to `victim` (signed by author 1)
  - wallet.transfer { from: victim, to: attacker, amount: 100 } SIGNED BY
    author 2 (attacker's own key, not victim's) - a forgery
  - asserts: victim's balance stays 100 (UNCHANGED), attacker's balance
    stays 0, the forged transfer lands in `state.rejected` with reason
    "unauthorized-signer".
  This is the exact `computeBalances([issue to victim by author1, transfer
  from:victim to:attacker signed by author2])` scenario from the plan's
  acceptance criteria. It failed before the fix (transfer silently
  applied, victim drained to 0) and passes after.

=== LEGITIMATE-PATH REGRESSION TEST (demo fixture still works) ===
"applies the Hollow demo fixture's wallet.issue + wallet.transfer when both are signed by the demo-loading identity"
  - builds the REAL `buildDemoWalletPayloads(agent)` payloads from
    `src/admin/seedEvents.ts` (not a hand-rolled stand-in), signs both the
    issue and transfer with the SAME agent identity's key (exactly how
    `AdminSeedPanel.tsx` calls `append()` for every demo event), and
    asserts the final balances match the real fixture constants
    (`HOLLOW_DEMO_WALLET_WELCOME_GRANT` - `HOLLOW_DEMO_WALLET_COMMONS_CONTRIBUTION`
    = 40 for the agent, 10 for `hollow-commons-fund`), with zero rejections.

=== COMMAND 1: npx vitest run src/lib/ledger.test.ts --reporter=verbose ===
 RUN  v5.1.0 /home/athapa/patriothacks-wt/first-place-overhaul-todo2

 ✓ src/lib/ledger.test.ts > ledger.computeBalances > replays a sequence of valid transfers to a hand-calculated balance 2ms
 ✓ src/lib/ledger.test.ts > ledger.computeBalances > rejects a transfer that would breach a configured credit limit 1ms
 ✓ src/lib/ledger.test.ts > ledger.computeBalances > defaults the credit limit floor to zero for accounts without one configured 0ms
 ✓ src/lib/ledger.test.ts > ledger.computeBalances > rejects a wallet.transfer not signed by the from account's own key (a forged transfer) 0ms
 ✓ src/lib/ledger.test.ts > ledger.computeBalances > applies the Hollow demo fixture's wallet.issue + wallet.transfer when both are signed by the demo-loading identity 0ms
 ✓ src/lib/ledger.test.ts > ledger.computeBalances > resolves two same-Lamport-timestamp entries deterministically regardless of input order 1ms
 ✓ src/lib/ledger.test.ts > ledger.computeBalances > ignores entries whose payload isn't a recognised wallet event 0ms
 ✓ src/lib/ledger.test.ts > ledger.computeBalances > ignores wallet.issue events with a negative, NaN, or Infinity amount instead of silently applying them 0ms
 ✓ src/lib/ledger.test.ts > ledger.computeBalances > still applies a legitimate zero-amount issue (0 is finite and non-negative) 0ms
 ✓ src/lib/ledger.test.ts > ledger.computeBalances > ignores wallet.transfer events with a negative, NaN, or Infinity amount instead of silently applying them 0ms
 ✓ src/lib/ledger.test.ts > ledger.computeBalances > still applies a legitimate zero-amount transfer (0 is finite and non-negative) 0ms
 ✓ src/lib/ledger.test.ts > ledger.computeBalances > treats an account named like an Object.prototype property identically to any other account name 0ms

 Test Files  1 passed (1)
      Tests  12 passed (12)
   Start at  14:59:56
   Duration  154ms (setup 35%, import 26%, transform 23%, tests 11%, worker 5%)

(10 pre-existing transfer tests + 2 new tests in this file = 12, matching
the file's prior count of 10 + the 2 new scenarios above.)

=== COMMAND 2: npx vitest run src/lib/ledger.test.ts src/lib/ledgerConflicts.test.ts ===
 RUN  v5.1.0 /home/athapa/patriothacks-wt/first-place-overhaul-todo2

 Test Files  2 passed (2)
      Tests  16 passed (16)
   Start at  14:59:52
   Duration  161ms (setup 39%, transform 22%, import 22%, tests 12%, worker 6%)

(ledgerConflicts.test.ts: 4 tests, all still pass after rebinding its
 hand-constructed entries the same way as ledger.test.ts - these were the
 ONE other place in the test suite that hit the same unbound-signer gap;
 confirmed by first reproducing their failure against the fix below.)

=== COMMAND 3 (BEFORE the ledgerConflicts.test.ts rebind - failure caused by this fix, then fixed): npm run test ===
First full run (after ledger.ts + ledger.test.ts changes, BEFORE updating
ledgerConflicts.test.ts) showed 3 failures:
  - src/lib/ledgerConflicts.test.ts > applies the earlier of two conflicting
    same-signer spends and flags the later one for refund  (FAIL - caused
    by this fix: its hand-built entries used an unrelated authorPubkey for
    `from: "A"`, the same gap the plan named)
  - src/lib/ledgerConflicts.test.ts > deduplicates an entry that both
    replicas already share  (FAIL - same cause)
  - src/miniapps/waterwatch/fixtureIsolation.test.ts > the production vite
    build output (dist/) does not contain the fixture's content  (FAIL -
    PRE-EXISTING/unrelated: this test requires `dist/` to already exist
    from a prior `npm run build`; it is not a regression caused by this
    fix, it's a test-ordering artifact of running `npm run test` before
    `npm run build` in a fresh worktree)
 Test Files  2 failed | 87 passed (89)
      Tests  3 failed | 791 passed (794)

Both ledgerConflicts.test.ts failures were fixed by applying the exact
same signer-binding update used in ledger.test.ts (bind `from` to
`toAccountId(authorPubkey)` for every legitimately-applied transfer in
that file). The fixtureIsolation failure resolved itself once `npm run
build` ran (see Command 4below) and is listed only for disclosure, not as
a regression this fix introduced.

=== COMMAND 4: npm run build ===
> the-commons-hollow@0.0.0 build
> tsc -b && vite build

vite v8.3.8 building client environment for production...
transforming...
✓ 291 modules transformed.
rendering chunks...
computing gzip size...
dist/manifest.webmanifest                            0.51 kB
dist/index.html                                      0.69 kB
dist/assets/index-DbEfal6P.css                      62.67 kB
dist/assets/index-Bxatjh7Y.js                    1,064.53 kB
✓ built in 538ms

PWA v2.0.0
mode      generateSW
precache  20 entries (1153.39 KiB)
files generated
  dist/sw.js
  dist/workbox-2fbc6a65.js

Exit code: 0 (tsc type-check gate passed, no type errors)

=== COMMAND 5 (final, post-build): npm run test ===
 Test Files  89 passed (89)
      Tests  794 passed (794)
   Start at  14:59:59
   Duration  6.32s

(Baseline before this todo was ~792 tests per the plan; this run shows 794
 - exactly +2 for the two new ledger.test.ts regression tests described
 above. No test that was passing before this change now fails; the only
 failing->passing transitions are the 2 new tests. The repeated
 "ECONNREFUSED 127.0.0.1:3000/3001" lines in the console output are
 `trySyncWithBackend()`'s expected silent no-op behavior against an absent
 backend during tests - not failures - matching the project's documented
 anti-pattern guarantee that the backend being unreachable never blocks or
 fails a local test/replay.)

=== REAL-BROWSER QA: legitimate issue -> transfer flow still works post-fix ===
Substitution disclosed: Playwright + the bundled chromium
(/home/athapa/.cache/ms-playwright/chromium-1255/chrome-linux64/chrome)
WAS available in this environment, so the real-browser QA scenario named
in the plan was run directly (no unit-level substitution needed):

1. `npm run build` (above) produced `dist/`.
2. Started `vite preview --port 4173 --strictPort` serving the real built
   app.
3. Launched headless chromium, navigated to `http://localhost:4173/admin`
   (fresh fake-indexeddb-free real IndexedDB profile - this was vite
   preview's real browser context, not a test harness).
4. Clicked "Load Hollow demo". Page confirmed:
     "Demo data loaded into the Event Log."
5. Navigated to `http://localhost:4173/` (the real wallet card, not
   /admin). Page text showed:
     BALANCE
     50
     ...
     Recent activity
       Issued          Verified  +10
       To hollow-commons-fund  Verified  -10
       Issued          Verified  +50
   The -10 `wallet.transfer` to `hollow-commons-fund` (buildDemoWalletPayloads'
   real contribution payload, signed by the demo-loading identity's own
   key) applied correctly and is marked "Verified" - proving the
   legitimate signed-transfer path still works exactly as before the fix.
   (The additional +10 "Issued" line is unrelated demo-fixture activity
   from elsewhere in the seed bundle, not part of this fix's scope.)
6. Screenshot saved to:
   .omo/evidence/first-place-overhaul/task-2-first-place-overhaul-wallet.png
7. Confirmed via the background bash log that `trySyncWithBackend()`
   failing against the absent `/api/sync`/`/api/admin/aggregate` backend
   on :3001 was a silent console-only no-op - no visible crash/toast, the
   app kept rendering the wallet correctly throughout.

No deviation/substitution was needed for this step - a real Playwright
browser session against the real built app was used end-to-end, as the
plan's primary QA path specified.

=== ACCEPTANCE CRITERIA CHECK ===
[x] New regression test asserts computeBalances([issue to victim by
    author1, transfer from:victim to:attacker signed by author2]) leaves
    victim's balance UNCHANGED - PASS (see Command 1 above, test name
    "rejects a wallet.transfer not signed by the from account's own key
    (a forged transfer)").
[x] npm run test exits 0 - PASS (794 passed, 0 failed).
[x] npm run build exits 0 - PASS (tsc + vite build succeeded).
[x] No regression vs. pre-change baseline test count (~792) - PASS: 794 =
    792 + 2 new tests; the only failing->passing transitions are the two
    new regression tests (confirmed by first observing them fail on
    ledgerConflicts.test.ts before applying the identical rebind fix
    there, then pass after).
[x] wallet.issue and every other payload type's validation untouched -
    confirmed by diff: only the isTransferPayload branch of
    computeBalances changed in src/lib/ledger.ts.
