Calindra / nonodo

Development Node for Cartesi Rolups
Apache License 2.0
14 stars 14 forks source link

Espresso eip712 #79

Closed ZzzzHui closed 1 month ago

ZzzzHui commented 2 months ago

currently, this branch is able to extract the eip712 signer address, and the signed data (which includes nonce and payload)

This branch is a work in progress and the to-dos include:

ZzzzHui commented 2 months ago

Here is a rough but working example of how to sign typed data using viem. The resulting signature and the typed data should together be sent to Espresso.

import { createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { mainnet } from "viem/chains";

const walletClient = createWalletClient({
    account: privateKeyToAccount(
        "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
    ),// private key to address 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
    chain: mainnet,
    transport: http(),
});

async function sign() {
    const typedData = {
        domain: {
            name: "EspressoM",
            version: "1",
            chainId: 1, // for example, ethereum mainnet
            verifyingContract:
                "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC", // should be namespace
            // probably don't need `salt`
        },
        types: {
            EIP712Domain: [
                { name: "name", type: "string" },
                { name: "version", type: "string" },
                { name: "chainId", type: "uint32" },
                { name: "verifyingContract", type: "address" },
            ],
            EspressoMessage: [
                { name: "nonce", type: "uint32" },
                { name: "payload", type: "string" },
            ],
        },
        primaryType: "EspressoMessage",
        message: {
            nonce: 0,
            payload: "beefbeef",
        },
    } as const;

    const signature = await walletClient.signTypedData(typedData);

    console.log(
        JSON.stringify({
            signature,
            typedData: btoa(JSON.stringify(typedData)),
        })
    );
}

sign();

In the future, maybe change uint32 to uint256 if necessary, although then we need to deal with bigint

Note: after spending some time debugging, I found out it's important to have EIP712Domain in the types too, otherwise it causes a weird bug. But on viem 's docs, EIP712Domain is not in the types. Should investigate too.

fabiooshiro commented 1 month ago

Hi @ZzzzHui, I think that if you update our branch with the main branch, the test will pass. See: https://github.com/Calindra/nonodo/pull/88