现在walletconnect v2已经上线了,我们必须升级。 签名消息和获取提供商的新方法是什么?
在版本 1 中我使用的是:
this.web3Modal.clearCachedProvider();
this.provider = await this.web3Modal.connect(); // set provider
this.web3js = new Web3(this.provider); // create web3 instance
this.accounts = await this.web3js.eth.getAccounts();
const signedMessage = await this.web3js.eth.personal.sign('You are login into gamers passport, This action will not cost you any transaction fee.', address);
但是目前,我们只有几种针对 web3modal 的方法,而且没有一个有帮助。
现在在 v2 中,我尝试像这样使用 web3modal:
import { EthereumClient, w3mConnectors, w3mProvider } from '@web3modal/ethereum'
import { Web3Modal } from '@web3modal/html'
import { configureChains, createConfig, getAccount } from '@wagmi/core';
import { arbitrum, mainnet, polygon } from '@wagmi/core/chains';
const chains = [arbitrum, mainnet, polygon]
const projectId = 'my project id'
const { publicClient } = configureChains(chains, [w3mProvider({ projectId })])
const wagmiConfig = createConfig({
autoConnect: true,
connectors: w3mConnectors({ projectId, chains }),
publicClient
})
const ethereumClient = new EthereumClient(wagmiConfig, chains)
this.web3Modal = new Web3Modal({ projectId, themeVariables: {'--w3m-z-index': '1100'} }, ethereumClient)
在我的自定义按钮上我使用:
this.web3Modal.openModal();
但我们没有:
await this.web3Modal.connect();
我如何知道用户何时连接?
尝试使用
const unwatch = watchAccount((account) => console.log(account));
watchAccount 在 WagmiCore 下
这可以帮助React App中的Web3Modal individual_sign(与问题主题相关)。
文档: https://docs.walletconnect.com/web3modal/react/about
套餐: @web3modal/[电子邮件受保护] [电子邮件受保护]
const ethersProvider = new BrowserProvider(walletProvider)
const signer = await ethersProvider.getSigner()
const signature = await signer.signMessage('message content')