Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Espresso eip712 #79

Merged
merged 3 commits into from
Jun 27, 2024
Merged

Espresso eip712 #79

merged 3 commits into from
Jun 27, 2024

Conversation

ZzzzHui
Copy link
Collaborator

@ZzzzHui ZzzzHui commented Jun 21, 2024

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:

  • only accept nonce that is incremental by 1 from the account's previous nonce in the database
  • if users query for a nonce, generate a report?
  • review and optimize frontend backend encoding schemes
  • figure out what's the best UX for users to submit to espresso

@ZzzzHui ZzzzHui changed the title extract eip712 signer and data Espresso eip712 Jun 21, 2024
@ZzzzHui
Copy link
Collaborator Author

ZzzzHui commented Jun 21, 2024

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.

@sandhilt sandhilt added the enhancement New feature or request label Jun 21, 2024
nonce, ok1 := typedData.Message["nonce"].(uint32)
payload, ok2 := typedData.Message["payload"].(string)
if !ok1 || !ok2 {
slog.Debug("type assertion error ")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we return an error when the cast fails?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Makes sense

@fabiooshiro
Copy link

Hi @ZzzzHui, I think that if you update our branch with the main branch, the test will pass.
See: #88

@ZzzzHui ZzzzHui merged commit dedb6f9 into main Jun 27, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants