eth-sig-util icon indicating copy to clipboard operation
eth-sig-util copied to clipboard

Ether js signedTypeData & metamask sig utils signedTypeData_v4 producing different signature

Open liho00 opened this issue 3 years ago • 2 comments

import {
  signTypedData,
} from "@metamask/eth-sig-util";
import { ethers } from "ethers";

const domain = {
  name: "name", // contract deploy name
  version: "1", // contract deploy version
  chainId: 1, // env chain id
  verifyingContract: "",
};

const types = {
  Test: [
    { name: "Request", type: "string" },
  ],
};
const privateKey = Buffer.from(
  "myprivatekey",
  "hex"
);

Metamask sig utils SignTypedDataVersion.V4, (tested with SignTypedDataVersion.V3 same)

const signature = signTypedData({
  privateKey,
  data: {
    types,
    primaryType: "Test",
    domain,
    message: {
      Request: "This is a request"
    },
  },
  version: SignTypedDataVersion.V4,
});
console.log("Metamask sig utils generated signature", signature); 

Metamask sig utils generated signature 0x93f39c57d7f8de7b033367cb5fa7bb68c6a339b0626c73febc57fdce5a779a8472b365951470ade9857e8bd26a3020e9088c110c931abd188034cbdd1e79f8191c

ether js signTypedData

const wallet = new ethers.Wallet(privateKey);
wallet
  ._signTypedData(domain, types, {
      Request: "This is a request" 
  })
  .then((signature) => {
    console.log("Ether js _signTypedData", signature);
  });

Ether js _signTypedData 0x9655d8635d4826a1c44185acb34f7c86b286e90e70bd260626c8dd112ae8f5f348f75ce387544c1734971f6ae51ad855e98a49c7e56c3a636a90f714378aca0d1c

Is sig utils deprecated, but why both produce different result of signatures? I was confusing on why these both producing different signature, does it possible both lib produce same signature?

or there is some bug over here? because when i using solidity ECDSA.recover i able to recover address from ether js's signature but not sig utils' generated signature

liho00 avatar Mar 22 '22 17:03 liho00

Sig util is not deprecated, it represents signing behavior in MetaMask in production today. Will require investigation to identify the deviation.

danfinlay avatar Jun 26 '22 17:06 danfinlay

#106 looks like a likely cause

wbt avatar Aug 05 '22 20:08 wbt