# Signature.recoverMessage

Gives the address whose key signed a message, as
[`Signer.signMessage`](https://ethect.dev/reference/signer/sign-message/) and `personal_sign` sign it (EIP-191).
It is arithmetic: no node is asked, and no service is needed.

## Usage

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

const program = Effect.gen(function* () {
  return yield* Signature.recoverMessage(
    "Sign in to Example",
    "0x8d532e72171f49ab25c4033b8b20a7b64566131ac0c7a534283dc786a4d8d99d3724bc048dc198c581f655d6bcb0922832a0666ae0321a1bdf82d6f75c66613f1b",
  );
});

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

It always gives an address when the bytes are a signature: the one of the key that made them
for this message. Another message gives another address, not a failure.

### Raw bytes

A text is read as its UTF-8 bytes. For a message that was signed as bytes, pass them as `raw`.

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

const signerOfTheOrder = (signature: Hex.Hex) => {
  const raw = Hash.keccak256Utf8("an order");
  return Signature.recoverMessage({ raw }, signature);
};
```

### Is it this account?

Compare with [`Address.equals`](https://ethect.dev/reference/primitives/address/#equals). It is right for an
account that is a key. A contract account has no key, and this says no to it:
[`verifyMessage`](https://ethect.dev/reference/signature/verify-message/) asks it.

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

const signedByKey = (address: Address.Address, signature: Hex.Hex) =>
  Effect.map(
    Signature.recoverMessage("Sign in to Example", signature),
    (signer) => Address.equals(signer, address),
  );
```

## Returns

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

The address of the key that signed, in lowercase as every
[`Address`](https://ethect.dev/reference/primitives/address/).

## Parameters

### message

- **Type:** `string | { readonly raw: Hex }`

The message: a text, or bytes as hex. It is what the signer was given, not its hash.

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

## recoverMessageResult

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

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

const recovered = Signature.recoverMessageResult(
  "Sign in to Example",
  "0x8d532e72171f49ab25c4033b8b20a7b64566131ac0c7a534283dc786a4d8d99d3724bc048dc198c581f655d6bcb0922832a0666ae0321a1bdf82d6f75c66613f1b",
);
const signer = Result.isSuccess(recovered) ? recovered.success : undefined;
```

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

## Behavior

- **What is recovered from is not the message alone.** It is the hash of
  `"\x19Ethereum Signed Message:\n"`, the length of the message in bytes, then the message.
- **`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).
- **Nothing is asked of anyone.** No node, no wallet, no service.

## Errors

| Error | When |
| --- | --- |
| `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 |
| `Signature.Error` | `raw` is not hex |

`error.message` says which.

## Under the hood

- **your program**: no request: the EIP-191 hash of the message, then the recovery of a public key on secp256k1
