# Signatures (Signature)

Checks a signature. Signature.recover gives the address whose key signed, with no node. Signature.verify says whether an account signed, also when the account is a contract.

Checks a signature: who signed a message, typed data or a digest, and whether an account did.

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

const Node = Transport.http("https://ethereum-rpc.publicnode.com").pipe(
  Layer.provide(FetchHttpClient.layer),
);

// the multisig that runs the app: a contract, with no key of its own
const ADMIN = Address.makeUnsafe("0x849D52316331967b6fF1198e5E32A0eB168D039d");

/** The message the server asked for: the nonce is its own, and is used once. */
const messageFor = (nonce: string) => `Sign in to Example\nNonce: ${nonce}`;

const signIn = (
  request: { readonly address: string; readonly signature: string },
  nonce: string,
) =>
  Effect.gen(function* () {
    const address = yield* Address.make(request.address);
    if (!Hex.isHex(request.signature)) return { signedIn: false, admin: false };
    const signedIn = yield* Signature.verifyMessage({
      address,
      message: messageFor(nonce),
      signature: request.signature,
    });
    return { signedIn, admin: signedIn && Address.equals(address, ADMIN) };
  }).pipe(Effect.provide(Node));
```

A sign-in on a server: a message, a signature, the address that claims it, and a boolean.

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

## Two questions

| Question | Functions | Needs |
| --- | --- | --- |
| Who signed? | [`recoverMessage`](https://ethect.dev/reference/signature/recover-message/), [`recoverTypedData`](https://ethect.dev/reference/signature/recover-typed-data/), [`recover`](https://ethect.dev/reference/signature/recover/) | nothing: it is arithmetic |
| Did this account sign? | [`verifyMessage`](https://ethect.dev/reference/signature/verify-message/), [`verifyTypedData`](https://ethect.dev/reference/signature/verify-typed-data/), [`verify`](https://ethect.dev/reference/signature/verify/) | a node, for an account that is a contract |

They are not the same question. An account can be a contract: a multisig, a smart wallet, an
account behind a passkey. It has no key. A key signs for it, and the contract says whether
that signature is its own.

- **To check that an account signed, use `verify*`.** It is right for every account.
  `recover*` gives the address of the key, which is not the account when the account is a
  contract.
- **Use `recover*` when the address is what you want**, or when the program has no node.
  Compare with [`Address.equals`](https://ethect.dev/reference/primitives/address/#equals). It is then right for
  accounts that are keys, and says no to every contract account.

## Exports

| Export | Kind | Description |
| --- | --- | --- |
| [`Signature.verifyMessage(claim)`](https://ethect.dev/reference/signature/verify-message/) | function | Whether an account signed a message. |
| [`Signature.verifyTypedData(claim)`](https://ethect.dev/reference/signature/verify-typed-data/) | function | Whether an account signed EIP-712 typed data. |
| [`Signature.verify(claim)`](https://ethect.dev/reference/signature/verify/) | function | Whether an account signed a digest. |
| [`Signature.recoverMessage(message, signature)`](https://ethect.dev/reference/signature/recover-message/) | function | The address whose key signed a message. |
| [`Signature.recoverTypedData(data, signature)`](https://ethect.dev/reference/signature/recover-typed-data/) | function | The address whose key signed typed data. |
| [`Signature.recover(digest, signature)`](https://ethect.dev/reference/signature/recover/) | function | The address whose key signed a digest. |
| [`Signature.recoverMessageResult`](https://ethect.dev/reference/signature/recover-message/#recovermessageresult), [`Signature.recoverTypedDataResult`](https://ethect.dev/reference/signature/recover-typed-data/#recovertypeddataresult), [`Signature.recoverResult`](https://ethect.dev/reference/signature/recover/#recoverresult) | function | The same three, as a `Result`. |
| [`Signature.fromHex(signature)`](https://ethect.dev/reference/signature/from-hex/) | function | Reads the 65 bytes of a signature as `r`, `s` and `yParity`. |
| [`Signature.fromHexResult(signature)`](https://ethect.dev/reference/signature/from-hex/#fromhexresult) | function | The same, as a `Result`. |
| [`Signature.toHex(signature)`](https://ethect.dev/reference/signature/from-hex/#tohex) | function | Writes `r`, `s` and `yParity` as 65 bytes. |
| [`Signature.Signature`](https://ethect.dev/reference/signature/from-hex/#signature) | type | `r`, `s` and `yParity`. |
| [`Signature.Error`](#failures) | error | What `recover*` and `fromHex` fail with. Its `_tag` is `"SignatureError"`. |

## A signature

Every function takes a signature in either form:

- **`Hex.Hex`**: what [`Signer.signMessage`](https://ethect.dev/reference/signer/sign-message/) and
  [`Signer.signTypedData`](https://ethect.dev/reference/signer/sign-typed-data/) give, and what a wallet gives.
  For a key it is 65 bytes, `r ‖ s ‖ v`. For a contract account it is whatever the contract
  reads, of any length.
- **`Signature.Signature`**: the `r`, `s` and `yParity` of a key's signature.

| Bytes | Read as |
| --- | --- |
| 65, `v` as 27 or 28 | a signature, as a signer gives it |
| 65, `v` as 0 or 1 | the same signature: `v` is the parity itself |
| 65, `s` in the upper half of the order | the same signature, mirrored. It recovers the same address |
| 64 | `r ‖ s` with no `v`. See below |
| anything else | no signature of a key. `verify*` asks the account, when it is a contract |

### 64 bytes

A signature of 64 bytes is `r ‖ s`: what a KMS or an HSM gives, and what
[`Signer.fromSign`](https://ethect.dev/reference/signer/from-sign/) takes. It does not say which of two keys
signed.

- `recover*` fails on it: two addresses fit, and it cannot choose.
- `verify*` checks it: the account you name is one of the two, or it is not.

It is not read as an EIP-2098 compact signature, which packs the parity in the first bit of
`s`. Read that way, the 64 bytes of a KMS would recover the wrong address half the time, with
no failure. Unpack a compact signature to 65 bytes before you pass it.

## Failures

`recover*` and `fromHex` fail with `Signature.Error`. It has a `message`, which says what was
wrong: the bytes are no signature, no key gave them, or what was signed cannot be hashed.

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

const signerOf = (signature: Hex.Hex) =>
  Signature.recoverMessage("Sign in to Example", signature).pipe(
    Effect.catchTag("SignatureError", (error) =>
      Effect.succeed(`no signer: ${error.message}`),
    ),
  );
```

`verify*` never fails with it. A signature that is malformed, or that the account refuses, is
`false`: that is the answer to the question. It fails only when the node does not answer,
with a [`Transport.Error`](https://ethect.dev/reference/transport/).
