PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 28, 2022

[FIXED] How to sign messages on the server side using web3

 July 28, 2022     blockchain, cryptography, ethereum, node.js, web3js     No comments   

Issue

I am building a decentralized app using Node.js. I need the node.js app to receive a message from the frontend, use web3.js to sign the message and send the signature back to the frontend.

I am thinking of passing as a environmental variable a predefined private key to the node.js app. Then use the private key to instantiate Web3 and call web3.personal.sign to sign the message. I need the signing process to occur on the server side, so I don't believe that using a client-side wallet like Metamask would be applicable.

I am new to Blockchain and Web3 development so I am not sure if what I am asking is feasible.


Solution

You can pass the private key to wallet.add(), and then sign the message using web3.eth.sign().

web3.eth.accounts.wallet.add(SIGNER_PRIVATE_KEY);
const message = "Hello world";

// sign
const signature = await web3.eth.sign(message, SIGNER_ADDRESS);

// recover
const recoveredSigner = web3.eth.accounts.recover(message, signature);
console.log(recoveredSigner == SIGNER_ADDRESS);


Answered By - Petr Hejda
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing