polkadot-js / extension

Simple browser extension for managing Polkadot and Substrate network accounts in a browser. Allows the signing of extrinsics using these accounts. Also provides a simple interface for compliant extensions for dapps.
Apache License 2.0
971 stars 414 forks source link

Enable Selective Wallet Pairing in web3Enable via Web3AccountsOptions #1391

Closed IsaccoSordo closed 2 months ago

IsaccoSordo commented 2 months ago

Calling web3Enable asks for permissions from all available extensions in a user's browser. This can become undesirable when a user has multiple extensions, as all the wallets will prompt an alert to ask for user permissions.

Additionally, our frontend differentiates between wallets. This means that if a user clicks a button to pair with a specific wallet, all the other Polkadot wallets will still ask for permissions.

Would it be possible to add Web3AccountsOptions to web3Enable so that a developer can programmatically choose which wallet to pair with?

We have a custom alert that allows a user to choose one wallet instead of another. With the current behavior, it is impossible to pair only with the selected wallet.

Tbaut commented 2 months ago

Hi, thanks for the issue. Please see the discussion here to achieve what you want: https://github.com/polkadot-js/extension/pull/1382#issuecomment-2199747134

IsaccoSordo commented 2 months ago

Thank you @Tbaut for the reply. While .enable() on the target extension works, any subsequent web3 function calls fail. For instance, we need to display the list of available accounts, which we accomplished using web3Accounts. We noticed that the object returned from enable() contains a property named accounts, but it is an empty object. Could you point me towards a documentation page that could help with this issue?

Tbaut commented 2 months ago

I don't think that there are much documentation around this, that's why I recommend to use tools that leverage it so as not to re-invent the wheel. Here are some others:

IsaccoSordo commented 2 months ago

Ultimately, we ended up using @talisman-connect as it integrates pretty nicely with our codebase. Thank you very much for all the support.

Tbaut commented 2 months ago

We noticed that the object returned from enable() contains a property named accounts, but it is an empty object.

For anyone reading this later, and willing to do the whole thing themselves. You can't use web3Accounts, because it indeed goes through all the extension, you have to selectively call enable on the ones you want, then you get objects with the accounts field that you have to get() it, like this.

window.injectedWeb3["polkadot-js"]
  .enable("app-name").then(o => 
    o.accounts.get()
      .then(console.log)
  )
// Array [ {…} ]
polkadot-js-bot commented 2 months ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue if you think you have a related problem or query.