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

Injected wallet discovery #197

Open
fracek opened this issue Oct 19, 2023 · 2 comments
Open

Injected wallet discovery #197

fracek opened this issue Oct 19, 2023 · 2 comments

Comments

@fracek
Copy link
Collaborator

fracek commented Oct 19, 2023

Background

This is a continuation of the discussion started on the new wallet snip.

/cc @avimak @janek26

Problem

At the moment, the only way to check what browser wallet are available is by checking for starknet_* objects in the window context. This creates an issue when the wallet is slow in injecting itself in the page because it may be unavailable when the user checks for it. The most common bug caused by this is that the wallet won't autoconnect on page load.

Proposed solution

The solution is to have an event mechanism for listening to wallets being injected into the window. There are two APIs that we can leverage:

I prefer the second option because it's more explicit.

MutationObserver

This solution requires the least amount of work. If we decide to go down this route, I propose wallets use their wallet id (starknet_*) as the id of the injected script so that it's easy to detect them.

What makes me uncomfortable is that it requires a callback invoked for all mutations to the DOM.

EventTarget

The idea is that when the wallet is injected, it emits an event to signal that to the dapp.

interface AnnounceWalletEvent extends Event {
  type: "starknet:announceWallet"
  detail: Wallet // from get-starknet
}

// on the wallet side
window.dispatchEvent(new CustomEvent("starknet:announceWallet", { detail: this }))


// on the dapp side
window.addEventListener("starknet:announceWallet", loadWallets)
loadWallets() // load already injected wallets
@tabaktoni
Copy link

Like the EventTarget approach, it seems a cleaner solution.
I think get-starknet UI should implement a listener and update UI when the event arrives.

@avimak
Copy link
Collaborator

avimak commented Dec 6, 2023

@naorye2 did you start working on it?

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

No branches or pull requests

3 participants