Skip to content

fix(sdk): clear in-memory keys in clearKeysStorage - #502

Open
0xcfloki wants to merge 1 commit into
FhenixProtocol:masterfrom
0xcfloki:fix/clear-keys-storage-in-memory
Open

0xcfloki wants to merge 1 commit into
FhenixProtocol:masterfrom
0xcfloki:fix/clear-keys-storage-in-memory

Conversation

@0xcfloki

@0xcfloki 0xcfloki commented Nov 3, 2026 •

Copy link
Copy Markdown

Summary

clearKeysStorage() on a store from createKeysStore only calls storage.removeItem(...). The zustand store keeps its state, so after a clear:

  • getFheKey / getCrs still return the cleared key and CRS, and fetchKeys keeps using them instead of refetching.
  • The next setFheKey or setCrs (for any chain or zone) persists the whole state again, so the cleared entries are written back to storage.

With an in-memory IStorage, on master:

before clear:     {"state":{"fhe":{"1":{"0":"OLD_KEY"}},"crs":{}},"version":0}
after clear:      (empty)   getFheKey(1, 0) -> "OLD_KEY"
after setCrs(2):  {"state":{"fhe":{"1":{"0":"OLD_KEY"}},"crs":{"2":{"0":"OTHER"}}},"version":0}

Changes

  • packages/sdk/core/keyStore.ts: clearKeysStorage resets the store to { fhe: {}, crs: {} } before removing the persisted item. A store created without storage is cleared too (it was a no-op).
  • packages/sdk/core/test/keyStore.test.ts: two tests, one with an in-memory storage checking that the cleared keys are gone from memory and are not written back by the next set, one for a store without storage. The existing test only checked that removeItem was called.
  • Changeset: @cofhe/sdk patch.

Testing

pnpm exec vitest run --project node in packages/sdk on Node 22.23.4:

  • core/test/keyStore.test.ts: 19 passed. On master the two new tests fail with expected '0xoldkey' to be undefined.
  • Full node project: 626 passed, 2 skipped. The four suites that import @cofhe/test-setup fail to load on my machine on both branches (Windows, tsup spawn, see fix(test-setup): run builds and command fixtures on Windows #494); unrelated.

🤖 Generated with Claude Code


Note

Low Risk
Small behavioral fix in key cache clearing with targeted tests; no auth or crypto logic changes beyond making clear actually empty memory and storage.

Overview
clearKeysStorage on createKeysStore now resets the Zustand store to empty fhe and crs maps, not only storage.removeItem. Before this fix, cleared keys still came back from getFheKey / getCrs, and the next setFheKey or setCrs could re-persist the old entries with new ones.

Stores created without persistence (createKeysStore(null)) are cleared in memory as well; previously clearKeysStorage was effectively a no-op there.

Tests cover that cleared keys stay gone after a subsequent set (with in-memory storage) and that non-persisted stores lose keys after clear. @cofhe/sdk patch changeset.

Reviewed by Cursor Bugbot for commit d870ed1. Bugbot is set up for automated code reviews on this repo. Configure here.

clearKeysStorage only removed the persisted item. The store kept its
state, so getFheKey/getCrs still returned the cleared keys and the next
setFheKey/setCrs persisted the whole state again, bringing them back.
Reset the store state before removing the persisted item.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Nov 3, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: d870ed1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 9 packages
Name Type
@cofhe/sdk Patch
@cofhe/abi Patch
@cofhe/hardhat-3-plugin Patch
@cofhe/hardhat-plugin Patch
@cofhe/mock-contracts Patch
@cofhe/react Patch
@cofhe/site Patch
@cofhe/example-react Patch
@cofhe/foundry-plugin Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

This branch has not been deployed

No deployments
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