0

I have a struct.

struct Order {
        uint256 price;
        uint256 quantity;
        OrderType orderType;
    }

    enum OrderType {
        BUY,
        SELL
    }

Question is, how should I create typehash for EIP712 in solidity. I'm doing this,

    bytes32 public constant _ORDER_TYPEHASH =
        keccak256(
            "Order(uint256 price,uint256 quantity,OrderType orderType)"
        );

And to create signature for test using ethers.js I'm creating type like this

        Order: [
          {
            name: "price",
            type: "uint256",
          },
          {
            name: "quantity",
            type: "uint256",
          },
          {
            name: "orderType",
            type: "uint8",
          },
        ],

I'm using uint8 for OrderType as ethers.js don't know what enum is. The signature generated from signing this is invalid.

When I remove OrderType from Order struct and from _ORDER_TYPEHASH then signature generated is valid.

Now I do not know I'm doing wrong encoding in solidity or in javascript.

1 Answer 1

1

The issue could be with the encoding of the data that is being signed.

In your Solidity code, you have defined the OrderType enum as BUY and SELL. However, in your JavaScript code, you are using uint8 to represent the OrderType. This can cause a mismatch in the encoding of the orderType field, leading to an invalid signature.

To ensure that the encoding is consistent between the Solidity and JavaScript code, you can use the toNumber() method in ethers.js to convert the OrderType enum to a uint8. Here's how you can update your JavaScript code:

Order: [
  {
    name: "price",
    type: "uint256",
  },
  {
    name: "quantity",
    type: "uint256",
  },
  {
    name: "orderType",
    type: "uint8",
    value: (type) => type.orderType === "BUY" ? 0 : 1,
  },
],

In this updated code, the value field is a function that converts the OrderType enum to a uint8 value based on its string representation.

With this change, the encoding of the orderType field should match between the Solidity and JavaScript code, resulting in a valid signature.

1
  • Thanks for the answer bro, but it did not work. I just hard coded 0 to value field. wrong signature again. Commented May 12, 2023 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.