Skip to content

fix(erc8128): add setIfNotExists to SecondaryStorage, use atomic nonce consumption#2

Open
jacobot01 wants to merge 1 commit intoslice-so:erc8128from
jacobot01:fix/erc8128-setnx-nonce
Open

fix(erc8128): add setIfNotExists to SecondaryStorage, use atomic nonce consumption#2
jacobot01 wants to merge 1 commit intoslice-so:erc8128from
jacobot01:fix/erc8128-setnx-nonce

Conversation

@jacobot01
Copy link
Collaborator

Problem

When using the erc8128 plugin with Redis as secondaryStorage, nonce consumption does GET + SET (2 round-trips) instead of a single atomic SET NX.

Changes

packages/core/src/db/type.ts

  • Added optional setIfNotExists method to the SecondaryStorage interface

packages/better-auth/src/plugins/erc8128/nonce-store.ts

  • createSecondaryStorageNonceStore now uses storage.setIfNotExists() when available (single atomic SET NX command)
  • Falls back to existing get + set pattern for storage implementations that don't support it

Impact

  • Halves nonce verification round-trips (2 → 1)
  • Eliminates race window between GET and SET
  • Fully backwards compatible (optional interface method with fallback)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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