未定义的Web3.ETH.ETACCOUNT()在React本机,MetAmask身份验证中

发布于 2025-02-03 00:41:10 字数 496 浏览 4 评论 0原文

我想通过用户的元掩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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故笙诉离歌 2025-02-10 00:41:11

您是正确的,但您还需要连接到MetAmask,而不是您自己的RPC。这是您的固定代码:

import detectEthereumProvider from '@metamask/detect-provider';

const web3 = await detectEthereumProvider(); // Use Metamask-injected web3
await web3.request({ method: 'eth_requestAccounts'});
const addresses = await web3.eth.getAccounts();

You are correct, but you also need to connect to MetaMask, not your own RPC. Here is your fixed code:

import detectEthereumProvider from '@metamask/detect-provider';

const web3 = await detectEthereumProvider(); // Use Metamask-injected web3
await web3.request({ method: 'eth_requestAccounts'});
const addresses = await web3.eth.getAccounts();
情深已缘浅 2025-02-10 00:41:11

我发现了一个解决方案,我不知道是否是最好的方法。

为了与块交互并签署交易,用户通过 walletconnect 服务。

用户经过身份验证后,我可以使用连接器来创建一个提供商,以便将其与 ethers.js.js lib一起使用(使用web3.js,与RPC的连接失败,我决定使用Ethers):

const walletConnectProvider = new WalletConnectProvider({
    rpc: {
        43114: RPC 
    },
    chainId: 43114,
    connector: this.connector,
    qrcode: false
})
let wp = await walletConnectProvider.enable();
if(wp){
    this.provider = new providers.Web3Provider(walletConnectProvider);
}


const signer = this.provider.getSigner();
const contract = new Contract(GAMESESSION_ADDRESS, GameSession.abi, signer);
....

这有点棘手,但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):

const walletConnectProvider = new WalletConnectProvider({
    rpc: {
        43114: RPC 
    },
    chainId: 43114,
    connector: this.connector,
    qrcode: false
})
let wp = await walletConnectProvider.enable();
if(wp){
    this.provider = new providers.Web3Provider(walletConnectProvider);
}


const signer = this.provider.getSigner();
const contract = new Contract(GAMESESSION_ADDRESS, GameSession.abi, signer);
....

It is a little tricky but WalletConnect works fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文