我想知道如何在我的node.js web服务器中正确地使用web3.js。我在主页上有一个按钮,允许用户连接到他们的钱包,它是在我的前端完成的。在连接到他们的钱包后,他们将被定向到仪表板上,我想重新检查他们是否连接到仪表板页面中的钱包。出于安全考虑(由于人们可以在开发人员工具中修改js,所以我不太确定在前端这样做是否安全),我决定在我的后端web服务器上检查它。我正在运行localhost:3000,如果这个信息有用的话。
这就是我尝试过的:
const express = require('express');
const Web3 = require('web3');
const app = express();
const port = process.env.PORT || 3000;
app.set('view engine', 'ejs');
app.listen(port, () => {
console.log("Connected!");
});
app.get('/', (request, response) => {
// User connects to wallet in this page
response.render('index');
});
app.get('/dashboard', async (request, response) => {
// Check if wallet is connected
if (typeof web3 !== "undefined") {
// This should run because I have Metamask installed but didn't
web3 = new Web3(web3.currentProvider);
const accounts = await web3.eth.getAccounts()
} else {
// This runs
web3 = new Web3(new Web3.providers.HttpProvider(infura provider));
const accounts = await web3.eth.getAccounts() // Returns []
}
response.render('dashboard');
});但问题是,即使我在浏览器中安装了Metamask,web3也是未定义的。通过使用呋喃服务提供商,await web3.eth.getAccounts()返回一个空数组,尽管我已经将我的钱包连接到主页中的网站。
也许我做错了一切。我不熟悉Web3是如何工作的,即使我做了很多研究。我找不到任何使用node.js网络服务器处理帐户的人的视频。
我还有一件事要担心,那就是可以公开观看因弗拉供应商吗?我不记得在哪里,但我看到人们在前端使用它。
发布于 2022-06-01 10:52:52
我觉得你把事情搞混了。您必须了解在前端(浏览器)和后端(服务器)运行的代码。window.ethereum是由元the注入的,但它只在前端可用。上面使用Express的代码运行在后端,因此window.ethereum将是未定义的。
要获得您想要的结果,请在前面进行元询问检查。在您的仪表板页面中,检查用户是否连接了他的帐户,如果没有,请将他重定向到您的主页。这些线周围的东西:
// script in your dashboard page
if (window.ethereum) {
// checks accounts connected
const accounts = await window.ethereum.request({
method: 'eth_requestAccounts',
})
console.log('accounts :>> ', accounts)
if(accounts.length == 0){
// redirect if no accounts found
window.location.replace('http://my-home-age-url')
}
}else{
console.log('Metamask not detected')
window.location.replace('http://my-home-age-url')
}发布于 2022-12-30 10:07:15
window.ethereum是通过MetaMask浏览器钱包扩展在页面中注入的提供者,在Nodejs环境中不可用。我创建了一个包来实现类似的功能:https://www.npmjs.com/package/web3-nodejs-provider
您可以在服务器端创建一个提供程序,并将其与Web3实例Github:https://github.com/naftalimurgor/web3-nodejs-provider一起使用。
const Web3NodejsProvider = require("web3-nodejs-provider");
const Web3 = require("web3");
const mnemonicPhrase = "mountains supernatural bird..."; // 12 word mnemonic
let provider = new Web3NodejsProvider({
mnemonic: {
phrase: mnemonicPhrase
},
providerOrUrl: "http://localhost:8545"
});
// Or, alternatively pass in a zero-based address index.
provider = new Web3NodejsProvider({
mnemonic: mnemonicPhrase,
providerOrUrl: "http://localhost:8545",
addressIndex: 5
});
// Or, use your own hierarchical derivation path
provider = new Web3NodejsProvider({
mnemonic: mnemonicPhrase,
providerOrUrl: "http://localhost:8545",
numberOfAddresses: 1,
shareNonce: true,
derivationPath: "m/44'/137'/0'/0/"
});
// To make HDWallet less "chatty" over JSON-RPC,
// configure a higher value for the polling interval.
provider = new Web3NodejsProvider({
mnemonic: {
phrase: mnemonicPhrase
},
providerOrUrl: "http://localhost:8545",
pollingInterval: 8000
});
// HDWalletProvider is compatible with Web3. Use it at Web3 constructor, just like any other Web3 Provider
const web3 = new Web3(provider);
// Or, if web3 is alreay initialized, you can call the 'setProvider' on web3, web3.eth, web3.shh and/or web3.bzz
web3.setProvider(provider)
// ...
// Write your code here.
// ...
// At termination, `provider.engine.stop()' should be called to finish the process elegantly.
provider.engine.stop();https://ethereum.stackexchange.com/questions/124940
复制相似问题