4

I've got a contract method that checks the signer of a message using the solidity ecrecover function. In my truffle test file I now want to check if this is done correctly. Therefore I need to sign a message within the testfile using a private key.

Using this

let signedMessage = await web3.personal.sign("data", accounts[0])

does not work with error "Method personal_sign not supported."

Any idea how to do this? Thanks for you help!

5

I went through exactly the same problem a couple of days ago and it seems that the personal_sign method is rarely exposed for security reason.

I sorted this out using a random private key and a JavaScript library called ethereumjs-util

var EthUtil             = require('ethereumjs-util');

const messageToSign = "hello world";
const privateKey = "43f2ee33c522046e80b67e96ceb84a05b60b9434b0ee2e3ae4b1311b9f5dcc46";

var msgHash = EthUtil.hashPersonalMessage(new Buffer(messageToSign));
var signature = EthUtil.ecsign(msgHash, new Buffer(privateKey, 'hex')); 
var signatureRPC = EthUtil.toRpcSig(signature.v, signature.r, signature.s)

console.log(signatureRPC);

That returns:

0xd74a20b5a5b58185cc701e9d1349f240ff04154ab600266748bd45986104f0a62b8b827e2ba4a0da018efffb0ea16506503f4292538d791232ce574ac283e47c00


Working Code is available on my github

  • Thanks! Worked for me! However, I replaced the random private key with the one generated by testrpc so that I can validate that the signer is actually accounts[0]. That is ok, but I need to do this every time I start a new testrpc process. – moinlars Apr 4 '18 at 11:02
  • 1
    When you start testrpc, you can force it to use an account by default rather than autogenerate a new one all the time: testrpc --account="0x0F0F0F0F0F%PRIVATE KEY%,10000000000000000000000%INITIAL BALANCE IN WEI%" --secure -u 0 - more info – Greg Jeanmart Apr 4 '18 at 11:06
  • @Greg Jeanmart Hi, I have a similar problem with web3.eth.personal.sign and web3.eth.sign here: ethereum.stackexchange.com/questions/52584/… But till now I could not solve this problem. Thank you – Questioner Jul 4 '18 at 14:22

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.