# Signer.signTypedData

Signs EIP-712 typed data with an account of the signer, such as a permit or an order. Nothing
is sent.

## Usage

```ts
import { Config, Effect, Layer } from "effect";
import { Signer } from "@ethect/core";

const Script = Layer.unwrap(Effect.map(Config.Redacted("PRIVATE_KEY"), Signer.privateKey));

const permit = (owner: string, spender: string, nonce: bigint): Signer.TypedData => ({
  domain: {
    name: "USD Coin",
    version: "2",
    chainId: 1,
    verifyingContract: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
  },
  types: {
    Permit: [
      { name: "owner", type: "address" },
      { name: "spender", type: "address" },
      { name: "value", type: "uint256" },
      { name: "nonce", type: "uint256" },
      { name: "deadline", type: "uint256" },
    ],
  },
  primaryType: "Permit",
  message: { owner, spender, value: 1_000_000n, nonce, deadline: 1_900_000_000n },
});

const program = Effect.gen(function* () {
  const owner = yield* Signer.address;
  return yield* Signer.signTypedData(
    permit(owner, "0x70997970c51812dc3a010c7d01b50e0d17dc79c8", 0n),
  );
});

const signature = await program.pipe(Effect.provide(Script), Effect.runPromise);
```

It needs a signer and no node.

## Returns

`Effect<Hex, Signer.Rejected | Signer.Error, Signer.Signer>`

The signature: `r ‖ s ‖ v`, 65 bytes, with `v` as 27 or 28.

## Parameters

### data

- **Type:** `Signer.TypedData`

| Property | Type | Holds |
| --- | --- | --- |
| `domain` | `Record<string, unknown>` | what the signature is for: `name`, `version`, `chainId`, `verifyingContract`, `salt`, each optional |
| `types` | `Record<string, ReadonlyArray<{ name: string; type: string }>>` | the structs, each a list of fields |
| `primaryType` | `string` | the struct that `message` is |
| `message` | `Record<string, unknown>` | the value to sign |

An integer is a `bigint`, a safe `number`, or digits in a string, decimal or hex: what JSON
carries is taken as it is.

### options.from (optional)

- **Type:** [`Address.Address`](https://ethect.dev/reference/primitives/address/)
- **Default:** the signer's first account

The account that signs.

## Behavior

- **The types are checked before anything is signed.** Every type a field has is declared or
  one of Solidity's, the primary type is declared, and the message has every field. A failure
  names the type or the field: `Permit.deadline is missing`.
- **`EIP712Domain` is built from the fields the domain has**, in the order of the standard,
  unless `types` declares it.
- **A domain field the standard does not define is refused.** It would be left out of the
  hash, and the signature would be for something else than what was shown.
- **The same key and data give the same signature.**
- **A wallet asks its user**, and shows the fields. A refusal fails with `Signer.Rejected`.

## Errors

| Error | Reason | When |
| --- | --- | --- |
| `Signer.Rejected` | | the user refused in the wallet, or on the device |
| `Signer.Error` | `"Unauthorized"` | the signer does not hold `from`, or holds no account |
| `Signer.Error` | `"Unknown"` | the data cannot be hashed: `message` says which type or field |
| `Signer.Error` | `"InvalidKey"` | what signed for a local signer is not the key of its address |

## Under the hood

- **a local key**: signed in the process: no request
- **Signer.fromSign**: one call of your `sign`, with the EIP-712 digest
- **Signer.make**: one call of the account's `signTypedData`, with the data
- **a wallet** `eth_signTypedData_v4(account, data as JSON)`
