Skip to content

Injected wallet discovery #197

Description

@franceccon

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions