Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix API nonce bug #1398

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions packages/stateful/hooks/useCfWorkerAuthPostRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useWallet } from './useWallet'
// Cloudflare KV is slow to update, so keep track of the last successful nonce
// that worked so we don't have to wait for the nonce query to update. Make this
// a global variable so it persists across all hook uses.
const lastSuccessfulNonceForType: Record<string, number | undefined> = {}
const lastSuccessfulNonceForApi: Record<string, number | undefined> = {}

export const useCfWorkerAuthPostRequest = (
apiBase: string,
Expand Down Expand Up @@ -57,8 +57,7 @@ export const useCfWorkerAuthPostRequest = (

// If nonce was already used, manually increment.
let nonce = nonceResponse.nonce
const lastSuccessfulNonce =
lastSuccessfulNonceForType[signatureType] ?? -1
const lastSuccessfulNonce = lastSuccessfulNonceForApi[apiBase] ?? -1
if (nonce <= lastSuccessfulNonce) {
nonce = lastSuccessfulNonce + 1
}
Expand Down Expand Up @@ -134,7 +133,7 @@ export const useCfWorkerAuthPostRequest = (
}

// If succeeded, store nonce.
lastSuccessfulNonceForType[signatureType] = nonce
lastSuccessfulNonceForApi[apiBase] = nonce

// If response OK, return response body.
return await response.json()
Expand Down