Skip to content

Commit

Permalink
Save last nonce by API and not type. (#1398) (#1399)
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso authored Oct 5, 2023
1 parent c116876 commit deee3e9
Showing 1 changed file with 3 additions and 4 deletions.
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

2 comments on commit deee3e9

@vercel
Copy link

@vercel vercel bot commented on deee3e9 Oct 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on deee3e9 Oct 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.