DePayFi / web3-mock

🤡 JavaScript library to mock web3 responses either by emulating web3 wallets or web3 RPC requests.
https://depay.com
MIT License
87 stars 20 forks source link

Support for signTypedData #9

Closed dmlayton closed 2 years ago

dmlayton commented 2 years ago

Example Usage:

    const sigUtil = require('eth-sig-util');
    const messageHex = sigUtil.signTypedData_v4(secret, {
      data:message
    })
    mock({
      blockchain: 'ethereum',
      accounts :{ return :accounts},
      signature: {
        params: [accounts[0], JSON.stringify(message)],
        return: messageHex
      }
    })
10xSebastian commented 2 years ago

Looks good so far. I would like to add a test. Can you also provide an example for how that would look on the implementation side of things?

dmlayton commented 2 years ago

Yeah, I can add a test here since that appears to be the appropriate place -- I didn't find it the first time around.

Not sure what you mean by "Can you also provide an example for how that would look on the implementation side of things?" is that not what my first comment is? Or do you mean in the docs?

Edit: If you mean the code under test, it looks like this:

const method = 'eth_signTypedData_v4';
const message = JSON.stringify({
        domain: {
          chainId: chainId,
          name
          verifyingContract,
          version: '1',
        },
        message: {
...
        },
        primaryType: 'Person',
          types: {
            // TODO: Clarify if EIP712Domain refers to the domain the contract is hosted on
            EIP712Domain: [
              { name: 'name', type: 'string' },
              { name: 'version', type: 'string' },
              { name: 'chainId', type: 'uint256' },
              { name: 'verifyingContract', type: 'address' },
            ],
            // Not an EIP712Domain definition
            Group: [
              { name: 'name', type: 'string' },
              { name: 'members', type: 'Person[]' },
            ],
            // Refer to PrimaryType
            Mail: [
              { name: 'from', type: 'Person' },
              { name: 'to', type: 'Person[]' },
              { name: 'contents', type: 'string' },
            ],
            // Not an EIP712Domain definition
            Person: [
              { name: 'name', type: 'string' },
              { name: 'wallets', type: 'address[]' },
              { name: 'nonce', type: 'string' },
            ],
          },
      });
const params = [from, message];
const result = await ethereum.request({ method, params, from})