# Signature.verify

Says whether an account signed a digest. A key's signature is checked with no request. Any other is asked of the node in one eth_call, which covers a contract account, deployed or not yet.

Says whether an account signed a digest: 32 bytes, hashed the way the signer hashed them. For
a message or typed data, [`verifyMessage`](https://ethect.dev/reference/signature/verify-message/) and
[`verifyTypedData`](https://ethect.dev/reference/signature/verify-typed-data/) hash for you.

## Usage

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

const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");

const program = Signature.verify({
  address: ADDRESS,
  digest: "0x5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c",
  signature:
    "0x563bf02d856c1151500e047d2dd047a9b80a7f9d6a0337c82b641574145dc8514bc0bb75aabf368dbaeb97619c5888a069ad2d297f19c3ac7d75422fe1960d1b1b",
});

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

The examples below leave the transport out.

### At a block

A contract account can change who signs for it. Ask what it said at a block with
[`Call.withBlock`](https://ethect.dev/reference/call/with-block/).

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

const signedThen = (address: Address.Address, digest: Hash.Hash, signature: Hex.Hex) =>
  Signature.verify({ address, digest, signature }).pipe(
    Call.withBlock(19_000_000n),
  );
```

### An account that is not deployed yet

A smart wallet signs before its contract exists. Its signature then says how to deploy it
(ERC-6492), and nothing changes in the call: pass it as it is.

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

const signed = (wallet: Address.Address, digest: Hash.Hash, wrapped: Hex.Hex) =>
  Signature.verify({ address: wallet, digest, signature: wrapped });
```

## Returns

`Effect<boolean, Transport.Error, Transport.Transport>`

`true` when the account signed the digest, `false` when it did not.

## Parameters

### claim.address

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

The account that is said to have signed: a key, or a contract.

### claim.digest

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

What was signed: 32 bytes.

### claim.signature

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

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

## Behavior

- **Check a digest your program hashed, never one it was sent.** For any account, anyone can
  make a signature that fits some digest. Only the hash of a message you know proves a thing.
- **A key's signature costs no request.** When the signature recovers to the address, the
  answer is `true` and the node is not asked. Only a key can make such a signature, so it is
  right whatever is deployed at the address.
- **Anything else is asked of the node.** A contract has no key: it says itself whether a
  signature is its own, with `isValidSignature` (ERC-1271). A multisig, a smart wallet and an
  account behind a passkey are checked this way.
- **An account that is not deployed yet is checked too.** Its signature ends with the 32 bytes
  `6492…6492` and says how to deploy it (ERC-6492). The account is deployed for the length of
  the call, then asked. Nothing is sent, and nothing changes on the chain.
- **`false` is an answer.** The account refuses, the signature is of another key or of another
  digest, the account has no code and the bytes are no signature of a key.
- **What cannot be asked is `false`, with no request.** A signature that is not hex, a digest
  that is not 32 bytes, the zero address, which no key and no contract has.
- **A failure is no answer.** A node that cannot be reached, a call it refuses, or one that
  runs out of gas fails with a `Transport.Error`. It is never turned into `false`.
- **The latest block is asked**, or the one of `Call.withBlock`.
- **64 bytes are `r ‖ s`.** The signature is the account's when either parity recovers it.
  See [64 bytes](https://ethect.dev/reference/signature/#64-bytes).
- **One signature has several writings.** `v` as 0 or as 27, `s` in either half of the order:
  each is the same signature, and each is accepted. Do not remember a signature by its bytes
  to refuse it a second time. Remember what was signed: a nonce in the message.

## Errors

| Error | When |
| --- | --- |
| [`Transport.Error`](https://ethect.dev/reference/transport/) | the node does not answer |
| [`TransportError`](https://ethect.dev/reference/errors/transport-error/), `"MalformedResponse"` | the node answered the call with something else than one byte, `0x00` or `0x01`: it does not run a call with no `to` as a deployment |

## Under the hood

- the signature recovers to the address: no request → `true`
- otherwise:
  - **your node** `eth_call({ data }, block)`: with no `to`. `data` is the validator's code ‖ `abi(address, digest, signature)`
    - the validator runs as a deployment that is never made, and returns one byte:
      - the signature ends in `6492…6492`: unwrap it: `(factory, calldata, signature)`. No code at the address: call the factory, which deploys it
      - there is code at the address: `isValidSignature(digest, signature)` on it (ERC-1271)
      - there is none: `ecrecover`, which the first line already tried
    - `0x01` → `true`
    - `0x00`, or the call reverts → `false`
    - anything else, or no answer → fails: a failure

The validator is the reference implementation of
[ERC-6492](https://eips.ethereum.org/EIPS/eip-6492), from the text of the standard, compiled
into the library: 3.4 kB that only a program that calls `verify*` ships. The call goes through
[`Call.Reader`](https://ethect.dev/reference/call/reader/), alone: it is never put in a multicall.
