Skip to content

Eth.signTypedDataV4

Asks the node or wallet to sign EIP-712 typed data with the key of an account. Returns the signature, 65 bytes of hex.

import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Transport } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const typedData = {
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "chainId", type: "uint256" },
],
Mail: [
{ name: "to", type: "address" },
{ name: "contents", type: "string" },
],
},
primaryType: "Mail",
domain: { name: "ethect", chainId: 31337 },
message: { to: "0x70997970C51812dc3A010C7d01b50e0d17dc79C8", contents: "hello" },
};
const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const program = Eth.signTypedDataV4(ADDRESS, JSON.stringify(typedData));
const signature = await program.pipe(
Effect.provide(Transport.http("http://127.0.0.1:8545")),
Effect.provide(FetchHttpClient.layer),
Effect.runPromise,
);
// "0x27de637b4508f088db9dc60bf22f422871320316e8dc0225bc8b1a06ef31243f
// 7fdcb85f72ddb8ed194a327cb4d9178981e3a5add4a025317da1512c9f6605961c"

The typed data is passed as its JSON text: JSON.stringify the object. The signature above is one string, split here to fit.

ethect does not sign, and it does not hash typed data either: the node or wallet does both.

The examples below leave the transport out.

JSON.stringify throws on a bigint. Write a uint256 as a decimal string.

import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const signature = Effect.gen(function* () {
const amount = 10n ** 18n;
const typedData = {
types: {
EIP712Domain: [{ name: "name", type: "string" }],
Order: [{ name: "amount", type: "uint256" }],
},
primaryType: "Order",
domain: { name: "ethect" },
message: { amount: amount.toString() },
};
return yield* Eth.signTypedDataV4(ADDRESS, JSON.stringify(typedData));
});

Effect<Hex, Method.Error, Transport>

The signature as Hex: 65 bytes, r then s then v.

The account whose key signs. The node or wallet must hold that key.

import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
declare const typedData: string;
const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const signature = Eth.signTypedDataV4(ADDRESS, typedData);
  • Type: string

The typed data, as JSON text. The object has four keys, defined by EIP-712:

Key Description
types the struct types, by name, each a list of name and type pairs, with EIP712Domain among them
primaryType the name of the struct type of message
domain what the signature is bound to: a name, a version, a chain id, a contract
message the data to sign
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const sign = (typedData: object) => Eth.signTypedDataV4(ADDRESS, JSON.stringify(typedData));
  • A string, not an object. Passing the object is a type error. The node parses the text itself.
  • Not checked here. ethect sends the text as it is. A text that is not JSON, or that names a type it does not define, is refused by the node.
  • Deterministic. The same account and typed data give the same signature.
  • Wallets show it. A wallet presents the fields of message to its user before signing, which is the point of typed data over an opaque hash.

The three errors every method can fail with. The cases specific to this method, as anvil 1.9 reports them:

Error Reason When
RpcError InvalidParams the text is not JSON, or uses a type that types does not define
RpcError InvalidParams the node holds no key for address

eth_signTypedData_v4