Repository navigation
fix: unique default nonce keys to prevent same-chain userOp nonce collision - #209
Merged
Merged
Conversation
size-limit report 📦
|
vr16x
approved these changes
Jul 23, 2026
…lision getDefaultNonceKey derived the key from Date.now() with a setTimeout(1) spacer to keep concurrently-built same-chain userOps distinct. setTimeout(1) does not guarantee Date.now() advances between the lock-serialized calls, so two userOps in one bundle (e.g. a session install via getSessionQuote PREPARE) could receive the same key and therefore the same nonce, failing the second at execution as a nonce reuse. Make the key strictly increasing per (account, chainId) so uniqueness no longer depends on the wall clock advancing. Adds a regression test that freezes the clock and asserts concurrent calls still return distinct keys. Fixes #208
fichiokaku
force-pushed
the
fix/nonce-key-collision
branch
from
September 1, 2026 13:55
40aa905 to
6331803
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #208
Problem
getSessionQuote({ mode: "PREPARE" })(andgetQuotegenerally) can return a quote whose userOps contain a duplicate nonce for the same account on the same chain. The second userOp then fails at execution as a nonce reuse. The duplicate is present in the quote as returned, so it is assigned at quote construction. Client-reported and intermittent.Root cause
Each userOp gets a nonce key; the sequence is read from chain per key. When no key is passed (the normal path),
getDefaultNonceKeyderived it from the wall clock:All userOps in a bundle fetch their nonces concurrently (
Promise.allinprepareUserOps).setTimeout(1)does not guaranteeDate.now()advances between the lock-serialized calls, so two same-chain userOps can receive the same key, hence the same nonce (both read sequence 0). A session install/enable PREPARE bundle has multiple same-chain userOps, which is where it surfaces.Fix
Make the default key strictly increasing per
(account, chainId)so uniqueness no longer depends on the wall clock advancing. The lock (for serialization) is kept; the fragilesetTimeout(1)spacer is removed.Test
Adds a regression test that freezes
Date.now()(the exact condition the old code failed under) and asserts 50 concurrentgetDefaultNonceKeycalls return distinct keys, plus a strictly-increasing check. Verified it fails on the pre-fix code and passes after.PR-Codex overview
This PR focuses on improving the
getDefaultNonceKeyfunction to ensure unique and strictly increasing nonce keys for concurrent calls, addressing a nonce collision issue caused by previous time-based logic.Detailed summary
getDefaultNonceKeyto ensure unique keys for concurrent calls and strictly increasing keys for sequential calls.getDefaultNonceKeyimplementation to avoid reliance onDate.now()with a timeout, ensuring distinct keys regardless of timer resolution.lastIssuedKeymap to track the last key issued for each(account, chainId).