Skip to content

fix(send): hydrate numPubkeys from server-authoritative num_sends - #125

Closed
TaprootFreak wants to merge 1 commit into
developfrom
fix/sync-num-pubkeys-from-balance
Closed

TaprootFreak wants to merge 1 commit into
developfrom
fix/sync-num-pubkeys-from-balance

Conversation

@TaprootFreak

Copy link
Copy Markdown
Contributor

Summary

Companion to zk-coins/node #129 — fixes 07-send.spec.ts::send-success failing on DEV with Interner Fehler: Vorheriger Public Key fehlt. (server prev_commitment_pubkey required for account update, 400).

The wallet's BIP-32 child-index counter (numPubkeys) is reset to 0 on every seed restore — but the server may still hold account.proof = Some(...) from a previous session for the same address. The next send then either omits prev_commitment_pubkey (→ 400) or reuses pubkey[0] and collides on the SMT slot at commit time.

Fix: read the counter from the server.

  • BalanceResponseSchema extended with num_sends: z.number().default(0).
  • useWalletStore adds syncNumPubkeys(n) (replace-semantics + no-op shortcut).
  • SendPage runs api.balance BEFORE signing every send, hydrates numPubkeys from num_sends, then signs with the correct index.
  • WalletScreen 5-s poll + onboarding (seed restore + passkey restore) + post-send balance refresh all sync.

Dependencies

This PR must wait for zk-coins/node #129 to merge + DEV-deploy. The schema's .default(0) keeps the fallback safe if the wire-format is missing the field, but the actual fix requires the server emitting it.

Test plan

The wallet tracks the BIP-32 child-index counter (`numPubkeys`)
purely in local state. After a seed restore the counter is reset
to 0 — but the server may still hold `account.proof = Some(...)`
from a previous session for the same address. The next /send then
either omits `prev_commitment_pubkey` (server 400
`"prev_commitment_pubkey required for account update"`, mapped
client-side to `Interner Fehler: Vorheriger Public Key fehlt.`)
or reuses pubkey[0] and collides on the same SMT slot at commit
time. The E2E `07-send.spec.ts::send-success` test exposed this
on every retry after the first global-setup minted balance was
spent.

This is the app side of zk-coins/node PR #129, which adds the
authoritative `Account.num_sends` counter and emits it on
`/api/balance`. The app:

* extends `BalanceResponseSchema` with `num_sends: z.number().default(0)`;
* adds `syncNumPubkeys(n)` to the wallet store — a no-op fast-path
  when the counter is already correct, replace semantics otherwise;
* drives a fresh `api.balance` BEFORE every send (SendPage) so the
  counter is hydrated against the server's source of truth even
  if the WalletScreen 5-s poll hasn't ticked yet;
* hydrates `numPubkeys` on every seed/passkey restore flow;
* re-syncs on every WalletScreen balance tick + after the post-send
  /api/balance refresh.

Tests:
* `wallet.test.ts` — `syncNumPubkeys` replace semantics + no-op
  shortcut + multi-account safety.
* `send-pipeline.test.tsx` — pre-send balance hydration is mocked
  in every send-confirm path; the existing `prev_commitment_pubkey`
  + `numPubkeys` assertions still hold.

App-PR depends on node-PR being merged + DEV-deployed first —
otherwise the wallet reads `undefined` for `num_sends` against the
old server. The schema's `.default(0)` keeps the fallback safe
(local counter behaviour preserved).
@github-actions

github-actions Bot commented May 27, 2026

Copy link
Copy Markdown
Contributor

[OK] Button-Inventory-Audit — all clear

Checked 71 testid(s) in src/ against 69 reference(s) in e2e/. Nothing to do.

@TaprootFreak
TaprootFreak marked this pull request as ready for review May 27, 2026 14:48
@TaprootFreak TaprootFreak reopened this May 28, 2026
@TaprootFreak

Copy link
Copy Markdown
Contributor Author

Superseded by zk-coins/node#132 (merged 2026-05-28, server now owns prev_commitment_pubkey as single source of truth — App-side syncNumPubkeys derivation no longer load-bearing). Closing as obsolete; the case is end-to-end covered by api_remote.rs::second_send_succeeds_without_prev_commitment_pubkey_field.

@TaprootFreak
TaprootFreak deleted the fix/sync-num-pubkeys-from-balance branch May 28, 2026 09:32
@TaprootFreak
TaprootFreak restored the fix/sync-num-pubkeys-from-balance branch May 29, 2026 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant