# Signature.verifyMessage

Says whether an account signed a message, as
[`Signer.signMessage`](https://ethect.dev/reference/signer/sign-message/) and `personal_sign` sign it (EIP-191).
The account can be a key or a contract, deployed or not yet.

## Usage

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

const program = (claimed: string, signature: Hex.Hex) =>
  Effect.gen(function* () {
    // an address that comes from outside is validated where it enters
    const address = yield* Address.make(claimed);
    return yield* Signature.verifyMessage({
      address,
      message: "Sign in to Example",
      signature,
    });
  });

const signedIn = await program(
  "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
  "0x8d532e72171f49ab25c4033b8b20a7b64566131ac0c7a534283dc786a4d8d99d3724bc048dc198c581f655d6bcb0922832a0666ae0321a1bdf82d6f75c66613f1b",
).pipe(
  Effect.provide(Transport.http("https://ethereum-rpc.publicnode.com")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
); // true
```

It needs a node, which it asks only when the signature is not the one of a key. The examples
below leave the transport out.

### Raw bytes

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

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

const signedTheOrder = (address: Address.Address, signature: Hex.Hex) => {
  const raw = Hash.keccak256Utf8("an order");
  return Signature.verifyMessage({ address, message: { raw }, signature });
};
```

`"0x1234"` as a text is six characters. `{ raw: "0x1234" }` is two bytes.

### An account that is a contract

Nothing changes: the same call says whether a multisig or a smart wallet signed. Write an
account of your own as a constant with
[`Address.makeUnsafe`](https://ethect.dev/reference/primitives/address/#makeunsafe).

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

// a multisig: it has no key, and says itself what it signed
const TREASURY = Address.makeUnsafe("0x849D52316331967b6fF1198e5E32A0eB168D039d");

const approvedByTreasury = (signature: Hex.Hex) =>
  Signature.verifyMessage({ address: TREASURY, message: "approved", signature });
```

### Without a node

A program with no node asks who signed, and compares. This is right for an account that is a
key, and says no to every contract account.

```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<boolean, Transport.Error, Transport.Transport>`

`true` when the account signed the message. `false` for everything else that is an answer:
another key, another message, an account that refuses, bytes that are no signature.

## Parameters

### claim.address

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

The account that is said to have signed.

### claim.message

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

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

### claim.signature

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

The signature: what the signer gave. See [a signature](https://ethect.dev/reference/signature/#a-signature).

## Behavior

- **What is checked is not the message alone.** It is the hash of
  `"\x19Ethereum Signed Message:\n"`, the length of the message in bytes, then the message:
  what `Signer.signMessage` and a wallet sign.
- **A key's signature costs no request.** The node is asked only for what a key did not sign.
- **`false` is an answer, a failure is not.** A node that cannot be reached, or that refuses
  the call, fails with a `Transport.Error`. It is never turned into `false`.
- **A message is not a session.** A signature is good forever for its message. Put a nonce of
  your own in the message, and use it once.

[`Signature.verify`](https://ethect.dev/reference/signature/verify/#behavior) has the rest: the block that is
asked, what a contract account is asked, and the zero address.

## Errors

| Error | When |
| --- | --- |
| [`Transport.Error`](https://ethect.dev/reference/transport/) | the node does not answer, or answers something that is no answer about the signature |

## Under the hood

- the signature recovers to the address: no request → `true`
- otherwise:
  - **your node** `eth_call`: one, with no `to`: the code of the ERC-6492 validator, with `(address, digest, signature)`

The digest is the EIP-191 hash of the message. The call is the one of
[`Signature.verify`](https://ethect.dev/reference/signature/verify/#under-the-hood).
