未定义的Web3.ETH.ETACCOUNT()在React本机,MetAmask身份验证中
我想通过用户的元掩as钱包来验证用户。我正在使用 web3软件包以与块和签名交易交互。当我尝试获取用户帐户时,我会得到一个空的结果:
const Web3 = require('web3');
const web3 = new Web3(
new Web3.providers.HttpProvider('https://api.avax.network/ext/bc/C/rpc')
);
const addresses = await web3.eth.getAccounts();
我想我需要请求这样的帐户等待窗口。不存在
窗口
移动应用。
我猜想在正常流中,用户按下了auth按钮,并将被重定向到metamask钱包以授权应用程序,我该怎么做?
I want to authenticate the user by his Metamask wallet. I am using web3 package in order to interact with the blocks and sign transactions. When I try to get the user accounts I get an empty result:
const Web3 = require('web3');
const web3 = new Web3(
new Web3.providers.HttpProvider('https://api.avax.network/ext/bc/C/rpc')
);
const addresses = await web3.eth.getAccounts();
I suppose that I need to request the accounts like this await window.ethereum.request({ method: 'eth_requestAccounts'});
but it doesn't exist window
mobile app.
I guess in a normal flow the user hit the auth button and will be redirected to Metamask wallet to authorize the app, how can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是正确的,但您还需要连接到MetAmask,而不是您自己的RPC。这是您的固定代码:
You are correct, but you also need to connect to MetaMask, not your own RPC. Here is your fixed code:
我发现了一个解决方案,我不知道是否是最好的方法。
为了与块交互并签署交易,用户通过 walletconnect 服务。
用户经过身份验证后,我可以使用连接器来创建一个提供商,以便将其与 ethers.js.js lib一起使用(使用web3.js,与RPC的连接失败,我决定使用Ethers):
这有点棘手,但WalletConnect效果很好。
I found out a solution, I do not know if is the best way to do it.
In order to interact with the blocks and sign transactions the users authenticate their MetaMask wallet through WalletConnect service.
After the user is authenticated I can use the connector to create a provider in order to use it with ethers.js lib (Using web3.js the connection with RPC failed, I decided to use ethers instead):
It is a little tricky but WalletConnect works fine.