Nethereum: EIP712 Signer doesn't work correctly

I get an example of EIP712 Sign from official EIPs example: https://github.com/ethereum/EIPs/blob/b95c19f7118740cb498e0f758803037189b4ca30/assets/eip-712/Example.js I’ve tested it with my private key: https://github.com/vt-btf/EIP712-nodeJs

Then i wrote some code for Metamask with the same data and private key: https://github.com/vt-btf/EIP712-Metamask

I had the same output of Signatures. Then i got your test example(wich has the same data like from EIPs example): https://github.com/Nethereum/Nethereum/blob/master/src/Nethereum.Signer.UnitTests/Eip712TypedDataSignerTest.cs

And created a C# project with this data: https://github.com/vt-btf/EIP712-CSharp

MetaMask and NodeJs examples has same signature. Nethereum Signer has different signature

Expected Behavior

All signatures are the same

Current Behavior

MetaMask and NodeJs examples has same signature. Nethereum Signer has different signature

Steps to Reproduce

  1. https://github.com/vt-btf/EIP712-nodeJs
  2. https://github.com/vt-btf/EIP712-Metamask
  3. https://github.com/vt-btf/EIP712-CSharp

Context (Environment)

Can’t work with GSN v2 example

Possible Implementation

Found out the right signature will be:

var key = new EthECKey(privKey);
var hashedData = Sha3Keccack.Current.CalculateHash(Eip712TypedDataSigner.Current.EncodeTypedData(typedData));
var correctSignature = key.SignAndCalculateV(hashedData);

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 36 (20 by maintainers)

Commits related to this issue

Most upvoted comments

Yeah sure. I’ll add a method SignTypedDataV4 that will be implementation up above from OP

@frostiq Initially I tried that & it didn’t work, however, I found out later that I was passing in a string into a field that should have been BigInteger for the uint256 encoding. After fixing that, the signature was valid. Hopefully, this helps someone else.

My use-case is slightly different, I’m not trying to sign using Nethereum, instead I’m trying to recover the signing address from an existing signature. I’ve basically got a REST API and I want to create an auth handler that takes a payload from a client, with a signature of that payload. To verify it, I reconstruct the TypedData at the auth handler, then verify that the recovered address matches the address in the payload. Rough code looks something like:

var typedData = new TypedData... //truncated for readability

var encoded = _eip712Signer.EncodeTypedData(typedData);
var resultHash = Sha3Keccack.Current.CalculateHash(encoded);
var addressRecovered = _signer.EcRecover(resultHash, signature);

I’ve got this working totally fine using libraries with node, but I’d really love to have this working with Nethereum. I’ve definitely confirmed that the domain, types and values match - plus I’ve been able to achieve it with 2 different methods in typescript. Any ideas?

I’ve implemented SignTypedData in such a way because that was needed for our use case (Gnosis Safe transactions). If you need to make a different signature for another use case (e.g. Metamask compatibility) – feel free to submit a PR with an alternative SignTypedData method if you are willing to contribute to the library. Looks like such a PR will be relatively easy to do