[Ethereum] Truffle web3.eth.sign produces wrong signature

contract-debuggingecrecoversignaturetruffletruffle-test

Somehow truffle produces an invalid signature.
To clarify this I imported the private key of the first Ganache account to metamask and compared the results.

The following code used in a chrome console returns the following (verifiable correct) signature:

// frontend
let msg = web3.sha3("truffle"); // => 0x28610e27d085e348e6ebd0da2c982947eddf7d308b82e951a896432cdac99a74
let sig;
web3.eth.sign(web3.eth.accounts[0], msg, (err, res) => {
  if(!err) {
    sig = res;
  }
})

// sig => 0xb453386b73ba5608314e9b4c7890a4bd12cc24c2c7bdf5f87778960ff85c56a8520dabdbea357fc561120dd2625bd8a904f35bdb4b153cf706b6ff25bb0d898d1c

Here's the code in truffle which should produce the same signature – but doesn't.

// Truffle:
let first = accounts[0];
let msg = web3.sha3("truffle");
let sig = await web3.eth.sign(first, msg)
// sig => 0x511fafdf71306ff89a063a76b52656c18e9a7d80d19e564c90f0126f732696bb673cde46003aad0ccb6dab2ca91ae38b82170824b0725883875194b273f709b901

This sucks as I am trying to test my contracts which rely on these signatures.

Best Answer

Apparently it's an issue with Truffle, see this issue here. More info here and here.