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: add check to ensure WC indexedDB store had been previously initialized on disconnect #1149

Merged
merged 4 commits into from
Nov 14, 2024

Conversation

iykazrji
Copy link
Collaborator

@iykazrji iykazrji commented Nov 13, 2024

Pull Request Checklist


PR-Codex overview

This PR focuses on enhancing the clearWalletConnectStore function to handle the scenario where the WALLET_CONNECT_V2_INDEXED_DB may not exist, ensuring the database is deleted if it is outdated.

Detailed summary

  • Added a check for walletConnectDBExists to determine if the database exists.
  • Implemented an onupgradeneeded event to delete the database if the version is outdated.
  • Wrapped the database operations in a try-catch block to handle errors gracefully.

✨ Ask PR-Codex anything about this PR by commenting with /codex {your question}

Copy link

vercel bot commented Nov 13, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
aa-sdk-site ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 14, 2024 6:15pm
aa-sdk-ui-demo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 14, 2024 6:15pm

Copy link

graphite-app bot commented Nov 13, 2024

Your org has enabled the Graphite merge queue for merging into main

Add the label “graphite-merge-queue” to the PR and Graphite will automatically add it to the merge queue when it’s ready to merge.

You must have a Graphite account and log in to Graphite in order to use the merge queue. Sign up using this link.

@iykazrji iykazrji changed the title fix: add check to ensure walletconned indexedDB store had been previously initialized before deleting content on disconnect fix: add check to ensure WC indexedDB store had been previously initialized on disconnect Nov 13, 2024
@iykazrji iykazrji closed this Nov 13, 2024
@iykazrji iykazrji reopened this Nov 13, 2024
@iykazrji iykazrji marked this pull request as ready for review November 13, 2024 17:29
@@ -46,17 +46,36 @@ export async function disconnect(config: AlchemyAccountsConfig): Promise<void> {
// Persistence of Wallet Connect connection state on error.
const clearWalletConnectStore = () => {
// Open Wallet Connect Indexed DB
let walletConnectDBExists = true;
Copy link
Collaborator

Choose a reason for hiding this comment

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

we should probably start with the assumption that it isn't initialized

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure! Would update to reflect this

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually on further testing, it is better we start with the assumption that it is in fact initialized. This is because dbRequest.onUpgradeneeded() isn't actually called on every request, only when there is a DB version change. This means when we call disconnect() after walletConnect has created the IDB store, we wouldn't be running that event handler, and thus if we default to false, we wouldn't have a way to change the value to true.

walletConnectDBExists = false;

// Remove the Database created from the indexedDB.open() call above.
indexedDB.deleteDatabase("WALLET_CONNECT_V2_INDEXED_DB");
Copy link
Collaborator

Choose a reason for hiding this comment

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

wouldn't this throw if this state means it's not connected?

Copy link
Collaborator Author

@iykazrji iykazrji Nov 14, 2024

Choose a reason for hiding this comment

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

Actually, by this point the DB would actually already have been created by the previous indexedDB.open() call made above. It's the behaviour for IDB to create a database if it doesn't exist when we call indexedDB.open(). So it actually wouldn't throw in this case

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see, so lemme run this back to see if I understand

db.open will create a db if it doesn't exist or open the existing one if it does.

if it doesn't exist and thus gets created, that fires the upgrade event here. We know it was created because version is 1 indexed (not 0 indexed?)

Since this only gets called on disconnect, there's no risk that this gets fired when WC first creates the db

@@ -46,17 +46,36 @@ export async function disconnect(config: AlchemyAccountsConfig): Promise<void> {
// Persistence of Wallet Connect connection state on error.
const clearWalletConnectStore = () => {
// Open Wallet Connect Indexed DB
let walletConnectDBExists = true;
const dbOpenRequest = indexedDB.open("WALLET_CONNECT_V2_INDEXED_DB");
Copy link
Collaborator

Choose a reason for hiding this comment

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

do we need to do any other checks here? does indexDb.open ever throw?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, but we're already handling the error case for that with the dbOpenRequest.onerror() handler below

@moldy530 moldy530 linked an issue Nov 14, 2024 that may be closed by this pull request
Copy link
Collaborator

@moldy530 moldy530 left a comment

Choose a reason for hiding this comment

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

LGTM, other sanity check - we don't need to remove the handlers right? they only get attached to this specific open request, not globally? Wanna make sure that, if you call disconnect (previously logged in as email) and then try to connect with WC for the first time, then you won't end up in a state where this handler deletes the DB because this handler is still registered

@iykazrji
Copy link
Collaborator Author

Yes! The handlers are only attached to this dbOpenRequest called only when the disconnect() call happens.

@iykazrji iykazrji merged commit 4377920 into main Nov 14, 2024
6 checks passed
@iykazrji iykazrji deleted the iyk/fix-wallet-connect-disconnect-state branch November 14, 2024 18:19
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.

Getting error while using social login in alchemy account kit.
2 participants