# Signature.recoverTypedData

Gives the address whose key signed EIP-712 typed data, as
[`Signer.signTypedData`](https://ethect.dev/reference/signer/sign-typed-data/) and `eth_signTypedData_v4` sign
it. It is arithmetic: no node is asked, and no service is needed.

## Usage

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

const permit: 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: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
    spender: "0x70997970C51812dc3A010C7d01b50e0d17dc79C8",
    value: 1_000_000n,
    nonce: 0n,
    deadline: 1_900_000_000n,
  },
};

const signer = await Signature.recoverTypedData(
  permit,
  "0x6b13efcf3f9fdce3e36de59e0208e5b91d85f17bfd9d7e6be21a74324d9be56b1794577252bbc1ea3bdc4fe5d3f9b8a00931fd7a07e02e4892c6acab1284e6011b",
).pipe(Effect.runPromise);
// "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266": the owner of the permit
```

It gives the address of the key that signed this typed data. One field that differs gives
another address, not a failure.

To check that an account signed, and not only a key, use
[`verifyTypedData`](https://ethect.dev/reference/signature/verify-typed-data/): a contract account has no key.

## Returns

`Effect<Address.Address, Signature.Error>`

## Parameters

### data

- **Type:** [`Signer.TypedData`](https://ethect.dev/reference/signer/sign-typed-data/#data)

The typed data that was signed: `domain`, `types`, `primaryType` and `message`.

### signature

- **Type:** `Hex | Signature.Signature`

The signature of a key: 65 bytes, or `r`, `s` and `yParity`. See
[a signature](https://ethect.dev/reference/signature/#a-signature).

## recoverTypedDataResult

The same as a `Result`, for code that is not inside an `Effect`.

```ts
import { Result } from "effect";
import { Signature, type Hex, type Signer } from "@ethect/core";

const signerOf = (order: Signer.TypedData, signature: Hex.Hex) =>
  Result.getOrUndefined(Signature.recoverTypedDataResult(order, signature));
```

- **Parameters:** `data`, `signature`, as above.
- **Returns:** `Result<Address.Address, Signature.Error>`. `recoverTypedData` is this function
  lifted into an `Effect`.

## Behavior

- **The typed data is hashed as the signer hashed it.** `EIP712Domain` is built from the
  fields the domain has, unless `types` declares it. An integer is a `bigint`, a safe `number`
  or digits in a string, decimal or hex.
- **The types are checked first.** Every type a field has is declared or one of Solidity's,
  the primary type is declared, and the message has every field. What is wrong is named in
  the failure: `Permit.deadline is missing`.
- **`v` is read as 0, 1, 27 or 28**, and an `s` in the upper half of the order as the same
  signature mirrored.
- **64 bytes fail.** See [64 bytes](https://ethect.dev/reference/signature/#64-bytes).

## Errors

| Error | When |
| --- | --- |
| `Signature.Error` | the typed data cannot be hashed: a type that is not declared, a field that is missing, a value that does not fit its type |
| `Signature.Error` | the signature is not 65 bytes of hex, or its `v` is not 0, 1, 27 or 28 |
| `Signature.Error` | its `r` or its `s` is zero or not below the order of the curve, or no key gives this signature |

`error.message` says which, and names the type or the field.

## Under the hood

- **your program**: no request: the EIP-712 hash of the typed data, then the recovery of a public key on secp256k1
