我正在尝试从我的 React 应用程序连接我的 coinbase 钱包扩展。我已遵循指导如何使用 @coinbase/wallet-sdk 执行此操作的官方文档,但我无法连接到钱包,也无法在 chrome 中打开扩展程序的弹出窗口。
这是我正在使用的功能,
const connectToCoinBase = async () => {
const APP_NAME = "Lottery";
const APP_LOGO_URL = "";
const DEFAULT_ETH_JSONRPC_URL =
"https://rinkeby.infura.io/v3/my-api-key";
const DEFAULT_CHAIN_ID = 4;
const coinbaseWallet = new CoinbaseWalletSDK({
appName: APP_NAME,
appLogoUrl: APP_LOGO_URL,
darkMode: false,
});
const ethereum = coinbaseWallet.makeWeb3Provider(
DEFAULT_ETH_JSONRPC_URL,
DEFAULT_CHAIN_ID
);
]
const account = ethereum.request({ method: "eth_requestAccounts" }); //gives undefined
const web3 = new Web3(ethereum);
};
扩展程序打开时如何弹出窗口?连接成功后如何从钱包中获取地址?
无需使用coinbase SDK即可连接。 尝试使用以太坊 API 进行连接。它连接到 Coinbase 钱包和 MetaMask。
try {
const { ethereum } = window as any
if (ethereum) {
await ethereum.request({ method: 'eth_requestAccounts' })
const web3js = new Web3(ethereum)
ethereum.enable
const accounts = await web3js.eth.getAccounts();
return true
}else {
console.log('Wallet is not installed!');
}
} catch (err) {
console.log('Failed connecting to wallet: ', err)
return false
}