1

I couldn't find a way to obtain accounts the user connected in my website in my node.js web server.

This is what I've tried

const express = require('express');
const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:3000"));

const app = express();
const port = process.env.PORT || 3000;

app.set('view engine', 'ejs');

app.listen(port, () => {
    console.log("Connected!");
});

app.get('/', (request, response) => {
    // This is the page where user connects to their wallet and it works fine
    response.render('index');
});

app.get('/dashboard', async (request, response) => {
    // The user gets redirected to this page after connecting to their wallet in the index page
    console.log(web3.eth.accounts); // Returns an empty array
    response.render('dashboard');
});

I even tried to use getAccounts() and many more I can find on the internet, but it stills return empty arrays. There are literally no questions and answers about working with user's wallet address on the server side. Perhaps I am doing it wrong, or it is not possible do that on the server side. I'm not very familiar with web3 development.

3
  • I am facing the same challenge. I want make users of my dapp signin via metamask on the front end and then allow the backend to mint an NFT and assign it to their wallet. I am running into similar challenges of how to fetch the accounts from metamask from the server. Is the dapp architecture incorrect? How do others do this when they gather wallet info from the front end and then execute a txn?
    – Aheesh
    May 13 at 10:13
  • Unfortunately it's not possible, I've did a lot of researching and trial and errors. There is only three way around this. One is to obtain the user's private key and call your contract method from the backend using the .call({from: key}), by this way you can execute txn without the user confirming it. Second is to call from your wallet's private key, but this way you are paying for everything, like a giveaway. Third is to call the mint function in the front end, this is what I did, I passed the contract address and ABI to the front end and created a contract instance in the front end.
    – Chock
    May 13 at 12:24
  • For additional information, calling a txn from the back end using the user's public key (wallet address) in the .call({from: key}) will not work at all, nothing will pop up on the client side and the transaction rejects automatically. Just passing the mint function to the front end will not work too, it's complicated to explain the reason but you could test it yourself to find out.
    – Chock
    May 13 at 12:30

1 Answer 1

1

You are correct - it's not possible to do it in the serve side. Browser wallets such as MetaMask are only available in the frontend. If you want wallet information to be available in your backend, the frontend has to forward the data to the backend.

Or if you wish to utilize blockchain in the server side, you typically have a separate connection. This is useful for example in cases where you want the backend to read blockchain data but not require users to have (or give access to their) wallet.

To utilize a separate connection from the backend (for read only access), you need access to some node - typically such as Alchemy or Infura. If you wish to also submit transactions, you'll need access to a (backend) wallet which has the necessary funds for it. This is basically just a private key stored in a secure place.

4
  • Do I have to let the user sign something and recover it in the backend to verify if they are connected to their wallet? Is that the only method? I am concerned about frontend safety because users can modify js from the dev tools
    – Chock
    Apr 1 at 8:44
  • I don't understand your use case, but in general the backend shouldn't care whether a user has connected a wallet to the frontend or not. You wouldn't know that even by requiring a signed message - the user can just sign that elsewhere and send to your backend. Apr 1 at 8:49
  • I am guessing there could be a scenario where the user logs in via metamask and the back end app needs to know the address to use it as ther "username". Like OpenSea, etc. I am sure there are lots of ways to do it but yes, the front end would need to send the address to the back end or the back end would need to ask.
    – ThirdPrize
    Apr 1 at 9:56
  • @ThirdPrize That is indeed true. I am creating a dashboard for my website, and seems like signing messages is the safest way to do so.
    – Chock
    Apr 1 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.