MetaMask/eth-sig-util

signTypedData invalid version error

Closed this issue ยท 5 comments

When attempting to use signTypedData with data from a test case:

const ethSigUtil = require("@metamask/eth-sig-util");
const { SignTypedDataVersion } = require("@metamask/eth-sig-util");

const privateKey = '4af1bceebf7f3634ec3cff8a2c38e51178d5d4ce585c52d6043e5e2cc3418bb0';
const msgParamsOg = {"types":{"EIP712Domain":[{"type":"string","name":"name"},{"type":"string","name":"version"},{"type":"uint256","name":"chainId"},{"type":"address","name":"verifyingContract"}],"Part":[{"name":"account","type":"address"},{"name":"value","type":"uint96"}],"Mint721":[{"name":"tokenId","type":"uint256"},{"name":"tokenURI","type":"string"},{"name":"creators","type":"Part[]"},{"name":"royalties","type":"Part[]"}]},"domain":{"name":"Mint721","version":"1","chainId":4,"verifyingContract":"0x2547760120aed692eb19d22a5d9ccfe0f7872fce"},"primaryType":"Mint721","message":{"@type":"ERC721","contract":"0x2547760120aed692eb19d22a5d9ccfe0f7872fce","tokenId":"1","uri":"ipfs://ipfs/hash","creators":[{"account":"0xc5eac3488524d577a1495492599e8013b1f91efa","value":10000}],"royalties":[],"tokenURI":"ipfs://ipfs/hash"}};
  
let msgParams = JSON.stringify(msgParamsOg);
let version = SignTypedDataVersion.V4;

const signature = ethSigUtil.signTypedData(privateKey, msgParams, version);
console.log("Signature: " + signature);

I'm getting the following error:

Error: Invalid version: 'undefined'
    at validateVersion (/Users/daniel/Code/nodetest/node_modules/@metamask/eth-sig-util/dist/sign-typed-data.js:68:15)
    at Object.signTypedData (/Users/daniel/Code/nodetest/node_modules/@metamask/eth-sig-util/dist/sign-typed-data.js:332:5)
    at Object.<anonymous> (/Users/daniel/Code/nodetest/app.js:10:30)
    at Module._compile (internal/modules/cjs/loader.js:1200:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

I'm trying to debug a potential faulty message for a USDC transferWithAuthorization signature in a dart version of eth-sig-util. (That issue can be found here)
And I thought using the js version would be easier :), but not finding much in terms of documentation for this use case.

@danielnordh Looks like the way you're calling signTypedData is incorrect. It takes an options object. See: https://metamask.github.io/eth-sig-util/modules.html#signTypedData

Changing the call to this solved the problem, thank you:
const signature = ethSigUtil.signTypedData({privateKey: privateKey, data: msgParams, version: version});

is it really working for you @danielnordh? Whenever I am trying to do it work on my message to it, I am getting error

     TypeError: Cannot read property 'EIP712Domain' of undefined
      at findTypeDependencies (node_modules/@metamask/eth-sig-util/dist/sign-typed-data.js:176:42)
      at encodeType (node_modules/@metamask/eth-sig-util/dist/sign-typed-data.js:152:26)
      at hashType (node_modules/@metamask/eth-sig-util/dist/sign-typed-data.js:206:37)
      at encodeData (node_modules/@metamask/eth-sig-util/dist/sign-typed-data.js:132:28)
      at hashStruct (node_modules/@metamask/eth-sig-util/dist/sign-typed-data.js:196:37)
      at Object.eip712Hash (node_modules/@metamask/eth-sig-util/dist/sign-typed-data.js:242:16)
      at signTypedData (node_modules/@metamask/eth-sig-util/dist/sign-typed-data.js:341:34)

same when trying the example code you pasted here. Do you saw this error? Not sure why its saying Cannot read property 'EIP712Domain' of undefined

@kabaneridev See this issue for example of working js code (and dart code that does not generate the same signature).

const signature = ethSigUtil.signTypedData({privateKey: privateKey, data: msgParams, version: version});

Thanks for this.
The documentation is very unclear:
https://metamask.github.io/eth-sig-util/latest/functions/signTypedData.html
I wish they would just put an example of code. Would save everyone hours of pain.

EDIT:

I think you have the arguments mis-ordered, the manual has:

options: {
data: V extends "V1"
? TypedDataV1
: TypedMessage;
privateKey: Buffer;
version: V;
}

i.e.:

const signature = ethSigUtil.signTypedData({data: msgParams, privateKey: privateKey, version: version});

This works:

const cust_signature = ethSigUtil.signTypedData({data: message, privateKey: Buffer.from(user_privateKey, 'hex'), version: 'V4'});