0

I want to sign a message with my ethereum account in a javascript application.

I do not want to work in a web-browser or with metamask.

I want to build a stand alone javascript program.

Is there a way to do that with ethereumjs-wallet for example ? I have searched for tutorials and i did not found anything.

Thanks

0

You can sign a message using your private key to confirm your identity:

data = "Hello bro";
privateKey = "YOUR-PRIVATE-KEY";

const Web3 = require("web3");
var web3 = new Web3(Web3.givenProvider);

async function main() {
  console.log(await web3.eth.accounts.sign(data, privateKey));
}
main();

Output:

pam@g3:~/web3sign$ node main.js
{ message: 'Hello bro',
  messageHash:
   '0x24901329ae47c41fa608d792f3a292f777daeed7cf7c41743590cb00ae898efd',
  v: '0x1c',
  r:
   '0x2537f4ba476ea221ddfb091c14e763d9396a82ff482039086389d324c4099de0',
  s:
   '0x5f1c896355123c7d74c39e4ac4908bf6e4427b219e15a0b809c87b5240393f77',
  signature:
   '0x2537f4ba476ea221ddfb091c14e763d9396a82ff482039086389d324c4099de05f1c896355123c7d74c39e4ac4908bf6e4427b219e15a0b809c87b5240393f771c' }

Referneced from web3 docs.

I hope it helps.

6
  • Thanks a lot. Does web3 need to connect to an Ethereum node ? This can works offline ? And how can i check signature ? – Bob5421 Apr 5 at 18:49
  • You have to initiate the web3 instance. Let me update my answer with the code. – Anupam Kumar Apr 5 at 19:11
  • Thanks, but can you tell me where the signature will be computed ? On a local endpoint ? Should i install something (rpc server) ? Thanks – Bob5421 Apr 5 at 19:33
  • You need an infura key to connect to. In the above code, it is mentioned as RPC-URL. Visit: infura.io or check this: link.medium.com/Sc64Avssdfb – Anupam Kumar Apr 5 at 19:41
  • Yes but how can i compute this signature on my local computer, without working with ethereum network ? – Bob5421 Apr 5 at 19:50

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.