英文:
Conflicting extensions Metamask and Trust Wallet with window.ethereum
问题
我正在为自己创建一个服务,我希望能够与浏览器扩展Metamask和Trust Wallet以及可能的其他扩展一起使用。然而,我在寻找如何在安装了这两个扩展时调用所需内容方面遇到了问题。
例如,我想要一个用于Metamask的按钮和一个用于Trust Wallet的按钮。单击每个按钮将打开必要的连接窗口(但一次只能连接一个扩展)。
我了解了web3-react,但它不适用于React的更新版本。我正在使用Next.js,但对它的经验还不多。
我知道这是可以实现的。在https://daomaker.com/ 上已经实现了这个功能。也许有人有这方面的经验。
如果Metamask被禁用,那么应该调用Trust Wallet。我尝试过使用window.ethereum和window.web3,但它们似乎只为我打开了Metamask扩展。
英文:
I'm creating a service for myself that I want to be able to work with the browser extensions Metamask and Trust Wallet, and possibly others in the future. However, I'm having trouble finding information on how to call what I need when both extensions are installed.
For example, I want to have a button for Metamask and a button for Trust Wallet. Clicking each button would open the necessary connection window (but only one extension can be connected at a time).
I came across web3-react, but it doesn't work with newer versions of React. I'm working with Next.js and I don't have much experience with it yet.
I know that it's possible to implement this. It's done on https://daomaker.com/. Perhaps someone has experience with this.
If Metamask is disabled, then Trust Wallet should be called instead. I've tried using window.ethereum and window.web3, but they both seem to only open the Metamask extension for me.
答案1
得分: 1
以下是已翻译的内容:
首先,我希望您正在使用 wagmi 包来连接和与钱包进行交互。这使得与钱包的交互变得更加容易。
现在,让我们来谈谈在浏览器中与 "metamask" 和 "trust wallet" 单独进行交互。由于它们都是注入钱包,所以事情会变得有点复杂。
但是我们知道一件事,那就是当我们安装任何钱包扩展时,它们会附加到 window 对象上。
在 "metamask" 的情况下,您可以在 "window.ethereum.isMetaMask" 键中找到它,如果它为 true,那意味着 "metamask" 已经安装为扩展程序。
在 "trustwallet" 的情况下,您可以在 "window.trustwallet" 键中找到它,如果它是一个对象,那意味着 "trustwallet" 已经安装为扩展程序。
现在,使用 wagmi,您可以开始使用它们。
对于 metamask,您可以使用 wagmi 的官方 "Metamask Connector"。
链接: https://wagmi.sh/react/connectors/metaMask
new MetaMaskConnector({
options: { UNSTABLE_shimOnConnectSelectAccount: true, shimDisconnect: true },
});
对于 trust wallet,您可以使用 wagmi 的官方 "Injected Connector" 并在配置中的 getProvider 函数中返回 window.trustwallet 作为值。
链接: https://wagmi.sh/react/connectors/injected
new InjectedConnector({
options: {
shimDisconnect: true,
name: 'Trust Wallet',
getProvider: () =>
typeof window !== 'undefined' ? (window as any).trustwallet : undefined,
},
});
我希望这有所帮助!
英文:
First of all I am hoping you are using wagmi package for connecting and interacting with wallets. It makes interaction with wallets a lot easier.
Now coming to interacting with "metamask" and "trust wallet" separately in browser. Since they both are injected wallets so things get a bit tricky.
But we know one thing for sure that when we install any wallet extension then they gets attached to window object.
In case of "metamask", you can find it in "window.ethereum.isMetaMask" key and if its true then that means "metamask" is installed as an extension.
In case of "trustwallet", you can find it in "window.trustwallet" key and if its an object then that means "trustwallet" is installed as an extension.
Now using wagmi you can start using both of them.
For metamask you can use wagmi's official "Metamask Connector".
Link: https://wagmi.sh/react/connectors/metaMask
new MetaMaskConnector({
options: { UNSTABLE_shimOnConnectSelectAccount: true, shimDisconnect: true },
});
For trust wallet you can use wagmi's official "Injected Connector" and return window.trustwallet as value in getProvider function in configuration.
Link: https://wagmi.sh/react/connectors/injected
new InjectedConnector({
options: {
shimDisconnect: true,
name: 'Trust Wallet',
getProvider: () =>
typeof window !== 'undefined' ? (window as any).trustwallet : undefined,
},
});
I hope this helps!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论