2

I've been trying to get MetaMask to produce a signature for a sha3 hash that I can verify in solidity. I've tried eth_sign, personal_sign, eth_signTypedData, and I've had 0 success.

My test suite runs fine (Truffle), but I can not for the life of me get MetaMask to play nice, and I can't find a working example anywhere.

What do I do?

1

Here's a working example: https://github.com/ConnextProject/ethcalate-2waypayment/blob/master/contracts/ChannelManager.sol

Relevant code:

bytes32 fingerprint = keccak256(channelId, nonce, balanceA, balanceB);
bytes32 signTypedDataFingerprint = keccak256(
    keccak256("bytes32 hash"),
    keccak256(fingerprint)
);
require(ECTools.isSignedBy(signTypedDataFingerprint, sigA, channel.agentA) == true);

This is what we do on the client/dapp side (https://github.com/ConnextProject/ethcalate-client/blob/master/src/Ethcalate.js):

async signTx ({ channelId, nonce, balanceA, balanceB }) {
  // fingerprint = keccak256(channelId, nonce, balanceA, balanceB)
  let hash = abi
    .soliditySHA3(
      ['bytes32', 'uint256', 'uint256', 'uint256'],
      [channelId, nonce, balanceA, balanceB]
    )
    .toString('hex')
  hash = `0x${hash}`
  console.log('hash: ', hash)

  const sig = await new Promise((resolve, reject) => {
    this.web3.currentProvider.sendAsync(
      {
        method: 'eth_signTypedData',
        params: [
          [
            {
              type: 'bytes32',
              name: 'hash',
              value: hash
            }
          ],
          this.web3.eth.accounts[0]
        ],
        from: this.web3.eth.accounts[0]
      },
      function (err, result) {
        if (err) reject(err)
        if (result.error) {
          reject(result.error.message)
        }
        resolve(result)
      }
    )
  })
  console.log('sig: ', sig)
  return sig
}
  • Does this result in a Metamask prompt per channel update? – rnbrady Jul 4 '18 at 14:52
  • Yes it does for Metamask. However, there will surely be other wallet implementations which have different ways to handle this. Spankchain's browser wallet autosigns channel update transactions. – rhlsthrm Jul 4 '18 at 17:45

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.