Ethers.js: EIP712 signTypedData in ethers.js

Created on 11 May 2020  路  3Comments  路  Source: ethers-io/ethers.js

I tried to find the EIP712 functionality in the docs as well as in the code, looks like it's not yet implemented. If I missed it and it is implemented, pls give me a pointer. If it's not, is EIP712 there in the ethers.js roadmap?

Inspired from ethereum library syntax, EIP712 might be implemented in ethers.js in this way:

const signature = await wallet.signTypedData(typedData);

I think we should also have a type encoder:

interface Type {
    name: string,
    type: string
}

class TypedDataEncoder {
    // types are compound type structs
    // domain separator, must be of type EIP712Domain declared in types
    constructor(
        types: {[key: string]: Type[]}, 
        domain: any
    );

    // messageObj is object to be encoded
    // primaryType is the type from types that represents messageObj
    encode(messageObj: any, primaryType: string);
}

const td = new ethers.utils.TypedDataEncoder({
    EIP712Domain: [
        { name: 'name', type: 'string' },
        { name: 'version', type: 'string' },
        { name: 'chainId', type: 'uint256' },
        { name: 'verifyingContract', type: 'address' },
    ],
    Person: [
        { name: 'name', type: 'string' },
        { name: 'wallet', type: 'address' }
    ],
    Mail: [
        { name: 'from', type: 'Person' },
        { name: 'to', type: 'Person' },
        { name: 'contents', type: 'string' }
    ],
}, {
    name: 'Mail DApp',
    version: '1.0.1',
    chainId: 1,
    verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
});

const typedData = td.encode({
    from: {
        name: 'James Bond',
        wallet: '0xC8e1F3B9a0CdFceF9fFd2343B943989A22517b26'
    },
    to: {
        name: 'John Snow',
        wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB'
    },
    contents: 'Hey hi'
}, 'Mail');

const signature = await wallet.signTypedData(typedData);
discussion duplicate

Most helpful comment

All 3 comments

Duplicate of #687

The encoding for EIP-712 is a bit complex and requires significant testing. I just haven鈥檛 had a chance to get to it yet... I will. :)

In the meantime, there is a library out there. I think by MetaMask?

That's cool!

Was this page helpful?
0 / 5 - 0 ratings