# Signature.recover

Gives the address whose key signed a digest: 32 bytes, hashed the way the signer hashed them.
For a message or typed data, [`recoverMessage`](https://ethect.dev/reference/signature/recover-message/) and
[`recoverTypedData`](https://ethect.dev/reference/signature/recover-typed-data/) hash for you.

## Usage

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

const program = Effect.gen(function* () {
  return yield* Signature.recover(
    "0x5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c",
    "0x563bf02d856c1151500e047d2dd047a9b80a7f9d6a0337c82b641574145dc8514bc0bb75aabf368dbaeb97619c5888a069ad2d297f19c3ac7d75422fe1960d1b1b",
  );
});

const signer = await Effect.runPromise(program);
// "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"
```

It needs nothing: no node, no signer. The digest is not hashed again.

### From r, s and yParity

A signature a contract or a transaction holds in parts is passed as it is.

```ts
import { Signature, type Hash } from "@ethect/core";

const signerOf = (digest: Hash.Hash, r: bigint, s: bigint, yParity: 0 | 1) =>
  Signature.recover(digest, { r, s, yParity });
```

## Returns

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

The address of the key that signed. Another digest gives another address, not a failure.

## Parameters

### digest

- **Type:** [`Hash.Hash`](https://ethect.dev/reference/primitives/hash/)

What was signed: 32 bytes.

### 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).

## recoverResult

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

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

const signerOf = (digest: Hash.Hash, signature: Hex.Hex) =>
  Result.getOrUndefined(Signature.recoverResult(digest, signature));
```

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

## Behavior

- **Recover from a digest your program hashed, never from one it was sent.** For any address,
  anyone can make a signature that fits some digest.
- **It answers "which key", not "which account".** A contract account has no key: to check
  that an account signed, use [`verify`](https://ethect.dev/reference/signature/verify/).
- **`v` is read as 0, 1, 27 or 28**, and an `s` in the upper half of the order as the same
  signature mirrored. Each gives the same address.
- **64 bytes fail.** `r ‖ s` without `v` fits two keys.
  See [64 bytes](https://ethect.dev/reference/signature/#64-bytes).

## Errors

| Error | When |
| --- | --- |
| `Signature.Error` | the digest is not 32 bytes of hex |
| `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.

## Under the hood

- **your program**: no request: the recovery of a public key on secp256k1, then the last 20 bytes of its hash
