1

I'm signing EIP712 message in my front-end app. But when I use web3js.ecrecover to verify address signer it return a wrong address (random).

Data to sign (EIP712) :

 msgParams = {
      domain: {
        name: "Project",
        chainId: chainId,
        verifyingContract: "0x0000000000000000000000000000000000000000"
      },
      message: {
        Maker:addressMetamask2,
    Taker :"0x0000000000000000000000000000000000000000",
    MakerWei:100000000000000,
    MakerIds:[111],
    TakerWei:0,
    TakerIds:[],
    Expiry:1625174589,
    Salt:1234567,
      },
      primaryType: 'Mail',
      types: {
        EIP712Domain: [
        { name: "name", type: "string" },
        { name: "chainId", type: "uint256" },
        { name: "verifyingContract", type: "address" }
        ],
        Mail: [
        { name: "Maker", type: "address" },
        { name: "Taker", type: "address" },
        { name: "MakerWei", type: "uint256" },
        { name: "MakerIds", type: "uint256[]" },
        { name: "TakerWei", type: "uint256" },
        { name: "TakerIds", type: "uint256[]" },
        { name: "Expiry", type: "uint256" },
        { name: "Salt", type: "uint32" },
        ],
      },
    }

JSON.stringify data :

{"domain":{"name":"Project","chainId":3,"verifyingContract":"0x0000000000000000000000000000000000000000"},"message":{"Maker":"0xdb596f82563f69202a4f1410442c20e5d5ba2910","Taker":"0x0000000000000000000000000000000000000000","MakerWei":100000000000000,"MakerIds":[111],"TakerWei":0,"TakerIds":[],"Expiry":1625174589,"Salt":1234567},"primaryType":"Mail","types":{"EIP712Domain":[{"name":"name","type":"string"},{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"}],"Mail":[{"name":"Maker","type":"address"},{"name":"Taker","type":"address"},{"name":"MakerWei","type":"uint256"},{"name":"MakerIds","type":"uint256[]"},{"name":"TakerWei","type":"uint256"},{"name":"TakerIds","type":"uint256[]"},{"name":"Expiry","type":"uint256"},{"name":"Salt","type":"uint32"}]}}

So I sign with my address 0xdb596f82563f69202a4f1410442c20e5d5ba2910 like this using MetaMask :

signTypedDataV4.onclick = async () => {

    try {

      const account = await getCurrentAccount();
      const sign = await ethereum.request({
        method: 'eth_signTypedData_v4',
        params: [addressMetamask2, JSON.stringify(msgParams)],
      })
      console.log(sign);

    } catch (err) {
      //todo
    }
  }

Sig :

0x3ea907ba73bd978d6e5d40bfbfa98c78a1f13d983be97264f89255c24093c8eb7b3e0ba786e9e4648703e7c80495ca65e4f7f2e89383382ed1f3685fd722ea2f1c

Then verify :

web3.eth.personal.ecRecover(msgParams, sign).then(console.log);
     

But return "0x5d128d9e19021130e2c69988e54d6a21e94fc9ca" ?

A correct signature with MyEtherWallet :

{
  "address": "0xdb596f82563f69202a4f1410442c20e5d5ba2910",
  "msg": "{\"domain\":{\"name\":\"Project\",\"chainId\":3,\"verifyingContract\":\"0x0000000000000000000000000000000000000000\"},\"message\":{\"Maker\":\"0xdb596f82563f69202a4f1410442c20e5d5ba2910\",\"Taker\":\"0x0000000000000000000000000000000000000000\",\"MakerWei\":100000000000000,\"MakerIds\":[111],\"TakerWei\":0,\"TakerIds\":[],\"Expiry\":1625174589,\"Salt\":1234567},\"primaryType\":\"Mail\",\"types\":{\"EIP712Domain\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"chainId\",\"type\":\"uint256\"},{\"name\":\"verifyingContract\",\"type\":\"address\"}],\"Mail\":[{\"name\":\"Maker\",\"type\":\"address\"},{\"name\":\"Taker\",\"type\":\"address\"},{\"name\":\"MakerWei\",\"type\":\"uint256\"},{\"name\":\"MakerIds\",\"type\":\"uint256[]\"},{\"name\":\"TakerWei\",\"type\":\"uint256\"},{\"name\":\"TakerIds\",\"type\":\"uint256[]\"},{\"name\":\"Expiry\",\"type\":\"uint256\"},{\"name\":\"Salt\",\"type\":\"uint32\"}]}}",
  "sig": "0x7dc838cffe46145660e6684f7be7a4e84b8b14563dd14814303eab15dbf6bfda170b213ec12842cf9213f11ed304214237685d830d2a8a9f07eb9cf4d682159e1b",
  "version": "3",
  "signer": "MEW"
}

Nota : MyEtherWallet convert "msg" as hex before signature.

What is wrong with web3JS ?

1
  • Using ecRecover is not enough, you can use eth-sig-util's recoverTypedSignature_V4.
    – Ismael
    Jun 2, 2021 at 6:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.