如何连接到 Metamask 或 Coinbase 钱包?
How to connect to either Metamask or Coinbase wallet?
如何分别指定与 Coinbase 钱包和 Metamask 的连接?
现在,当使用 window.ethereum.enable() 时,Metamask 和 Coinbase 钱包扩展弹出窗口。我想要两个单独的按钮,一个用于 Metamask,另一个用于 Coinbase 钱包。
我的代码:
const { createAlchemyWeb3 } = require("@alch/alchemy-web3");
const web3 = createAlchemyWeb3(alchemyKey);
export const connectWallet = async () => {
if (window.ethereum) { //check if Metamask is installed
try {
const address = await window.ethereum.enable(); //connect Metamask
}
}
})
从 this discussion 开始,您似乎可以使用不同的 Metamask 请求方法来强制选择连接:
const connectWallet = async () => {
if (window.ethereum) { //check if Metamask is installed
try {
const address = await window.ethereum.request({
method: 'wallet_requestPermissions',
params: [{ eth_accounts: {}}]
});
} catch (error) {
console.log(error);
}
}
}
wallet_requestPermissions
似乎可以解决问题。
如何分别指定与 Coinbase 钱包和 Metamask 的连接?
现在,当使用 window.ethereum.enable() 时,Metamask 和 Coinbase 钱包扩展弹出窗口。我想要两个单独的按钮,一个用于 Metamask,另一个用于 Coinbase 钱包。
我的代码:
const { createAlchemyWeb3 } = require("@alch/alchemy-web3");
const web3 = createAlchemyWeb3(alchemyKey);
export const connectWallet = async () => {
if (window.ethereum) { //check if Metamask is installed
try {
const address = await window.ethereum.enable(); //connect Metamask
}
}
})
从 this discussion 开始,您似乎可以使用不同的 Metamask 请求方法来强制选择连接:
const connectWallet = async () => {
if (window.ethereum) { //check if Metamask is installed
try {
const address = await window.ethereum.request({
method: 'wallet_requestPermissions',
params: [{ eth_accounts: {}}]
});
} catch (error) {
console.log(error);
}
}
}
wallet_requestPermissions
似乎可以解决问题。